
var map;
var localSearch = new GlocalSearch();

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 usePointFromPostcode(postcode, callbackFunction) {
	//alert(postcode);
	
	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);
				callbackFunction(point);
			}else{
				
				document.getElementById('branchLocatorDiv1').innerHTML='<div id="newbodysection" style=""><div style="float:left"><img alt="" src="{$SiteImagePath}/whtietop.jpg" width="951" height="14" align="bottom" /></div><div class="whitearea"><div id="postcode1"><div   class="serchresultbg2branch"><div class="imgpart"></div><div class="cardetailspart"><h3>&nbsp;</h3><h3>&nbsp;</h3><p style="color:red;">No Results Found </p></div></div></div><div style="width:951px; height:14px; float:left"><img alt="" src="{$SiteImagePath}/1stbottom_round.jpg" width="951" height="14" align="top" /></div></div></div>';
				document.getElementById('resdiv').style.display='none';
				alert("Postcode not found!");
			}
		});	
		
	localSearch.execute(postcode + ", UK");
}

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

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

function makeObject()
{
var x;
var browser = navigator.appName;
//alert(browser);
if(browser == "Microsoft Internet Explorer"){
x = new ActiveXObject("Microsoft.XMLHTTP");
}else{
x = new XMLHttpRequest();
}
return x;
}
var request = makeObject();


function showPointLatLng(point)
{

document.getElementById('latitude').value=point.lat();
document.getElementById('longitude').value=point.lng();
document.getElementById('resdiv').style.display='none';
document.getElementById('ajaxImageDiv').style.display='block';		
	request.open('get', 'branchlocatorAjax.php?latitude='+document.getElementById('latitude').value+'&longitude='+document.getElementById('longitude').value);
	request.onreadystatechange = showResult;
	request.send('');
}

function showResult()
{
		if(request.readyState != 4)
	{
	
	}
	
	if(request.readyState == 4)
	{
	document.getElementById('ajaxImageDiv').style.display='none';		
	document.getElementById('branchLocatorDiv1').innerHTML=request.responseText;
	document.getElementById('branchLocatorDiv').style.display='none';
	document.getElementById('branchLocatorDiv1').style.display='block';
	
	}
}

function mapLoad() {

	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("map"));
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(54.622978,-2.592773), 5, 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);




function foc()
{
	if(document.getElementById('vehReg').value=='Enter Registration')
	{
		document.getElementById('vehReg').value='';
		var myElem = document.getElementById('vehReg');
		var myStyle = myElem.style;
		myStyle.textTransform='uppercase';
	}
}
function blu()
{
	if(document.getElementById('vehReg').value=='')
	{
		document.getElementById('vehReg').value='';
		var myElem = document.getElementById('vehReg');
		var myStyle = myElem.style;
		myStyle.textTransform='none';
		document.getElementById('vehReg').value='Enter Registration';	
   }
}
function foc1()
{
	if(document.getElementById('postalcode').value=='Enter Postcode')
	{
		document.getElementById('postalcode').value='';
		var myElem = document.getElementById('postalcode');
		var myStyle = myElem.style;
		myStyle.textTransform='uppercase';
	}
}
function blu1()
{
	if(document.getElementById('postalcode').value=='')
	{
		document.getElementById('postalcode').value='';
		var myElem = document.getElementById('postalcode');
		var myStyle = myElem.style;
		myStyle.textTransform='none';
		document.getElementById('postalcode').value='Enter Postcode';	
   }
}
 function val_first_chk()
 {
 	 if(document.getElementById('vehReg').value=='Enter Registration')
	 {	
	 	alert("Enter Registraion Number");
		 return false; 
	 }
	else if(document.getElementById('vehReg').value=='')
	 {
       alert("Enter Registraion Number");
		 return false; 
	 }
	 else
	 {
	 document.getElementById('newValueMyCarButton').innerHTML="<img src='http://trademymotor.co.uk/images/value_my_car_button.jpg'  alt='Button' />";
	 document.alls.action="get_vrm.php";
	 document.alls.submit();
	 }
 }	
	
function test()
{
return false;
}	
	
	

function tiger1()
{
if(document.getElementById('postalcode').value=='')
{
alert('Please enter postal code');
return false;
}
usePointFromPostcode(document.getElementById('postalcode').value, showPointLatLng);
return false;
}
function tiger2()
{
if(document.getElementById('postalcode').value=='')
{
alert('Please enter postal code');
return false;
}
usePointFromPostcode(document.getElementById('postalcode').value, showPointLatLng);
return false;
}

function redirectToDealer(urlValue,dealer_id)
{
document.getElementById('dealer_id').value=dealer_id;
document.branchLocatorFrm.action='sell-my-car/'+urlValue;
document.branchLocatorFrm.submit();
}
