// This file has all the validation functions 

function form_validateString(textField, textValue, fieldName, required) 
{
    var valid = true;
    textField.value = textField.value.trim();
    textValue = textField.value;

    // checks to see if the person has entered anything in a nonrequired field
    if (!required && textValue.length == 0)
        return true;

    // this is validation to see if the required field is there
    if (required == true)
    {
        valid = form_requireField(textField, fieldName);
        if (!valid) return false
    }
    
    // this is validation to see if there are acceptable chars and they are not all blank
    valid = form_validCharactersInGeneral(textValue);
    
    if (valid)
    {
        textField.value = textValue;
    }
    else    // the field is invalid
    {    
        alert (fieldName + " is invalid. You have entered in invalid characters. \n Please remove them.");
        textField.focus();
        textField.select();
        valid = false;
    }
        
    return valid;
}

function form_validateLength(textField, fieldName, minLength) {
    var textValue = textField.value;
    if (textValue.length < minLength) {
        alert(fieldName + " must be at least " + minLength + " characters long");
        textField.focus();
        textField.select();
        return false;
    }
    return true;
}


function form_validateAlphaNumericWLength(textField, fieldName, required, minLength) {
    var valid = form_validateString(textField, textValue, fieldName, required);
    if (!valid)
        return false;

    var textValue = textField.value;
    var reAlphanumeric = /^[a-zA-Z0-9_]+$/
    valid = reAlphanumeric.test(textValue);

    if (valid) {
        valid = form_validateLength(textField, fieldName, minLength)
    } else {
        alert (fieldName + " is invalid. You have entered in invalid characters. \nPlease enter only alphanumeric characters.");
        textField.focus();
        textField.select();
    }
    return valid;
}

function form_validateStringSilent(textField) {
    textField.value = textField.value.trim();
    if (textField.value.length == 0)
    {
        return false;
    }
    return true;
}

function form_validateHiddenString(textField, fieldName, required) {
    textField.value = textField.value.trim();

    // this is validation to see if the required field is there
    if (required == true) {    
        if(textField.value.length == 0) {
            alert (fieldName + " is required");
            return false;
        }
    }
    return true;
}

function form_requireField(field, fieldName)
{
    if (field.value.length == 0)
    {
        alert(fieldName + " is required");
        field.focus();
        return false;
    }
    return true;
}

function form_validCharactersInGeneral(line)
{
    var valid = true;
    for (var i=0; i<line.length; i++)
    {   
        // Check that current character is in the basis ASCII set.
        var c = line.charAt(i);
        valid = isASCII(c);
        
        if (!valid) 
          return false;
    }
    return valid;
}


// this function will strip all the leading and trailing blank characters.
// it will also remove all the extended ASCII characters
function form_trimString()
{
    //Match spaces at beginning and end of text and replace
    //with null strings
    var newString = this.replace(/^\s+/,'').replace(/\s+$/,'');

    var returnString = "";
    for (var i=0; i < newString.length; i++)
    {   
        var c = newString.charAt(i);
        if (isASCII(c))
            returnString += c;
    }
    return returnString;
}
String.prototype.trim = form_trimString;


function form_removeFrontBlankCharacters(line)
{
    return line.trim();
}

function isASCII (c)
{
   return ( (c >= " " && c <= "~") || (c == "\n" || c == "\r") )
}

function form_validateInteger(textField, fieldName, required) 
{
    var valid;
    textField.value = textField.value.trim();
    var textValue = textField.value;

    // checks to see if the person has entered anything in a nonrequired field
    if (!required && textValue.length == 0)
        return true;
    
    // this is validation to see if the required field is there
    if (required == true)
    {
        valid = form_requireField(textField, fieldName);
        if (!valid) return false;
    }
    
    // this is validation to see if there are acceptable integers and they are not all blank
    textValue = form_stripCommaAndWhitespace(textValue);
    valid = form_validIntegersInGeneral(textValue);
    
    if (valid)
    {
        // change the textfield value to the one that was stripped
        textField.value = textValue;
        return true; // the field is valid so return true
    }
    else    // the field is invalid
    {
        alert (fieldName + " is invalid. Please enter a valid number e.g. 42");
        textField.focus();
        textField.select();
        return false;
    }
}

function form_validateIntegerNoZero(textField, fieldName, required) 
{
    var valid;
    textField.value = textField.value.trim();
    var textValue = textField.value;
    // checks to see if the person has entered anything in a nonrequired field
    if (!required && textValue.length == 0)
        return true;
    
    // this is validation to see if the required field is there
    if (required == true) 
    {
        valid = form_requireField(textField, fieldName);
        if (!valid) return false;
    }
    
    // this is validation to see if there are acceptable integers and they are not all blank
    textValue = form_stripCommaAndWhitespace(textValue);
    valid = form_validIntegersInGeneral(textValue);

    if (valid)
    {
        if (!parseInt(textValue) > 0) {
            alert (fieldName + " is invalid. Please enter a number greater than 0");
            textField.focus();
            textField.select();
            return false;
        }
        // change the textfield value to the one that was stripped
        textField.value = textValue;
        return valid; // the field is valid so return true
    }
    else    // the field is invalid
    {
        valid = false;
        alert (fieldName + " is invalid. Please enter a valid number e.g. 42");
        textField.focus();
        textField.select();
        return valid;
    }
    
    return valid;
}

function form_stripCommaAndWhitespace(s)
{
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is a comma (,) don't append to return string.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if ((c!= ",")  &&  (c!=" ") )
        returnString += c;
    }

    return returnString;
}



function form_validIntegersInGeneral(line)
{
var valid = true;

    for (var i=0; i<line.length; i++)
    {   
        // Check that current character is number.
        var c = line.charAt(i);
    var valid = isDigit(c);
        
    if (!valid) 
      return false;
    }

    // All characters are numbers.
    return valid;

}


function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function form_stripWhiteSpaceAndDash(s)
{   var i;
    var returnString = "";
        
    // Search through string's characters one by one.
    // If character is not a space append to returnString.
        
     for (i = 0; i < s.length; i++)
     {   
         // Check that current character isn't whitespace.
         var c = s.charAt(i);
         if ((c!= " ") && (c!= "-") )
            returnString += c;
    }

    return returnString;
}


function form_stripCharsInPhone (s) {
    var i;
    var returnString = "";
    
    for (i = 0; i < s.length; i++) {   
        var c = s.charAt(i);
        if ((c!= " ") && (c!= "-") && (c!= "(" ) && (c!=")") && (c!=".") )
        returnString += c;
    }

    return returnString;
}

function form_validatePhone(textField, fieldName, required) {
    var valid;
    textField.value = textField.value.trim();
    textValue = textField.value;
    textValue =  form_stripCharsInPhone(textValue);
    
    // nothing has been input into this form and it is not a required field so just return true and finish
    if (!required && textValue.length == 0)
        return valid = true;

    // this is validation to see if the required field is there
    if (required == true)
    {
        valid = form_requireField(textField, fieldName);
        if (!valid) return false;
    }

    // now someone has either input information or it is now a required field

    valid = form_validIntegersInGeneral(textValue);

    if (valid && (textValue.length == 10)) { // check to see if the length is 10 digits
        return valid = true;
    } else {    
        alert(fieldName + " is not a valid phone number. Try area code + 7 digit number. \nex: 555-555-5555");
        textField.focus();
        textField.select();
        return valid = false;
    }

    return valid;    // should not reach here    
}

function reformatTelephone(textField, fieldName, required) {
    var valid;
    var textValue = textField.value;
    valid = form_validatePhone(textField, fieldName, required);
    if (valid) {
        var returnNumber;
        if (textValue.length == 10) {
            returnNumber = textValue.substring(0,3) + "-" + textValue.substring(3,6) + "-" + textValue.substring(6,10)
            textField.value = returnNumber;
        }
    }
    return valid;
}


function form_stateNotBlank(textValue, fieldName, required)
{
  //  alert("In statenotblank, the textValue is " + textValue); 
    if (textValue == " " || textValue == "")
    {
      alert("Please choose a state for " + fieldName);
      return false;
    }
    else
    {
        return true;
    }
}


function form_dropdownNotBlank(textValue, fieldName, required) 
{
	if (!required)
		return true;

    if (textValue == " " || textValue == "-1"  || textValue == "" || textValue == "-2147483648") {
        alert("Please choose an option for the " + fieldName + " field. You must specify a value from the drop down menu.");
        return false;
    } else {
        return true;
    }
}

function form_multiSelectNotBlank(field, fieldName, required) {
    if (field.selectedIndex == "-1") {
        alert("Please choose an option for the " + fieldName + " field. You must specify a value from the drop down menu.");
        field.focus();
        return false;
    } else {
        return true;
    }
}


function form_validateTextAreaBoxes(textArea, textAreaName, maxChars)
{
    textArea.value.trim();
    if (textArea.value.length > maxChars)
    {
        var removeChars = textArea.value.length - maxChars;
        alert("You have entered too much data in " + textAreaName + ". Please remove " + removeChars + " characters.");
        textArea.focus();
        return false;
    }
    else
    {
        return true;
    }
}


// This function will validate a short 7 digit phone number
function form_validateShortPhone(textField, textValue, fieldName, required)
{
    var valid;
    textValue =  form_stripCharsInPhone(textValue);
    if (!required && textValue.length == 0)
        return true;

    if (required == true)
    {
        valid = form_requireField(textField, fieldName);
        if (!valid) return false;
    }
    
    valid = form_validIntegersInGeneral(textValue);
    if (valid)
    {// check to see if the length is 7
        if ((textValue.length == 7))
        return valid = true;
        else
        alert (fieldName + " is not a valid phone number. Try a 7 digit number. \nex) 555-5555");
        textField.focus();
        textField.select();
        return valid = false;              
    }
    else
    {    
        alert (fieldName + " is not a valid phone number. Try a 7 number. \nex) 555-5555");
        textField.focus();
        textField.select();
        return valid = false;
    }
    
return valid;    // should not reach here    

}

function reformatShortTelephone(textField, fieldName, required)
{    
    // XXX-YYYY
    textValue = textField.value;
    var valid;
     textValue = form_stripCharsInPhone(textValue);
    valid = form_validateShortPhone(textField, textValue, fieldName, required);
    if (valid)
    {
        var returnNumber;
        if (textValue.length == 7)
        {
           returnNumber = textValue.substring(0,3) + "-" + textValue.substring(3,8);
           textField.value = returnNumber;
            return returnNumber;
        }
    }
    else
    {    
        return false;
    }


}

function form_validateAreaCode(textField, fieldName, required)
{
    textField.value = textField.value.trim();
    textValue = textField.value;
    
    if (!required && textValue.length == 0)
        return true;

    if (textValue.length != 3)
    {
        alert(fieldName + " is not a valid area code.");
        textField.focus();
        textField.select();
        return false;
    }
    
    var valid = form_validIntegersInGeneral(textValue);
    if (valid)
        return true;
    else
    {
        alert(fieldName + " is not a valid area code.");
        textField.focus();
        textField.select();
        return false;
    }
}

function form_capitalizeString(textValue)
{

    // This function requires that all leading and trailing blank characters
    // in a string MUST be stripped before it is passed in.
    // this function will capitalize the first letter in a string
    // and if there are spaces capitalize each other letter
    
    var returnString = textValue;
    for (var i=0; i< textValue.length; i++)
    {
        if (i==0)
        {
            returnString = textValue.charAt(i).toUpperCase();
            continue;
        }
            
        if (textValue.charAt(i-1) == " ")
        {    
            // if previous char was a blank then capitalize this one
            returnString += textValue.charAt(i).toUpperCase();
            continue;
        
        }
        returnString += textValue.charAt(i);    
    }
    return returnString;
}

function form_validateCity(textField, textValue, fieldName, required)
{
    textField.value = textField.value.trim();
    var textValue = textField.value;

    // call the validateString function
    var valid = form_validateString(textField, textValue, fieldName, required);
    if (valid)
    {
        // so now capitalize the string
        var returnString = form_capitalizeString(textValue);
        textField.value = returnString;
        return true;
        
    }
    else
        return false;

}
 
function form_validateEmail(textField, fieldName, required) {
    var valid;
    textField.value = textField.value.trim();
    textValue = textField.value;

    // nothing has been input into this form and it is not a required field so just return true and finish
    if (!required && textValue.length == 0)
        return true;
    
    // this is validation to see if the required field is there
    if (required == true)
    {
        valid = form_requireField(textField, fieldName);
        if (!valid) return false;
    }

    // now someone has either input information or it is now a required field
    valid = false

    var str = textValue; // email string
    var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)|(\s)/ ; // not valid
    var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,4})(\]?)$/; // valid
    if (!reg1.test(str) && reg2.test(str)) { // if syntax is valid
        valid = true;
    }

    if (valid != true) {
        alert (fieldName + " is not a valid");
        textField.focus();
        textField.select();
    }
                              
    return valid;
}


function form_validateTerms(textField, fieldName, required)
{
    var valid;
    textField.value = textField.value.trim();
    textValue = textField.value;

    // nothing has been input into this form and it is not a required field so just return true and finish
    if (!required && textValue.checked == 1)
        return true;
    
    // this is validation to see if the required field is there
    if (required == true)
    {
        valid = form_requireField(textField, fieldName);
        if (!valid) return false;
    }
	
    if (valid != true) {
        alert (fieldName + " have not been agreed to, please read and click on the checkbox");
    }
                              
    return valid;
}

function form_validateDate(textField, fieldName, required, silent)
{
// checks if date passed is in valid MM-DD-YY format 
// OR MM/DD/YY MMDDYY
    
    var valid;
    textField.value = textField.value.trim();
    textValue = textField.value;
        
    if  (!required && textValue.length == 0)
        return true;

    if (required == true)
    {
        valid = form_requireField(textField, fieldName);
        if (!valid) return false;
    }
    

// strip the chars in the dates
    textValue = form_stripCharsInDate(textValue);

// there is input if length != 6 then either separators were incorrect or missing fields
    if (textValue.length == 6) {
        if (form_validIntegersInGeneral(textValue)) {
            var month  = textValue.substring(0,2);
            var date = textValue.substring(2,4);
            var year  = textValue.substring(4,6);
            year = Number(year);

            (year < 70) ? year += 2000: year += 1900;

            var test = new Date(year,Number(month)-1,date);

            if (year == y2k(test.getYear()) && (Number(month)-1 == test.getMonth()) && (date == test.getDate())) {
                return true;
            } 
            else {
                if (!silent) {
	                // If month is not valid (1-12) show error message.
	            	if (Number(month) < 1 || Number(month) > 12) {
	            		alert(fieldName + " has an invalid date. Month part of MM-DD-YY is invalid.");
	            	}
	            	// else number of days must be incorrect
	            	else {
	            		alert(fieldName + " has an invalid date. Day part of MM-DD-YY is invalid.");
	            	}
                
                    textField.focus();
                    textField.select();
                }
                
                return false;
            }
        } else {    
            if (!silent) {
                alert(fieldName + " is an invalid Date. Try entering date as MM-DD-YY. " );
                textField.focus();
                textField.select();
            }
            return false;        
        }
    } else {
        if (!silent) {
            alert (fieldName + " is an invalid date. Try entering date as MM-DD-YY");
            textField.focus();
            textField.select();
        }
        return false;
    }
}



function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function form_stripCharsInDate(s)
{

    var i;
    var returnString = "";
        
    // Search through string's characters one by one.
    // If character is not a a dash or a / append to returnString.
        
     for (i = 0; i < s.length; i++)
     {   
         // Check that current character isn't whitespace.
         var c = s.charAt(i);
         if ((c!= "/") && (c!= "-") && (c!=".") && (c!=" ") )
            returnString += c;
    }


    return returnString;

}

function form_validateEIN1(textField, fieldName, required)
{
    textField.value = textField.value.trim();
    textValue = textField.value;
    
    if (!required && textValue.length == 0)
        return true;

    if (textValue.length != 2)
    {
        alert(fieldName + " is not a valid number please try 12.");
        textField.focus();
        textField.select();
        return false;
    }
    
    // this is validation to see if there are acceptable integers and they are not all blank
    textValue = form_stripCommaAndWhitespace(textValue);
    valid = form_validIntegersInGeneral(textValue);
    
    if (valid)
    {
        // change the textfield value to the one that was stripped
        textField.value = textValue;
        return true; // the field is valid so return true
    }
    else    // the field is invalid
    {
        alert (fieldName + " is invalid. Please enter a valid number e.g. 42");
        textField.focus();
        textField.select();
        return false;
    }
}

function form_validateEIN2(textField, fieldName, required)
{
    textField.value = textField.value.trim();
    textValue = textField.value;
    
    if (!required && textValue.length == 0)
        return true;

    if (textValue.length != 7)
    {
        alert(fieldName + " is not a valid number please try 1234567.");
        textField.focus();
        textField.select();
        return false;
    }
    
    // this is validation to see if there are acceptable integers and they are not all blank
    textValue = form_stripCommaAndWhitespace(textValue);
    valid = form_validIntegersInGeneral(textValue);
    
    if (valid)
    {
        // change the textfield value to the one that was stripped
        textField.value = textValue;
        return true; // the field is valid so return true
    }
    else    // the field is invalid
    {
        alert (fieldName + " is invalid. Please enter a valid number e.g. 42");
        textField.focus();
        textField.select();
        return false;
    }
}

function form_validateMCNumber(textField, fieldName, required)
{
    textField.value = textField.value.trim();
    textValue = textField.value;
    
    if (!required && textValue.length == 0)
        return true;

    if (textValue.length != 6)
    {
        alert(fieldName + " is not a valid number please try 123456.");
        textField.focus();
        textField.select();
        return false;
    }
    
    // this is validation to see if there are acceptable integers and they are not all blank
    textValue = form_stripCommaAndWhitespace(textValue);
    valid = form_validIntegersInGeneral(textValue);
    
    if (valid)
    {
        // change the textfield value to the one that was stripped
        textField.value = textValue;
        return true; // the field is valid so return true
    }
    else    // the field is invalid
    {
        alert (fieldName + " is invalid. Please enter a valid number e.g. 123456");
        textField.focus();
        textField.select();
        return false;
    }
}

function form_validateDOTNumber(textField, fieldName, required)
{
    textField.value = textField.value.trim();
    textValue = textField.value;
    
    if (!required && textValue.length == 0)
        return true;

    if (textValue.length != 7)
    {
        alert(fieldName + " is not a valid number please try 1234567.");
        textField.focus();
        textField.select();
        return false;
    }
    
    // this is validation to see if there are acceptable integers and they are not all blank
    textValue = form_stripCommaAndWhitespace(textValue);
    valid = form_validIntegersInGeneral(textValue);
    
    if (valid)
    {
        // change the textfield value to the one that was stripped
        textField.value = textValue;
        return true; // the field is valid so return true
    }
    else    // the field is invalid
    {
        alert (fieldName + " is invalid. Please enter a valid number e.g. 1234567");
        textField.focus();
        textField.select();
        return false;
    }
}

function form_validateTime(textField, textValue, fieldName, required)
{
    // in HH:MM
    var hours;
    var minutes;
    var valid1;
    var valid2;
    var returnString;

    // strip out all the unwanted characters
    textField.value = textField.value.trim();
    var textValue = textField.value;
    textValue = form_stripCharsInTime(textValue);
    
    if (!required && textValue.length == 0)
        return valid = true;
    
    if (required == true) 
    {
        valid = form_requireField(textField, fieldName);
        if (!valid) return false;
    }

    validInts = form_validIntegersInGeneral(textValue);
    if (!validInts)
    {
        alert("The time is not in the correct format. Please enter the time in 24 hour format HH:MM \nex. 05:30 for 5:30am,  17:30 for 5:30pm and 00:00 for Midnight. ");
        textField.focus();
        textField.select();
        return false;
    }
    
    if (textValue.length == 4)
    {
        // if the length of the string is four then it is in the form HHMM 
        hours = textValue.substring(0,2);
        minutes = textValue.substring(2,4);
        
        valid1 = form_validateHours(hours);
        valid2 = form_validateMinutes(minutes);

        if ((valid1 && valid2))    
        {
            returnString = form_reformatTime(textValue);
            textField.value = returnString;
            return true;            
        }
        else
        {
            alert("The time is not in the correct format. Please enter the time in 24 hour format HH:MM \nex. 05:30 for 5:30am,  17:30 for 5:30pm and 00:00 for Midnight. ");
            textField.focus();
            textField.select();
            return false;
        }
    
    }
    else 
    {
        alert("The time is not in the correct format. Please enter the time in 24 hour format HH:MM \nex. 05:30 for 5:30am,  17:30 for 5:30pm and 00:00 for Midnight. ");
        textField.focus();
        textField.select();
        return false;
    }

}

function form_validateHours(textValue)
{
    // 24 = number of hours in a day.
    if (textValue < 24 && textValue >= 0)
        return true;
    else
        return false;
}

function form_validateMinutes(textValue)
{
    // 60 = number of minutes in an hour
    if (textValue < 60 && textValue >= 0)
        return true;
    else
        return false;
}

function form_stripCharsInTime(s)
{
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is a colon , don't append to return string.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if ((c!= ":") )
        returnString += c;
    }

    return returnString;
    

}

// reformats a time into HH:MM
function form_reformatTime(textValue)
{    
    var hours;
    var minutes;
    var returnString = "";
    if (textValue.length == 3) // ie HMM
    {
        hours = textValue.substring(0,1);
        minutes = textValue.substring(1,3);
        returnString = "0" + hours + ":" + minutes;
         return returnString;
    }

    if (textValue.length == 4) // ie HHMM
    {
        hours = textValue.substring(0,2);
        minutes = textValue.substring(2,4);
        returnString = hours + ":" + minutes;
         return returnString;
    }
    
    // should never reach here
    return returnString;

}

