function setDefaultClick(p_src, p_default) {
    if (p_src.value == p_default) { p_src.value = ""; }
}
function setDefaultBlur(p_src, p_default) {
    if (p_src.value == "") { p_src.value = p_default; }
}
function updatePropertyType(p_obj) {
    var frm = p_obj.form;
    for (var i = 0; i < p_obj.options.length; i++) {
        var field = p_obj.options[i].value;
        if (field != "") frm[field].value = (i == p_obj.selectedIndex) ? "Y" : "";
    }
}
function quickSearch(frm) {
    if (document.getElementById('temp_address')) { if (frm.temp_address && frm.temp_address.value != "Address") frm.address.value = frm.temp_address.value; }
    if (document.getElementById('temp_zip_code')) { if (frm.temp_zip_code && frm.temp_zip_code.value != "Zip") frm.zip_code.value = frm.temp_zip_code.value; }
    if (document.getElementById('temp_property_id')) { if (frm.temp_property_id && frm.temp_property_id.value != "MLS Number") frm.property_id.value = frm.temp_property_id.value; }
    frm.submit();
}

// this is set to yes after all the variables have been loaded
city_county_loaded = "no";
city_page_load_first_run = "Y";
county_page_load_first_run = "Y";
useall = true;
allfactor = 1;

// when all the variables have been loaded via the external js file the city_county_loaded variable will be set to yes.
// then we can move on
function checkDataLoad() {
    if (city_county_loaded == "no") {
        setTimeout("checkDataLoad();", 250);
    } else {
        stateToggle();
        updateCounties();
        updateCities();
    }
}

// Checks to see if state_list has more than one state - if not, disable the state dropdown
function stateToggle() {
    if (document.searchForm.state.length == 1) {
        document.searchForm.state.disabled = true;
        document.searchForm.state.style.display = "none";
    }
}

// checks the current state (if one is defined) and populates the list of counties in the select box
function updateCounties() {
    f = document.searchForm;
    // blank the select box
    RemoveAll(f.county);
    if (useall) {
        AddToSelect(f.county, "- County -", "");
    }
    // loop over the array of states, adding them to the dropdown
    if (counties.length > 0) {
        for (x = 0; x < counties.length; x++) {
            // a little test to make sure only the right counties show up if a state is chosen
            use_this_county = 1;

            chosen_state = f.state.options[f.state.selectedIndex].value;
            if (states[x] != chosen_state) {
                use_this_county = 0;
            }

            // if the state matches, or if there is no state, add it to the select
            if (use_this_county == 1) {
                AddToSelect(f.county, counties[x], counties[x]);
                //Set the Default County to selected if one is present
                if (f.DefaultCounty.value.toLowerCase() == counties[x].toLowerCase()) {
                    SelectOption(f.county, counties[x], true);
                }
            }
        }
        // select the first one
        if (f.DefaultCounty.value == "") {
            f.county.selectedIndex = 0;
        }
    } else {
        // There are no counties, so disable the selection box
        f.county.disabled = true;
        f.county.style.display = "none";
    }
    if (county_page_load_first_run == "Y") {
        county_page_load_first_run = "N";

        if ("" != '') {
            SelectOption(f.county, "", "");
        } else if ("" != '') {
            SelectOption(f.county, "", "");
        }

    } else {
        // populate the city list
        //RemoveAll(f.chosencities);
        updateCities();
    }
}
// this function accepts a county name and returns the index of that county
// from within the js array that contains the city data
function getCountyIndex(countyname) {
    chosen_state = f.state.options[f.state.selectedIndex].value;
    for (x = 0; x < counties.length; x++) {
        if (counties[x] == countyname && states[x] == chosen_state) {
            return (x);
        }
    }
}
// update the city dropdown according to chosen county or 'all' for a given state
function updateCities() {
    // blank the dropdown
    RemoveAll(f.citylist);
    f.city.value = "";
    AddToSelect(f.citylist, "- City -", "");
    // get the county array index
    countySelectIndex = f.county.selectedIndex - allfactor;
    // less than zero if they chose 'all'
    if (countySelectIndex < 0) {
        // now we choose which index points to the chosen state
        chosen_state = f.state.options[f.state.selectedIndex].value;
        chosen_state_index = listfind(state_list, chosen_state, ",");
        // loop over the state/city array and build the list
        if (chosen_state_index > 0) {
            for (x = 0; x < all_array[chosen_state_index].length; x++) {
                AddToSelect(f.citylist, all_array[chosen_state_index][x], all_array[chosen_state_index][x]);

            }
        } else {
            // this will only happen if the admin puts a state in the list for a given market and there happen
            // to be no cities in the db that match.  this will be VERY rare, if ever.
            alert("No Cities in this state")
        }
    } else {
        // pull back the index for the chosen county
        countyIndex = getCountyIndex(f.county.options[f.county.selectedIndex].value)

        // loop over the county/city array and add to city select
        for (x = 0; x < cities[countyIndex].length; x++) {
            AddToSelect(f.citylist, cities[countyIndex][x], cities[countyIndex][x]);
            //Set the Default city to selected if one is present
            if (f.DefaultCity.value.toLowerCase() == cities[countyIndex][x].toLowerCase()) {
                SelectOption(f.citylist, cities[countyIndex][x], true);
            }
        }
    }
}
var f;

function listfind(thelist, thevalue, delim) {
    if (!delim) {
        delim = ",";
    }
    l = listlen(thelist, delim);
    listpos = -1;

    for (var x = 1; x < l; x++) {
        listelement = listgetat(thelist, x, delim);
        if (listelement == thevalue) {
            listpos = x;
            break;
        }
    }
    return listpos;
}
function doSearch() {
    
    var qs = ''; 
    var f = document.searchForm;
    //				    qs = appendSearch(qs, f.citylist.options[f.citylist.selectedIndex].value, 'city');
    //				    qs = appendSearch(qs, f.min_bedrooms.options[f.min_bedrooms.selectedIndex].value, 'min_bedrooms');
    //				    qs = appendSearch(qs, f.min_bathrooms.options[f.min_bathrooms.selectedIndex].value, 'min_bathrooms');
    //				    qs = appendSearch(qs, f.min_price.options[f.min_price.selectedIndex].value, 'min_price');
    //				    qs = appendSearch(qs, f.max_price.options[f.max_price.selectedIndex].value, 'max_price');
    //				    qs = appendSearch(qs, 'Y', f.tmp_property_type.options[f.tmp_property_type.selectedIndex].value);

    qs = appendSearch(qs, f.citylist, 'city',true);
    
    qs = appendSearch(qs, f.min_bedrooms, 'min_bedrooms', true);
    qs = appendSearch(qs, f.min_bathrooms, 'min_bathrooms', true);
    qs = appendSearch(qs, f.min_price, 'min_price', true);
    qs = appendSearch(qs, f.max_price, 'max_price', true);
   
    if (f.tmp_property_type.selectedIndex > 0) {
        qs = appendSearch(qs, 'Y', f.tmp_property_type.options[f.tmp_property_type.selectedIndex].value, false);
    }
    document.location.href = qs;
}
function appendSearch(currentQS, valueToAppend, qsToAppend, isSelect) {
    if ((!isSelect && valueToAppend != '') || (isSelect && valueToAppend.selectedIndex > 0)) {
        if (currentQS.length == '') {
            currentQS = currentQS + 'http://www.toniarney.com/mls?action=newsearchsession';
        }
        var theVal = '';
        if (isSelect) {
            theVal = valueToAppend.options[valueToAppend.selectedIndex].value;
        }
        else {
            theVal = valueToAppend;
        }
        currentQS = currentQS + '&' + qsToAppend + '=' + theVal;
    }
    return currentQS;
}

window.onload = checkDataLoad;


