 	var xmlHttp;  // 1st global instance of XMLHttpRequest

	function createXmlHttpRequest()
	{
	   if(window.ActiveXObject)
	   {
	      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	   }
	   else if(window.XMLHttpRequest)
	   {
	     xmlHttp=new XMLHttpRequest();
	   }
	}		

	function handleStateChange()
	{
		var e = document.getElementById('zip_noresults_layer');
	
		if(xmlHttp.readyState==4)
		{
			if(xmlHttp.status==200)
			{
			
				var valid = xmlHttp.responseXML.getElementsByTagName("valid")[0].childNodes[0].nodeValue;

				if (valid == 'false')
				{
					
					hideLayer('location_results');
					
					document.getElementById("no_results").innerHTML = "<br/><h2 class='no_results_header'>0 Results Found</h2><p class='no_results_text'><span class=\"bold_text\">We\'re sorry but we presently do not have a sales office within your location.</span> <br/> Please search again or call <span class=\"bold_text red_canon_text\">1-800-815-4000</span>.</p>";
					
					unhideLayer('no_results')
				}
				else
				{
					hideLayer('no_results');
					var x=xmlHttp.responseXML.documentElement.getElementsByTagName("location");

					//document.getElementById("zip_results_layer").visibilty = 'visible';

					var message = "<div id='zip_noresults_layer' style='display:block'><table style='display:block' cellspacing='0' cellpadding='0' border='0' class='rounded_table'>" 
					+ "<tr> " 
					+ "<td class='col_select'>Select</td>"
					+ "<td class='col_office_name'>Office Name</td>"
					+ "<td class='col_address'>Address</td>"
					+ "<td class='col_phone'>Phone</td>" + "</tr>" ;


					for (i=0; i < x.length; i++) 
					{
					   var office_name_str = xmlHttp.responseXML.getElementsByTagName("office_name")[i].childNodes[0].nodeValue;
					   var address1 = xmlHttp.responseXML.getElementsByTagName("address1")[i].childNodes[0].nodeValue;
					   var city = xmlHttp.responseXML.getElementsByTagName("city")[i].childNodes[0].nodeValue;
					   var state = xmlHttp.responseXML.getElementsByTagName("state")[i].childNodes[0].nodeValue;
					   var zip = xmlHttp.responseXML.getElementsByTagName("zip")[i].childNodes[0].nodeValue;
					   var phone = xmlHttp.responseXML.getElementsByTagName("phone")[i].childNodes[0].nodeValue;

					   message = message + "<tr><td class='select'><input onclick='setDestAddress(\"" + office_name_str + "\",\"" + address1 + "\",\"" + city + "\",\"" + state + "\",\"" + zip + "\",\"" + phone + "\")' type='radio'  name='location' /></td>" 
					   + "<td class='office_name'>" +  office_name_str +  "</td>" 
					   + "<td class='address'>" + address1 +  "<br> " +  city  +  ", " + state  + " " + zip +  "</td>" 
					   + "<td class='phone'>" +  phone +  "</td>" + "</tr>";	
					}

					message = message  + "<tr class='grey'>"
					   + "<td class='left_bottom'>&nbsp;</td>"
					   + "<td class='bottom'>&nbsp;</td>"
					   + "<td class='bottom'>&nbsp;</td>"
					   + "<td class='right_bottom'>&nbsp;</td></tr>"
					   + "</table></div>";

					document.getElementById("zip_noresults_layer").innerHTML = message;
					unhideLayer('location_results');
				}
				
			   }
			   else
			   {
				alert("Error loading page" + xmlHttp.status + ":" + xmlHttp.statusText);
			   }
		 }
	}


	// This function will retreive the listing of address from the database.  If the 
	// zip-code is not in the database a message will be displayed to the user indicating the zip does
	// not exist.
	
	
	function startRequest()
	{
	  createXmlHttpRequest();
	  var u1=document.zipform.zip.value;
	  xmlHttp.open("GET","http://localhost:8085/xmlproj/servlet/ZipCodeLocatorServlet?zip_code="+u1, true);
	  xmlHttp.onreadystatechange=handleStateChange;
	  xmlHttp.send(null);
	}
	
	
	function unhideLayer(layerName)
	{
	   document.getElementById(layerName).style.visibility = "visible";
	}
	
	
	function hideLayer(layerName)
	{
		   document.getElementById(layerName).style.visibility = "hidden";
	}
	
	
	function setDestAddress(compName, address, city, state, zip, phone)
	{
	   alert(compName + address + city + state + zip + phone);
	}
