
function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function isWhitespace (s)

{   var i;
	var whitespace = " \t\n\r";
    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    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 stripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

function isInteger (s) {   
	var i;

   	 if (isEmpty(s)) 
   	    if (isInteger.arguments.length == 1) return false;
   	    else return (isInteger.arguments[1] == true);

   	 // Search through string's characters one by one
   	 // until we find a non-numeric character.
   	 // When we do, return false; if we don't, return true.

   	 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 isUSPhoneNumber (s)
{   

var digitsInUSPhoneNumber = 10;

if (isEmpty(s)) 
       if (isUSPhoneNumber.arguments.length == 1) return false;
       else return (isUSPhoneNumber.arguments[1] == true);
    return (isInteger(s) && s.length == digitsInUSPhoneNumber)
}

function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return true;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    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 checkUSPhone (theField, emptyOK)
{   

var phoneNumberDelimiters = "()- .";

if (checkUSPhone.arguments.length == 1) emptyOK = false;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {  var normalizedPhone = stripCharsInBag(theField.value, phoneNumberDelimiters)
       if (!isUSPhoneNumber(normalizedPhone, false)) 
          return false;
       else 
       {  // if you don't want to reformat as (123) 456-789, comment next line out
          //theField.value = reformatUSPhone(normalizedPhone)
          return true;
       }
    }
}

function isZIPCode (s){  
	
	var digitsInZIPCode1 = 5;
	var digitsInZIPCode2 = 9;
		if (isEmpty(s)) 
  	   		if (isZIPCode.arguments.length == 1) 
  	   			return false;
  	     	else 
  	     		return (isZIPCode.arguments[1] == true);
  	 return (isInteger(s) && ((s.length == digitsInZIPCode1) ||(s.length == digitsInZIPCode2)))
	}

function checkEmail (theField, emptyOK)
{   if (checkEmail.arguments.length == 1) emptyOK = true;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else if (!isEmail(theField.value, false)) 
       return false;
    else return true;
}


function SubmitForm() {
		iFormError = 0;
		sCount = 0;
		Public = 0; //check to make sure this is a public family for Jersey City 2003
		strPublic = "";
		iSpanish = 0
		var StudentArray = new Array(8);
		var StudentArrayLast = new Array(8);
		var GradeArray = new Array(8);
		var CurrSchoolArray = new Array(8);
		sErrorText = "";
		sNotify = "";
		var ZipCodeValue;

		iSpanish = document.ExpansionForm.Spanish.value
		
		
		if (document.ExpansionForm.FirstTime.value == "") {
			iFormError = iFormError + 1;
			if (iSpanish > 0){
				sErrorText = sErrorText + "- Por favor declare si este es su primera vez aplicando.\n";
			}
			else{
				sErrorText = sErrorText + "- Please specify whether or not this is your first time applying.\n";
			}
				
		}
		if (document.ExpansionForm.HighIncome.value == "") {
			iFormError = iFormError + 1;
			if (iSpanish > 0){
				sErrorText = sErrorText + "- Por favor declare si su ingreso es sobre el ingreso indicado.\n"
			}
			else{
				sErrorText = sErrorText + "- Please state whether or not your household income is higher than\n";
				sErrorText = sErrorText + "  what is stated in the table.\n";
			}

		} 
		if (document.ExpansionForm.FirstName.value == "" || document.ExpansionForm.FirstName.value == "?") {
			iFormError = iFormError + 1;
			if (iSpanish > 0){
				sErrorText = sErrorText + "- Nombre\n";
			}
			else{
				sErrorText = sErrorText + "- First Name\n";
			}
		}
		if (document.ExpansionForm.LastName.value == "" || document.ExpansionForm.LastName.value == "?") {
			iFormError = iFormError + 1;
			if (iSpanish > 0){
				sErrorText = sErrorText + "- Apellido\n";
			}
			else{
				sErrorText = sErrorText + "- Last Name\n";
			}
		}
		if (document.ExpansionForm.Address.value == "" || document.ExpansionForm.Address.value == "?") {
			iFormError = iFormError + 1;
			if (iSpanish > 0){
				sErrorText = sErrorText + "- Dirección\n";
			}
			else{
				sErrorText = sErrorText + "- Mailing Address\n";
			}
		}
		if (document.ExpansionForm.City.value == "" || document.ExpansionForm.City.value == "?") {
			iFormError = iFormError + 1;
			if (iSpanish > 0){
				sErrorText = sErrorText + "- Cuidad\n";
			}
			else{
				sErrorText = sErrorText + "- City\n";
			}
		}		
		if (document.ExpansionForm.State.value == "" || document.ExpansionForm.State.value == "?") {
			iFormError = iFormError + 1;
			if (iSpanish > 0){
				sErrorText = sErrorText + "- Estado\n";
			}
			else{
				sErrorText = sErrorText + "- State\n";
			}
		}	
		
		if (document.ExpansionForm.Zip.value == "" || document.ExpansionForm.Zip.value == "?") {
			iFormError = iFormError + 1;
			if (iSpanish > 0){
				sErrorText = sErrorText + "- Código Postal\n";
			}
			else{
				sErrorText = sErrorText + "- Zip Code\n";
			}
		}
		else if (isZIPCode(document.ExpansionForm.Zip.value , false) == false ) {
			iFormError = iFormError + 1;
			if (iSpanish > 0){
				sErrorText = sErrorText + "- El Código Postal es inválido. Por favor ecriba a máquina su código postal, el número tiene 5 dígitos.  (por ejemplo  94043)\n";
			}
			else{
				sErrorText = sErrorText + "- Zip Code invalid, please enter 5 digit U.S. ZIP Code (like 94043)\n";
			}
		}
		if (document.ExpansionForm.Phone.value == "" || document.ExpansionForm.Phone.value == "?") {
			iFormError = iFormError + 1;
			if (iSpanish > 0){
				sErrorText = sErrorText + "- Número de teléfono\n";
			}
			else{
				sErrorText = sErrorText + "- Phone\n";
			}
		}
		else if (checkUSPhone(document.ExpansionForm.Phone , false) == false ) {
			iFormError = iFormError + 1;
			if (iSpanish > 0){
				sErrorText = sErrorText + "- El número de teléfono es inválido. Por favor ecriba a máquina su número de teléfono, el número tiene 10 dígitos.  (por ejemplo 415 555 1212)\n";
			}
			else{
				sErrorText = sErrorText + "- Phone number invalid, please enter 10 digit U.S. phone number (like 415 555 1212)\n";
			}
		}
		
		if (!(checkEmail(document.ExpansionForm.Email, true))) {
			iFormError = iFormError + 1;
			if (iSpanish > 0){
				sErrorText = sErrorText + "- El correo electrónico es inválido, por favor escriba a máquina su correo electrónico correcto (como su dirreción@algúnlugar.com)\n";
			}
			else{
				sErrorText = sErrorText + "- Email invalid, please enter correct Email (like youraddress@someplace.com)\n";
			}
		}

		//Gather the data for checks on the student data
		StudentArray[0] = document.ExpansionForm.CFirstName1.value;
		StudentArray[1] = document.ExpansionForm.CFirstName2.value;
		StudentArray[2] = document.ExpansionForm.CFirstName3.value;
		StudentArray[3] = document.ExpansionForm.CFirstName4.value;
		StudentArray[4] = document.ExpansionForm.CFirstName5.value;
		StudentArray[5] = document.ExpansionForm.CFirstName6.value;
		StudentArray[6] = document.ExpansionForm.CFirstName7.value;
		StudentArray[7] = document.ExpansionForm.CFirstName8.value;
		StudentArrayLast[0] = document.ExpansionForm.CLastName1.value;
		StudentArrayLast[1] = document.ExpansionForm.CLastName2.value;
		StudentArrayLast[2] = document.ExpansionForm.CLastName3.value;
		StudentArrayLast[3] = document.ExpansionForm.CLastName4.value;
		StudentArrayLast[4] = document.ExpansionForm.CLastName5.value;
		StudentArrayLast[5] = document.ExpansionForm.CLastName6.value;
		StudentArrayLast[6] = document.ExpansionForm.CLastName7.value;
		StudentArrayLast[7] = document.ExpansionForm.CLastName8.value;
		GradeArray[0] = document.ExpansionForm.Grade1.value;
		GradeArray[1] = document.ExpansionForm.Grade2.value;
		GradeArray[2] = document.ExpansionForm.Grade3.value;
		GradeArray[3] = document.ExpansionForm.Grade4.value;
		GradeArray[4] = document.ExpansionForm.Grade5.value;
		GradeArray[5] = document.ExpansionForm.Grade6.value;
		GradeArray[6] = document.ExpansionForm.Grade7.value;
		GradeArray[7] = document.ExpansionForm.Grade8.value;
		CurrSchoolArray[0] = document.ExpansionForm.CurrSchool1.value;
		CurrSchoolArray[1] = document.ExpansionForm.CurrSchool2.value;
		CurrSchoolArray[2] = document.ExpansionForm.CurrSchool3.value;
		CurrSchoolArray[3] = document.ExpansionForm.CurrSchool4.value;
		CurrSchoolArray[4] = document.ExpansionForm.CurrSchool5.value;
		CurrSchoolArray[5] = document.ExpansionForm.CurrSchool6.value;
		CurrSchoolArray[6] = document.ExpansionForm.CurrSchool7.value;
		CurrSchoolArray[7] = document.ExpansionForm.CurrSchool8.value;
		
		//count the number of students listed
		//if they have entered any fields they are counted
		for (i = 0; i  < 8; i++) {
			if (StudentArray[i] != "" || StudentArrayLast[i] != "" || StudentArrayLast[i] == "?" || StudentArray[i] == "?" || 
				isInteger(GradeArray[i]) || CurrSchoolArray[i] != "" ) 
				sCount++;	
		}
		
		//check for any students listed
		if (sCount < 1) {
			if (iSpanish > 0){
				sErrorText = sErrorText + "- Por lo menos un estudiante debe ser llenado.\n";
			}
			else{
				sErrorText = sErrorText + "- At least one student should be filled in.\n";
			}
			iFormError = iFormError + 1;
		}
		else {
			document.ExpansionForm.CountOfStudents.value = sCount;
		}
		
		//check for grade and current school type entered for filled in students
		for (i = 0; i < sCount; i++) {
		
			//K should be entered as 0, not k or K
			if (GradeArray[i] == "k" || GradeArray[i] == "K") {
				iFormError = iFormError + 1;
				if (iSpanish > 0){
					sErrorText = sErrorText + "- El estudiante con el número " + (i+1) + " debe de tener un '0' como su grado, la letra 'k' o 'K' está incorrecta.\n";
				}
				else{
					sErrorText = sErrorText + "- Student number " + (i+1) + " should have a '0' as their grade, not 'k' or 'K'.\n";
				}
			}
		
			if (GradeArray[i] == "" || GradeArray[i] == "?"  ) {
				if (iSpanish > 0){
					sErrorText = sErrorText + "- El estudiante con el número " + (i+1) + " falta un grado.\n";
				}
				else{
					sErrorText = sErrorText + "- Student number " + (i+1) + " is missing a grade.\n";
				}
				iFormError = iFormError + 1;	
			}
			else if (!isInteger(GradeArray[i])) {
				if (iSpanish > 0){
					sErrorText = sErrorText + "- El estudiante con el número " + (i+1) + " falta un grado (K hasta el cinco).\n";
				}
				else{
					sErrorText = sErrorText + "- Student number " + (i+1) + " needs grade 0-5.\n";
				}
				iFormError = iFormError + 1;
			}//endelseif

			if (StudentArray[i] == "" || StudentArrayLast[i] == "") {
				if (iSpanish > 0){
					sErrorText = sErrorText + "- El estudiante con el número " + (i+1) + " falta el nombre o apellido.\n";
				}
				else{
					sErrorText = sErrorText + "- Student number " + (i+1) + " requires a first AND last name.\n";
				}
				iFormError = iFormError + 1;
			}//endif
					
			if ( CurrSchoolArray[i] == "" ) {
				if (iSpanish > 0){
					sErrorText = sErrorText + "- El estudiante con el número " + (i+1) + " falta el tipo de escuela.\n";
				}
				else{
					sErrorText = sErrorText + "- Student number " + (i+1) + " is missing a school type.\n";
				}
				iFormError = iFormError + 1;
			}//endif

			
			//check the Public Status
			if (isInteger(GradeArray[i]) || CurrSchoolArray[i] == "Government")
			{
				if (CurrSchoolArray[i] == "Government" || GradeArray[i] == 0)
				{
					Public += 1;
				}
				
			}//end if Public check

		}//end for

		//check for public only status.  Comment out if not needed.

		if ( Public > 0 ) 
		{	
			document.ExpansionForm.FamPubPriv.value = "Public"
		}//endif
		
		if (iFormError != 0) {
			if (iSpanish > 0){
				sErrorText = "Requieren la información siguiente:\n\n" + sErrorText + "\nPor favor complete la forma antes del sometimiento."
			}
			else{
				sErrorText = "The following information is required:\n\n" + sErrorText + "\nPlease complete the form before submitting."
			}
			alert(sErrorText);
		} else {
			document.ExpansionForm.action = 'send.asp'
			document.ExpansionForm.submit();
		}

	}//end Submitform
	
	function ResetForm() {
		document.ExpansionForm.reset();
	}
