function setupForm() {
    var searchSettings = getCookie("unified-ps");
    fillForm(searchSettings);
    setPrices(document.ps.pt);
    // these have to be done *after* the property type and prices are set
    getValue(searchSettings, "min", document.ps.min, "select");
    getValue(searchSettings, "max", document.ps.max, "select");
    // AutoComplete these fields
    $("#city").autocomplete({source: "/unified/cgi-bin/autocomplete.cgi?f=city"});
    
    //FutureAdvanced Search Capability
    $("#nbh").autocomplete("/unified/cgi-bin/autocomplete.cgi?f=nbh");
    //$("#add").autocomplete("/unified/cgi-bin/autocomplete.cgi?f=add");
    $("#hs").autocomplete("/unified/cgi-bin/autocomplete.cgi?f=hs");
}
function fillForm(str)
{
    // if there's no "city" cookie, use the geocity if defined (make it *look* like the cookie)
    if (str.indexOf("city&") > -1) { getValue(str, "city", document.ps.city, "text"); }
    else if (typeof(geocity) != 'undefined') { getValue("city&"+geocity, "city", document.ps.city, "text"); }

    //Future Advanced Search Capability
    getValue(str, "hs", document.ps.hs, "select");
    //getValue(str, "pt", document.ps.pt, "select");
    getValue(str, "nbh", document.ps.nbh, "text");
    getValue(str, "zip", document.ps.zip, "text");
    //getValue(str, "add", document.ps.add, "text");
    getValue(str, "br", document.ps.br, "select");
    getValue(str, "ba", document.ps.ba, "select");
    //getValue(str, "sqft", document.ps.sqft, "select");
    //getValue(str, "acre", document.ps.acre, "select");
    //getValue(str, "remarks", document.ps.remarks, "text");
    getValue(str, "mls", document.ps.mls, "text");
    //getValue(str, "fc", document.ps.f_FC, "checkbox");
}

function getValue(string,elementName,object,elementType) {
    var startPos = string.indexOf(elementName + "&");
    if (startPos > -1) {
	startPos = startPos + elementName.length + 1;
	var endPos = string.indexOf("&",startPos);
	if (endPos == -1) endPos = string.length;
	
	var elementValue = unescape(string.substring(startPos,endPos));


	if (elementType == "text")     object.value = elementValue;
	if (elementType == "password") object.value = elementValue;
	if (elementType == "select") {
	    for (var i=0; i<object.length; i++) {
		if (object[i].value == elementValue) object.selectedIndex = i;
	    }
	}
	if (elementType == "checkbox" && elementValue == "on") { object.checked = true; }
	if (elementType == "radio")    object[elementValue].checked = true;
    }
}
function getCookie(name) {
 var start = document.cookie.indexOf(name+"=");
 var len = start+name.length+1;
 if ((!start) && (name != document.cookie.substring(0,name.length))) return '';
 if (start == -1) return '';
 var end = document.cookie.indexOf(";",len);
 if (end == -1) end = document.cookie.length;
  return unescape(document.cookie.substring(len,end));
 }

function getValue(string,elementName,object,elementType) {
    var startPos = string.indexOf(elementName + "&");
    if (startPos > -1) {
	startPos = startPos + elementName.length + 1;
	var endPos = string.indexOf("&",startPos);
	if (endPos == -1) endPos = string.length;
	
	var elementValue = unescape(string.substring(startPos,endPos));


	if (elementType == "text")     object.value = elementValue;
	if (elementType == "password") object.value = elementValue;
	if (elementType == "select") {
	    for (var i=0; i<object.length; i++) {
		if (object[i].value == elementValue) object.selectedIndex = i;
	    }
	}
	if (elementType == "checkbox" && elementValue == "on") { object.checked = true; }
	if (elementType == "radio")    object[elementValue].checked = true;
    }
}



function setPrices(t) {
    var min = document.ps.min;
    var max = document.ps.max;

    if (t.value == "RNT" && min.options.length == 6) { return; }
    if (t.value != "RNT" && min.options.length == 24) { return; }

    min.options.length = max.options.length = 0;    
    if (t.value == "RNT")
    {
	mins = new Array(0,500,750,1000,1250,1500);
	maxs = new Array(750,1000,1250,1500);
    }
    else
    {
	mins = new Array(0,25000,50000,75000,100000,125000,150000,175000,200000,225000,250000,275000,300000,325000,350000,375000,400000,450000,500000,600000,700000,800000,900000,1000000);
	maxs = new Array(25000,50000,75000,100000,125000,150000,175000,200000,225000,250000,275000,300000,325000,350000,375000,400000,450000,500000,600000,700000,800000,900000,1000000,1200000);
    }
    
    for (v in mins)
	{
	    min.options[ min.options.length ] = new Option(mins[v], mins[v]);
	}
    for (v in maxs)
	{
	    max.options[ max.options.length ] = new Option(maxs[v], maxs[v]);
	}
    max.options[ max.options.length ] = new Option("No Limit...", 99999999);
    max.options[ max.options.length - 1 ].selected = true;
}

