//PRINT CAL
var chk_in = null;
var chk_out = null;
var request_page = null;
var no_out_date = false;

$(document).ready(function()
{
	var php_cal = "/includes/print_cal.php";
	$(document).click(function(e)
	{
		if(!($(e.target).parents().is(".subTile") || $(e.target).parents().is(".rs_chk_in_vis") || $(e.target).parents().is(".ppn_cal_showButton") || $(e.target).parents().is(".cal_page_left")))
		{
			$("#cal").hide();
		}
	});
	$(".cal_close").live("click", function()
	{ 
		$("#cal").hide(); 
	});
	
	$("#rs_chk_in, #rs_chk_out").click(show_cal);
	$(".ppn_cal_showButton").click(function()
	{
		$(this).parents().each(function()
		{
			if($(this).is(".subTile"))
			{
				$("input[type=text]", this).trigger("click");
			}
		});
	});
	$(".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("_");
		var el_height = $(this).css("height").replace(/px/, ""); 
		if(el_height == "auto")
		{
			el_height = 24;	
		}
		else
		{
			el_height = (el_height * 1) + 6;	
		}
		
		var offset = $("#" + this.id).offset(); 
		$(".cal_main").css("top", (offset.top + (el_height * 1)) + "px");
		$(".cal_main").css("left", offset.left + "px");
		print_cal(_id[2]); 
		$("#cal").css("display", "block").bgiframe();
	}
	
	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();
		
		///alert(in_date + " " + out_date);
		
		if(in_date == "")
		{
			in_date = "mm/dd/yyyy";	
		}
		
		if(out_date == "")
		{
			out_date = "mm/dd/yyyy";	
		}
		
		///alert(in_date + " " + out_date);
		
		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;
		}
	
		$(".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]);
			}
			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 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;
	});
});


