// JScript source code
function validateForm() {
	var error = false;

	//Checks to see if FNAME was left blank.  If so changes color to yellow,
	//if not changes color back to white.
	if (document.getElementById("RequestInfo").FNAME.value == "") {
	    document.getElementById("RequestInfo").FNAME.style.backgroundColor = '#F7FF55';
	    error = true;
	}else {
	    document.getElementById("RequestInfo").FNAME.style.backgroundColor = 'white';
	}

	//Checks to see if LNAME was left blank.  If so changes color to yellow,
	//if not changes color back to white.
	if (document.getElementById("RequestInfo").LNAME.value == "") {
	    document.getElementById("RequestInfo").LNAME.style.backgroundColor = '#F7FF55';
	    error = true;
	}else {
	    document.getElementById("RequestInfo").LNAME.style.backgroundColor = 'white';
	}

	//Checks to see if ADDRESS1 was left blank.  If so changes color to yellow,
	//if not changes color back to white.
	if (document.getElementById("RequestInfo").ADDRESS1.value == "") {
	    document.getElementById("RequestInfo").ADDRESS1.style.backgroundColor = '#F7FF55';
	    error = true;
	}else {
	    document.getElementById("RequestInfo").ADDRESS1.style.backgroundColor = 'white';
	}
	
	//Checks to see if CITY was left blank.  If so changes color to yellow,
	//if not changes color back to white.
	if (document.getElementById("RequestInfo").CITY.value == "") {
	    document.getElementById("RequestInfo").CITY.style.backgroundColor = '#F7FF55';
	    error = true;
	}else {
	    document.getElementById("RequestInfo").CITY.style.backgroundColor = 'white';
	}
	
	//Checks to see if COUNTRY was left blank.  If so changes color to yellow,
	//if not changes color back to white.
	if (document.getElementById("RequestInfo").COUNTRY.value == "") {
	    document.getElementById("RequestInfo").COUNTRY.style.backgroundColor = '#F7FF55';
	    error = true;
	}else {
	    document.getElementById("RequestInfo").COUNTRY.style.backgroundColor = 'white';
	}
	
	//Checks to see if ZIP was left blank.  If so changes color to yellow,
	//if not changes color back to white.
	if (document.getElementById("RequestInfo").ZIP.value == "") {
	    document.getElementById("RequestInfo").ZIP.style.backgroundColor = '#F7FF55';
	    error = true;
	}else {
	    document.getElementById("RequestInfo").ZIP.style.backgroundColor = 'white';
	}
	
	//Checks to see if EMAIL was left blank.  If so changes color to yellow,
	//if not changes color back to white.
	if (document.getElementById("RequestInfo").EMAIL.value == "") {
	    document.getElementById("RequestInfo").EMAIL.style.backgroundColor = '#F7FF55';
	    error = true;
	}else {
	    document.getElementById("RequestInfo").EMAIL.style.backgroundColor = 'white';
	}
	
	//Checks to see if EMAIL_2 was left blank.  If so changes color to yellow,
	//if not changes color back to white.
	if (document.getElementById("RequestInfo").EMAIL_2.value == "") {
	    document.getElementById("RequestInfo").EMAIL_2.style.backgroundColor = '#F7FF55';
	    error = true;
	}else {
	    document.getElementById("RequestInfo").EMAIL_2.style.backgroundColor = 'white';
	}
	
	//Checks to see if DOB was left blank.  If so changes color to yellow,
	//if not changes color back to white.
	if (document.getElementById("RequestInfo").DOB.value == "") {
	    document.getElementById("RequestInfo").DOB.style.backgroundColor = '#F7FF55';
	    error = true;
	}else {
	    document.getElementById("RequestInfo").DOB.style.backgroundColor = 'white';
	}
    	
	//Checks to see if the first radio button under group school is checked, if so checks to
	//see if user entered data into HIGH_SCHOOL.  If not turns the field yellow.
	if (document.getElementById("RequestInfo").school[0].checked && document.getElementById("RequestInfo").HIGH_SCHOOL.value == ""){ 
		document.getElementById("RequestInfo").HIGH_SCHOOL.style.backgroundColor = '#F7FF55';
	    error = true;
	}else {
	    document.getElementById("RequestInfo").HIGH_SCHOOL.style.backgroundColor = 'white';
	}

    //Checks to see if the second radio button under group school is checked, if so checks to
    //see if user entered data into COLLEGE.  If not turns the field yellow.
	if (document.getElementById("RequestInfo").school[1].checked && document.getElementById("RequestInfo").COLLEGE.value == ""){ 
		document.getElementById("RequestInfo").COLLEGE.style.backgroundColor = '#F7FF55';
	    error = true;
	}else {
	    document.getElementById("RequestInfo").COLLEGE.style.backgroundColor = 'white';
	}

    //Checks to see if the email addresses in EMAIL and EMAIL_2 match.  If not turns both
    //fields yellow.
	if (document.getElementById("RequestInfo").EMAIL.value != document.getElementById("RequestInfo").EMAIL_2.value) {
	    document.getElementById("RequestInfo").EMAIL.style.backgroundColor = '#F7FF55';
	    document.getElementById("RequestInfo").EMAIL_2.style.backgroundColor = '#F7FF55';
	    error = true;
    }
    
    //Checks to see if the phone number given is 10 digits.  Also checks to see
    //if all characters in the phone number given are numbers.  If either of these tests fail,
    //The field is turned yellow.
    if (!IsNumeric(document.getElementById("RequestInfo").PHONE.value) || document.getElementById("RequestInfo").PHONE.value.length != 10) {
        document.getElementById("RequestInfo").PHONE.style.backgroundColor = '#F7FF55';
        error = true;
    } else {
        document.getElementById("RequestInfo").PHONE.style.backgroundColor = 'white';
    }

    //Checks to see if the SSN given is 9 digits or is empty. If the test fails the
    //field is turned yellow.
    if (document.getElementById("RequestInfo").SSN.value.length != 9 && document.getElementById("RequestInfo").SSN.value.length != 0) {
        document.getElementById("RequestInfo").SSN.style.backgroundColor = '#F7FF55';
        error = true;
    } else {
        document.getElementById("RequestInfo").SSN.style.backgroundColor = 'white';
    }
    
    //Insures that the SSN's value is numeric.
    if (!IsNumeric(document.getElementById("RequestInfo").SSN.value)) {
        document.getElementById("RequestInfo").SSN.style.backgroundColor = '#F7FF55';
        error = true;
    }

    //Checks to see if the birthdate given is valid, if not turns the field yellow.
    if (!ValidDay(document.getElementById("RequestInfo").DOB.value)) {
        document.getElementById("RequestInfo").DOB.style.backgroundColor = '#F7FF55';
        error = true;
    } else {
        document.getElementById("RequestInfo").DOB.style.backgroundColor = 'white';
    }
   
    //If their zip code fails zip validation change color.
    if(!ValidZip(document.getElementById("RequestInfo").ZIP.value)) {
       document.getElementById("RequestInfo").ZIP.style.backgroundColor = '#F7FF55';
       error = true;
    }
    
    //Makes sure that STATE isn't the default value.
    if(document.getElementById("RequestInfo").STATE.value == "Choose Your State") {
        error = true;
    }
      
    //If no errors were found on the page, the page will be submitted, otherwise
    //the window will be scrolled back up to the top.
    if (!error) {
        document.getElementById("RequestInfo").submit();
    } else {
        window.scroll(0,0);
    }
}

//This function tests to see if all characters in a string are numeric or not.
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;
}

//This function tests dates given to see if the date given was correctly entered.
function ValidDay(sText) {
    var Month;
    var Day;
    var Year;
    var Separator = "/";
    
    if(sText.length != 10) {
        return false;
    }
    
    Month = sText.charAt(0) + sText.charAt(1);
    Day = sText.charAt(3) + sText.charAt(4);
    Year = sText.charAt(6) + sText.charAt(7) + sText.charAt(8) + sText.charAt(9);

    if(!IsNumeric(Month) || Month < 1 || Month > 12) {
        return false;
    }

    if(!IsNumeric(Year) || Year < 1900 || Year > 2008) {
        return false;
    }

    if(!IsNumeric(Day)) {
        return false;
    } else if(Month30(Month) && Day > 30) {
        return false;
    } else if(Month == 2 && ((Year % 4 == 0 && Day > 29) || (Year % 4 != 0 && Day > 28))) {
        return false;
    } else if(Day > 31) {
        return false;
    }
       
    if(sText.charAt(2) != Separator || sText.charAt(5) != Separator) {
        return false;
    }
    
    return true;
}

//This function checks to see if the month given has 30 days in it.  If so
//returns true, otherwise returns false.
function Month30(Month) {
    if(Month == 4 || Month == 6 || Month == 9 || Month == 11) {
        return true;
    } else {
        return false;
    }
}

//This function insures that the zipcode is in the correct form.
function ValidZip(zip) {
    if (zip.length == 5) {
        return IsNumeric(zip);
    }else if(zip.length == 10) {
        var check = true;
        var Region = "";
        var Local = "";
        var Separator = "";
        
        Region = zip.charAt(0) + zip.charAt(1) + zip.charAt(2) + 
                 zip.charAt(3) + zip.charAt(4);
                 
        Local =  zip.charAt(6) + zip.charAt(7) + zip.charAt(8) +
                 zip.charAt(9);
                 
        Separator = zip.charAt(5);        
       
        if(!IsNumeric(Region) || !IsNumeric(Local) || Separator != "-") {
            return false;
        } else {
            return true;
        }
    } else {
        return false;
    }
}

