// JavaScript Document

function markPropertyOnMap(prop_area,prop_city,latitude,longitude,map,latlng)
{
  var mapDiv = document.getElementById(map);
	mapDiv.style.display = "block";
	var latlngDiv = document.getElementById(latlng);
	latlngDiv.style.display = "block";
	//alert("dsfdsf");
	window.gmap = new GMap2(mapDiv);
	gmap.addControl(new GSmallMapControl());
	gmap.addControl(new GMapTypeControl());
	latSpan = document.getElementById(latitude);    
	lngSpan = document.getElementById(longitude);
	defaultLatlong = new GLatLng(28.635308, 77.22496); //New Delhi is the default 	
	//alert(defaultLatlong.lat());
	gmap.setCenter(defaultLatlong, 13); //Initialize map center - necessary.
	
	//Click event listener. Marks the property and picks up its longitude latitude.
	var marker = null;
	GEvent.addListener(gmap, "click", function(overlay,  latlng) {
		
		//Want to handle only the case when user clicks on the map
		//If user clicks on the marker itself, do nothing...
		if(latlng == null) return;
		
	  latSpan.value = latlng.lat();
	  lngSpan.value = latlng.lng();	  	 
	  
	  if(marker != null) {
	     gmap.removeOverlay(marker);
	   }
	  
	   marker = new GMarker(latlng);
	   marker.map = gmap;
	   gmap.addOverlay(marker);
	 });
	
	// Set up Geocoder
	//alert("sdfds");
	var tries = 0;
	var var1=document.getElementById(prop_area);
	var var2=document.getElementById(prop_city);
	var area = var1.value;
	var city = var2.value;
	//alert(area);
	//alert(city);
	if(area != null && area != ""&& area != "Please Enter Locality") {
	  address = area + "," + city;
	} else {
	  address = city;
	}
	
	address = address + ",India";
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(address, function addToMap(latlong) {
   	
	var latlngarray = new Array;
		latlngarray = latlong;
		/*latlngarray=latlngarray.replace('(', "W3Schools"));
		alert(latlngarray);
	var latlngarray1 = new Array;
		alert(latlngarray);
		alert(latlngarray.split(","));
		alert(latlngarray1);*/
    document.getElementById("defaultlatlng").value=latlngarray;
	//document.getElementById("defaultlng").value=latlngarray1[1];
	//alert(document.getElementById("defaultlat").value);
	//alert(document.getElementById("defaultlng").value);
    //In case the geocoding fails, try again without area
    if(latlong == null) {
    	
    	if(tries == 0) { //Try it again with only city if area search fails. Test case - Sancoale,Panaji fails.
	    	address = city + ",India";
	    	geocoder.getLatLng(address, addToMap);
	    	tries++;
    	} else { //Failed second time? then center to New Delhi
        latlong = defaultLatlong;
    	}
    }
    
    gmap.setCenter(latlong, 13);
	
  });
}


//this function is being used on loadListProperty page
// used in mark property on map feature
function mapDivOnOff(divId1, divId2, divId3, editMapLocation)
{
  var e = document.getElementById(divId1);
  var f = document.getElementById(divId2);
  var m = document.getElementById(divId3);
  var n = document.getElementById(editMapLocation);
  /*alert(divId1);
  alert(divId2);
  alert(editMapLocation);*/
  if(e.style.display == 'block')
  {
    e.style.display = 'none';
    f.style.display = 'none';
		m.style.display = 'none';
		n.style.display = 'block';
		    }

	 else
	 {
	   e.style.display = 'block';
		 f.style.display = 'block';
		 m.style.display = 'block';
		 n.style.display = 'none';
	 }
}
/**
   * Set Lat-Longs in the 'Edit Lat-Long' message DIV
   */	        
	function setMapLatLongOnDiv(div1,div2,div3)
	{
		document.getElementById(div1).innerHTML="<b>Latitude:</b> "+document.getElementById(div2).value+", <b>Longitude:</b>"+document.getElementById(div3).value;
	}
//this function is being used on loadListProperty page
//Enable the disabled button of mark on map feature
function ennableButtons(mapButtonTop,mapButtonBottom)
{
  document.getElementById(mapButtonTop).disabled = false;
	document.getElementById(mapButtonTop).className='';
	document.getElementById(mapButtonBottom).disabled = false;
	document.getElementById(mapButtonBottom).className='';
}

