//************** Validate.js *******************

var sNumeric = "0123456789"
var sLowerAlpha = "abcdefghijklmnopqrstuvwxyz"
var sUpperAlpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
var sAlphaNumeric = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
var whitespace = " \t\n\r";

function isDigit(c) {
   return ((c >= "0") && (c <= "9"))
}

function isEmpty(s) {
   return ((s == null) || (s.length == 0))
}

function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {   
        //Check that current character is number.
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    // All characters are numbers.
    return true;
}


function isWhitespace(s) {
    var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }
    // All characters are whitespace.
    return true;
}

function isEmailFormatValid(s) {
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function isEmailAddressesEqual(s1, s2) {
	var s1Clean = removeWhitespace(s1)
	var s2Clean = removeWhitespace(s2) 
	if (s1Clean == s2Clean) return true;
	else return false;
}

function isZIPCode(s) {
   return (isInteger(s) && (s.length == 5))
}

function charInString(c, s) {
    for (i = 0; i < s.length; i++) {
       if (s.charAt(i) == c) return true;
    }
    return false
}

function removeBadChar(s, sgood) {
    var i;
    var returnString = "";
    for (i = 0; i < s.length; i++) {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (sgood.indexOf(c) != -1) returnString += c;
    }
    return returnString;
}

function removeInitialWhitespace(s) {
    var i = 0;
    while ((i < s.length) && charInString (s.charAt(i), whitespace))
       i++;
    return s.substring (i, s.length);
}


function removeListedChar(s, slist) {
    var i;
    var returnString = "";
    for (i = 0; i < s.length; i++) {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (slist.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function removeWhitespace(s) {
    return removeListedChar(s, whitespace)
}


//***** the functions below troubleshoot forms *****
function displayFormElements(form) {
	var i;
	var c = "";
	var returnString ="";
	for (i = 0; i < form.elements.length; i++) {
		var c = c + form.elements[i].name + ", ";
	}
	alert("form elements are " + c)
	return false;
}

function displayNeighborhoods(form) {
	var i;
	var c = ""
	for (i = 0; i < form.Contact_Neighborhood.options.length; i++) {
		var c = c + form.Contact_Neighborhood.options[i].value + ", "
	}
	alert("form elements are " + c)
	return false
}

function displayZipCodes(form) {
	var i;
	var c = ""
	for (i = 0; i < form.Contact_ZipCode.options.length; i++) {
		var c = c + form.Contact_ZipCode.options[i].value + ", "
	}
	alert("Zip Codes are " + c)
	return false
}

//***** the functions above troubleshoot forms *****

function checkForm(oForm) {
	//alert("running checkform")

   //*********************** Name validation below ***********************
   var fullName = oForm.Contact_FullName.value
   if (isEmpty(fullName)) {
	  alert("Name is a required field")
	  oForm.Contact_FullName.focus()
	  return false
   }

   if (isWhitespace(fullName)) {
	  alert("Name is a required field")
	  oForm.Contact_FullName.focus()
	  return false
   }

   if (fullName.length <= 1) {
	  alert("Please enter your name")
	  oForm.Contact_FullName.focus()
	  return false
   }

   //*********************** Phone Number validation below ***********************
   var phoneNumber = oForm.Contact_Phone.value
   if (isEmpty(phoneNumber)) {
	  alert("Phone number is a required field")
	  oForm.Contact_Phone.focus()
	  return false
   }

   if (isWhitespace(phoneNumber)) {
	  alert("Phone number is a required field")
	  oForm.Contact_Phone.focus()
	  return false
   }

   if (phoneNumber.length <= 9) {
	  alert("Please check your Phone number and area code")
	  oForm.Contact_Phone.focus()
	  return false
   }

   var sCleanString = removeBadChar(phoneNumber, sNumeric)
   if (sCleanString.length < 10) {
	  alert("Please check your Phone number and area code")
	  oForm.Contact_Phone.focus()
	  return false
   }

   var sCleanString2 = removeBadChar(phoneNumber, sNumeric)
   if (sCleanString2.length > 10) {
	  alert("Please check your Phone number and area code")
	  oForm.Contact_Phone.focus()
	  return false
   }

return  true
}  

function checkForm2(oForm) {
	//alert("running checkform")

   //*********************** First Name validation below ***********************
   var firstName = oForm.Contact_First_Name.value
   if (isEmpty(firstName)) {
	  alert("First name is a required field")
	  oForm.Contact_First_Name.focus()
	  return false
   }

   if (isWhitespace(firstName)) {
	  alert("First name is a required field")
	  oForm.Contact_First_Name.focus()
	  return false
   }

   if (firstName.length <= 1) {
	  alert("Please enter your full First name")
	  oForm.Contact_First_Name.focus()
	  return false
   }
  
   //*********************** Last Name validation below ***********************
   var lastName = oForm.Contact_Last_Name.value
   if (isEmpty(lastName)) {
	  alert("Last name is a required field")
	  oForm.Contact_Last_Name.focus()
	  return false
   }

   if (isWhitespace(lastName)) {
	  alert("Last name is a required field")
	  oForm.Contact_Last_Name.focus()
	  return false
   }

   if (lastName.length <= 1) {
	  alert("Please enter your full Last name")
	  oForm.Contact_Last_Name.focus()
	  return false
   }
   

   //*********************** email validation below ***********************
   //start Contact_Email check 
   var emailAddress = oForm.Contact_Email.value
   if (isEmpty(emailAddress)) {
	  alert("E-mail is a required field")
	  oForm.Contact_Email.focus()
	  return false
   }

   if (isWhitespace(emailAddress)) {
	  alert("E-mail is a required field")
	  oForm.Contact_Email.focus()
	  return false
   }

   if (!isEmailFormatValid(emailAddress)) {
	  alert("Please check your E-mail entry")
	  oForm.Contact_Email.focus()
	  return false
   }
   //end Contact_Email check 

   //start Contact_EmailVerification check 
   var emailVerification = oForm.Contact_EmailCheck.value
   if (isEmpty(emailVerification)) {
	  alert("E-Mail verificaton is a required field")
	  oForm.Contact_EmailCheck.focus()
	  return false
   }

   if (isWhitespace(emailVerification)) {
	  alert("E-Mail verificaton is a required field")
	  oForm.Contact_EmailCheck.focus()
	  return false
   }

   if (!isEmailFormatValid(emailVerification)) {
	  alert("Please check your E-Mail verificaton entry")
	  oForm.Contact_EmailCheck.focus()
	  return false
   }
   //end Contact_EmailCheck check

   //start email addresses are equal check
   if (!isEmailAddressesEqual(emailAddress, emailVerification)) {
	  alert("Please check that your email addresses match")
	  oForm.Contact_Email.focus()
	  return false
   }
   //end email addresses are equal check
   //*********************** email validation above ***********************

   //*********************** Phone Number validation below ***********************
   var phoneNumber = oForm.Contact_Phone.value
   if (isEmpty(phoneNumber)) {
	  alert("Phone number is a required field")
	  oForm.Contact_Phone.focus()
	  return false
   }

   if (isWhitespace(phoneNumber)) {
	  alert("Phone number is a required field")
	  oForm.Contact_Phone.focus()
	  return false
   }

   if (phoneNumber.length <= 9) {
	  alert("Please check your Phone number and area code")
	  oForm.Contact_Phone.focus()
	  return false
   }

   var sCleanString = removeBadChar(phoneNumber, sNumeric)
   if (sCleanString.length < 10) {
	  alert("Please check your Phone number and area code")
	  oForm.Contact_Phone.focus()
	  return false
   }

   var sCleanString2 = removeBadChar(phoneNumber, sNumeric)
   if (sCleanString2.length > 10) {
	  alert("Please check your Phone number and area code")
	  oForm.Contact_Phone.focus()
	  return false
   }

return  true
}

function checkForm3(oForm) {
	//alert("running checkform")

   //*********************** First Name validation below ***********************
   var firstName = oForm.Contact_First_Name.value
   if (isEmpty(firstName)) {
	  alert("First name is a required field")
	  oForm.Contact_First_Name.focus()
	  return false
   }

   if (isWhitespace(firstName)) {
	  alert("First name is a required field")
	  oForm.Contact_First_Name.focus()
	  return false
   }

   if (firstName.length <= 1) {
	  alert("Please enter your full First name")
	  oForm.Contact_First_Name.focus()
	  return false
   }
  
   //*********************** Last Name validation below ***********************
   var lastName = oForm.Contact_Last_Name.value
   if (isEmpty(lastName)) {
	  alert("Last name is a required field")
	  oForm.Contact_Last_Name.focus()
	  return false
   }

   if (isWhitespace(lastName)) {
	  alert("Last name is a required field")
	  oForm.Contact_Last_Name.focus()
	  return false
   }

   if (lastName.length <= 1) {
	  alert("Please enter your full Last name")
	  oForm.Contact_Last_Name.focus()
	  return false
   }
   

   //*********************** email validation below ***********************
   //start Contact_Email check 
   var emailAddress = oForm.Contact_Email.value
   if (isEmpty(emailAddress)) {
	  alert("E-mail is a required field")
	  oForm.Contact_Email.focus()
	  return false
   }

   if (isWhitespace(emailAddress)) {
	  alert("E-mail is a required field")
	  oForm.Contact_Email.focus()
	  return false
   }

   if (!isEmailFormatValid(emailAddress)) {
	  alert("Please check your E-mail entry")
	  oForm.Contact_Email.focus()
	  return false
   }
   //end Contact_Email check 

   //start Contact_EmailVerification check 
   var emailVerification = oForm.Contact_EmailCheck.value
   if (isEmpty(emailVerification)) {
	  alert("E-Mail verificaton is a required field")
	  oForm.Contact_EmailCheck.focus()
	  return false
   }

   if (isWhitespace(emailVerification)) {
	  alert("E-Mail verificaton is a required field")
	  oForm.Contact_EmailCheck.focus()
	  return false
   }

   if (!isEmailFormatValid(emailVerification)) {
	  alert("Please check your E-Mail verificaton entry")
	  oForm.Contact_EmailCheck.focus()
	  return false
   }
   //end Contact_EmailCheck check

   //start email addresses are equal check
   if (!isEmailAddressesEqual(emailAddress, emailVerification)) {
	  alert("Please check that your email addresses match")
	  oForm.Contact_Email.focus()
	  return false
   }
   //end email addresses are equal check
   //*********************** email validation above ***********************

 return  true
}