/* Submit Once form validation */

function ValidateMe2(theform){
  //if IE 4+ or NS 6+
  if (document.all||document.getElementById){
  //screen thru every element in the form, and hunt down "submit" and "reset"
    for (i=0; i<theform.length; i++) {
      var tempobj=theform.elements[i]
      if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset") { tempobj.disabled=true; }
    }
  }
}

function phone1() {
  if (myForm.PHONE1.value.length < 3) {
    myForm.PHONE2.focus();
  }
}

function phone1() {
  if (myForm.PHONE2.value.length < 3) {
    myForm.PHONE3.focus();
  }
}

// ===========================================================
function check_email(e) {
  ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

  for(i=0; i < e.length ;i++){
    if(ok.indexOf(e.charAt(i))<0){ 
      return (false);
    }	
  } 

  if (document.images) {
    re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
    re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    if (!e.match(re) && e.match(re_two)) {
      return (-1);		
    } 
  }
}

// ===========================================================
function isNumeric(sText) {
   var ValidChars = "0123456789-$,.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

// ===========================================================
function isValidSSN(value) { 
    // I got this code from: http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256F6A0072B54C
    var re = /^([0-6]\d{2}|7[0-6]\d|77[0-2])([ \-]?)(\d{2})\2(\d{4})$/; 
    if (!re.test(value)) { return false; } 
    var temp = value; 
    if (value.indexOf("-") != -1) { temp = (value.split("-")).join(""); } 
    if (value.indexOf(" ") != -1) { temp = (value.split(" ")).join(""); } 
    if (temp.substring(0, 3) == "000") { return false; } 
    if (temp.substring(3, 5) == "00") { return false; } 
    if (temp.substring(5, 9) == "0000") { return false; } 
    return true; 
}

// ===========================================================
function isValidZipCode(value) {
   // I got this code from: http://www.breakingpar.com/bkp/home.nsf/0/87256B280015193F87256F6B005294C2
   var re = /^\d{5}([\-]\d{4})?$/;
   return (re.test(value));
}

// ===========================================================
function isValidDate(dateStr, format) {
   if (format == null) { format = "MDY"; }
   format = format.toUpperCase();
   if (format.length != 3) { format = "MDY"; }
   if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) || (format.indexOf("Y") == -1) ) { format = "MDY"; }
   if (format.substring(0, 1) == "Y") { // If the year is first
      var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
      var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
   } else if (format.substring(1, 2) == "Y") { // If the year is second
      var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
      var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
   } else { // The year must be third
      var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
      var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
   }
   // If it doesn't conform to the right format (with either a 2 digit year or 4 digit year), fail
   if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) { return false; }
   var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
   // Check to see if the 3 parts end up making a valid date
   if (format.substring(0, 1) == "M") { var mm = parts[0]; } else if (format.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
   if (format.substring(0, 1) == "D") { var dd = parts[0]; } else if (format.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
   if (format.substring(0, 1) == "Y") { var yy = parts[0]; } else if (format.substring(1, 2) == "Y") { var yy = parts[1]; } else { var yy = parts[2]; }
   if (parseFloat(yy) <= 50) { yy = (parseFloat(yy) + 2000).toString(); }
   if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
   var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
   if (parseFloat(dd) != dt.getDate()) { return false; }
   if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
   return true;
}

// ===========================================================
// Credit Card Validation Javascript
// http://javascript.about.com/library/blccard.htm

function validateCreditCard(s) {
var v = "0123456789";
var w = "";
for (var i=0; i < s.length; i++) {
x = s.charAt(i);
if (v.indexOf(x,0) != -1)
w += x;
}
var j = w.length / 2;
if (j < 6.5 || j > 8 || j == 7) return false;
var k = Math.floor(j);
var m = Math.ceil(j) - k;
var c = 0;
for (var i=0; i<k; i++) {
a = w.charAt(i*2+m) * 2;
c += a > 9 ? Math.floor(a/10 + a%10) : a;
}
for (var i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
return (c%10 == 0);
}


// ===========================================================
// These next 3 functions are for auto tabbing the phone number
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e, whichPhoneNumber) {
    var keyCode = (isNN) ? e.which : e.keyCode;
    var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];

    if(input.value.length >= len && !containsElement(filter,keyCode)) {
        input.value = input.value.slice(0, len);
        if (whichPhoneNumber == "areacode") {
            myForm.PHONE2.focus();
        } else {
            myForm.PHONE3.focus();
        }
        //input.form[(getIndex(input)+1) % input.form.length].focus(); // old way I copied from code
    }
    
    function containsElement(arr, ele) {
        var found = false, index = 0;
        while(!found && index < arr.length)
        if(arr[index] == ele)
            found = true;
        else
            index++;
        return found;
    }
    
    function getIndex(input) {
        var index = -1, i = 0, found = false;
        while (i < input.form.length && index == -1)
        if (input.form[i] == input)index = i;
        else i++;
        return index;
    }
}


function W4_ValidateMe(theform) {
  //if IE 4+ or NS 6+
  //alert("running fuct");
  if (document.all||document.getElementById) {
  
      valid = true;
      
      if (document.myForm.First_Name.value.length < 2) {
          alert("Incorrect submission!\n\nPlease enter a valid first name.");
          //document.getElementByID("FN").style.backgroundColor = "#ff0000";
          myForm.First_Name.focus();
          valid = false;
      
      } else if (document.myForm.Last_Name.value.length < 2) { 
          alert("Incorrect submission!\n\nPlease enter a valid last name.");
          myForm.Last_Name.focus();
          valid = false;

      } else if (!isValidSSN(document.myForm.Social.value)) { 
          alert("Incorrect submission!\n\nPlease enter a valid Social Security Number.\n\nYou can enter the number without dashes (a total of 9 digits) or with dashes in this format XXX-XX-XXXX.");
          myForm.Social.focus();
          valid = false;

      } else if (!check_email(document.myForm.Email.value)) { 
          alert("Incorrect submission!\n\nPlease enter a valid email address.");
          myForm.Email.focus();
          valid = false;

      } else if (document.myForm.Address.value.length < 5) { 
          alert("Incorrect submission!\n\nPlease enter a valid address.");
          myForm.Address.focus();
          valid = false;

      } else if (document.myForm.City_State_Zip.value.length < 5) { 
          alert("Incorrect submission!\n\nPlease enter a valid city, state and zip.");
          myForm.City_State_Zip.focus();
          valid = false;

      } else if ((document.myForm.Allowances.value.length < 1) || (document.myForm.Allowances.value.length > 2) || (!isNumeric(document.myForm.Allowances.value))) {
          alert("Incorrect submission!\n\nPlease enter a valid number of Allowances.");
          myForm.Allowances.focus();
          valid = false;

      } else if (!isNumeric(document.myForm.Additional_Amount_Withheld.value)) {
          alert("Incorrect submission!\n\nPlease enter a valid 'Additional Withholding' amount.");
          myForm.Additional_Amount_Withheld.focus();
          valid = false;

      }

      if (valid) {
	      	// Disable the submit button so user doesn't click twice!
	      	// myForm.submit.disabled = true;
	      	window.setTimeout(function(){ myForm.submit.disabled = true; }, 5);
      }

      // alert("THE valid VARIABLE IS SET TO..."); alert(valid); // checks return value for testing
      return valid;

  }
}


function I9_ValidateMe(theform) {
  if (document.all||document.getElementById) {
  
      valid = true;
      
      if (document.myForm.First_Name.value.length < 2) {
          alert("Incorrect submission!\n\nPlease enter a valid first name.");
          //document.getElementByID("FN").style.backgroundColor = "#ff0000";
          myForm.First_Name.focus();
          valid = false;
      
      } else if (document.myForm.Last_Name.value.length < 2) { 
          alert("Incorrect submission!\n\nPlease enter a valid last name.");
          myForm.Last_Name.focus();
          valid = false;

      } else if (!isValidSSN(document.myForm.Social_Security_Number.value)) { 
          alert("Incorrect submission!\n\nPlease enter a valid Social Security Number.\n\nYou can enter the number without dashes (a total of 9 digits) or with dashes in this format XXX-XX-XXXX.");
          myForm.Social_Security_Number.focus();
          valid = false;

      } else if (!check_email(document.myForm.Email.value)) { 
          alert("Incorrect submission!\n\nPlease enter a valid email address.");
          myForm.Email.focus();
          valid = false;

      } else if (document.myForm.Address.value.length < 5) { 
          alert("Incorrect submission!\n\nPlease enter a valid address.");
          myForm.Address.focus();
          valid = false;

      } else if (!isValidDate(document.myForm.Date_of_Birth.value, "MDY")) {
          alert("Incorrect submission!\n\nPlease enter a valid birth date.");
          myForm.Date_of_Birth.focus();
          valid = false;

      } else if (document.myForm.City.value.length < 2) { 
          alert("Incorrect submission!\n\nPlease enter a valid city.");
          myForm.City.focus();
          valid = false;

      } else if (!isValidZipCode(document.myForm.Zip.value)) {
          alert("Incorrect submission!\n\nPlease enter a valid zip code.");
          myForm.Zip.focus();
          valid = false;

      }

      if (valid) {
	      	// Disable the submit button so user doesn't click twice!
        	// myForm.submit.disabled = true;
        	window.setTimeout(function(){ myForm.submit.disabled = true; }, 5);
      }

      return valid;

  }
}


function Auth_to_Obtain_Info_ValidateMe(theform) {
  if (document.all||document.getElementById) {
  
      valid = true;
      
      if (document.myForm.First_Name.value.length < 2) {
          alert("Incorrect submission!\n\nPlease enter a valid first name.");
          //document.getElementByID("FN").style.backgroundColor = "#ff0000";
          myForm.First_Name.focus();
          valid = false;
      
      } else if (document.myForm.Last_Name.value.length < 2) { 
          alert("Incorrect submission!\n\nPlease enter a valid last name.");
          myForm.Last_Name.focus();
          valid = false;

      } else if (!isValidSSN(document.myForm.Social_Security_Number.value)) { 
          alert("Incorrect submission!\n\nPlease enter a valid Social Security Number.\n\nYou can enter the number without dashes (a total of 9 digits) or with dashes in this format XXX-XX-XXXX.");
          myForm.Social_Security_Number.focus();
          valid = false;

      } else if (!check_email(document.myForm.Email.value)) { 
          alert("Incorrect submission!\n\nPlease enter a valid email address.");
          myForm.Email.focus();
          valid = false;

      } else if (document.myForm.Current_Address.value.length < 5) { 
          alert("Incorrect submission!\n\nPlease enter a valid address.");
          myForm.Current_Address.focus();
          valid = false;

      } else if (document.myForm.City.value.length < 2) { 
          alert("Incorrect submission!\n\nPlease enter a valid city.");
          myForm.City.focus();
          valid = false;

      } else if (!isValidZipCode(document.myForm.Zip.value)) {
          alert("Incorrect submission!\n\nPlease enter a valid zip code.");
          myForm.Zip.focus();
          valid = false;

      } else if ((!isNumeric(document.myForm.Home_Phone.value)) || (document.myForm.Home_Phone.value.length < 2)) {
          alert("Incorrect submission!\n\nPlease enter a valid home phone number.");
          myForm.Home_Phone.focus();
          valid = false;

      }

      if (valid) {
	      	// Disable the submit button so user doesn't click twice!
        	// myForm.submit.disabled = true;
        	window.setTimeout(function(){ myForm.submit.disabled = true; }, 5);
      }

      return valid;

  }
}


function submitApplication_ValidateMe(theform) {
  if (document.all||document.getElementById) {
  
      valid = true;
      
      if (document.myForm.f_first.value.length < 2) {
          alert("Incorrect submission!\n\nPlease enter a valid first name.");
          //document.getElementByID("FN").style.backgroundColor = "#ff0000";
          myForm.f_first.focus();
          valid = false;
      
      } else if (document.myForm.f_last.value.length < 2) { 
          alert("Incorrect submission!\n\nPlease enter a valid last name.");
          myForm.f_last.focus();
          valid = false;

      } else if (document.myForm.f_address.value.length < 2) { 
          alert("Incorrect submission!\n\nPlease enter a valid address.");
          myForm.f_address.focus();
          valid = false;

      } else if (document.myForm.f_city.value.length < 2) { 
          alert("Incorrect submission!\n\nPlease enter a valid city name.");
          myForm.f_city.focus();
          valid = false;

      } else if (document.myForm.f_state.value.length < 2) { 
          alert("Incorrect submission!\n\nPlease enter a valid state name.");
          myForm.f_state.focus();
          valid = false;

      } else if (!isValidZipCode(document.myForm.f_zip.value)) {
          alert("Incorrect submission!\n\nPlease enter a valid zip code.");
          myForm.f_zip.focus();
          valid = false;

      } else if ((!isNumeric(document.myForm.f_phone.value)) || (document.myForm.f_phone.value.length < 2)) {
          alert("Incorrect submission!\n\nPlease enter a valid phone number.");
          myForm.f_phone.focus();
          valid = false;

      } else if (!check_email(document.myForm.resume_Email_.value)) { 
          alert("Incorrect submission!\n\nPlease enter a valid email address.");
          myForm.resume_Email_.focus();
          valid = false;

	  } else if (document.myForm.resume_New_Password_.value.length < 6) { 
          alert("Incorrect submission!\n\nPlease choose a valid password. Passwords must be at least 6 characters long.");
          myForm.resume_New_Password_.focus();
          valid = false;

      } else if (document.myForm.resume_Confirm_New_Password_.value != document.myForm.resume_New_Password_.value) { 
          alert("Incorrect submission!\n\nThe password fields do not match. Please enter the password properly in both password boxes so we can confirm there is no error.");
          myForm.resume_Confirm_New_Password_.focus();
          valid = false;

      } else if (!isValidDate(document.myForm.resume_Date_Available.value, "MDY")) {
          alert("Incorrect submission!\n\nPlease enter a valid date in the 'Date Available' field.");
          myForm.resume_Date_Available.focus();
          valid = false;

      } else if (!isValidSSN(document.myForm.resume_Social_Security_Number.value)) { 
          alert("Incorrect submission!\n\nPlease enter a valid Social Security Number.\n\nYou can enter the number without dashes (a total of 9 digits) or with dashes in this format XXX-XX-XXXX.");
          myForm.resume_Social_Security_Number.focus();
          valid = false;
          
      } else if (!isNumeric(document.myForm.resume_Desired_Salary.value)) {
          alert("Incorrect submission!\n\nPlease enter a valid desired salary. You may only enter numeric characters in this field.");
          myForm.resume_Desired_Salary.focus();
          valid = false;

      }

      if (valid) {
	      	// Disable the submit button so user doesn't click twice!
        	myForm.submit.disabled = true;
      }

      return valid;

  }
}
