/**
 * This is custom client side validation code for
 * the Powerwave training request form.
 * Validation is simple, but conditional upon several
 * fields.
 */

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",
                                        "cvportal"
                                        );
        return excludedDomains;
 }
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 ldot=theEmail.indexOf(dot);
       var string_start = lat+1;
       var string_end = ldot;
       var emailDomain = theEmail.slice(string_start,string_end);
       return emailDomain;
 }
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 == blockedEmails[i])
             {
               alert("Email from the domain \""+blockedEmails[i]+"\" is not allowed");
               return false;
             }
       }

       return true;
 }
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;
	}
/**
 * This code performs a basic check for a valid email address
 * and excludes generic email domains.
 */
function ValidateEmail(){
        /**
         * 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 conferenceRoomFields()
{
   var theFields = [
                    "ConferenceRoom_LocationName",
                    "ConferenceRoom_Address_Street",
                    "ConferenceRoom_Address_City",
                    "ConferenceRoom_Address_State",
                    "ConferenceRoom_Address_Zip",
                    "Comments"
                    ];
   return theFields;
}
function alwaysRequired1()
{
   var theFields = [
                   "first-name",
                   "last-name",
                   "title",
                   "company",
                   "email",
                   "phone_number",
                   "country"
                   ];
   return theFields;
}
function alwaysRequired2()
{
   var theFields = [
                   "street",
                   "city",
                   "zip_code",
                   "Training_Dates",
                   "Training_Attendees",
                   "Training_Sessions",
                   "system_ProductName",
                   "BaseSation_Model",
                   "Training_Type",
                   "BaseStation_ModelName",
                   "BaseStation_ModelNumber",
                   "Comments"
                   ];
   return theFields;
}
function requestorFields()
{
  var theFields = [
                  "Requestor_FullName",
                  "Requestor_Badge",
                  "Requestor_Location",
                  "Requestor_Mgr_Email"
                  ];
  return theFields;
}

function requestorRequired()
{
  var checkElement = document.getElementById("Requestor_Type");
  if (checkElement.value !== "Customer")
  {
    return true;
  }
  else
  {
    return false;
  }
}
function validateForm()
{
  var theMessage = "";
  var flag = true;//set this to false if form input is missing.
  //first check the requestor type
  var i;
  var checkElement;
  var checkFields;
  if (requestorRequired())
  {
     //check the requestor fields
     checkFields = requestorFields();
     for (i = 0; i < checkFields.length; i++)
     {
       checkElement = document.getElementById(checkFields[i])
              if (
                  checkElement.value === "" ||
                  checkElement.value === null ||
                  checkElement.value === undefined
                  )
                  {
                     flag = false;
                     theMessage = theMessage + checkElement.name + "\n";
                  }

     }
  }
  //check first set of always required fields
   checkFields = alwaysRequired1();
     for (i = 0; i < checkFields.length; i++)
     {
       checkElement = document.getElementById(checkFields[i])
              if (
                  checkElement.value === "" ||
                  checkElement.value === null ||
                  checkElement.value === undefined
                  )
                  {
                     flag = false;
                     theMessage = theMessage + checkElement.name + "\n";
                  }

     }
   //check if state is required
   
   var theCountry = document.getElementById("country");
   if (
       theCountry.value === "Canada" ||
       theCountry.value ===  "United States"
       )
       {
         //state is required - check for state value
         var theState = document.getElementById("state");
         if (
             theState.value === "" ||
             theState.value === null ||
             theState.value === undefined
             )
             {
               flag = false;
               theMessage = theMessage + theState.name + "\n";
             }
       };

   //check next set of required elements

   checkFields = alwaysRequired2();
     for (i = 0; i < checkFields.length; i++)
     {
       checkElement = document.getElementById(checkFields[i])
              if (
                  checkElement.value === "" ||
                  checkElement.value === null ||
                  checkElement.value === undefined
                  )
                  {
                     flag = false;
                     theMessage = theMessage + checkElement.name + "\n";
                  }

     }
    //check the radio button
    checkFields = ["Cell_Site_Type1", "Cell_Site_Type2"];
    var checkRadio1 = document.getElementById(checkFields[0]);
    var checkRadio2 = document.getElementById(checkFields[1]);
    if (!checkRadio1.checked && !checkRadio2.checked)
    {
      flag = false;
      theMessage = theMessage + "Cell Site Type" + "\n";
    }
    /**
     *  Check the conference room.
     *
     * The conference room contact fields are only required if 2 conditions are set.
     * if conference room is "Customer" and same as customer contact info is "No" .
     * Below we check for those 2 conditions.
     */
    var checkConditions = ["Training_Location","Conference_Location_Same"];
    var condition1 = document.getElementById(checkConditions[0]);
    var condition2 = document.getElementById(checkConditions[1]);
    if (condition1.value === "Customer" && condition2.value === "No")
    {
       checkFields = conferenceRoomFields();
       for (i = 0; i < checkFields.length; i++)
       {
         checkElement = document.getElementById(checkFields[i])
                if (
                    checkElement.value === "" ||
                    checkElement.value === null ||
                    checkElement.value === undefined
                    )
                    {
                       flag = false;
                       theMessage = theMessage + checkElement.name + "\n";
                    }
  
       }
    }

  //final check that all required values have been entered....
  if (!flag)
  {
      alert(
            "The following required fields" + 
            "\n" +
            "must be filled in:" +
            "\n\n" +
            theMessage
            );
      return false;
  }
  else
  {
  //If all the required entries have been made then check the email address validity.
       if (ValidateEmail())
       {
         return true;
       }
       else
       {
         return false;
       }
  }
}
