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;

function doErrorCheck()
{
    if ( toDate <= fromDate )
    {
        var tempDate = new Date ( fromDate );
        tempDate.setDate ( fromDate.getDate() + 1 );
        $('.rs_chk_out_vis').val("mm/dd/yyyy");
        setToDate ( tempDate.getDate(), tempDate.getMonth(), tempDate.getFullYear(), true );
    }
    else
    {
        $.get("/inc/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 .rs_chk_out').attr("value", (month+1) + "/" + day + "/" + year);
        $('.ppn_cal_to .rs_chk_out_vis').attr("value", $('.ppn_cal_to .rs_chk_out').attr("value") );
    }
    if ( fromDate.getTime() < toDate.getTime() && !blank )
    {
        var duration = (Math.ceil((toDate.getTime() - fromDate.getTime())/(1000*60*60*24)));
        if ( duration > 1 )
        {
            duration = (duration) + " nights";
        }
        else
        {
            duration = duration + " night";
        }
        $('.ppn_cal_from .ppn_cal_popupHeader .ppn_cal_currDate').html( $('.ppn_cal_from .rs_chk_in').attr("value") + " - " + $('.ppn_cal_to .rs_chk_out').attr("value") + " : " + duration );
        $('.ppn_cal_to .ppn_cal_popupHeader .ppn_cal_currDate').html($('.ppn_cal_from .rs_chk_in').attr("value") + " - " + $('.ppn_cal_to .rs_chk_out').attr("value") + " : " + duration );
    }
    else
    {
        $('.ppn_cal_to .ppn_cal_popupHeader .ppn_cal_currDate').html( $('.ppn_cal_to .rs_chk_out').attr("value") );
        $('.ppn_cal_from .ppn_cal_popupHeader .ppn_cal_currDate').html( $('.ppn_cal_from .rs_chk_in').attr("value") );
    }

    $.get("/inc/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("/inc/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 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 .rs_chk_in').attr("value", (month+1) + "/" + day + "/" + year);
        $('.ppn_cal_from .rs_chk_in_vis').attr("value", $('.ppn_cal_from .rs_chk_in').attr("value") );
    }
    
    if ( fromDate.getTime() < toDate.getTime() && !blank )
    {
        var duration = (Math.ceil((toDate.getTime() - fromDate.getTime())/(1000*60*60*24)));
        if ( duration > 1 )
        {
            duration = (duration) + " nights";
        }
        else
        {
            duration = duration + " night";
        }
        $('.ppn_cal_from .ppn_cal_popupHeader .ppn_cal_currDate').html( $('.ppn_cal_from .rs_chk_in').attr("value") + " - " + $('.ppn_cal_to .rs_chk_out').attr("value") + " : " + duration );
        $('.ppn_cal_to .ppn_cal_popupHeader .ppn_cal_currDate').html($('.ppn_cal_from .rs_chk_in').attr("value") + " - " + $('.ppn_cal_to .rs_chk_out').attr("value") + " : " + duration );
    }
    else
    {
        $('.ppn_cal_to .ppn_cal_popupHeader .ppn_cal_currDate').html( $('.ppn_cal_to .rs_chk_out').attr("value") );
        $('.ppn_cal_from .ppn_cal_popupHeader .ppn_cal_currDate').html( $('.ppn_cal_from .rs_chk_in').attr("value") );
    }
    
    $.get("/inc/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); } );
    
    if (!blank)
        doErrorCheck();    
}

function drawFromNext ( month, year )
{
    if ( fromOffset < 11 )
    {
        fromOffset ++;
        $.get("/inc/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 )
{
    if ( toOffset < 11 )
    {
        toOffset ++;
        $.get("/inc/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 )
{
    if ( fromOffset > 0 )
    {
        fromOffset --;
        $.get("/inc/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 )
{
    if ( toOffset > 0 )
    {
        toOffset --;
        $.get("/inc/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 )
{
    fromOffset = newOff;
    
    var drawYear = today.getFullYear();
    if ( (today.getMonth()-1) + newOff >= 11 )
    {
        drawYear = drawYear + 1;
    }
    
    drawMonth = ( ( today.getMonth() +  newOff ) % 12 ) + 1;
    
    $.get("/inc/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 )
{
    toOffset = newOff;
    
    var drawYear = today.getFullYear();
    if ( (today.getMonth()-1) + newOff >= 11 )
    {
        drawYear = drawYear + 1;
    }
    
    drawMonth = ( ( today.getMonth() +  newOff ) % 12 ) + 1;
    
    $.get("/inc/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();
        setDates('','');
        //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(); } );
        
        $('.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();
                }
            }
        );
    }
);