var day;
var month;
var year;
var curYY;
var curMM;
var curDD;
var fromYY;
var toYY;

function formatNum1(i, default_v) 
{
    i = parseInt(i, 10);
    if (isNaN(i)) 
    	return default_v;

    if (i > default_v) 
    	return default_v;
    return i;
}

function formatNum2(i, valtype) 
{
    f = (i < 10 ? '0' : '') + i;
    if (valtype && valtype != '') 
    {
        switch(valtype) 
        {
            case 'month':
                f = (f > 12 ? 12 : f);
                break;
            case 'day':
                f = (f > 31 ? 31 : f);
                break;
            case 'hour':
                f = (f > 24 ? 24 : f);
                break;
            default:
            case 'second':
            case 'minute':
                f = (f > 59 ? 59 : f);
                break;
        }
    }

    return f;
}

function formatNum2d(i, default_v, valtype) 
{
    i = parseInt(i, 10);
    if (isNaN(i)) 
    	return default_v;
    return formatNum2(i, valtype)
}

function formatNum4(i) 
{
    i = parseInt(i, 10)
    return (i < 1000 ? i < 100 ? i < 10 ? '000' : '00' : '0' : '') + i;
}

function initCalendar(curY, curM, curD, fromY, toY)
{
	curYY = curY;
	curMM = curM;
	curDD = curD;
	fromYY = fromY;
	toYY = toY;
	
    if (!year && !month && !day) 
    {
        if (isNaN(year) || isNaN(month) || isNaN(day) || day == 0) 
        {
            dt      = new Date();
            year    = dt.getFullYear();
            month   = dt.getMonth();
            day     = dt.getDate();
        }
    } 
    else 
    {
        if (month > 11) 
        {
            month = 0;
            year++;
        }
        if (month < 0) 
        {
            month = 11;
            year--;
        }
    }

    if (document.getElementById) 
    {
        cnt = document.getElementById("calendar_data");
    } else if (document.all) {
        cnt = document.all["calendar_data"];
    }

    cnt.innerHTML = "";
    str = ""

    str += '<table width=100% cellpadding=4 cellspacing=0 class="normal"><tr><form method="NONE" onsubmit="return 0">';
    str += '<td width="50%">';
    str += '<select id="select_month" class="calendarText" style="width:120" name="monthsel" onchange="month = parseInt(document.getElementById(\'select_month\').value); monthYearChanged(); initCalendar(curYY, curMM, curDD, fromYY, toYY);dayChanged">';
    for (i =0; i < 12; i++) 
    {
        if (i == month) 
        	selected = ' selected="selected"';
        else 
        	selected = '';
        str += '<option value="' + i + '" ' + selected + '>' + month_names[i] + '</option>';
    }
    str += '</select>';
    str += '</td>';
    str += '</form><form method="NONE" onsubmit="return 0">';
    str += '<td width="50%">';
    str += '<select id="select_year" class="calendarText" name="yearsel" onchange="year = parseInt(document.getElementById(\'select_year\').value); monthYearChanged(); initCalendar(curYY, curMM, curDD, fromYY, toYY);">';
    for (i = fromY; i < toY; i++) 
    {
        if (i == year) 
        	selected = ' selected="selected"';
        else 
        	selected = '';
        str += '<option value="' + i + '" ' + selected + '>' + i + '</option>';
    }
    str += '</select>';
    str += '';
    str += '</td></form></tr><tr><td colspan=2 align=center><a href=# onclick="todayClicked()" style="font-size:9pt;color:red"><b>' + today_names[0] + '</b></a></td></tr></table>';

    str += '<table cellpadding=2 cellspacing=0 width=100% class="copy"><tr><td height=1 colspan=7 bgcolor="#999999"></td></tr><tr>';
    for (i = 0; i < 7; i++) 
    {
    	if (i == 0)
    		str += "<th><label><font color='red'>" + day_names[i] + "</font></label></th>";
    	else
        	str += "<th><label>" + day_names[i] + "</label></th>";
    }
    str += "</tr>";

    var firstDay = new Date(year, month, 1).getDay();
    var lastDay = new Date(year, month + 1, 0).getDate();

    str += "<tr>";
    dayInWeek = 0;
    for (i = 0; i < firstDay; i++) 
    {
        str += "<td align=center>&nbsp;</td>";
        dayInWeek++;
    }
    for (i = 1; i <= lastDay; i++) 
    {
        if (dayInWeek == 7) 
        {
            str += "</tr><tr>";
            dayInWeek = 0;
        }

        dispmonth = 1 + month;
        actVal = formatNum4(year) + "," + formatNum2(dispmonth, 'month') + "," + formatNum2(i, 'day');
        if (i == day) 
        {
            style = ' class="selected"';
            current_date = actVal;
        } 
        else
        	style = '';

        if (dayInWeek == 0)
        	if (style == '')
        	{
        		style = ' style="color:red"';
        		str += "<td onclick=\"dayChanged('" + actVal + "');\" style='cursor:pointer;color:red' onmouseover='this.style.backgroundColor=\"red\";this.style.color=\"white\"' onmouseout='this.style.backgroundColor=\"transparent\";this.style.color=\"red\"' align=center" + style + "><label style='cursor:pointer' onclick=\"dayChanged('" + actVal + "');\">" + i + "</label></td>"
        	}
        	else
        		str += "<td onclick=\"dayChanged('" + actVal + "');\" style='cursor:pointer' align=center" + style + "><label style='cursor:pointer' onclick=\"dayChanged('" + actVal + "');\">" + i + "</label></td>"
        else
        	if (style == '')
        		str += "<td onclick=\"dayChanged('" + actVal + "');\" style='cursor:pointer;color:black' onmouseover='this.style.backgroundColor=\"red\";this.style.color=\"white\"' onmouseout='this.style.backgroundColor=\"transparent\";this.style.color=\"black\"' align=center" + style + "><label style='cursor:pointer' onclick=\"dayChanged('" + actVal + "');\">" + i + "</label></td>"
        	else
        		str += "<td onclick=\"dayChanged('" + actVal + "');\" style='cursor:pointer' align=center" + style + "><label style='cursor:pointer' onclick=\"dayChanged('" + actVal + "');\">" + i + "</label></td>"
        
        dayInWeek++;
    }
    for (i = dayInWeek; i < 7; i++) 
    {
        str += "<td>&nbsp;</td>";
    }

    str += "</tr></table>";

    cnt.innerHTML = str;
}

function monthYearChanged()
{
	performDateChange(year,month+1,day);
}
function dayChanged(vl)
{
	arr = vl.split(",");
	year = arr[0];
	month = arr[1]-1;
	day = arr[2];

	performDateChange(year,month+1,day);
	initCalendar(curYY, curMM, curDD, fromYY, toYY);
}

function todayClicked()
{
	year = curYY;
	month = curMM-1;
	day = curDD;
	performDateChange(curYY,curMM,curDD);
	initCalendar(curYY, curMM, curDD, fromYY, toYY);
}