/*  Copyright 2009  |  http://aboutpro.com/
 * ---------------------------------------------------------------------------
 *
 * The Combo Style
 *
 * Details and latest version at:
 * http://aboutpro.com/
 *
 * This script is distributed under the GNU Lesser General Public License.
 * Read the entire license text here: http://www.gnu.org/licenses/lgpl.html
 *
 * This file defines helper functions for setting up the calendar.  They are
 * intended to help non-programmers get a working calendar on their site
 * quickly.  This script should not be seen as part of the calendar.  It just
 * shows you what one can do with the calendar, while in the same time
 * providing a quick and simple method for setting it up.  If you need
 * exhaustive customization of the calendar creation process feel free to
 * modify this code to suit your needs (this is recommended and much better
 * than modifying calendar.js itself).
 */
$(function(){
	// 
		   
	$("div.start").bind("mouseenter",function(){
	}).bind("mouseleave",function(){
	  $('#show_Date').addClass("hidden");
	  $('#show_Month').addClass("hidden");
	  $('#show_Year').addClass("hidden");
	});
	
	$("#date_a").bind("mouseenter",function(){
		$('#day_search-tip').removeClass("hidden");													
	}).bind("mouseleave",function(){
		$('#day_search-tip').addClass("hidden");
	});
	
	$("#month_a").bind("mouseenter",function(){
		$('#month_search-tip').removeClass("hidden");													
	}).bind("mouseleave",function(){
		$('#month_search-tip').addClass("hidden");
	});
	
	$("#year_a").bind("mouseenter",function(){
		$('#year_search-tip').removeClass("hidden");													
	}).bind("mouseleave",function(){
		$('#year_search-tip').addClass("hidden");
	});
	
	$("#date_a").click(function(){
		if ($('#show_Date').hasClass("hidden")){  
			$('#show_Date').removeClass("hidden");
			
			if ($('#show_Year').hasClass("hidden")){}
			else {$('#show_Year').addClass("hidden");}
			if ($('#show_Month').hasClass("hidden")){}
			else {$('#show_Month').addClass("hidden");}
		}
		else {
			$('#show_Date').addClass("hidden");	
		}
	});	
	$("#month_a").click(function(){
		if ($('#show_Month').hasClass("hidden")){  
			$('#show_Month').removeClass("hidden");
			
			if ($('#show_Year').hasClass("hidden")){}
			else {$('#show_Year').addClass("hidden");}
			if ($('#show_Date').hasClass("hidden")){}
			else {$('#show_Date').addClass("hidden");}
		}
		else {
			$('#show_Month').addClass("hidden");	
		}
	});	
	$("#year_a").click(function(){
		if ($('#show_Year').hasClass("hidden")){  
			$('#show_Year').removeClass("hidden");
			
			if ($('#show_Month').hasClass("hidden")){}
			else {$('#show_Month').addClass("hidden");}
			if ($('#show_Date').hasClass("hidden")){}
			else {$('#show_Date').addClass("hidden");}
		}
		else {
			$('#show_Year').addClass("hidden");	
		}
	});	
	
	$("#show_Date li").click(function() {
										  
		var text = $(this).text();
		
		$("#day_search").val(text);//set value for hidden Input
		
		$("#val-day_search").text(text);		
		$('#show_Date').addClass("hidden");
		
		//
		var date_current = $('#val-day_search').text();
		var date_out = parseInt(date_current)+3;
		if(date_out<10)
			date_out = '0' +date_out;
		if(date_out>31){
			date_out = date_out - 31;
			var month_current = $('#val-month_search_out').text();
			var month_out = parseInt(month_current)+1;
			if(month_out<10)
				month_out = '0' +month_out;
			if(month_out>12){
				month_out = month_out - 12;
				var year_current = $('#val-year_search_out').text();
				var year_out = parseInt(year_current)+1;
				$('#val-year_search_out').text(year_out);
				$("#year_search_out").val(year_out);//set value for hidden Input
			}
			$('#val-month_search_out').text(month_out);
			$("#month_search_out").val(month_out);//set value for hidden Input
		}
		$('#val-day_search_out').text(date_out);
		$("#day_search_out").val(date_out);//set value for hidden Input
	});
	
	$("#show_Month li").click(function() {
		var text = $(this).text();
		$("#month_search").val(text);//set value for hidden Input
		$("#val-month_search").text(text);
		$('#show_Month').addClass("hidden");
		var month_current = $('#val-month_search').text();
		var day_current = $('#val-day_search').text();
		var year_current = $('#val-year_search').text();
		
		$('#val-month_search_out').text(month_current);
		var num_day = getDaysInMonth(month_current,year_current);
		if(num_day==28){
			$("#show_Date li:nth-child(29)").addClass('invisible');
			$("#show_Date li:nth-child(30)").addClass('invisible');
			$("#show_Date li:nth-child(31)").addClass('invisible');
		}
		if(num_day==29){
			$("#show_Date li:nth-child(29)").removeClass('invisible');
			$("#show_Date li:nth-child(30)").addClass('invisible');
			$("#show_Date li:nth-child(31)").addClass('invisible');
		}
		if(num_day==30){
			$("#show_Date li:nth-child(29)").removeClass('invisible');
			$("#show_Date li:nth-child(30)").removeClass('invisible');
			$("#show_Date li:nth-child(31)").addClass('invisible');
		}
		if(num_day==31){
			$("#show_Date li:nth-child(29)").removeClass('invisible');
			$("#show_Date li:nth-child(30)").removeClass('invisible');
			$("#show_Date li:nth-child(31)").removeClass('invisible');
		}
		
	});
	$("#show_Year li").click(function() {
		var text = $(this).text();
		$("#year_search").val(text);//set value for hidden Input
		$("#val-year_search").text(text);
		$('#show_Year').addClass("hidden");
		var year_current = $('#val-year_search').text();
		var month_current = $('#val-month_search').text();
		var day_current = $('#val-day_search').text();
		//
		$('#val-year_search_out').text(year_current);
		//
		var num_day = getDaysInMonth(month_current,year_current);
		if(num_day==28){
			$("#show_Date li:nth-child(29)").addClass('invisible');
			$("#show_Date li:nth-child(30)").addClass('invisible');
			$("#show_Date li:nth-child(31)").addClass('invisible');
		}
		if(num_day==29){
			$("#show_Date li:nth-child(29)").removeClass('invisible');
			$("#show_Date li:nth-child(30)").addClass('invisible');
			$("#show_Date li:nth-child(31)").addClass('invisible');
		}
		if(num_day==30){
			$("#show_Date li:nth-child(29)").removeClass('invisible');
			$("#show_Date li:nth-child(30)").removeClass('invisible');
			$("#show_Date li:nth-child(31)").addClass('invisible');
		}
		if(num_day==31){
			$("#show_Date li:nth-child(29)").removeClass('invisible');
			$("#show_Date li:nth-child(30)").removeClass('invisible');
			$("#show_Date li:nth-child(31)").removeClass('invisible');
		}
	});		
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////
	//Select Type Combo Box
	$("div.start_select_type").bind("mouseenter",function(){
	}).bind("mouseleave",function(){
	  $('#show_select_type').addClass("hidden");
	});
	
	$("#select_type_a").click(function(){
		if ($('#show_select_type').hasClass("hidden")){  
			$('#show_select_type').removeClass("hidden");
		}
		else {
			$('#show_select_type').addClass("hidden");	
		}
	});	
	
	$("#show_select_type li").click(function() {
		var text = $(this).text();
		var accommodation_id = $(this).attr("class");
		$("#select_type_search").val(accommodation_id);//set value for hidden Input
		
		$("#val-select_type_search").text(text);
		$('#show_select_type').addClass("hidden");
	});
	//
	$("div.start_room").bind("mouseenter",function(){
	}).bind("mouseleave",function(){
	  $('#show_room').addClass("hidden");
	});
	
	$("#room_a").click(function(){
		if ($('#show_room').hasClass("hidden")){  
			$('#show_room').removeClass("hidden");
		}
		else {
			$('#show_room').addClass("hidden");	
		}
	});	
	
	$("#show_room li").click(function() {
		var text = $(this).text();
		$("#room_search").val(text);//set value for hidden Input
		$("#val-room_search").text(text);
		$('#show_room').addClass("hidden");
	});
	
	////////////====================start_out===============================/////////////////
	$("div.start_out").bind("mouseenter",function(){
	}).bind("mouseleave",function(){
	  $('#show_Date_out').addClass("hidden");
	  $('#show_Month_out').addClass("hidden");
	  $('#show_Year_out').addClass("hidden");
	});
	
	$("#date_a_out").bind("mouseenter",function(){
		$('#day_search-tip_out').removeClass("hidden");													
	}).bind("mouseleave",function(){
		$('#day_search-tip_out').addClass("hidden");
	});
	
	$("#month_a_out").bind("mouseenter",function(){
		$('#month_search-tip_out').removeClass("hidden");													
	}).bind("mouseleave",function(){
		$('#month_search-tip_out').addClass("hidden");
	});
	
	$("#year_a_out").bind("mouseenter",function(){
		$('#year_search-tip_out').removeClass("hidden");													
	}).bind("mouseleave",function(){
		$('#year_search-tip_out').addClass("hidden");
	});
	
	$("#date_a_out").click(function(){
		if ($('#show_Date_out').hasClass("hidden")){  
			$('#show_Date_out').removeClass("hidden");
			
			if ($('#show_Year_out').hasClass("hidden")){}
			else {$('#show_Year_out').addClass("hidden");}
			if ($('#show_Month_out').hasClass("hidden")){}
			else {$('#show_Month_out').addClass("hidden");}
		}
		else {
			$('#show_Date_out').addClass("hidden");	
		}
	});	
	$("#month_a_out").click(function(){
		if ($('#show_Month_out').hasClass("hidden")){  
			$('#show_Month_out').removeClass("hidden");
			
			if ($('#show_Year_out').hasClass("hidden")){}
			else {$('#show_Year_out').addClass("hidden");}
			if ($('#show_Date_out').hasClass("hidden")){}
			else {$('#show_Date_out').addClass("hidden");}
		}
		else {
			$('#show_Month_out').addClass("hidden");	
		}
	});	
	$("#year_a_out").click(function(){
		if ($('#show_Year_out').hasClass("hidden")){  
			$('#show_Year_out').removeClass("hidden");
			
			if ($('#show_Month_out').hasClass("hidden")){}
			else {$('#show_Month_out').addClass("hidden");}
			if ($('#show_Date_out').hasClass("hidden")){}
			else {$('#show_Date_out').addClass("hidden");}
		}
		else {
			$('#show_Year_out').addClass("hidden");	
		}
	});	
	
	$("#show_Date_out li").click(function() {
		var text = $(this).text();
		$("#day_search_out").val(text);//set value for hidden Input
		$("#val-day_search_out").text(text);
		$('#show_Date_out').addClass("hidden");
		//
		checkRange();
	});
	$("#show_Month_out li").click(function() {
		var text = $(this).text();
		$("#month_search_out").val(text);//set value for hidden Input
		$("#val-month_search_out").text(text);
		$('#show_Month_out').addClass("hidden");
		//
		var month_current = $('#val-month_search_out').text();
		var day_current = $('#val-day_search_out').text();
		var year_current = $('#val-year_search_out').text();
		var num_day = getDaysInMonth(month_current,year_current);
		if(num_day==28){
			$("#show_Date_out li:nth-child(29)").addClass('invisible');
			$("#show_Date_out li:nth-child(30)").addClass('invisible');
			$("#show_Date_out li:nth-child(31)").addClass('invisible');
		}
		if(num_day==29){
			$("#show_Date_out li:nth-child(29)").removeClass('invisible');
			$("#show_Date_out li:nth-child(30)").addClass('invisible');
			$("#show_Date_out li:nth-child(31)").addClass('invisible');
		}
		if(num_day==30){
			$("#show_Date_out li:nth-child(29)").removeClass('invisible');
			$("#show_Date_out li:nth-child(30)").removeClass('invisible');
			$("#show_Date_out li:nth-child(31)").addClass('invisible');
		}
		if(num_day==31){
			$("#show_Date_out li:nth-child(29)").removeClass('invisible');
			$("#show_Date_out li:nth-child(30)").removeClass('invisible');
			$("#show_Date_out li:nth-child(31)").removeClass('invisible');
		}
		//
		checkRange();
	});
	$("#show_Year_out li").click(function() {
		var text = $(this).text();
		$("#year_search_out").val(text);//set value for hidden Input
		$("#val-year_search_out").text(text);
		$('#show_Year_out').addClass("hidden");
		//
		var year_current = $('#val-year_search_out').text();
		var month_current = $('#val-month_search_out').text();
		var day_current = $('#val-day_search_out').text();
		var num_day = getDaysInMonth(month_current,year_current);
		if(num_day==28){
			$("#show_Date_out li:nth-child(29)").addClass('invisible');
			$("#show_Date_out li:nth-child(30)").addClass('invisible');
			$("#show_Date_out li:nth-child(31)").addClass('invisible');
		}
		if(num_day==29){
			$("#show_Date_out li:nth-child(29)").removeClass('invisible');
			$("#show_Date_out li:nth-child(30)").addClass('invisible');
			$("#show_Date_out li:nth-child(31)").addClass('invisible');
		}
		if(num_day==30){
			$("#show_Date_out li:nth-child(29)").removeClass('invisible');
			$("#show_Date_out li:nth-child(30)").removeClass('invisible');
			$("#show_Date_out li:nth-child(31)").addClass('invisible');
		}
		if(num_day==31){
			$("#show_Date_out li:nth-child(29)").removeClass('invisible');
			$("#show_Date_out li:nth-child(30)").removeClass('invisible');
			$("#show_Date_out li:nth-child(31)").removeClass('invisible');
		}
		//
		checkRange();
	});
	//
	function checkRange(){
		//check valid range
		var m_i = $('#val-month_search').text();
		var d_i = $('#val-day_search').text();
		var y_i = $('#val-year_search').text();
		
		var m_o = $('#val-month_search_out').text();
		var d_o = $('#val-day_search_out').text();
		var y_o = $('#val-year_search_out').text();
		//
		$("#year_search_out").val(y_o);//set value for hidden Input
		$("#month_search_out").val(m_o);//set value for hidden Input
		$("#day_search_out").val(d_o);//set value for hidden Input
		
		$("#year_search").val(y_i);//set value for hidden Input
		$("#month_search").val(m_i);//set value for hidden Input
		$("#day_search").val(d_i);//set value for hidden Input
		//
		if(checkValidRangeTime(d_i,m_i,y_i,d_o,m_o,y_o))
			return true;
		else{
			alert("You must choose checkout date greater than checkin date!");
			return false;
		}
	}
	//
	function checkValidRangeTime(fd,fm,fy,td,tm,ty)
	{
		mkDate_f = new Date(fm+"/"+fd+"/"+fy);
		mkDate_t = new Date(tm+"/"+td+"/"+ty);
		if(mkDate_f>mkDate_t)
			return false;
		return true;			
	}
	//Function count number day in month of year
	function getDaysInMonth(intMon,intYr)
	{
		switch(parseInt(intMon,10))
		{
			case 2:
				if ((intYr%100)==0)
					if ((intYr%400)==0) 
						return 29;
					else
						return 28;
				else if((intYr%4)==0)
						return 29;
				else
					return 28;
				break;
			case 4:
				return 30
				break;
			case 6:
				return 30
				break;
			case 9:
				return 30
				break;
			case 11:
				return 30
				break;
			default:
				return 31;
				break;
		}
	}
	//
	$('#btn_submit').click(function(){	
		if(checkRange())									
			$('#frmQuickReservation').submit();			
		else
			return false;
	});
	//#################Adult Combo#######################
	$("div.start_adult").bind("mouseenter",function(){
	}).bind("mouseleave",function(){
	  $('#show_adult').addClass("hidden");
	});
	$("#adult_a").click(function(){
		if ($('#show_adult').hasClass("hidden")){  
			$('#show_adult').removeClass("hidden");
		}
		else {
			$('#show_adult').addClass("hidden");	
		}
	});	
	
	$("#show_adult li").click(function() {
		var text = $(this).text();
		$("#adult_search").val(text);//set value for hidden Input
		$("#val-adult_search").text(text);
		$('#show_adult').addClass("hidden");
	});
	//#################End Adult Combo#######################
	//#################Children Combo#######################
	$("div.start_children").bind("mouseenter",function(){
	}).bind("mouseleave",function(){
	  $('#show_children').addClass("hidden");
	});
	$("#children_a").click(function(){
		if ($('#show_children').hasClass("hidden")){  
			$('#show_children').removeClass("hidden");
		}
		else {
			$('#show_children').addClass("hidden");	
		}
	});	
	
	$("#show_children li").click(function() {
		var text = $(this).text();
		$("#children_search").val(text);//set value for hidden Input
		$("#val-children_search").text(text);
		$('#show_children').addClass("hidden");
	});
	//#################End Adult Combo#######################
	$('#btn_submit_reservation').click(function(){	
		//alert('Your room number need is greater than our minimum number room for this range');return false;												
		var min_allot = parseInt($('#min_allot').text());
		var current_allot = parseInt($('#val-room_search').text());
		
		if(min_allot<current_allot){
			alert('Your room number need is greater than our minimum number room for this range');
			return false;
		}
		else{
			if(checkRange())									
				$('#frmQuickReservation').submit();			
			else
				return false;
		}
	});
	///////////////////////////////////
	
});