function createXMLHttpRequest() {
	if (typeof XMLHttpRequest != "undefined") {
		return new XMLHttpRequest();
	} else if (typeof ActiveXObject != "undefined") {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		throw new Error("XMLHttpRequest not supported");
	}
}

// value should be a valid state abbreviation
// citySelect should be the select object to popuplate
// currentCity can optionally be the currently selected city value
function getCities(value, citySelect, currentCity) {
	try {
		var xmlHttp = createXMLHttpRequest();
	} catch(e) {
		//TODO handle this
	}
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			var cityList=xmlHttp.responseText;
			var cityOpts = citySelect.options;
			cityOpts.length = 0;
			if (cityList.length < 1) {
				if (value) {
					cityOpts[0] = new Option("No listings for " + value.toUpperCase(), "", true, true);
				} else {
					cityOpts[0] = new Option("Select state first", "", true, true);
				}
			} else {
				cityOpts[0] = new Option("Select a city", "", true, true);
				var cityListArray = cityList.split("|")
				for (var i =0; i < cityListArray.length; i++) {
					var valueDisplay = cityListArray[i].split("~");
					var selected = currentCity == valueDisplay[0];
					cityOpts[cityOpts.length] = new Option(valueDisplay[1], valueDisplay[0], false, selected);
				}
			}
		
		}
	}
	xmlHttp.open("GET","/include/getCities.asp?state="+value,true);
	xmlHttp.send(null);
}
function buildTypeDot(theForm) {
	var elems = theForm.elements;
	var result = '';
	for (var i = 0; i < elems.length; i++) {
		var elem = elems[i];
		if (elem.name && elem.value) {
			var value = elem.value;
			if (elem.name == 'minprice' || elem.name == 'maxprice') {
				value = cleanPrice(value);
			}
			result += elem.name + '-' + value + '/';
		}
	}
	return result.toLowerCase();
}
function cleanPrice(value) {
	// strip spaces
	var result = value.replace(/^\s+/,'').replace(/\s+$/, '');
	if (value) {
		// strip $ and ,
		result = result.replace(/\$/g,'').replace(/\,/g, '');
		// remove everything after first decimal point
		result = result.replace(/\..*/,'');
		if (result != (result * 1)) {
			// not numeric so set to nothing
			result = '';
		}
	}
	return result;
}

function formatUSCurrency(num, isMinimum) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.round(num * 100);
	cents = num%100;
	num = Math.floor(num / 100).toString();
	if(cents < 10)
		cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length - (1 + i))/3); i++)
		num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
    retValue = (((sign) ? '' : '-') + '$' + num + '.' + cents);
	return (retValue == '$0.00') ? (isMinimum ? 'Minimum' : 'Maximum') : retValue;
}

function formHasValues(theForm) {
	var elems = theForm.elements;
	for (var i = 0; i < elems.length; i++) {
		if(elems[i].value) {
			return true;
		}
	}
	return false;
}
