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");
        $('.ppn_cal_to .ppn_cal_monthSelect option:selected').attr("selected","false");
        $('.ppn_cal_to .ppn_cal_monthSelect option[value=""]').attr("selected",true);
        $('.ppn_cal_to .ppn_cal_daySelect').html("<select val=''>Day</select>");
        setToDate ( tempDate.getDate(), tempDate.getMonth(), tempDate.getFullYear(), true );
        //setToDate ( tempDate.getFullYear(), tempDate.getDate(), tempDate.getMonth(), true );
    }
    else
    {
        $.get("/searchbox/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); } );
        //$.get("/searchbox/inc/printDoubleCal.php?inDate=" + fromDate.getFullYear() + "/" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "&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 .monthBlank').hide();
        //$('.ppn_cal_to .rs_chk_out').attr("value", (month+1) + "/" + day + "/" + year);
        $('.ppn_cal_to .rs_chk_out').attr("value", year + "-" + (month+1) + "-" + day);
        $('.ppn_cal_to .rs_chk_out_vis').attr("value", $('.ppn_cal_to .rs_chk_out').attr("value") );
        printDaysInMonth(false, month, year);
    }
    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("/searchbox/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("/searchbox/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);} );
    
    if ( !blank )
    {
        $('.ppn_cal_to .ppn_cal_monthSelect option:selected').attr("selected","false");
        $('.ppn_cal_to .ppn_cal_monthSelect option[value="' + (month+1) + '-' + year + '"]').attr("selected",true);
    }
    else
    {
        $('.ppn_cal_to .ppn_cal_daySelect').html("<option value=''>Day</option>");
    }
    
}
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 .monthBlank').hide();
        //$('.ppn_cal_from .rs_chk_in').attr("value", (month+1) + "/" + day + "/" + year);
        $('.ppn_cal_from .rs_chk_in').attr("value", year + "-" + (month+1) + "-" + day);
        $('.ppn_cal_from .rs_chk_in_vis').attr("value", $('.ppn_cal_from .rs_chk_in').attr("value") );
        printDaysInMonth(true, month, year);
    }
    
    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("/searchbox/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("/searchbox/inc/printDoubleCal.php?inDate=" + fromDate.getFullYear() + "/" + (fromDate.getMonth()+1) + "/" + fromDate.getDate() + "&outDate=" + toDate.getFullYear() + "/" + (toDate.getMonth()+1) + "/" + toDate.getDate() + "&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();
        $('.ppn_cal_to .ppn_cal_monthSelect option').each ( function(obj) { var splitted = ($(this).val()).split("-"); if ( (fromDate.getMonth() >= splitted[0] && fromDate.getFullYear() == splitted[1]) || fromDate.getFullYear() > splitted[1] ) { $(this).hide(); } else { $(this).show(); } } );
        $('.ppn_cal_from .ppn_cal_monthSelect option:selected').attr("selected","false");
        $('.ppn_cal_from .ppn_cal_monthSelect option[value="' + (month+1) + '-' + year + '"]').attr("selected",true);
        if ( $('.ppn_cal_to .ppn_cal_monthSelect').val() != "" )
            printDaysInMonth(false, toDate.getMonth(), toDate.getFullYear());
    }
}

function drawFromNext ( month, year )
{
    if ( fromOffset < 11 )
    {
        fromOffset ++;
        $.get("/searchbox/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("/searchbox/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("/searchbox/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("/searchbox/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("/searchbox/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("/searchbox/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 );
    }    
}

function printDaysInMonth ( isFrom, month, year )
{
    var minDay = 1;
    var selDay = 1;
    var result = '';
    
    if ( isFrom && month == today.getMonth() && year == today.getFullYear() )
    {
        minDay = today.getDate();
    }
    else if ( !isFrom && month == fromDate.getMonth() && year == fromDate.getFullYear() )
    {
        minDay = fromDate.getDate() + 1;
    }
    
    if ( isFrom && month == fromDate.getMonth() && year == fromDate.getFullYear() )
        selDay = fromDate.getDate();
    if ( !isFrom && month == toDate.getMonth() && year == toDate.getFullYear() )
        selDay = toDate.getDate();
    
    for ( var i = minDay; i <= (32 - new Date(year, month, 32).getDate()); i ++ )
    {
        if ( i == selDay )
            result = result + "<option selected=SELECTED value='" + i + "'>" + i + "</option>";
        else
            result = result + "<option value='" + i + "'>" + i + "</option>";
    }
    
    if ( isFrom )
        $('.ppn_cal_from .ppn_cal_daySelect').html(result);
    else
        $('.ppn_cal_to .ppn_cal_daySelect').html(result);
}

$(document).ready(
    function()
    {            
        setDates('','');
        $('.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(); } );
        
        $('.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();
                }
            }
        );
    }
);