// javascript document

// write ie style sheet
var client = navigator.userAgent;
if (client.indexOf("MSIE") != -1) {
	document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"css/styles_ie.css\" />");	
}

// dreamweaver installed rollovers
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

// write qry updates to layer
/*************************************************************************
  This code is from Dynamic Web Coding at http://www.dyn-web.com/
  See Terms of Use at http://www.dyn-web.com/bus/terms.html
  Permission granted to use this code 
  as long as this entire notice is included.
*************************************************************************/
// slightly modified by jb
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;
		}
	}
}

// focus the first text input
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;
		}
	}
}

// load images and focus the first text input
function loadFocus() {
	MM_preloadImages('images_new/login_f2.gif', 'images_new/search_f2.gif', 'images_new/qeiabkgnd_f2.gif', 'images_new/techassist_f2.gif', 'images/help_down.gif');
	formFocus();
}

// toggle page help column
function toggleHelp() {
	var hidelink = "";
	hidelink += "<p><img src=\"images/help_down.gif\" alt=\" \" width=\"9\" height=\"9\" /> ";
	hidelink += "<a href=\"javascript:toggleHelp();\">Hide help</a></p>";
	var helplink = "";
	helplink += "<p><img src=\"images/help_up.gif\" alt=\" \" width=\"9\" height=\"9\" />";
	helplink += "<a href=\"javascript:toggleHelp();\">Help for this page</a></p>";
	var a = document.getElementById("helplink");
	var b = document.getElementById("helpcontent");
	if (b.style.display == "none") {
		changeContent("helplink", hidelink);
		b.style.display = "block";
	} else {
		changeContent("helplink", helplink);
		b.style.display = "none";
	}
}

// ajax object
function ajaxRequest() {
	var request = null;
	if (window.XMLHttpRequest) {
		request = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		request = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return (request === null) ? false : request;
}

// globals for ajax progress display
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\" />";

// get list of districts based on county code
function reqDistrict(id) {
	var a = document.forms[0].district_code;
	var b = document.forms[0].school_code;
	var r = Math.round(Math.random() * 10000000);
	if (id == 0) {
		a.options.length = 0;
		b.options.length = 0;
		a.options[0] = new Option("Select...","");
		b.options[0] = new Option("Select...","");
		a.setAttribute("disabled","disabled");
		b.setAttribute("disabled","disabled");
		return;
	} else {
		b.options.length = 0;
		b.options[0] = new Option("Select...","");
		b.setAttribute("disabled","disabled");
	}
	changeContent("districtprogress", progressOn);
	var request = ajaxRequest();
	if (request) {
		request.onreadystatechange = function() {
			if (request.readyState === 4) {
				if (request.status === 200) {
					var queryresults = request.responseText;			
					var resultsarray = queryresults.split("\r");
					a.options.length = 0;
					a.options[0] = new Option("Select...","");
					var optionscounter = 1;
					var resultsarraylength = resultsarray.length - 1;
					for (var i = 0; i < resultsarraylength; i++) {
						var districtoptions = resultsarray[i].split("|");
						a.options[optionscounter] = new Option(districtoptions[0], districtoptions[1]);
						optionscounter++;
					}
					a.removeAttribute("disabled");
					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_qeiadistricts.lasso" + action_params, true);
		request.send(null);
	} else {
		alert("Sorry, an error has occurred. Your browser is unable to support dynamic requests.");
	}
}

// get list of schools based on district code
function reqSchool(id) {
	var a = document.forms[0].school_code;
	var r = Math.round(Math.random() * 10000000);
	if (id == 0) {
		a.options.length = 0;
		a.options[0] = new Option("Select...","");
		a.setAttribute("disabled","disabled");
		return;
	}
	changeContent("schoolprogress", progressOn);
	var request = ajaxRequest();
	if (request) {
		request.onreadystatechange = function() {
			if (request.readyState === 4) {
				if (request.status === 200) {
					var queryresults = request.responseText;			
					var resultsarray = queryresults.split("\r");
					a.options.length = 0;
					a.options[0] = new Option("Select...","");
					var optionscounter = 1;
					var resultsarraylength = resultsarray.length - 1;
					for (var i = 0; i < resultsarraylength; i++) {
						var schooloptions = resultsarray[i].split("|");
						a.options[optionscounter] = new Option(schooloptions[0], schooloptions[1]);
						optionscounter++;
					}
					a.removeAttribute("disabled");
					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_qeiaschools.lasso" + action_params, true);
		request.send(null);
	} else {
		alert("Sorry, an error has occurred. Your browser is unable to support dynamic requests.");
	}
}

// valid county, district, school selections
function qeiaStart() {
	var a = document.qeiastart.county_code.selectedIndex;
	var b = document.qeiastart.district_code.selectedIndex;
	var c = document.qeiastart.school_code.selectedIndex;
	if (a == 0 || b == 0 || c == 0) {
		alert("Please select county, district, and school.");
		return false;
	}
	return true;
}

// valid school profile update
function profileUpdate() {
	var tysm = document.profileupdate.training_year_start_month;
	var tysd = document.profileupdate.training_year_start_day;
	var tyem = document.profileupdate.training_year_end_month;
	var tyed = document.profileupdate.training_year_end_day;
	var grs = document.profileupdate.grade_id_range_start;
	var gre = document.profileupdate.grade_id_range_end;
	var a = tysm.selectedIndex;
	var b = tysm[a].value;
	var c = tysd.selectedIndex;
	var d = tysd[c].value;
	var e = tyem.selectedIndex;
	var f = tyem[e].value;
	var g = tyed.selectedIndex;
	var h = tyed[g].value;
	var j = grs.selectedIndex;
	var k = parseInt(grs[j].value);
	var l = gre.selectedIndex;
	var m = parseInt(gre[l].value);
	var dialog = "";
	// no empty fields
	if (a == 0) {
		dialog += "\n- Training year start month";
	}
	if (c == 0) {
		dialog += "\n- Training year start day";
	}
	if (e == 0) {
		dialog += "\n- Training year end month";
	}
	if (g == 0) {
		dialog += "\n- Training year end day";
	}
	if (j == 0) {
		dialog += "\n- School grade range start";
	}
	if (l == 0) {
		dialog += "\n- School grade range end";
	}
	// invalid month lengths (april, june, september, november)
	if ((b == 4 || b == 6 || b == 9 || b == 11) && (d == 31)) {
		dialog += "\n- Invalid start day for selected start month";
	}
	if ((f == 4 || f == 6 || f == 9 || f == 11) && (h == 31)) {
		dialog += "\n- Invalid end day for selected end month";
	}
	// invalid february length
	if ((b == 2 && d > 28) || (f == 2 && h > 28)) {
		dialog += "\n- Invalid February date(s)";
	}
	// end grade less than start grade
	if (m < k) {
		dialog += "\n- Grade range end less than grade range start";
	}
	if (dialog != "") {
		alert("The following field(s) require(s) valid entry:" + dialog);
		return false;
	}
	return true;
}

// valid new staff
// valid staff update
function staffAddUpdate() {
	var a = document.forms[0].name_first.value;
	var b = document.forms[0].name_last.value;
	var c = document.forms[0].position_id.selectedIndex;
	var d = document.forms[0].fte.value;
	var i = 0;
	var dialog = "";
	if (a == "") {
		dialog += "\n- First name";
	}
	if (b == "") {
		dialog += "\n- Last name";
	}
	if (c == 0) {
		dialog += "\n- Position";
	}
	if (d == "") {
		dialog += "\n- FTE";
	} else {
		if (isNaN(d) || d < 0 || d > 1) {
			dialog += "\n- FTE must be a number 0-1";
		}
	}
	if (dialog != "") {
		alert("The following field(s) require(s) valid entry:" + dialog);
		return false;
	}
	return true;
}

// get list of school training types
function reqSchoolTypes(id) {
	var a = document.forms[0].training_type_school_id;
	if (id != 10) {
		a.options.length = 0;
		a.options[0] = new Option("Select...","");
		a.setAttribute("disabled","disabled");
		return;
	}
	var r = Math.round(Math.random() * 10000000);
	changeContent("typeprogress", progressOn);
	var request = ajaxRequest();
	if (request) {
		request.onreadystatechange = function() {
			if (request.readyState === 4) {
				if (request.status === 200) {
					var queryresults = request.responseText;
					if (queryresults == "") {
						changeContent("typeprogress", progressOff);
						a.options.length = 0;
						a.options[0] = new Option("Select...","");
						a.setAttribute("disabled","disabled");
						return;
					} else {
						var resultsarray = queryresults.split("\r");
						a.options.length = 0;
						a.options[0] = new Option("Select...","");
						var optionscounter = 1;
						var resultsarraylength = resultsarray.length - 1;
						for (var i = 0; i < resultsarraylength; i++) {
							var typeoptions = resultsarray[i].split("|");
							a.options[optionscounter] = new Option(typeoptions[0], typeoptions[1]);
							optionscounter++;
						}
						a.removeAttribute("disabled");
						changeContent("typeprogress", progressOff);
					}
				} else {
					alert("There was a problem retrieving the data:\n" + request.statusText + request.responseText);
				}
			}
		};
		var action_params = "";
		action_params += "?r=" + escape(r);
		request.open("GET", "qry_qeiaschooltrainingtypes.lasso" + action_params, true);
		request.send(null);
	} else {
		alert("Sorry, an error has occurred. Your browser is unable to support dynamic requests.");
	}
}

// valid individual training hours
function validIndvHours() {
	var a = document.indvhours.assignment_id.selectedIndex;
	var b = document.indvhours.training_type_all_id.selectedIndex;
	var c = document.indvhours.training_type_all_id[b].value;
	var d = document.indvhours.training_type_school_id;
	var e = d.length;
	var f = d.selectedIndex;
	var g = document.indvhours.training_hours.value;
	var h = document.indvhours.training_date_start.value;
	var startdate = new Date(h);
	var startdatetime = startdate.getTime();
	var j = document.indvhours.training_date_end.value;
	var enddate = new Date(j);
	var enddatetime = enddate.getTime();
	var dialog = "";
	if (a == 0) {
		dialog += "\n- Name";
	}
	if (b == 0) {
		dialog += "\n- Training type";
	}
	// if training type selected is other and value list is populated
	if (c == 10 && e != 1 && f == 0) {
		dialog += "\n- Other training type";
	}
	if (g == "" || isNaN(g) || g > 80) {
		dialog += "\n- Training hours (digits only; no more than 80)";
	}
	if (h == "") {
		dialog += "\n- Start date";
	} else {
		if (j !== "" && enddatetime < startdatetime) {
			dialog += "\n- End date (prior to start date).";
		}
	}
	if (dialog !== "") {
		alert("The following field(s) require(s) valid entry: " + dialog);
		return false;
	}
	return true;
}

// valid group training hours
function validGroupHours() {
	var a = document.grouphours.training_type_all_id.selectedIndex;
	var b = document.grouphours.training_type_all_id[a].value;
	var c = document.grouphours.training_type_school_id;
	var d = c.length;
	var e = c.selectedIndex;
	var f = document.grouphours.training_hours.value;
	var g = document.grouphours.training_date_start.value;
	var startdate = new Date(g);
	var startdatetime = startdate.getTime();
	var h = document.grouphours.training_date_end.value;
	var enddate = new Date(h);
	var enddatetime = enddate.getTime();
	var j = document.grouphours.assignment_id.length;
	var peoplechecked = false;
	var dialog = "";
	if (a == 0) {
		dialog += "\n- Training type";
	}
	// if training type selected is other and value list is populated
	if (b == 10 && d != 1 && e == 0) {
		dialog += "\n- Other training type";
	}
	if (f == "" || isNaN(f) || f > 80) {
		dialog += "\n- Training hours (digits only; no more than 80)";
	}
	if (g == "") {
		dialog += "\n- Start date";
	} else {
		if (h !== "" && enddatetime < startdatetime) {
			dialog += "\n- End date (prior to start date).";
		}
	}
	if (j == null) {
		peoplechecked = document.grouphours.assignment_id.checked;
	} else {
		for (var i = 0; i < j; i ++) {
			peoplechecked = document.grouphours.assignment_id[i].checked;
			if (peoplechecked) {
				break;
			}
		}
	}
	if (!peoplechecked) {
		dialog += "\n- At least one staff member";
	}
	if (dialog != "") {
		alert("The following field(s) require(s) valid entry:" + dialog);
		return false;
	}
	return true;
}

// valid training type
function validTypeAdd() {
	var a = document.typeadd.training_title.value;
	var b = document.typeadd.training_abbr.value;
	var c = document.typeadd.adm.checked;
	var d = document.typeadd.para.checked;
	var e = document.typeadd.tea.checked;
	var dialog = "";
	if (a == "") {
		dialog += "\n- Training title";
	}
	if (b == "") {
		dialog += "\n- Training abbreviation";
	}
	if (!c && !d && !e) {
		dialog += "\n- Training position";
	}
	if (dialog != "") {
		alert("The following field(s) require(s) valid entry:" + dialog);
		return false;
	}
	return true;
}