var ecalDateFormat = "DD/MM/YYYY";
ecalDateFormat = "dd/mm/yyyy";
var ecalDateSeparator = "/";

ecalCreateControl();

//var ns6 = document.getElementById&&!document.all;

function ecalCreateControl() {
    if (document.all) {
	    document.write(
		    '<iframe src="about:blank" id="EasyCalendarControlIFrame" style="DISPLAY: none; LEFT: 0px; POSITION: absolute; TOP: 0px"' +
		    '	class="eciframe" src="" frameBorder="0" scrolling="no"></iframe><span id="ctrlCalendar" style="POSITION: absolute" name="ctrlCalendar">' +
		    '</span>');
	} else {
	    document.write(
		    '<span id="EasyCalendarControlIFrame" name="EasyCalendarControlIFrame" style="POSITION: absolute"' +
		    '	width="89" height="65"></span><span id="ctrlCalendar" style="POSITION: absolute" name="ctrlCalendar">' +
		    '</span>');
    }
}

var ecalMonthNames = new Array();
ecalMonthNames[0] =  "Jan";
ecalMonthNames[1] =  "Feb";
ecalMonthNames[2] =  "Mar";
ecalMonthNames[3] =  "Apr";
ecalMonthNames[4] =  "May";
ecalMonthNames[5] =  "Jun";
ecalMonthNames[6] =  "Jul";
ecalMonthNames[7] =  "Aug";
ecalMonthNames[8] =  "Sep";
ecalMonthNames[9] =  "Oct";
ecalMonthNames[10] = "Nov";
ecalMonthNames[11] = "Dec";

var ecalCurrYear;
var ecalCurrMonth;
// var currentControl;
var ecalCurrentControlDay;
var ecalCurrentControlMonth;
var ecalCurrentControlYear;
var ecalOldOnClickBody;
var ecalIsCalendarShowing;

function ecalIsLeapYear(year) {	
	return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0));
}

function ecalDaysPerMonth(year, month) 
{
	switch (month) 
	{
		case 1: 
		case 3: 
		case 5: 
		case 7: 
		case 8: 
		case 10: 
		case 12: return 31;
		case 4: 
		case 6: 
		case 9: 
		case 11: return 30;
		case 2: return ecalIsLeapYear(year) ? 29 : 28;
		default: return 0;
	}
}

function ecalBuildCalendar(year, month) {
	var strCal = '<table border="0" cellpadding="0" cellspacing="2" class="calTable" onclick="event.cancelBubble=true">';
	
	strCal += '<tr><td align="center">';
	strCal += '<table border="0" cellpadding="0" cellspacing="0"><tr class="calMonth">';
	strCal += '<td><a href="#" class="CalNavLink" onclick="ecalCalPrevYear()"><<&nbsp;&nbsp;</a></td>';
	strCal += '<td><a href="#" class="CalNavLink" onclick="ecalCalPrevMonth()"><&nbsp;&nbsp;</a></td>';
	strCal += '<td class="monthName">' + ecalMonthNames[month-1] + ' (' + year + ')</td>';
	strCal += '<td>&nbsp;&nbsp;<a href="#" class="CalNavLink" onclick="ecalCalNextMonth()">></a>&nbsp;&nbsp;</td>';
	strCal += '<td><a href="#" class="CalNavLink" onclick="ecalCalNextYear()">>></a></td>';
	strCal += "</tr></table>";
	strCal += '</td></tr>';
	strCal += '<tr><td>';
	strCal += '<table border="0" cellpadding="0" cellspacing="4" class="calMain">';
	strCal += '<tr>';
	strCal += '<td class="calDayOfWeekWeekend">Su</td><td class="calDayOfWeek">Mo</td><td class="calDayOfWeek">Tu</td><td class="calDayOfWeek">We</td><td class="calDayOfWeek">Th</td><td class="calDayOfWeek">Fr</td><td class="calDayOfWeekWeekend">Sa</td>';
	strCal += '</tr>';

	strCal += '<tr>';
	var day = 1;
	var maxDay = ecalDaysPerMonth(year, month);	
	monthStart = new Date(year, month - 1, 1);

	var prevMonth;
	if ((month-1) > 1)
		prevMonth = new Date(year, month - 2, 1);
	else
		prevMonth = new Date(year - 1, 11, 1);

	var nextMonth;
	if (month < 12)
		nextMonth = new Date(year, month, 1);
	else
		nextMonth = new Date(year + 1, 0, 1);

	var prevMonthDays = ecalDaysPerMonth(prevMonth.getFullYear(), prevMonth.getMonth() + 1);
	//DayOfWee 0, 1, 2, 3, 4, 5, 6
	var monthDayOfWeek = monthStart.getDay();
	var calendarStartPrevMonthDay = prevMonthDays - monthDayOfWeek;

	for(var i=calendarStartPrevMonthDay+1; i<=prevMonthDays; i++)
		strCal += '<td class="calNotCurMonth"><a href="#" class="CalDateLink2" onclick="ecalSetDate(' + prevMonth.getFullYear() + ', ' + (prevMonth.getMonth() + 1) + ', ' + i + ');">' + ecalFormatNumber(i) + '</a></td>';
		
	var curDate;

	while(day <= maxDay){
		strCal += '<td align="center"><b><a href="#" class="CalDateLink" onclick="ecalSetDate(' + year + ', ' + month + ', ' + day + ');">' + ecalFormatNumber(day) + '</a></b></td>';
		day++;

		curDate = new Date(year, month - 1, day);
		if ((curDate.getDay()) == 0) 
		{
			strCal += "</tr><tr>";
		}
	}

	for(var i=1; i <= 7 - curDate.getDay(); i++)
		strCal += '<td><a href="#" class="CalDateLink2" onclick="ecalSetDate(' + nextMonth.getFullYear() + ', ' + (nextMonth.getMonth() + 1) + ', ' + i + ');">' + ecalFormatNumber(i) + '</a></td>';
	
	strCal += "</tr></table>";
	strCal += '</td></tr>';
	strCal += '</table>';

	document.getElementById('ctrlCalendar').innerHTML = strCal;	
}

function ecalShowCalendar(/*obj, */objDay, objMonth, objYear, event) {
	var e;
    //firefox fix
    if (!event)
        e=window.event;
    else
        e = event;
    
/*	if (ns6)
		e = event;
	else
		e = window.Event;*/

	var deltaY = 0;	
	var deltaX = 0;	
//	if (arguments.length == 2) {
//		deltaY = arguments[1]
//	}

    if (document.body.scrollTop + document.body.clientHeight - (e.clientY + 10 + deltaY + document.body.scrollTop) < 170) {
		deltaY = deltaY - (170-(document.body.scrollTop + document.body.clientHeight - (e.clientY + 10 + deltaY + document.body.scrollTop)));
	}
	if (document.body.scrollLeft + document.body.clientWidth - (e.clientX + 10 + deltaX + document.body.scrollLeft) < 190) {
		deltaX = deltaX - (190-(document.body.scrollLeft + document.body.clientWidth - (e.clientX + 10 + deltaX + document.body.scrollLeft)));
	}

    var ctr = document.getElementById("ctrlCalendar");

	//ctr.style.left = e.clientX + 10 + deltaX + document.body.scrollLeft + "px";
	//ctr.style.top = e.clientY + 10 + deltaY + document.body.scrollTop  + "px";
	
	var ctrf = document.getElementById('EasyCalendarControlIFrame');
	ctrf.style.top = ctr.style.top;
	ctrf.style.left = ctr.style.left;
	ctrf.style.display = 'block';
	
	ecalIsCalendarShowing = true;

//	currentControl = obj;
	ecalCurrentControlDay = objDay;
	ecalCurrentControlMonth = objMonth;
	ecalCurrentControlYear = objYear;

	var d = ecalGetCurrentDate(); //new Date();
	ecalCurrYear = d.getFullYear();
	ecalCurrMonth = d.getMonth() + 1;

	ecalBuildCalendar(ecalCurrYear, ecalCurrMonth);
	
	return false;
}

function ecalGetCurrentDate() {
    //due to the client side has unknown date format in comparison to the value in o.value
	/*var o = eval("document.forms[0]." + currentControl);

	if (o != undefined) {
		var d = new Date(o.value);
		if (!isNaN(d)) {
			return new Date()//o.value);
		}
		else {
			return new Date();
		}
	} else {
		return new Date();
	}*/
//	return new Date();

	var oDay = eval("document.forms[0]." + ecalCurrentControlDay);
	var oMonth = eval("document.forms[0]." + ecalCurrentControlMonth);
	var oYear = eval("document.forms[0]." + ecalCurrentControlYear);
	return new Date(ecalGetSelectedItem(oYear), ecalGetSelectedItem(oMonth) - 1, ecalGetSelectedItem(oDay));
}

function ecalGetSelectedItem(oCtl) {
    for (var i=0; i<oCtl.options.length; i++) 
    {
	    if (oCtl.options[i].selected) {
	        if (oCtl.options[i].value != "" || i+1 == oCtl.options.length) {
                return oCtl.options[i].value;
            } else {
                return oCtl.options[i+1].value;
            }
	    }
    }
   
    return oCtl.options[oCtl.options.length - 1].value;
}

function ecalCalPrevMonth() {	
	var d;
	if (ecalCurrMonth > 1)
		ecalCurrMonth = ecalCurrMonth - 1;
	else {
		ecalCurrYear = ecalCurrYear - 1;
		ecalCurrMonth = 12;
	}

	ecalBuildCalendar(ecalCurrYear, ecalCurrMonth);
}

function ecalCalNextMonth() {	
	var d;
	if (ecalCurrMonth < 12)
		ecalCurrMonth = ecalCurrMonth + 1;
	else {
		ecalCurrYear = ecalCurrYear + 1;
		ecalCurrMonth = 1;		
	}

	ecalBuildCalendar(ecalCurrYear, ecalCurrMonth);
}

function ecalCalPrevYear() {
	ecalCurrYear = ecalCurrYear - 1;
	ecalBuildCalendar(ecalCurrYear, ecalCurrMonth);
}

function ecalCalNextYear() {
	ecalCurrYear = ecalCurrYear + 1;
	ecalBuildCalendar(ecalCurrYear, ecalCurrMonth);
}

function ecalSetDate(year, month, date) {
/*	var o = eval("document.forms[0]." + currentControl);
	if (o != undefined) {
	    var tokens = ecalDateFormat.split(ecalDateSeparator);
		//o.value = ecalFormatNumber(month) + "/" + ecalFormatNumber(date) + "/" + year;
		o.value = ecalGetDateToken(tokens[0], year, month, date) + ecalDateSeparator + ecalGetDateToken(tokens[1], year, month, date) + ecalDateSeparator + ecalGetDateToken(tokens[2], year, month, date);
	}
*/
	var oDay = eval("document.forms[0]." + ecalCurrentControlDay);
	ecalSelectOption(oDay, date);
	var oMonth = eval("document.forms[0]." + ecalCurrentControlMonth);
	ecalSelectOption(oMonth, month);
	var oYear = eval("document.forms[0]." + ecalCurrentControlYear);
	ecalSelectOption(oYear, year);

	ecalHideCalendar();
}

function ecalSelectOption(oCtl, value) {
    if (oCtl.options.length>0) {
        oCtl.options[0].selected = true;
    }

    for (var i=0; i<oCtl.options.length; i++) 
    {
	    if (oCtl.options[i].value == value) {
		    oCtl.options[i].selected = true;
		    return;
	    }
    }

    if (oCtl.options.length>0) {
        oCtl.options[oCtl.options.length-1].selected = true;
    }
}

function ecalGetDateToken(token, year, month, date) {
    if (token == 'dd')
        return ecalFormatNumber(date);
    if (token == 'mm')
        return ecalFormatNumber(month);
    if (token == 'yyyy')
        return year;
}

function ecalFormatNumber(data) {
	if (data < 10)
		return "0" + data;
	else
		return data;
}

function ecalHideCalendar() {
//	if (ns6) {
	if (ecalIsCalendarShowing) {
		ecalIsCalendarShowing = false;
		return;
	}

	if (document.getElementById('ctrlCalendar').innerHTML != "")
		document.getElementById('ctrlCalendar').innerHTML = "";
		
	document.getElementById('EasyCalendarControlIFrame').style.display = 'none';
}

function ecalInitCalendar() {
	window.onresize = ecalHideCalendar;
	document.onkeydown = ecalHideCalendar;
	ecalOldOnClickBody = document.onclick;
	document.onclick = ecalOnBody;
	ecalIsCalendarShowing = false;
//	for(var i=1; i<=12; i++)
//		ecalBuildCalendar(year, 1);	//month + 1
}

function ecalOnBody() {
	ecalHideCalendar();
	if (ecalOldOnClickBody != undefined)
		ecalOldOnClickBody;
}

ecalInitCalendar();

