
$(document).ready(function(){
  // bind change function to Type of Enquiry dropdown
  $('#f_country').change( function() { changeFormCountry(); });
  $('#region').hide(0);
  $('#f_enquiry').change( function() { changeEnquiry(); });
  
   $("#_techenquiry").submit(function() {
      return validateTechenquiryForm();
    });

});

function validateTechenquiryForm() {
  //alert($("textarea[name=detailenquiry]").val());
  // Check Email field
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")

/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
emailStr = $("input[name=email]").val();
var matchArray=emailStr.match(emailPat)

if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	alert("Email address seems incorrect (check @ and .'s)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    alert("The email username doesn't seem to be valid.")
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Destination IP address for email is invalid!")
		return false
	    }
    }
    //return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("The email domain name doesn't seem to be valid.")
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   alert("The email address must end in a three-letter domain, or two letter country.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!"
   alert(errStr)
   return false
}
	
   
   // Start checking other fields
	//form = document.forms[0];
//	var os_input = "";
//	var os1_input = "";
	var cable_input = "";
	var info_input = "";
	var software_input = "";
	var collect_input = "";
	var message = "";
	
	if ($("input[name=familyname]").val() == "") {
		message = message + "Please enter Family Name!\n";}
		
	if ($("input[name=givenname]").val() == "") {
		message = message + "Please enter Given Name!\n"; }
		
	if ($("input[name=Email]").val() == "") {
		message = message + "Please enter Email!\n"; }
		
	if ($("input[name=contact]").val() == "") {
		message = message + "Please enter Contact Number!\n"; }
		
	if ($("input[name=country]").val() == "Choose your Country") {
		message = message + "Please select a Country!\n";}
		
	if ($("input[name=enquiry]").val() == "Choose your Type of Enquiry") {
		message = message + "Please select your Type of Enquiry!\n";}
	
/*
	for (i=0;i<document.forms[0].os.length;i++) {
	if (document.forms[0].os[i].checked) {
		os_input = document.forms[0].os[i].value;
	}
} */
	if (($("input[name=enquiry]").val() == "Technical Troubleshooting") && ($("input[name=os]").val() == "Choose your Operating System")) {
		message = message + "Please select your Operating System!\n";}
		
	/*for (i=0;i<document.forms[0].cable.length;i++) {
	if (document.forms[0].cable[i].checked) {
		cable_input = document.forms[0].cable[i].value;
	}
}	*/
	if (($("input[name=enquiry]").val() == "Technical Troubleshooting") && ($("input[name=cable]:checked").length == 0)) {
		message = message + "Please select your Cable Type of Connection!\n";}

	if (($("input[name=enquiry]").val() == "Service & Repair") && ($("input[name=serialno1]").val() == "")) {
		message = message + "Please enter your Product Serial Number!\n";}
		
	/*for (i=0;i<document.forms[0].info.length;i++) {
	if (document.forms[0].info[i].checked) {
		info_input = document.forms[0].info[i].value;
	}
}	*/
	if (($("input[name=enquiry]").val() == "Presales Enquiry") && ($("input[name=info]:checked").length == 0)) {
		message = message + "Please select which information you would like to enquire!\n";}

	/*for (i=0;i<document.forms[0].software1.length;i++) {
	if (document.forms[0].software1[i].checked) {
		software_input = document.forms[0].software1[i].value;
	}
}	*/
	if (($("input[name=enquiry]").val() == "Request for Epson Software/Driver") && ($("input[name=software1]:checked").length == 0)) {
		message = message + "Please select your Type of software!\n";}
		
	if (($("input[name=enquiry]").val() == "Request for Epson Software/Driver") && ($("input[name=model3]").val() == "")) {
		message = message + "Please enter your Product Model!\n";}
		
/*	for (i=0;i<document.forms[0].os1.length;i++) {
	if (document.forms[0].os1[i].checked) {
		os1_input = document.forms[0].os1[i].value;
	}
} */
	if (($("input[name=enquiry]").val() == "Request for Epson Software/Driver") && ($("input[name=os1]").val() == "Choose your Operating System")) {
		message = message + "Please select your Operating System!\n";}
	
/*	for (i=0;i<document.forms[0].collect.length;i++) {
	if (document.forms[0].collect[i].checked) {
		collect_input = document.forms[0].collect[i].value;
	}
} */	
	if (($("input[name=enquiry]").val() == "Request for Epson Software/Driver") && ($("input[name=address1]").val() == "") ) {
		message = message + "Please enter your Local Postage!\n";}
		
	if  (($("input[name=enquiry]").val() != "Compliment") && ($("textarea[name=detailenquiry]").val() == "")) {
		message = message + "Please enter Detailed Description of Enquiry!\n"; }	
		
			if (message != "") {
		alert (message);}
	else {
	    //document.getElementById("_techenquiry").submit();
		/*
		day=new Date();
		id=day.getTime();			   
		eval("page" + id + " = window.open('http://www.epson.com.sg/epson_singapore/error.page', '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,directories=0,width=350,height=200,left = 280,top = 285');");
		widows.location(location);	*/
	  //emailSendingProcess();
	return true; 
	}
		
		
		
  return false;
}

/********Show country form based on selection*********/
function changeFormCountry(){
  var country_type = $("input[name=customerCountry]").val();
  
  if(country_type === 'Malaysia' || country_type === 'India'){
    $('#region').show(0);
  }else{
    $('#region').hide(0);
  }  
}

/********Show enquiry form based on selection*********/
function changeEnquiry(){
  var enquiry_type = $("input[name=enquiry]").val();
  hideAllEnquiryForms();
  
  if(enquiry_type === 'Technical Troubleshooting'){
    $('#Technical_Troubleshooting').show(0);
  } else if(enquiry_type === 'Service & Repair'){
    $('#Service_Repair').show(0);
  } else if(enquiry_type === 'Presales Enquiry'){
    $('#Presales_Enquiry').show(0);
  } else if(enquiry_type === 'Complaint'){
    $('#Complaint').show(0);
  }  else if(enquiry_type === 'Compliment'){
    $('#Compliment').show(0);
    $('#f_detailenquiry_box').hide(0);
    $('#f_attachment_box').hide(0);
  } else if(enquiry_type === 'General/Others'){
    $('#General_Others').show(0);
  } else if(enquiry_type === 'Request for Epson Software/Driver'){
    $('#Request_for_Epson_Software_Driver').show(0);
  }
}

/********Hide all enquiry forms*********/
function hideAllEnquiryForms(){
  $('#Technical_Troubleshooting').hide(0);
  $('#Service_Repair').hide(0);
  $('#Presales_Enquiry').hide(0);
  $('#General_Others').hide(0);
  $('#Compliment').hide(0);
  $('#Complaint').hide(0);
  $('#Request_for_Epson_Software_Driver').hide(0);
  $('#f_detailenquiry_box').show(0);
  $('#f_attachment_box').show(0);
}

function showIndustryType(hide){
  if(hide){
    $('#type_of_industry').show(0);
  } else {
    $('#type_of_industry').hide(0);
  }
}
