//
// zip.js : Javascript zipcode search support functions
//
// 2003.20.31.ET
//

//
// checkZipCode(): First look for a matching zipcode in the client-side
//                 JavaScript array.  If not found, call PHP to search
//                 the database.
//
function checkZipCode(string){
   
	var zipval=parseInt(string);
	if(zipval>0){
		if( zip[ zipval ] ){
			//
			// Answer is available client-side:
			//
			document.getElementById("state").value    = zip[ zipval ].slice(0,2);
			document.getElementById("city").value     = zip[ zipval ].slice(3);
			document.getElementById("zipcode").value  = "" + zipval;
		}else{
			//
			// Try to get answer from the server:
			//
			var zipWindow = window.open("","Zip Window","screenX=10, screenY=10, width=200, height=200,resizable=no");

			var OutputString = "<html>\n";
			OutputString += " <head>\n";
			OutputString += "  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n";
			OutputString += " </head>\n";
			OutputString += " <body>\n";
			OutputString += "  <h2>Searching ... please wait...</h2>\n";
			OutputString += "  <form enctype=\"multipart/form-data\" id=\"zipform\" method=\"post\" action=\"zipcodes.php\">\n";
			OutputString += "   <input type=\"text\" name=\"zip\" value=\"" + zipval + "\">\n";
			OutputString += "   <input type=\"submit\" name=\"search\" value=\"Search\">\n";
			OutputString += "  </form>\n  </body>\n</html>\n";
			
			//
			// Create the document:
			//
			zipWindow.document.write(OutputString);
			zipWindow.document.close();
			//
			// Submit the form:
			//
			zipWindow.document.getElementById("zipform").submit();
			
		}
	}
}

//
// function putQueryResultsIntoParentWindow(): called by the result window's <body> "onload" event:
//
function putQueryResultsIntoParentWindow(){
	
	if(window.opener){
		window.opener.document.getElementById("state").value   = document.getElementById("state").value;
		window.opener.document.getElementById("city").value    = document.getElementById("city").value;
		window.opener.document.getElementById("zipcode").value = document.getElementById("zip").value;
	}
	
}
