//A function used to validate enter annual leave form
function formVerification(form)
{
     //check filled
	 for (var i=0; i<form.elements.length; i++)
     {
         var theElement = form.elements[i];
		 if(theElement.name=="alumniSearch"||
		    theElement.name=="address"||
		    theElement.name=="city" ||
			theElement.name=="state" ||
			theElement.name=="zip" ||
			theElement.name=="companyName" ||
			theElement.name=="getRecruitmentInfo" ||
			theElement.name=="getAlumniInfo" ||
			theElement.name=="buz_title" ||
			theElement.name=="comment" ||
			theElement.name=="resume")continue;
         if(!checkFilled(theElement, true)) return false;
     }
	 
	 //check 7 digits id
	 validate7DigitsField(form.embassyID);
	 //check year field
	 validate4DigitsField(form.graduationYr);
	 //check email
	 validateEmail(form.email);
	 return true;
}

//A function used to check if the field contains five digits
function validate7DigitsField(element)
{

	if(!checkFilled(element, true)) return false;

    var myRegEx = /\d{7}/;
    var str = element.value;
    if(!myRegEx.test(str)){
       alert("The field \"" + element.name + "\"  should include exact 7 digits. Please try again!");

       element.focus();
       element.select();
       return false;
    }
	
    return true;
}

//A function used to check if the field contains four digits
function validate4DigitsField(element)
{

	if(!checkFilled(element, true)) return false;

    var myRegEx = /\d{4}/;
    var str = element.value;
    if(!myRegEx.test(str)){
       alert("The field \"" + element.name + "\"  should include exact 4 digits. Please try again!");

       element.focus();
       element.select();
       return false;
    }
	
    return true;
}

//A function that checks field email to see if it is in valid format
  function validateEmail(element)
  {
      var myRegEx = /@/;
      var str = element.value;
      if(!myRegEx.test(str))  {
          alert("You did not enter a valid email address." +
                "\nThe Email Address needs to include an \"@\"." + "\nPlease try again!");

          element.focus();
          element.select();

          return false;
      }
      return true;
  }
  
  


//A function that checks if the field is not empty.
function checkFilled(element, bAlert)
{
   if(element.type == "text"){
        //alert(element.name + "'s value length=" + element.value.length);
       if(trim(element.value)==""){
           if(bAlert) {
               alert("The field \"" + element.name+ "\" is required. Please go back and check it.");
               element.focus();
               element.select();
           }
           return false;
       }
   }if(element.type == "select-one"){
       if(element.selectedIndex==0){
           if(bAlert){
               alert("The field \" " + element.name + "\" is required. Please go back and check it.");
               element.focus();
           }
           return false;
       }
   }if(element.type == "select-multiple"){

   }if(element.type == "textarea"){
       if(element.value.length==0){
           if(bAlert) {
               alert("The field \" " + element.name + "\" is required. Please go back and check it.");
               element.focus();
           }
           return false;
       }
   }if(element.type == "password"){
       if(element.value.length==0){
           if(bAlert) {
               alert("The field \" " + element.name + "\" is required. Please go back and check it.");
               element.focus();
           }
           return false;
       }
   }

   return true;
}

function trim(inputString)
{
    // Removes leading and trailing spaces from the passed string. Also removes
    // consecutive spaces and replaces it with one space. If something besides
    // a string is passed in (null, custom object, etc.) then return the input.
    if (typeof inputString != "string")
    {
        return inputString;
    }

    var retValue = inputString;
    var ch = retValue.substring(0, 1);
    while (ch == " ")
    { // Check for spaces at the beginning of the string
        retValue = retValue.substring(1, retValue.length);
        ch = retValue.substring(0, 1);
    }

    ch = retValue.substring(retValue.length-1, retValue.length);
    while (ch == " ")
    { // Check for spaces at the end of the string
        retValue = retValue.substring(0, retValue.length-1);
        ch = retValue.substring(retValue.length-1, retValue.length);
    }

    while (retValue.indexOf("  ") != -1)
    { // Note that there are two spaces in the string - look for multiple spaces within the string
        retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
    }

    return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function


