var strXmlPath = "xml-centres.aspx";
var strPagePath = "centres.aspx";
var strDefaultName = "Search For A Centre";
var strHeadEl = "headName";
var strSelectEl = "slCentre";

function changeCentreName(strName)
{
	var ptrEl = document.getElementById(strHeadEl);
	
	if (ptrEl)
	{
		ptrEl.innerHTML = strName;
	};
};

function resetCentreName()
{
	changeCentreName(strDefaultName)
};

function selectCentre(strCentreID)
{
	document.location = strPagePath + "?slCentre=" + strCentreID;
};

function setupForm()
{
	var slCentre;
	var txtPostcode;
	var strVars;
	var strDealerID = "";
	var strPostcode = "";
	var arSplit;
	var ptrInputPostcode, ptrBtnSubmit;

	// attempt to load the ID for the centre from the select list first
	slCentre = document.getElementById("slCentre");

	if (slCentre != null)
	{
		if (slCentre.selectedIndex > 0)
		{
			strDealerID = slCentre[slCentre.selectedIndex].value;
		};
	};

	if (strDealerID == "")
	{
		arSplit = document.location.href.split("?slCentre=");
		if (arSplit.length > 1)
		{
			strDealerID = arSplit[1];
		};
	};
	
	txtPostcode = document.getElementById("txtPostcode");
	
	if (txtPostcode != null)
	{
		strPostcode = txtPostcode.value;
	};

	strVars = "xmlsrc=" + strXmlPath;

	if (strDealerID != "")
	{
		strVars += "&amp;dealerid=" + strDealerID;
	};
	
	if (strPostcode != "")
	{
		strVars += "&amp;postcode=" + strPostcode;
	};
	
	ufoReplace("ufoCentresMap", "flash/", strVars);
	
	ptrEl = document.getElementById("txtPostcode");
	
	if (ptrEl)
	{
		ptrEl.onclick = function()
		{
			if (this.value == strDefaultPostcodeText)
			{
				this.value = "";
			};
		};
		ptrEl.onblur = function()
		{
			if (this.value == "")
			{
				this.value = strDefaultPostcodeText;
			};
		};
	};
	
	ptrEl = document.getElementById("btnFindCentre");
	
	if (ptrEl)
	{
		ptrEl.onclick = function()
		{
			var ptrEl = document.getElementById("txtPostcode");
			
			if (ptrEl)
			{
				if (ptrEl.value == strDefaultPostcodeText)
				{
					ptrEl.value = "";
				};
			};
		};
	};
};

function setupOptions()
{
	var ptrEl = document.getElementById(strSelectEl);
	
	if (ptrEl)
	{
		var arEls = ptrEl.getElementsByTagName("option");
		var i = 0;
		
		for (i = 0; i < arEls.length; i++)
		{
			arEls[i].onclick = function()
			{
				selectCentre(this.value);
			};
		};
	};
};

/*
	MAIN
*/

var strDefaultPostcodeText = "Enter postcode";

function bespokeOnLoad()
{
	setupForm();
	setupOptions();
};