var xmlHttp;
var xID;

/* 
 * Description: Retrieve info via ajax.
 * param f = function number in ajax file to call
 * param d = data to send to ajax file
 * param i = HTML id to write the ajax results to
 */
function getInfo(f, d, i)
{
  xmlHttp=GetXmlHttpObject()
  if (xmlHttp == null)
  {
    alert ("Browser does not support HTTP Request");
    return;
  } 
  var url = "/inc-ajax.php?f="+f+"&d="+d;
  xID = i;
  xmlHttp.onreadystatechange = stateChanged;
  xmlHttp.open("GET", url, true);
  xmlHttp.send(null);
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function markFav(id)
{
  xmlHttp=GetXmlHttpObject()
  if (xmlHttp == null)
  {
    alert ("Browser does not support HTTP Request");
    return;
  }
  var email = document.getElementById('favemail');
  if (email != null)
    var e = email.value;
  else
    var e = '';
  
  var url = "/inc-ajax.php?f=2&id="+id+"&e="+e;
  xmlHttp.onreadystatechange = stateChanged;
  xmlHttp.open("GET", url, true);
  xmlHttp.send(null);
  setCookie('BeachFav'+id, id, 7);
  document.getElementById('favourite').innerHTML='';
}

function stateChanged() 
{ 
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete")
    document.getElementById(xID).innerHTML = xmlHttp.responseText;
} 

function GetXmlHttpObject()
{ 
  var objXMLHttp = null;
  if (window.XMLHttpRequest)
    objXMLHttp = new XMLHttpRequest();
  else if (window.ActiveXObject)
    objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
  return objXMLHttp;
} 

function checkSearch(data)
{
  var el = document.getElementById('qText');
  var sText = '      Search Beaches';
  if (data == sText)
  {
    el.value = '';
    el.style.color = '#000000';
  }
  if (data == '')
  {
    el.value = sText;
    el.style.color = '#666666';
  }
}

var map = null;
var geocoder;

function load() {
  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("setMap"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(45.120053, -62.666016), 6);
    
		GEvent.addListener(map, 'click', function(overlay, point) {
      if (document.getElementById("readySet").checked) {
        map.clearOverlays();
        map.setCenter(point);
        var geocoder = new GClientGeocoder();
        map.addOverlay(new GMarker(point));
				document.getElementById("beachLat").value = point.y;
				document.getElementById("beachLong").value = point.x;
				geocoder.getLocations(point, showAddress);
				}

		});
    
  }
}

    function showAddress(response) {
      if (!response || response.Status.code != 200) {
        //alert("Status Code:" + response.Status.code);
      } else {
        place = response.Placemark[0];
        document.getElementById("mapLocation").innerHTML = place.address;
      }
}

function clearMarkers()
{
  map.clearOverlays();
  document.getElementById("beachLat").value = '';
	document.getElementById("beachLong").value = '';
  document.getElementById("mapLocation").innerHTML = "Beach Map Location is not set";
}


