
/*******************************
 *  Zip Lookup
 *  Jeff Martin
 *  8/18/2006
 * ----------------------------
 *  Usage:
 *  	<script type="text/javascript" src="/js/xml.js"></script>
 *		<script type="text/javascript" src="/js/zipLookup.js"></script>
 *  Dependencies:
 *  	xml.js
 *  Notes:
 *  	Adapted from javascript in the cf_internationalAddress2 custom tag
 *******************************/
 
var zipLookup_xml = new XML();

function zipLookup_lookup( prefix, script )
{
	// if something goes wrong, just fail silently
	//try
	{
		// hook events
		zipLookup_xml.onLoad = function( xmlDoc ) { zipLookup_xml_onLoad( xmlDoc, prefix ); };

		var eCountry = document.getElementById( prefix + "Country" );
		var eZip = document.getElementById( prefix + "Zip" );
	
		// if the zip is null or empty, skip the lookup
		if( eZip.value == "" )
			return;
					
		// if the country isn't US, skip the lookup
		if( eCountry != null && eCountry.value != "US" )
			return;

		// perform the lookup using AJAX
		if( script.indexOf( "?", 1 ) > -1 )
		{
			var url = script + "&hideComments&zip=" + eZip.value;
		}
		else
		{
			var url = script + "?hideComments&zip=" + eZip.value;	
		}
		
		zipLookup_xml.loadXML( url );
	}
	//catch( exception )
	{
		// ignore exceptions
	}
}

function zipLookup_xml_onLoad( xmlDoc, prefix )
{
	// if something goes wrong, just fail silently
	try
	{
		var eCity = document.getElementById( prefix + "City" );
		var eState = document.getElementById( prefix + "State" );
		var eZip = document.getElementById( prefix + "Zip" );
		
		// read the response
		var error = xmlDoc.selectSingleNode( "//zipLookup/error/text()" );
		if( error != null && error.nodeValue != "" )
		{
			alert( error.nodeValue );
			eZip.value = "";
		}
		else
		{
			eCity.value = xmlDoc.selectSingleNode( "//zipLookup/city/text()" ).nodeValue;
			eState.value = xmlDoc.selectSingleNode( "//zipLookup/state/text()" ).nodeValue;
		}
	}
	catch( exception )
	{
		// ignore exceptions
	}
}

