// javascript document

// windows ie style sheet
var user = navigator.userAgent;
if(user.indexOf("MSIE") != -1) {
	document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"styles_ie.css\" />");	
}

// popup notes
function popUp(page) {
var features = "width=500,height=340,resizable,status,scrollbars,screenX=30,screenY=30,top=30,left=30";
	window.open(page,'popUp',features);
    return false;
}

function popUpWide(page) {
var features = "width=640,height=480,resizable,status,scrollbars,screenX=30,screenY=30,top=30,left=30";
	window.open(page,'popUp',features);
    return false;
}

// focus the first text input elemnt
function formFocus() {
	var numElements = document.forms[0].elements.length;
	for(i=0; i<numElements; i++) {
		if(document.forms[0].elements[i].type == "text") {
			document.forms[0].elements[i].focus();
			break;
		}
	}
}

// popup big page
function popUpBig(page) {
var features = "width=780,height=600,resizable,status,scrollbars,screenX=30,screenY=30,top=30,left=30";
	window.open(page,'popUp',features);
    return false;
}

// write content to page div
function changeContent(id, shtml) {
	if (document.getElementById || document.all) {
		var el = document.getElementById? document.getElementById(id): document.all[id];
		if (el && typeof el.innerHTML !== "undefined") {
			el.innerHTML = shtml;
		}
	}
}

// basis of writing new ajax requests
function ajaxRequest() {
	var request;
	try {
		// firefox, opera 8.0+, safari
		request = new XMLHttpRequest();
	}
	catch (e) {
		// internet explorer
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				alert("Sorry, an error has occurred.");
				return false;
			}
		}
	}
	return request;
}

var progressOn = "<img src=\"images/ajax-loader.gif\" alt=\"loading\" width=\"16\" height=\"16\" />";
var progressOff = "<img src=\"images/transpixel.gif\" alt=\" \" width=\"16\" height=\"16\" />";

// dynamically populate district list based on county choice
function reqDistrict(id) {
	var a = document.forms[0].district_code;
	var b = document.forms[0].school_code;
	if (id == 0) {
		a.options.length = 0;
		b.options.length = 0;
		a.options[0] = new Option("Select...","");
		b.options[0] = new Option("Select...","");
		return;
	} else {
		b.options.length = 0;
		b.options[0] = new Option("Select...","");

	}
	changeContent("districtprogress", progressOn);
	var r = Math.round(Math.random() * 10000000);
	var i = 0;
	var optionscounter = 1;
	var request = ajaxRequest();
	if (request) {
		request.onreadystatechange = function() {
			if (request.readyState === 4) {
				if (request.status === 200) {
					var dbresults = request.responseText;
					var resultsarray = dbresults.split("\r");
					a.options.length = 0;
					a.options[0] = new Option("Select...","");
					var resultsarraylength = resultsarray.length - 1;
					for (i = 0; i < resultsarraylength; i++) {
						var districtoptions = resultsarray[i].split("|");
						a.options[optionscounter] = new Option(districtoptions[0], districtoptions[1]);
						optionscounter++;
					}
					changeContent("districtprogress", progressOff);
				} else {
					alert("There was a problem retrieving the data:\n" + request.statusText + request.responseText);
				}
			}
		};
		var action_params = "";
		action_params += "?county_code=" + escape(id);
		action_params += "&r=" + escape(r);
		request.open("GET", "qry_district_script.lasso" + action_params, true);
		request.send(null);
	} else {
		alert("Sorry, an error has occurred. Your browser is unable to support dynamic requests.");
	}
}

// dynamically populate school list based on district choice
function reqSchool(id) {
	var a = document.forms[0].school_code;
	if (id == 0) {
		a.options.length = 0;
		a.options[0] = new Option("Select...","");
		return;
	}
	changeContent("schoolprogress", progressOn);
	var r = Math.round(Math.random() * 10000000);
	var i = 0;
	var optionscounter = 1;
	var request = ajaxRequest();
	if (request) {
		request.onreadystatechange = function() {
			if (request.readyState === 4) {
				if (request.status === 200) {
					var dbresults = request.responseText;
					if (dbresults == "none") {
						a.options.length = 0;
						a.options[0] = new Option("Select...","");
					} else {
						var resultsarray = dbresults.split("\r");
						a.options.length = 0;
						a.options[0] = new Option("Select...","");
						var resultsarraylength = resultsarray.length - 1;
						for (i = 0; i < resultsarraylength; i++) {
							var schooloptions = resultsarray[i].split("|");
							a.options[optionscounter] = new Option(schooloptions[0],schooloptions[1]);
							optionscounter++;
						}
						changeContent("schoolprogress", progressOff);
					}
				} else {
					alert("There was a problem retrieving the data:\n" + request.statusText + request.responseText);
				}
			}
		};
		var action_params = "";
		action_params += "?district_code=" + escape(id);
		action_params += "&r=" + escape(r);
		request.open("GET", "qry_school_script.lasso" + action_params, true);
		request.send(null);
	} else {
		alert("Sorry, an error has occurred. Your browser is unable to support dynamic requests.");
	}
}

// valid district school search
function validAccountSearch() {
	var a = document.account.county_code.selectedIndex;
	var b = document.account.district_code.selectedIndex;
	if (a != 0 && b === 0) {
		alert("If you select the county, you must also select the district.");
		return false;
	}
	return true;
}

// valid agency name search
function validAgencyNameSearch() {
	var a = document.agencysearch.district.value;
	var b = document.agencysearch.school.value;
	if (a === "" && b === "") {
		alert("Please enter a district, school or both.");
		return false;
	}
	return true;
}

// valid name search
function validNameSearch() {
	var a = document.account.name_last.value;
	var b = document.account.name_first.value;
	if (a === "" && b === "") {
		alert("Please enter a first name, last name, or both.");
		return false;
	}
	return true;
}
