//Ensure the correct location value is submitted.
function IsValidForm()
{
	var value = $('#CountryState').val();
	if (value != null && value.indexOf('|') > -1){
		var arrTemp = value.split('|');
		var country = arrTemp[0];
		var stateAbbr = arrTemp[1];
		var stateName = arrTemp[2];

		$('#Country').val(country);
		$('#State').val(stateAbbr);
		$('#StateLong').val(stateName);

		var city = '';
		var latitude = '';
		var longitude = '';
		value = $('#CityLatLon').val()
		if(value != null && value.indexOf('|') > -1){
			arrTemp = value.split('|');
			city = arrTemp[0];
			latitude = arrTemp[1];
			longitude = arrTemp[2];
		}
		$('#City').val(city);
		$('#Lat').val(latitude);
		$('#Lon').val(longitude);
		
		if(city != null && city.length > 0){
			var term = city;
			if(stateName.length > 0)
				term += ', ' + stateName
			else if(stateAbbr.length > 0)
				term += ', ' + stateAbbr
			if(country.length > 0 && country != 'United States')
				term += ', ' + country;
			$('#location').val(term);
		}
		return true;
	}
	
	value = $('#location').val();
	if(value != null && value.length > 0){
		return true;
	}
	
	alert('To continue, please enter a location (city/town, postal code or landmark).');
	$('#location').css('border','2px solid orange').focus();
    return false;
}

//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 );
    });
};

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=/;';
}
