﻿function clearForm() {
    document.RequestCard.FirstName.value = "";
    document.RequestCard.FirstName.style.backgroundColor = 'white';
    document.RequestCard.MiddleInitial.value = "";
    document.RequestCard.MiddleInitial.style.backgroundColor = 'white';
    document.RequestCard.LastName.value = "";
    document.RequestCard.LastName.style.backgroundColor = 'white';
    document.RequestCard.POBox.value = "";
    document.RequestCard.POBox.style.backgroundColor = 'white';
    document.RequestCard.StreetAddress.value = "";
    document.RequestCard.StreetAddress.style.backgroundColor = 'white';
    document.RequestCard.City.value = "";
    document.RequestCard.City.style.backgroundColor = 'white';
    document.RequestCard.State.item = 0;
    document.RequestCard.ZipCode.value = "";
    document.RequestCard.ZipCode.style.backgroundColor = 'white';
    document.RequestCard.DayPhone.value = "";
    document.RequestCard.DayPhone.style.backgroundColor = 'white';
    document.RequestCard.HomePhone.value = "";
    document.RequestCard.HomePhone.style.backgroundColor = 'white';
    document.RequestCard.MessagePhone.value = "";
    document.RequestCard.MessagePhone.style.backgroundColor = 'white';
    document.RequestCard.EmailAddress.value = "";
    document.RequestCard.EmailAddress.style.backgroundColor = 'white';
    document.RequestCard.Birthdate.value = "";
    document.RequestCard.Birthdate.style.backgroundColor = 'white';
    document.RequestCard.PINnumber.value = "";
    document.RequestCard.PINnumber.style.backgroundColor = 'white';
    document.RequestCard.Agree.checked = false;
    window.scroll(0,0);
}

function validateForm() {
    var error = false;
    var alertText = "";
    
	//Checks to see if FirstName was left blank.  If so changes color to yellow,
	//if not changes color back to white.
	if (document.RequestCard.FirstName.value == "") {
	    document.RequestCard.FirstName.style.backgroundColor = '#F7FF55';
	    error = true;
	}else {
	    document.RequestCard.FirstName.style.backgroundColor = 'white';
	}
	
	//Checks to see if MiddleInitial was left blank.  If so changes color to yellow,
	//if not changes color back to white.
	if (document.RequestCard.MiddleInitial.value == "") {
	    document.RequestCard.MiddleInitial.style.backgroundColor = '#F7FF55';
	    error = true;
	}else {
	    document.RequestCard.MiddleInitial.style.backgroundColor = 'white';
	}
	
	//Checks to see if LastName was left blank.  If so changes color to yellow,
	//if not changes color back to white.
	if (document.RequestCard.LastName.value == "") {
	    document.RequestCard.LastName.style.backgroundColor = '#F7FF55';
	    error = true;
	}else {
	    document.RequestCard.LastName.style.backgroundColor = 'white';
	}
	
	//Checks to see if StreetAddress was left blank.  If so changes color to yellow,
	//if not changes color back to white.
	if (document.RequestCard.StreetAddress.value == "") {
	    document.RequestCard.StreetAddress.style.backgroundColor = '#F7FF55';
	    error = true;
	}else {
	    document.RequestCard.StreetAddress.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.RequestCard.City.value == "") {
	    document.RequestCard.City.style.backgroundColor = '#F7FF55';
	    error = true;
	}else {
	    document.RequestCard.City.style.backgroundColor = 'white';
	}
	
	//Checks to see if ZipCode was left blank.  If so changes color to yellow,
	//if not changes color back to white.
	if (document.RequestCard.ZipCode.value == "") {
	    document.RequestCard.ZipCode.style.backgroundColor = '#F7FF55';
	    error = true;
	}else {
	    document.RequestCard.ZipCode.style.backgroundColor = 'white';
	}

	//Checks to see if all the phone fields were left blank.  If so changes color to yellow,
	//if not changes color back to white.
	if (document.RequestCard.DayPhone.value == "" && 
	    document.RequestCard.HomePhone.value == "" &&
		document.RequestCard.MessagePhone.value == "") {
	    
	    document.RequestCard.DayPhone.style.backgroundColor = '#F7FF55';
   	    document.RequestCard.HomePhone.style.backgroundColor = '#F7FF55';
	    document.RequestCard.MessagePhone.style.backgroundColor = '#F7FF55';

	    error = true;
	}else {
	    document.RequestCard.DayPhone.style.backgroundColor = 'white';
	    document.RequestCard.HomePhone.style.backgroundColor = 'white';
	    document.RequestCard.MessagePhone.style.backgroundColor = 'white';
	}

    //Checks to see if EmailAddress was left blank.  If so changes color to yellow,
	//if not changes color back to white.
	if (document.RequestCard.EmailAddress.value == "") {
	    document.RequestCard.EmailAddress.style.backgroundColor = '#F7FF55';
	    error = true;
	}else {
	    document.RequestCard.EmailAddress.style.backgroundColor = 'white';
	}
	
	//Checks to see if Birthdate was left blank.  If so changes color to yellow,
	//if not changes color back to white.
	if (document.RequestCard.Birthdate.value == "") {
	    document.RequestCard.Birthdate.style.backgroundColor = '#F7FF55';
	    error = true;
	}else {
	    document.RequestCard.Birthdate.style.backgroundColor = 'white';
	}

	//Checks to see if PINnumber was left blank.  If so changes color to yellow,
	//if not changes color back to white.
	if (document.RequestCard.PINnumber.value == "") {
	    document.RequestCard.PINnumber.style.backgroundColor = '#F7FF55';
	    error = true;
	}else {
	    document.RequestCard.PINnumber.style.backgroundColor = 'white';
	}

    //If DayPhone is not either blank, or have 10 characters or if DayPhone
    //isn't entirely numeric change color.
    if((document.RequestCard.DayPhone.value.length != 10 && 
        document.RequestCard.DayPhone.value.length != 0) || 
        !IsNumeric(document.RequestCard.DayPhone.value)) {
       
        document.RequestCard.DayPhone.style.backgroundColor = '#F7FF55';
        error = true;
       }

    //If HomePhone is not either blank, or have 10 characters or if HomePhone
    //isn't entirely numeric change color.
    if((document.RequestCard.HomePhone.value.length != 10 && 
        document.RequestCard.HomePhone.value.length != 0) || 
        !IsNumeric(document.RequestCard.HomePhone.value)) {
       
        document.RequestCard.HomePhone.style.backgroundColor = '#F7FF55';
        error = true;
       }

    //If MessagePhone is not either blank, or have 10 characters or if MessagePhone
    //isn't entirely numeric change color.
    if((document.RequestCard.MessagePhone.value.length != 10 && 
        document.RequestCard.MessagePhone.value.length != 0) || 
        !IsNumeric(document.RequestCard.MessagePhone.value)) {
       
        document.RequestCard.MessagePhone.style.backgroundColor = '#F7FF55';
        error = true;
       }
       
     //If their birthday fails date validation change color  
     if(!ValidDay(document.RequestCard.Birthdate.value)) {
   	    document.RequestCard.Birthdate.style.backgroundColor = '#F7FF55';
   	    error = true;
   	   }
   	   
   	 //If their zip code fails zip validation change color.
   	 if(!ValidZip(document.RequestCard.ZipCode.value)) {
   	    document.RequestCard.ZipCode.style.backgroundColor = '#F7FF55';
   	    error = true;
   	 }
   	 
     //If there has been an error informs user to correct any fields
     //shaded yellow.
     if(error) {
        alertText += "Please correct any fields shaded yellow!\n";
     }

	//Checks to see if State was left on the default option.  If so changes
	//color, if not changes color back to white.
    if (document.RequestCard.State.value == "empty") {
        alertText += "You must choose a state before continueing!\n"
	    error = true;
    }	    

   	 //If they do not agree to conditions error.
   	 if(document.RequestCard.Agree.checked != true) {
   	    error = true;
   	    alertText += "You must check the checkbox stating that you agree!";
   	 }
   	 
     //If there was an error or they didn't agree scroll to the
     //top of the window, otherwise submit the page.
     if(!error) {
        document.RequestCard.submit();
     } else {
        if (alertText != "") {
            alert(alertText);
        }
        
        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;
    }
}