//custom function
var iIntID;
var iCur;
function OpenPanel(pnlID)
{
	pnl = document.getElementById(pnlID);
	pnl.style.visibility = "visible";
	pnl.style.display = "inline";
	iCur = 0;
	pnl.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=0)";
	iIntID = window.setInterval("showPanel('"+pnlID+"')",10);
}
function showPanel(pnlID)
{
	pnl = document.getElementById(pnlID);
	var sFilter = "progid:DXImageTransform.Microsoft.Alpha(opacity={0})";
	if(iCur < 100)
	{
		iCur += 20;
		pnl.style.filter = sFilter.replace("{0}",iCur);
	}
	else
	{
		clearInterval(iIntID);
		eval(afteropened);
	}
}
function ClosePanel(pnlID)
{
	pnl = document.getElementById(pnlID);
	pnl.style.visibility = "hidden";
	pnl.style.display = "none";
	ggPanel = "";
	eval(afterselected);
}
function ReturnData(returnitem,data,pnlID)
{
	var item = document.getElementById(returnitem);
	item.value = data;
	ClosePanel(pnlID);
}
// Customize your Calendar here..
var weekend = [0,6];
var dateFormat = "MM/dd/yyyy";
var cssHeader = "";
var cssLink = "";
var cssCurrent = "";
var cssWeekend = "";
var cssWeekday = "";
var cssItem = "";
var cssFooter = "";
var cssTable = "";
var borderColor = "";
var borderStyle = "";
var borderWidth = "";
var cellPadding = "";
var cellSpacing = "";
var bgTable = "";
var bgHeader = "";
var bgFooter = "";
var bgWeekend = "";
var afteropened = "";
var afterselected = ""
//	End customize
var selectedDate = null;
var gNow = new Date();
isNav = (navigator.appName.indexOf("Netscape") != -1) ? true : false;
isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;
var ggPanel = "";
Calendar.Months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
Calendar.DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
Calendar.lDOMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

function WriteAttribute(attname, attval)
{
	if(attval.length > 0)
		return " " + attname + "='" + attval + "' ";
	return "";
}
function Calendar_Build(p_item, p_month, p_year, p_format) {
	var p_Panel = ggPanel;
	gCal = new Calendar(p_item, p_Panel, p_month, p_year, p_format);	
	gCal.show();
}

//calendar class
function Calendar(p_item, p_Panel, p_month, p_year, p_format) {
	if ((p_month == null) && (p_year == null))	return;

	if (p_Panel == null)
		this.gPanel = ggPanel;
	else
		this.gPanel = p_Panel;
	
	if (p_month == null) {
		this.gMonthName = null;
		this.gMonth = null;
		this.gYearly = true;
	} else {
		this.gMonthName = Calendar.get_month(p_month);
		this.gMonth = new Number(p_month);
		this.gYearly = false;
	}

	this.gYear = p_year;
	this.gFormat = p_format;
	this.gReturnItem = p_item;
}

Calendar.get_month = Calendar_get_month;
Calendar.get_daysofmonth = Calendar_get_daysofmonth;
Calendar.calc_month_year = Calendar_calc_month_year;

function Calendar_get_month(monthNo) {
	return Calendar.Months[monthNo];
}

function Calendar_get_daysofmonth(monthNo, p_year) {
	/* 
	Check for leap year ..
	1.Years evenly divisible by four are normally leap years, except for... 
	2.Years also evenly divisible by 100 are not leap years, except for... 
	3.Years also evenly divisible by 400 are leap years. 
	*/
	if ((p_year % 4) == 0) {
		if ((p_year % 100) == 0 && (p_year % 400) != 0)
			return Calendar.DOMonth[monthNo];
	
		return Calendar.lDOMonth[monthNo];
	} else
		return Calendar.DOMonth[monthNo];
}

function Calendar_calc_month_year(p_Month, p_Year, incr) {
	/* 
	Will return an 1-D array with 1st element being the calculated month 
	and second being the calculated year 
	after applying the month increment/decrement as specified by 'incr' parameter.
	'incr' will normally have 1/-1 to navigate thru the months.
	*/
	var ret_arr = new Array();
	
	if (incr == -1) {
		// B A C K W A R D
		if (p_Month == 0) {
			ret_arr[0] = 11;
			ret_arr[1] = parseInt(p_Year) - 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) - 1;
			ret_arr[1] = parseInt(p_Year);
		}
	} else if (incr == 1) {
		// F O R W A R D
		if (p_Month == 11) {
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(p_Year) + 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) + 1;
			ret_arr[1] = parseInt(p_Year);
		}
	}
	
	return ret_arr;
}

function Calendar_calc_month_year(p_Month, p_Year, incr) {
	/* 
	Will return an 1-D array with 1st element being the calculated month 
	and second being the calculated year 
	after applying the month increment/decrement as specified by 'incr' parameter.
	'incr' will normally have 1/-1 to navigate thru the months.
	*/
	var ret_arr = new Array();
	
	if (incr == -1) {
		// B A C K W A R D
		if (p_Month == 0) {
			ret_arr[0] = 11;
			ret_arr[1] = parseInt(p_Year) - 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) - 1;
			ret_arr[1] = parseInt(p_Year);
		}
	} else if (incr == 1) {
		// F O R W A R D
		if (p_Month == 11) {
			ret_arr[0] = 0;
			ret_arr[1] = parseInt(p_Year) + 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month) + 1;
			ret_arr[1] = parseInt(p_Year);
		}
	}
	
	return ret_arr;
}

// This is for compatibility with Navigator 3, we have to create and discard one object before the prototype object exists.
new Calendar();

Calendar.prototype.getMonthlyCalendarCode = function() {
	var vCode = "";
	var vHeader_Code = "";
	var vData_Code = "";
	
	// Begin Table Drawing code here..
	vHeader_Code = this.cal_header();
	vData_Code = this.cal_data();
	vCode = vCode + vHeader_Code + vData_Code;
	
	return vCode;
}

Calendar.prototype.show = function() {
	var vCode = "";
	var sHtml = "";
	var pnl = document.getElementById(this.gPanel);
	pnl.innerHTML = pnl.innerText = "";
	var prevMMYYYY = Calendar.calc_month_year(this.gMonth, this.gYear, -1);
	var prevMM = prevMMYYYY[0];
	var prevYYYY = prevMMYYYY[1];

	var nextMMYYYY = Calendar.calc_month_year(this.gMonth, this.gYear, 1);
	var nextMM = nextMMYYYY[0];
	var nextYYYY = nextMMYYYY[1];	

	sHtml += "<TABLE BGCOLOR='" + bgTable + "' WIDTH='100%' HEIGHT='100%' CELLPADDING='0' CELLSPACING='" + cellSpacing + "' STYLE='border: solid " + borderWidth + " " + borderColor + "' bgcolor='white'" + WriteAttribute("CLASS",cssTable.toString()) + ">\n";
	// Show navigation buttons
	sHtml += "<TR" + WriteAttribute("BGCOLOR",bgHeader) + ">\n";
	sHtml += "<TD " + WriteAttribute("CLASS",cssHeader) + "ALIGN='left' nowrap style='padding: " + cellPadding + "'>\n";
	sHtml += "<A " + WriteAttribute("CLASS",cssLink) + "HREF=\"javascript:Calendar_Build('" + this.gReturnItem + "', '" + this.gMonth + "', '" + (parseInt(this.gYear)-1) + "', '" + this.gFormat + "');\">[<<]<\/A>";
	sHtml += "<A " + WriteAttribute("CLASS",cssLink) + "HREF=\"javascript:Calendar_Build('" + this.gReturnItem + "', '" + prevMM + "', '" + prevYYYY + "', '" + this.gFormat + "');\">[<]<\/A>";
	sHtml += "</TD>\n";
	sHtml += "<TD " + WriteAttribute("CLASS",cssHeader) + "ALIGN='center' style='padding: " + cellPadding + "' nowrap>\n";
	sHtml += "<B>" + this.gMonthName + " " + this.gYear + "</b>\n";
	sHtml += "</TD>\n";
	sHtml += "<TD " + WriteAttribute("CLASS",cssHeader) + "ALIGN='right' nowrap style='padding: " + cellPadding + "'>\n";
	sHtml += "<A " + WriteAttribute("CLASS",cssLink) + "HREF=\"javascript:Calendar_Build('" + this.gReturnItem + "', '" + nextMM + "', '" + nextYYYY + "', '" + this.gFormat + "');\">[>]<\/A>";
	sHtml += "<A " + WriteAttribute("CLASS",cssLink) + "HREF=\"javascript:Calendar_Build('" + this.gReturnItem + "', '" + this.gMonth + "', '" + (parseInt(this.gYear)+1) + "', '" + this.gFormat + "');\">[>>]<\/A>\n";
	sHtml += "</TD>\n";
	sHtml += "</TR>\n";
	sHtml += "<TR><TD colspan='3'><TABLE " + WriteAttribute("CLASS",cssTable) + "WIDTH='100%' HEIGHT='100%' CELLPADDING='0' CELLSPACING='" + cellSpacing + "' STYLE='BORDER-TOP: solid " + borderWidth + " " + borderColor + "; BORDER-BOTTOM: solid " + borderWidth + " " + borderColor + "'>\n";
	// Get the complete calendar code for the month..
	vCode = this.getMonthlyCalendarCode();
	sHtml += vCode;
	// Get the footer
	sHtml += "</TABLE></TD></TR>\n";
	sHtml += "<tr" + WriteAttribute("BGCOLOR",bgFooter) + ">\n";
	sHtml += "<td " + WriteAttribute("CLASS",cssFooter) + "colspan='3' align='center' valign='middle' style='padding: " + cellPadding + "'>\n";
	sHtml += "<A " + WriteAttribute("CLASS",cssLink) + "HREF=\"javascript:ReturnData('" + this.gReturnItem + "','','" + this.gPanel + "')\">Clear Date</A>\n";
	sHtml += "&nbsp;&nbsp;|&nbsp;&nbsp;<A " + WriteAttribute("CLASS",cssLink) + "HREF=\"javascript:ClosePanel('" + this.gPanel + "')\">Close</A>\n";
	sHtml += "</td>\n";
	sHtml += "</tr>\n";
	sHtml += "</TABLE>";
	this.wwrite(sHtml);
}

Calendar.prototype.wwrite = function(wtext) {
	var pnl = document.getElementById(this.gPanel);
	pnl.innerHTML = pnl.innerHTML + wtext;
	//this.gPanel.insertAdjacentHTML('beforeEnd',wtext);
}

Calendar.prototype.cal_header = function() {
	var vCode = "";
	
	vCode = vCode + "<TR>";
	vCode = vCode + "<TD WIDTH='" + (100/7) + "%' " + WriteAttribute("CLASS",cssWeekday) + "align='center' valign='middle' style='padding: " + cellPadding + "'><B>Sun</B></TD>\n";
	vCode = vCode + "<TD WIDTH='" + (100/7) + "%' " + WriteAttribute("CLASS",cssWeekday) + "align='center' valign='middle' style='padding: " + cellPadding + "'><B>Mon</B></TD>\n";
	vCode = vCode + "<TD WIDTH='" + (100/7) + "%' " + WriteAttribute("CLASS",cssWeekday) + "align='center' valign='middle' style='padding: " + cellPadding + "'><B>Tue</B></TD>\n";
	vCode = vCode + "<TD WIDTH='" + (100/7) + "%' " + WriteAttribute("CLASS",cssWeekday) + "align='center' valign='middle' style='padding: " + cellPadding + "'><B>Wed</B></TD>\n";
	vCode = vCode + "<TD WIDTH='" + (100/7) + "%' " + WriteAttribute("CLASS",cssWeekday) + "align='center' valign='middle' style='padding: " + cellPadding + "'><B>Thu</B></TD>\n";
	vCode = vCode + "<TD WIDTH='" + (100/7) + "%' " + WriteAttribute("CLASS",cssWeekday) + "align='center' valign='middle' style='padding: " + cellPadding + "'><B>Fri</B></TD>\n";
	vCode = vCode + "<TD WIDTH='" + (100/7) + "%' " + WriteAttribute("CLASS",cssWeekday) + "align='center' valign='middle' style='padding: " + cellPadding + "'><B>Sat</B></TD>\n";
	vCode = vCode + "</TR>";
	
	return vCode;
}

Calendar.prototype.cal_data = function() {
	var vDate = new Date();
	vDate.setDate(1);
	vDate.setMonth(this.gMonth);
	vDate.setFullYear(this.gYear);
	var vFirstDay=vDate.getDay();
	var vDay=1;
	var vLastDay=Calendar.get_daysofmonth(this.gMonth, this.gYear);
	var vOnLastDay=0;
	var vCode = "";	

	/*
	Get day for the 1st of the requested month/year..
	Place as many blank cells before the 1st day of the month as necessary. 
	*/

	vCode = vCode + "<TR>\n";
	for (i=0; i<vFirstDay; i++) {
		vCode += "<TD" + this.write_weekend_string(i) + " align='center' valign='middle' style='padding: " + cellPadding + "'>&nbsp;</TD>\n";
	}

	// Write rest of the 1st week
	for (j=vFirstDay; j<7; j++) {
		vCode += this.format_day(vDay);
		vDay=vDay + 1;
	}
	vCode += "</TR>\n";

	var rowCount = 1;

	// Write the rest of the weeks
	for (k=2; k<7; k++) {

		rowCount++;
				
		vCode = vCode + "<TR>\n";		
		for (j=0; j<7; j++) {
			vCode += this.format_day(vDay);
			vDay=vDay + 1;

			if (vDay > vLastDay) {
				vOnLastDay = 1;
				break;
			}
		}

		if (j == 6)
			vCode = vCode + "</TR>\n";			
		if (vOnLastDay == 1)
			break;
	}
	
	// Fill up the rest of last week with proper blanks, so that we get proper square blocks
	for (m=1; m<(7-j); m++) {
		vCode = vCode + "<TD" + WriteAttribute("CLASS",cssItem) + this.write_weekend_string(j+m) + " align='center' valign='middle'>" + m + "</TD>\n";
	}
	
	// Fill up the last of row
//	if(rowCount < 6)
//	{
//		vCode = vCode + "<TR>";
//		for (t=0; t<7; t++) {
//			vCode += "<TD" + WriteAttribute("CLASS",cssItem) + this.write_weekend_string(t) + " align='center' valign='middle'>" + (m++) + "</TD>";
//		}
//		vCode = vCode + "</TR>";
//	}
	
	return vCode;
}

Calendar.prototype.format_day = function(vday) {
	var vNowDay = gNow.getDate();
	var vNowMonth = gNow.getMonth();
	var vNowYear = gNow.getFullYear();
	var vCode = "";

	if (vday == vNowDay && this.gMonth == vNowMonth && this.gYear == vNowYear)
	{
		vCode += "<TD" + WriteAttribute("CLASS",cssCurrent) + this.write_weekend_string(j) + " align='center' valign='middle' style='padding: " + cellPadding + "'>\n"; 
		vCode += "<A " + WriteAttribute("CLASS",cssLink) + "HREF=\"javascript:ReturnData('" + this.gReturnItem + "','" + this.format_data(vday) + "','" + this.gPanel + "')\">";
		if(cssCurrent.length <= 0)
			vCode += "<B>" + vday + "</B></A>\n";
		else
			vCode += vday + "</A>\n";
	}
	else
	{
		vCode += "<TD" + this.write_weekend_string(j) + " align='center' valign='middle' style='padding: " + cellPadding + "'>\n"; 
		vCode += "<A " + WriteAttribute("CLASS",cssLink) + "HREF=\"javascript:ReturnData('" + this.gReturnItem + "','" + this.format_data(vday) + "','" + this.gPanel + "')\">" + vday + "</A>\n"; 
	}
	return vCode + "</TD>\n";
}

Calendar.prototype.write_weekend_string = function(vday) {
	var i;

	// Return special formatting for the weekend day.
	for (i=0; i<weekend.length; i++) {
		if (vday == weekend[i])
			return WriteAttribute("BGCOLOR",bgWeekend) + WriteAttribute("CLASS",cssWeekend);
	}
	return WriteAttribute("CLASS",cssItem);
}

Calendar.prototype.format_data = function(p_day) {
	var vData;
	var vMonth = 1 + this.gMonth;
	vMonth = (vMonth.toString().length < 2) ? "0" + vMonth : vMonth;
	var vMon = Calendar.get_month(this.gMonth).substr(0,3).toUpperCase();
	var vFMon = Calendar.get_month(this.gMonth).toUpperCase();
	var vY4 = new String(this.gYear);
	var vY2 = new String(this.gYear.substr(2,2));
	var vDD = (p_day.toString().length < 2) ? "0" + p_day : p_day;

	switch (this.gFormat) {
		case "MM\/DD\/YYYY" :
			vData = vMonth + "\/" + vDD + "\/" + vY4;
			break;
		case "MM\/DD\/YY" :
			vData = vMonth + "\/" + vDD + "\/" + vY2;
			break;
		case "MM-DD-YYYY" :
			vData = vMonth + "-" + vDD + "-" + vY4;
			break;
		case "MM-DD-YY" :
			vData = vMonth + "-" + vDD + "-" + vY2;
			break;

		case "DD\/MON\/YYYY" :
			vData = vDD + "\/" + vMon + "\/" + vY4;
			break;
		case "DD\/MON\/YY" :
			vData = vDD + "\/" + vMon + "\/" + vY2;
			break;
		case "DD-MON-YYYY" :
			vData = vDD + "-" + vMon + "-" + vY4;
			break;
		case "DD-MON-YY" :
			vData = vDD + "-" + vMon + "-" + vY2;
			break;

		case "DD\/MONTH\/YYYY" :
			vData = vDD + "\/" + vFMon + "\/" + vY4;
			break;
		case "DD\/MONTH\/YY" :
			vData = vDD + "\/" + vFMon + "\/" + vY2;
			break;
		case "DD-MONTH-YYYY" :
			vData = vDD + "-" + vFMon + "-" + vY4;
			break;
		case "DD-MONTH-YY" :
			vData = vDD + "-" + vFMon + "-" + vY2;
			break;

		case "DD\/MM\/YYYY" :
			vData = vDD + "\/" + vMonth + "\/" + vY4;
			break;
		case "DD\/MM\/YY" :
			vData = vDD + "\/" + vMonth + "\/" + vY2;
			break;
		case "DD-MM-YYYY" :
			vData = vDD + "-" + vMonth + "-" + vY4;
			break;
		case "DD-MM-YY" :
			vData = vDD + "-" + vMonth + "-" + vY2;
			break;

		default :
			vData = vMonth + "\/" + vDD + "\/" + vY4;
	}

	return vData;
}

function wapper_Calendar() {
	/* 
		p_month : 0-11 for Jan-Dec; 12 for All Months.
		p_year	: 4-digit year
		p_format: Date format (mm/dd/yyyy, dd/mm/yy, ...)
		p_item	: Return Item.
	*/
	if (arguments[22] != null || arguments[22] != "")//OnAction script
		eval(arguments[22]);
		
	p_item = arguments[0];
	if(ggPanel != null && ggPanel != "")
		ClosePanel(ggPanel);
	ggPanel = arguments[1];
	OpenPanel(ggPanel);
	var pnl = document.getElementById(ggPanel);
	var tb = document.getElementById(p_item);
	pnl.style.left = tb.offsetLeft;// = 0;

	if (arguments[2] == null || arguments[2] == "")
		p_month = new String(gNow.getMonth());
	else
		p_month = arguments[2];
	if (arguments[3] == null || arguments[3] == "")
		p_year = new String(gNow.getFullYear().toString());
	else
		p_year = arguments[3];
	if (arguments[4] == null || arguments[4] == "")
		p_format = "DD/MM/YYYY";
	else
		p_format = arguments[4];
	
	//	Set Customize
	if(arguments[5] != null)
		cssHeader = arguments[5];
	if(arguments[6] != null)
		cssLink = arguments[6];
	if(arguments[7] != null)
		cssCurrent = arguments[7];
	if(arguments[8] != null)
		cssWeekend = arguments[8];
	if(arguments[9] != null)
		cssWeekday = arguments[9];
	if(arguments[10] != null)
		cssItem = arguments[10];
	if(arguments[11] != null)
		cssFooter = arguments[11];
	if(arguments[12] != null)
		borderColor = arguments[12];
	if(arguments[13] != null)
		borderStyle = arguments[13];
	if(arguments[14] != null)
		borderWidth = arguments[14];
	if(arguments[15] != null)
		cellPadding = arguments[15];
	if(arguments[16] != null)
		cellSpacing = arguments[16];
	if(arguments[17] != null)
		cssTable = arguments[17];
	if(arguments[18] != null)
		bgHeader = arguments[18];
	if(arguments[19] != null)
		bgFooter = arguments[19];
	if(arguments[20] != null)
		bgWeekend = arguments[20];
	if(arguments[21] != null)
		bgTable = arguments[21];
	if(arguments[23] != null)
		afteropened = arguments[23];
	if(arguments[24] != null)
		afterselected = arguments[24];

	//	End Customize
	Calendar_Build(p_item, p_month, p_year, p_format);
	
//	var txt = document.getElementById('text');
//	txt.innerText = ggPanel.innerHTML;
}