//PRINT CAL
var chk_in = null;
var chk_out = null;
var request_page = null;

$(document).ready(function()
{
	var php_cal = "/includes/print_cal.php";
	if(no_out_date)
	{
		php_cal = "/includes/print_full_cal.php";
	}
	$(document).click(function(e)
	{
		if(!(
			$(e.target).parents().is(".check-in") 				||
			$(e.target).parents().is(".check-out") 				||
			$(e.target).parents().is(".check-in-ppc") 			||
			$(e.target).parents().is(".check-out-ppc")			||
			$(e.target).parents().is(".date_in")				||
			$(e.target).parents().is(".date_out")				||
			$(e.target).parents().is(".cal-ctrl")				||
			$(e.target).parents().is(".update-select-form-b")	||
			$(e.target).parents().is(".cal_main")				||
			$(e.target).parents().is(".ppn_cal_form")			||
			$(e.target).parents().is(".reservation-field")			||
			$(e.target).parents().is("#search-results-header") 	||
			$(e.target).parents().is("#res-login")))
		{
			$("#cal").hide();
		}
	});
	$(".cal_close").live("click", function(){ $("#cal").hide(); });
	$("#rs_chk_in").click(show_cal);
	$("#rs_chk_in_vis").click(show_cal);
	$("#rs_chk_out_vis").click(show_cal);
	$("#rs_chk_in_hotel").click(show_cal);
	$("#rs_chk_out_hotel").click(show_cal);
	$(".ppn_cal_showButton").click(show_cal);
	$(".btn-calendar").click(function()
	{
		$("#chk_" + this.id.split("-")[3] + "_txt").trigger("click");
		return false;
	});
	
	function show_cal(e)
	{
		if(this.id != "rs_chk_in")
		{
			$(this).css("display", "block");
		}
		var _id = this.id.split("_");
		$("#cal").hide(); 
		var el_height = $(this).css("height").replace(/px/, ""); 
		if(el_height == "auto")
		{
			el_height = 14;	
		}
		
		if(active_tab == "hotel")
		{
			var offset = $("a#cal_btn_" + _id[2]).offset(); 
		}
		else if(active_tab == "change")
		{
			var offset = $(this).parents().offset();
		}
		else
		{
			var offset = $("div.date_" + _id[2] + " select").offset();
		}
	
		if($(e.target).parents().parents().attr("id") == "")
		{ 
			$("#cal").css("left", Math.floor(offset.left + 1) + "px");
			$("#cal").css("top", Math.floor(offset.top + 1) + "px");
		}
		else if($(e.target).parents().parents().attr("id") == "search-form")
		{
			var cal_width = ($("#cal").css("width").replace(/px/, "")) * 1;
			var input_width = ($(this).css("width").replace(/px/, "")) * 1;
			$("#cal").css("left", Math.floor(offset.left + 1 - cal_width) + Math.floor(input_width) + "px");
			$("#cal").css("top", Math.floor(offset.top + 1) + "px");
		}
		else if(no_out_date)
		{
			offset = $("input#rs_chk_in").offset(); 
			input_width = ($(this).css("width").replace(/px/, "")) * 1;
			$("#cal").css("left", Math.floor(offset.left - 1) + "px");
			$("#cal").css("top", Math.floor(offset.top + 5) + Math.floor(el_height) + "px");
		}
		else if(active_tab == "change")
		{ 
			$("#cal").css("left", Math.floor(offset.left - 3) + "px");
			$("#cal").css("top", Math.floor(offset.top + 23) + "px");
		}

		if(no_out_date)
		{
			print_in_cal(this.id);
		}
		else
		{
			print_cal(_id[2]); 
		}
		$("#cal").css("display", "block").bgiframe();
	}
	
	function print_in_cal(el_id)
	{
		cal_side = "in";
		var in_date = $("#" + el_id).val();
		if(in_date == "")
		{
			in_date = "mm/dd/yyyy";	
		}
		var cur_date = new Date();
		var cur_month = cur_date.getMonth() + 1;
		var cur_year = cur_date.getYear();
		if(cur_year < 2000)
		{
			cur_year += 1900;	
		}
		var _in_date = in_date.split("/");  
		if(_in_date[0] == "MM" || _in_date[0] == "mm")
		{
			add_month = 0;
		}
		else if(_in_date[0] != "MM" && _in_date[0] != "mm")
		{
			add_month = _in_date[0] - cur_month - ( 12 * (cur_year - _in_date[2]));
			if(add_month == 0)
			{
				if(cur_year < _in_date[2])
				{
					add_month = 11;
				}
			}
		}
		else if(_in_date[0] == "MM" || _in_date[0] == "mm")
		{
			add_month = 0;
			if(cur_year < _in_date[2])
			{
				add_month = 11;
			}
		}

		if(_in_date[0] != "MM" && _in_date[0] != "mm" && add_month == 0 && _in_date[0] > cur_month) 
		{
			add_month = 11;
		}
		
		
	
		$(".cal_main").load(php_cal + "?add_month=" + add_month + "&cal_side=" + cal_side + "&in_date=" + in_date + "&out_date=mm/dd/yyyy",
		function()
		{
			$(this).find("select.select_month").change(function()
			{
				return redraw_cal(this, cal_side, add_month);
			});
		});
	}

	function print_cal(cal_side)
	{
		var add_month = 0; 
		var in_date = $("#rs_chk_in_vis").val();
		var out_date = $("#rs_chk_out_vis").val();
		var cur_date = new Date();
		var cur_month = cur_date.getMonth() + 1;
		var cur_year = cur_date.getYear() + 1900;
		var _in_date = in_date.split("/");
		var _out_date = out_date.split("/");	

		if((_in_date[0] == "MM" || _in_date[0] == "mm") && (_out_date[0] == "MM" || _out_date[0] == "mm"))
		{
			add_month = 0;
		}
		else if(_in_date[0] != "MM" && _in_date[0] != "mm" && (_out_date[0] == "MM" || _out_date[0] == "mm"))
		{
			add_month = _in_date[0] - cur_month;
			if(add_month < 0)
			{
				add_month += 12;
			}
			if(add_month == 0)
			{
				if(cur_year < _in_date[2])
				{
					add_month = 11;
				}
			}
		}
		else if((_in_date[0] == "MM" || _in_date[0] == "mm") && _out_date[0] != "MM" && _out_date[0] != "mm")
		{
			if(cal_side == "in")
			{
				add_month = 0;
			}
			else if(cal_side == "out")
			{
				add_month = _out_date[0] - cur_month;
				if(add_month < 0)
				{
					add_month += 12;
				}
			}

			if(add_month == 0)
			{
				if(cur_year < _in_date[2])
				{
					add_month = 11;
				}
			}
		}
		else if(_in_date[0] != "MM" && _in_date[0] != "mm" && _out_date[0] != "MM" && _out_date[0] != "mm")
		{
			if(cal_side == "in")
			{
				add_month = _in_date[0] - cur_month; 
			}
			else if(cal_side == "out")
			{
				add_month = _out_date[0] - cur_month;
			}

			if(add_month < 0)
			{
				add_month += 12;
			}
			if(add_month == 0)
			{
				if(cur_year < _in_date[2])
				{
					add_month = 11;
				}
			}
		}

		if(_in_date[0] != "MM" && _in_date[0] != "mm" && add_month == 0 && _in_date[0] > cur_month) 
		{
			add_month = 11;
		}
		
		//alert("?add_month=" + add_month + "&cal_side=" + cal_side + "&in_date=" + in_date + "&out_date=" + out_date);
		$(".cal_main").load(php_cal + "?add_month=" + add_month + "&cal_side=" + cal_side + "&in_date=" + in_date + "&out_date=" + out_date,
		function()
		{
			$(this).find("select.select_month").change(function()
			{
				return redraw_cal(this, cal_side, add_month);
			});
		});
	}
	
	function redraw_cal(el, cal_side, add_month)
	{
			var id = $(el).attr("id");
			var _id = id.split("_");
			var _val = $(el).val().split("_"); 
			add_month = _val[0];  
			var out_date = "";
			var in_date = ($("#rs_chk_in_vis").length == 0) ? $("#rs_chk_in").val() : $("#rs_chk_in_vis").val();
			if(!no_out_date)
			{
				out_date = ($("#rs_chk_out_vis").length == 0) ? $("#rs_chk_out").val() : $("#rs_chk_out_vis").val();
			}
			
			$(".cal_main").hide(); 
			$(".cal_main").load(php_cal + "?add_month=" + (add_month - 1) + "&cal_side=" + cal_side + "&in_date=" + in_date + "&out_date=" + out_date,
			function()
			{
				$(this).find("select.select_month").change(function()
				{
					return redraw_cal(this, cal_side, add_month);
				});
			});
			$(".cal_main").show();	
	}

	$(".cal_date").live("click", function(e)
	{
		var _ident = $(this).attr("class").split(" "); 
		if(this.id.length > 0 && !$(this).hasClass("cal_date_passed"))
		{
			var _date = this.id.split("_");
			var from = new Date();
			var to = new Date();
			
			var other_id = "";
			if(_date[4] == "in")
			{
				other_id = "out"
			}
			else if(_date[4] == "out")
			{
				other_id = "in";
			}
			
			if(!no_out_date)
			{
				var other_date = $("#rs_chk_" + other_id + "_vis").val();
				if(other_date != "MM/DD/YYYY" && other_date != "mm/dd/yyyy")
				{
					var _other_date = other_date.split("/");
					from.setFullYear(_date[1], _date[2] - 1, _date[3]);
					to.setFullYear(_other_date[2], _other_date[0] - 1, _other_date[1]);
					if(to <= from)
					{	
						$("#rs_chk_out_vis").val("mm/dd/yyyy");
					}
				}
			}
			if(!no_out_date)
			{
				$("#rs_chk_" + _date[4] + "_vis").val(_date[2] + "/" + _date[3] + "/" + _date[1]);
				$("#rs_chk_" + _date[4]).val(_date[2] + "/" + _date[3] + "/" + _date[1]);
			}
			else
			{
				$("#rs_chk_" + _date[4]).val(_date[2] + "/" + _date[3] + "/" + _date[1]);
			}
			$("#cal").hide();
		}
	});

	$(".cal_btn").live("click", function()
	{
		if(no_out_date)
		{
			$("div.cal_date_note").css("display", "none");	
		}
		if(this.id == "cal_in_btn" || this.id == "cal_out_btn")
		{
			$(".cal_main").show();
			return;
		}
		var _ident = $(this).attr("class").split(" ");
		var _val = $(".select_month").val().split("_"); 
		var _id = this.id.split("_");
		
		var out_date = "";
		var in_date = ($("#rs_chk_in_vis").length == 0) ? $("#rs_chk_in").val() : $("#rs_chk_in_vis").val();
		if(!no_out_date)
		{
			out_date = ($("#rs_chk_out_vis").length == 0) ? $("#rs_chk_out").val() : $("#rs_chk_out_vis").val();
		}
		//var in_date = $("#rs_chk_in_vis").val();
		//var out_date = $("#rs_chk_out_vis").val();
		var add_month = _val[0];
		if(_id[0] == "prev")
		{
			add_month -= (no_out_date) ? 1 : 2;
		}
		
		if(no_out_date && _id[0] == "next")
		{
			add_month = parseInt(add_month) + 1;
		}
		
		$(".cal_main").load(php_cal + "?add_month=" + add_month + "&cal_side=" + _ident[1] + "&in_date=" + in_date + "&out_date=" + out_date, 
		function()
		{
			$(this).find("select.select_month").change(function()
			{
				return redraw_cal(this, _ident[1], add_month);
			});
		});
		$(".cal_main").show();
	});
	
	$("div.cal_close_btn").live("click", function()
	{
		$("#cal").hide();
		return false;
	});
});


