
var map;
var localSearch 	= new GlocalSearch();



function usePointFromPostcode(postcode, callbackFunction, form_id) {

	localSearch.setSearchCompleteCallback(null, 
		function() {
			
			if (localSearch.results[0])
			{		
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				return callbackFunction(point, form_id);
			}else{
				alert("Sorry we couldn't find a co ordindate for your area, please try using your UK postcode.");
			}
		});	
	// return true to submit form
	localSearch.execute(postcode + ", UK");
 	// have to return false here and use submit() function in callback 
	return false;
	
}

// dyp assign results forms hidden vars, submits form on success
// or returns alert
function assign_to_fields (point, form_id)
{
	// assign to prod search in header
	if (form_id == 'prod-search')
	{
		document.getElementById('p_lat').value 	= point.lat();
		document.getElementById('p_long').value	= point.lng();
	}
	// all other forms
	else
	{
		document.getElementById('lat').value 	= point.lat();
		document.getElementById('long').value	= point.lng();
	}
	
	if(check_fields (form_id))
	{
		document.getElementById(form_id).submit();	
	}
	else
	{
		alert("Sorry we couldn't find a co ordindate for your area, please try using your UK postcode.");
	}
}

// checks we have the values we want
function check_fields (form_id)
{
	// assign to prod search in header
	if (form_id == 'prod-search')
	{
		if(document.getElementById('p_lat').value == "" || document.getElementById('p_long').value	== "")
		{
			return false;
		}
		return true;		
	}
	// all other forms
	else
	{
		if(document.getElementById('lat').value == "" || document.getElementById('long').value	== "")
		{
			return false;
		}
		return true;
	}
}

/*

var icon 			= new GIcon();
icon.image 			= "http://www.google.com/mapfiles/marker.png";
icon.shadow 		= "http://www.google.com/mapfiles/shadow50.png";
icon.iconSize 		= new GSize(20, 34);
icon.shadowSize 	= new GSize(37, 34);
icon.iconAnchor 	= new GPoint(10, 34);

function placeMarkerAtPoint(point)
{
	var marker = new GMarker(point,icon);
	map.addOverlay(marker);
}

function setCenterToPoint(point)
{
	map.setCenter(point, 17);
}

function showPointLatLng(point)
{
	alert("Latitude: " + point.lat() + "\nLongitude: " + point.lng());
}

function mapLoad() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("google_map"));
		
		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		//map.setCenter(new GLatLng(54.622978,-2.592773), 12, G_HYBRID_MAP);
	}
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addUnLoadEvent(func) {
	var oldonunload = window.onunload;
	if (typeof window.onunload != 'function') {
	  window.onunload = func;
	} else {
	  window.onunload = function() {
	    oldonunload();
	    func();
	  }
	}
}

*/



//addLoadEvent(mapLoad);
//addUnLoadEvent(GUnload);

