// JavaScript Document

function suggest(inputString){
		if(inputString.length == 0) {
			$('#suggestions').fadeOut();
		} else {
		$('#Location').addClass('load');
		
			$.post("handlers/suggest.php", {queryString: ""+inputString+""}, function(data){
				if(data.length >0) {
					$('#suggestions').fadeIn();
					$('#suggestionsList').html(data);
					$('#Location').removeClass('load');
				}
			});
		}
	}

	function fill(thisValue) {
		//$('#VisibleLocation').val(thisValue);
		$('#Location').val(thisValue);
		$('#Address1').focus();
		//$('#suggestions').fadeOut();
		setTimeout("$('#suggestions').fadeOut();", 150);
		//$(Zip).focus();
		//
		//
		//$('#Location').blur();
		//$(Zip).focus();
	}
	
	
	
	    function initialize(addresses) {
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(37.4419, -122.1419), 13);
        map.addControl(new GSmallMapControl());
        map.addControl(new GMapTypeControl());
    
        // Create a base icon for all of our markers that specifies the
        // shadow, icon dimensions, etc.
        var baseIcon = new GIcon(G_DEFAULT_ICON);
        baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        baseIcon.iconSize = new GSize(20, 34);
        baseIcon.shadowSize = new GSize(37, 34);
        baseIcon.iconAnchor = new GPoint(9, 34);
        baseIcon.infoWindowAnchor = new GPoint(9, 2);
    geocoder = new GClientGeocoder();
        // Creates a marker whose info window displays the letter corresponding
        // to the given index.
        function createMarker(point, index, caption) {
          // Create a lettered icon for this point using our icon class
          var letter = String.fromCharCode("A".charCodeAt(0) + index);
          var letteredIcon = new GIcon(baseIcon);
          letteredIcon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
    
          // Set up our GMarkerOptions object
          markerOptions = { icon:letteredIcon };
          var marker = new GMarker(point, markerOptions);
    
          GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml("<b>" + caption + "</b>" );
          });
          return marker;
        }
    function showManyAddresses(addresses) {

		
		//parse and store into array individually each is separated by *		
	var address=addresses.split("*");
	
		for (var i = 0; i < 10; i++) {
			b=i+1;
		showAddress(address[b], b/2, address[i]);
		i++;
		}
	}
	
	
	function showAddress(address, i, caption) {
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert(address + " not found");
        } else {
          map.setCenter(point, 13);
//          var marker = new GMarker(point);
//          map.addOverlay(marker);
		  map.addOverlay(createMarker(point, i, caption));
          //marker.openInfoWindowHtml(address);
        }
      }
    );
  }
}
	
	

		showManyAddresses(addresses);
			//showManyAddresses("1918 E Fremont Drive*2040 E Oxford");	
		
      }
    }

function parseJSON(json){
    try{
        if(/^("(\\.|[^"\\\n\r])*?"|[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t])+?$/.test(json)){
            var j = eval('(' + json + ')');
            return j;
		}
	}catch(e){
    }
    throw new SyntaxError("parseJSON");
}


function DisplaySearch(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser doesnt support the scripts on this page. Try Firefox or IE 6 or greater.");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
		ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			//parse ajaxRequest.responseText
			//create variable for html output
			//create variable for map code. 
			//ajaxRequest.callback = function (responseText) {
			//alert(ajaxRequest.responseText);
			
			
			JSONData=parseJSON(ajaxRequest.responseText);
//			JSONData = responseText.parseJSON();
			//alert(JSONData.map);
			
			var ajaxDisplay = document.getElementById('resultsdiv');
			ajaxDisplay.innerHTML = JSONData.html;//display data
			initialize(JSONData.map);//load map
			
			
//			var ajaxDisplay = document.getElementById('resultsdiv');
//			ajaxDisplay.innerHTML = ;
			
			//ajaxDisplay = document.getElementById('headerdiv');
			//ResponseStatus = ajaxRequest.responseText;		
			//ajaxDisplay.innerHTML = ajaxRequest.responseText;	
			
			//ajaxDisplay.innerHTML = '<span class= "med">Fee added successfully</span>';
			//ResponseStatusBig = '<div style="background: #C1CFE6"><span class= "big">Fee added successfully</span></div>';
			//ResponseStatusMed = '<div style="background: #C1CFE6"><span class= "med" >Fee added successfully</span></div>';
			//ResponseStatusNorm = '<span class= "norm">Fee added successfully</span>';
			//setTimeout( function(){document.getElementById('headerdiv').innerHTML = ResponseStatusBig}, 300);
			//setTimeout( function(){document.getElementById('headerdiv').innerHTML = ResponseStatusMed}, 900);
			//setTimeout( function(){document.getElementById('headerdiv').innerHTML = ResponseStatusNorm}, 1000); 


		}
	}

	var Search = document.getElementById('Search').value;
	var Location = document.getElementById('Location').value;
	//var Type = "Extra";
	//var Notes = document.getElementById('FeeNote').value;
	//var Fee = document.getElementById('Fee').value;
	//var ClientID = document.getElementById('client').value;
	
	
	var queryString = "?Search=" + Search +"&Location="+Location;
	ajaxRequest.open("GET", "ajax/reviews_handler.php" + queryString, true);
	ajaxRequest.send(null); 
	

}




