// pass in the ID of the city and state fields
function getCityState(cityField, stateField, zipField)
{
	var zip = document.getElementById(zipField);
	var url = ZIP_URL + zip.value;
	sendXMLRequest('post', url, "updateCityState(xmlhttp,'" + cityField + "','" + stateField + "')");
}

function updateCityState(xmlhttp, cityField, stateField) {
	// this bit borrowed from http://www.van-steenbeek.net/?q=explorer_domparser_parsefromstring
	// makes DOMParser.parseFromString work in all browsers with minimal monkeying around
	if(typeof(DOMParser) == 'undefined') {
		DOMParser = function() {}
		DOMParser.prototype.parseFromString = function(str, contentType) {
			if(typeof(ActiveXObject) != 'undefined') {
				var xmldata = new ActiveXObject('MSXML.DomDocument');
				xmldata.async = false;
				xmldata.loadXML(str);
				return xmldata;
			} else if(typeof(XMLHttpRequest) != 'undefined') {
				var xmldata = new XMLHttpRequest;
				if(!contentType) {
					contentType = 'application/xml';
				}
				xmldata.open('GET', 'data:' + contentType + ';charset=utf-8,' + encodeURIComponent(str), false);
				if(xmldata.overrideMimeType) {
					xmldata.overrideMimeType(contentType);
				}
				xmldata.send(null);
				return xmldata.responseXML;
			}
		}
	}
	// end

	var p = new DOMParser();
	var doc = p.parseFromString(xmlhttp.responseText,"text/xml");

	if(document.getElementById(cityField).value == '' && doc.getElementsByTagName("city")[0].firstChild.nodeValue != 'XXXUNKNOWNXXX') {
		document.getElementById(cityField).value = doc.getElementsByTagName("city")[0].firstChild.nodeValue;
		document.getElementById(cityField).select();
	}
	var opts = document.getElementById(stateField).options;
	
	if(document.getElementById(stateField).selectedIndex == 0 && doc.getElementsByTagName("state")[0].firstChild.nodeValue != 'XXXUNKNOWNXXX') {
		for(i=0; i<opts.length; i++) {
			if(opts[i].value == doc.getElementsByTagName("state")[0].firstChild.nodeValue) {
				document.getElementById(stateField).selectedIndex = i;
				break;
			}
		}
	}
}

