//jsIncludes.js
//14/08/08 JPROU Created
//09/02/09 JPROU Removed postit function and replaced it with isPostcode()
//20/06/09 RNORM update isPostcode to use the now one from wickes loyalty


//This page will hold all Javascript functions that are used over multiple pages

var map = null;
var geocoder = null;
var directionsPanel;
var directions;

//14/08/08 JPROU Created
//This function will be used to create and activate Google API when the page is loaded. An address string OR latitude and longitute co-ords can be used to search.
function initialize(address) 
{
	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("map_canvas"));		
		map.addControl(new GSmallMapControl());	
		geocoder = new GClientGeocoder();
		geocoder.getLatLng(
			address,
			function(point) 
			{
				if (!point) 
				{ } 
				else 
				{	
					map.setCenter(point, 13);
					var marker = new GMarker(point);
					map.addOverlay(marker);
					//marker.openInfoWindowHtml(address);
				}
			}
		)
	}
}


// ----------------------------------------------------------------------------------------------------------
// Summary 
//   the old name of the function 
//   created for Legacy apps 
// Returns
//   false - if postcode is invalid
//   postcode - if it's valid or has been corrected
// History
// rnorm - 2009-06-29
function postit(strInput)
{
	return isPostcode(strInput)
}


// ----------------------------------------------------------------------------------------------------------
// Summary 
//   Checks if supplied postcode is valid
// Returns
//   false - if postcode is invalid
//   postcode - if it's valid or has been corrected
// History
//   21/01/2009 Alistir WS3 Created 
//   05/02/2009 Alistir WS3 added support for lower case and spaces
//   11/03/2009 Paul Ws3 Added functionality to cope with 5 Digit Postcodes
function isPostcode(strInput)
{ 
	// convert to upper case first
	strInput = strInput.toUpperCase();
	
	// this is the ridiculously complex expression that validates the postcode
	var regex = /(?:(?:A[BL]|B[ABDHLNRST]?|C[ABFHMORTVW]|D[ADEGHLNTY]|E[CHNX]?|F[KY]|G[LUY]?|H[ADGPRSUX]|I[GMPV]|JE|K[ATWY]|L[ADELNSU]?|M[EKL]?|N[EGNPRW]?|O[LX]|P[AEHLOR]|R[GHM]|S[AEGKLMNOPRSTWY]?|T[ADFNQRSW]|UB|W[ACDFNRSV]?|YO|ZE)\d(?:\d|[A-Z])? \d[A-Z]{2})/;
	// return if successful match
	if (regex.test(strInput) == true) return true;

	// try adding a space
	if (strInput.length == 5) 
	{
		if (regex.test(strInput.substr(0,2) + " " + strInput.substr(2,3)) == true) 
		{
			strInput = strInput.substr(0,2) + " " + strInput.substr(2,3);
			return true;
		}
		if (regex.test(strInput.substr(0,3) + " " + strInput.substr(3,2)) == true) 
		{
			strInput = strInput.substr(0,3) + " " + strInput.substr(3,2);
			return true;
		}
	}
	
	if (strInput.length == 6) 
	{
		if (regex.test(strInput.substr(0,3) + " " + strInput.substr(3,3)) == true) 
		{
			strInput = strInput.substr(0,3) + " " + strInput.substr(3,3);
			return true;
		}
	}
	
	if (strInput.length == 7) 
	{
		if (regex.test(strInput.substr(0,4) + " " + strInput.substr(4,3)) == true) 
		{
			strInput = strInput.substr(0,4) + " " + strInput.substr(4,3);
			return true;
		}
	}	

	// still here - no match has been possible
	document.getElementById("invPostcode").style.display = 'block';
	return false;
} 


// ----------------------------------------------------------------------------------------------------------
// Summary 
//function to remove the default 'Your Postcode...' value from the message box
// Returns
//   either removes the text or leave is alone if not the dafault text
// History
//   25/6/2009 RNORM 
function postcode_onClick(strInput)
{ 
	if (document.getElementById(strInput).value == 'Your Postcode...')
	{
		document.getElementById(strInput).value = '';
	}
}