function SetupCalendars() {
    var currentYear = new Date().getFullYear();
    var nextYear = currentYear +  1;

    // checkin calendar
    Calendar.setup({
        inputField     : "checkinValue",
        ifFormat       : "%Y-%m-%d",
        button         : "checkinTrigger",
        weekNumbers    : false,
        onUpdate       : checkinCalendarUpdated,
        //dateStatusFunc : disabledStatus,
        range          : [currentYear, nextYear]
    });
    
    // checkout calendar
    Calendar.setup({
        inputField     : "checkoutValue",
        ifFormat       : "%Y-%m-%d",
        button         : "checkoutTrigger",
        weekNumbers    : false,
        onUpdate       : checkoutCalendarUpdated,
        //dateStatusFunc : disabledStatus,
        range          : [currentYear, nextYear]
    });
}

function checkinCalendarUpdated(cal) {
    if (cal.dateClicked) {
        document.getElementById("checkinMonth").selectedIndex = cal.date.getMonth();
        document.getElementById("checkinDay").selectedIndex = (cal.date.getDate() - 1);
        cal.hide();
        checkinUpdated();
    }
}

function checkoutCalendarUpdated(cal) {
    if (cal.dateClicked) {
        document.getElementById("checkoutMonth").selectedIndex = cal.date.getMonth();
        document.getElementById("checkoutDay").selectedIndex = (cal.date.getDate() - 1);
        cal.hide();
        checkoutUpdated();
    }
}

function getDate(dateString) {
    var year = dateString.substr(0,4)
    var month = dateString.substr(5,2) - 1 // 0 - 11
    var day = dateString.substr(8,2)
    return new Date(year,month,day)
}

//changes departure month when arrival month is changed
function checkinUpdated() {

    inM = document.getElementById("checkinMonth");
    inD = document.getElementById("checkinDay");
    outM = document.getElementById("checkoutMonth");
    outD = document.getElementById("checkoutDay");

    var res = adjustDate( inM.options.selectedIndex, inD );
    if( res != 0 ) {
           outD.options.selectedIndex = 0;
           if ( inM.options.selectedIndex == 11 ) {
                outM.options.selectedIndex = 0
           } else if( res == 4 ) {
                outM.options.selectedIndex=inM.options.selectedIndex + 1;
                outD.options.selectedIndex = 0;
           } else {
                outM.options.selectedIndex=inM.options.selectedIndex + 1;
                outD.options.selectedIndex = 1;
           }
    } else {
        outM.options.selectedIndex = inM.options.selectedIndex;
        if (outD.options.selectedIndex <= inD.options.selectedIndex) {
            outD.options.selectedIndex = inD.options.selectedIndex + 2;
        }
    }

    var checkinMonth = inM.options[inM.selectedIndex].value * 1;
    var checkinDay = inD.options[inD.selectedIndex].value * 1;
    var checkoutMonth = outM.options[outM.selectedIndex].value * 1;
    var checkoutDay = outD.options[outD.selectedIndex].value * 1;
    document.getElementById("checkinValue").value = getYear(inM.selectedIndex) + "-" + checkinMonth + "-" + checkinDay;
    document.getElementById("checkoutValue").value = getYear(outM.selectedIndex) + "-" + checkoutMonth + "-" + checkoutDay;
    return;
}

function checkoutUpdated() {
    outM = document.getElementById("checkoutMonth");
    outD = document.getElementById("checkoutDay");
    adjustDate( outM.options.selectedIndex, outD );

    var checkoutMonth = outM.options[outM.selectedIndex].value * 1;
    var checkoutDay = outD.options[outD.selectedIndex].value * 1;
    document.getElementById("checkoutValue").value = getYear(outM.selectedIndex) + "-" + checkoutMonth + "-" + checkoutDay;
    return;
}

// do not allow selection of days that are not valid
// return non-zero if it is the last day of the month
function adjustDate( mthIdx, Dt ) {
    var value = 0;
    var theYear = getYear(mthIdx)
    var numDays = getDaysInMonth( mthIdx, theYear );
    
    if( mthIdx == 1 ) {
        if( Dt.options.selectedIndex + 2 < numDays ) {
            return 0;
        } else {
            if( Dt.options.selectedIndex + 1 > numDays) {
                Dt.options.selectedIndex=numDays - 1;
            }
            //check for leap year
            if( (Dt.options.selectedIndex + 1) == numDays ) {
                return 1;
            } else {
                return 4;
            }
        }
    }

    if( Dt.options.selectedIndex + 2 < numDays ) {
        value = 0;
    } else {
        if ( Dt.options.selectedIndex + 1 > numDays ) {
            Dt.options.selectedIndex--;
            value = 3;
        } else if ( Dt.options.selectedIndex + 1 == numDays ) {
            //index is 31 or 30
            value = 2;
        } else {
            value = 4;
        }
    }
    return value;
}

function getDaysInMonth( mthIdx, YrStr ) {
    // all the rest have 31
    var maxDays = 31
    // expect Feb. (of course)
    if( mthIdx == 1 ) {
        if( isLeapYear( YrStr ) ) {
            maxDays=29;
        } else {
            maxDays=28;
        }
    }

    // thirty days hath...
    if( mthIdx == 3 || mthIdx == 5 || mthIdx == 8 || mthIdx == 10 ) {
        maxDays=30;
    }
    return maxDays;
}

function getYear(mthIdx) {
    var today = new Date()
    var theYear = parseInt(today.getFullYear())
    
    if( mthIdx < today.getMonth() ) {
        theYear = ( parseInt(today.getFullYear()) + 1 )
    }

    return theYear    
}

function isLeapYear( yrStr ) {
    var leapYear = false;
    var year = parseInt( yrStr, 10 );
    // every fourth year is a leap year
    if ( year % 4 == 0 ) {
        leapYear = true;
        // unless it's a multiple of 100
        if( year % 100 == 0 ) {
            leapYear = false;
            // unless it's a multiple of 400
            if( year % 400 == 0 ) {
                leapYear=true;
            }
        }
    }
    return leapYear;
}

	// do not allow selection of days that are not valid
// return non-zero if it is the last day of the month
function adjustDate( mthIdx, Dt ) {
    var value = 0;
    var theYear = getYear(mthIdx)
    var numDays = getDaysInMonth( mthIdx, theYear );
    
    if( mthIdx == 1 ) {
        if( Dt.options.selectedIndex + 2 < numDays ) {
            return 0;
        } else {
            if( Dt.options.selectedIndex + 1 > numDays) {
                Dt.options.selectedIndex=numDays - 1;
            }
            //check for leap year
            if( (Dt.options.selectedIndex + 1) == numDays ) {
                return 1;
            } else {
                return 4;
            }
        }
    }

    if( Dt.options.selectedIndex + 2 < numDays ) {
        value = 0;
    } else {
        if ( Dt.options.selectedIndex + 1 > numDays ) {
            Dt.options.selectedIndex--;
            value = 3;
        } else if ( Dt.options.selectedIndex + 1 == numDays ) {
            //index is 31 or 30
            value = 2;
        } else {
            value = 4;
        }
    }
    return value;
}

function addToFaves() 
{ 
     var urlAddress = "http://www.hotelpricebot.com"; 
     var pageName = "Hotelpricebot.com - hotel price comparison"; 
     if ( navigator.appName != 'Microsoft Internet Explorer' ) 
      { window.sidebar.addPanel(pageName,urlAddress,""); } 
    else { window.external.AddFavorite(urlAddress,pageName); } 
} 
