// Proper e-mail check script
function emailCheck( emailFormField, showerror ) {

	var txt=emailFormField.value;  
	var error = "";
	
	var emailRe = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/
	var phoneRe = /^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,5})|(\(?\d{2,6}\)?))(-| )?(\d{3,4})(-| )?(\d{4})(( x| ext)\d{1,5}){0,1}$/
                  
	if (!(emailRe.test(txt))) { 
		   error = "Please enter a valid email address.\n";
	}
	
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
	if (txt.match(illegalChars)) {
	   error += "The email address contains illegal characters.\n";
	}
	
	if( error.length > 0 ) {
		if(showerror) {
		    emailFormField.focus();
			alert(error);	
		}
		return false;
	}
	return true;  
}



/* Find a Luxe Hotel filter functions Start */
function setCityDefaultAs() {
	bindCitiesAs($('#state_id_as :selected').val());
	$('#state_id_as').attr('disabled', 'disabled');
	$('#state_id_as').resetSS();
	$('#select_state_id_as').hide();

}

function setStateDefaultAs() {
	$('#state_id_as').val(0);
	setTimeout("setCityDefaultAs()",100);
}

function bindStatesAs(country_id) {
	$('#state_id_as option, #city_id_as option').remove();
	$('#state_id_as').append('<option value="" class="nosel" selected="selected">Select state</option>').attr('disabled', 'disabled');
	$('#city_id_as').append('<option value="" class="nosel" selected="selected">Select city</option>').attr('disabled', 'disabled');

	if(typeof(json_cities[country_id]) != 'undefined') {
		$('#state_id_as').removeAttr('disabled', '');

		var items = json_cities[country_id].states;
		for (var o in items) {
			if (items[o].state) $('#state_id_as').append('<option value="' + o + '">' + items[o].state + '</option>');
		}
		if(items.length == 1) {
			$('#select_state_id_as').hide();
			setTimeout("setStateDefaultAs()",1);
		} else {
			$('#state_id_as').resetSS();
			$('#select_state_id_as').show();
			$('#city_id_as').resetSS();
			$('#select_city_id_as').hide();
		}
	} else {
		$('#state_id_as').resetSS();
		$('#select_state_id_as').hide();
		$('#city_id_as').resetSS();
		$('#select_city_id_as').hide();
	}
}


function bindCitiesAs(state_id) {
	state_id = state_id && state_id.length ? state_id : 0;
	var country_id = $('#country_id_as').val();

	$('#city_id_as option').remove();
	$('#city_id_as').append('<option value="" class="nosel" selected="selected">Select city</option>').attr('disabled', 'disabled');

	if(typeof(json_cities[country_id].states[state_id]) != 'undefined') {
		$('#city_id_as').removeAttr('disabled', '');

		var items = json_cities[country_id].states[state_id].cities;
		for (var o in items) {
			$('#city_id_as').append('<option value="'+o+'">' + items[o].city_name + '</option>');
		}
		$('#city_id_as').resetSS();

	} else {
		$('#city_id_as').resetSS();
	}
}

/* Find a Luxe Hotel filter functions End */
function setCityDefault() {
    bindCities($('#state_id :selected').val());
    $('#state_id').attr('disabled', 'disabled');
	$('#state_id').resetSS();
}
function setStateDefault() {
    $('#state_id').val(0);
    setTimeout("setCityDefault()",100);
}

function bindStates(country_id) {
    $('#state_id option, #City option').remove();
    $('#state_id, #City').append('<option value="" class="nosel" selected="selected">- Select -</option>').attr('disabled', 'disabled');
    $('#Dest').val('');
    
    if(typeof(json_cities[country_id]) != 'undefined') {
        $('#state_id').removeAttr('disabled', '');
        
        var items = json_cities[country_id].states;
        for (var o in items) $('#state_id').append('<option value="' + o + '">' + (items[o].state ? items[o].state : '- No state -') + '</option>');
        if(items.length == 1) {
            setTimeout("setStateDefault()",1);
		} else {
			$('#state_id').resetSS();
			$('#City').resetSS();
        }
	}
	$('#state_id').resetSS();
	$('#City').resetSS();
}

function bindCities(state_id) {
    var country_id = $('#country_id').val();
    $('#Dest').val('');
    $('#City option').remove();
    $('#City').append('<option value="" class="nosel" selected="selected">- Select -</option>').attr('disabled', 'disabled');
    
    if(typeof(json_cities[country_id].states[state_id]) != 'undefined') {
        $('#City').removeAttr('disabled', '');
        
        var items = json_cities[country_id].states[state_id].cities;
        for (var o in items) {
            $('#City').append('<option value="'+o+'">' + items[o].city_name + '</option>');
        }
        
    }
	$('#City').resetSS();
}

function bindRes(city_id) {
    var country_id = $('#country_id').val();
    var state_id = $('#state_id').val();
    $('#Dest').val('');
    if(typeof(json_cities[country_id].states[state_id].cities[city_id]) != 'undefined') {
        $('#Dest').val(json_cities[country_id].states[state_id].cities[city_id].syx);
    }
}

Date.format = 'mm/dd/yy';

$(function(){
	var cal;
	var $this;

	var checkForMouseout = function(event)
	{
		var el = event.target;

		while (true){
			if (el == cal) {
				return true;
			} else if (el == document) {
				$this.dpClose();
				return false;
			} else {
				el = $(el).parent()[0];
			}
		}
	};


	//home page

	$('.date-pick')
	   .datePicker({clickInput:true, createButton:true})
	   .bind(
			'dpDisplayed',
			function(event, datePickerDiv)
			{
				cal = datePickerDiv;
				$this = $(this);
				$(document).bind(
					'mouseover',
					checkForMouseout
				);
			}
		).bind(
			'dpClosed',
			function(event, selected)
			{
				$(document).unbind(
					'mouseover',
					checkForMouseout
				);
			}
		);
	$('#Arrive').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
				$('#Depart').dpSetStartDate(d.addDays(1).asString());
				$('#Depart').dpSetSelected(d.addDays(1).asString());
				$('#Depart').val(d.asString());
			}
		}
	);
	$('#Depart').bind(
		'dpClosed',
		function(e, selectedDates)
		{
			var d = selectedDates[0];
			if (d) {
				d = new Date(d);
			}
		}
	);
	$('#Arrive').trigger('change');

	LinkExtender.run({
		linkMarker: "synxis.com",
		urlParameters: ['template', 'chebs', 'src']
	});
	
	$("#mainmenu li").hover(
		function(){
			$(this).addClass("hover");
		},
		function(){
			$(this).removeClass("hover");
		});

	//link blog's title to actual blog
	var blog_container = $('#recent_posts').closest('.moduletable');
	$('h3:first', blog_container).css('cursor', 'pointer').click(function(){
		window.location.href = 'http://blog.luxehotels.com/';
	});

	//set states and cities dropdowns disabled by default for "Find a Luxe Hotel" filter
	$('#state_id_as, #city_id_as').attr('disabled', 'disabled');
	//set states and cities dropdowns disabled by default for "Find a Hotel" reservations widget
	$('#state_id, #City').attr('disabled', 'disabled');


	//IMPORTANT! init styled selects only after all <select> manipulations are complete
	$('#find-hotel-widget form select, .form-search form select').each(function(){
		var el = $(this);
		var is_visible = el.is(':visible');
		el.sSelect({
			ddMaxHeight: 243,
			defaultText: el.children().eq(0).text()
		});

		if (!is_visible) $('#select_'+el.attr('id')).hide();
	});

	//"Find a Luxe Hotel" filter	
	$('#country_id_as').change(function() {
		bindStatesAs(this.value);
	});
	$('#state_id_as').change(function() {
		bindCitiesAs(this.value);
	});
	$('#city_id_as').change(function() {
		$(this).closest('form').submit();
	});

	//"Find a Hotel" reservations widget
	$('#country_id').change(function() {
		bindStates(this.value);
	});
	$('#state_id').change(function() {
		bindCities(this.value);
	});
	$('#City').change(function() {
		bindRes(this.value);
	});

	/* initialize facebook like button */
	$('.home .contentpaneopen td h1:first').before($('<div>').attr('class', 'fb_like').attr('title', 'http://www.luxehotels.com'));
	
	var current_url = $('.fb_like').attr("title");
	
	$('.fb_like').html('<iframe src="http://www.facebook.com/plugins/like.php?href=' +current_url+ '&amp;layout=button_count&amp;show_faces=false&amp;width=91&amp;action=like&amp;colorscheme=light&amp;height=35" scrolling="no" frameborder="0" allowTransparency="true" class="facebook-like"></iframe>');

});
