//  PMS postcode entry functionality script
//  - Used in conjuction with pcvalidation.js, pcobdata.js and pcfcdata.js
//  - Author: Cy Pearce 
//	- Vers: 1.0 29/08/2006


function change(id,newClass)
{
//This function switches the colour of the text in the postcode form box to red if invalid and black if valid/being edited.
         identity=document.getElementById(id);
         identity.className=newClass;
}

function clearText(thefield) 
{
//This function clears the text contained in the initial postcode form box so that a new postcode can be entered in a blank box.
//It is only called the first time as, if it is called every time the box has focus, the user would not be able to re-edit incorrect entries.
	if (thefield.defaultValue == thefield.value)
		{
			thefield.value = "";
		}
}

function isarrayloaded()
{
//isarrayloadedflag looks for the existence of a flag
//This flag function is held in both pcobdata.js and pcfcdata.js.
//If the flag does not exist, the function tries to load the postcode array file again (pcfcdata.js or pcobdata.js)
	if(!self.isarrayloadedflag) {loadarray();}
// if(!document.postcodes) {loadarray();} Non-tested alternative
}

function loadarray()
{
//Appends the postcode data array JavaScript file (pcobdata.js/pcfcdata.js) to the page when called by the isarrayloaded function.
//The array loaded depends upon the pagetype variable hard coded into each page.
	var added = document.createElement("script");
	if(pagetype=="forecast") {added.src = "/weather/javascript/pcfcdata.js";}
	else {added.src = "/weather/javascript/pcobdata.js";}
	added.type="text/javascript";
	document.getElementsByTagName("head")[0].appendChild(added);
}

function locationfind(code)
{
//Test the entered  postcode validity and isolate leading block of postcode
	lbpcode = pcvalidate(code);
        if (lbpcode=="") {change('postcode', 'invalidpc');return;}
//Search postcodes array to locate corresponding location and generate next href
	for (i=0;i<postcodes.length;i++)
	{
		for (j=0;j<postcodes[i].length;j++)
		{
			sublist = postcodes[i][j].split("|")
			for (k=0;k<sublist.length;k++)
			{
				if (lbpcode == sublist[k])
				{
		var baseDir = "/weather/uk/";//Directory containing both forecast and latest pages
        if (pageparam != "wind" && pageparam != "temp" && pageparam != "uv") {pageparam = "weather"}
		document.location.href= baseDir + locations[i][j].split("|")[1] + "_" + pagetype + "_" + pageparam + ".html";
		return;
				}
			}
		}
	}
	alert("Postcode Format OK.\n\nUnable to find matching location.\n\n Please try another postcode.");
	//If this alert ever shows for a known valid postcode, the postcode arrays must be incomplete
}
