﻿var arraySchoolList = document.getElementById('ctl00_contentBody_hfSchoolList').value.split('~');
var tbSrApp = document.getElementById('ctl00_contentBody_tbSrApp');
var tbFirstName = document.getElementById('ctl00_contentBody_tbFirstName');
var tcFirstNameValidater = document.getElementById('ctl00_contentBody_tcFirstNameValidater')
var tbLastName = document.getElementById('ctl00_contentBody_tbLastName');
var tcLastNameValidater = document.getElementById('ctl00_contentBody_tcLastNameValidater')
var ddAge = document.getElementById('ctl00_contentBody_ddAge');
var tcAgeValidator = document.getElementById('ctl00_contentBody_tcAgeValidator')
var ddSchoolFilter = document.getElementById('ctl00_contentBody_ddSchoolFilter');

var arraySchoolList = document.getElementById('ctl00_contentBody_hfSchoolList').value.split('~');
var ddSchool = document.getElementById('ctl00_contentBody_ddSchool');
var tbSchoolOther = document.getElementById('ctl00_contentBody_tbSchoolOther');
var ddLocation = document.getElementById('ctl00_contentBody_ddLocation');
var tcLocationValidator = document.getElementById('ctl00_contentBody_tcLocationValidator');

var optn = document.createElement('option');
optn.value = '-1';
optn.text = '-- Sacramento County Schools --';
ddSchool.options.add(optn);

for (var i = 0; i < arraySchoolList.length; i = i + 4)
{
	var optn = document.createElement('option');
	optn.value = arraySchoolList[i];
	optn.text = arraySchoolList[i + 1];
	ddSchool.options.add(optn);
}

function schoolListFilter() {
	ddSchool.length = 0;
	var optnHeader = document.createElement('option');
	optnHeader.value = '-1';
	optnHeader.text = '-- Sacramento County Schools --';
	ddSchool.options.add(optnHeader);
	for (var i = 0; i < arraySchoolList.length; i = i + 4) {
		minGrade = arraySchoolList[i + 2];
		maxGrade = arraySchoolList[i + 3];
		addOption = 0;
		switch (ddSchoolFilter.value) {
			case '1':
			if (minGrade < 6) {
				addOption = 1;
			}
			break;
			case '2':
				if (minGrade > 5 & maxGrade < 10) {
					addOption = 1;
				}
			break;
			case '3':
				if (minGrade > 7) {
					addOption = 1;
				}
			break;
			default:
			addOption = 1;
			break;
		}
			
		if (addOption == 1) {
			var optn = document.createElement('option');
			optn.value = arraySchoolList[i];
			optn.text = arraySchoolList[i + 1];
			ddSchool.options.add(optn);
		} 
	}// loop i} // schoolListFilter


function submitForm() {
	var valid = 1;
	if(tbFirstName.value ==''){
		valid = 0;
		tcFirstNameValidater.innerHTML = 'You must enter your first name';
	};
	if(tbLastName.value ==''){
		valid = 0;
		tcLastNameValidater.innerHTML = 'You must enter your last name';
	};
	if(ddAge.value =='-1'){
		valid = 0;
		tcAgeValidator.innerHTML = 'You must enter your age';
	};
	if(ddLocation.value =='-1'){
		valid = 0;
		tcLocationValidator.innerHTML = 'You must enter your library';
	};
	if(valid == 1){
		fromSubmit.summerReading(tbFirstName.value, tbLastName.value, ddAge.value, ddSchool.value, tbSchoolOther.value, ddLocation.value, saveCompleted, saveFailed);
	};
}; // submitForm

function saveCompleted() {
	

	if (parseInt(ddAge.value) < 5) {
		document.getElementById('ctl00_contentBody_imgSrSubmitReturn').src = "image/UI/summerReading/Sr2010SubmitReturnKids.gif";
		document.getElementById('ctl00_contentBody_hlPdfSrSubmitReturn').innerHTML = "Download Pre-Readers Bingo Card";
		document.getElementById('ctl00_contentBody_hlPdfSrSubmitReturn').href = "file/265.pdf";
	} else {
		if (parseInt(ddAge.value) < 13) {
			document.getElementById('ctl00_contentBody_imgSrSubmitReturn').src = "image/UI/summerReading/Sr2010SubmitReturnKids.gif";
			document.getElementById('ctl00_contentBody_hlPdfSrSubmitReturn').innerHTML = "Download Kids Bingo Card";
			document.getElementById('ctl00_contentBody_hlPdfSrSubmitReturn').href = "file/266.pdf";
		} else {
			if (parseInt(ddAge.value) < 18) {
				document.getElementById('ctl00_contentBody_imgSrSubmitReturn').src = "image/UI/summerReading/Sr2010SubmitReturnTeens.gif";
				document.getElementById('ctl00_contentBody_hlPdfSrSubmitReturn').innerHTML = "Download Teens Bingo Card";
				document.getElementById('ctl00_contentBody_hlPdfSrSubmitReturn').href = "file/267.pdf";
			} else {
				document.getElementById('ctl00_contentBody_imgSrSubmitReturn').src = "image/UI/summerReading/Sr2010SubmitReturnAdults.gif";
				document.getElementById('ctl00_contentBody_hlPdfSrSubmitReturn').innerHTML = "Download Adults Bingo Card";
				document.getElementById('ctl00_contentBody_hlPdfSrSubmitReturn').href = "file/268.pdf";
			};
		};		
	};
	
	//Clear the form and switch to the completed view
	tbFirstName.value = "";
	document.getElementById('ctl00_contentBody_trFirstName').style.display = 'none';
	tbLastName.value = "";
	document.getElementById('ctl00_contentBody_trLastName').style.display = 'none';
	ddAge.value = -1;
	document.getElementById('ctl00_contentBody_trAge').style.display = 'none';
	ddSchoolFilter.value = 0;
	document.getElementById('ctl00_contentBody_trGradeLevel').style.display = 'none';
	ddSchool.value = -1;
	document.getElementById('ctl00_contentBody_trSchool').style.display = 'none';
	tbSchoolOther.value = "";
	document.getElementById('ctl00_contentBody_trSchoolOther').style.display = 'none';
	ddLocation.value = -1;
	document.getElementById('ctl00_contentBody_trLocation').style.display = 'none';
	document.getElementById('ctl00_contentBody_trSubmit').style.display = 'none';
	document.getElementById('ctl00_contentBody_trSubmitReturn').style.display = 'block';


} // saveCompleted

function saveFailed() {
	alert('submit failed');
}; // saveFailed

function nameFirstChanged() {
	if(tbFirstName.value != ''){
		tcFirstNameValidater.innerHTML = '';
	}
}; // nameFirstChanged

function nameLastChanged() {
	if(tbLastName.value != ''){
		tcLastNameValidater.innerHTML = '';
	}
}; // nameLastChanged

function ddAgeChanged() {
	if(ddAge.value != '-1'){
		tcAgeValidator.innerHTML = '';
	}
}; // ddAgeChanged

function ddLocationChanged() {
	if(ddLocation.value != '-1'){
		tcLocationValidator.innerHTML = '';
	}
}; // ddLocationChanged

function enterAnother() {

	document.getElementById('ctl00_contentBody_trFirstName').style.display = 'block';
	document.getElementById('ctl00_contentBody_trLastName').style.display = 'block';
	document.getElementById('ctl00_contentBody_trAge').style.display = 'block';
	document.getElementById('ctl00_contentBody_trGradeLevel').style.display = 'block';
	document.getElementById('ctl00_contentBody_trSchool').style.display = 'block';
	document.getElementById('ctl00_contentBody_trSchoolOther').style.display = 'block';
	document.getElementById('ctl00_contentBody_trLocation').style.display = 'block';
	document.getElementById('ctl00_contentBody_trSubmit').style.display = 'block';
	document.getElementById('ctl00_contentBody_trSubmitReturn').style.display = 'none';

} //enterAnother


