// MGeocoder() GMaps API extension copyright 2005 Mikel Maron (mikel_maron@yahoo.com)
// http://brainoff.com/gmaps/mgeocoder.html
// free for non-commercial use
//  use must also comply with licensing on geocoder.us and brainoff.com/geocoder

function MGeocoder(){}
MGeocoder.prototype.initialize=function() {
    this.xmlhttp = false;
    this.request = false;
}

MGeocoder.prototype.callgeocoder=function(type,addr,city,country) {
  if (this.request != false)  { return; }
  this.request = type;
  document.getElementById("MGeocoderError").innerHTML = "Querying...";
  if (! this.xmlhttp) {
	this.loadReq();
  }
  if (this.request == "us") {
	url = "http://geocoder.us/service/rest/?address=" + encodeURI(addr);
  } else {
	url = "http://brainoff.com/geocoder/rest/?city=" + encodeURI(city+","+country);
  }
  this.xmlhttp.open("GET","proxy.php?"+url,true);
  this.xmlhttp.onreadystatechange=mgeocoder_cb;
  this.xmlhttp.send(null);
}

MGeocoder.prototype.loadReq = function() {
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
 try {
  this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   this.xmlhttp = false;
  }
 }
@end @*/
if (!this.xmlhttp && typeof XMLHttpRequest!='undefined') {
  this.xmlhttp = new XMLHttpRequest();
}
}

MGeocoder.prototype.getNodeValue = function(obj, nodeName) {
  var st = "";
  if (obj.hasChildNodes()) {
    var i = 0;
    while ((st == "") && (i < obj.childNodes.length)) {
      st = (obj.childNodes[i].nodeName == nodeName) ? obj.childNodes[i].firstChild.nodeValue : this.getNodeValue(obj.childNodes[i], nodeName);
      i++;
      }
    }
  return st;
}

function mgeocoder_cb() {
  	if (document.geocoder.xmlhttp.readyState==4) {
		lat = false; lon = false;
		if (document.geocoder.xmlhttp.status == "200") {
          		if (document.geocoder.xmlhttp.responseXML && document.geocoder.xmlhttp.responseXML.hasChildNodes()) {
				try {
            				lat = document.geocoder.getNodeValue(document.geocoder.xmlhttp.responseXML, "geo:lat");
              				lon = document.geocoder.getNodeValue(document.geocoder.xmlhttp.responseXML, "geo:long");
				} catch(e) {
				}
			}
		}

		if (lat != false && lon != false) {
			document.getElementById("MGeocoderError").innerHTML = "";

			var zoom = lat + "," + lon + ",64";
			document.worldkit.SetVariable("JZoomComm",zoom);
			//setFlashVariables('worldkit','JZoomComm='+zoom);
		} else {
			document.getElementById("MGeocoderError").innerHTML = "Lookup Failed";
		}
		document.geocoder.request = false;
	}
}

