// JavaScript Document
var mr,cb,cd,db,dd,tb,td,sb,sd,ss,bt,ds,a,t,fil,sfil;
function initSchoolPicker(localPath)
{
	mr = document.getElementById("schoolMode");
	cb = document.getElementById("countyDrop");
	cd = document.getElementById("cboCounty");
	tb = document.getElementById("typeDrop");
	td = document.getElementById("cboType");
	db = document.getElementById("districtDrop");
	dd = document.getElementById("cboDistrict");
	sb = document.getElementById("schoolDrop");
	sd = document.getElementById("cboSchool");
	ss = document.getElementById("schoolSingle");
	bt = document.getElementById("schoolSubmit");
	ds = true;

	a = ( localPath ) ? localPath : '../_data/schoolPicker.php'; //a
	m = 'public';	//m
}

//gm
function gm(mode) {
	m = mode;
	if ( cd.selectedIndex > 0 ) {
		runAjaxCall(a + "?mode=" + m + "&county=" + cd.options[cd.selectedIndex].value + "&n=1",'modeSelect',null,null);
	}
}

//cc
function cc(obj) {
	if ( cd.selectedIndex>0 ) {
		runAjaxCall(a + "?mode=" + m + "&county=" + cd.options[cd.selectedIndex].value + "&n=1",'modeSelect',null,null);
	}
}

//st
function st(obj) {
	if ( obj.selectedIndex > 0 ) {
		runAjaxCall(a + "?mode=" + m + "&county=" + cd.options[cd.selectedIndex].value + "&type=" + obj.options[obj.selectedIndex].value + "&n=1",'typeSelect',null,null);
	}
}
//snd
function snd(obj) {
	if (obj.selectedIndex > 0) {
		runAjaxCall(a + "?mode=" + m + "&county=" + cd.options[cd.selectedIndex].value + "&type=" + td.options[td.selectedIndex].value + "&inst=" + obj.options[obj.selectedIndex].value + "&n=1",'districtSelect',null,null);
	}
}
//r
function resetDropdown(obj)
{
	if (obj.options.length>1)
	{
		obj.options.length = 0;
		opt = document.createElement('option');
		opt.value = ""; opt.text = "Select...";
		obj.options.add(opt);
	}
}
//sdd
function showDistrictDrop(mode)
{
	if (mode) {
		// SHOW the district drop
		db.style.display = "block";
		dd.style.display = "inline";
	} else {
		// Hide the district drop
		db.style.display = "none";
		dd.style.display = "none";
	}
}

//
function showSchoolDrop(mode)
{
	if (mode) {
		// SHOW the district drop
		sb.style.display = "block";
		sd.style.display = "inline";
	} else {
		// Hide the district drop
		sb.style.display = "none";
		sd.style.display = "none";
	}
}

function showSchoolSingle(mode)
{
	if (mode) {
		// SHOW the district drop
		ss.style.display = "block";
	} else {
		// Hide the district drop
		ss.style.display = "none";
	}
}

//
function drawSingleItem(ary) {
	var itemBox = "<input type=\"hidden\" name=\"cboSchool\" id=\"cboSchool\" value=\""+ary[0]+"\" /><span id=\"lblSingleSchool\">"+ary[1]+"</span>";
	ss.innerHTML = itemBox;
}

function modeSelect(str) {
	
	oAry = str.split('|');
	
	showDistrictDrop(false);
	showSchoolDrop(false);
	showSchoolSingle(false);
	td.options.length = 0;
	var stOpt = document.createElement('option');
	stOpt.value = "";
	stOpt.text = "Select...";
	td.options[0] = stOpt;
	for(o=0;o<oAry.length;o++)
	{
		tmpAry = oAry[o].split('~');
		var newOpt = document.createElement('option');
		newOpt.value = tmpAry[0];
		newOpt.text = tmpAry[1];
		td.options[td.options.length] = newOpt;
	}
	
}

function typeSelect(str) {
	
	oAry = str.split('|');
	
	if (m == "public") {
		showDistrictDrop(true);
		showSchoolDrop(false);
		showSchoolSingle(false);
		resetDropdown(dd);
		for(o=0;o<oAry.length;o++)
		{
			tmpAry = oAry[o].split('~');
			if (tmpAry[0] != "" && tmpAry[1] != "") {
				newOpt = document.createElement('option');
				newOpt.value = tmpAry[0];
				newOpt.text = tmpAry[1];
				newOpt.disabled = (tmpAry[1].search(/\* /)>-1)?true:false;
				dd.options[dd.options.length] = newOpt;
			}
		}
	} else {
		showDistrictDrop(false);
		showSchoolDrop(true);
		showSchoolSingle(false);
		resetDropdown(sd);
		for(o=0;o<oAry.length;o++)
		{
			tmpAry = oAry[o].split('~');
			if ( tmpAry[0] != "" && tmpAry[1] != "" ) {
				newOpt = document.createElement('option');
				newOpt.value = tmpAry[0];
				newOpt.text = tmpAry[1];
				newOpt.disabled = (tmpAry[1].search(/\* /)>-1)?true:false;
				sd.options[sd.options.length] = newOpt;
			}
		}
	}
}

function districtSelect(str) {
	
	oAry = str.split('|');
	
	showDistrictDrop(true);
	// If the length is not > 1 then its a single entity - replace dropdown with single line
	if ( oAry.length > 1 ) {
		showSchoolDrop(true);
		showSchoolSingle(false);
		resetDropdown(sd);
		for(o=0;o<oAry.length;o++)
		{
			tmpAry = oAry[o].split('~');
			if ( tmpAry[0] != "" && tmpAry[1] != "" ) {
				newOpt = document.createElement('option');
				newOpt.value = tmpAry[0];
				newOpt.text = tmpAry[1];
				newOpt.disabled = ( tmpAry[1].search(/\* /) > -1 ) ? true : false;
				sd.options[sd.options.length] = newOpt;
			}
		}
	} else {
		// WE ONLY HAVE ONE - replace dropdown with checkbox
		showSchoolDrop(false);
		showSchoolSingle(true);
		tmpAry = oAry[0].split('~');
		drawSingleItem(tmpAry);
	}
}

function lookupAddress(ajaxPath)
{
	thisMode = m;
	thisSchool = sd.options[sd.selectedIndex].value;
	runAjaxCall(ajaxPath + "?mode=" + m + "&school=" + thisSchool,'popAddress',null,null);
}

function popAddress(str)
{
	addr1 = document.getElementById('txtStreetAddress1');
	addr2 = document.getElementById('txtStreetAddress2');
	city = document.getElementById('txtCity');
	zip = document.getElementById('txtZipcode');
	
	oAry = str.split('~');
	
	addr1.value = oAry[0];
	addr2.value = oAry[1];
	city.value = oAry[2];
	zip.value = oAry[3];
}