// JavaScript Document

// Declaring required variables and functions to check phone number
	var digits = "0123456789";
	// non-digit characters which are allowed in phone numbers
	var phoneNumberDelimiters = "()-.+ ";
	// characters which are allowed in international phone numbers
	// (a leading + is OK)
	var validWorldPhoneChars = phoneNumberDelimiters + "+";
	// Minimum no of digits in an international phone no.
	var minDigitsInIPhoneNumber = 10;

	function isInteger(s)
	{   var i;
    	for (i = 0; i < s.length; i++)
	    {   
	        // Check that current character is number.
	        var c = s.charAt(i);
	        if (((c < "0") || (c > "9"))) return false;
	    }
	    // All characters are numbers.
	    return true;
	}
	function trim(s)
	{   var i;
	    var returnString = "";
	    // Search through string's characters one by one.
	    // If character is not a whitespace, append to returnString.
	    for (i = 0; i < s.length; i++)
	    {   
	        // Check that current character isn't whitespace.
	        var c = s.charAt(i);
	        if (c != " ") returnString += c;
	    }
	    return returnString;
	}
	function stripCharsInBag(s, bag)
	{   var i;
	    var returnString = "";
    	// Search through string's characters one by one.
	    // If character is not in bag, append to returnString.
    	for (i = 0; i < s.length; i++)
    	{   
    	    // Check that current character isn't whitespace.
        	var c = s.charAt(i);
        	if (bag.indexOf(c) == -1) returnString += c;
    	}
	    return returnString;
	}

	function checkInternationalPhone(strPhone){
	var bracket=3
	strPhone=trim(strPhone)
	if(strPhone.indexOf("+")>1) return false
	if(strPhone.indexOf("-")!=-1)bracket=bracket+1
	if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
	var brchr=strPhone.indexOf("(")
	if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
	if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
	}

// Declaring required variables and functions to check phone number
	function echeck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}
		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		}
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }
		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		 if (str.indexOf(" ")!=-1){
		    return false
		 }
 		 return true					
	}
	
// Form validation function
function checkForm()
{
	request = false;
	fname = document.getElementById("First_Name").value;
	lname = document.getElementById("Last_Name").value;
	email = document.getElementById("from").value;
	company = document.getElementById("Company").value;
	industry = document.getElementById("Industry").value;
	Phone = document.getElementById("Phone").value;
	frequency = false;
	part = document.getElementById("Customer_Part_Num").value;
	qty = document.getElementById("Qty_Req").value;
	delmonth = document.getElementById("Delivery_Month").value;
	delday = document.getElementById("Delivery_Day").value;
	delyear = document.getElementById("Delivery_Year").value;
	mat = document.getElementById("Materials").value;
	
	// Loop from zero to the one minus the number of radio button selections
	// Used to validate the Request_Type field
	for (counter = 0; counter < document.registerForm.Request_Type.length; counter++) {
		// If a radio button has been selected it will return true
		// (If not it will return false)
		if (document.registerForm.Request_Type[counter].checked)
		request = true; 
		}

	// Loop from zero to the one minus the number of radio button selections
	// Used to validate the Order_Frequency field
	for (fcounter = 0; fcounter < document.registerForm.Order_Freq.length; fcounter++) {
		// If a radio button has been selected it will return true
		// (If not it will return false)
		if (document.registerForm.Order_Freq[fcounter].checked)
		frequency = true; 
		}
	
	// Validate request type 
	if (!request) {
	hideAllErrors();
		document.getElementById("typeError").style.display = "inline";
		document.getElementById("info").select();
		document.getElementById("info").focus();
	  return false;
	  }
	// Validate first name 
	  else if (fname == "") {
		hideAllErrors();
		document.getElementById("fnameError").style.display = "inline";
		document.getElementById("First_Name").select();
		document.getElementById("First_Name").focus();
	  return false;
	  }
	// Validate last name 
	  else if (lname == "") {
		hideAllErrors();
		document.getElementById("lnameError").style.display = "inline";
		document.getElementById("Last_Name").select();
		document.getElementById("Last_Name").focus();
	  return false;
	  }
	// Validate email 
	  else if (email == "") {
		hideAllErrors();
		document.getElementById("emailError").style.display = "inline";
		document.getElementById("from").select();
		document.getElementById("from").focus();
	  return false;
	  }
	  else if (echeck(email)==false) {
		hideAllErrors();
		email=""
		document.getElementById("emailError2").style.display = "inline";
		document.getElementById("from").select();
		document.getElementById("from").focus();
	  return false;
	  }
	// Validate company 
	  else if (company == "") {
		hideAllErrors();
		document.getElementById("coError").style.display = "inline";
		document.getElementById("Company").select();
		document.getElementById("Company").focus();
	  return false;
	  }
	// Validate industry 
	  else if (industry == "") {
		hideAllErrors();
		document.getElementById("industryError").style.display = "inline";
		document.getElementById("Industry").select();
		document.getElementById("Industry").focus();
	  return false;
	  }
	// Validate phone 
	  else if (Phone=="") {
		hideAllErrors();
		document.getElementById("phoneError").style.display = "inline";
		document.getElementById("Phone").select();
		document.getElementById("Phone").focus();
	  return false;
	  }
	  else if (checkInternationalPhone(Phone)==false) {
		hideAllErrors();
		document.getElementById("phoneError2").style.display = "inline";
		document.getElementById("Phone").select();
		document.getElementById("Phone").focus();
	  return false;
	  }
	  // Validate frequency 
	  else if ((document.registerForm.Request_Type[1].checked) && (!frequency)) {
		hideAllErrors();
		document.getElementById("freqError").style.display = "inline";
		document.getElementById("once").select();
		document.getElementById("once").focus();
	  return false;
	  }
	  // Validate part number 
	  else if ((document.registerForm.Request_Type[1].checked) && (part == "")) {
		hideAllErrors();
		document.getElementById("partError").style.display = "inline";
		document.getElementById("Customer_Part_Num").select();
		document.getElementById("Customer_Part_Num").focus();
	  return false;
	  }
	  // Validate qty 
	  else if ((document.registerForm.Request_Type[1].checked) && (qty == "")) {
		hideAllErrors();
		document.getElementById("qtyError").style.display = "inline";
		document.getElementById("Qty_Req").select();
		document.getElementById("Qty_Req").focus();
	  return false;
	  }
	  // Validate delivery month 
	  else if ((document.registerForm.Request_Type[1].checked) && (delmonth == "0")) {
		hideAllErrors();
		document.getElementById("delError1").style.display = "inline";
		document.getElementById("Delivery_Month").focus();
	  return false;
	  }
	  // Validate delivery day 
	  else if ((document.registerForm.Request_Type[1].checked) && (delday == "0")) {
		hideAllErrors();
		document.getElementById("delError2").style.display = "inline";
		document.getElementById("Delivery_Day").focus();
	  return false;
	  }		  
	  // Validate delivery year 
	  else if ((document.registerForm.Request_Type[1].checked) && (delyear == "0")) {
		hideAllErrors();
		document.getElementById("delError3").style.display = "inline";
		document.getElementById("Delivery_Year").focus();
	  return false;
	  }
	  // Validate materials 
	  else if ((document.registerForm.Request_Type[1].checked) && (mat == "")) {
		hideAllErrors();
		document.getElementById("matError").style.display = "inline";
		document.getElementById("Materials").select();
		document.getElementById("Materials").focus();
	  return false;
	  }
  hideAllErrors();  
  return true;
}
 
function hideAllErrors() {
	document.getElementById("typeError").style.display = "none"
	document.getElementById("fnameError").style.display = "none"
	document.getElementById("lnameError").style.display = "none"
	document.getElementById("emailError").style.display = "none"
	document.getElementById("emailError2").style.display = "none"
	document.getElementById("coError").style.display = "none"
	document.getElementById("industryError").style.display = "none"
	document.getElementById("phoneError").style.display = "none"
	document.getElementById("phoneError2").style.display = "none"
	document.getElementById("freqError").style.display = "none"
	document.getElementById("partError").style.display = "none"
	document.getElementById("qtyError").style.display = "none"
	document.getElementById("freqError").style.display = "none"
	document.getElementById("delError1").style.display = "none"
	document.getElementById("delError2").style.display = "none"
	document.getElementById("delError3").style.display = "none"
	document.getElementById("matError").style.display = "none"
  }
