//Global variable for the method called
var method = "";
	
//Create a new php serializer variable
var php = new PHP_Serializer();
	
//Display the serialized results
JSserializerOnLoad = function(result){
	switch (method)
	{
		case "searchFormattedAddress":
			if ((result instanceof Array) == false)
			{
				alert("It appears that your licence key is invalid");
			}
			else
			{
				var searchResultArea = document.getElementById("searchResultArea");
				searchResultArea.innerHTML = "";
				
				// Display error message if address not found
				if ((result.length == 0) || (result[0]['EntityName'] == ""))
				{
					//searchResultArea.innerHTML = "<center style=\"color: red\">Place Not Found</center>";
					alert ("Place Not Found");
					return;
				}

				searchResultArea.style.visibility='visible';
				searchResultArea.style.width="500px";
				searchResultArea.style.height="300px";


				var sMessage = "";
				//Show all the arrays
				for (i=0; i<result.length; ++i)
				{
					// Exit if empty result as remainder will also be empty
					if(result[i]['EntityName'] == "")
						return false;
						
					//sMessage += 'Entity Name: ' + result[i]['EntityName'] + '\n<br>';
					//sMessage += 'x: ' + result[i]['x'] + '\n<br>';
					//sMessage += 'y: ' + result[i]['y'] + '\n<br>';
															
					// display search results
					//searchResultArea.innerHTML += "<div class=\'searchResult\'>" + sMessage + "</div>";
					sMessage = "";



					if (GBrowserIsCompatible()) {
						var map = new GMap2(document.getElementById("searchResultArea"));
						map.addControl(new GSmallMapControl());
						map.addControl(new GMapTypeControl());
						map.setCenter(new GLatLng(result[i]['y'],result[i]['x']), 13);
						map.openInfoWindow(map.getCenter(),document.createTextNode(result[i]['EntityName']));
						//window.location.href = "#map";
					}



				}
			}
			break;
	}
		
}

//Test function to search for an array of formatted addresses
function test_searchFormattedAddresses(postcode){
			
       //Set the method variable
       method = "searchFormattedAddress";
	
       //The addresses to search on
       address = new atAddress(	"","","",postcode,"");
		
       //Array
       var addresses = new Array(address);
       //var licensekey = 'js7795-9867-9070';		// License key with 'js' in the front.  e.g. js0000-0000-0000"
       var licensekey = 'ws1726-8974-6400';		// License key with 'js' in the front.  e.g. js0000-0000-0000"
       var recordcount = 10;
       var threshold = 0.85;
       var coordsys = 1;
		
       //Call the web service & get the results
       searchFormattedAddress(address, licensekey, recordcount, threshold, coordsys);
}

