function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
		limitCount.value = 0;
		alert('Your text exceeds the maximum\nlimit and has been truncated to fit.');
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function writeCookie(cname,cvalue,days) {
 if (days) {
  var date = new Date();
  date.setTime(date.getTime()+(days*24*60*60*1000));
  var expires = "; expires="+date.toGMTString();
 } else {
	var expires = "";
 }
 document.cookie = cname+"="+encodeURIComponent(cvalue)+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return decodeURIComponent(c.substring(nameEQ.length,c.length));
  }
  return "";
}

function addToFavorites() { 
  if (window.external) {
    window.external.AddFavorite("http://www.rhackett.net/HATS/GrantApp.htm","HATS STEDTRAIN Grant Application") 
  } else { 
    alert("Sorry! Your browser doesn't support this function."); 
  } 
}

function updateLimits() {
	limitText(document.grantApp.Project_Abstract,document.grantApp.countdown1,600);
	limitText(document.grantApp.Project_Activities,document.grantApp.countdown2,1600);
	limitText(document.grantApp.Project_Management,document.grantApp.countdown3,800);
	limitText(document.grantApp.Time_Schedule,document.grantApp.countdown4,1200);
	limitText(document.grantApp.Project_Budget,document.grantApp.countdown5,1200);
	limitText(document.grantApp.Eval_Criteria,document.grantApp.countdown6,1400);
}

function countBox(state) {
	if (state) {
		document.grantApp.Prior_Grants.value = parseInt(document.grantApp.Prior_Grants.value) + 1;
	} else {
		document.grantApp.Prior_Grants.value = parseInt(document.grantApp.Prior_Grants.value) - 1;		
	}
}

function countGrade(state) {
	if (state) {
		document.grantApp.Grades_Involved.value = parseInt(document.grantApp.Grades_Involved.value) + 1;
	} else {
		document.grantApp.Grades_Involved.value = parseInt(document.grantApp.Grades_Involved.value) - 1;		
	}
}

function lockCB(boxID) {
	if (boxID.checked) {
		boxID.checked = false;
	} else {
		boxID.checked = true;
	}
}

function checkMax() {
	if (isNaN(document.grantApp.Funds_Requested.value)) {
			alert("Funds requested must be a number.\nDo not include commas or dollar signs.");
			document.grantApp.Funds_Requested.value = "200.00";
	} else {
		if (document.grantApp.Funds_Requested.value > 1000) {
			alert("Maximum grant is $1000.00");
			document.grantApp.Funds_Requested.value = "1000.00";
		}
		if (document.grantApp.Funds_Requested.value < 200) {
			alert("Minimum grant is $200.00");
			document.grantApp.Funds_Requested.value = "200.00";
		}
	}
}

function calcShare() {
	if (isNaN(document.grantApp.Total_Cost.value)) {
			alert("Total cost must be a number.\nDo not include commas or dollar signs.");
			document.grantApp.Total_Cost.value = document.grantApp.Funds_Requested.value;
			document.grantApp.HATS_Percentage.value = "100";
	} else {
		if (document.grantApp.Total_Cost.value < 1) {
			alert("Invalid project cost");
			document.grantApp.Total_Cost.value = document.grantApp.Funds_Requested.value;
			document.grantApp.HATS_Percentage.value = "100";
		}
		if (parseFloat(document.grantApp.Total_Cost.value) < parseFloat(document.grantApp.Funds_Requested.value)) {
			alert("Funds requested cannot be greater than the total cost");
			document.grantApp.Funds_Requested.value = document.grantApp.Total_Cost.value
			document.grantApp.HATS_Percentage.value = "100";
		}
		if (document.grantApp.Funds_Requested.value < 1) {
			alert("Invalid funding request");
			document.grantApp.Funds_Requested.value = document.grantApp.Total_Cost.value;
		}
		if (document.grantApp.Total_Cost.value > 0) {
			document.grantApp.HATS_Percentage.value = parseInt(10000 * parseFloat(document.grantApp.Funds_Requested.value) / parseFloat(document.grantApp.Total_Cost.value)) / 100;
		}
	}
}

function checkFreeLunch() {
	if (isNaN(document.grantApp.Free_Lunch.value)) {
			alert("Free lunch percentage must be a number");
			document.grantApp.Free_Lunch.value = "0";
	} else {
		if (document.grantApp.Free_Lunch.value > 100) {
			alert("Maximum free lunch is 100%");
			document.grantApp.Free_Lunch.value = "100";
		}
		if (document.grantApp.Free_Lunch.value < 0) {
			alert("Invalid free lunch value");
			document.grantApp.Free_Lunch.value = "0";
		}
	}
}

function validateForm() {
	if (document.grantApp.userType.value.length > 0) {
		alert("User type error.  Please contact the webmaster for assistance.");
	} else if (document.grantApp.Project_Title.value.length < 1) {
		alert("Section 1: Project title cannot be blank");
	} else if (document.grantApp.School_Address.value.length < 1) {
		alert("Section 2: School Address cannot be blank");
	} else if (document.grantApp.School_City.value.length < 1) {
		alert("Section 2: School City cannot be blank");
	} else if (document.grantApp.School_Zip.value.length < 1) {
		alert("Section 2: School Zip cannot be blank");
	} else if (document.grantApp.School_County.value.length < 1) {
		alert("Section 2: School County cannot be blank");
	} else if (document.grantApp.School_Phone.value.length < 1) {
		alert("Section 2: School Phone cannot be blank");
	} else if (document.grantApp.School_Fax.value.length < 1) {
		alert("Section 2: School Fax cannot be blank.\nEnter 'None' if the school has no fax.");
	} else if (document.grantApp.Principal_Name.value.length < 1) {
		alert("Section 3: Principal's Name cannot be blank");
	} else if (document.grantApp.Principal_Email.value.toLowerCase() == 'none') {
		alert("Section 3: Principal's Email is required");
	} else if (document.grantApp.Principal_Email.value.length < 1) {
		alert("Section 3: Principal's Email cannot be blank");
	} else if (isValidEmail(document.grantApp.Principal_Email.value) == false) {
		alert("Section 3: Principal's Email is not a valid email address.");
	} else if (document.grantApp.Lead_Name.value.length < 1) {
		alert("Section 4: Lead Name cannot be blank");
	} else if (document.grantApp.Lead_Function.value.length < 1) {
		alert("Section 4: Lead Function cannot be blank");
	} else if (document.grantApp.Lead_Email1.value.length < 1) {
		alert("Section 4: Lead School Email cannot be blank.\nEnter 'None' if no school email.");
	} else if ((document.grantApp.Lead_Email1.value.toLowerCase() != 'none') && (isValidEmail(document.grantApp.Lead_Email1.value) == false)) {
		alert("Section 4: School Lead Email is not a valid email address.");
	} else if (document.grantApp.Lead_Email2.value.length < 1) {
		alert("Section 4: Lead Home Email cannot be blank.\nEnter 'None' if no school email.");
	} else if ((document.grantApp.Lead_Email2.value.toLowerCase() != 'none') && (isValidEmail(document.grantApp.Lead_Email2.value) == false)) {
		alert("Section 4: Home Lead Email is not a valid email address.");
	} else if ((document.grantApp.Lead_Email1.value.toLowerCase() == 'none') && (document.grantApp.Lead_Email2.value.toLowerCase() == 'none')) {
		alert("Section 4: Either a home email or a school email\nis required for the Lead Teacher.");
	} else if (document.grantApp.Lead_Address.value.length < 1) {
		alert("Section 4: Lead Address cannot be blank");
	} else if (document.grantApp.Lead_City.value.length < 1) {
		alert("Section 4: Lead City cannot be blank");
	} else if (document.grantApp.Lead_Zip.value.length < 1) {
		alert("Section 4: Lead Zip cannot be blank");
	} else if (document.grantApp.Lead_County.value.length < 1) {
		alert("Section 4: Lead County cannot be blank");
	} else if (document.grantApp.Lead_Phone1.value.length < 1) {
		alert("Section 4: Lead School Phone cannot be blank.\nEnter 'None' if no school phone.");
	} else if (document.grantApp.Lead_Fax1.value.length < 1) {
		alert("Section 4: Lead School Fax cannot be blank.\nEnter 'None' if no school fax.");
	} else if (document.grantApp.Lead_Phone2.value.length < 1) {
		alert("Section 4: Lead Home Phone cannot be blank.\nEnter 'None' if no home phone.");
	} else if (document.grantApp.Lead_Fax2.value.length < 1) {
		alert("Section 4: Lead Home Fax cannot be blank.\nEnter 'None' if no home fax.");
	} else if (document.grantApp.Sponsors_Count.value.length < 1) {
		alert("Section 5: Number of other actual or potential\nsponsors cannot be blank");
	} else if (document.grantApp.Grades_Involved.value.length < 1) {
		alert("Section 5: Grades Involved cannot be blank");
	} else if (document.grantApp.Student_Count.value.length < 1) {
		alert("Section 5: Number of students involved cannot be blank");
	} else if (document.grantApp.Project_Abstract.value.length < 1) {
		alert("Section 6: Project Abstract cannot be blank");
	} else if (document.grantApp.Project_Activities.value.length < 1) {
		alert("Section 7: Project Activities cannot be blank");
	} else if (document.grantApp.Project_Management.value.length < 1) {
		alert("Section 8: Project Management cannot be blank");
	} else if (document.grantApp.Time_Schedule.value.length < 1) {
		alert("Section 9: Time Schedule cannot be blank");
	} else if (document.grantApp.Project_Budget.value.length < 1) {
		alert("Section 10: Project Budget cannot be blank");
	} else if (document.grantApp.Eval_Criteria.value.length < 1) {
		alert("Section 11: Evaluation Criteria cannot be blank");
	} else if (document.grantApp.Project_Abstract.value.length > 600) {
		alert("Section 6: Project Abstract has too may characters.");
	} else if (document.grantApp.Project_Activities.value.length > 1600) {
		alert("Section 7: Project Activities has too may characters.");
	} else if (document.grantApp.Project_Management.value.length > 800) {
		alert("Section 8: Project Management has too may characters.");
	} else if (document.grantApp.Time_Schedule.value.length > 1200) {
		alert("Section 9: Time Schedule has too may characters.");
	} else if (document.grantApp.Project_Budget.value.length > 1200) {
		alert("Section 10: Project Budget has too may characters.");
	} else if (document.grantApp.Eval_Criteria.value.length > 1400) {
		alert("Section 11: Evaluation Criteria has too may characters.");
	} else {
		clearCookies();
		encodeText();
		return true
	}
	return false
}

function initSubmit() {
	calcShare();
	if (validateForm()) {
		window.onbeforeunload = '';
		window.onUnload = '';
		document.grantApp.submit();
	}
}

function reviseSubmit() {
	calcShare();
	if (validateForm()) {
		var reviseConfirm= confirm("If you revise your application, your principal will have to\napprove the revision before 4:30PM on 1 April 2011.\nClick OK to submit your revised application.");
		if (reviseConfirm == true) {
			document.grantApp.Revision.value = parseInt(document.grantApp.Revision.value) + 1;
			window.onbeforeunload = '';
			window.onUnload = '';
			document.grantApp.submit();
		} else {
			alert("Submit has been cancelled.");
		}
	}
}

function rejectSubmit() {
	document.grantApp.action = "http://rhackett.net/cgi-bin/cgiemail/HATS/GrantAppReject.txt";
	document.grantApp.success.value ='http://www.rhackett.net/HATS/RejectSuccess.htm';
	document.grantApp.submit();
}

function checkPhone(str) {
	if (str.toLowerCase() == 'none') return;
	
	var stripped = str.replace(/[\(\)\.\-\ ]/g, '');
	//strip out acceptable non-numeric characters
	if (isNaN(stripped)) {
		alert("The phone number contains illegal characters.");
		return false;
	}
	if (!(stripped.length == 10)) {
		alert("The phone number is the wrong length.\nMake sure you included an area code.");
		return false;
	}
	return true;
}

function checkZip(str) {
	if (!(str.length == 5)) {
		alert("The zipcode is the wrong length.\nUse only 5 digits");
		return false;
	}
	if (isNaN(str)) {
		alert("The zipcode contains illegal characters.");
		return false;
	}
	return true;
}

function isValidEmail(str) {
	// DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
	// This function is used to verify if the given value is a possible valid email address. This function makes 
	// sure the email address has one (@) and at least one (.). It also makes sure that there are no spaces, 
	// extra '@'s or a (.) just before or after the @. It also makes sure that there is atleast one (.) after the @.

	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/

	if (str.match(illegalChars)) {
		return false;
	} 

	if (str.indexOf(at)==-1){
		 return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		 return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
			return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
			return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
			return false
	 }
	
	 if (str.indexOf(" ")!=-1){
			return false
	 }

	 return true					
}

function checkEmail(field) {
	if (field.value.toLowerCase() != 'none') {
		if (isValidEmail(field.value) == false) {
			alert("Email address is not valid.  Enter 'none'\nif you do not have an email address.");
		}
	}
}

function saveForm(noDisplay) {

	alert('The save function has been disabled\nbecause of technical problems.');
	return;

	if (typeof noDisplay == 'undefined' ) noDisplay = false;
	
	if (!noDisplay) {
		if (navigator.cookieEnabled == false) {
			alert("Cookies are disabled!  You need to enable cookies to use the Save Draft button on this page.");
			return null;
		}
	}
	
	var CurrentDate = new Date();
	var ExpireDate = new Date();
	var one_day=1000*60*60*24;

	ExpireDate = ExpireDate.setFullYear(2011,3,3);
	var DaysLeft = parseInt((ExpireDate - CurrentDate) / one_day);
 
	//Textboxes
	writeCookie("App_ID", document.grantApp.App_ID.value, DaysLeft);
	writeCookie("Revision", document.grantApp.Revision.value, DaysLeft);
	writeCookie("Project_Title", document.grantApp.Project_Title.value, DaysLeft);
	writeCookie("School_Name", document.grantApp.School_Name.value, DaysLeft);
	writeCookie("School_Address", document.grantApp.School_Address.value, DaysLeft);
	writeCookie("School_City", document.grantApp.School_City.value, DaysLeft);
	writeCookie("School_Zip", document.grantApp.School_Zip.value, DaysLeft);
	writeCookie("School_County", document.grantApp.School_County.value, DaysLeft);
	writeCookie("School_Phone", document.grantApp.School_Phone.value, DaysLeft);
	writeCookie("School_Fax", document.grantApp.School_Fax.value, DaysLeft);
	writeCookie("Free_Lunch", document.grantApp.Free_Lunch.value, DaysLeft);
	writeCookie("Principal_Name", document.grantApp.Principal_Name.value, DaysLeft);
	writeCookie("Principal_Email", document.grantApp.Principal_Email.value, DaysLeft);
	writeCookie("Lead_Name", document.grantApp.Lead_Name.value, DaysLeft);
	writeCookie("Lead_Function", document.grantApp.Lead_Function.value, DaysLeft);
	writeCookie("Lead_Email1", document.grantApp.Lead_Email1.value, DaysLeft);
	writeCookie("Lead_Email2", document.grantApp.Lead_Email2.value, DaysLeft);
	writeCookie("Lead_Address", document.grantApp.Lead_Address.value, DaysLeft);
	writeCookie("Lead_City", document.grantApp.Lead_City.value, DaysLeft);
	writeCookie("Lead_Zip", document.grantApp.Lead_Zip.value, DaysLeft);
	writeCookie("Lead_County", document.grantApp.Lead_County.value, DaysLeft);
	writeCookie("Lead_Phone1", document.grantApp.Lead_Phone1.value, DaysLeft);
	writeCookie("Lead_Fax1", document.grantApp.Lead_Fax1.value, DaysLeft);
	writeCookie("Lead_Phone2", document.grantApp.Lead_Phone2.value, DaysLeft);
	writeCookie("Lead_Fax2", document.grantApp.Lead_Fax2.value, DaysLeft);
	writeCookie("Prior_Grants", document.grantApp.Prior_Grants.value, DaysLeft);
	writeCookie("Funds_Requested", document.grantApp.Funds_Requested.value, DaysLeft);
	writeCookie("Sponsors_Count", document.grantApp.Sponsors_Count.value, DaysLeft);
	writeCookie("Total_Cost", document.grantApp.Total_Cost.value, DaysLeft);
	writeCookie("HATS_Percentage", document.grantApp.HATS_Percentage.value, DaysLeft);
	writeCookie("Grades_Involved", document.grantApp.Grades_Involved.value, DaysLeft);
	writeCookie("Student_Count", document.grantApp.Student_Count.value, DaysLeft);

	//Checkboxes
	var Temp = document.grantApp.Prior_1988.checked.toString() + ":";
	Temp += document.grantApp.Prior_1989.checked.toString() + ":";
	Temp += document.grantApp.Prior_1990.checked.toString() + ":";
	Temp += document.grantApp.Prior_1991.checked.toString() + ":";
	Temp += document.grantApp.Prior_1992.checked.toString() + ":";
	Temp += document.grantApp.Prior_1993.checked.toString() + ":";
	Temp += document.grantApp.Prior_1994.checked.toString() + ":";
	Temp += document.grantApp.Prior_1995.checked.toString() + ":";
	Temp += document.grantApp.Prior_1996.checked.toString() + ":";
	Temp += document.grantApp.Prior_1997.checked.toString() + ":";
	Temp += document.grantApp.Prior_1998.checked.toString() + ":";
	Temp += document.grantApp.Prior_1999.checked.toString() + ":";
	Temp += document.grantApp.Prior_2000.checked.toString() + ":";
	Temp += document.grantApp.Prior_2001.checked.toString() + ":";
	Temp += document.grantApp.Prior_2002.checked.toString() + ":";
	Temp += document.grantApp.Prior_2003.checked.toString() + ":";
	Temp += document.grantApp.Prior_2004.checked.toString() + ":";
	Temp += document.grantApp.Prior_2005.checked.toString() + ":";
	Temp += document.grantApp.Prior_2006.checked.toString() + ":";
	Temp += document.grantApp.Prior_2007.checked.toString() + ":";
	Temp += document.grantApp.Prior_2008.checked.toString() + ":";
	Temp += document.grantApp.Prior_2009.checked.toString() + ":";
	Temp += document.grantApp.Prior_2010.checked.toString() + ":";
	Temp += document.grantApp.Grade_0.checked.toString() + ":";
	Temp += document.grantApp.Grade_1.checked.toString() + ":";
	Temp += document.grantApp.Grade_2.checked.toString() + ":";
	Temp += document.grantApp.Grade_3.checked.toString() + ":";
	Temp += document.grantApp.Grade_4.checked.toString() + ":";
	Temp += document.grantApp.Grade_5.checked.toString() + ":";
	Temp += document.grantApp.Grade_6.checked.toString() + ":";
	Temp += document.grantApp.Grade_7.checked.toString() + ":";
	Temp += document.grantApp.Grade_8.checked.toString() + ":";
	Temp += document.grantApp.Grade_9.checked.toString() + ":";
	Temp += document.grantApp.Grade_10.checked.toString() + ":";
	Temp += document.grantApp.Grade_11.checked.toString() + ":";
	Temp += document.grantApp.Grade_12.checked.toString() + ":";
	writeCookie("Checkboxes", Temp, DaysLeft);
	
	//Comboboxes
	writeCookie("School_State", findOption(document.grantApp.School_State), DaysLeft);
	writeCookie("Lead_State", findOption(document.grantApp.Lead_State), DaysLeft);
	writeCookie("Principal_Title", findOption(document.grantApp.Principal_Title), DaysLeft);
	writeCookie("Lead_Title", findOption(document.grantApp.Lead_Title), DaysLeft);
	writeCookie("Project_Duration", findOption(document.grantApp.Project_Duration), DaysLeft);
	writeCookie("Federal_Funding", findOption(document.grantApp.Federal_Funding), DaysLeft);
	writeCookie("School_Type", findOption(document.grantApp.School_Type), DaysLeft);
	
	//Text Areas
	writeCookie("Project_Abstract", document.grantApp.Project_Abstract.value.replace(/\n/g,"\\n").replace(/\r/g,"\\r"), DaysLeft);
	writeCookie("Project_Activities", document.grantApp.Project_Activities.value.replace(/\n/g,"\\n").replace(/\r/g,"\\r"), DaysLeft);
	writeCookie("Project_Management", document.grantApp.Project_Management.value.replace(/\n/g,"\\n").replace(/\r/g,"\\r"), DaysLeft);
	writeCookie("Time_Schedule", document.grantApp.Time_Schedule.value.replace(/\n/g,"\\n").replace(/\r/g,"\\r"), DaysLeft);
	writeCookie("Project_Budget", document.grantApp.Project_Budget.value.replace(/\n/g,"\\n").replace(/\r/g,"\\r"), DaysLeft);
	writeCookie("Eval_Criteria", document.grantApp.Eval_Criteria.value.replace(/\n/g,"\\n").replace(/\r/g,"\\r"), DaysLeft);
	writeCookie("Form_Data_Saved", "True", DaysLeft);

	if (!noDisplay) {
		var reviseConfirm= confirm("Your application data has been saved to this computer.  Your application data will be restored the next time you visit this website from this computer.  Your application data will not be available on other computers, and it has not been submitted to HATS STEDTRAIN.\n\nYour application must be submited and approved by your principal before 4:30PM on 1 April 2011.\n\nClick OK to bookmark this page.");
		if (reviseConfirm == true) {
			addToFavorites();
		}
	}
}

function restoreForm() {
	if (readCookie("Form_Data_Saved") == "True") {
		document.grantApp.App_ID.value = readCookie("App_ID");
		document.grantApp.Revision.value = readCookie("Revision")
		document.grantApp.Project_Title.value = readCookie("Project_Title");
		document.grantApp.School_Name.value = readCookie("School_Name");
		document.grantApp.School_Address.value = readCookie("School_Address");
		document.grantApp.School_City.value = readCookie("School_City");
		document.grantApp.School_Zip.value = readCookie("School_Zip");
		document.grantApp.School_County.value = readCookie("School_County");
		document.grantApp.School_Phone.value = readCookie("School_Phone");
		document.grantApp.School_Fax.value = readCookie("School_Fax");
		document.grantApp.Free_Lunch.value = readCookie("Free_Lunch");
		document.grantApp.Principal_Name.value = readCookie("Principal_Name");
		document.grantApp.Principal_Email.value = readCookie("Principal_Email");
		document.grantApp.Lead_Name.value = readCookie("Lead_Name");
		document.grantApp.Lead_Function.value = readCookie("Lead_Function");
		document.grantApp.Lead_Email1.value = readCookie("Lead_Email1");
		document.grantApp.Lead_Email2.value = readCookie("Lead_Email2");
		document.grantApp.Lead_Address.value = readCookie("Lead_Address");
		document.grantApp.Lead_City.value = readCookie("Lead_City");
		document.grantApp.Lead_Zip.value = readCookie("Lead_Zip");
		document.grantApp.Lead_County.value = readCookie("Lead_County");
		document.grantApp.Lead_Phone1.value = readCookie("Lead_Phone1");
		document.grantApp.Lead_Fax1.value = readCookie("Lead_Fax1");
		document.grantApp.Lead_Phone2.value = readCookie("Lead_Phone2");
		document.grantApp.Lead_Fax2.value = readCookie("Lead_Fax2");
		document.grantApp.Prior_Grants.value = readCookie("Prior_Grants");
		document.grantApp.Funds_Requested.value = readCookie("Funds_Requested");
		document.grantApp.Sponsors_Count.value = readCookie("Sponsors_Count");
		document.grantApp.Total_Cost.value = readCookie("Total_Cost");
		document.grantApp.HATS_Percentage.value = readCookie("HATS_Percentage");
		document.grantApp.Grades_Involved.value = readCookie("Grades_Involved");
		document.grantApp.Student_Count.value = readCookie("Student_Count");

		//Checkboxes
		var cb = new Array();
		var Temp = readCookie("Checkboxes");
		cb = Temp.split(":")
		document.grantApp.Prior_1988.checked = StringToBool(cb[0]);
		document.grantApp.Prior_1989.checked = StringToBool(cb[1]);
		document.grantApp.Prior_1990.checked = StringToBool(cb[2]);
		document.grantApp.Prior_1991.checked = StringToBool(cb[3]);
		document.grantApp.Prior_1992.checked = StringToBool(cb[4]);
		document.grantApp.Prior_1993.checked = StringToBool(cb[5]);
		document.grantApp.Prior_1994.checked = StringToBool(cb[6]);
		document.grantApp.Prior_1995.checked = StringToBool(cb[7]);
		document.grantApp.Prior_1996.checked = StringToBool(cb[8]);
		document.grantApp.Prior_1997.checked = StringToBool(cb[8]);
		document.grantApp.Prior_1998.checked = StringToBool(cb[10]);
		document.grantApp.Prior_1999.checked = StringToBool(cb[11]);
		document.grantApp.Prior_2000.checked = StringToBool(cb[12]);
		document.grantApp.Prior_2001.checked = StringToBool(cb[13]);
		document.grantApp.Prior_2002.checked = StringToBool(cb[14]);
		document.grantApp.Prior_2003.checked = StringToBool(cb[15]);
		document.grantApp.Prior_2004.checked = StringToBool(cb[16]);
		document.grantApp.Prior_2005.checked = StringToBool(cb[17]);
		document.grantApp.Prior_2006.checked = StringToBool(cb[18]);
		document.grantApp.Prior_2007.checked = StringToBool(cb[19]);
		document.grantApp.Prior_2008.checked = StringToBool(cb[20]);
		document.grantApp.Prior_2009.checked = StringToBool(cb[21]);
		document.grantApp.Prior_2010.checked = StringToBool(cb[22]);
		document.grantApp.Grade_0.checked = StringToBool(cb[23]);
		document.grantApp.Grade_1.checked = StringToBool(cb[24]);
		document.grantApp.Grade_2.checked = StringToBool(cb[25]);
		document.grantApp.Grade_3.checked = StringToBool(cb[26]);
		document.grantApp.Grade_4.checked = StringToBool(cb[27]);
		document.grantApp.Grade_5.checked = StringToBool(cb[28]);
		document.grantApp.Grade_6.checked = StringToBool(cb[29]);
		document.grantApp.Grade_7.checked = StringToBool(cb[30]);
		document.grantApp.Grade_8.checked = StringToBool(cb[31]);
		document.grantApp.Grade_9.checked = StringToBool(cb[32]);
		document.grantApp.Grade_10.checked = StringToBool(cb[33]);
		document.grantApp.Grade_11.checked = StringToBool(cb[34]);
		document.grantApp.Grade_12.checked = StringToBool(cb[35]);

		//Comboboxes
		document.grantApp.School_State.options[parseInt(readCookie("School_State"))].selected=true;
		document.grantApp.Lead_State.options[parseInt(readCookie("Lead_State"))].selected=true;
		document.grantApp.Principal_Title.options[parseInt(readCookie("Principal_Title"))].selected=true;
		document.grantApp.Lead_Title.options[parseInt(readCookie("Lead_Title"))].selected=true;
		document.grantApp.Project_Duration.options[parseInt(readCookie("Project_Duration"))].selected=true;
		document.grantApp.Federal_Funding.options[parseInt(readCookie("Federal_Funding"))].selected=true;
		document.grantApp.School_Type.options[parseInt(readCookie("School_Type"))].selected=true;
		
		//Text Areas
		document.grantApp.Project_Abstract.value = readCookie("Project_Abstract").replace(/\\n/g,"\n").replace(/\\r/g,"\r");
		document.grantApp.Project_Activities.value = readCookie("Project_Activities").replace(/\\n/g,"\n").replace(/\\r/g,"\r");
		document.grantApp.Project_Management.value = readCookie("Project_Management").replace(/\\n/g,"\n").replace(/\\r/g,"\r");
		document.grantApp.Time_Schedule.value = readCookie("Time_Schedule").replace(/\\n/g,"\n").replace(/\\r/g,"\r");
		document.grantApp.Project_Budget.value = readCookie("Project_Budget").replace(/\\n/g,"\n").replace(/\\r/g,"\r");
		document.grantApp.Eval_Criteria.value = readCookie("Eval_Criteria").replace(/\\n/g,"\n").replace(/\\r/g,"\r");
		
		updateLimits();
	}
}

function clearCookies() {
	if (readCookie("Form_Data_Saved") == "True") {
		writeCookie("App_ID", "", 0);
		writeCookie("Revision", "", 0);
		writeCookie("Date_Submitted", "", 0);
		writeCookie("Date_Approved", "", 0);
		writeCookie("Project_Title", "", 0);
		writeCookie("School_Name", "", 0);
		writeCookie("School_Address", "", 0);
		writeCookie("School_City", "", 0);
		writeCookie("School_State", "", 0);
		writeCookie("School_Zip", "", 0);
		writeCookie("School_County", "", 0);
		writeCookie("School_Phone", "", 0);
		writeCookie("School_Fax", "", 0);
		writeCookie("Free_Lunch", "", 0);
		writeCookie("School_Type", "", 0);
		writeCookie("Principal_Title", "", 0);
		writeCookie("Principal_Name", "", 0);
		writeCookie("Principal_Email", "", 0);
		writeCookie("Lead_Title", "", 0);
		writeCookie("Lead_Name", "", 0);
		writeCookie("Lead_Function", "", 0);
		writeCookie("Lead_Email1", "", 0);
		writeCookie("Lead_Email2", "", 0);
		writeCookie("Lead_Address", "", 0);
		writeCookie("Lead_City", "", 0);
		writeCookie("Lead_State", "", 0);
		writeCookie("Lead_Zip", "", 0);
		writeCookie("Lead_County", "", 0);
		writeCookie("Lead_Phone1", "", 0);
		writeCookie("Lead_Fax1", "", 0);
		writeCookie("Lead_Phone2", "", 0);
		writeCookie("Lead_Fax2", "", 0);
		writeCookie("Prior_Grants", "", 0);
		writeCookie("Checkboxes", "", 0);
		writeCookie("Funds_Requested", "", 0);
		writeCookie("Sponsors_Count", "", 0);
		writeCookie("Total_Cost", "", 0);
		writeCookie("HATS_Percentage", "", 0);
		writeCookie("Grades_Involved", "", 0);
		writeCookie("Student_Count", "", 0);
		writeCookie("Project_Duration", "", 0);
		writeCookie("Federal_Funding", "", 0);
		writeCookie("Project_Abstract", "", 0);
		writeCookie("Project_Activities", "", 0);
		writeCookie("Project_Management", "", 0);
		writeCookie("Time_Schedule", "", 0);
		writeCookie("Project_Budget", "", 0);
		writeCookie("Eval_Criteria", "", 0);
		writeCookie("Form_Data_Saved", "", 0);
	}
}

function clearForm() {
	var reviseConfirm= confirm("This will clear the form and delete all of your data.\n\nClick OK to clear this form.");
	if (reviseConfirm == true) {
		clearCookies();
		window.location.reload()
	}
}

function findOption(oSelect) {
	var i=0;
	while (i <= parseInt(oSelect.options.length)-1) {
	if (oSelect.options[i].selected) {
			return i;
		}
		i++;
	}
	return 0;
}

function StringToBool(str) {
	if (str == "true") {
		return true;
	} else {
		return false;
	}
}

function encodeText() {
	//Make sure they haven't used any prohibited characters that will cause problems for XML

	document.grantApp.Project_Title.value = document.grantApp.Project_Title.value.replace(/&/g,"&amp;");
	document.grantApp.School_Name.value = document.grantApp.School_Name.value.replace(/&/g,"&amp;");
	document.grantApp.Lead_Function.value = document.grantApp.Lead_Function.value.replace(/&/g,"&amp;");
	
	document.grantApp.Project_Abstract.value = document.grantApp.Project_Abstract.value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
	document.grantApp.Project_Activities.value = document.grantApp.Project_Activities.value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
	document.grantApp.Project_Management.value = document.grantApp.Project_Management.value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
	document.grantApp.Time_Schedule.value = document.grantApp.Time_Schedule.value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
	document.grantApp.Project_Budget.value = document.grantApp.Project_Budget.value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
	document.grantApp.Eval_Criteria.value = document.grantApp.Eval_Criteria.value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
}

function reportSubmit() {
	if (document.MidTermReport.Grant_Status.selectedIndex == 0) {
		alert("Section 2A: Question must be answered.");
	} else if (document.MidTermReport.Need_Assist.selectedIndex == 0) {
		alert("Section 2K: Question must be answered.");
	} else if (document.MidTermReport.POC_Visit.selectedIndex == 0) {
		alert("Section 3A: First question must be answered.");
	} else if (document.MidTermReport.POC_Contact.selectedIndex == 0) {
		alert("Section 3A: Second question must be answered.");
	} else if (document.MidTermReport.New_Interest.selectedIndex == 0) {
		alert("Section 3B: Question must be answered.");
	} else if (document.MidTermReport.Media_Interest.selectedIndex == 0) {
		alert("Section 3C: First question must be answered.");
	} else if (document.MidTermReport.STEDTRAIN_Recognized.selectedIndex == 0) {
		alert("Section 3C: Second question must be answered.");
	} else if (document.MidTermReport.Status_Reason.value.length > 700) {
		alert("Section 2A: Explanation contains too much text.");
	} else if (document.MidTermReport.Prelim_Results.value.length > 700) {
		alert("Section 2B: Preliminary results contains too much text.");
	} else if (document.MidTermReport.Future_Activities.value.length > 700) {
		alert("Section 2E: Anticipated future activites contains too much text.");
	} else if (document.MidTermReport.Goal_Status.value.length > 700) {
		alert("Section 2F: Acomplished goals contains too much text.");
	} else if (document.MidTermReport.Grant_Impact.value.length > 700) {
		alert("Section 2G: Children's Impact contains too much text.");
	} else if (document.MidTermReport.Prof_Develop.value.length > 700) {
		alert("Section 2H: Professional Development contains too much text.");
	} else if (document.MidTermReport.Grant_Problems.value.length > 700) {
		alert("Section 2I: Problems contains too much text.");
	} else if (document.MidTermReport.Prob_Solutions.value.length > 700) {
		alert("Section 2J: Solutions contains too much text.");
	} else if (document.MidTermReport.Assist_Request.value.length > 700) {
		alert("Section 2K: Assistance Needed contains too much text.");
	} else if (document.MidTermReport.Suggestions.value.length > 700) {
		alert("Section 2L: Suggestions contains too much text.");
	} else if (document.MidTermReport.Other_Results.value.length > 700) {
		alert("Section 2M: Other Results contains too much text.");
	} else if (document.MidTermReport.Interest_Description.value.length > 700) {
		alert("Section 3B: Explanation contains too much text.");
	} else if (document.MidTermReport.Media_Description.value.length > 700) {
		alert("Section 3C: References contains too much text.");
	} else if (document.MidTermReport.Number_Type.value.length > 175) {
		alert("Section 2D: Number and type of student contains too much text.");
	} else if (document.MidTermReport.Grant_Status.selectedIndex == 2 && document.MidTermReport.Status_Reason.value.length < 1) {
		alert("Section 2A: Explanation is required.");
	} else if (document.MidTermReport.Need_Assist.selectedIndex == 1 && document.MidTermReport.Assist_Request.value.length < 1) {
		alert("Section 2K: You must tell us what assistance is required.");
	} else if (document.MidTermReport.New_Interest.selectedIndex == 1 && document.MidTermReport.Interest_Description.value.length < 1) {
		alert("Section 3B: Explanation is required.");
	} else if (document.MidTermReport.Media_Interest.selectedIndex == 1 && document.MidTermReport.Media_Description.value.length < 1) {
		alert("Section 3C: References are required.");
	} else if (document.MidTermReport.Prelim_Results.value.length < 1) {
		alert("Section 2B: Preliminary results are required.");
	} else if (document.MidTermReport.Future_Activities.value.length < 1) {
		alert("Section 2E: Anticipated future activites is required.");
	} else if (document.MidTermReport.Goal_Status.value.length < 1) {
		alert("Section 2F: Acomplished goals is required.");
	} else if (document.MidTermReport.Grant_Impact.value.length < 1) {
		alert("Section 2G: Children's Impact is required.");
	} else if (document.MidTermReport.Prof_Develop.value.length < 1) {
		alert("Section 2H: Professional Development is required.");
	} else if (document.MidTermReport.Grant_Problems.value.length < 1) {
		alert("Section 2I: Problems is a required field.\nEnter none if there were no problems.");
	} else if (document.MidTermReport.Grant_Problems.value.toLowerCase() != 'none' && document.MidTermReport.Prob_Solutions.value.length < 1) {
		alert("Section 2J: Solutions is a required field if\nthere were problems noted in Section 2I.");
	} else if (document.MidTermReport.Suggestions.value.length < 1) {
		alert("Section 2L: Suggestions are required.");
	} else if (document.MidTermReport.Other_Results.value.length < 1) {
		alert("Section 2M: Other Results is required.");
	} else if (isNaN(document.MidTermReport.Students_Involved.value)) {		
		alert('Section 2C: This must be a number.');
	} else if (document.MidTermReport.Students_Involved.value.length == 0) {		
		alert('Section 2C: This question must be answered.');
	} else if (document.MidTermReport.Students_Involved.value < 1) {		
		alert('Section 2C: This must be greater than zero.');
	} else if (document.MidTermReport.Number_Type.value.length < 1) {		
		alert('Section 2D: This question must be answered.');
	} else {
		document.MidTermReport.Status_Reason.value = document.MidTermReport.Status_Reason.value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
		document.MidTermReport.Prelim_Results.value = document.MidTermReport.Prelim_Results.value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
		document.MidTermReport.Future_Activities.value = document.MidTermReport.Future_Activities.value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
		document.MidTermReport.Goal_Status.value = document.MidTermReport.Goal_Status.value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
		document.MidTermReport.Grant_Impact.value = document.MidTermReport.Grant_Impact.value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
		document.MidTermReport.Prof_Develop.value = document.MidTermReport.Prof_Develop.value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
		document.MidTermReport.Grant_Problems.value = document.MidTermReport.Grant_Problems.value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
		document.MidTermReport.Prob_Solutions.value = document.MidTermReport.Prob_Solutions.value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
		document.MidTermReport.Assist_Request.value = document.MidTermReport.Assist_Request.value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
		document.MidTermReport.Suggestions.value = document.MidTermReport.Suggestions.value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
		document.MidTermReport.Other_Results.value = document.MidTermReport.Other_Results.value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
		document.MidTermReport.Interest_Description.value = document.MidTermReport.Interest_Description.value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
		document.MidTermReport.Media_Description.value = document.MidTermReport.Media_Description.value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
		document.MidTermReport.Number_Type.value = document.MidTermReport.Number_Type.value.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
		document.MidTermReport.Report_Date.value = Date();
		window.onbeforeunload = '';
		document.MidTermReport.submit();
	}
}

function XMLEncode(str) {
	str = str.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
	return str;
}

function submitFeedback() {
	if (document.feedback.userType.value.length > 0) {
		alert("User type error.  Please contact the webmaster for assistance.");
	} else {
		document.feedback.submit();
	}
}

function padlength(what){
	var output=(what.toString().length==1)? "0"+what : what
	return output
}

function displaytime(){
	serverdate.setSeconds(serverdate.getSeconds()+1);
	var datestring=montharray[serverdate.getMonth()]+" "+padlength(serverdate.getDate())+", "+serverdate.getFullYear();
	var timestring=padlength(serverdate.getHours()+1)+":"+padlength(serverdate.getMinutes())+":"+padlength(serverdate.getSeconds());
	document.getElementById("servertime").innerHTML=timestring+" Central Time";
	document.getElementById("serverDate").innerHTML=datestring;
	//document.getElementById("servertime").innerHTML=datestring+" "+timestring+" Central Time";
	displayTZCountDown();
}

function displayTZCountDown() {
	//var CurrDate = new Date();
	//Date format:  Year, Month (0-11), Day(1-31),Hour (MST), Minute, Seconds
	var EndDate = new Date(2012, 0, 15, 23, 00, 0, 0);
	var countdown = parseInt((EndDate - serverdate) / 1000);
	
	if (countdown < 0) document.getElementById("tzcd").innerHTML = "Your HATS STEDTRAIN Midterm Report is late; however, we still need your report. Please submit it."; 
	else {var secs = countdown % 60; 
	if (secs < 10) secs = '0'+secs;
	var countdown1 = (countdown - secs) / 60;
	var mins = countdown1 % 60; 
	if (mins < 10) mins = '0'+mins;
	countdown1 = (countdown1 - mins) / 60;
	var hours = countdown1 % 24;
	var days = (countdown1 - hours) / 24;
	document.getElementById("tzcd").innerHTML = days + " day" + (days == 1 ? '' : 's') + ' + ' +hours+ 'h : ' +mins+ 'm : '+secs+'s';
	setTimeout('displayTZCountDown('+(countdown-1)+',\''+tzcd+'\');',999);
	}
}

function startClock() {
	if (document.getElementById('serverClock') != null) {
		clockID=setInterval("displaytime()", 1000);
	}
}

function stopClock() {
	if (document.getElementById('serverClock') != null) {
		clearInterval(clockID);
	}
}


