var currentDate=null;
var replyHandler=null;
var lowDate=null;
var highDate=null;
/************************************************************\
*
\************************************************************/
function showCalendar(srcObj,returnHandler,thisDate,startDate,endDate)
{
    removeTheNode("calendarFloater");
    replyHandler=returnHandler;
    var temp=thisDate.split("-");
    currentDate=new Date(temp[0],temp[1],temp[2],temp[3],temp[4],0,0);
    if (startDate!==null)
    {
        temp=startDate.split("-");
        lowDate=new Date(temp[0],temp[1],temp[2],temp[3],temp[4],0,0);
    }
    if (endDate!==null)
    {
        temp=endDate.split("-");
        highDate=new Date(temp[0],temp[1],temp[2],temp[3],temp[4],0,0);
    }
    var content="<div style=\"text-align:right\"><span style=\"cursor:pointer;font-size:10px\" onclick=\"removeTheNode('calendarFloater')\">close</span></div>";
    content+="<div id=\"calendarContainer\">"+drawCalendar()+"</div>";
    content+="<span style=\"cursor:pointer\" onclick=\"saveDate()\">save</span>";
    var theTable=returnRoundedBox("calendarFloater",200,content,61,"lb")
    
    var theTable="<table id=\"calendarFloater\" style=\"left:0px;top:0px;position:absolute;z-index:61\" cellspacing=\"0\" cellpadding=\"0\">";
    theTable+="<tr><td rowspan=\"3\" style=\"vertical-align:top\">"+returnPNGTag("caltick")+"</td><td style=\"width:5px;height:5pxz\">"+returnPNGTag("caltl")+"</td><td style=\"background-color:#fff\"></td><td style=\"width:5px;height:5px\">"+returnPNGTag("caltr")+"</td></tr>";
    theTable+="<tr><td style=\"background-color:#fff\"></td><td style=\"background-color:#fff;text-align:left;padding:25px\">";
    
    theTable+="<div style=\"color:#2f3d4d;border-bottom:1px solid #a8acb0;padding-bottom:8px;margin-bottom:10px;font-size:12px\"><img src=\"/skin/close.png\" onclick=\"removeTheNode('calendarFloater')\" style=\"cursor:pointer\" align=\"right\" /><b>SELECT...</b></div>";
    
    theTable+="<div style=\"border-bottom:1px dotted #a8acb0;padding-bottom:8px;margin-bottom:10px\" id=\"calendarContainer\">"+drawCalendar()+"</div>";
    theTable+="<img src=\"/skin/save.gif\" onclick=\"saveDate()\" style=\"cursor:pointer\" />";
    theTable+="</td><td style=\"background-color:#fff\"></td></tr>";
    theTable+="<tr><td style=\"width:5px;height:5pxz\">"+returnPNGTag("calbl")+"</td><td style=\"background-color:#fff\"></td><td style=\"width:5px;height:5px\">"+returnPNGTag("calbr")+"</td></tr>";
    
    theTable+="</table>";

    pasteRawHTML(theTable,document.body);
    alignObjectLeftTopWithTargetRightMiddleOffset(document.getElementById("calendarFloater"),srcObj,3,-15);
}
/************************************************************\
*
\************************************************************/
function saveDate()
{
    var command=replyHandler+"("+currentDate.getFullYear()+","+currentDate.getMonth()+","+currentDate.getDate()+")";
    eval(command);
    removeTheNode("calendarFloater");
}
/************************************************************\
*
\************************************************************/
function selectDate(dy,dm,dd)
{
    if (dy!==0)
    {
        currentDate.setFullYear(currentDate.getFullYear()+dy);
    }
    if (dm!==0)
    {
        nm=currentDate.getMonth()+dm;
        if (nm<0)
        {
            nm=11;
            currentDate.setFullYear(currentDate.getFullYear()-1);
        }
        if (nm>11)
        {
            nm=0;
            currentDate.setFullYear(currentDate.getFullYear()+1);
        }
        currentDate.setMonth(nm);
    }
    if (dd!==0)
    {
        currentDate.setDate(dd);
        saveDate();
    }
    else
    {
        document.getElementById("calendarContainer").innerHTML=drawCalendar();
    }
}
/************************************************************\
*
\************************************************************/
function drawCalendar()
{
    var commonStyle="text-align:center;font-size:10px";
    var yearRow=commonStyle+";background-color:#ecf3f8;color:#75b4e9;border:1px solid #75b4e9";
    var monthRow=commonStyle+";background-color:#c9e2f4;color:#5fa9dd;border:1px solid #5fa9dd";
    var dayRow=commonStyle+";background-color:#f4f0da;color:#a79f71;border:1px solid #a79f71";
    var dateRow=commonStyle+";background-color:#d8f6da;color:#71a771;border:1px solid #71a771";
    var dateRowOn=commonStyle+";background-color:#edfdbc;color:#9aef40;border:1px solid #9aef40";
    var dateRowOff=commonStyle+";background-color:#ffffff;color:#d0d0d0;border:1px solid #d0d0d0";
    var days=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
    var monthNames=[["January",31],["February",(leapyear(currentDate.getFullYear()) ? 29 : 28)],["March",31],["April",30],["May",31],["June",30],["July",31],["August",31],["September",30],["October",31],["November",30],["December",31]];
    var suffix=["st","nd","rd","th","th","th","th","th","th","th","th","th","th","th","th","th","th","th","th","th","st","nd","rd","th","th","th","th","th","th","th","st"];
    var theTable="<table>";
    var leftButton="<td onclick=\"selectDate(-1,0,0)\" style=\"cursor:pointer;"+yearRow+"\">&lt;</td>";
    if (lowDate!==null)
    {
        if (currentDate.getFullYear()<=lowDate.getFullYear())
        {
            leftButton="<td></td>";
        }
    }
    var rightButton="<td onclick=\"selectDate(1,0,0)\" style=\"cursor:pointer;"+yearRow+"\">&gt;</td>";
    if (highDate!==null)
    {
        if (currentDate.getFullYear()>=highDate.getFullYear())
        {
            rightButton="<td></td>";
        }
    }
    var downDecade="<span onclick=\"selectDate(-10,0,0)\" style=\"cursor:pointer;"+yearRow+"\" title=\"-10\">&lt;&lt;</span>";
    if (lowDate!==null)
    {
        if (currentDate.getFullYear()-10<=lowDate.getFullYear())
        {
            downDecade="&nbsp;&nbsp;";
        }
    }
    var upDecade="<span onclick=\"selectDate(10,0,0)\" style=\"cursor:pointer;"+yearRow+"\" title=\"+10\">&gt;&gt;</span>";
    if (highDate!==null)
    {
        if (currentDate.getFullYear()+10>=highDate.getFullYear())
        {
            upDecade="&nbsp;&nbsp;";
        }
    }
    downDecade=downDecade.replace("1px solid","0px none");
    upDecade=upDecade.replace("1px solid","0px none");
    theTable+="<tr>"+leftButton+"<td colspan=\"5\" style=\"cursor:default;"+yearRow+"\">"+downDecade+"&nbsp;"+currentDate.getFullYear()+"&nbsp;"+upDecade+"</td>"+rightButton+"</tr>";
    leftButton="<td onclick=\"selectDate(0,-1,0)\" style=\"cursor:pointer;"+monthRow+"\">&lt;</td>";
    if (lowDate!==null)
    {
        if (currentDate.getFullYear()<=lowDate.getFullYear())
        {
            if (currentDate.getMonth()<=lowDate.getMonth())
            {
                leftButton="<td></td>";
            }
        }
    }
    rightButton="<td onclick=\"selectDate(0,1,0)\" style=\"cursor:pointer;"+monthRow+"\">&gt;</td>";
    if (highDate!==null)
    {
        if (currentDate.getFullYear()>=highDate.getFullYear())
        {
            if (currentDate.getMonth()>=highDate.getMonth())
            {
                rightButton="<td></td>";
            }
        }
    }
    theTable+="<tr>"+leftButton+"<td colspan=\"5\" style=\"cursor:default;"+monthRow+"\">"+monthNames[currentDate.getMonth()][0]+"</td>"+rightButton+"</tr>";
    theTable+="<tr>";
    for (d=0;d<7;d++)
    {
        theTable+="<td style=\"cursor:default;"+dayRow+"\" title=\""+days[d]+"\">"+days[d].substr(0,1)+"</td>";
    }
    theTable+="</tr>";
    var firstDayOfMonth=new Date(currentDate.getFullYear(),currentDate.getMonth(),1,0,0,0,0);
    var cellDate=-(firstDayOfMonth.getDay()-1);
    var cols=0;
    var theDays="<tr>";
    var topMonth=false;
    if (highDate!==null)
    {
        if (currentDate.getFullYear()==highDate.getFullYear())
        {
            if (currentDate.getMonth()==highDate.getMonth())
            {
                topMonth=true;
            }
        }
    }
    var bottomMonth=false;
    if (lowDate!==null)
    {
        if (currentDate.getFullYear()==lowDate.getFullYear())
        {
            if (currentDate.getMonth()==lowDate.getMonth())
            {
                bottomMonth=true;
            }
        }
    }
    while (cellDate<=monthNames[currentDate.getMonth()][1])
    {
        if (cols==7)
        {
            cols=0;
            theDays+="</tr><tr>";
        }
        if (cellDate>0)
        {
            var available=true;
            if (topMonth)
            {
                if (cellDate>highDate.getDate()) available=false;
            }
            if (bottomMonth)
            {
                if (cellDate<highDate.getDate()) available=false;
            }
            if (available)
            {
                    theDays+="<td style=\"cursor:pointer;"+dateRow+"\" onclick=\"selectDate(0,0,"+cellDate.toString()+")\">"+cellDate.toString()+"</td>";
            }
            else
            {
                theDays+="<td style=\"cursor:default;"+dateRowOff+"\")\">"+cellDate.toString()+"</td>";
            }
        }
        else
        {
            theDays+="<td></td>";
        }
        if (cellDate==monthNames[currentDate.getMonth()][1])
        {
            theDays+="</tr>";
        }
        cols++;
        cellDate++;
    }
    theTable+=theDays;
    theTable+="</table>";
    return theTable;
}
/************************************************************\
*
\************************************************************/
function leapyear(year)
{
    var leapyear=false;
    if ((year/4)==Math.floor(year/4)) leapyear=true;
    if ((year/100)==Math.floor(year/100)) leapyear=false;
    if ((year/400)==Math.floor(year/400)) leapyear=true;
    return leapyear;
}
/************************************************************\
*
\************************************************************/
function returnDateString(aDate)
{
    var reply="Undefined";
    if (aDate!==null)
    {
        var days=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
        var monthNames=[["January",31],["February",(leapyear(aDate.getFullYear()) ? 29 : 28)],["March",31],["April",30],["May",31],["June",30],["July",31],["August",31],["September",30],["October",31],["November",30],["December",31]];
        var suffix=["st","nd","rd","th","th","th","th","th","th","th","th","th","th","th","th","th","th","th","th","th","st","nd","rd","th","th","th","th","th","th","th","st"];
        reply=days[aDate.getDay()]+" "+ aDate.getDate().toString()+suffix[aDate.getDate()-1]+" "+monthNames[aDate.getMonth()][0]+" "+aDate.getFullYear().toString();
    }
    return reply;
} 
