    var geocoder;
    var map;
    var oldbgcolor;
    var clickurl = null;
    var currenturl = null;
    var IsIE = (navigator.userAgent.indexOf('MSIE') !=-1);
    
 
    if(IsIE==false){ 
    window.onbeforeunload = function() {
        if(clickurl){
            return clickurl + " tries to take away the current page, click cancel to stay";
        }  
    };
    }

    function showLocationInput(show)
    {
  	  if(show)
  	  {
  		  document.getElementById('option_location').checked = true;
  	      document.getElementById('input').size = 35;		  
  	      document.getElementById('forLocation').className = "inline";
  	  }else
  	  {
  		  document.getElementById('option_web').checked = true;
  	      document.getElementById('forLocation').className = "hidden";
  	      document.getElementById('input').size = 55;
      	  document.getElementById('location').value = "";  	            	  
  	  }
    }  
    
    function iframeLoad() {
    	currenturl = clickurl;
    	clickurl = null;    	
    }
    
    function PreViewResult(url) {
         var iframe = document.getElementById('divFrame');
       	 if(IsIE){
    		 iframe.src = url;
    		 clickurl = url;
    	 }else{
    		 iframe.src = "preview.html";
    	 }
    }

    function openurl(link) {
    	link.href = currenturl;
    }
    
    
    function gup( name )
    {
      name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
      var regexS = "[\\#&]"+name+"=([^&]*)";
      var regex = new RegExp( regexS );
      var results = regex.exec( document.location.hash );
      if( results == null )
        return "";
      else
        return decodeURIComponent(results[1]);
    }
    
    function ReadParameters() {
		document.getElementById('input').value = gup('input');
		document.getElementById('location').value = gup('location');
		showLocationInput(gup('option') == 'Location');
    }    

    function load() {
      if (GBrowserIsCompatible()) {
        geocoder = new GClientGeocoder();
      }
      ReadParameters();
      onPreSubmit();
    }

    function createCustomizedMarker(x, y, image, width, height, info) {
        var icon = new GIcon(); 

        icon.image = image;
        icon.iconSize = new GSize(width, height);
        icon.iconAnchor = new GPoint(11, 22);
        icon.infoWindowAnchor = new GPoint(5, 2);

        var marker = new GMarker(new GLatLng(x,y),icon);
        if(info)
        {          
	        GEvent.addListener(marker, 'click', function() {
	            marker.openInfoWindowHtml(info);
	          });
        }
        map.addOverlay(marker); 
        return marker;
      }

    
    function createMarker(x, y, info) {
        var marker = new GMarker(new GLatLng(x,y));
        GEvent.addListener(marker, 'click', function() {
          marker.openInfoWindowHtml(info);
        });
        map.addOverlay(marker);   
        return marker;
      }

    function setupMarker(n, marker) {

    	  var div = document.getElementById(n);
    	  div.style.cursor = 'pointer';
    	  GEvent.addDomListener(div, 'click', function() {
    	    GEvent.trigger(marker, 'click');
    	  });
    	  GEvent.addDomListener(div, 'mouseover', function() {
    		oldbgcolor = div.style.backgroundColor; 
    	    div.style.backgroundColor = '#C3D9FF';
    	  });
    	  GEvent.addDomListener(div, 'mouseout', function() {
      	    div.style.backgroundColor = oldbgcolor;
    	  });
    	  return div;
    }

    function sendRequest(url,callback) {
    	var request = new XMLHttpRequest; 
    	request.open("GET",url, false); 
    	request.onreadystatechange = function() 
    	{ 
        	if (this.readyState == XMLHttpRequest.DONE) 
            { 
    			callback(request.responseXML);
            }
    	}
    	request.send(null);
    }

    function search(question){
		   var searchUrl = 'handlesearch.php?question=' + encodeURIComponent(question);
		   sendRequest(searchUrl, function(xml) {			   
			   ChangeLayout(false);		   
			   result=xml.getElementsByTagName("Result")[0].childNodes;				   
			   for (var i=0;i<result.length;i++)
			   { 
				 if(result[i].tagName=='JavaScript')
				 {
					 eval(result[i].childNodes[0].nodeValue);
				 }else
				 {
					 document.getElementById(result[i].tagName).innerHTML = result[i].childNodes[0].nodeValue;
				 }
			    }
		   });        
 	}
    
 	
	   function searchWithLocation(question,address) {
	     geocoder.getLatLng(address, function(latlng) {
		       var latitude = latlng.lat();
		       var longitude = latlng.lng();
			   var searchUrl = 'handlesearch.php?lat=' + latitude + '&lng=' + longitude + '&question=' + encodeURIComponent(question);
			   sendRequest(searchUrl, function(xml) {
				   ChangeLayout(true);
				   result=xml.getElementsByTagName("Result")[0].childNodes;		
				   for (var i=0;i<result.length;i++)
				   { 
					 if(result[i].tagName=='JavaScript')
					 {
						 eval(result[i].childNodes[0].nodeValue);
					 }else
					 {
						 document.getElementById(result[i].tagName).innerHTML = result[i].childNodes[0].nodeValue;
					 }
				    }
	     });
	   });
	   }
        function Resize()
        {
            var content = document.getElementById('divContent');
            content.style.width=document.body.clientWidth/2+"px";
			content.style.height=document.body.clientHeight-findPosY(content)+"px";            
        }
         	     		
	    function ChangeLayout(location)
		{
            var map = document.getElementById('divMap');
            var frame = document.getElementById('divFrame');
            var button = document.getElementById('divButton');
            var content = document.getElementById('divContent');
			
			if(location)
			{
            	frame.className = "hidden";
            	button.className = "hidden";
            	map.className = "visible";
            	map.style.width=document.body.clientWidth/2+"px";
				map.style.height=document.body.clientHeight-findPosY(map)+"px";			
			}else
			{				
            	map.className = "hidden";
            	frame.className = "visible";
            	button.className = "visible";
            	frame.style.width=document.body.clientWidth/2+"px";    		            	
            	frame.style.height=document.body.clientHeight-findPosY(frame)+"px";            	
			}			
            content.style.width=document.body.clientWidth/2+"px";            
			content.style.height=document.body.clientHeight-findPosY(content)+"px";
		}
	    
	    function get_radio_value(radio)
	    {
		    for (var i=0; i < radio.length; i++)
		    {
			       if (radio[i].checked)
			    	   return radio[i].value;
			}
		    return null;
	    }
	    
   	    function onPreSubmit()
		{  	    	
   			var input = document.getElementById('input').value;
   			var location = document.getElementById('location').value;
   			var option = get_radio_value(document.getElementsByName('option'));
   			  			
   	    	if(input=="")
			{
   				document.getElementById('input').focus();
   				return false;
			}    	    	
  	   	    	
			if(option == "Location")
			{
				geoSearch = true;
				searchWithLocation(input, location);
			}else
			{
				geoSearch = false;
				search(input);
			}
   	    	document.location.hash = '#input='+encodeURIComponent(input)+'&option='+encodeURIComponent(option)+'&location='+encodeURIComponent(location);
			return false;							
  		}
