var xmlHttp;
var isTrust = 0;

function region_select(str)
{
	isTrust = 0;

	if (str != -1)
	{
		//Has changed so lets reset the hospitals...
		document.getElementById('hospArea1').innerHTML = "<select name=\"yd_hospital1\" id=\"yd_hospital1\"  onchange=\"showFieldIf(this.name, 'other', 'yd_hospital1_other')\" ><option value=\"-1\">Please select a trust...</option></select>"
		document.getElementById('hospArea2').innerHTML = "<select name=\"yd_hospital2\" id=\"yd_hospital2\"  onchange=\"showFieldIf(this.name, 'other', 'yd_hospital2_other')\" ><option value=\"-1\">Please select a trust...</option></select>"



		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		}

		xmlHttp2=GetXmlHttpObject()
		if (xmlHttp2==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		}


		var url="/registration/region_select.php"
		url=url+"?q="+str
		url=url+"&t=1"
		url=url+"&sid="+Math.random()

		xmlHttp.onreadystatechange=stateChanged
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)


		var url="/registration/region_select.php"
		url=url+"?q="+str
		url=url+"&t=2"
		url=url+"&sid="+Math.random()

		xmlHttp2.onreadystatechange=stateChanged
		xmlHttp2.open("GET",url,true)
		xmlHttp2.send(null)
	}
	else
	{
		// They have changed the db region back to -1.... reset everything!
		document.getElementById('trustArea').innerHTML = "<select name=\"yd_trust\" id=\"yd_trust\"  onchange=\"trust_select(this.value); showFieldIf(this.name, 'other', 'yd_trust_other')\" ><option value=\"-1\">Please select a database region...</option></select>"
		document.getElementById('uniArea').innerHTML = "<select name=\"yd_uni\" id=\"yd_uni\"  onchange=\"trust_select(this.value); showFieldIf(this.name, 'other', 'yd_uni_other')\" ><option value=\"-1\">Please select a database region...</option></select>"
		document.getElementById('hospArea1').innerHTML = "<select name=\"yd_hospital1\" id=\"yd_hospital1\"  onchange=\"showFieldIf(this.name, 'other', 'yd_hospital1_other')\" ><option value=\"-1\">Please select a trust...</option></select>"
		document.getElementById('hospArea2').innerHTML = "<select name=\"yd_hospital2\" id=\"yd_hospital2\"  onchange=\"showFieldIf(this.name, 'other', 'yd_hospital2_other')\" ><option value=\"-1\">Please select a trust...</option></select>"

	}
}



function removeOpts(selectObj)
{
	var i;
	for (i = selectObj.length - 1; i>=0; i--)
	{
		if (selectObj.options[i].selected)
		{
			selectObj.remove(i);
		}
	}
}


function trust_select(str)
{
	if (str != -1)
	{

		xmlHttp=GetXmlHttpObject()
		if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		}



		xmlHttp2=GetXmlHttpObject()
		if (xmlHttp2==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		}


		isTrust = 1;

		var url="/registration/trust_select.php"
		url=url+"?q="+str
		url=url+"&sid="+Math.random()

		xmlHttp.onreadystatechange=stateChanged
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)




		var url="/registration/trust_select.php"
		url=url+"?q="+str
		url=url+"&t=2"
		url=url+"&sid="+Math.random()

		xmlHttp2.onreadystatechange=stateChanged
		xmlHttp2.open("GET",url,true)
		xmlHttp2.send(null)



	}
}


function stateChanged()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{
		if (isTrust == 1)
		{
			// Trust has been modified.... update the hospitals
			document.getElementById("hospArea1").innerHTML=xmlHttp.responseText
			document.getElementById("hospArea2").innerHTML=xmlHttp2.responseText
		}
		else
		{
			document.getElementById("trustArea").innerHTML=xmlHttp.responseText
		}
	}

	if (isTrust != 1)
	{
		if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete")
		{
			document.getElementById("uniArea").innerHTML=xmlHttp2.responseText
		}
	}
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}