var currentDate=null;
var replyHandler=null;
var lowDate=null;
var highDate=null;
/************************************************************\
*
\************************************************************/
function showCalendar(srcObj,returnHandler,thisDate,startDate,endDate,title)
{
    if (title==null) title="";
    removeTheNode("calendarFloater");
    replyHandler=returnHandler;
    if (thisDate!=null) {
        if (typeof(thisDate)=="string") {
            temp=thisDate.split("-");
            currentDate=new Date(temp[0],temp[1],temp[2],temp[3],temp[4],0,0);
        }else{
            currentDate=thisDate;
        }
    }else{
        currentDate=new Date();
    }
    //currentDate=new Date(temp[0],temp[1],temp[2],temp[3],temp[4],0,0);
    if (startDate!==null) {
        if (typeof(startDate)=="string") {
            temp=startDate.split("-");
            lowDate=new Date(temp[0],temp[1],temp[2],temp[3],temp[4],0,0);
        }else{
            lowDate=startDate;
        }
    }
    if (endDate!==null) {
        if (typeof(endDate)=="string") {
            temp=endDate.split("-");
            highDate=new Date(temp[0],temp[1],temp[2],temp[3],temp[4],0,0);
        }else{
            highDate=endDate;
        }
    }
    var theTable="<div id=\"calendarContainer\">"+drawCalendar()+"</div>";
    theTable+="<img src=\"/skin/save.png\" onmouseover=\"javascript:this.src='/skin/saveOver.png'\" onmouseout=\"javascript:this.src='/skin/save.png'\" onclick=\"saveDate()\" style=\"cursor:pointer\" />";

    cal=returnRoundedBox("calendarFloater",title,theTable,true,51,null,true,true);
    pasteRawHTML(cal,document.body);
    alignObjectWithTarget(document.getElementById("calendarFloater"),true,false,srcObj,true,false,-2,2);
}
/************************************************************\
*
\************************************************************/
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()
{
    if (highDate!==null) {
        if (currentDate>highDate) currentDate=new Date(highDate.valueOf());
    }
    if (lowDate!==null) {
        if (currentDate<lowDate) currentDate=new Date(lowDate.valueOf());
    }
    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)
            {
                if (cellDate==currentDate.getDate())
                {
                    theDays+="<td style=\"cursor:pointer;"+dateRowOn+"\" onclick=\"selectDate(0,0,"+cellDate.toString()+")\">"+cellDate.toString()+"</td>";
                }
                else
                {
                    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>";
    //if (document.getElementById("dateComment")!=null) document.getElementById("dateComment").innerHTML=currentDate.toString();
    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;
} 
