   
// This function deals with the return addres from the db
function gMapItCompleted(){
  var e = document.getElementById('container'); 
	if (gMapIt.responseStatus){
	  gMapItReturn = gMapIt.response;
		var toProcess = gMapItReturn.split('<$>');  
		GMid = toProcess[0]; //This is the voter ID
		address = toProcess[1]; //This is the combined address string
		if (GMid > 0) {
      getGCode();
    }
	} 
}
	
// This function deals with the return addres from the db (for signature process)
function gMapItSignCompleted(){
  var e = document.getElementById('container'); 
  if (gMapItSign.responseStatus){
    gMapItReturn = gMapItSign.response;
    var toProcess = gMapItReturn.split('<$>');  
    GMid = toProcess[0]; //This is the voter ID
    address = toProcess[1]; //This is the combined address string
    if (GMid > 0) {
      getGCode();
    }
  } 
}
  
// This function uses a sack AJAX request to get and address for geocoding	
var gMapIt = new sack();					
function gMapdoit(){
  gMapIt.setVar("a", "g"); // name, value.
	gMapIt.requestFile = "return_address.php";
	gMapIt.method = "POST";
	//gMapIt.element = 'container'; // If we wanted to replace a container we would use this				
	gMapIt.onCompletion = gMapItCompleted;
  gMapIt.runAJAX();
}
  
// This function uses a sack AJAX request to get and address for geocoding (for signature process)
var gMapItSign = new sack();
function gMapdoitSign(id){
  gMapItSign.setVar("a", "s"); // name, value.
  gMapItSign.setVar("vid",id);
  gMapItSign.requestFile = "return_address.php";
  gMapItSign.method = "POST";
  //gMapItSign.element = 'container'; // If we wanted to replace a container we would use this        
  gMapItSign.onCompletion = gMapItSignCompleted;
  gMapItSign.runAJAX();
}

// This function sends the geocoded address back to the server
var gMapSendIt = new sack();
function gMapSend(id,a,b,c) {
  gMapSendIt.setVar("a", "a");
	gMapSendIt.setVar("vid",id);
	gMapSendIt.setVar("x", a);
	gMapSendIt.setVar("y", b);
	gMapSendIt.setVar("z", c);
	gMapSendIt.requestFile = "return_address.php";
	gMapSendIt.method = "POST";
	gMapSendIt.runAJAX();
}

// This function sends an error back if the address can't be geocoded
var gMapSendErr = new sack();
function gMapSendError(id,error) {
	gMapSendErr.setVar("a", "e");
	gMapSendErr.setVar("er", error);
	gMapSendErr.setVar("vid",id);
	gMapSendErr.requestFile = "return_address.php";
	gMapSendErr.method = "POST";
	gMapSendErr.runAJAX();
}

//-------------------------------------------------------------------------------------
// Google Mapping functions below  
   
function getGCode()
{
  // Create new geocoding object
  geocoder = new GClientGeocoder();
  // Retrieve location information, pass it to addToMap()
  geocoder.getLocations(address, processMap);
}

function processMap(response)
{
  if (response && response.Status.code != 200) {
	  // variable x does not exist, trigger error function
	  gMapSendError(GMid,response.Status.code);
  } else {
    // Retrieve the object
	  place = response.Placemark[0];
	  // Retrieve the latitude and longitude
	  point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
	  accuracy = place.AddressDetails.Accuracy;
		  
	  // If we want to capture the info and send it we would do it now. Send via ajax to server...	
	  //------------------------------------------------------------------------------------------
	  // The example file return_address.php shows how we provide the address
	  var a = place.Point.coordinates[1];
	  var b = place.Point.coordinates[0];
	  var c = accuracy;
	  gMapSend(GMid, a, b, c);
  }	
}
//-------------------------------------------------------------------------------------


//------------------------------------------------------------------------------------
// The code below makes the browser wait for a set idle time, then fires the getGCode() function if this time is reached.
var setTimeOut = 60000; // 30000 is 30 seconds. 

var xScroll, yScroll, timerPoll, timerRedirect, timerClock;

function initRedirect(){
  if (typeof document.body.scrollTop != "undefined"){ //IE,NS7,Moz
    xScroll = document.body.scrollLeft;
    yScroll = document.body.scrollTop;

    clearInterval(timerPoll); //stop polling scroll move
    clearInterval(timerRedirect); //stop timed redirect

    timerPoll = setInterval("pollActivity()",1); //poll scrolling
    timerRedirect = setInterval("gMapdoit(); initRedirect;",setTimeOut); //set timed redirect
  }
  else if (typeof window.pageYOffset != "undefined"){ //other browsers that support pageYOffset/pageXOffset instead
    xScroll = window.pageXOffset;
    yScroll = window.pageYOffset;

    clearInterval(timerPoll); //stop polling scroll move
    clearInterval(timerRedirect); //stop timed redirect

    timerPoll = setInterval("pollActivity()",1); //poll scrolling
    timerRedirect = setInterval("gMapdoit(); initRedirect;",setTimeOut); //set timed redirect
  }
  //else do nothing
}

function pollActivity(){
  if ((typeof document.body.scrollTop != "undefined" && (xScroll!=document.body.scrollLeft || yScroll!=document.body.scrollTop)) //IE/NS7/Moz
   ||
   (typeof window.pageYOffset != "undefined" && (xScroll!=window.pageXOffset || yScroll!=window.pageYOffset))) { //other browsers
      initRedirect(); //reset polling scroll position
  }
}

//document.onmousemove=initRedirect;
//document.onclick=initRedirect;
//document.onkeydown=initRedirect;
//window.onload=initRedirect;
//window.onresize=initRedirect;

// End idle browser code
//------------------------------------------------------------------------------------

