/* Create the request object for accessing the server */
var req;
var schools;
var zeroSchoolsWarning = "No schools currently listed for this area";

/* loadSchools function accesses the schools for the current area */
function loadSchools() {
	var searchForm = document.getElementById("search_form");
	var leaBox = searchForm.lea;
	var schoolsBox = searchForm.school;
	
	if(leaBox.options[leaBox.selectedIndex].value !=0){
		
		req = false;
	    // branch for native XMLHttpRequest object
	    if(window.XMLHttpRequest) {
	    	try {
				req = new XMLHttpRequest();
	        } catch(e) {
				req = false;
	        }
	    // branch for IE/Windows ActiveX version
	    } else if(window.ActiveXObject) {
	       	try {
	        	req = new ActiveXObject("Msxml2.XMLHTTP");
	      	} catch(e) {
	        	try {
	          		req = new ActiveXObject("Microsoft.XMLHTTP");
	        	} catch(e) {
	          		req = false;
	        	}
			}
	    }
		if(req) {		
			req.onreadystatechange = processReqChange;
			req.open("GET", "/schools/?lea="+leaBox.options[leaBox.selectedIndex].value, true);
			req.send("");
		}
	} else {
		schoolsBox.options.length = 0;
		schoolsBox.options[0] = new Option("Please select your school",0);
	}
}

/* processReqChange receives the callback from the server */
function processReqChange() {
    // only if req shows "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        loader = document.getElementById("loading");
        loader.style.display="none";
        if (req.status == 200) {
            // ...processing statements go here...
            schools = req.responseXML.getElementsByTagName("school");            			
			
			/* getElementsByTagName produces an array, which we can access  like this:
				product_id[n], the same way we access an array item in PHP. 
				Let's get the id attribute from the product elements like this: */
			
			populateSchools();



        } else {
            alert("There was a problem retrieving the XML data:\n" +
                req.statusText);
        }
    } else if(req.readyState == 1){
    		loader = document.getElementById("loading");
        	loader.style.display="block";
        	var searchForm = document.getElementById("search_form");	
			var schoolsBox = searchForm.school;
        	schoolsBox.options.length = 0;
			schoolsBox.options[0] = new Option("Please wait....",0);
    }
}


function populateSchools(){
	// Build the options associated with this value
	var searchForm = document.getElementById("search_form");
	var schoolsBox = searchForm.school;
	
	// clear the options
	schoolsBox.options.length = 0;
	
	if(schools.length > 0){
		// Loop through and create options
		schoolsBox.options[0] = new Option("Please select your school",0);
		for(i=0; i<schools.length; i++){ //length is the same as count($array)
			id = schools[i].getAttribute('id');
			name = schools[i].getAttribute('name');	
			schoolsBox.options[i+1] = new Option(name,id);
			toggleAffiliates();								
		}
	} else {
			schoolsBox.options[0] = new Option("Please select your school",0);
			schoolsBox.options[1] = new Option("No schools listed for current LEA",0);
			toggleAffiliates();			
	}
	
		
}

function clearSchools(){
	
}

function toggleAffiliates(){
	
}

function openStore(store){
	var searchForm = document.getElementById("search_form");
	var schoolsBox = searchForm.school;
	if(schoolsBox.options[schoolsBox.selectedIndex].value > 0){
		window.open('/jumpoff/?affiliate='+store+'&school='+schoolsBox.options[schoolsBox.selectedIndex].value);
	} else {
		alert("You haven't selected a school so all proceeds will be shared amongst the participating schools!");	
		window.open('/jumpoff/?affiliate='+store+'&school='+schoolsBox.options[schoolsBox.selectedIndex].value);
	}
}

function hideLoader(){
		loader = document.getElementById("loading");
        loader.style.display="none";	
}
