/**
 *
 * VersaFlexValidate.js validation form validation script
 *
 * by Thomas Nicolosi Redbus Corp. http://www.redbuscorp.com
 *
 * Contact: thomas@redbuscorp.com
 *
 * Current Revision:
 * 2.1.2
 * 2008-09-08
 * Changes:
 * Added "region" to array of required fields.
 *
 * 2.1.1
 * 2008-09-01
 * Changes:
 * Added missing semicolons to the email validation section after return false statements.
 *
 * 2.1.0
 * Revision Date:
 * 2008-08-21
 *
 * Changes:
 * Modified the list of required fields. Retained state/province conditional
 * validation.
 *
 * Revised 05-05-2008 to repair flaw in email blocking section.
 *
 * Portions adapted from DHTML email validation script. Courtesy of
 * SmartWebby.com (http://www.smartwebby.com/dhtml/)
 *
 *
 */
function validateForm(){
  //First check that the required fields have non-empty values.
  var checkFields = requiredFields();//Gets the array of required fields.
  if(fieldsFilled(checkFields)==false)
  {
    return false;
  }
  //Next validate the email address.
  if(ValidateEmail()==false)
  {
    return false;
  }

  return true;
}

function requiredFields(){
        /**
         * This function returns an array of the required fields.
         * Add the id of each required field to the array.
         */
        var required = new Array(
                                "first-name",
                                "last-name",
                                "company",
                                "region",
                                "email",
                                "phone_number"
                                );
        /**
         * conditional array contains variables that are required only if
         * regions requiring states are selected.
         *
         */
        var conditional = new Array(
                                    "state"
                                    );
        var theRegions = new Array(
                             "region"
                             );
        /**
         * "conditional" array is passed to stateRegion function.
         * stateRegion function returns an array  of required variables to add
         * to the "required" array. "required" array is returned to stateRequired.
         */
        var stateRequired = stateRegion(conditional,theRegions);
        var count;
        var requiredState;
        for(count = 0; count < stateRequired.length; count++){
           requiredState = stateRequired[count];
           if(requiredState != null || requiredState != ""){
              required.push(requiredState);
           }else{
             continue;
           }
        }
        var checkAffiliation = document.getElementById("affiliation");
        if(checkAffiliation){
          required.push("affiliation");
          var checkAffiliationOther = checkAffiliation.value;
          if(checkAffiliationOther == "other"){
            required.push("affiliation-other");
            return required;
          }else{
            return required;
          }
        }else{
        return required;
        }
}

function stateRegion(conditionals,regions){
  var count;
  var regionId;
  var regionCheck;
  var regionValue;
  var regionRequired = new Array;
  var requiredCount = 0;
  for(count = 0; count < regions.length; count++){
    regionId = regions[count];
    regionCheck = document.getElementById(regionId);
    if(regionCheck){
      regionValue = regionCheck.value;
    }else{
      continue;
    }
    if(regionValue == "United States" || regionValue == "Canada"){
      regionRequired[requiredCount] = conditionals[count];
      requiredCount++;
    } else {
      continue;
    }
  }
  return regionRequired;
}
function fieldsFilled(theFields){
        /**
         * "theFields" is an array of required fields to check.
         * form name="versaflex_doc_request"
         */
       var i;
       var validateCount = 0;
       var checkId;
       var checkElement;
       var validateArray = new Array;
       /**
        * Fill the validateArray by selecting the required elements
        * by their id. Must check to see if they exist because all forms
        * do not have all elements and some required elements are conditional
        * based on choice of "region".
        */
       for(i = 0; i < theFields.length; i++){
             checkId = theFields[i];
             checkElement = document.getElementById(checkId);
             if(checkElement){
               validateArray[validateCount] = checkElement;
               validateCount++;
             }else{
               continue;
             }
       }
       /**
        * Next check to see that the value of each element in the validateArray is filled
        * with something.
        */
       var j;
       var checkValue;
       for(j = 0; j < validateArray.length; j++){
         checkValue = validateArray[j].value;
         if(checkValue == null || checkValue =="" || checkValue == undefined){
           alert("A value is required for "+validateArray[j].name);
           return false;
           }else{
             continue;
           }
       }
}
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){
		   alert("Invalid E-mail ID")
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID");
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID");
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID");
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID");
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID");
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID");
		    return false;
		 }

 		 return true;
	}

function ValidateEmail(){
        /**
         * form name="versaflex_doc_request"
         * email input name="email"
         */
	var emailID=document.getElementById("email");
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID");
		emailID.focus();
		return false;
	}
	if (echeck(emailID.value)==false){
		emailID.value="";
		emailID.focus();
		return false;
	}
	if(testAddress(emailID.value)==false){
                emailID.value="";
		emailID.focus();
		return false;
	}
	return true;
 }
 function blockedDomains(){
        /**
         * This function returns an array that contains the email
         * domains that we want to exclude.
         */
        var excludedDomains = new Array(
                                        "yahoo",
                                        /* "aol", */
                                        "gmail",
                                        "google",
                                        "hotmail",
                                        "andrew",
                                        "kathrein",
                                        "jaybeamwireless"
                                        );
        return excludedDomains;
 }
 function testAddress(address){
        /**
         * "address" is an email address that has been validated in
         * ValidateForm() and echeck(str)
         */
       
       var blockedEmails = blockedDomains(); //Get the list of blocked domains.

       var theDomain = getDomain(address); //Extract the email address domain.
       
       /**
        * Compare the domain of the submitted email address to the blocked
        * email domains. Throw an alert and return "false" to prevent
        * submission of the form.
        */
       
       //var l = blockedEmails.length;
       var i;
       for(i in blockedEmails)
       {
             if(theDomain.match(blockedEmails[i]) == blockedEmails[i])
             {
               alert("Email from the domain \""+blockedEmails[i]+"\" is not allowed");
               return false;
             }
       }

       return true;
 }
 function getDomain(theEmail){
       /**
        * This function extracts the domain associated
        * with an email address, theEmail, that has been previously
        * validated for proper format.
        */
       var at="@";
       var dot=".";
       var lat=theEmail.indexOf(at);
       var string_start = lat+1;
       var emailDomain = theEmail.slice(string_start);
       //var ldot=emailPart.indexOf(dot);
       //var string_end = ldot;
       //var emailDomain = theEmail.slice(string_start,string_end);
       return emailDomain;
 }