// JavaScript Document

//<![CDATA[
        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 (frm.temp_address && frm.temp_address.value != "Address") frm.address.value = frm.temp_address.value;
                if (frm.temp_zip_code && frm.temp_zip_code.value != "Zip") frm.zip_code.value = frm.temp_zip_code.value;
                if (frm.temp_property_id && frm.temp_property_id.value != "MLS Number") frm.property_id.value = frm.temp_property_id.value;
                frm.submit();
        }
        function quickSearch2() {
                var frm = document.forms.searchForm;
                if (frm.address.value == "Address") frm.address.value = "";
                frm.submit();
        }
//]]>
//<![CDATA[
        // 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{
                        updateCounties();
                        updateCities();
                }
        }
        // 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);
//              RemoveAll(f.chosencities);
//              f.city.value = '';
                // if we need to, add the 'all' option
                if (useall){
                        AddToSelect(f.county,"County","");
                }
                // loop over the array of states, adding them to the dropdown
                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;
                 }


                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]);
                        }
                }
        }
        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;
        }
        
        window.onload=checkDataLoad;
//]]>