// JScript File
google.load("search", "1");
    //To roundOff the Number to decimal places
    function roundNumber(num, dec) 
    {
	    var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	    return result;
    }
    function OnLoad(address) 
    {
    // Create a search control
      var searchControl = new google.search.SearchControl();
      // Add in local search
      var localSearch = new google.search.LocalSearch();
      var options = new google.search.SearcherOptions();
      options.setExpandMode(GSearchControl.EXPAND_MODE_OPEN);
      searchControl.addSearcher(localSearch, options);
      // Set the Local Search center point
      localSearch.setCenterPoint(address);
      // Tell the searcher to draw itself and tell it where to attach
      searchControl.draw(document.getElementById("searchcontrol"));
      // Execute an inital search
      searchControl.execute("Hospital");
      // Declare function for using results
	  searchControl.setSearchCompleteCallback(this, gotResults);
    }
    function gotResults(sc, searcher)
	{
		var resultcontent = '';
		var resultdiv = document.getElementById('searchresults');
		    for (i = 0; i < searcher.results.length; i++) {
		        var result = searcher.results[0];
		        if (result.title != null) {
		            resultcontent = result.titleNoFormatting;
		        }
		        if (result.streetAddress != '') { 
		        }
		        if (result.city != '') { 
		            resultcontent += ',' + result.city;
		        }		        
		        searcher.results.length = 0;		        
		    }		
		showAddress(resultcontent);
	}
	var search = '';
	var chkValue = 0;
	var latitude;
	var longitude;
	// ====== Geocoding ======
	function showAddress(address) {	    
	    if (GBrowserIsCompatible()) { 
	            geo = new GClientGeocoder();
	            // ====== Perform the Geocoding ======
	            geo.getLocations(address, function(result) {
	                if (result.Status.code == G_GEO_SUCCESS) {
	                    // ===== If there was more than one result, "ask did you mean" on them all =====
	                    if (result.Placemark.length > 1) {
	                        // Loop through the results
	                        for (var i = 0; i < result.Placemark.length; i++) {
	                            var p = result.Placemark[i].Point.coordinates;
	                        }
	                    }
	                    // ===== If there was a single marker =====
	                    else {
	                        place = result.Placemark[0];
	                        if (place.AddressDetails.Accuracy < 6) {
	                            alert('Unable to find the address. Please verify your location on the map and click Save.');
	                            window.open("EmployeeLocation.aspx", "List", "scrollbars=no,resizable=no,width=400px,height=350px");
	                            return false;
	                        }
	                        else {
	                            window.open("EmployeeLocation.aspx", "List", "scrollbars=no,resizable=no,width=400px,height=350px");
	                            return false;
	                        }
	                        var str = result;
	                        var p = result.Placemark[0].Point.coordinates;
	                        latitude = p[1];
	                        longitude = p[0];
	                        if (document.getElementById('hdnAddressStatus').value == address) {
	                            document.getElementById('hdnAddressStatus').value = address + '*' + latitude + '*' + longitude;
	                        }
	                        else {
	                            document.getElementById('hdnLocationDetails').value = address + '*' + latitude + '*' + longitude;
	                        }
	                    }

	                }
	                // ====== Decode the error status ======
	                else {
	                    var reason = "Code " + result.Status.code;
	                    alert('Unable to find the address. Please verify your location on the map and click Save.');
	                    window.open("EmployeeLocation.aspx", "List", "scrollbars=no,resizable=no,width=400px,height=350px");
	                    return false;
	                }
	            }
        );	
        }      	 	    
        else { alert("Sorry, the Google Maps API is not compatible with this browser"); }
	}
	function getEmpLatLong(address) {
	    if (address == '') {
	    return 
	    }
	    else {
	        if (document.getElementById('hdnAddressStatus').value == '') {
	            document.getElementById('hdnAddressStatus').value = address;
	            showAddress(address);
	        }	        
	    }
	}

	
