$(document).ready(function(){
	$('#city_search').change(function(){
		if ($(this).val() == "")
		{			
			$('#area_search').resetSelect(lblSelectArea).disabled(); 
			$('#hotel_search').resetSelect(lblSelectHotel).disabled(); 
		}
		else 
		{
			$('#area_search').showLoading(lblLoading); 
			$.getJSON(
				'/search_available/get_area/' + $(this).val() + '/' + Math.random(), 
				{},
				function(data){
					if (data != null)
					{
						$('#area_search').enabled(); 
						$('#area_search').loadSelect(data, lblSelectArea); 
					}
					else 
					{
						$('#area_search').resetSelect(lblSelectArea).disabled(); 
					}					
				}
			); 

			$('#hotel_search').showLoading(lblLoading); 
			$.getJSON(
				'/search_available/get_hotel_by_city/' + $(this).val() + '/' + Math.random(),
				{},
				function (data){
					$('#hotel_search').enabled(); 
					$('#hotel_search').loadSelect(data, lblSelectHotel); 
				}

			); 
		}
	}); 

	$('#area_search').change(function(){
		if ($(this).val() == "")
		{		
			$.getJSON(
				'/search_available/get_hotel_by_city/' + $('#city_search').val() + '/' + Math.random(),
				{}, 
				function (data){
					$('#hotel_search').enabled(); 
					$('#hotel_search').loadSelect(data, lblSelectHotel); 
				}
			); 
		}
		else 
		{
			$('#hotel_search').disabled(); 
			$('#hotel_search').showLoading(lblLoading); 
			$.getJSON(
				'/search_available/get_hotel/' + $(this).val() + '/' + Math.random(),
				{},
				function(data){
					$('#hotel_search').enabled(); 
					$('#hotel_search').loadSelect(data, lblSelectHotel);
				}
			); 
		}		
	}); 

	$('#area_search').attr('disabled', true).addClass('disabled_elem'); 
	$('#hotel_search').attr('disabled', true).addClass('disabled_elem'); 

	// Auto Loading to Drop Down Search Available 
	
	// If page name is 'Hotel Search Result' or 'Hotel Factsheet' or Hotel Types' or 'Hotel Gateway' show hotel select box too.
	if (  rsegment1 == "hotel_search_result" ||  /** Search Result **/
		  rsegment1 == "hotel"  ||  /** Hotel Factsheet **/ 
		  rsegment1 == "hotel_gateway" ||  /* Hotel Gateway Set */
		  rsegment1 == "travel_info"
		)
	{
		$('#city_search').showLoading(lblLoading); 
		
		$.getJSON(
			'/search_available/get_city/' + $.cookie('ck_country_search') + '/' + Math.random(), 
			{},
			function(data){		
				if (data != null)
				{
					$('#city_search').enabled(); 
					$('#city_search').loadSelect(data, lblSelectCity, $.cookie('ck_city_search')); 
				}
				else 
				{
					$('#city_search').resetSelect(lblSelectCity); 
					$('#area_search').resetSelect(lblSelectArea).disabled(); 
					$('#hotel_search').resetSelect(lblSelectHotel).disabled(); 
				}		
			}
		);			

		if ($.cookie('ck_city_search') != null)
		{
	
			$('#area_search').showLoading(lblLoading); 
			
			$.getJSON(
				'/search_available/get_area/' + $.cookie('ck_city_search') + '/' + Math.random(), 
				{},
				function(data){
					if (data != null)
					{
						$('#area_search').enabled(); 
						$('#area_search').loadSelect(data, lblSelectArea, $.cookie('ck_area_search')); 
					}
					else 
					{
						$('#area_search').resetSelect(lblSelectArea).disabled(); 
					}					
				}
			); 
		}


		// If page is not hotel gateway COUNTRY LEVEL ( Because don't have citiesID OR areaID , cannot retrieve the hotel into selectbox)
		if (rsegment2 != "country")
		{
			$('#hotel_search').showLoading(lblLoading); 
			var hotel_location_id; 
			if ($.cookie('ck_area_search') != null && $.cookie('ck_area_search') != "")
			{
				hotel_location_id = $.cookie('ck_area_search'); 
			}
			else 
			{
				hotel_location_id = $.cookie('ck_city_search'); 
			}

			$.getJSON(
				'/search_available/get_hotel_by_city/' + hotel_location_id + '/' + Math.random(),
				{},
				function (data){
					$('#hotel_search').enabled(); 
					$('#hotel_search').loadSelect(data, lblSelectHotel, $.cookie('ck_hotel_search')); 
				}
			); 			
		}
		
	}
	// In case: Other Page is not below case (included home page)
	else 
	{
		$('#city_search').showLoading(lblLoading); 
		// automatic Load - Get City to select box
		$.getJSON(
			'/search_available/get_city/9/' + Math.random(), 
			null,
			function(data){
				$('#city_search').loadSelect(data, lblSelectCity, $.cookie('ck_city_search'))
			}
		);

		

	}

	$('#package_city_search').showLoading(lblLoading); 
		
		// automatic Load - Get City to select box
	$.getJSON(
		'/search_available/get_province/' + Math.random(), 
		null,
		function(data){
			$('#package_city_search').loadSelect(data, lblSelectCity)
		}
	);

	$('#promotion_city_search').showLoading(lblLoading); 
	// automatic Load - Get City to select box
	$.getJSON(
		'/search_available/get_province/' + Math.random(), 
		null,
		function(data){
			$('#promotion_city_search').loadSelect(data, lblSelectCity)
		}
	);

	if (rsegment1 == "search_keyword" && rsegment2 == "hotel")
	{
		$('#chk_search_keyword').attr('checked', true); 
		$('#txtKeyword').enabled(); 
		$('#keyword_part').enabled(); 
		$('#keyword_all').enabled(); 
		$('#txtKeyword').val($.cookie('ck_search_hotel_keyword'));
		if ($.cookie('ck_search_type') == "Part")
		{
			$('#keyword_part').attr('checked', true); 
		}
		else 
		{
			$('#keyword_all').attr('checked', true); 
		}
	}
	// Validate Search Form 
	$('#frmSearchHotel').submit(function(){
	
		var isError = false; 

		if ($('#chk_search_keyword').attr('checked') == true)
		{

			if ($('#txtKeyword').val() == "")
			{
				alert($('#txtKeyword').attr('title'));
			}
			else 
			{
				$.cookie('ck_search_hotel_keyword', $('#txtKeyword').val(), {path:'/'}); 
				if ($('#keyword_part').attr('checked'))
				{
					var search_type = $('#keyword_part').val(); 
				}
				else 
				{
					var search_type = $('#keyword_all').val(); 
				}
				$.cookie('ck_search_type', search_type, {path:'/'}); 
				$.post(
					'/search_available/get_search_hotel_by_keyword_url', 
					{},
					function (data){
						window.location.href = data; 
					},
					'html'
				); 
			}
			return false; 
		}
		
		else 
		{
			$.cookie('ck_search_hotel_keyword', null, {path:'/'}); 
			$.cookie('ck_search_type', null, {path:'/'}); 
			$(this).find('input, select, textarea').each(function(){
				if ($(this).hasClass('search_required'))
				{
					if ($(this).val() == "")
					{
						alert($(this).attr('title')); 
						$(this).focus(); 
						isError = true; 
						return false; 
					}
				}
			}); 

			/*
			if (is_not_more($('#checkout_date_search').val(), get_date_after($('#checkin_date_search').val(),  31)))
			{
				alert("Checkout is less than 30 days from checkin WOW !!!"); 
			}
			*/

			// Invalid Form 
			if (isError == true)
			{
				return false; 
			}
			else 
			{
				// Set Cookie before submit 

				$.cookie('ck_checkin_search', $('#checkin_date_search').val(), {path: '/'});
				$.cookie('ck_checkout_search', $('#checkout_date_search').val(), {path: '/'});
				
				// If select hotel search 
				if ($('#hotel_search').val() != "")
				{				
					$.get(
						'/search_available/get_hotel_url/' + $('#hotel_search').val() + '/' + Math.random(),
						null,
						function(data){
							window.location.href = data; 
						}
					); 
					return false; 
				}

				else if ($('#area_search').val() != "")
				{
					$.get(
						'/search_available/get_search_by_area_url/' + $('#area_search').val() + '/' + Math.random(), 
						null,
						function(data){
							window.location.href = data; 
						}					
					); 

					return false; 
				}
				else if ($('#city_search').val() != "")
				{
					$.get(
						'/search_available/get_search_by_city_url/' + $('#city_search').val() + '/' + Math.random(), 
						null,
						function(data){
							window.location.href = data; 
						}					
					); 

					return false; 
				}
				else
				{
					return false; 
				}
			}
		}
	}); 

	$('#frmSearchPackage').submit(function(){
		
		$.cookie('ck_search_package_keyword', $('#txtPackageKeyword').val(), {path:'/'}); 
		if ($('#package_keyword_part').attr('checked'))
		{
			var search_type = $('#package_keyword_part').val(); 
		}
		else 
		{
			var search_type = $('#package_keyword_all').val(); 
		}
		$.cookie('ck_search_package_type', search_type, {path:'/'}); 
		$.cookie('ck_province_search', $('#package_city_search').val(), {path:'/'}); 
		$.post(
			'/search_available/get_search_package_by_keyword_url', 
			{},
			function (data){
				window.location.href = data; 
			},
			'html'
		); 
		
		return false; 
		
	}); 

	$('#frmSearchPromotion').submit(function(){
		
		$.cookie('ck_search_promotion_keyword', $('#txtPromotionKeyword').val(), {path:'/'}); 
		if ($('#promotion_keyword_part').attr('checked'))
		{
			var search_type = $('#promotion_keyword_part').val(); 
		}
		else 
		{
			var search_type = $('#promotion_keyword_all').val(); 
		}
		$.cookie('ck_search_promotion_type', search_type, {path:'/'}); 
		$.cookie('ck_province_search', $('#promotion_city_search').val(), {path:'/'}); 
		$.post(
			'/search_available/get_search_promotion_by_keyword_url', 
			{},
			function (data){
				window.location.href = data; 
			},
			'html'
		); 
		
		return false; 
		
	}); 

	$('input.check_rate').click(function(){
		window.location.href = $(this).parent().parent().siblings('div.hotel_info').children().children('a.check_rate').attr('href'); 

	}); 

	$('#cboSearchRooms').change(function(){
		
		for (var i = 0; i < $(this).val(); i++)
		{
			$(this).parent('li').parent('ul').parent().append('<ul class="search"><li>&nbsp;</li><li>Col 2</li><li>Col 3</li><li class="clear"></li></ul>'); 
		}
		
	}); 

	$('#chk_search_keyword').click(function(){
			
		if ($(this).attr('checked') == true)
		{
			$('#search_hotel_by_keyword').find('input.keyword').each(function(){
				$(this).attr('disabled', false); 
			
			});
		}
		else 
		{
			$('#search_hotel_by_keyword').find('input.keyword').each(function(){
				$(this).attr('disabled', true); 
			}); 		
		
		}
	}); 

	$("#checkin_date_search").datepicker({
		showOn: 'button', 
		buttonImage: '/resources/images/calendar.gif', 
		buttonImageOnly: true,
		beforeShow: customDateSearchFunction,
	    onSelect: function(dateText, inst) { 
			$('#checkout_date_search').val(get_date_after(dateText, 1)); 
		}		
	});


	$('#checkin_date_search').datepicker('option', {dateFormat: 'dd/mm/yy'}); 
	$('#checkin_date_search').datepicker($.datepicker.regional[curr_lang]); 
	
	
	
	$("#checkout_date_search").datepicker({
		showOn: 'button', 
		buttonImage: '/resources/images/calendar.gif', 
		buttonImageOnly: true,
		beforeShow: customDateSearchFunction

	});
	$('#checkout_date_search').datepicker('option', {dateFormat: 'dd/mm/yy'}); 
	
	//$('#checkin_date_search').val($.cookie('ck_checkin_search'));
	//$('#checkout_date_search').val($.cookie('ck_checkout_search')); 

	// If cookie is null , Do not input NULL value into textbox
	if ($.cookie('ck_checkin_search') != null)
	{
		$('#checkin_date_search').val($.cookie('ck_checkin_search')); 
	}
	if ($.cookie('ck_checkout_search') != null)
	{
		$('#checkout_date_search').val($.cookie('ck_checkout_search')); 
	}
	
}); 

/*
use the dateMin and dateMax attributes to enable
the date selection to not end before the start date and 
not start before the end date
*/
function customDateSearchFunction(input) 
{   
	// if the button called is checkin
	// set the maxDate to the checkout date
	if (input.id == "checkin_date_search") 
    {
		var dateMax; 
		if ($("#checkout_date_search").datepicker("getDate") != null) 
		{
			dateMax = $("#checkout_date_search").datepicker("getDate"); 
		}
		//return {maxDate: dateMax, minDate: new Date()};                     
		return {minDate: new Date()}; 
	}
	else if (input.id == "checkout_date_search")
	{ 
		var dateMin; 
		//if ($("#txtCheckInRate").datepicker("getDate") != null)
		//	dateMin = $("#txtCheckInRate").datepicker("getDate"); 

		var tmpCheckOut = $('#checkout_date_search').val(); 
		$('#checkout_date_search').val(get_date_after($('#checkin_date_search').val(), 1)); 

		dateMin = $('#checkout_date_search').datepicker('getDate'); 

		$('#checkout_date_search').val(tmpCheckOut); 
		return { minDate: dateMin  }; 
	}
}

(function($) {
	$.fn.emptyTest = function(){
		return this.each(function(){
			this.options.length = 0; 
		}); 
	}
	$.fn.resetSelect = function(text) {
		return this.emptySelect().each(function(){
			if (this.tagName == "SELECT")
			{
				var selectElement = this; 
				var option = new Option(text, ''); 

				if ($.browser.msie)
				{
					selectElement.add(option);
				}
				else 
				{
					selectElement.add(option, null); 
				}
			}
		}); 
	}
	$.fn.showLoading = function(text) {
		return this.emptySelect().each(function(){
			if (this.tagName == "SELECT")
			{
				var selectElement = this; 
				var option = new Option(text, ''); 

				if ($.browser.msie)
				{
					selectElement.add(option);
				}
				else 
				{
					selectElement.add(option, null); 
				}
				
			}
		}); 
	}
	$.fn.disabled = function() {
		this.attr('disabled', true); 
		this.addClass('disabled_elem'); 
	}
	$.fn.enabled = function() {
		this.attr('disabled', false); 
		this.removeClass('disabled_elem'); 
	}

	$.fn.emptySelect = function() {
		return this.each(function(){
			if (this.tagName=='SELECT') this.options.length = 0;
		});
	}
	$.fn.loadSelect = function(optionsDataArray, lblFirst, currentValue) {
		return this.emptySelect().each(function(){
			if (this.tagName=='SELECT') {
				var selectElement = this;
				if (lblFirst != "")
				{
					//selectElement.add(new Option(lblFirst, ""));
					var option = new Option(lblFirst, ""); 
					if ($.browser.msie)
						selectElement.add(option); 
					else 
						selectElement.add(option, null); 
				}		
				if (optionsDataArray != null)
				{
					$.each(optionsDataArray,function(index,optionsData){		
						if (optionsData.opt_value == currentValue)
						{
							var option = new Option(optionsData.opt_text, optionsData.opt_value);
							option.selected = true
						}
						else
						{
							var option = new Option(optionsData.opt_text, optionsData.opt_value);
						}
						if ($.browser.msie) {
							selectElement.add(option);
						}
						else {
							selectElement.add(option,null);
						}
					});					
				}			
			}
		});
	}
	$.fn.loadSelectHotel = function(optionsDataArray, lblFirst) {
		return this.emptySelect().each(function(){
			if (this.tagName=='SELECT') {
				var selectElement = this;
				if (lblFirst != "")
				{
					//selectElement.add(new Option(lblFirst, ""));
					var option = new Option(lblFirst, ""); 
					if ($.browser.msie)
						selectElement.add(option); 
					else 
						selectElement.add(option, null); 
				}		
				if (optionsDataArray != null)
				{
					$.each(optionsDataArray,function(index,optionsData){		
						if (optionsData.opt_value == $.cookie('ck_hotel_search'))
						{
							var option = new Option(optionsData.opt_text, optionsData.opt_value);
							option.selected = true
						}
						else
						{
							var option = new Option(optionsData.opt_text, optionsData.opt_value);
						}
						if ($.browser.msie) {
							selectElement.add(option);
						}
						else {
							selectElement.add(option,null);
						}
					});
					
				}

				
			}
		});
	}
})(jQuery);

