// JavaScript Document

function validation()
{
	if(trim(document.contactus.firstname.value) == "")
	{
		alert("Please enter the first name");
		document.contactus.firstname.focus();
		return false;
	}
	if(ischar(document.contactus.firstname.value) == false)
	{
		alert("Please enter only alphabets for first name");
		document.contactus.firstname.focus();
		return false;	
	}
	if(trim(document.contactus.lastname.value) == "")
	{
		alert("Please enter the last name");
		document.contactus.lastname.focus();
		return false;
	}
	if(ischar(document.contactus.lastname.value) == false)
	{
		alert("Please enter only alphabets for last name");
		document.contactus.lastname.focus();
		return false;	
	}
		if (trim(document.contactus.email.value) == "")
	{
		alert("Please enter the valid email address  ");
		document.contactus.email.focus();
		return false; 
	}
    if(isEmail(trim(document.contactus.email.value)) == false )
	{
		alert("Please Enter the valid email address ");
		document.contactus.email.focus();
		return false; 
	}
	var Phone=document.contactus.phone
	
	

     if ((Phone.value==null)||(Phone.value=="")){
         alert("Please enter the Phone Number in a valid format (###-###-####)")
         Phone.focus()

         return false
     }
	
	 var checkOK = "0123456789-";
   var checkStr = trimString(Phone.value);

  var allValid = true;
   var decPoints = 0;
   var allNum = "";
   for (i = 0;  i < checkStr.length;  i++)
   {
     ch = checkStr.charAt(i);
     for (j = 0;  j < checkOK.length;  j++)
       if (ch == checkOK.charAt(j))
         break;
     if (j == checkOK.length)
     {
       allValid = false;
       break;
     }
     allNum += ch;
   }

   if (!allValid)
   {
     alert("Please enter the phone number as numeric");
     Phone.focus();
     return false;
   }


   bill_billing_phone=trim(Phone.value);

   if(trimString(Phone.value).length == 12)
     {


for(phone_length=0;phone_length<trimString(Phone.value)
.length;phone_length++)
         {

             if((phone_length == 3) || (phone_length == 7))
            {

                 if(bill_billing_phone.charAt(phone_length) == "-")
                 {
                 }
                 else
                 {
                     alert("Please enter the phone number in a valid format (###-###-####) ");
                     Phone.focus();
                     return false;
                 }
             }
         }
     }
     else
     {
             alert("Please enter the phone number in a valid format (###-###-####)");
             Phone.focus();
             return false;
     }
	
	
	
	
	
	
	if(trim(document.contactus.city.value) == "")
	{
		alert("Please enter the city");
		document.contactus.city.focus();
		return false;
	}
	
	if(ischar(document.contactus.city.value) == false)
	{
		alert("Please enter only alphabets for city");
		document.contactus.city.focus();
		return false;	
	}
	
	if(document.contactus.stateid.value == "0")
	{
		alert("Please select the state");
		document.contactus.stateid.focus();
		return false;	
	}
		
	
	if(trim(document.contactus.zipcode.value) == "")
	{
		alert("Please enter the valid postal code");
		document.contactus.zipcode.focus();
		return false;	
	}
	
	if(IsNumeric(trim(document.contactus.zipcode.value))== false)
	{
		alert("Please enter the valid postal code in numeric");
		document.contactus.zipcode.focus();
		return false;
	}
	if(document.contactus.zipcode.value.length < 5)
	{
		alert("Please enter valid postal code");
		document.contactus.zipcode.focus();
		return false;
	}
     var checkOK = "0123456789-";
   var checkStr = trimString(Phone.value);

  var allValid = true;
   var decPoints = 0;
   var allNum = "";
   for (i = 0;  i < checkStr.length;  i++)
   {
     ch = checkStr.charAt(i);
     for (j = 0;  j < checkOK.length;  j++)
       if (ch == checkOK.charAt(j))
         break;
     if (j == checkOK.length)
     {
       allValid = false;
       break;
     }
     allNum += ch;
   }

   if (!allValid)
   {
     alert("Please enter the phone number in a valid format (###-###-####)");
     Phone.focus();
     return false;
   }


   bill_billing_phone=trim(Phone.value);

   if(trimString(Phone.value).length == 12)
     {


for(phone_length=0;phone_length<trimString(Phone.value)
.length;phone_length++)
         {

             if((phone_length == 3) || (phone_length == 7))
            {

                 if(bill_billing_phone.charAt(phone_length) == "-")
                 {
                 }
                 else
                 {
                     alert("Please enter the phone number in a valid format (###-###-####) ");
                     Phone.focus();
                     return false;
                 }
             }
         }
     }
     else
     {
             alert("Please enter the phone number in a valid format (###-###-####) ");
             Phone.focus();
             return false;
     }
	 document.contactus.method="POST";
document.contactus.action="contact_mail.php";
document.contactus.submit()	
return true;

}
 
   function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char; 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;   
   }	
 function trimString (str)
 {
   str = this != window? this : str;
   return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
 }

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") || (c > "-"))) return false;
    }
    // All characters are numbers.
    return true;
}

function trim(s) {
	while (s.substring(0,1) == ' ') {
	s = s.substring(1,s.length);
  	}
	while (s.substring(s.length-1,s.length) == ' ') {
 	s = s.substring(0,s.length-1);
  	}
	 return s;		
}	

function ischar(svalue)
{
	var checkOK = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var checkStr = svalue;
	var allValid = true;
	var decPoints = 0;
	var allNum = "";
	
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length)
		{
			//alert(msg);
			//eval("document."+frmpage+"."+ctrlname+"."+"focus();")
			return false;
			break;
		}
			allNum += ch;
	}
}

function isEmail(email) {
    invalidChars = " ~\'^\`\"*+=\\|][(){}$&!#%/:,;";
    // Check for invalid characters as defined above
    for (i=0; i<invalidChars.length; i++) {
        badChar = invalidChars.charAt(i);
        if (email.indexOf(badChar,0) > -1) {
            return false;
        }
    }
    lengthOfEmail = email.length;
    if ((email.charAt(lengthOfEmail - 1) == ".") || (email.charAt(lengthOfEmail - 2) == ".")) {
        return false;
    }
    Pos = email.indexOf("@",1);
    if (email.charAt(Pos + 1) == ".") {
        return false;
    }
    while ((Pos < lengthOfEmail) && ( Pos != -1)) {
        Pos = email.indexOf(".",Pos);
        if (email.charAt(Pos + 1) == ".") {
            return false;
        }
        if (Pos != -1) {
            Pos++;
        }
    }
    // There must be at least one @ symbol
    atPos = email.indexOf("@",1);
    if (atPos == -1) {
        return false;
    }
    // But only ONE @ symbol
    if (email.indexOf("@",atPos+1) != -1) {
        return false;
    }
    // Also check for at least one period after the @ symbol
    periodPos = email.indexOf(".",atPos);
    if (periodPos == -1) {
        return false;
    }
    if (periodPos+3 > email.length) {
        return false;
    }
    return true;
}
