var xmlDoc;	
var p; // number of jobs parsed
var init = false;
var n;

function setJob() {
	
	if (n<p-1) {
		n ++;
	} else {
		n=0;
	}
	
	// set HTML content based on XML values
	var position = "<strong>" + xmlDoc.getElementsByTagName("POSITION")[n].childNodes[0].nodeValue + "</strong><br/><br/>";
	var skills = xmlDoc.getElementsByTagName("SKILLS")[n].childNodes[0].nodeValue;
	var appURL=xmlDoc.getElementsByTagName("APPL_URL")[n].childNodes[0].nodeValue;

	document.getElementById("position").innerHTML= position + skills + ". " + "<a href='" + appURL + "' target='_blank'>More info</a>";
	
	var ourInterval = setTimeout("setJob();", 5000); // timeout to change image at set period
}

function showAllJobs() {
	var allJobs="";
	
	if (p>0) {
		for (var i=0;i<p;i++){
		
			// set HTML content based on XML values
			var position = "<strong>" + xmlDoc.getElementsByTagName("POSITION")[i].childNodes[0].nodeValue + "</strong><br/><br/>";
			var skills = xmlDoc.getElementsByTagName("SKILLS")[i].childNodes[0].nodeValue;
			var appURL=xmlDoc.getElementsByTagName("APPL_URL")[i].childNodes[0].nodeValue;
	
			allJobs += "<div>" + "<h2>" + position + "</h2>" + "<p>" + skills + ". " + "<a href='" + appURL + "' target='_blank'>More info</a></p></div>";
			
			if (i<p-1) allJobs += "<div class='greySeparator'></div>";
		}
		var summary = "<h3>We have " + p + " fantastic opportunities now available. Displaying 1 - " + p + " of " + p + " jobs found.</h3>";
		document.getElementById("featuredJobs").innerHTML= summary + allJobs;
	} else {
		document.getElementById("featuredJobs").innerHTML="There are no jobs that match your search criteria.";
	}
}

function fAsynchronousGet(URL,page) {
	var oXMLHttpRequest	= new XMLHttpRequest;
	oXMLHttpRequest.open("GET", URL, true);
	oXMLHttpRequest.onreadystatechange = function() {
		if (this.readyState == XMLHttpRequest.DONE) {
			// THIS IS CALLED ON A SUCCESSFUL LOAD OF THE XML
			//alert("success");
			xmlDoc=oXMLHttpRequest.responseXML;
			
			try {
				var x=xmlDoc.getElementsByTagName('POSITION'); // find number of child elements
				p = x.length; // set P to number of XML child elements
				
				if (page=="home") {
					setJob();
				} else if (page=="current positions"){
					showAllJobs();
				}
			
			} catch(err){
				//alert("error loading xml from " + page);
				
				var msg = "There are no jobs that match your search criteria.";
			
				if (page=="home") {
					document.getElementById("position").innerHTML=msg;
				} else{
					document.getElementById("featuredJobs").innerHTML=msg;
				}
			}
			
		} else {
			//alert("loading xml...");			
		}
		
	}
	oXMLHttpRequest.send(null);
}
