
function lTrim(str) {
   var re;
   re = /^\s*/;
   str = (str.replace(re, ""));
   return str;
}
function rTrim(str) {
   var len = str.length;
   var cnt = len - 1;
   while (str.charAt(cnt) == " ") {
      cnt = cnt - 1;
      if (cnt <= 0) {
         break;
      }
   }
   str = str.slice(0, cnt + 1);
   return str;
}
function trimString(str) {
   return lTrim(rTrim(str));
}
function isNumber(str) {
   var strReg = /^([0-9])+$/;
   str = trimString(str);
   if (str.length > 0) {
      return (strReg.test(str));
   }
   return true;
}

function setAddress() {
   document.frm_contactus.street.value = trimString(trimString(document.frm_contactus.address1.value) + " " + trimString(document.frm_contactus.address2.value));
}
function setCaptureDate() {
   var currentTime = new Date();
   var month = currentTime.getMonth() + 1;
   var day = currentTime.getDate();
   var year = currentTime.getFullYear();
   var captureDate = month + "/" + day + "/" + year;
   document.write("<input type='hidden' name='00N60000001KYVO' value='" + captureDate + "'>");
}
function validateData() {
   var errorCount = 0;
   var firstnameError = false;
   var lastnameError = false;
   var companyError = false;
   var emailError = false;
   var phoneError = false;
   var zipError = false;

    // Reset error message
   document.getElementById("errs").innerHTML = "";
   document.getElementById("fn_label").style.color = "";
   document.getElementById("fn_label").style.fontWeight = "";
   document.getElementById("last_name_label").style.color = "";
   document.getElementById("last_name_label").style.fontWeight = "";
   document.getElementById("company_label").style.color = "";
   document.getElementById("company_label").style.fontWeight = "";
   document.getElementById("email_label").style.color = "";
   document.getElementById("email_label").style.fontWeight = "";
   document.getElementById("phone_label").style.color = "";
   document.getElementById("phone_label").style.fontWeight = "";
   document.getElementById("zip_label").style.color = "";
   document.getElementById("zip_label").style.fontWeight = ""; 
   document.getElementById("country_label").style.color = "";
   document.getElementById("country_label").style.fontWeight = "";
   document.getElementById("state_label").style.color = "";
   document.getElementById("state_label").style.fontWeight = "";
   setAddress();
   if (document.frm_contactus.first_name.value == "") {
      document.getElementById("fn_label").style.color = "#bd3824";
      document.getElementById("fn_label").style.fontWeight = "bold";
      document.frm_contactus.first_name.focus();
      errorCount++;
   } else {
      var check = 0;
      var str = trimString(document.frm_contactus.first_name.value);
        var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]\*\`\&\$\/\{\}]/ ;
        if (str.match(illegalChars)) {
         firstnameError = true;
      }
   }
   if (document.frm_contactus.last_name.value == "") {
      document.getElementById("last_name_label").style.color = "#bd3824";
      document.getElementById("last_name_label").style.fontWeight = "bold";
      document.frm_contactus.last_name.focus();
      errorCount++;
   } else {
      var str = trimString(document.frm_contactus.last_name.value);
        var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]\*\`\&\$\/\{\}]/ ;
        if (str.match(illegalChars)) {
         lastnameError = true;
      }
   }
   if (document.frm_contactus.company.value == "") {
      document.getElementById("company_label").style.color = "#bd3824";
      document.getElementById("company_label").style.fontWeight = "bold";
      document.frm_contactus.company.focus();
      errorCount++;
   } else {
      var str = trimString(document.frm_contactus.company.value);
        var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]\*\`\&\$\/\{\}]/ ;
        if (str.match(illegalChars)) {
         companyError = true;
      }
   }
   
   if (document.frm_contactus.email.value == "") {
      document.getElementById("email_label").style.color = "#bd3824";
      document.getElementById("email_label").style.fontWeight = "bold";
      document.frm_contactus.email.focus();
      errorCount++;
   } else {
      var check = 0;
      var str = trimString(document.frm_contactus.email.value);
      var emailFilter=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if (!(emailFilter.test(str))) {
         emailError = true;
      }
   }
   
   if (document.frm_contactus.phone.value == "") {
      document.getElementById("phone_label").style.color = "#bd3824";
      document.getElementById("phone_label").style.fontWeight = "bold";
      document.frm_contactus.phone.focus();
      errorCount++;
   } 
   else if (document.frm_contactus.country.value != "" && document.frm_contactus.country.value == "US"){
      var st = document.frm_contactus.phone.value;
      st=st.replace(new RegExp("([\\s])+", "g"),"");
      st=st.replace(new RegExp("([\-\.\(\)])+", "g"),"");      
      var tempst = /^\d{10}$/;
      if (tempst.test(st) == false) {
         phoneError = true;
      }
      else
      {
         document.frm_contactus.phone.value=st.substring(0,3)+"-"+st.substring(3,6)+"-"+st.substring(6,10);
      }
   }
   else
   {
      var st = document.frm_contactus.phone.value;
      st=st.replace(new RegExp("([\\s])+", "g"),"");      
      var tempst = /^([0-9\.\-\+\(\)])+$/;
      if (tempst.test(st) == false) {
         phoneError = true;
      }   
      else
      {
         document.frm_contactus.phone.value=st;
      }
   }
   
   if (document.frm_contactus.zip.value !="")
    {    
	  if (document.frm_contactus.country.value != "" && document.frm_contactus.country.value == "US"){    
	      var st = document.frm_contactus.zip.value;
	      var tempst=/^\d{5}-\d{4}$/;
	      var tempst2=/^\d{5}$/;
	      if(tempst.test(st)==false && tempst2.test(st)==false)
	      {
	          zipError=true;
	      }
      }
    }
   
   if (document.frm_contactus.country.value == "") {
      document.getElementById("country_label").style.color = "#bd3824";
      document.getElementById("country_label").style.fontWeight = "bold";
      document.frm_contactus.country.focus();
      errorCount++;
   }
   
   if (document.frm_contactus.country.value != "" && document.frm_contactus.country.value == "US" || document.frm_contactus.country.value == "Canada") {
      if (document.frm_contactus.state.selectedIndex == 0) {
         document.getElementById("state_label").style.color = "#bd3824";
         document.getElementById("state_label").style.fontWeight = "bold";
         document.frm_contactus.state.focus();
         errorCount++;
      }
   }
   
   if (!document.frm_contactus.emailOptOut[0].checked && !document.frm_contactus.emailOptOut[1].checked) {
      errorCount++;
   }
   if (errorCount > 0) {
      document.getElementById("errs").innerHTML = "<p style='color: #bd3824; padding: 0'> <b>Please complete all required fields before submitting your request for information.<br>  Thank you! </b></p>";
      return false;
   }
   if (firstnameError) {
      document.getElementById("fn_label").style.color = "#bd3824";
      document.getElementById("fn_label").style.fontWeight = "bold";
      document.getElementById("errs").innerHTML = "<p style='color: #bd3824; padding: 0'> <b>The first name contains one or more illegal characters: ( ) < > , ; : \ \" [ ] * ` & $ / { } </b></p>";
      document.frm_contactus.first_name.focus();
      return false;
   }
   if (lastnameError) {
      document.getElementById("last_name_label").style.color = "#bd3824";
      document.getElementById("last_name_label").style.fontWeight = "bold";
      document.getElementById("errs").innerHTML = "<p style='color: #bd3824; padding: 0'> <b>The last name contains one or more illegal characters: ( ) < > , ; : \ \" [ ] * ` & $ / { } </b></p>";
      document.frm_contactus.last_name.focus();
      return false;
   }
   if (companyError) {
      document.getElementById("company_label").style.color = "#bd3824";
      document.getElementById("company_label").style.fontWeight = "bold";
      document.getElementById("errs").innerHTML = "<p style='color: #bd3824; padding: 0'> <b>The company name contains one or more illegal characters: ( ) < > , ; : \ \" [ ] * ` & $ / { } </b></p>";
      document.frm_contactus.company.focus();
      return false;
   }
   if (emailError) 
   {
      document.getElementById("email_label").style.color = "#bd3824";
      document.getElementById("email_label").style.fontWeight = "bold";
      document.getElementById("errs").innerHTML = "<p style='color: #bd3824; padding: 0'> <b>Please enter a valid email address. </b></p>";
      document.frm_contactus.email.focus();
      return false;
   }
   
   if (phoneError) 
   {
      document.getElementById("phone_label").style.color = "#bd3824";
      document.getElementById("phone_label").style.fontWeight = "bold";
      document.getElementById("errs").innerHTML = "<p style='color: #bd3824; padding: 0'> <b>Please provide a valid phone no. (EX:333-333-3333)</b></p>";
      document.frm_contactus.phone.focus();
      return false;
   }
   
   if (zipError)
   {
       document.getElementById("zip_label").style.color = "#bd3824";
       document.getElementById("zip_label").style.fontWeight = "bold";
       document.getElementById("errs").innerHTML="<p style='color: #bd3824; padding: 0'> <b>Please provide a valid zipcode. (EX:12345-6789 or 12345)</b></p>";
       document.frm_contactus.zip.focus();       
       return false;
   } 
   return true;
}