
var cal_months = jsGlobalMonths;
var cal_days = jsGlobalDaysAbbrev;
var cal_dayOffset = jsGlobalDayOffset;
var cal_cbf;
var cal_savedBody;
var cal_savedWinc;
var cal_savedDiv;
var cal_savedFrame;
var cal_calldoc;
var cal_currentSetting;
var cal_backgroundcolor='#b5c7e7';
var cal_firstDate;
var cal_lastDate;
var cal_setDate;

function showCalSelf(d, callback, fd, ld){
    if (!cal_checkUserAgent()) return;
    if (cal_savedBody){
        cal_close();
        return;
    }
    cal_setDate=d;
    cal_cbf=callback;
    cal_savedBody=document.body.innerHTML;
    cal_calldoc=document;
    document.body.innerHTML=cal_getHTML("");
    cal_currentSetting=new Date(d);
    cal_firstDate=fd;
    cal_lastDate=ld;
    cal_draw();
    return;
}

function showCalPop(d, callback,e, fd, ld){
    if (!cal_checkUserAgent()){
        return;
    }
    if (cal_savedWinc){
        cal_close();
        return;
    }
    cal_cbf=callback;
    cal_savedWinc = window.open('','cal'+(''+new Date()).replace(new RegExp('[^A-Za-z0-9]','g'),''),'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=450,height=300,left='+cal_getScreenX(e)+',screenX='+cal_getScreenX(e)+',top='+cal_getScreenY(e)+',screenY='+cal_getScreenY(e)+'');
    cal_calldoc = cal_savedWinc.document;
    cal_calldoc.open();
    cal_calldoc.write("<html><head><title>Calendar</title></head><body>"+cal_getHTML("window.opener.")+"</body></html>");
    cal_calldoc.close();
    cal_currentSetting=new Date(d);
    cal_firstDate=fd;
    cal_lastDate=ld;
    cal_draw();
    return;
}

function showCalDiv(d, callback, e, fd, ld){
    showCalDivOffset(d,callback,e,fd,ld,0,20);
}

function cal_checkUserAgent(){
    var ua=navigator.userAgent.toLowerCase();
    // Calendar can freeze IE 5.2 on the Mac, disable it.
    if (ua.indexOf('msie')!=-1 && ua.indexOf('mac')!=-1 && ua.indexOf('5.')!=-1){
        return false;
    }
    return true;
}

function showCalDivOffset(d, callback, e, fd, ld, xOff, yOff){
    if (!cal_checkUserAgent()) return;
    if (cal_savedDiv){
        cal_close();
        return;
    }
    cal_setDate=d;
    cal_cbf=callback;
    cal_calldoc=document;
    cal_savedDiv=document.createElement('div');
	cal_savedDiv.className='oldcal';
    cal_savedDiv.innerHTML=cal_getHTML("window.");
    cal_savedDiv.style.position='absolute';
    cal_savedDiv.style.top=cal_getMouseY(e)+yOff + 'px';
    cal_savedDiv.style.left=cal_getMouseX(e)+xOff + 'px';
    
    // Use an iframe to hide select form elements under the
    // calendar in Internet Explorer
    cal_savedFrame=document.createElement('iframe');
    cal_savedFrame.style.border='none';
    cal_savedFrame.style.position='absolute';
    cal_savedFrame.style.top=cal_getMouseY(e)+yOff + 'px';
    cal_savedFrame.style.left=cal_getMouseX(e)+xOff + 'px';
    cal_savedFrame.style.width='150px';
    cal_savedFrame.style.height='140px';
    document.body.appendChild(cal_savedFrame);
    
    document.body.appendChild(cal_savedDiv);    

    cal_currentSetting=new Date(d);
    cal_firstDate=fd;
    cal_lastDate=ld;
    cal_draw();
    return;
}

function cal_getHTML(wR){
    var s = "";
    s+="<table border='0' cellpadding='0' cellspacing='0' width='150' class='twk_month'>";
    s+="<tr><td colspan=3 border=0 bgcolor='#4B73BB' style='border-left: 1px solid #666666; border-right: 1px solid #666666; border-top: 1px solid #666666;'>";
    s+=cal_getDF(wR);
    s+="</td></tr>";
    s+="<tr><td border=0 bgcolor='#DAE4EA' background='/img/calendar/rounded_l.gif'></td><td border=0 bgcolor='#DAE4EA'>";
    s+=cal_getBody(wR);
    s+="</td><td border=0 bgcolor='#DAE4EA' background='/img/calendar/rounded_r.gif'></td></tr>";
    s+="<tr><td background='/img/calendar/rounded_bl.gif' width='7' height='7' border='0'></td><td background='/img/calendar/rounded_b.gif'></td><td background='/img/calendar/rounded_br.gif' width='7' height='7' border='0'></td></tr></table>";
    s+="</table>";
    return s;
}

function cal_getDF(wR){
    var s="";
    s+="<table style='border-collaspse:collapse;border:none;height:100%;width:100%;background-color:#4B73BB;'><tr>";
    s+="<td width='14' border=0 onclick='"+wR+"cal_prevMonth();' style='cursor:pointer;cursor:hand;'><img src='/img/calendar/arrow_left.gif' width='7' height='8' border='0' hspace='2'></td>";
    s+="<td border=0 style='color:#ffffff;font-size:11px;font-family:arial;' align='center'><b><span id=cal_monthyear></span></b></td>";
    s+="<td border=0 width='14' onclick='"+wR+"cal_nextMonth();' style='cursor:pointer;cursor:hand;'><img src='/img/calendar/arrow_right.gif' width='7' height='8' border='0' hspace='2'></td>";
    s+="<td border=0 width='7' onclick='"+wR+"cal_close();' style='cursor:pointer;cursor:hand;'><img src='/img/calendar/x.gif' width='5' height='6' border='0' align='right' hspace='1' vspace='1' style='padding-left:2px;'></td>";
    s+="</tr></table>";
    return s;
}

function cal_getBody(wR){
    var s="";
    s+="<table style='font-family:Tahoma,Arial,Helvetica,sans-serif;font-size:10px;background-color:#DAE4EA;border:none;cursor:pointer;cursor:hand;' class='twk_days'>";
    s+=cal_getWDayH(wR);
    s+=cal_getDays(wR);
    s+="</table>";
    return s;
}

function cal_getWDayH(wR){
    var s="";
    s+="<tr>";
    for (var i=0;i<cal_days.length; i++)s+="<td style='width:20px;border:1px solid #999999;color:#000099;font-weight:bold;background-color:#e3Ebf3;text-align:center;'>"+cal_days[i]+"</td>";
    s+="</tr>";
    return s;
}

function cal_getDays(wR){
    var s="";
    var k=1;
    for (var j=0;j<6;j++){
        s+="<tr>";
        for (var i=0;i<cal_days.length; i++)s+="<td id=cal_c"+k+" onclick='"+wR+"cal_dCb("+(k++)+");' type=text style='text-align:right;'></td>";
        s+="</tr>";
    }
    return s;
}

function cal_getScreenX(e){
    if (!e && window.event) e = window.event;
    if (e && e.screenX) return e.screenX;
    return 0;
}

function cal_getScreenY(e){
    if (!e && window.event) e = window.event;
    if (e && e.screenY) return e.screenY;
    return 0;
}

function cal_getMouseX(e){
    if (!e && window.event) e = window.event;
    if (e && e.pageX) return e.pageX;
    if (e && e.clientX) return e.clientX + document.body.scrollLeft;
    return 50;
}

function cal_getMouseY(e){
    if (!e && window.event) e = window.event;
    if (e && e.pageY) return e.pageY;
    if (e && e.clientY) return e.clientY + document.body.scrollTop;
    return 50;
}

function cal_setToday(){
  cal_currentSetting = new Date();
  cal_draw();
}

function cal_dCb(dateIndex){
    cal_currentSetting.setDate(1);
    var subtract = (cal_currentSetting.getDay() + 6 - cal_dayOffset) % 7 + 1;
    if (subtract <= 2) subtract += 7;
    var workingDate = new Date(cal_currentSetting);
    var dateCorrection=dateIndex-subtract-1;
    for (var i=0; i<dateCorrection; i++){
        workingDate.setMilliseconds(86400000);
    }
    for (var i1=0; i1>dateCorrection; i1--){
        workingDate.setMilliseconds(-86400000);
    }
    if (cal_dcomp(cal_firstDate,workingDate) <= 0 && cal_dcomp(cal_lastDate,workingDate) >= 0){
        cal_close();
        cal_cbf(workingDate);
    }
}

function cal_close(){
    if (cal_savedBody) cal_calldoc.body.innerHTML = cal_savedBody;
    if (cal_savedWinc) cal_savedWinc.close();
    if (cal_savedDiv) document.body.removeChild(cal_savedDiv);
    if (cal_savedFrame) document.body.removeChild(cal_savedFrame);
    cal_savedBody=cal_savedWinc=cal_savedDiv=null;
}

function cal_prevMonth(){
    cal_currentSetting.setDate(1);
    var month = cal_currentSetting.getMonth();
    while (month == cal_currentSetting.getMonth()){
        cal_currentSetting.setMilliseconds(-86400000);
    }
    cal_draw();
}

function cal_nextMonth(){
    cal_currentSetting.setDate(28);
    var month = cal_currentSetting.getMonth();
    while (month == cal_currentSetting.getMonth()){
        cal_currentSetting.setMilliseconds(86400000);
    }
    cal_draw();
}

function cal_dcomp(d1, d2){
    if (!d1 || !d2) return 0;
    if (d1.getYear() == d2.getYear()){
        if (d1.getMonth() == d2.getMonth()){
            return d1.getDate() - d2.getDate();
        }
        return d1.getMonth()-d2.getMonth();
    }
    return d1.getYear()-d2.getYear();
}

function cal_draw(){
    var today = new Date();
    cal_currentSetting.setDate(1);
    var year = cal_currentSetting.getFullYear();
    var month = cal_currentSetting.getMonth();
    
    cal_calldoc.getElementById("cal_monthyear").innerHTML=cal_month_header(month,year);    
    
    var workingDate = new Date(cal_currentSetting);
    var subtract = (cal_currentSetting.getDay() + 6 - cal_dayOffset) % 7 + 1;
    if (subtract <= 2) subtract += 7;
    workingDate.setMilliseconds(-subtract*86400000);
    for(var dayCount=1; dayCount <= 42; dayCount++){
        var field = cal_calldoc.getElementById("cal_c"+dayCount);
        field.innerHTML = workingDate.getDate();
        var bgcolor = '#ffffff';
        var bordcolor = '#ffffff';
        var fgcolor = '#000000';
        if (cal_dcomp(cal_firstDate,workingDate) > 0 || cal_dcomp(cal_lastDate,workingDate) < 0){
            bgcolor = '#666666';
            bordcolor = '#cccccc';
            fgcolor = '#cccccc';
        } else if (workingDate.getMonth() != month){
            bgcolor = '#DAE4EA';
            bordcolor = '#DAE4EA';
            fgcolor = '#999999';
        } else if (cal_dcomp(workingDate, cal_setDate) == 0){
            bgcolor = '#FFCC66';
            bordcolor = '#000000';
            fgcolor = '#000000';
        } else if (cal_dcomp(workingDate, today) == 0){
            bordcolor = '#999999';
        } else if ((workingDate.getDay() + 6 - cal_dayOffset) % 7 + 1 == 0 || (workingDate.getDay() + 6 - cal_dayOffset) % 7 + 1 == 6){
        } 
        
        field.style.backgroundColor = bgcolor;
        field.style.borderColor = bordcolor;
        field.style.color = fgcolor;
        workingDate.setMilliseconds(86400000);
    }
}

// func: getInDate
function getInDate()
{
    return getInDateN(0);
}

// gunc: getInDate1
function getInDate1()
{
    return getInDateN(1);
}

// func: getOutDate
function getOutDate()
{
    return getOutDateN(0);
}

// func: getOutDate1
function getOutDate1()
{
    return getOutDateN(1);
}

// func: getInDateN
function getInDateN(n)
{
    var f;

    if (n == 0)
    {
        f = document.HotelAvailabilityComparisonForm;
    } else
    {
        f = document.HacCheckForm;
    }

    var monY = f.inMonth[f.inMonth.selectedIndex].value.split(/\//);
    return new Date(monY[1],monY[0]-1,f.inDay.selectedIndex+1);
}

// func: getOutDateN
function getOutDateN(n)
{
    var f;
    if (n == 0)
    {
        f = document.HotelAvailabilityComparisonForm;
    } else
    {
        f = document.HacCheckForm;
    }
    var monY = f.outMonth[f.outMonth.selectedIndex].value.split(/\//);
    return new Date(monY[1],monY[0]-1,f.outDay.selectedIndex+1);
}

// func: setInDate
function setInDate(d)
{
    setInDateN(d,0);
}

// func: setInDate1
function setInDate1(d)
{
    setInDateN(d,1);
}

// check for hac and vacation advisor forms
// func: setInDateN
function setInDateN(d,n)
{
    var f;
    if (n == 0)
    {
        f = document.HotelAvailabilityComparisonForm;
    } else
    {
        f = document.HacCheckForm;
    }
    if (f) _setInDate(f, d , n);

}

// func: _setInDate
function _setInDate(f, d,n)
{
    f.inDay.selectedIndex=d.getDate()-1;
    for (var i=0; i<f.inMonth.length;i++)
    {
        if (f.inMonth[i].value == padMonth(d.getMonth()+1)+"/"+d.getFullYear())
        {
            f.inMonth.selectedIndex=i;
        }
    }
    if (cal_dcomp(getOutDateN(n), d)<=0)
    {
        var dout=new Date(d);
        dout.setMilliseconds(86400000);
        setOutDateN(dout,n);
    }
}

// func: setOutDate
function setOutDate(d)
{
    setOutDateN(d,0);
}

// func: setOutDate1
function setOutDate1(d)
{
    setOutDateN(d,1);
}

// cehck for hac and vacation advisor forms
// func: setOutDateN
function setOutDateN(d,n)
{
    var f;
    if (n == 0)
    {
        f = document.HotelAvailabilityComparisonForm;
    } else
    {
        f = document.HacCheckForm;
    }
	   if (f) _setOutDate(f, d , n);

}

// func: _setOutDate
function _setOutDate(f, d,n)
{

    f.outDay.selectedIndex=d.getDate()-1;
    for (var i=0; i<f.outMonth.length;i++)
    {
        if (f.outMonth[i].value == padMonth(d.getMonth()+1)+"/"+d.getFullYear())
        {
            f.outMonth.selectedIndex=i;
        }
    }
}

// func: getLastDate
function getLastDate()
{
    var d = new Date();
    d.setFullYear(d.getFullYear()+1);
    d.setDate(1);
    d.setMilliseconds(-86400000);
    return d;
}

// func: padMonth
function padMonth(m)
{
    var s=""+m;
    if (s.length==1) return "0"+s;
    return s;
}
