﻿var allcities;
var allstates;

function getStates(countryName)
{
    var foundstates;
    countries.each(function(acountry){
        if(acountry.name == countryName)
        {
            foundstates = acountry.states;
        }

    });
    
    return foundstates;
    
}

function getCities(countryName, stateName)
{

    var foundcities;
    
    var cStates = getStates(countryName);
    
    if(cStates)
    {
        cStates.each(function(astate){
            if(astate.name == stateName)
            {
                foundcities = astate.cities;    
            }
        });
    }
    
    return foundcities;
}

function setSelected(selectId, newValue)
{
    var found = false;
    var selectedOptions = $A($(selectId).options);
    var newIndex = -1;
    
   
    selectedOptions.each(function(anoption, theIndex){
        //I hate IE.  We have to check the actual text too
        
        if(anoption.value == newValue || anoption.text == newValue)
        {
           
            newIndex = theIndex;
            found = true;
           
            return;
        }
    
    });

    $(selectId).selectedIndex = newIndex;
    
}

function loadSelects(selectId, newOptions)
{
      $(selectId).options.length = 0; //clear any options
      
            newOptions.each(function(anewoption){
                
               var newValue;
               
                
                //add the All option
                $(selectId).options[0] = new Option("All","", true, true);
                
                //does this thing have a name?
                if(anewoption && !Object.isUndefined(anewoption.name) && anewoption.name != null )
                {
                    newValue = anewoption.name
                }
                else
                {
                    if(Object.isString(anewoption) )
                    {
                        newValue = anewoption;
                    }
                    else
                    {
                        newValue = "";
                    }
                }
                
                if(newValue != "" && anewoption != "undefined")
                {
                    $(selectId).options[$(selectId).options.length] = new Option(newValue,newValue);
                }
               
             
            });

}

function getUniqueStatesArray(options)
{
    var uniqueStates = new Array();
    if(!options)
    {
        options = {};
        options.country = null;
    
    }
    
    if(options.country == "")
    {
        options.country = null;
    }
    
    countries.each(function(acountry){
        //are we filtering by country?
        if(options.country)
        {
           if(options.country == acountry.name)
           {
                acountry.states.each(function(astate){
                    uniqueStates.push(astate.name);
                    
                });
           
           }
        }
        else
        {
            //we have to grab just the name of the state
            acountry.states.each(function(astate){
                uniqueStates.push(astate.name);
            
            });
        
        }
    
    });
    
    //dedupe, sort, and send off
    
    return  uniqueStates.uniq().sort();
}

function getUniqueCitiesArray(options)
{
    var uniqueCities = new Array();
    if(!options)
    {
        options = {};
        options.country = null;
        options.state = null;
    }
    
    if(options.country == "")
    {
        options.country = null;
    }
    
    if(options.state == "")
    {
        options.state = null;
    }
    
    countries.each(function(acountry){
        //are we filtering out by country?
        if(options.country)
        {
            if(acountry.name == options.country)
            {
                acountry.states.each(function(astate){
                
                    //are we filtering by state?
                    if(options.state)
                    {
                        if(options.state == astate.name)
                        {
                            uniqueCities = uniqueCities.concat(astate.cities);
                        }
                    }
                    //nope grab all the cities in the states
                    else
                    {
                        uniqueCities = uniqueCities.concat(astate.cities);
                    }
                });
           }
        }
        
        //no country filter, grab all the cities
        else
        {
                acountry.states.each(function(astate){
                    uniqueCities = uniqueCities.concat(astate.cities);
                });
        }

   
     });
    //return the deduped alphabetized city list
    return uniqueCities.uniq().sort();
}


function getCityStateAndCountry(someCity)
{
    var results = {};
    countries.each(function(acountry){
        //found a result, don't process further
        if(results.country) return;
        
        acountry.states.each(function(astate){
        
            //found a result, don't process further
            if(results.country) return;
            
            astate.cities.each(function(acity){
            
                if(results.country) return;
                
                if(acity == someCity)
                {
                    results.country = acountry.name;
                    results.state = astate.name;
                    return;
                    
                }
            
            });
        
        });
        
    
    });
    
    if(results.country == null || Object.isUndefined(results.country))
    {
        results.country = "";
    }
    
    if(results.state == null || Object.isUndefined(results.state))
    {
        results.state = "";
    }
    
    return results;
}

function getStateCountry(someState)
{
    var thecountry = null;
    countries.each(function(acountry){
        
        if(thecountry) return;
        acountry.states.each(function(astate){
        
            if(astate.name == someState)
            {
                thecountry = acountry.name;
            }
        });
    
    });

    return thecountry;
}

function initializeSearchOptions()
{
 //load the initial countries
     //add the empty state
    $("searchCountrySelect").options[0] = new Option("All","", true, true);
    countries.each(function(acountry){
        $("searchCountrySelect").options[$("searchCountrySelect").options.length] = new Option(acountry.name);
    });
    
     $("searchCountrySelect").observe('change',countryChange);
     $("searchStateSelect").observe('change',stateChange);
     $("searchCitySelect").observe('change',cityChange);
     $("arrive").observe();
     $("depart").observe();
    //setup the link
    $("searchButton").observe('click',sendoff);
    
   
        //load all the cities
        allCities = getUniqueCitiesArray();
        loadSelects("searchCitySelect",allCities);
        
        //load all the states
        allStates = getUniqueStatesArray();
        loadSelects("searchStateSelect",allStates);
        
        
  
}
    
function loadStates(event) {
   //get the states for the country
   var foundstates = getStates($F("searchCountrySelect"));
        
   if(foundstates)
   {
       loadSelects("searchStateSelect",foundstates);
           
   }      
   loadCities();
        
}
    
    function loadCities(event)
    {
        //var foundcities = getCities($F("searchCountrySelect"),$F("searchStateSelect"));
        var options = {};
        options.country = $F("searchCountrySelect");
        options.state = $F("searchStateSelect");
        foundcities = getUniqueCitiesArray(options);
        if(foundcities)
        {
            loadSelects("searchCitySelect",foundcities);
        }      
    
    }
    
    function cityChange(event)
    {
          var info = getCityStateAndCountry($F("searchCitySelect"));
          var selectedCity = $F("searchCitySelect");
          setSelected("searchCountrySelect",info.country);
          loadStates();
          setSelected("searchStateSelect",info.state);
          loadCities();
          setSelected("searchCitySelect",selectedCity);
          
           
        
    }
    
    function stateChange(event)
    {
            //set the country to the current state's country
            setSelected("searchCountrySelect",getStateCountry($F("searchStateSelect")));
            
            //load the cities for that state
            loadCities();
            
    
    
    }
    
    function countryChange(event)
    {
        //if we select an empty country value (All), then reinitialize
        if($F("searchCountrySelect") == "" || $F("searchCountrySelect") == null || $F("searchCountrySelect") == "All")
        {
            //start over
             initializeSearchOptions();
            // alert("Starting Over");
        }
        else
        {
            //otherwise just filter the states (cities will propogate through the loadStates function
            loadStates();
        }
        
    
    }
    
    function sendoff(event)
    {
        var theCountry = $F("searchCountrySelect");
        var theState = $F("searchStateSelect");
        var theCity = $F("searchCitySelect");
        var arrive = $F("arrive");
        var depart = $F("depart");
     
        
        
        var newURL = "./searchResults.aspx?fml=0";        
        if(theCountry && theCountry != "" && Object.isString(theCountry))
        {
            newURL +="&country=" + encodeURIComponent(theCountry);
        }
        
        if(theState && theState != "" && Object.isString(theState))
        {
            newURL += "&state=" + encodeURIComponent(theState);
            
        }
        
        if(theCity && theCity != "" && Object.isString(theCity))
        {
            newURL += "&city=" + encodeURIComponent(theCity);
        }
        
        if(arrive && arrive != "" && Object.isString(arrive))
        {
            newURL += "&arrive=" + encodeURIComponent(arrive);
        }
        
        if(depart && depart != "" && Object.isString(depart))
        {
            newURL += "&depart=" + encodeURIComponent(depart);
        }
       
        window.location.href = newURL;
        
        
    
    }
    
 