//-------------------------
// AJAX Main Function
//-------------------------
function getXMLHTTPRequest()
{
	try
	{
		req = new XMLHttpRequest();
	}
	catch(e)
	{
		try
		{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				req = false;
			}
		}
	}
	if(!req)
	{
		alert("Error creating Ajax Object, please contact Web & Systems Dept.");
	}
	else
	{
		return req;
	}
	return req;
}

var http = getXMLHTTPRequest();

function changeit()
{
	var view = document.getElementById("theselector").options[document.getElementById("theselector").selectedIndex].value;
	changetheview(view);
}

function changetheview(view)
{
	http.open("GET", "content/operations/scripts/flyuk_today_ajax.php?do=view&view="+view+"", true);
	http.onreadystatechange = returnchangeview;
	http.send(null);
}

function returnchangeview()
{
	if(http.readyState == 4)
	{
		if(http.status == 200)
		{
			// Common
			var view = http.responseXML.getElementsByTagName("view")[0].childNodes[0].nodeValue;
			
			if(view == 'hours')
			{
				var niceview = "Pilots Based On Hours";
				var tophtml = "<tr><td width='248'><b>Pilot ID</b></td><td width='249'><b>Name</b></td width='249'><td><b>Hours</b></td></tr>";
				var selector = "<option value='hours' selected>Pilots Based On Hours</option><option value='pireps'>Pilots Based On Pireps</option>";
			}
			if(view == 'pireps')
			{
				var niceview = "Pilots Based On Pireps";
				var tophtml = "<tr><td width='248'><b>Pilot ID</b></td><td width='249'><b>Name</b></td width='249'><td><b>Pireps</b></td></tr>";
				var selector = "<option value='hours'>Pilots Based On Hours</option><option value='pireps' selected>Pilots Based On Pireps</option>"
			}
			if(view == 'newpilots')
			{
				var niceview = "Latest Recruits";
				var tophtml = "<tr><td><b>Pilot ID & Name</b></td><td><b>Date Joined</b></td><td>&nbsp;</td></tr>";
			}
			
			// Start Table
			var html = "<table border='0' width='100%' cellspacing='0' cellpadding='4'><tr><td colspan='3'><p align='left'><select size='1' id='theselector' onchange='changeit();'>" + selector + "</select><br><br></p></td></tr>";
			html += tophtml;
			
			// Fetch Arrays
			var pilotidarray = http.responseXML.getElementsByTagName("pilotid");
			var pilotnamearray = http.responseXML.getElementsByTagName("pilotname");
			var colonearray = http.responseXML.getElementsByTagName("colone");
			var rowarray = http.responseXML.getElementsByTagName("row");
			
			// Loop
			for( i=0; i<pilotidarray.length; i++)
			{
				var pilotid = pilotidarray.item(i).firstChild.data.toString();
				var pilotname = pilotnamearray.item(i).firstChild.data.toString();
				var colone = colonearray.item(i).firstChild.data.toString();
				var row = rowarray.item(i).firstChild.data.toString();
				
				html += "<tr class='"+row+"'><td><a href='index.php?page=pilotprofile&id="+pilotid+"' title='View Profile'>"+pilotid+"</a></td><td>"+pilotname+"</td><td>"+colone+"</td></tr>";
			}
			
			// Finish Up
			html += "</table>";
			
			document.getElementById("thetabletodo").innerHTML = html;
		}
	}
	else
	{
		document.getElementById("thetabletodo").innerHTML = "<img src='images/icons/wait.gif' border'0' alt='Loading...' />&nbsp;&nbsp;<span class='mainheaderbold'>Loading Data...</span>";
	}
}
