var letters = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
var letNum = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var letNumSymbol = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789@."
var numbers = " 0123456789"

   
//**************************************************************************************
//**************************************************************************************
//
//FUNCTION showError
//
//This function is called by function "checkNameAndEmail", it takes in the variable 
//"dataSource" as a parameter, which identifies the field containing errors. 
//
//Errors are then displayed according to the value set in variable "foundErr"
//which is updated during the routines of the validation functions.
//
//It also resets the value of the field containing the error and refocuses on this field.
//
//**************************************************************************************
//**************************************************************************************

function showError(dataSource)
{
   
      switch (dataSource) {              //value of datasource used to identify error field
         
              case 1:
                        if (foundErr == "true1")      //If no characters entered
                           {
                              alert (" You Must Enter A Name!");
                           }//end if
                        else                         //If invalid characters entered
                           {
                              alert ("Name Contains Invalid Characters!"); 
                           }//end else
                        jobForm.name.value = "";  //reset value of field
                        jobForm.name.focus();     //refocus cursor to field
                        break;
	      case 2:

                        if (foundErr == "true1")        //If no characters entered
                           {
                              alert (" You Must Enter A Company! (Current Or Most Recent)");
                           }//end if
                        else                            //If invalid characters entered
                           {
                              alert ("Company Contains Invalid Characters!"); 
                           }//end else
                        jobForm.company.value = "";     //reset value of field
                        jobForm.company.focus();        //refocus cursor to field
                        break;

              case 3:

                        if (foundErr == "true1")        //If no characters entered
                           {
                              alert (" You Must Enter A Job Title! (Current Or Most Recent)");
                           }//end if
                        else                            //If invalid characters entered
                           {
                              alert ("Job Title Contains Invalid Characters!"); 
                           }//end else
                        jobForm.job.value = "";       //reset value of field
                        jobForm.job.focus();          //refocus cursor to field
                        break;

              case 4:

                        if (foundErr == "true1")        //If no or not enough  characters entered
                           {
                              alert ("You Must Enter A Valid Email Address!");
                           }//end if
                        else                            //If invalid characters entered
                           {
                              alert ("Email Address Must Be In Valid Format!"); 
                           }//end else
                        jobForm.email.value = "";    //reset value of field
                        jobForm.email.focus();       //refocus cursor to field
                        break;

              case 5:

                        if (foundErr == "true1")        //If no or not enough  characters entered
                           {
                              alert (" Please Enter A Valid Phone Number Or Email So We May Contact You!");
                              jobForm.email.value = "";   //reset value of field
                              jobForm.email.focus();      //refocus cursor to field
                              break;
                           }//end if
                        else                            //If invalid characters entered
                           {
                              alert ("Please Note That Only Numbers May Be Entered In Telephone Field!"); 
                              jobForm.telephone.value = "";   //reset value of field
                              jobForm.telephone.focus();      //refocus cursor to field
                           }//end else                                        
                        break;
						
              case 6:

                        if (foundErr == "true1")        //If no or not enough  characters entered
                           {
                              alert (" You Must Enter A Message!");
                           }//end if
                                      
                        jobForm.message.value = "";   //reset value of field
                        jobForm.message.focus();      //refocus cursor to field
                        break;
						
              
						
																																		
                   }//end switch
} //end function showErr  




//**************************************************************************************
//**************************************************************************************
//
//FUNCTION checkLetters
//
//This function is called by function "checkNameAndEmail", it takes in the variable 
//"checkData" as a parameter, which holds the object containing the value to either
//the Forename or Surname fields of the form. 
//
//The function then checks to make sure that  some characters are entered.
//Checks are then made to see if the characters entered are acceptable.
//
//A value is attached to variable "foundErr" according to specific error.
//
//If no error is found, then variable "foundErr" keeps the initial value "false".
//
//Variable "foundErr is then returned to calling function "checkNameAndEmail".
//
//**************************************************************************************
//**************************************************************************************
 function checkLetters(checkData)
   {
      var letter;
      var strlen = checkData.value.length;     //strlen takes the value of the length of the value held
      var foundLet;                            //in object "checkData".
      var foundErr = "false";
	  var letTemp;
	  
	 
      
         if (strlen == "0")                    //if length of value is 0 
                    {
                      foundErr = "true1";      //set value of variable foundErr
                    }//end if
        // else
         //   {
     
        //    for(n= 0; n < strlen; n++)         //repeat for each character in the string
          //     {  
          //        letter = checkData.value.charAt(n); //variable char takes value of current character
          //        foundLet = letters.indexOf(letter); //variable foundlet takes the value of the index value
                                                      //of current letter in string "letters"
													  
                  
           //       if ((foundLet == "0") || (foundLet == "-1"))   //if not found
	        //         {
             //               foundErr = "true2";      //set value of variable foundErr
	                 //   break;
	               //  }// end if 
            
              //  }//endfor  
          //   }//end else     


          
       return foundErr;

}//end function checkLetters

 function checkMessage(checkData)
   {
      var letter;
      var strlen = checkData.value.length;     //strlen takes the value of the length of the value held
      var foundLet;                            //in object "checkData".
      var foundErr = "false";
      
         if (strlen == "0")                    //if length of value is 0 
                    {
                      foundErr = "true1";      //set value of variable foundErr
                    }//end if
       return foundErr;

}//end function checkMessage


 function checkPhone(checkData)
   {
      var Num;
      var strlen = checkData.value.length;     //strlen takes the value of the length of the value held
	  var checkData2 = jobForm.email;
	  var strlen2 = checkData2.value.length;
      var foundNum;                            //in object "checkData".
      var foundErr = "false";
      
         if ((strlen == "0") && (strlen2 == "0"))  //if length of value is 0 or greater than 11
                    {
                      foundErr = "true1";      //set value of variable foundErr
                    }//end if
         else
            {
     
            for(n= 0; n < strlen; n++)         //repeat for each character in the string
               {  
                  Num = checkData.value.charAt(n); //variable char takes value of current character
                  foundNum = numbers.indexOf(Num); //variable foundlet takes the value of the index value
                                                      //of current letter in string "letters"

                  if ((foundNum == "0") || (foundNum == "-1"))   //if not found
	                 {
                            foundErr = "true2";      //set value of variable foundErr
	                    break;
	                 }// end if 
            
                }//endfor  
             }//end else     


          
       return foundErr;

}//end function checkPhone






//**************************************************************************************
//**************************************************************************************
//
//FUNCTION checkEmail
//
//This function is called by function "checkNameAndEmail", it takes in the variable 
//"checkData" as a parameter, which holds the object containing the value to the
//Email field of the form. 
//
//The function checks the first letter to make sure something is entered and checks
//to make sure it is alphabetic.
//
//Checks are then made to see if the characters contain both "@" and "."
//
//Checks are then made to see if all the characters are alphanumeric or above symbols.
//
//Checks are then made to make sure that characters preceding symbols are alphanumeric.
//
//A value is attached to variable "foundErr" according to specific error.
//
//If no error is found, then variable "foundErr" keeps the initial value "false".
//
//Variable "foundErr is then returned to calling function "checkNameAndEmail".
//
//**************************************************************************************
//**************************************************************************************

function checkEmail(checkData)
{
   var letter;
   var firstLet = checkData.value.charAt();       //gets first letter of variable checkdata
   var foundFirst = letters.indexOf(firstLet);     //gets index value of first letter from string "letters"
   var foundLet;
   var strlen = checkData.value.length;         //strlen takes the value of the length of the value held
   var foundErr = "false";                      //in object "checkData".
   var symbol ="@";
   var dot = "."
   var symbols = " @,."
   var char;
   var charPrev;
   var foundLetPrev;
   var foundSymbol = checkData.value.indexOf(symbol);       //checks for inclusion of @
   var foundDot = checkData.value.indexOf(dot);            //checks for inclusion of .


 
   if ((foundFirst == "0") || (foundFirst == "-1"))  //If first character not entered or not alphabetic
      {
        foundErr = "true1";                     //set value of variable foundErr
	                          
       } //end if      
   else
     if ((foundSymbol == "-1") || (foundDot == "-1")) //if EMail not contain both "@" and "."
        {
          foundErr = "true2";                         //set value of variable foundErr
        } //end if
     else
       if (foundErr= "false")
          {               
            for(n= 1; n < strlen; n++)                  //repeat for each character in the string after first
               {
                   char = checkData.value.charAt(n);       //variable char takes value of current character
                   charPrev = checkData.value.charAt(n-1)  //variable char2 takes value of previous character
                             
                                                                    //if previous character is also symbol
                   if (((char == "@") || (char == ".")) && ((charPrev == "@") || (charPrev == ".")))    
	              {
	                 foundErr = "true2";                      //set value of variable foundErr
	                 break;
	              }//end if  
	        }//end for  
	    }//end if
        else
                                    
          for(n= 0; n < strlen; n++)                  //repeat for each character in the string
             {
                char = checkData.value.charAt(n);       //variable char takes value of current character
                foundLet = letNumSymbol.indexOf(char);  //variable foundlet takes the value of the index value
                                                                 //of current letter in string "letNumSymbol"
         
                if ((foundLet == "0") || (foundLet == "-1"))   //if not found
	           {
	              foundErr = "true2";                 //set value of variable foundErr
	              break;
	           }//end if 
              }//end for
                                  
            

return foundErr;                        // return variable foundErr to calling function

}//end function checkEmail         



//**************************************************************************************
//**************************************************************************************
//
//FUNCTION checkNameAndEmail
//
//This function is called by  both function "checkFormId" and by itself iteratively. It
// takes in the variables "checkData" and "datasource" as parameters, which  hold the 
//objects containing the values to the identified field of the form and the field ID. 
//
//The function uses the value in the datasource field to identify which field
//to carry checks on (and also which error to call when required). and the "checkData"
//variable carries the object containing the value of the identified form field.
//
//Depending on which field is identified in the variable "datasource", the function
//calls a specific function in order to carry out validation on the value of the
//"checkData" variable".
//
//If no error is found in this data, then the function assigns the id of the next field
//in the form to variable "dataDource" and the object containing the value of this new
//field is assigned to "checkData".
//
//The function then checks the value of variable "foundErr" if it is true, then the 
//Function "showError" is called. If "foundErr" is false, then the function
//"checkNameAndEmail" is recalled with the next set of values, until all fields
// are checked. 
//
//**************************************************************************************
//**************************************************************************************

function checkNameAndEmail (checkData,dataSource)
{  
    
      switch (dataSource) {                                 //value of datasource used to identify error field
          
      case 1:
                        foundErr = checkLetters(checkData); //call function 
                        

                        if (foundErr == "false")           //if no errors found
                           {
                            checkData = jobForm.company;   //set checkdata to value of surname
                            dataSource = 2;                //set datasource to company id
 
                            }
                        break;
	  case 2:
                      foundErr = checkLetters(checkData); //call function 
                       
                        if (foundErr == "false")          //if no errors found
                           {
                             checkData = jobForm.job;   //set checkdata to value of Email
                             dataSource = 3;              //set datasource to job id
	                   }
                        break;
                      

      case 3:
                       foundErr = checkLetters(checkData); //call function 
                        if (foundErr == "false")         //if no errors found
                          {
                           checkData = jobForm.email; //set checkdata to value of User Name
                           var dataSource = 4;           //set datasource to email id
                          }

                       break;

	  case 4:
                      foundErr = checkEmail(checkData);   //call function Email
                       
                        if (foundErr == "false")         //if no errors found
                          {
                           checkData = jobForm.telephone; //set checkdata to value of pass
                           dataSource = 5;               //set datasource to telephone id
	                  }
                        break;
	  case 5:
                      foundErr = checkPhone(checkData);   //call function 
                       
                        if (foundErr == "false")         //if no errors found
                          {
                           checkData = jobForm.message; //set checkdata to value of pass
                           dataSource = 10;               //set datasource to message id
	                  }
                        break;
						
						
	  case 6:
                      foundErr = checkMessage(checkData);   //call function 
                       
                        if (foundErr == "false")         //if no errors found
                          {
                           checkData = jobForm.message; //set checkdata to value of pass
                           dataSource = 10;               //set datasource to function termination id
	                  }
                        break;
	 
                                                                
    }//end switch 
                   
    
    
    if ((foundErr == "false") && (dataSource < 10))      //if no error and datasource not termination
      {
         checkNameAndEmail(checkData,dataSource);      //re-call function with new variable values
      }//end if 
      else 
         if (dataSource < 10)                           //if error and datasource not termination
           {
              showError(dataSource);                   //call error display function
           }//end if
         else
         if (dataSource = 10)                           //if no error and datasource is termination
            {
               jobForm.submit();                      //check cookie
            }//end if
                          
           
}//end function checkNameAndEmail

//**************************************************************************************
//**************************************************************************************
//
//FUNCTION checkFormId
//
//This function is called by the submit button on the form.
//
//The hidden field "formId" on the form is set to "addReg" which will be used in the
//server side application to identify the source of the data. 
// 
//Then the function "checkNameAndEmail" is called, sending the varibles
//"checkdata" (containing value of forename field) and "dataSource" containing the field ID. 
//
//**************************************************************************************
//**************************************************************************************

function checkFormId()    
{
   var checkData = jobForm.telephone;   
   var dataSource = 5;
   
   jobForm.formId.value = "jobForm";                       //set hidden field to id of user data
   checkNameAndEmail (checkData,dataSource);              //call function for first time
   
}//end function checkFormId          
   

