
var HOME_FOLDER = '/scripts/';

var xmlreqs = new Array();
var boxToUpdate;
var currentValueToSet;
var countryList;
var currentCountryText;
var countryBox;

function CXMLReq(freed) {
	this.freed = freed;
	this.xmlhttp = false;
	if (window.XMLHttpRequest) {
		this.xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
}

function xmlreqGetPos(url,functionToUse) {
	var pos = -1;
	
	for (var i=0; i<xmlreqs.length; i++) {
		if (xmlreqs[i].freed == 1) { pos = i; break; }
	}
	if (pos == -1) { pos = xmlreqs.length; xmlreqs[pos] = new CXMLReq(1); }
	return pos;
}
	
function xmlreqGET(pos,url,functionToUse) {	
	if (xmlreqs[pos].xmlhttp) {
		xmlreqs[pos].freed = 0;
		xmlreqs[pos].xmlhttp.open("GET",url,true);
		xmlreqs[pos].xmlhttp.onreadystatechange = function() {
			if (typeof(xmlhttpChange) != 'undefined') { xmlhttpChange(pos,functionToUse); }
			else{alert("equal to undefined");}
		}
		if (window.XMLHttpRequest) {
			xmlreqs[pos].xmlhttp.send(null);
		} else if (window.ActiveXObject) {
			xmlreqs[pos].xmlhttp.send();
		}
	}
	return pos;
}

function xmlhttpChange(pos,handler) {
	if (typeof(xmlreqs[pos]) != 'undefined' && xmlreqs[pos].freed == 0 && xmlreqs[pos].xmlhttp.readyState == 4) {
		if (xmlreqs[pos].xmlhttp.status == 200 || xmlreqs[pos].xmlhttp.status == 304) {
			handler(xmlreqs[pos].xmlhttp.responseXML,pos);
			} 
		else {
			if(xmlreqs[pos].xmlhttp.status != 0)
			{
			alert("Unable to connect to server www.uvolunteer.org");
			}
		}
		xmlreqs[pos].freed = 1;
	}
}

function getAllProjects() {
	var url = HOME_FOLDER + "ajax/load_projects.php";
	var pos = xmlreqGetPos();
	xmlreqGET(pos,url,loadAllProjects);
}


function loadAllProjects(request,pos){
	countryList = request.getElementsByTagName("country");
	//countryBox.options[0].text = currentCountryText;
}


function loadProjects(selectToUpdate,selectedCountry,addAll){
		
	try
    	{
    	selectToUpdate.length = 0;//empty box
		if (selectedCountry == "")
		{
			addOption(selectToUpdate,"","");
			return true;
		}
    	
    	
    	for (var x=0; x<countryList.length; x++) {
    		if (countryList[x].getAttribute("name") == selectedCountry){
    			project = countryList[x].getElementsByTagName("project")
    			
    			if (project.length > 1)	
    			{    			
    				addOption(selectToUpdate,'','');
    				for (var y=0; y<project.length; y++) {
	    				projectName = project[y].childNodes[0].nodeValue;
	    				addOption(selectToUpdate,projectName,projectName);
    				}
    			}
    			else {
    			//there's only one project so we can automatically change the box and project image
    				projectName = project[0].childNodes[0].nodeValue;
	    			addOption(selectToUpdate,projectName,projectName);
	    			getProjectImage(projectName);
	    			//some pages have validation on the project - below will remove any errors on the project field
					try{
						var errorMessage = document.getElementById("error_project");
						errorMessage.parentNode.removeChild(errorMessage);
					}
					catch(e){} 
    			}	
    			break;
    			}
    	   	}
    	selectToUpdate.focus();
    	
    	}
    	
        catch(e){alert("this" + e);}
}



function addOption(selectbox,text,value)//adds an option to a select list
{
	var optn = document.createElement("option");
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}

function showArea(areaToShow){
	var showDiv = document.getElementById(areaToShow);
	showDiv.style.display = "block";
}

function hideArea(areaToHide){
	var hideDiv = document.getElementById(areaToHide);
	hideDiv.style.display = "none";
}


//handle the ajax response - only used when we just need to respond with either success or error message on failure
function handleResponse(request,successMessage,reload,newLocation)
{
	if (!request){//no response in standards /dom2 supporting browsers. 
		alert("Update Failed");
		return false;
	}
	
	/*if there was no response in ie it will continue and run the below however it will then throw an error 
	when we try to access the non existent element. unfortunately hasAttribute isn't supported by ie */
	var response = request.getElementsByTagName("document");
	try{
		if(response[0].getAttribute("success")== "true"){
				alert(successMessage);
			if (reload){
				document.full_application_form.action = newLocation;
					document.full_application_form.submit();
				}
			return true;		
		}
		else if (response[0].getAttribute("success")== "false"){
			if (response[0].getAttribute("type") == "Session Expired Exception")
			{
				alert(response[0].getAttribute("message"));
				document.full_application_form.submit();
				return false;
			}
			else alert(response[0].getAttribute("message") + ",\n Line No = " + response[0].getAttribute("line")); 
			return true;		
		}	
		
	}
	catch (e)
	{//there was no response or the attribute didn't exist - ie only
		alert("Update Failed");
		return false;
	}
}

