/*######################################################################################
# PHP STORE LOCATOR SCRIPT (phpscriptindex.com, phpstorelocatorscript.com)
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# NOTICE: (C) DB DESIGN 2007, DO NOT COPY OR DISTRIBUTE CODE WITH OUT PERMISSION
# Code is NOT open source and subject to a software license agreement. You are
# allowed to modify the software to meet the needs of your domain in accordance with
# the software license agreement.
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++#
# SUPPORT: phpstorelocatorscript.com, phpscriptindex.com/support/
# EMAIL SUPPORT: phpsales@gmail.com Monday - Friday 10:00am to 5:00pm EST
######################################################################################*/

//ajax function for w3c school tutorial
function ajax(sendVars, page, responceHandler){
  var xmlHttp;
  try{
    // Firefox, Opera 8.0+, Safari
    xmlHttp=new XMLHttpRequest();
  }catch (e){
    // Internet Explorer
    try{
      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }catch (e){
      try{
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }catch (e){
        alert("Your browser does not support AJAX!");
        return false;
        }
      }
    }
    xmlHttp.onreadystatechange=function(){
      if(xmlHttp.readyState==4)
        {		
          responceHandler(xmlHttp.responseText, xmlHttp.responseXML);
        }
      }
    xmlHttp.open("POST",page,true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 	  	
    xmlHttp.send(sendVars);
}

function stopRKey(evt) { //http://www.mediacollege.com/internet/javascript/form/disable-return.html
	var evt  = (evt) ? evt : ((event) ? event : null);
	var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
	if ((evt.keyCode == 13) && (node.type=="text")) { return false; }
}
document.onkeypress = stopRKey;

function postcodeLookUp(form){
	try{
		var node = new geoNode();
	}catch(e){
		alert('Cannot conntect to Google AJAX API to fetch latitude and longitude information.');		
		return;		
	}
	node.returnHandler = function(result){ 
		form.lat.value = result.lat;
		form.lng.value = result.lng;
		form.submit();
	};
	node.geocode(form.postcode.value+', '+form.countryIso.value);
}

function locationLookUp(form){
	try{
		var node = new geoNode();
	}catch(e){
		alert('Cannot conntect to Google AJAX API to fetch latitude and longitude information.');		
		return;		
	}
	node.returnHandler = function(result){ 
		form.lat.value = result.lat;
		form.lng.value = result.lng;
		form.submit();
	};
	var loc = form.location.value + ", " + form.countryIso.value;
//	alert(loc);
	node.geocode(loc);	
}

function townLookUp(form){
	try{
		var node = new geoNode();
	}catch(e){
		alert('Cannot conntect to Google AJAX API to fetch latitude and longitude information.');		
		return;		
	}
	node.returnHandler = function(result){ 
		form.lat.value = result.lat;
		form.lng.value = result.lng;
		form.town.value = result.city;
		form.state.value = result.region;
		form.submit();
	};
	var loc = form.town.value + " " + form.state.value + ", " + form.countryIso.value;
//	alert(loc);
	node.geocode(loc);	
}

function addressLookUp(form){
	if(form.fetchLL.checked){
		try{
			var node = new geoNode();
		}catch(e){
			alert('Cannot conntect to Google AJAX API to fetch latitude and longitude information.');		
			return;
		}
		node.returnHandler = function(result){ 
			form.latitude.value = result.lat;
			form.longitude.value = result.lng;
			form.fetchLL.checked = false;
			form.latitude.disabled = '';
			form.longitude.disabled = '';
			form.submit();
		};		
		var loc = form.address.value + " " + form.address2.value + ", " + form.city.value + ", " + form.state.value + " " + form.zip.value + ", " + form.country.value;		
		node.geocode(loc);			
	}else{
		form.submit();
	}
}

function addressLookUp2(form){
		try{
			var node = new geoNode();
		}catch(e){
			alert('Cannot conntect to Google AJAX API to fetch latitude and longitude information.');		
			return false;
		}
		node.returnHandler = function(result){ 
			form.latitude.value = result.lat;
			form.longitude.value = result.lng;
			form.latitude.disabled = '';
			form.longitude.disabled = '';
			var sendVars = "&latitude="+form.latitude.value+"&longitude="+form.longitude.value+"&id="+form.id.value+"&redirect_get="+form.redirect_get.value;
			ajax(sendVars, "updatell.php", function(x){ document.getElementById('box'+form.id.value).style.backgroundColor='green'; });
			
		};		
		var loc = form.address.value + " " + form.address2.value + ", " + form.city.value + ", " + form.state.value + " " + form.zip.value + ", " + form.country.value;		
		node.geocode(loc);	
}

function geoNode(){
	this.gls = new GlocalSearch();
	this.returnHandler = function (latitude, longitude){ };
	this.parseResult = function(){ 
		if(this.gls.results[0]){
			this.returnHandler(this.gls.results[0]);
		}else{
			alert('Sorry your location was not found.');
		}
	}
	this.geocode = function(loc){
		this.gls.setSearchCompleteCallback(this, this.parseResult);
		this.gls.execute(loc);
	}	
}
