var datesArray        = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
var gPopupCal         = null; //where to write the calendar

var today             = new Date();           //todays date
var todayT            = today.getDate();      //current day 1-31
var todayM            = today.getMonth();     //current month 0-11
var todayY            = today.getFullYear();  //current Year
var dispT             = todayT;               //displayed date
var dispM             = todayM;               //displayed month
var dispY             = todayY;               //displayed year
var selT              = todayT;               //selected date
var selM              = todayM;               //selected month
var selY              = todayY;               //selected year


function selectOption(el, value)
{
    var done = false;
    var i = 0;
    
    while ((i < el.length) && (!done))
    {
        if (el.options[i].value == value)
        {
           el.options[i].selected = true;
           done = true;
        }
        
        i++;
    }
}

function removeFloat(number)
{
    var idx = number.indexOf(".");
    
    if (idx > -1)
    {
        number = number.substring(0, idx);
    }
    
    return number;
}

function grabDateSelectOptions(inputField)
{
    var baseName = pullBaseName(inputField);
    var theDate = 1;
    var theMonth = todayM;
    var theYear = todayY;
    
    var dateItem = document.getElementById(baseName + "_year");
    
    if (dateItem)
    {
        theYear = dateItem.options[dateItem.selectedIndex].value;
        theYear = removeFloat(theYear);
    }
    
    dateItem = document.getElementById(baseName + "_month");
    
    if (dateItem)
    {
        theMonth = dateItem.options[dateItem.selectedIndex].value;
        theMonth = removeFloat(theMonth);
    }
    
    dateItem = document.getElementById(baseName + "_date");
    
    if (dateItem)
    {
        theDate = dateItem.options[dateItem.selectedIndex].value;
        theDate = removeFloat(theDate);
    }
    
    selT = theDate;
    selM = theMonth;
    selY = theYear;

    return joinDate(theMonth, theDate, theYear);
}

function pullBaseName(inputField)
{
    var baseName = inputField.name;
    var idx = baseName.indexOf("_");
    
    if (idx > -1)
    {
        baseName = baseName.substring(0, idx);
    }
    
    return baseName
}

function findFirstDayOfMonth(theMonth, theYear)
{
    var firstDay = new Date();
        firstDay.setDate(1);
        firstDay.setMonth(theMonth);
        firstDay.setFullYear(theYear);
    return firstDay.getDay();
}

function splitDate(strInput)
{

    this.date = -1;this.month = -1;this.year = -1;
    if (strInput != null)
    {
        this.delimiter = (strInput.indexOf("/") > -1) ? "/" : (strInput.indexOf(".") > -1) ? "." : (strInput.indexOf("-") > -1) ? "-" : (strInput.indexOf(",") > -1) ? "," : "/";
        var inputStr = strInput.split(this.delimiter);
        
        if((inputStr.length == 3) && !isNaN(inputStr[0]) && !isNaN(inputStr[1]) && !isNaN(inputStr[2]))
        {

            var monthIdx = (gShortDateFormat[0] == "mm" ? 0 : gShortDateFormat[1] == "mm" ? 1 : 2);
            var dateIdx =  (gShortDateFormat[0] == "dd" ? 0 : gShortDateFormat[1] == "dd" ? 1 : 2);
            var yearIdx =  (gShortDateFormat[0] == "yy" ? 0 : gShortDateFormat[1] == "yy" ? 1 : 2);
            
            this.month = parseInt(inputStr[monthIdx], 10) - 1;
            this.date = parseInt(inputStr[dateIdx], 10);
            this.year = inputStr[yearIdx];
            
            if (this.month > 11 || this.month < 0) this.month = -1;
            if (this.date > 31 || this.month < 0) this.date = -1;
            
            i_yrlength = this.year.toString().length;
            
            if (i_yrlength == 2) this.year = "20" + this.year;
            if (i_yrlength < 1 || i_yrlength==3) this.year = -1;
            if (this.year > 9999) this.year = -1;
        }
    }
}

function ZeroPadNum(n, length)
{
    var zeros = "0000";
    var s = "";
    s = "" + n;
    return (zeros.substring(0, length-s.length) + s);
}

function joinDate(theMonth, theDate, theYear)
{
   var trueMonth = ZeroPadNum(parseInt(theMonth, 10) + 1, 2);
   var part0 = (gShortDateFormat[0] == "mm" ? trueMonth : gShortDateFormat[0] == "dd" ? theDate : theYear);
   var part1 = (gShortDateFormat[1] == "mm" ? trueMonth : gShortDateFormat[1] == "dd" ? theDate : theYear);
   var part2 = (gShortDateFormat[2] == "mm" ? trueMonth : gShortDateFormat[2] == "dd" ? theDate : theYear);

   return part0 + gShortDateFormatSep + part1 + gShortDateFormatSep + part2;
}

function changeCalMonth(baseName, theMonth)
{
    var theYear = dispY;
    if (theMonth < 0)
    {
        theMonth = 11;
        theYear--;
    }
    else if (theMonth >= 12)
    {
        theMonth = theMonth - 12;
        theYear++;
    }
    
    buildCalBox(baseName, dispT, theMonth, theYear);
}

function makeCalendar(inputField, initialDate)
{
    var baseName = pullBaseName(inputField);
    
    var fromDate = (initialDate ? new splitDate(initialDate) : null);
    if (fromDate && (fromDate.date == -1 || fromDate.month == -1 || fromDate.year == -1))
        fromDate = null;

    if (fromDate)
    {
        buildCalBox(baseName, fromDate.date, fromDate.month, fromDate.year);
    }
    else
    {
        buildCalBox(baseName, todayT, todayM, todayY);
    }
}

function ShowCalDropdown(dtstartEl, startValue)
{
    if (!gPopupCal)
    {
        gPopupCal = document.getElementById("calbox");
    }
    
    hideCalendar();

    makeCalendar(dtstartEl, startValue);
    
    gPopupCal.style.display   = "block";
    gPopupCal.className       = "calboxon";

    if (dtstartEl.id == "paramUNTIL")
    {
        gPopupCal.style.top  = getOffsetTop(dtstartEl) - 150;
    }
    else
    {
        gPopupCal.style.top  = getOffsetTop(dtstartEl) - 50;
    }

    gPopupCal.style.left = getOffsetLeft(dtstartEl) + 50; 
    
}

function hideCalendar()
{
    if (gPopupCal)
        gPopupCal.style.display = "none";
}

function buildCalBox(baseName, theDate, theMonth, theYear)
{
    if (gPopupCal != null)
    {
        gPopupCal.innerHTML = "";

        dispM = theMonth;
        dispY = theYear;
        dispT = theDate;

        var numDaysInMonth = datesArray[dispM];
        isLeap  = (dispY % 4 == 0 && (dispY % 100 != 0 || dispY % 400 == 0 )) ? 1:0
        
        if (dispM == 1)
        {
            numDaysInMonth = numDaysInMonth + isLeap;
        }
        
        var firstDayOfMonth = findFirstDayOfMonth(dispM, dispY);
        var countDay = firstDayOfMonth;
        var calTableStr = "";
        var calCellStr = "";
        
        calTableStr += "<table month='" + dispM + "' year='" + dispY + "' cellpadding='0' cellspacing='0' border='0' class='calTableV'>\n";
        calTableStr += "<tr>\n<td colspan='7' class='calLabel'>";
        calTableStr += "<a href='#' onclick='changeCalMonth(\"" + baseName + "\", " + dispM + " - 1);return false' class='calNavA' style='text-decoration:none;'>&lt;&lt;</a>&nbsp;&nbsp;&nbsp;&nbsp;";
        calTableStr += SHORTMONTHLBLS[dispM] + "&nbsp;" + dispY;
        calTableStr += "&nbsp;&nbsp;&nbsp;&nbsp;<a href='#' onclick='changeCalMonth(\"" + baseName + "\", " + dispM + " + 1);return false;' class='calNavA' style='text-decoration:none;'>&gt;&gt;</a>";
        calTableStr += "</td>";
        calTableStr += "</tr>\n";
        calCellStr = "";
        
        for(var i = 0; i < TGDTDOWLbls.length; i++)
        {
            calCellStr += "<td class='calDayName'>";
            calCellStr += TGDTDOWLbls[i];
            calCellStr += "&nbsp;</td>\n";
        }
        
        calTableStr += "<tr>\n" + calCellStr + "</tr>\n";
        calCellStr = "";
        var i_calRows = 0;
        
        for (var d = 1; d <= numDaysInMonth; d++)
        {
            if (d == 1)
            {
                for (var blankday = 0; blankday < firstDayOfMonth; blankday++)
                {
                    calCellStr += "<td class='calDate'>&nbsp;</td>\n";
                }
            }
            
            calCellStr += "<td class='" + (selY == theYear && selM == theMonth && selT == d ? "calDateSel" : "calDate") + "'>";
            calCellStr += "<a href='#' onclick='setSelectedDate(\"" + baseName + "\", " + dispM + ", " + d + ", " + dispY + ");return false;' class='calNavA'>" + d + "</a></td>\n";
            countDay++;
            
            if(countDay == 7)
            {
                countDay = 0;
                calTableStr += "<tr>\n" + calCellStr + "</tr>\n";
                calCellStr = "";
                i_calRows++;
            }
            
            if(d == numDaysInMonth && countDay != 0)
            {
                for (var blankday = countDay; blankday < 7; blankday++)
                {
                    calCellStr += "<td class='calDate'>&nbsp;</td>\n";
                }
                
                
                calTableStr += "<tr>\n" + calCellStr + "</tr>\n";
                calCellStr = "";
                i_calRows++;
            }
        }
        
        if(i_calRows < 6)
        {
            calCellStr = "";
            
            for(var blankday = 0; blankday < 7; blankday++)
            {
                calCellStr += "<td class='calDate'>&nbsp;</td>\n";
            }
            
            calTableStr += "<tr>\n" + calCellStr + "</tr>\n";
        }
        
        calTableStr += "</table>";
        gPopupCal.innerHTML += calTableStr;
        gPopupCal.innerHTML += "<a href='#' onclick='hideCalendar();return false;' class='calNavA'>close</a>";
    }
}
