// - code
/* 
	* Updated by: Sayvai Tosu (yeah... the CBSS lad) on 19/07/2008.
	* Amendment: Created a single variable named sboroughname and added IF conditional in lines 97-101 and 105.
	* Purpose of amendment: To disable list printout of SN teams based in City of London, since the MPS do not cover that single area.
	* Amendment: Created a single variable named sWard on line 138 and re-modified CASE conditionl in lines 140-164.
	* Purpose of amendment: To dynamically create new links, linking to the new SN template. NOTE: Will need to uncomment the relevant lines for either PILOT or COMPLETE rollout. Please read comments within the CASE conditional for help.
*/

var requester = null; 

function init()
{
	document.getElementById("frmPostcode").onsubmit=onChangeReceipt;
	handleWardCode();
	
}

function handleWardCode()
{

	if(window.location.hash.replace("#","")!="")
	{
		for(var i=0;i<document.getElementsByTagName("h1").length;i++)
		{
			if (document.getElementsByTagName("h1")[i].id.substr(0,3)=="h00")
			{
				document.getElementsByTagName("h1")[i].style.color=document.getElementById("hMainTitle").style.color;
			}
		}
		document.getElementById("h"+window.location.hash.replace("#","")).style.color='red';
	}
}

function onChangeReceipt() 
{
	/* Check for running connections */ 
	if (requester != null && requester.readyState != 0 && requester.readyState!= 4) 
	{ 
		requester.abort(); 
	} 

	 try 
	 { 
	   	requester = new XMLHttpRequest(); 
	 } 
	 catch (error) 
	 { 
		   try 
		   { 
			 	requester = new ActiveXObject("Microsoft.XMLHTTP"); 
		   } 
		   catch (error) 
		   {
			 	requester = null; 

			 	return false; 
		   } 
 	} 

	requester.onreadystatechange = onreadystatechangeReceipt; 
	requester.open("GET", "/scripts/postcode.php?postcode=" + document.getElementById("postcode").value); 
	requester.send(null); 

	return false; 
}

function onreadystatechangeReceipt() 
{ 
	/* If XMLHR object has finished retrieving the data */ 
	if (requester.readyState == 4) 
	{ 
		/* If the data was retrieved successfully */ 
		if (requester.status == 200) 
		{ 
		 writeDetails(); 
		} 
		/* IE returns a status code of 0 on some occasions, so ignore this
		case */ 
		else if (requester.status != 0) 
		{ 
		 alert("There was an error while retrieving the URL: " + requester.statusText); 
		} 
	} 
	
	return true; 
}

function writeDetails() 
{ 
	//var receipt = document.getElementById("ward"); 
	if (requester.responseText.charAt(0) == "<") 
	{ 
		//redirect
		var oXML=requester.responseXML;
		if (oXML.getElementsByTagName("ward").length>1)
		{
			var s="<ul>";
			for(var i=0;i<oXML.getElementsByTagName("ward").length;i++)
			{
				var sboroughname = oXML.getElementsByTagName("ward")[i].getElementsByTagName("borough")[0].childNodes[0].nodeValue.toLowerCase().replace(/ /g,"");	
				if (sboroughname == "cityoflondon") {
					//Do nothing. Basically do not write the link texts.
				}
				else {
					s+="<li><a class='redschemelink' href='" + retrieveSNURL(oXML.getElementsByTagName("ward")[i]) + 
					"'>" + oXML.getElementsByTagName("wardname")[i].childNodes[0].nodeValue + 
					", " + oXML.getElementsByTagName("borough")[i].childNodes[0].nodeValue + "</a></li>";
				}
			}
			s+="</ul>";
		}
		else
		{
			window.location=retrieveSNURL(oXML.getElementsByTagName("ward")[0]);
			s=null;
			handleWardCode();
		}

	}
	else
	{
		s="No match found.";
	}
	if(s!=null)
	{
		document.getElementById("divPostcodeWardMatches").innerHTML=s;
	}
	
	return true; 
}

function retrieveSNURL(oWard)
{

	var url="";
	var ward_name = "";
	var sBorough=oWard.getElementsByTagName("borough")[0].childNodes[0].nodeValue.toLowerCase().replace(/ /g,"");
	var sWard=oWard.getElementsByTagName("wardname")[0].childNodes[0].nodeValue.toLowerCase().replace(/ /g, "").replace(/&/g,"and").replace(/'/g, "").replace(/\(/g, "_").replace(/\)/g, "");

	switch(sBorough)
	{
////////******** When new template system for piloting is ready, UNCOMMENT lines 144 to 155 ********//////// 
		case "haringey":
				url="/saferneighbourhoods/boroughs/" + sBorough + "/" + sWard + ".htm";
				break;
/*		case "hackney":
				url="/saferneighbourhoods/boroughs/" + sBorough + "/" + sWard + ".htm";
				break;
		case "southwark":
				url="/saferneighbourhoods/boroughs/" + sBorough + "/" + sWard + ".htm";
				break;
		case "ealing":
				url="/saferneighbourhoods/boroughs/" + sBorough + "/" + sWard + ".htm";
				break; */
////////******** When new template system is ready for COMPLETE rollout, UNCOMMENT lines 157 to 160 ********////////
/*		default:
			url="/saferneighbourhoods/boroughs/" + sBorough + "/" + sWard + ".htm";
			url=url.replace(/kingstonuponthames/,"kingston");
			url=url.replace(/richmonduponthames/,"richmond"); */
			// NOTE: If a situations arises that the wardname outputted by script is different to SN team filename therefore creating an incorrect link then just ADD another url=url.replace() line at the end of 'default' within the CASE conditional. Simple as pie!
		default:
			//url="/saferneighbourhoods/boroughs/" + sBorough + "/saferneighbourhoods.htm#" + oWard.getElementsByTagName("wardcode")[0].childNodes[0].nodeValue;
			url="/saferneighbourhoods/boroughs/" + sBorough + "/saferneighbourhoods.htm#" + oWard.getElementsByTagName("wardcode")[0].childNodes[0].nodeValue.toUpperCase();
			url=url.replace(/kingstonuponthames/,"kingston");
			url=url.replace(/richmonduponthames/,"richmond");
	}
	return (url);

}

// - end of code
