	
		document.onkeypress = userenter;
		if (document.captureEvents) document.captureEvents(Event.KEYPRESS);
		
		function ajax_SearchLocations()
		{
			map.clearOverlays();
		    directions.clear();
		    loadingPanel.style.display='';
			var state = 'oz';
			var q = document.getElementById('searchquery').value;
			var script = "/ajax/locations.php?action=search&q="+q;
			var ajax = new Ajax.Request(
					script,
					{method: 'get', 
					 onSuccess: function(response)
					 {
					 	OutputLocations(state,response.responseText,true,q);
					 },
					 onFailure: function()
					 {		 	
					 }
					}
			);
			return false;
		}
		
		function ajax_SearchById(id)
		{
			map.clearOverlays();		   
			var state = 'oz';
			var script = "/ajax/locations.php?action=searchById&q="+id;
			var ajax = new Ajax.Request(
					script,
					{method: 'get', 
					 onSuccess: function(response)
					 {
					 	OutputOne(response.responseText);
					 },
					 onFailure: function()
					 {		 	
					 }
					}
			);
			return false;
		}
		
		function OutputOne(locationstext)
		{
			var response = eval("(" + locationstext + ")");
			// generate HTML listing the locations and update the page DOM so the user will see the HTML
			var j = response.locations.location.length + 0;
			
			if (j>=1)
			{								
				for(i=0;i < j; i++) {
       				var id = response.locations.location[i].id;
					var city = response.locations.location[i].suburb;
					var name = response.locations.location[i].name;
					var address = response.locations.location[i].searchaddress;
					var lat = response.locations.location[i].lat;
					var lng = response.locations.location[i].lng;
					var description = response.locations.location[i].description;
					var openinghours = response.locations.location[i].openinghours;					
					var phone = response.locations.location[i].phone;					
					var isnew = response.locations.location[i].isnew;
					var iscoming = response.locations.location[i].iscoming;
					var distance = response.locations.location[i].distance;
					var fulldescription	='<div style="font-size:11px; font-family: verdana"><img src="http://www.rejectshop.com.au/assets/images/trs_logo_googlemaps.gif" border="0"/><br/>'+'<br/><strong>'+name+'</strong><br/>'+description+'<br/><strong>Phone:</strong> '+phone+'<br/><strong>Opening Hours:</strong><br/>'+openinghours;
					fulldescription	+='<br/><br/><strong>Driving Directions</strong><br/> from: <input onkeypress="enterdirections(\''+lat+','+lng+'\', this.value);" style="font-size:11px; font-family: verdana;width:200px;" type="text" id="from'+id+'"> <img onclick="getDirections(\''+lat+','+lng+'\', document.getElementById(\'from'+id+'\').value);" alt="Get Driving Directions" style="vertical-align:bottom;" src="/assets/images/go_btn_white.gif"/></div>';
					var anchor = '<a href="#" onclick="javascript:markLocation(\''+id+'\',\''+lat+'\',\''+lng+'\',\''+name+'\',\''+description+'\',\''+openinghours+'\',\''+phone+'\'); return false;">';
					
				    point = new GLatLng(lat,lng);
				    
		            customIcon = new GIcon(G_DEFAULT_ICON);
		            customIcon.image = "/assets/images/trs_sml_marker.png";
		            
		            if (isnew=="1")
		            {
		  				customIcon.image = "/assets/images/trs_sml_marker_new.gif";
		            }
		            if (iscoming=="1")
		            {
		  				customIcon.image = "/assets/images/trs_sml_marker_coming.gif";
		            }
		            // Set up our GMarkerOptions object							
			        var markerOptions = {clickable:true, icon:customIcon, draggable:true, title:city };
			        var markers = new GMarker(point, markerOptions);			        
			        markers.bindInfoWindowHtml(fulldescription);
			        GEvent.addListener(markers, "dragend", function() {
			        	var thispoint= markers.getPoint();
			        	var thislat = thispoint.lat();
			        	var thislng = thispoint.lng();
			        	document.getElementById("lat").value=thislat;
    				    document.getElementById("lng").value=thislng;
					});
								        
					map.setCenter(point,15);
					map.addOverlay(markers);	            
				 		
				}
			}
		}
		
		function state_ajax(state,script) {
			var ajax = new Ajax.Request(
					script,
					{method: 'get', 
					 onSuccess: function(response)
					 {
					 	OutputLocations(state,response.responseText,true,'');
					 },
					 onFailure: function()
					 {					 	
					 }
					}
			);
			return false;
		}
			
		
		function stated(state) {
			map.clearOverlays();
		    directions.clear();
			loadingPanel.style.display='';
			if (GBrowserIsCompatible()) {
        		state_ajax(state,"/ajax/locations.php?action=fetch&s=byState&q="+state);
      		}
      	
      		return false;
		}
		
		function australia() {
			backPanel = document.getElementById('BackPanel');
			locationsPanel = document.getElementById('LocaleUpdate');
			loadingPanel = document.getElementById('Loading');
			statePanel = document.getElementById('StateList');		
			directionsPanel = document.getElementById("drivingdirections");
				
			loadingPanel.style.display='';
			document.getElementById("RejectMap").innerHTML = "<img src='/assets/images/map_loading.gif' width='620px' height='460px'>";
			if (GBrowserIsCompatible()) {
        		map = new GMap2(document.getElementById("RejectMap"));
	        	map.addControl(new GLargeMapControl());
				map.addControl(new GMapTypeControl());
				map.addControl(new GScaleControl());
				map.enableDoubleClickZoom();
				directions = new GDirections(map, directionsPanel);
				OutputLocations('oz','',false,'');      		
			}
			loadingPanel.style.display='none';      	
      		return false;
		}
		
		function markLocation(id,lat,lng,name,description,openinghours,phone) {
			directions.clear();
			var fulldescription	='<div style="font-size:11px; font-family: verdana"><img src="http://www.rejectshop.com.au/assets/images/trs_logo_googlemaps.gif" border="0"/><br/>'+'<br/><strong>'+name+'</strong><br/>'+description+'<br/><strong>Phone:</strong> '+phone+'<br/><strong>Opening Hours:</strong><br/>'+openinghours;
			fulldescription	+='<br/><br/><strong>Driving Directions</strong><br/> from: <input onkeypress="enterdirections(\''+lat+','+lng+'\', this.value);" style="font-size:11px; font-family: verdana;width:200px;" type="text" id="from'+id+'"> <img onclick="getDirections(\''+lat+','+lng+'\', document.getElementById(\'from'+id+'\').value);" alt="Get Driving Directions" style="vertical-align:bottom;" src="/assets/images/go_btn_white.gif"/></div>';
			point = new GLatLng(lat,lng);
			map.setCenter(point, 15);
        	markers[id].openInfoWindowHtml(fulldescription);
			return false;
		}
		
		function getDirections(to, from) {
		  directions.clear();
		  loadingPanel.style.display='';
		  if (from=="" || to=="") return;
		  var geocoder=new GClientGeocoder();
		  from = from + ", australia";
		  geocoder.getLatLng(
	        from,
	        function(point) {
	          if (!point) {
	          	statePanel.style.display='none';
				backPanel.style.display='';
				locationsPanel.style.display='none';
				directionsPanel.style.display='';
				loadingPanel.style.display='none';
	            directionsPanel.innerHTML="<span style=''><strong>Sorry. Your address "+from + " could not be found! Please try again with your closest main street. </strong></span>";
	          } else {
	         	var fromlat=point.lat();
	         	var fromlng=point.lng();
	         	var query=fromlat + "," +fromlng + " to " + to ;
	         	directionsPanel.innerHTML='<a target="_newwindow" href="http://maps.google.com.au/maps?f=d&hl=en&geocode=&time=&date=&ttype=&q='+query+'&ie=UTF8&z=13&om=1&pw=2">click here for Print View</a>';
	         	directions.load(query);
	         	map.clearOverlays();
		  		statePanel.style.display='none';
				backPanel.style.display='';
				locationsPanel.style.display='none';
				directionsPanel.style.display='';
				loadingPanel.style.display='none';
		      }
	        }
	      );
		  return false;
		}
		
		function enterdirections(from,to) 
		{			
			if (window.event) userenter();
			if (lastkey == 13) {
				getDirections(from,to);
			}
		}
		
		function entersearchbox() 
		{	
			if (window.event) userenter();
			if (lastkey == 13) {
				ajax_SearchLocations();
			}
		}
		
		function userenter(e) {
			if (!e) var e = window.event;
			if (e.keyCode) lastkey = e.keyCode;
			else if (e.which) lastkey = e.which;
		}
		
		
		function back() {
			statePanel.style.display='';
			locationsPanel.style.display='none';
			directionsPanel.style.display='none';
			backPanel.style.display='none';
			return false;
		}
		
		function OutputLocations(state, locationstext, searching, query) {
			backPanel = document.getElementById('BackPanel');
			locationsPanel = document.getElementById('LocaleUpdate');
			loadingPanel = document.getElementById('Loading');
			statePanel = document.getElementById('StateList');		
			directionsPanel = document.getElementById("drivingdirections");
			
			var location = locations[state];
    		var locdata = location.split(',');    		
			
			if (state=="oz") 
			{
				map.setCenter(new GLatLng(locdata[0],locdata[1]), 4);
				if (!searching) return false;
			}
			else if (state=="act" )
			{
				map.setCenter(new GLatLng(locdata[0],locdata[1]),9);
			}
			else if (state=="tas" )
			{
				map.setCenter(new GLatLng(locdata[0],locdata[1]), 7);
			}
        	else
        	{
        		 map.setCenter(new GLatLng(locdata[0],locdata[1]), 5);
        	}
        	
        	
			var response = eval("(" + locationstext + ")");
			// generate HTML listing the locations and update the page DOM so the user will see the HTML
			var j = response.locations.location.length + 0;
			
			if (j>=1)
			{	
				markers = [];				
				locationsHTML = "<ul><li><strong> " + response.locations.location.length + " results.</strong></li>";
       			for(i=0;i < j; i++) {
       				var id = response.locations.location[i].id;
					var city = response.locations.location[i].suburb;
					var name = response.locations.location[i].name;
					var address = response.locations.location[i].searchaddress;
					var lat = response.locations.location[i].lat;
					var lng = response.locations.location[i].lng;
					var description = response.locations.location[i].description;
					var openinghours = response.locations.location[i].openinghours;					
					var phone = response.locations.location[i].phone;					
					var isnew = response.locations.location[i].isnew;
					var iscoming = response.locations.location[i].iscoming;
					var distance = response.locations.location[i].distance;
					var fulldescription	='<div style="font-size:11px; font-family: verdana"><img src="http://www.rejectshop.com.au/assets/images/trs_logo_googlemaps.gif" border="0"/><br/>'+'<br/><strong>'+name+'</strong><br/>'+description+'<br/><strong>Phone:</strong> '+phone+'<br/><strong>Opening Hours:</strong><br/>'+openinghours;
					fulldescription	+='<br/><br/><strong>Driving Directions</strong><br/> from: <input onkeypress="enterdirections(\''+lat+','+lng+'\', this.value);" style="font-size:11px; font-family: verdana;width:200px;" type="text" id="from'+id+'"> <img onclick="getDirections(\''+lat+','+lng+'\', document.getElementById(\'from'+id+'\').value);" alt="Get Driving Directions" style="vertical-align:bottom;" src="/assets/images/go_btn_white.gif"/></div>';
					var anchor = '<a href="#" onclick="javascript:markLocation(\''+id+'\',\''+lat+'\',\''+lng+'\',\''+name+'\',\''+description+'\',\''+openinghours+'\',\''+phone+'\'); return false;">';
					locationsHTML += "<li>"+ anchor + "<strong>" + name + "</strong></a>";
					if (distance)
					{
						locationsHTML += " ( " + distance + " km )";
       				}
					locationsHTML += "<br/>" + description + "<br/>Ph: " + phone + "</a></li>";
					
				    point = new GLatLng(lat,lng);
				    
		            customIcon = new GIcon(G_DEFAULT_ICON);
		          	customIcon.image = "/assets/images/trs_sml_marker.png";
		            if (isnew=="1")
		            {
		  				customIcon.image = "/assets/images/trs_sml_marker_new.gif";
		            }
		            if (iscoming=="1")
		            {
		  				customIcon.image = "/assets/images/trs_sml_marker_coming.gif";
		            }
		           
		           // Set up our GMarkerOptions object							
			        markerOptions = {clickable:true, icon:customIcon, title:city };
			        markers[id] = new GMarker(point, markerOptions);			        
			        markers[id].bindInfoWindowHtml(fulldescription);
			        GEvent.addListener(markers[id], "infowindowopen", function(marker) {
			        	var thispoint= marker.getLatLng();
			        	var thislat = thispoint.lat()+0.06;
			        	var thislng = thispoint.lng();
			        	var newpoint = new GLatLng(thislat,thislng);
					    map.setCenter(newpoint,11);
					  });
		            map.addOverlay(markers[id]);	            
				 		
				}
				
				locationsHTML += "</ul>";  
				locationsPanel.innerHTML = locationsHTML; 
				statePanel.style.display='none';
				locationsPanel.style.display='';
				directionsPanel.style.display='none';
				backPanel.style.display='';
				loadingPanel.style.display='none';
				if (query!='')
	        	{
	        		  var zoom = response.zoom;
	        		  zoomin = 7;
	        		  if (zoom) zoomin = zoom;
	        		  zoomin=zoomin - 0;
					  var geocoder=new GClientGeocoder();
					  geocoder.getLatLng(
				        query,
				        zoomto
				      );
	        	}
				
			}
			else if (searching)
			{				
				locationsPanel.innerHTML = "<ul><li><strong>Sorry, there are currently no Reject Shops in this area.</strong></li></ul>";
				statePanel.style.display='none';
				locationsPanel.style.display='';
				directionsPanel.style.display='none';
				backPanel.style.display='';
				loadingPanel.style.display='none';
			} 
			directionsPanel.style.display='none';
			return true;
			
		}		
		
		function zoomto(point) {
          if (!point) {	          	
          } else {
         	var lat=point.lat();
         	var lng=point.lng();
         	map.setCenter(new GLatLng(lat,lng),zoomin);	         	
	      }
        }
