var reqStatus

var xmlhttp=false;
/*@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 {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }
@end @*/
if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}


function RSchange() {

 if (xmlhttp.readyState==4) {
	//alert(xmlhttp.status);
	if(xmlhttp.status == 200) {
		var strTemp = xmlhttp.getAllResponseHeaders();		
		if (typeof netscape != 'undefined' && typeof netscape.security !=
			'undefined') {
			 netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
			 var strTest = xmlhttp.responseText;
		     var xmldoc = xmlhttp.responseXML;
	    }else{
		  var strTest = xmlhttp.responseText;
		  var xmldoc = xmlhttp.responseXML;
	    }
		
		if(strTemp.indexOf('text/html')>-1){
			parseText(strTest);		
		}
		else
		{
			parseNodes(xmldoc, strTest);		
		}		
	} else {
		if(xmlhttp.status!=200){
		hideThrobber();
		
		if(objID.indexOf('zipcode') > -1){			
			alert("Invalid Zip Code");
			$(objID).style.border='solid 2px red';
			$(objID).focus();
		}
		reqStatus = false;
		}
	}
 	//processResult(fileStatus);
	//alert(xmlhttp.responseText);
 }
  
}

function postRequest(reqPage, parameters) {

	if(xmlhttp) {
		xmlhttp.open('POST', reqPage, false);
		xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-length", parameters.length);
		xmlhttp.setRequestHeader("Connection", "close");
		xmlhttp.onreadystatechange=RSchange;
		xmlhttp.send(parameters);
	
	}
}

function sendRequest(reqPage) {
if (xmlhttp) { 
  if (typeof netscape != 'undefined' && typeof netscape.security !=
	'undefined') {
	 netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
	 xmlhttp.open("GET", reqPage, true);
  }else{
  	 xmlhttp.open("GET", reqPage, true);
  }
  //alert(reqPage);
  
  xmlhttp.onreadystatechange=RSchange;
  if (typeof netscape != 'undefined' && typeof netscape.security !=
	'undefined') {
	 netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserRead');
	 xmlhttp.send(null);
  }else{
  	 xmlhttp.send(null);
  }  
 }
}

function parseNodes(objXML, objText){

			//  The follwoing block of code allows the return XML page to be displayed in a browser window
			//   The debug option for a CFM page can be set to yes to view page errors.
			
						
				  /*myWin= open("", "displayWindow", "");
				
				  // open document for further output
				  myWin.document.open();
				  // create document
				  var objXmlElem = objXML.documentElement;
				  
				  alert(objXmlElem.getElementsByTagName('State')[0].firstChild.data);
				  
				  strText = objText;
				  myWin.document.write(strText);
				
				  // close the document - (not the window!)
				  myWin.document.close();  
		
				 
				  
				  return;  	*/
		
		//alert(objText);
		
		var objXmlElem = objXML.documentElement;

		node = objXmlElem.selectSingleNode("Result");
		
		if(node != null){
			//var root_node = objXmlElem.getElementsByTagName('session')[0].firstChild.data;
			if(objXmlElem.getElementsByTagName('City')[0].firstChild.data!=null){
				var vCity = capitalize(objXmlElem.getElementsByTagName('City')[0].firstChild.data);
			}
			else{
				var vCity="";	
			}
			
			if(objXmlElem.getElementsByTagName('State')[0].firstChild.data!=null){
				var vState = capitalize(objXmlElem.getElementsByTagName('State')[0].firstChild.data);
			}
			else{
				var vState="";	
			}
			
			if(objXmlElem.getElementsByTagName('Zip')[0].firstChild.data!=null){
				var vZip = objXmlElem.getElementsByTagName('Zip')[0].firstChild.data;
			}
			else{
				var vZip="";	
			}
			
			if(objXmlElem.getElementsByTagName('Country')[0].firstChild.data!=null){
				var vCountry = objXmlElem.getElementsByTagName('Country')[0].firstChild.data;
			}
			else{
				var vCountry="";	
			}
			
			
			
			if(vCity.length==0 || vState.length==0 || vZip.length==0){
				//hideThrobber();
				return;
			}
			
			var curForm = document.getElementById("currentForm").value;
			if(objID == 'Zip_Code'){
				$('city').value = vCity;
				updateSO($('city'), curForm);
				$('state').value = vState;
				updateSO($('state'), curForm);
				$('country').value = vCountry;
				updateSO($('country'), curForm);
				//document.getElementById('zipResults').style.visibility="";
			}
			
			if(objID == 'mailingzipcode'){
				$('mailingcity').value = vCity;
				$('mailingstate').value = vState;
				//$('country').value = objXmlElem.getElementsByTagName('Country')[0].firstChild.data;
				//document.getElementById('zipResults').style.visibility="";
			}
			
			if(objID == 'shippingzipcode'){
				$('shippingcity').value = vCity;
				$('shippingstate').value = vState;
				//$('country').value = objXmlElem.getElementsByTagName('Country')[0].firstChild.data;
				//document.getElementById('zipResults').style.visibility="";
			}
			
			if(objID == 'remittancezipcode'){
				$('remittancecity').value = vCity;
				$('remittancestate').value = vState;
				//$('country').value = objXmlElem.getElementsByTagName('Country')[0].firstChild.data;
				//document.getElementById('zipResults').style.visibility="";
			}
			
			if(objID == "ForeignZipCode"){
				alert("foreignzipcode");
			}
			$(objID).style.border = 'solid 1px black';
			
			hideThrobber();
			return;
		}		
		 
		 return;
			
}

function parseText(strText){
	
	//alert(strText);
	if(strText.indexOf('sectDResults') > -1){
		document.getElementById('sectionD').innerHTML = strText;
	}
		
	if(strText.indexOf('busTypeResults') > -1){
		document.getElementById('busType').innerHTML = strText;
	}
		
}

function capitalize(str){
	
	var newStr = str.charAt(0) + str.substr(1).toLowerCase();
	
	return(newStr);
}


// mozXPath [http://km0ti0n.blunted.co.uk/mozxpath/] km0ti0n@gmail.com
// Code licensed under Creative Commons Attribution-ShareAlike License 
// http://creativecommons.org/licenses/by-sa/2.5/
if( document.implementation.hasFeature("XPath", "3.0") ){
	if( typeof XMLDocument == "undefined" ){ XMLDocument = Document; }
  XMLDocument.prototype.selectNodes = function(cXPathString, xNode){
    if( !xNode ) { xNode = this; } 
		var oNSResolver = this.createNSResolver(this.documentElement)
		var aItems = this.evaluate(cXPathString, xNode, oNSResolver, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
		var aResult = [];
		for( var i = 0; i < aItems.snapshotLength; i++){aResult[i] =  aItems.snapshotItem(i);	}
		
		return aResult;
	}
		
	Element.prototype.selectNodes = function(cXPathString){
		if(this.ownerDocument.selectNodes){	return this.ownerDocument.selectNodes(cXPathString, this);}
		else{throw "For XML Elements Only";}
	}
}


if( document.implementation.hasFeature("XPath", "3.0") ){
	
	XMLDocument.prototype.selectSingleNode = function(cXPathString, xNode){
		if( !xNode ) { xNode = this; } 
		var xItems = this.selectNodes(cXPathString, xNode);
		if( xItems.length > 0 ){return xItems[0];	}
		else{return null;	}
	}
	
	Element.prototype.selectSingleNode = function(cXPathString){	
		if(this.ownerDocument.selectSingleNode){return this.ownerDocument.selectSingleNode(cXPathString, this);	}
		else{throw "For XML Elements Only";}
	}
}