//var loadstatustext="<img src='images/loading.gif' /> Requesting content...<br/><br/><br/><br/><br/><br/><br/><br/>"

//ajax object
function createRequestObject(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	return ajaxRequest;
}


var http = createRequestObject();

var county_request = "<img src='images/loading.gif' /> Requesting content...";
var main_search = "<img src='images/loading.gif' /> Searching ...";

var active_county = false

//county schools request
function sndReq(id, county_name, currentlink) {
	document.getElementById('loading').style.visibility="visible";
	document.getElementById('loading').innerHTML = county_request;
	if (active_county) active_county.style.color = ""; //reset old link
	active_county = document.getElementById(currentlink);
	active_county.style.color = "#FFFAC4";
	
    http.open('get', 'scripts/countybyid.php?id='+id+'&countyname='+escape(county_name));
    http.onreadystatechange = handleResponse;
    http.send(null);
	//return true; this kills ajax
}

//county schools response
function handleResponse() {
    if(http.readyState == 4){
            document.getElementById('content_centre').innerHTML = http.responseText;
			//document.getElementById('content_centre').style.height = 500;
			document.getElementById('loading').style.visibility="hidden";
    }
}

//main search request
function MainSearch() {
	document.getElementById('loading').innerHTML = main_search;
	document.getElementById('loading').style.visibility="visible";
    http.open('get', '/sphider/search.php?query='+escape(document.getElementById('mainsearch').value));
    http.onreadystatechange = MainSearchResponse;
    http.send(null);
	//return false;
}

//county schools response
function MainSearchResponse() {
    if(http.readyState == 4){
            document.getElementById('content_centre').innerHTML = http.responseText;
			document.getElementById('loading').style.visibility="hidden";
    }
}



//prospectus: get checked boxes before calling the form
function ProspectusRequest()
{
	var strquery = "";
	
	var ci = document.prospectus.school_request.length;
	
	if (ci == undefined)
	{
		if (document.prospectus.school_request.checked) strquery = document.prospectus.school_request.value;
	} else {
	for (var i=0; i < document.prospectus.school_request.length; i++)
	   {
	   if (document.prospectus.school_request[i].checked)
		  {
		  strquery = strquery + document.prospectus.school_request[i].value + ",";
		  }
	   }
	}
	if (strquery == ''){
		alert ('Please select schools before submitting.');
	} else {
		document.getElementById('loading').style.visibility="visible";
		http.open('get', 'scripts/prospectusform.php?id='+strquery);
		http.onreadystatechange = handleResponse;
		http.send(null); 
	}
}

function ProspectusCheckCheckboxes()
{
	var strquery = "";
	
	var ci = document.prospectus.school_request.length;
	
	if (ci == undefined)
	{
		if (document.prospectus.school_request.checked) strquery = document.prospectus.school_request.value;
	} else {
	for (var i=0; i < document.prospectus.school_request.length; i++)
	   {
	   if (document.prospectus.school_request[i].checked)
		  {
		  strquery = strquery + document.prospectus.school_request[i].value + ",";
		  }
	   }
	}
	if (strquery == ''){
		alert ('Please select schools before submitting.');
		return false;
	} else {
		return true;
	}

}


//quick search request
function QuickSearch(sage, stype, schtype, scounty) 
{
	document.getElementById('quick_loading').style.visibility="visible";
    http.open('get', 'scripts/quicksearch.php?sage='+sage+'&stype='+stype+'&scounty='+scounty+'&sschooltype='+schtype);
    http.onreadystatechange = QuickSearchResponse;
    http.send(null);
	//return false;
}

//quick search response
function QuickSearchResponse() {
    if(http.readyState == 4){
            //document.getElementById('main_center_pic').style.background = "none";
			//document.getElementById('main_center_pic').style.marginLeft = "0px";
			document.getElementById('mc').innerHTML = http.responseText;
			document.getElementById('quick_loading').style.visibility="hidden";
    }
}





/******************************************************************************
File upoload functions    */

function startUpload(){
		//document.getElementById('fileup').innerHTML = "<img src='images/loading.gif' /> Uploading image ...";
      document.getElementById('fileup').style.visibility="visible";
      return true;
}

function stopUpload(success, fpath){
	 if (success == "failed")
	 {
	  	document.getElementById('fileup').innerHTML = "There was an error uploading the file, please try again!"; 
	 } else {
		 document.getElementById('cimg').src = fpath+"/"+success;
		 document.getElementById('logo').value = success;
		 document.getElementById('fileup').innerHTML = "<span style='color:blue'>The image "+success+" has been uploaded. Please check the preview below.</p>";
		 
	 }
      return true;   
}



//home page rollovers
function Seth(stext)
{
	document.getElementById('h_h').innerHTML = stext;
}
function Setp(stext)
{
	document.getElementById('h_p').innerHTML = stext;
}
function Setl(stext)
{
	document.getElementById('h_l').href = stext;
}
function Seti(stext)
{
	newimage = 'url('+stext+')';
	document.getElementById('main_center_pic').style.backgroundImage = newimage;
}

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];}}
}


/*school preview functions */
function SchoolPreview() {
	//alert(document.getElementById('G_low').selectedIndex);
	//var dropy = document.getElementById('G_low');
	//school name
	document.getElementById('cschool').innerHTML = document.getElementById('sch1').value;
	//address
	document.getElementById('caddress').innerHTML= document.getElementById('ad1').value+' '+document.getElementById('ad2').value+' '+document.	getElementById('ad3').value+' '+document.getElementById('zip').value;
	//county
	document.getElementById('ccounty').innerHTML = document.getElementById('cnt').value;
	//tel
	document.getElementById('ctel').innerHTML = document.getElementById('tel1').value;
	
	//type
	var comma = false;
	var stype = "";
	
	if (document.getElementById('Boarding').checked)
	{
		stype += 'Bo'; 
		comma = true;
		if (document.getElementById('Bo_low').selectedIndex != 0 || document.getElementById('Bo_high').selectedIndex != 0)
		{
			stype += '('+document.getElementById('Bo_low').selectedIndex+'-'+document.getElementById('Bo_high').selectedIndex+')';
		}
	}
	if (document.getElementById('Day').checked)
	{
		if (comma == true) stype += ',';
		stype += 'D'; 
		comma = true;
		if (document.getElementById('D_low').selectedIndex != 0 || document.getElementById('D_high').selectedIndex != 0)
		{
			stype += '('+document.getElementById('D_low').selectedIndex+'-'+document.getElementById('D_high').selectedIndex+')';
		}
	}
	if (document.getElementById('Boys').checked)
	{
		if (comma == true) stype += ',';
		stype += 'B'; 
		comma = true;
		if (document.getElementById('B_low').selectedIndex != 0 || document.getElementById('B_high').selectedIndex != 0)
		{
			stype += '('+document.getElementById('B_low').selectedIndex+'-'+document.getElementById('B_high').selectedIndex+')';
		}
	}
	if (document.getElementById('Girls').checked)
	{
		if (comma == true) stype += ',';
		stype += 'G'; 
		comma = true;
		if (document.getElementById('G_low').selectedIndex != 0 || document.getElementById('G_high').selectedIndex != 0)
		{
			stype += '('+document.getElementById('G_low').selectedIndex+'-'+document.getElementById('G_high').selectedIndex+')';
		}
	}
	if (document.getElementById('Mixed').checked)
	{
		if (comma == true) stype += ',';
		stype += 'M'; 
		comma = true;
		if (document.getElementById('M_low').selectedIndex != 0 || document.getElementById('M_high').selectedIndex != 0)
		{
			stype += '('+document.getElementById('M_low').selectedIndex+'-'+document.getElementById('M_high').selectedIndex+')';
		}
	}
	if (document.getElementById('Age_low').selectedIndex != 0 || document.getElementById('Age_high').selectedIndex != 0)
	{
		if (comma == true) stype += ',';
		stype += document.getElementById('Age_low').selectedIndex+'-'+document.getElementById('Age_high').selectedIndex;
	}
	document.getElementById('ctype').innerHTML = stype;
}

function SetSchoolFields (scheckbox)
{
	if(scheckbox.checked)
	{
		document.getElementById('sch1').value = document.freecopy.schoolname.value;
		document.getElementById('ad1').value = document.freecopy.address1.value;
		document.getElementById('ad2').value = document.freecopy.address2.value;
		document.getElementById('ad3').value = document.freecopy.address3.value;
		document.getElementById('cnt').value = document.freecopy.county.value;
		document.getElementById('zip').value = document.freecopy.postcode.value;
	}
}