﻿       // 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.aspnetForm.state.length == 1) {
               document.aspnetForm.state.disabled = true;
               document.aspnetForm.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.aspnetForm;
           // 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]);
               }
           }
       }
       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;
