function validateForm(whichbutton) {
    var btton = whichbutton;
    if (btton == "contact_form") {
        with (document.form2Validate) {
            var alertMsg = "The following REQUIRED fields\nhave been left empty or contain an error:\n";

            if ((Subject.value == null) || (Subject.value == "")) alertMsg += "\n Subject";
            if ((Description.value == null) || (Description.value == "")) alertMsg += "\n Description";
            if ((phone.value == null) || (phone.value == "") || (IsPhoneLength(phone.value)==false)) alertMsg += "\n Phone";
            if ((IsUSNumber(phone.value) == false) && (IsInternational(phone.value) == false)) alertMsg += "\n For our US customers, please enter your phone number in the following format:(xxx-xxx-xxxx or (xxx)-xxx-xxxx) . For our international customers, please enter your phone number in one of the following formats:(+xxx(xx)xxx-xxx or xxx-xx-xxxxxxxx).\n\n";            
            if ((email.value == null) || (email.value == " ")) alertMsg += "\n Email is blank";
            if (echeck(email.value) == false) alertMsg += "\n Invalid Email";
            if (ContactSource.selectedIndex == 0) alertMsg += "\n Please select a method in which you heard of Nutricap Labs";
            if ((ContactSource.value == "Other") &&
                  (
                     (ContactSourceOther.value == "Please explain how you heard about us in more detail here.") ||
                     (ContactSourceOther.value == "") ||
                     (ContactSourceOther.value == null)
                  )
                ) alertMsg += "\n You selected a referral source as other, please explain in more detail.";

            if (alertMsg != "The following REQUIRED fields\nhave been left empty or contain an error:\n") {
                alert(alertMsg);
                return false;
            } else {
                return true;
                //alert("test");
            }
        }
    } else if (btton == "request_packet") {
        with (document.form2Validate) {        
            
            var alertMsg = "The following REQUIRED fields\nhave been left empty or contain an error:\n";

            //if (Subject.value == "") alertMsg += "\n Name";						
            //if (Description.value == "") alertMsg += "\n Description";
            if ((Name.value == null) || (Name.value == "")) alertMsg += "\n Name";
            if ((Phone.value == null) || (Phone.value == "") || (IsPhoneLength(Phone.value)==false)) alertMsg += "\n Phone";                        
            if ((IsUSNumber(Phone.value) == false) && (IsInternational(Phone.value) == false)) alertMsg += "\n For our US customers, please enter your phone number in the following format:(xxx-xxx-xxxx or (xxx)-xxx-xxxx). For our international customers, please enter your phone number in one of the following formats:(+xxx(xx)xxx-xxx or xxx-xx-xxxxxxxx).\n\n";            
            if (ContactSource.selectedIndex == 0) alertMsg += "\n Please select a method in which you heard of Nutricap Labs";
            if ((ContactSource.value == "Other") &&
                  (
                     (ContactSourceOther.value == "Please explain how you heard about us in more detail here.") ||
                     (ContactSourceOther.value == "") ||
                     (ContactSourceOther.value == null)
                  )
                ) alertMsg += "\n You selected a referral source as other, please explain in more detail.";
                
	     
            if (HowToContact[0].checked == false && HowToContact[1].checked == false) 
            {
                alertMsg += "\n Please select a contact method";
            } else if (HowToContact[1].checked == true)            
            {
               if ((Email.value == null) || (Email.value == "")) alertMsg += "\n Email is blank";
               if (echeck(Email.value) == false) alertMsg += "\n Invalid Email";

            } else if (HowToContact[0].checked == true) 
            {
                    if (Name.value == "") alertMsg += "\n Name";
                    if (Address1.value == "") alertMsg += "\n Address1";
                    //if (Address2.value == "") alertMsg += "\n Address2";
                    if (City.value == "") alertMsg += "\n City";
                    if (State.value == "") alertMsg += "\n State";
                    if (Zip.value == "") alertMsg += "\n Zip";
                    if (Country.value == "") alertMsg += "\n Country";
                    if ((Email.value == null) || (Email.value == "")) alertMsg += "\n Email is blank";
                    if (echeck(Email.value) == false) alertMsg += "\n Invalid Email";

            }
            if (alertMsg != "The following REQUIRED fields\nhave been left empty or contain an error:\n") 
            {
                alert(alertMsg);
                return false;
            } else 
            {
                return true;
                //alert("test");
            }
        }
    } else if (btton == "request_quote") {
        with (document.form2Validate) {
            var alertMsg = "The following REQUIRED fields\nhave been left empty or contain an error:\n";

            //if (Subject.value == "") alertMsg += "\n Name";	
            if (Type.selectedIndex == 0) alertMsg += "\n Type";
            if (Description.value == "") alertMsg += "\n Description";
            if ((Quantity.value == null) || (Quantity.value == "")) alertMsg += "\n Quantity";
            if ((Product.value == null) || (Product.value == "")) alertMsg += "\n Product";
            if ((Phone.value == null) || (Phone.value == "") || (IsPhoneLength(Phone.value)==false)) alertMsg += "\n Phone";           
            if ((IsUSNumber(Phone.value) == false) && (IsInternational(Phone.value) == false)) alertMsg += "\n For our US customers, please enter your phone number in the following format:(xxx-xxx-xxxx or (xxx)-xxx-xxxx). For our international customers, please enter your phone number in one of the following formats:(+xxx(xx)xxx-xxx or xxx-xx-xxxxxxxx).\n\n";            
            if ((Email.value == null) || (Email.value == "")) alertMsg += "\n Email is blank";
            if (echeck(Email.value) == false) alertMsg += "\n Invalid Email";
            if (ContactSource.selectedIndex == 0) alertMsg += "\n Please select a method in which you heard of Nutricap Labs";
            if ((ContactSource.value == "Other") &&
                  (
                     (ContactSourceOther.value == "Please explain how you heard about us in more detail here.") ||
                     (ContactSourceOther.value == "") ||
                     (ContactSourceOther.value == null)
                  )
                ) alertMsg += "\n You selected a referral source as other, please explain in more detail.";

            if (alertMsg != "The following REQUIRED fields\nhave been left empty or contain an error:\n") {
                alert(alertMsg);
                return false;
            } else {
                return true;
                //alert("test");
            }
        }
    } else if (btton == "nutrichat") {
        with (document.form1) {
            var alertMsg = "The following REQUIRED fields\nhave been left empty or contain an error:\n";

            //if (Subject.value == "") alertMsg += "\n Name";	
            if (Name.value == "") alertMsg += "\n Name (Empty)";
            if ((Email.value == null) || (Email.value == "")) alertMsg += "\n Email Address (Empty)";
            if ((Phone.value == null) || (Phone.value == "") || (IsPhoneLength(phone.value)==false)) alertMsg += "\n Phone Number (Empty) \n";
            if (ADate.selectedIndex == 0) alertMsg += "\n Please select preferred appointment date (Error)";

            if (echeck(Email.value) == false) alertMsg += "\n Invalid Email Address (Error)";
            if ( (IsUSNumber(Phone.value) == false) && (IsInternational(Phone.value) == false)) alertMsg += "\n For our US customers, please enter your phone number in the following format:(xxx-xxx-xxxx or (xxx)-xxx-xxxx). For our international customers, please enter your phone number in one of the following formats:(+xxx(xx)xxx-xxx or xxx-xx-xxxxxxxx).\n\n";

            if (alertMsg != "The following REQUIRED fields\nhave been left empty or contain an error:\n") {
                alert(alertMsg);
                return false;
            } else {
                return true;
                //alert("test");
            }
        }
    }
}

function IsNumeric(strString)
//  check for valid numeric strings	
{
    var strValidChars = "0123456789-";
    var strPhonePattern = "^(1\s*[-\/\.]?)?(\((\d{3})\)|(\d{3}))\s*([\s-./\\])?([0-9]*)([\s-./\\])?([0-9]*)$";
    var strChar;
    var blnResult = true;
    var numericCount;

    //Phone length should be a minimum of 10
    if ((strString.length == 0) || (strString.length < 16)) return false;

    numericCount = 0;
    //  test strString consists of valid characters listed above
    for (i = 0; i < strString.length && blnResult == true; i++) {
        strChar = strString.charAt(i);
        if (strValidChars.indexOf(strChar) == -1) {
            return false;
        }
    }


    return blnResult;

}


function IsPhoneLength(strString)
//  check for valid numeric strings	
{
     var blnResult = true;
     
    //Phone length should be a minimum of 10
    if ( (strString.length == 1) ||  (strString.length < 10) || (strString.length > 16) ) blnResult = false;    
    return blnResult;

}

function IsUSNumber(strString)
//  check for valid numeric strings	
{
    var strValidChars = "0123456789-";    
    var optionalPattern = /^(\(?\d\d\d\)?)?( |-|\.)?\d\d\d( |-|\.)?\d{4,4}(( |-|\.)?[ext\.]+ ?\d+)?$/;       
    var stdPattern = /^\(\d{3]\) \d{3}-\d{4}$/;
    var blnResult = true;    
    
    if (strString.match(stdPattern) || strString.match(optionalPattern) )
    {    	
       	blnResult = true;
       	
    } 
    else
    {    	
      	blnResult = false;
    }
    return blnResult;

}

function IsInternational(strString)
//  check for valid numeric strings	
{
    var stdPattern1 = /^(1\s*[-\/\.]?)?(\((\d{3})\)|(\d{3}))\s*([\s-./\\])?([0-9]*)([\s-./\\])?([0-9]*)$/;
    var stdPattern2 = /^(\+[0-9][0-9][0-9]*(\([0-9]*\)|-[0-9]*-))?[0]?[1-9][0-9\- ]*$/;
    var stdPattern3 = /^\+(?:[0-9] ?){6-14}[0-9]$/;
    

    var blnResult = true;        
          
    if (strString.match(stdPattern1) || strString.match(stdPattern2) || strString.match(stdPattern3) ) 
    {	
    	blnResult = true;    	
    } 
    else
    {	
    	blnResult = false;
    }
    
    return blnResult;

}


function echeck(str) {
    var blnResult = true;
    var at = "@"
    var dot = "."
    var lat = str.indexOf(at)
    var lstr = str.length
    var ldot = str.indexOf(dot)
    if (str.indexOf(at) == -1) {
        //alert("Invalid E-mail ID")
        blnResult = false;
    }
    var filter = /^.+@.+\..{2,3}$/

    if (!filter.test(str)) {
        blnResult = false;
    }

    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        //alert("Invalid E-mail ID")
        blnResult = false;
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        //alert("Invalid E-mail ID")
        blnResult = false;
    }

    if (str.indexOf(at, (lat + 1)) != -1) {
        //alert("Invalid E-mail ID")
        blnResult = false;
    }

    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        //alert("Invalid E-mail ID")
        blnResult = false;
    }

    if (str.indexOf(dot, (lat + 2)) == -1) {
        //alert("Invalid E-mail ID")
        blnResult = false;
    }

    if (str.indexOf(" ") != -1) {
        // alert("Invalid E-mail ID")
        blnResult = false;
    }

    return blnResult;
}

function hideOther(objSelect) {
if(objSelect == "Other"){
document.getElementById("OtherStatus").style.display='block'; //hide textbox
document.getElementById("TypeOther").style.display='block'; //hide textbox
} else {
document.getElementById("OtherStatus").style.display='none'; //hide textbox
document.getElementById("TypeOther").style.display='none'; //hide textbox
}
//document.getElementById("textbox").style.display='none'; //hide textbox
//document.getElementById("otherOpt").style.display='block'; //show other options
}
