var searchArea = "";

//Ensure the correct location value is submitted.
function IsValidForm()
{
    var location = document.getElementById("location");
    var countryState = document.getElementById("CountryState");
    var isRegionSearch = document.getElementById("IsRegionSearch");
    //Reset border style
 //   location.css('border','1px solid #999999');
   // countryState.css('border','1px solid #999999');
   // alert("countryState: " + countryState.value+" :: location: " + location.value+" :: isRegionSearch: " + isRegionSearch.value);
    if( isRegionSearch.value == 1 ) //Search by country/state
    {
        if( countryState.value.length > 0 )//Do we have a country state?
        {
			
            SetSearchArea( );
            location.readonly =  true;
            return true;
        }
        else if( location.value.length > 0 )//Do we have a location?
        {
            isRegionSearch.value = 0;
            countryState.disabled = true;
            if($('#Country').val() != null ) { $('#Country').attr("disabled", true); }
            if($('#State').val() != null ) { $('#State').attr("disabled", true); }
            if($('#StateLong').val() != null ) { $('#StateLong').attr("disabled", true); }
            if($('#City').val() != null ) { $('#City').attr("disabled", true); }
            return true;
        }
    }
    else //Search by location
    {
        if( location.value.length > 0 ) //Do we have a location?
        {
            countryState.disabled = true;
             if($('#Country').val() != null ) { $('#Country').attr("disabled", true); }
            if($('#State').val() != null ) { $('#State').attr("disabled", true); }
            if($('#StateLong').val() != null ) { $('#StateLong').attr("disabled", true); }
            if($('#City').val() != null ) { $('#City').attr("disabled", true); }
            return true;
        }
        else if( countryState.value.length > 0 ) //Do we have a country/state?
        {
            SetSearchArea( );
            isRegionSearch.value = 1;
            location.readonly =  true;
            return true;
        }
    }
    location.style.border = '2px solid firebrick';
    alert("To continue, please enter a location (City, ZIP Code or Landmark).");
    return false;
}

function SetSearchArea(CountryState)
{
    var country = '';
    var state = '';
    var stateLong = '';
    var city = '';
    searchArea = ParseCountryState($('#CountryState').val());
	//alert("searchArea: " + searchArea);
    if( searchArea.length == 3 )
    {
        country = searchArea[0];
        state = searchArea[1];
        stateLong = searchArea[2];
    }
    UpdateLocation( country, state );
    
    $('#Country').val(country);
    $('#State').val(state);	        
    $('#StateLong').val(stateLong);
    
    $('#IsRegionSearch').val('1');
}

function UpdateLocation( country, state )
{
    var value = country;
    if( state.length > 0 )
    {
        value = country + ' - ' + state;
        var city = $('#City').val();
        if(city != null && city.length > 0 )
        {
            value = city + ', ' + state ;
        }
    }
	//alert("value" + $('#location').val());
    $('#location').css('border', '1px solid #999999');
    $('#location').val(value);
}

//Displays the cities combo box when a country is selected and sets the search type.
function OnSelectCountryState(CountryState)
{
    //Clear the city if available
    var city = $('City');
    if( city != null ) city.value = '';
    SetSearchArea(CountryState);
    ajaxFunction( '/incfolder/AdvancedSearchCity.asp?k=7'+CountryState,'select_city' );
}
 

//Called when the city combo box changes and updates the value of the location box.
function OnSelectCity()
{
    SetSearchArea();
}
        
//Shows/Hides the search options.
function ToggleOptions()
{
	var isVisible = ( document.getElementById('advancedSearchOptions').style.display == 'none' );
    var button = ( isVisible ? '/assets/images/02_minus.png' : '/assets/images/02_plus.png' );
	document.getElementById('OptionsButton').src = button;
    document.getElementById('OptionsLinkText').innerHTML = ( isVisible ? 'Hide' : 'Show' );            
   
    if( isVisible )
    {
        $('#advancedSearchOptions').slideDown('normal');
		SetCookie('ShowOptions', isVisible,30);
        document.getElementById('ShowOptions').value = "1";
    }
    else
    {
		$('#advancedSearchOptions').slideUp('normal');
		SetCookie('ShowOptions',"",-1); //Erase the cookie
        document.getElementById('ShowOptions').value = "0";
    }
}

jQuery.fn.replace = function() {
    return this.domManip(arguments, true, 1, function(a){
        this.parentNode.replaceChild( a, this );
    });
};


//Returns an array created from the given country parameters.
function ParseCountryState(parameters)
{
//alert("parameters: " + parameters);
    parameters = parameters.replace("&Country=", "" );
    parameters = parameters.replace("&State=", "|" );
    parameters = parameters.replace("&StateLong=", "|" );
	
    return String(parameters).split("|");
	
}

function SetCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires=" + date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"=" + value + expires + "; path=/";
}