var today = new Date();
var fromDate = today;
var toDate = today;

var monthNames = "January^February^March^April^May^June^July^August^September^October^November^December^";
var dayNames = "sun^mon^tue^wed^thu^fri^sat";

var fromOffset = 0;
var toOffset = 0;

var finalDate = new Date();
finalDate.setDate(new Date().getDate()+330);
finalDate = (finalDate.getMonth() + 1) + "/" + (finalDate.getDate() + 1) + "/" + (finalDate.getFullYear() + 1);


function doErrorCheck()
{
	if ( (toDate < fromDate) )
	{
		var tempDate = new Date ( fromDate );
		tempDate.setDate ( fromDate.getDate() + 1 );
		$('#rs_chk_out_vis').val("mm/dd/yyyy");
        toOffset = ( tempDate.getMonth() - today.getMonth() ) + ( 12 * ( tempDate.getFullYear() - today.getFullYear() ));
		$.get("/includes/double_cal/printDoubleCal.php?inDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&outDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + toOffset + "&cal=to&currDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&printDate=" + (tempDate.getMonth()+1) + "/" + 1 + "/" + tempDate.getFullYear() + "&minDate=" + (fromDate.getMonth()+1) + "/" + (fromDate.getDate()) + "/" + fromDate.getFullYear(), function (data) { $(".ppn_cal_to .ppn_cal_popupBody").html ( data ); $('.ppn_cal_to .ppn_cal_popupBody .ppn_cal_monthHeader select').attr('selectedIndex',toOffset);} );
	}
	else
	{
        if ( toDate.getDate() == fromDate.getDate() && toDate.getFullYear() == fromDate.getFullYear() && toDate.getMonth() == fromDate.getMonth() )
		    $.get("/includes/double_cal/printDoubleCal.php?blank=true&inDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&outDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + toOffset + "&cal=to&currDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&printDate=" + (toDate.getMonth()+1) + "/" + 1 + "/" + toDate.getFullYear() + "&minDate=" + (fromDate.getMonth()+1) + "/" + (fromDate.getDate()) + "/" + fromDate.getFullYear(), function (data) { $(".ppn_cal_to .ppn_cal_popupBody").html ( data ); $('.ppn_cal_to .ppn_cal_popupBody .ppn_cal_monthHeader select').attr('selectedIndex',toOffset); } );
        else
            $.get("/includes/double_cal/printDoubleCal.php?inDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&outDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + toOffset + "&cal=to&currDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&printDate=" + (toDate.getMonth()+1) + "/" + 1 + "/" + toDate.getFullYear() + "&minDate=" + (fromDate.getMonth()+1) + "/" + (fromDate.getDate()) + "/" + fromDate.getFullYear(), function (data) { $(".ppn_cal_to .ppn_cal_popupBody").html ( data ); $('.ppn_cal_to .ppn_cal_popupBody .ppn_cal_monthHeader select').attr('selectedIndex',toOffset); } );
	}
}

function setToDate ( day, month, year, blank )
{
	$('.ppn_cal_popup').hide();

	toOffset = ( month - today.getMonth() ) + ( 12 * ( year - today.getFullYear() ));
	toDate = new Date ( year, month, day );

	if ( !blank )
	{
        $('.ppn_cal_to .ppn_cal_currDate').text( (month+1) + "/" + day + "/" + year );
		$('.ppn_cal_to #rs_chk_out').attr("value", (month+1) + "/" + day + "/" + year);
		$('.ppn_cal_to #rs_chk_out_vis').attr("value", (month+1) + "/" + day + "/" + year );
        $.get("/includes/double_cal/printDoubleCal.php?inDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&outDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + fromOffset + "&cal=from&currDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&printDate=" + (fromDate.getMonth()+1) + "/" + 1 + "/" + fromDate.getFullYear() + "&minDate=" + (today.getMonth()+1) + "/" + today.getDate() + "/" + today.getFullYear(), function (data) { $(".ppn_cal_from .ppn_cal_popupBody").html ( data ); $('.ppn_cal_from .ppn_cal_popupBody .ppn_cal_monthHeader select').attr('selectedIndex',fromOffset); } );
        $.get("/includes/double_cal/printDoubleCal.php?inDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&outDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + toOffset + "&cal=to&currDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&printDate=" + (toDate.getMonth()+1) + "/" + 1 + "/" + toDate.getFullYear() + "&minDate=" + (fromDate.getMonth()+1) + "/" + (fromDate.getDate()) + "/" + fromDate.getFullYear(), function (data) { $(".ppn_cal_to .ppn_cal_popupBody").html ( data ); $('.ppn_cal_to .ppn_cal_popupBody .ppn_cal_monthHeader select').attr('selectedIndex',toOffset);} );
	}
    else
    {
        $.get("/includes/double_cal/printDoubleCal.php?blank=true&inDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&outDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + fromOffset + "&cal=from&currDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&printDate=" + (fromDate.getMonth()+1) + "/" + 1 + "/" + fromDate.getFullYear() + "&minDate=" + (today.getMonth()+1) + "/" + today.getDate() + "/" + today.getFullYear(), function (data) { $(".ppn_cal_from .ppn_cal_popupBody").html ( data ); $('.ppn_cal_from .ppn_cal_popupBody .ppn_cal_monthHeader select').attr('selectedIndex',fromOffset); } );
        $.get("/includes/double_cal/printDoubleCal.php?blank=true&inDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&outDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + toOffset + "&cal=to&currDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&printDate=" + (toDate.getMonth()+1) + "/" + 1 + "/" + toDate.getFullYear() + "&minDate=" + (fromDate.getMonth()+1) + "/" + (fromDate.getDate()) + "/" + fromDate.getFullYear(), function (data) { $(".ppn_cal_to .ppn_cal_popupBody").html ( data ); $('.ppn_cal_to .ppn_cal_popupBody .ppn_cal_monthHeader select').attr('selectedIndex',toOffset);} );
    }

}
function setFromDate ( day, month, year, blank )
{
	$('.ppn_cal_popup').hide();

	fromOffset = ( month - today.getMonth() ) + ( 12 * ( year - today.getFullYear() ));
	fromDate = new Date ( year, month, day );

	if ( !blank )
	{
        $('.ppn_cal_from .ppn_cal_currDate').text( (month+1) + "/" + day + "/" + year );
		$('.ppn_cal_from #rs_chk_in').attr("value", (month+1) + "/" + day + "/" + year);
		$('.ppn_cal_from #rs_chk_in_vis').attr("value", (month+1) + "/" + day + "/" + year );
        $.get("/includes/double_cal/printDoubleCal.php?inDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&outDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + fromOffset + "&cal=from&currDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&printDate=" + (fromDate.getMonth()+1) + "/" + 1 + "/" + fromDate.getFullYear() + "&minDate=" + (today.getMonth()+1) + "/" + today.getDate() + "/" + today.getFullYear(), function (data) { $(".ppn_cal_from .ppn_cal_popupBody").html ( data ); $('.ppn_cal_from .ppn_cal_popupBody .ppn_cal_monthHeader select').attr('selectedIndex',fromOffset); } );
	}
    else
    {
        $.get("/includes/double_cal/printDoubleCal.php?blank=true&inDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&outDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + fromOffset + "&cal=from&currDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&printDate=" + (fromDate.getMonth()+1) + "/" + 1 + "/" + fromDate.getFullYear() + "&minDate=" + (today.getMonth()+1) + "/" + today.getDate() + "/" + today.getFullYear(), function (data) { $(".ppn_cal_from .ppn_cal_popupBody").html ( data ); $('.ppn_cal_from .ppn_cal_popupBody .ppn_cal_monthHeader select').attr('selectedIndex',fromOffset); } );
    }

	if (!blank)
		doErrorCheck();
}

function drawFromNext ( month, year, blank )
{
	if ( fromOffset < 11 )
	{
		fromOffset ++;
        if ( blank )
		    $.get("/includes/double_cal/printDoubleCal.php?blank=true&inDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&outDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + fromOffset + "&cal=from&currDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + (fromDate.getFullYear()) + "&printDate=" + (month) + "/" + 1 + "/" + (year) + "&minDate=" + (today.getMonth()+1) + "/" + today.getDate() + "/" + today.getFullYear(), function (data) { $(".ppn_cal_from .ppn_cal_popupBody").html ( data ); $('.ppn_cal_from .ppn_cal_popupBody .ppn_cal_monthHeader select').attr('selectedIndex',fromOffset); } );
        else
            $.get("/includes/double_cal/printDoubleCal.php?inDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&outDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + fromOffset + "&cal=from&currDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + (fromDate.getFullYear()) + "&printDate=" + (month) + "/" + 1 + "/" + (year) + "&minDate=" + (today.getMonth()+1) + "/" + today.getDate() + "/" + today.getFullYear(), function (data) { $(".ppn_cal_from .ppn_cal_popupBody").html ( data ); $('.ppn_cal_from .ppn_cal_popupBody .ppn_cal_monthHeader select').attr('selectedIndex',fromOffset); } );
	}
}
function drawToNext ( month, year, blank )
{
	if ( toOffset < 11 )
	{
		toOffset ++;
        if ( blank )
		    $.get("/includes/double_cal/printDoubleCal.php?blank=true&inDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&outDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + toOffset + "&cal=to&currDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + (toDate.getFullYear()) + "&printDate=" + (month) + "/" + 1 + "/" + (year) + "&minDate=" + (fromDate.getMonth()+1) + "/" + (fromDate.getDate()) + "/" + fromDate.getFullYear(), function (data) { $(".ppn_cal_to .ppn_cal_popupBody").html ( data ); $('.ppn_cal_to .ppn_cal_popupBody .ppn_cal_monthHeader select').attr('selectedIndex',toOffset); } );
        else
            $.get("/includes/double_cal/printDoubleCal.php?inDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&outDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + toOffset + "&cal=to&currDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + (toDate.getFullYear()) + "&printDate=" + (month) + "/" + 1 + "/" + (year) + "&minDate=" + (fromDate.getMonth()+1) + "/" + (fromDate.getDate()) + "/" + fromDate.getFullYear(), function (data) { $(".ppn_cal_to .ppn_cal_popupBody").html ( data ); $('.ppn_cal_to .ppn_cal_popupBody .ppn_cal_monthHeader select').attr('selectedIndex',toOffset); } );
	}
}

function drawFromPrev ( month, year, blank )
{
	if ( fromOffset > 0 )
	{
		fromOffset --;
        if ( blank )
		    $.get("/includes/double_cal/printDoubleCal.php?blank=true&inDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&outDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + fromOffset + "&cal=from&currDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + (fromDate.getFullYear()) + "&printDate=" + (month) + "/" + 1 + "/" + (year) + "&minDate=" + (today.getMonth()+1) + "/" + today.getDate() + "/" + today.getFullYear(), function (data) { $(".ppn_cal_from .ppn_cal_popupBody").html ( data ); $('.ppn_cal_from .ppn_cal_popupBody .ppn_cal_monthHeader select').attr('selectedIndex',fromOffset); } );
        else
            $.get("/includes/double_cal/printDoubleCal.php?inDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&outDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + fromOffset + "&cal=from&currDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + (fromDate.getFullYear()) + "&printDate=" + (month) + "/" + 1 + "/" + (year) + "&minDate=" + (today.getMonth()+1) + "/" + today.getDate() + "/" + today.getFullYear(), function (data) { $(".ppn_cal_from .ppn_cal_popupBody").html ( data ); $('.ppn_cal_from .ppn_cal_popupBody .ppn_cal_monthHeader select').attr('selectedIndex',fromOffset); } );
	}
}
function drawToPrev ( month, year, blank )
{
	if ( toOffset > 0 )
	{
		toOffset --;
        if ( blank )
		    $.get("/includes/double_cal/printDoubleCal.php?blank=true&inDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&outDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + toOffset + "&cal=to&currDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + (toDate.getFullYear()) + "&printDate=" + (month) + "/" + 1 + "/" + (year) + "&minDate=" + (fromDate.getMonth()+1) + "/" + (fromDate.getDate()) + "/" + fromDate.getFullYear(), function (data) { $(".ppn_cal_to .ppn_cal_popupBody").html ( data ); $('.ppn_cal_to .ppn_cal_popupBody .ppn_cal_monthHeader select').attr('selectedIndex',toOffset); } );
        else
            $.get("/includes/double_cal/printDoubleCal.php?inDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&outDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + toOffset + "&cal=to&currDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + (toDate.getFullYear()) + "&printDate=" + (month) + "/" + 1 + "/" + (year) + "&minDate=" + (fromDate.getMonth()+1) + "/" + (fromDate.getDate()) + "/" + fromDate.getFullYear(), function (data) { $(".ppn_cal_to .ppn_cal_popupBody").html ( data ); $('.ppn_cal_to .ppn_cal_popupBody .ppn_cal_monthHeader select').attr('selectedIndex',toOffset); } );
	}
}

function drawFromOffset ( newOff, blank )
{
	fromOffset = newOff;

	var drawYear = today.getFullYear();
	if ( (today.getMonth()-1) + newOff >= 11 )
	{
		drawYear = drawYear + 1;
	}

	drawMonth = ( ( today.getMonth() +  newOff ) % 12 ) + 1;

    if ( blank )
	    $.get("/includes/double_cal/printDoubleCal.php?blank=true&inDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&outDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + fromOffset + "&cal=from&currDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + (fromDate.getFullYear()) + "&printDate=" + (drawMonth) + "/" + 1 + "/" + (drawYear) + "&minDate=" + (today.getMonth()+1) + "/" + today.getDate() + "/" + today.getFullYear(), function (data) { $(".ppn_cal_from .ppn_cal_popupBody").html ( data ); $('.ppn_cal_from .ppn_cal_popupBody .ppn_cal_monthHeader select').attr('selectedIndex',newOff); } );
    else
        $.get("/includes/double_cal/printDoubleCal.php?inDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&outDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + fromOffset + "&cal=from&currDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + (fromDate.getFullYear()) + "&printDate=" + (drawMonth) + "/" + 1 + "/" + (drawYear) + "&minDate=" + (today.getMonth()+1) + "/" + today.getDate() + "/" + today.getFullYear(), function (data) { $(".ppn_cal_from .ppn_cal_popupBody").html ( data ); $('.ppn_cal_from .ppn_cal_popupBody .ppn_cal_monthHeader select').attr('selectedIndex',newOff); } );
}
function drawToOffset ( newOff, blank )
{
	toOffset = newOff;

	var drawYear = today.getFullYear();
	if ( (today.getMonth()-1) + newOff >= 11 )
	{
		drawYear = drawYear + 1;
	}

	drawMonth = ( ( today.getMonth() +  newOff ) % 12 ) + 1;

    if ( blank )
	    $.get("/includes/double_cal/printDoubleCal.php?blank=true&inDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&outDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + toOffset + "&cal=to&currDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + (toDate.getFullYear()) + "&printDate=" + (drawMonth) + "/" + 1 + "/" + (drawYear) + "&minDate=" + (fromDate.getMonth()+1) + "/" + (fromDate.getDate()) + "/" + fromDate.getFullYear(), function (data) { $(".ppn_cal_to .ppn_cal_popupBody").html ( data ); $('.ppn_cal_to .ppn_cal_popupBody .ppn_cal_monthHeader select').attr('selectedIndex',newOff); } );
    else
        $.get("/includes/double_cal/printDoubleCal.php?inDate=" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "/" + fromDate.getFullYear() + "&outDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + toDate.getFullYear() + "&monthNames=" + monthNames + "&dayNames=" + dayNames + "&offset=" + toOffset + "&cal=to&currDate=" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "/" + (toDate.getFullYear()) + "&printDate=" + (drawMonth) + "/" + 1 + "/" + (drawYear) + "&minDate=" + (fromDate.getMonth()+1) + "/" + (fromDate.getDate()) + "/" + fromDate.getFullYear(), function (data) { $(".ppn_cal_to .ppn_cal_popupBody").html ( data ); $('.ppn_cal_to .ppn_cal_popupBody .ppn_cal_monthHeader select').attr('selectedIndex',newOff); } );
}

function setDates ( newFrom, newTo )
{
	if ( newFrom != "" )
	{
		var splitted = newFrom.split("/");
		fromDate = new Date(splitted[2], (splitted[0]-1), splitted[1]);
		setFromDate ( fromDate.getDate(), fromDate.getMonth(), fromDate.getFullYear(), false );
	}
	else
	{
		fromDate = new Date ();
		setFromDate ( fromDate.getDate(), fromDate.getMonth(), fromDate.getFullYear(), true );
	}

	if ( newTo != "" )
	{
		var splitted = newTo.split("/");
		toDate = new Date(splitted[2], (splitted[0]-1), splitted[1]);
		setToDate ( toDate.getDate(), toDate.getMonth(), toDate.getFullYear(), false );
	}
	else
	{
		toDate = new Date();
		toDate.setDate ( fromDate.getDate() );
		setToDate ( toDate.getDate(), toDate.getMonth(), toDate.getFullYear(), true );
	}
}

$(document).ready(
	function()
	{
		$('.ppn_cal_popup').bgiframe();

		//FUNCTIONS
        $('.ppn_cal_to .ppn_cal_showButton').click ( function () { $('.ppn_cal_from .ppn_cal_popup').hide(); $('.ppn_cal_to .ppn_cal_popup').show(); } );
        $('.ppn_cal_from .ppn_cal_showButton').click ( function () { $('.ppn_cal_to .ppn_cal_popup').hide(); $('.ppn_cal_from .ppn_cal_popup').show(); } );

        $('#rs_chk_out_vis').click ( function () { $('.ppn_cal_from .ppn_cal_popup').hide(); $('.ppn_cal_to .ppn_cal_popup').show(); } );
        $('#rs_chk_in_vis').click ( function () { $('.ppn_cal_to .ppn_cal_popup').hide(); $('.ppn_cal_from .ppn_cal_popup').show(); } );

		$("input#rs_chk_in_vis,input#rs_chk_out_vis").blur(function() {
			if ($(this).val() == "mm/dd/yyyy")
			{
				return true;
			}

			if ($(this).val() != $("input#" + $(this).attr("id").replace("_vis","")).val())
			{
				var valid_change = false;
				var date_regex = /([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4})/;

				if (date_regex.test($(this).val()))
				{
					var now = new Date();
					var date_array = $(this).val().split("/");

					// ensure year is valid
					if (date_array[2] >= now.getFullYear())
					{
						// ensure month is valid
						if ((date_array[0] <= 12 && date_array[0] >= 1) && (date_array[0] >= now.getMonth() + 1 || date_array[2] > now.getFullYear()))
						{
							// ensure day is valid
							if ((date_array[1] <= 31 && date_array[1] >= 1) && (date_array[1] >= now.getDate() || date_array[0] > now.getMonth() + 1 || date_array[2] > now.getFullYear()))
							{
								// RCC only: max 330 days into the future
								var new_date = new Date();
								new_date.setFullYear($(this).val().split("/")[2],$(this).val().split("/")[0] - 1,$(this).val().split("/")[1] - 1);

								if (Math.abs(new_date.getTime() - now.getTime()) / (1000 * 60 * 60 * 24) <= 330)
								{
									// RCC only: max 310 total booked days
									var compare_date = new Date();
									if ($(this).attr("id") == "rs_chk_in_vis" && $("input#rs_chk_out_vis").val() != "mm/dd/yyyy")
									{
										compare_date.setFullYear($("input#rs_chk_out").val().split("/")[2],$("input#rs_chk_out").val().split("/")[0] - 1,$("input#rs_chk_out").val().split("/")[1] - 1);
									}
									else if ($(this).attr("id") == "rs_chk_out_vis" && $("input#rs_chk_in_vis").val() != "mm/dd/yyyy")
									{
										compare_date.setFullYear($("input#rs_chk_in").val().split("/")[2],$("input#rs_chk_in").val().split("/")[0] - 1,$("input#rs_chk_in").val().split("/")[1] - 1);
									}

									if ((Math.abs(compare_date.getTime() - new_date.getTime()) / (1000 * 60 * 60 * 24) <= 310) || ($("input#rs_chk_out_vis").val() == "mm/dd/yyyy" || $("input#rs_chk_in_vis").val() == "mm/dd/yyyy"))
									{
										// Ensure rs_out happens after rs_in if both are set
										if (($("input#rs_chk_out_vis").val() == "mm/dd/yyyy" || $("input#rs_chk_in_vis").val() == "mm/dd/yyyy"))
										{
											valid_change = true;
										}
										else
										{
											if (($(this).attr("id") == "rs_chk_in_vis" && new_date <= compare_date) || ($(this).attr("id") == "rs_chk_out_vis" && new_date >= compare_date))
											{
												valid_change = true;
											}
											else
											{
												alert("Your drop-off date must not precede your pick-up date");
											}
										}
									}
									else
									{
										alert("You cannot book a rental car for more than 310 days");
									}
								}
								else
								{
									alert("You cannot book a rental car more than 330 days into the future");
								}
							}
						}
					}
				}

				if (valid_change)
				{
					if ($(this).attr("id") == "rs_chk_in_vis")
					{
						setFromDate(date_array[1],date_array[0] - 1,date_array[2]);
					}
					else if ($(this).attr("id") == "rs_chk_out_vis")
					{
						setToDate(date_array[1],date_array[0] - 1,date_array[2]);
					}
				}
				else
				{
					//$(this).val($("input#" + $(this).attr("id").replace("_vis","")).val());
					$(this).val("mm/dd/yyyy");
					if ($(this).attr("id") == "rs_chk_in_vis")
					{
						fromDate = new Date();
					}
					else if ($(this).attr("id") == "rs_chk_out_vis")
					{
						toDate = new Date();
					}
					doErrorCheck();
				}
			}
		});

        $('.ppn_cal_closeButton').click ( function () { $('.ppn_cal_popup').hide(); } );

        $(document).click (
            function(e)
            {
                var $clicked=$(e.target);
                if ( ! ( $clicked.is('#rs_chk_out_vis') || $clicked.is('#rs_chk_in_vis') || $clicked.is('img') || $clicked.parents().is('.ppn_cal') || $clicked.parents().is('.ppn_cal_popupHeader') || $clicked.parents().is('.ppn_cal_popupBody') ) )
                {
                    $('.ppn_cal_popup').hide();
                }
            }
        );
	}
);
