// --------------------- These are the callback functions that receive the data
function clearStates() {
	var tt=document.form1.state;
	// Remove current states listed
	for(var i=tt.options.length-1;i>=0;i--) { 
		tt.remove(i); 
	}
}
function clearCities() {
	var tt=document.form1.city;
	for(var i=tt.options.length-1;i>=0;i--) { 
		tt.remove(i); 
	}
}
function receiveStates() { 
	// Note this function gets called multiple times before receipt is complete -- hence the check for readyState
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		//alert("=="+xmlHttp.responseText+"==response") ;
		clearStates();
		clearCities();
		// If no states returned, finish execution
		if(xmlHttp.responseText == "") { 
			return ;
		}
		var tt=document.form1;
		var selarr = explode(xmlHttp.responseText,"|",false) ;
		if(selarr.length > 0) {
			tt.state.options[tt.state.length] = new Option('Please select State',''); 
			tt.state.selectedIndex = 0 ;
		}
        	var selval ;
		for (k=0;k<selarr.length;k++) {
			selval = explode(selarr[k],"^",false) ;
			tt.state.options[tt.state.length] = new Option(selval[1],selval[0]);
		}
	} 
} 
function receiveCities() { 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
		//alert("=="+xmlHttp.responseText+"==response") ;
		clearCities();
		if (xmlHttp.responseText == "") {
			return ; 
		}
		var tt=document.form1 ;
		var selarr = explode(xmlHttp.responseText,"|",false) ;
		if ( selarr.length > 0 ) {
			// The 'Please select city' doesn't have a value passed as a second parameter because if it does,
			// it will be passed to the SQL statement and generate an execution error.
			tt.city.options[tt.city.length] = new Option('Please select City'); 
			tt.city.selectedIndex = 0 ;
		}
		var selval ;
		for (k=0;k<selarr.length;k++) {
			selval = explode(selarr[k],"^",false) ;
			tt.city.options[tt.city.length] = new Option(selval[1],selval[0]);
		}
	} 
}
	
function receiveSchools() {
	tt=document.form1 ;
	var selval ;
	var selarr ;
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		//alert("=="+xmlHttp.responseText+"==response") ;
		var out = "" ;
		var sc = document.getElementById('school_choice') ;
		sc.innerHTML = "" ;
		sc.style.height = "250px" ;
		var out = trim(xmlHttp.responseText) ;
		if ( out.length > 0 ) {
			//selarr = explode(out,"|",false) ;
			sc.innerHTML = xmlHttp.responseText ;
			//for (k=0;k<selarr.length;k++) {
			//	selval = explode(selarr[k],"^",false) ;
//				sc.innerHTML +="<a href='"+tt.go_to_page.value+"?school_id="+selval[1]+"&choose_school=Y' style='size:2;text-decoration:none;' >"+selval[0]+" (Principal: "+selval[2]+")  "+selval[3]+", "+selval[4]+", "+selval[5]+" "+selval[6]+"</a><br>" ;
				//sc.innerHTML +=selarr[k] ;
				//sc.innerHTML +="<a href='"+tt.go_to_page.value+"?school_id="+selval[1]+"&choose_school=Y' style='size:2;text-decoration:none;' >"+selval[0]+", "+selval[2]+", "+selval[3]+" "+selval[4]+"</a><br>" ;
				//}
		}
		var scl = document.getElementById('school_click') ;
		if ( out.length > 0 ) { 
			scl.innerHTML = "<span style='font-weight:bold;'>Please click on your school below</span>" ; 
			document.getElementById('school_choice').style.display='' ; 
		} else { 
			scl.innerHTML = "<span style='color:red;font-weight:bold;'>Currently there are no participating schools that match your selection.</span>" ; }
		}
}
function GetXmlHttpObject() { 
	var objXMLHttp=null ;
	if (window.XMLHttpRequest) { 
		objXMLHttp=new XMLHttpRequest() ; 
	} else if (window.ActiveXObject) { 
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP") ; 
	}
	return objXMLHttp ;
}

// --------------------------------- New code ------------------------------------------
function callGetData(inURL,fnReceive) {
	tt=document.form1 ;
	xmlHttp=GetXmlHttpObject() ;
	if (xmlHttp==null) {
		show_message("Browser does not support HTTP Request") ;
		document.getElementById('floater1').display='none' ;
		return ;
	}
	var url=syspath+"get_data.php"+inURL;
	// This sets up the callback function that is passed in
	xmlHttp.onreadystatechange=fnReceive;
	xmlHttp.open("GET",url,true) ;
	xmlHttp.send(null) ;
	return false ;	
}

function fetch_schools_by_zip(inZip,gotoPage,fromPage){
	var url="?type=fetch_by_zip";
	if(fromPage=='choose_school') {
		url+="_teacherfilter";
	}
	url=url+"&zip="+escape(inZip);
	url=url+"&go_to_page="+escape(gotoPage) ;
	url=url+"&makeanchor=1" ;
	return callGetData(url,receiveSchools);
}

function fetch_schools_by_citystate(inCity,inState,gotoPage,fromPage){
	var url="?type=fetch_by_citystate";
	if(fromPage=='choose_school') {
		url+="_teacherfilter";
	}
	url=url+"&city="+escape(inCity);
	url=url+"&state="+escape(inState);
	url=url+"&go_to_page="+escape(gotoPage) ;
	url=url+"&makeanchor=1" ;
	return callGetData(url,receiveSchools);
}

function fetch_choose_state(inCountry,fromPage){
	var url="?type=choose_state";
	if(fromPage=='choose_school') {
		url+="_teacherfilter";
	} else if(fromPage=='libsys') {
		url="?type=choose_state_lib";
	}
	url=url+"&country="+escape(inCountry);
	return callGetData(url,receiveStates);
}

function fetch_choose_city(inCountry,inState,fromPage){
	var url="?type=choose_city";
	if(fromPage=='choose_school') {
		url+="_teacherfilter";
	} else if(fromPage=='libsys') {
		url="?type=choose_city_lib";
	}
	url=url+"&country="+escape(inCountry);
	url=url+"&state="+escape(inState);
	return callGetData(url,receiveCities);
}

// ---------------------------- User interface functions ----------------------
// This code is used in at least two pages: register/register.html and /yourlocker/student_locker/configure/choose_school.html
function getZipSchools(fromPage) {
	if (document.form1.zip.value.length > 0 ) { 
		fetch_schools_by_zip(document.form1.zip.value,document.form1.go_to_page.value,fromPage); 
	} else { 
		alert('Please enter a zip code.');
	}
	if(fromPage != 'choose_school') {
		document.getElementById('school_choice').style.display='';
	}
}
function getCityStateSchools(fromPage) {
	var tt = document.form1;
	if ((document.form1.city.selectedIndex > 0) && (document.form1.state.selectedIndex > 0) ) { 
		fetch_schools_by_citystate(tt.city.options[tt.city.selectedIndex].value,tt.state.options[tt.state.selectedIndex].value,tt.go_to_page.value,fromPage); 
	} 
	if(fromPage != 'choose_school') {
		tt.submit.disabled=true ;
	}
}
function getStates(fromPage) {
	var tt = document.form1;
	if(document.form1.country.selectedIndex > 0) {
		fetch_choose_state(tt.country.options[tt.country.selectedIndex].value,fromPage);
	}
}
function getCities(fromPage) {
	var tt = document.form1;
	if( (tt.country.selectedIndex > 0) && (tt.state.selectedIndex > 0) ) {
		fetch_choose_city(tt.country.options[tt.country.selectedIndex].value,tt.state.options[tt.state.selectedIndex].value,fromPage) ;
	}
}

function get_k12mall_affilate(){
callGetData('?type=get_k12mall_affiliate',update_merchant_link) ;
setTimeout("get_k12mall_affilate()",10000);
}
function update_merchant_link(){
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
//alert("=="+xmlHttp.responseText+"==response") ;
if (xmlHttp.responseText == "") {
return ;
}   
document.getElementById('ads1').innerHTML=xmlHttp.responseText;
}
}

