
var lastLocation = "";
var lastDesc = "";
var lastPic = "";
var lastAgent = "";
var lastAgentLogoSrc = "";
var lastAgentReq = "";
var lastPcode = "";

	function domsearch() {

	
		var latlng =  marker.getLatLng();
		
		ajaxpm_getPropertiesByGrid(latlng.lat(), latlng.lng(),
			document.sform.forsale.value ,
			document.sform.beds.value ,
			document.sform.minprice.value ,
			document.sform.maxprice.value ,
			document.sform.sortby.value 	
		);
		
		criteria = "";
		
		if(document.sform.beds.value>-1) 
			criteria = criteria+  document.sform.beds.value + " Beds plus"; 	
		
		if(document.sform.forsale.value==1) 
			criteria = criteria+  " To Let";
		else if(document.sform.forsale.value==0) 
		 	criteria = criteria+  " For Sale";
		 	
		if(document.sform.maxprice.value>-1) 
			criteria = criteria+  ", maximum price " + document.sform.maxprice.value; 	
		 	
 		// marker.openInfoWindowHtml( "Searching  " + criteria + " in this area ..." );
 		 updateLayer('results', ''); 
 		 
 		 window.document.images['prefresh_image'].src = "images/refresh2.gif";
 
	}


	function findbypcode(pcode, locationid, forsale) {
		window.document.images['prefresh_image'].src = "images/refresh2.gif";
		var loc = "";
		if(locationid>-1) {
			//alert('by location ' + locationid);
			loc = "&loc="+locationid;
		}
		if(forsale>-1) 
			sal = "&forsale="+forsale;
		else
			sal = "";
		url = "ajax/ajax.php?type=getpropsbypcode&pcode="+pcode+loc+sal;
//alert(url);

		if (window.XMLHttpRequest) {
        reqp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        reqp = new ActiveXObject("Microsoft.XMLHTTP");
    }
	
	//alert(url);
    reqp.open("GET", url, true);  
    reqp.onreadystatechange = ajax_getPropertiesByGrid_callback;
    reqp.send(null);
	}

	function ajaxpm_getPropertiesByGrid(lat,lng,forsale,beds,minprice,maxprice,sortby) {

		url = "ajax/ajax.php?type=getpropsbygrid&lat="+lat+"&lng="+lng+"&forsale="+forsale+"&beds="+beds+"&minprice="+minprice+"&maxprice="+maxprice+"&sortby="+sortby;
	//alert(url);
		if (window.XMLHttpRequest) {
        reqp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        reqp = new ActiveXObject("Microsoft.XMLHTTP");
    }
	
	//alert(url);
    reqp.open("GET", url, true);  
    reqp.onreadystatechange = ajax_getPropertiesByGrid_callback;
    reqp.send(null);
	}

	function ajax_getPropertiesByGrid_callback() {
//alert('create came back');
	if (reqp.readyState == 4) {
       		if (reqp.status == 200) {
			//alert(  reqp.responseText ); 
			
			elements = reqp.responseXML.getElementsByTagName("props");
			//alert(elements.item(0).firstChild.nodeValue);
			savedprops = elements.item(0).firstChild.nodeValue;

			//elements = reqp.responseXML.getElementsByTagName("organisation");
			pr = elements.item(0); 
			
			var lat  = pr.getAttribute("lat");
			var lng  = pr.getAttribute("lng");
			
			if(lat) {
				var point = new GLatLng(lat,lng)
   				//var marker = new GMarker(point);
   				//map.addOverlay( marker );
				//map.setCenter(point,13);
				//map.panTo(point);
				//map.zoomIn();map.zoomIn();map.zoomIn();
				//map.panBy(50);
   				//map.setCenter(point,13);
			}
			
			
			updateLayer('results', savedprops); 
			window.document.images['prefresh_image'].src = "images/refresh.gif";
		}
	}
} 

	function fadeout() {
		Effect.Fade('viewproperty');
	}
	function showpropertyframe(src){
		//alert(src);
		document.getElementById('viewproperty').src  = src;
	}

	function  showprop(id,pcode,locationtext,pic,desc,agentname,agentlogosrc,$reqform,lat,lng) {

		alert('show prop');

		window.document.images['prefresh_image'].src = "images/refresh2.gif";

		lastLocation 	= locationtext;
		lastpic 	= pic;
		lastDesc	= desc;
		lastAgent	= agentname;
		lastAgentLogoSrc = agentlogosrc;
		lastAgentReq	= $reqform;

		
		if(lat>0 && lng<0) { // caching 
			//alert('cached values ' + lat +' ' +lng );
			gmap_map_postcode(lat,lng );
		}
		else {
			gmap_geocode_postcode(pcode);
		}
		
		gmap_geocode_postcode(pcode);

		
		
		
		
	}


   function gmap_geocode_postcode(postcode){
	lastPcode = postcode;
	//alert('get '+postcode);
    var s = document.createElement('script');
    s.src = 'http://geo.localsearchmaps.com/?cb=gmap_map_postcode&use=google2&country=uk&zip=' + postcode;
    s.type = 'text/javascript';
	
    document.getElementsByTagName('head')[0].appendChild(s);
} 

function ajax_updatepcode_cache(pcode, lat, lon)  {
	
	var url = "ajax/ajax.php?type=updatepostcode&lat="+lat+"&lng="+lon+"&pcode="+pcode;
//alert(url);
		if (window.XMLHttpRequest) {
        reqpu = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        reqpu = new ActiveXObject("Microsoft.XMLHTTP");
    }
	
	//alert(url);
    reqpu.open("GET", url, true);  
    //reqpu.onreadystatechange = ajax_debug;
    reqpu.send(null);
	
}

function ajax_debug() {

	if (reqpu.readyState == 4) {
       		if (reqpu.status == 200) {
			alert(  reqpu.responseText ); 
			
		}
	}
} 

function gmap_map_postcode(lon, lat){
	// yep there are the right way round!
	ajax_updatepcode_cache(lastPcode, lon, lat );
	
    	
	var dis = "<table height=\"200\"><tr><td valign=top><img src='"+lastpic+"'></td><td valign=top><font size=1>"+lastDesc+"</td></tr></table>";
	var dis2= "<table height=\"200\"><tr><td valign=top><img src='"+lastAgentLogoSrc+"'></td><td valign=top><font size=1>"+lastAgent+lastAgentReq+"</td></tr></table>";

	

	var infoTabs = [
		new GInfoWindowTab(lastLocation, dis),
  		new GInfoWindowTab('Agent Info', dis2)
	];

	var point = new GLatLng(lon, lat);
	map.setCenter(point,13);
	var marker = new GMarker(point);

	GEvent.addListener(marker, "click", function() {
  		marker.openInfoWindowTabsHtml(infoTabs);
	});

	map.addOverlay( marker );
   	
	marker.openInfoWindowTabsHtml(infoTabs);

	//map.openInfoWindow(map.getCenter(),
                   //document.createTextNode(dis));
	window.document.images['prefresh_image'].src = "images/refresh.gif";
}

    var map = null;
    var geocoder = null;
	var marker = null;
	
    function load() {
	
	
	 // Create a base icon for all of our markers that specifies the
        // shadow, icon dimensions, etc.
        var baseIcon = new GIcon();
        baseIcon.image  = "http://www.propertymail.co.uk/images/blue-pushpin.png";
        baseIcon.shadow = "http://www.propertymail.co.uk/images/pushpin_shadow.png";
        baseIcon.iconSize = new GSize(32, 32);
     	 baseIcon.shadowSize=new GSize(52,32);
  		 baseIcon.iconAnchor=new GPoint(14,32);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);
        baseIcon.infoShadowAnchor = new GPoint(18, 25);
	
      if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));

	var center = new GLatLng(51.479, -3.184); // millen stad

	map.setCenter(center, 9);
     
	map.addControl(new GSmallMapControl());
	
	
	//var marker = new GMarker(center, {draggable: true});
	
   	//var marker = new GMarker(center);
   	//map.addOverlay( marker );
   	map.setCenter(center);

	//markerOptions = { icon: baseIcon };

	marker = new GMarker(center, {icon: baseIcon , draggable: true});

GEvent.addListener(marker, "dragstart", function() {
  map.closeInfoWindow();
  });
	/*
	GEvent.addListener(marker, "dragend", function() {
		var latlng =  marker.getLatLng();
		
		ajaxpm_getPropertiesByGrid(latlng.lat(), latlng.lng(),
			document.sform.forsale.value ,
			document.sform.beds.value ,
			document.sform.minprice.value ,
			document.sform.maxprice.value ,
			document.sform.sortby.value 	
		);
		
		criteria = "";
		
		if(document.sform.beds.value>-1) 
			criteria = criteria+  document.sform.beds.value + " Beds plus"; 	
		
		if(document.sform.forsale.value==1) 
			criteria = criteria+  " To Let";
		else if(document.sform.forsale.value==0) 
		 	criteria = criteria+  " For Sale";
		 	
		if(document.sform.maxprice.value>-1) 
			criteria = criteria+  ", maximum price " + document.sform.maxprice.value; 	
		 	
 		 marker.openInfoWindowHtml( "Searching  " + criteria + " in this area ..." );
 		 updateLayer('results', ''); 
 		 
 		 window.document.images['prefresh_image'].src = "images/refresh2.gif";
 		 
  });
	*/
	
map.addOverlay(marker);



	

	}
	
    }



