var bustcachevar=1 //bust potential caching of external pages after initial request? (1=yes, 0=no)
var rootdomain="http://"+window.location.hostname
var xmlHttp

function makeGETRequest(url){
	url = unescape(url);
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp==null){
		alert("Your browser does not support AJAX");
		return;
	}
	xmlHttp.onreadystatechange = loadpage;
	if (bustcachevar) //if bust caching of external page
		var bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime()
	xmlHttp.open('GET', url+bustcacheparameter, true)
	xmlHttp.send(null)
}

function GetXmlHttpObject(){
	var xmlHttp=null;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}catch (e){
		// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e)			{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function loadpage(){
		if (xmlHttp.readyState == 4 && (xmlHttp.status==200 || window.location.href.indexOf("http")==-1)){
			var xmlDoc=xmlHttp.responseXML;
			findObj('citystatelabel').innerHTML=xmlDoc.getElementsByTagName("label")[0].childNodes[0].nodeValue;
			findObj('citystatevalue').innerHTML=xmlDoc.getElementsByTagName("city")[0].childNodes[0].nodeValue+", "+xmlDoc.getElementsByTagName("state")[0].childNodes[0].nodeValue;
			findObj('cityval').value=xmlDoc.getElementsByTagName("city")[0].childNodes[0].nodeValue;
			findObj('stateval').value=xmlDoc.getElementsByTagName("state")[0].childNodes[0].nodeValue;
		}
}

function checkzipchange(a){
	if(a.length==5){
		makeGETRequest(rootdomain+'/ziplookup.php?zip='+a);
	}else{
		findObj('citystatelabel').innerHTML='';
		findObj('citystatevalue').innerHTML='';
	}
}

function findObj(n, d) {
	var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
	if(!x && d.getElementById) x=d.getElementById(n); return x;
} 