var weekend = [0,6];

var gNow = new Date();
var gCurrent = gNow;
var ggWinCal;
isNav = (navigator.appName.indexOf("Netscape") != -1) ? true : false;
isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;

var g_Calendar_Months = ["January", "February", "March", "April", "May", "June","July", "August", "September", "October", "November", "December"];
var g_Calendar_Days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];

Calendar.firstWeekDay = 1;
Calendar.closeWinMsg = "Close calendar";


// Non-Leap year Month days..
Calendar.DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// Leap year Month days..
Calendar.lDOMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

function Calendar(p_ItemDay, p_ItemMonthYear, p_ItemDate, p_WinCal, p_month, p_year, p_format) {
	if ((p_month == null) && (p_year == null))	return;

	if (p_WinCal == null)
		this.gWinCal = ggWinCal;
	else
		this.gWinCal = p_WinCal;
	
	if (p_month == null) {
		this.gMonthName = null;
		this.gMonth = null;
		this.gYearly = false;
	} 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.gReturnItemDate = p_ItemDate;
	this.gReturnItemDay = p_ItemDay;
	this.gReturnItemMonthYear = p_ItemMonthYear;
}

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 g_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, 10) - 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month, 10) - 1;
			ret_arr[1] = parseInt(p_Year, 10);
		}
	} 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, 10) + 1;
		}
		else {
			ret_arr[0] = parseInt(p_Month, 10) + 1;
			ret_arr[1] = parseInt(p_Year, 10);
		}
	}
	
	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..
	vCode = vCode + "<TABLE border='0'  cellpadding='3' width='100%' class=\"Calendar_BGColor\">";
	
	vHeader_Code = this.cal_header();
	vData_Code = this.cal_data();
	vCode = vCode + vHeader_Code + vData_Code;
	
	vCode = vCode + "</TABLE>";
	
	return vCode;
}

Calendar.prototype.show = function() {
	var vCode = "";
	
	this.gWinCal.document.open();

	// Setup the page...
	this.wwrite("<html>");
	this.wwrite("<head><title>calendar</title>");
	this.wwrite("");
	this.wwrite("<script LANGUAGE=\"javascript\">function setDateVal(p_ItemDay, p_ItemMonthYear,p_ItemDate,p_day,p_month,p_year,p_format){try{self.opener.setVal(p_ItemDay, p_ItemMonthYear,p_ItemDate,p_day,p_month,p_year,p_format);}catch(e){} self.close();}<\/script>");

	this.wwrite("<link href=\"LookAndFeel/Calendar.css\" type=\"text/css\" rel=\"stylesheet\">");

	this.wwrite("</head>");

	this.wwrite("<body>");


	// Show navigation buttons
	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];
	
	var vPrevCode = "";
	var yearMin = Calendar.yearMin ;
	var monthMin = Calendar.monthMin ;
	if(this.gYear < yearMin
	|| this.gYear == yearMin && this.gMonth <= monthMin)
	{
		//vPrevCode = "<img src=\"images/prev_calendar_dsbl.gif\" border=0>";
		vPrevCode = "&nbsp;";
	}
	else
	{
		vPrevCode = "<A HREF=\"" +
		"javascript:window.opener.Build(" + "'" + 
		this.gReturnItemDay + "', '" +
		this.gReturnItemMonthYear + "', '" +
		this.gReturnItemDate + "', '" + prevMM + "', '" + prevYYYY + "', '" + this.gFormat + "'" +
		");" +
		"\" class='Calendar_Arrows'>&laquo;<\/A>";
		//"\"><img src=\"images/prev_calendar.gif\" border=0><\/A>";
	}
	
	var vNextCode = "";
	if(nextYYYY > Calendar.yearMax)
	{
		//vNextCode = "<img src=\"images/next_calendar_dsbl.gif\" border=0>";
		vNextCode = "&nbsp;";
	}
	else
	{
		vNextCode = "<A HREF=\"" +
		"javascript:window.opener.Build(" + "'" + 
		this.gReturnItemDay + "', '" +
		this.gReturnItemMonthYear + "', '" +
		this.gReturnItemDate + "', '" + nextMM + "', '" + nextYYYY + "', '" + this.gFormat + "'" +
		");" +
		"\" class='Calendar_Arrows'>&raquo;<\/A>";
		//"\"><img src=\"images/next_calendar.gif\" border=0><\/A>";
	}
	

	this.wwrite("<table border='0' width='100%' cellpadding='2'><TR><TD ALIGN='center' valign='middle'>");
	
	this.wwrite(vPrevCode);
		
	this.wwriteA("</TD><TD width='100%' ALIGN='center' valign='middle' class='Calendar_CurrentMY'>");
	this.wwriteA(this.gMonthName + " " + this.gYear);
		
	this.wwrite("</TD><TD ALIGN='center' valign='middle'>");
	this.wwrite(vNextCode);
	this.wwrite("</TD></TR>");

	// Get the complete calendar code for the month..
	vCode = this.getMonthlyCalendarCode();
	this.wwrite(vCode);

	this.wwrite("<table align='center' height='' width='100%' border='0'><tr><td valign='bottom' align='center'><A HREF=\"javascript:self.close();\" class=\"Calendar_CloseMsg\">" + Calendar.closeWinMsg + "</a>");
	
	this.wwrite("</td></tr></table></html>");
	this.gWinCal.document.close();
}


Calendar.prototype.wwrite = function(wtext) {
	this.gWinCal.document.writeln(wtext);
}

Calendar.prototype.wwriteA = function(wtext) {
	this.gWinCal.document.write(wtext);
}

Calendar.prototype.cal_header = function() {
	var vCode = "";
	var vFWD = Calendar.firstWeekDay;
	var i;
	vCode = vCode + "<TR width='100%'>";
	for(i=vFWD; i<g_Calendar_Days.length+vFWD; i++)
	{
		vCode = vCode + "<TD class=\"Calendar_DayHeadings\" align='center'><B>" + g_Calendar_Days[i%7] + "</B></TD>";
	}
	
	vCode = vCode + "</TR>";
	
	return vCode;
}

Calendar.prototype.cal_data = function() {
	var vFWD = Calendar.firstWeekDay;
//	var vDate = new Date(Calendar.yearNow, Calendar.monthNow, Calendar.dayNow);
	var vDate = new Date(Calendar.yearMin, Calendar.monthMin, Calendar.dayMin);

	if(vDate.getMonth()!= this.gMonth || vDate.getFullYear() != this.gYear)
	{
		vDate.setDate(1);
		vDate.setMonth(this.gMonth);
		vDate.setFullYear(this.gYear);
	}

	var vFirstDay=(vFWD > vDate.getDay())? 7+vDate.getDay() : vDate.getDay();
	var vDay=vDate.getDate();
	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 width='100%'>";
	for (i=vFWD; i<vFirstDay; i++) {
		vCode = vCode + "<TD WIDTH='14%' align='center'" + this.write_weekend_string(i%7) + "></TD>";
	}

	// Write rest of the 1st week
	for (j=vFirstDay; j<7+vFWD; j++) {
		vCode = vCode + "<TD WIDTH='14%' align='center'" + this.write_weekend_string(j%7) + ">" + 
			"<A HREF=\"javascript:setDateVal('" + 
					this.gReturnItemDay + "', '" +
					this.gReturnItemMonthYear + "', '" + 
					this.gReturnItemDate + "', " + vDay + "," + this.gMonth + "," + this.gYear + 
					", '" + this.gFormat + "');\"" + this.write_weekend_style(j%7) + " >" + 
				this.format_day(vDay, this.gMonth) + 
				"</A>" + 
			"</TD>";
		vDay=vDay + 1;

		if (j == 6+vFWD)
			vCode = vCode + "</TR>\n";
			
		if (vDay > vLastDay) {
			vOnLastDay = 1;
			break;
		}		
	}

	// Write the rest of the weeks
	if(vOnLastDay==0)
	{	
		for (k=2; k<7; k++) {
			vCode = vCode + "<TR>";
	
			for (j=vFWD; j<7+vFWD; j++) {
				vCode = vCode + "<TD WIDTH='14%' align='center'" + this.write_weekend_string(j%7) + ">" + 
					"<A HREF=\"javascript:setDateVal('" + 
						this.gReturnItemDay + "', '" +
						this.gReturnItemMonthYear + "', '" +
						this.gReturnItemDate + "', " + vDay + "," + this.gMonth + "," + this.gYear + 
						", '" + this.gFormat + "');\"" + this.write_weekend_style(j%7) + ">" + 
					this.format_day(vDay, this.gMonth) + 
					"</A>" + 
					"</TD>";
				vDay=vDay + 1;
	
				if (vDay > vLastDay) {
					vOnLastDay = 1;
					break;
				}
			}
	
			if (j == 7+vFWD)
				vCode = vCode + "</TR>\n";
			if (vOnLastDay == 1)
				break;
		}
	}
	
	var nextMMYYYY = Calendar.calc_month_year(this.gMonth, this.gYear, 1);
	var nextMM = nextMMYYYY[0];
	var nextYYYY = nextMMYYYY[1];
		
	// Fill up the rest of last week with proper blanks, so that we get proper square blocks
	for (m=1; m<(7-j)+vFWD; m++) {
			vCode = vCode + "<TD WIDTH='14%' align='center'" + this.write_weekend_string((j+m)%7) + 
			"><A HREF=\"javascript:setDateVal('" + 
						this.gReturnItemDay + "', '" +
						this.gReturnItemMonthYear + "', '" + 
						this.gReturnItemDate + "', " + m + "," + nextMM + "," + nextYYYY + 
						", '" + this.gFormat + "');\"" + this.write_weekend_style(j%7) + ">" + 
					this.format_day(m, this.gMonth+1) + 
					"</A>" + "</TD>";
			//">" + m + "</TD>";
			vOnLastDay = 2;
	}

	if (vOnLastDay == 2)
		vCode = vCode + "</TR>\n";	

	return vCode;
}

Calendar.prototype.format_day = function(vday, vmonth) {
	var xNow = new Date();

	var vNowDay = xNow.getDate();
	var vNowMonth = xNow.getMonth();
	var vNowYear = xNow.getFullYear();

	if (vday == vNowDay && vmonth == vNowMonth && this.gYear == vNowYear)
		// highlite current day
		return ("<span class='Calendar_CurDate'>" + vday + "</span>");

	vNowDay = gCurrent.getDate();
	vNowMonth = gCurrent.getMonth();
	vNowYear = gCurrent.getFullYear();

	if (vday == vNowDay && vmonth == vNowMonth && this.gYear == vNowYear)
		// highlite current day
		return ("<span class='Calendar_SelectedDate'>" + vday + "</span>");
		
	return (vday);
}

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 (" class='Calendar_Weekend'");
	}
	
	return "";
}

Calendar.prototype.write_weekend_style = function(vday) {
	var i;

	// Return special formatting for the weekend day.
	for (i=0; i<weekend.length; i++) {
		if (vday == weekend[i])	
			return (" class=\"Calendar_WkEnd\"");
	}
	
	return (" class=\"Calendar_WkDay\"");
}

function formateDate(p_day,p_month,p_year,p_format) {
	var date = new String(p_format);
	
	date = date.replace("DD", (p_day.toString().length < 2) ? "0" + p_day : p_day);
	date = date.replace("D", p_day);
	
	var month = parseInt(p_month.toString(), 10)+1;
	date = date.replace("MM", month < 10 ? "0" + new String(month) : new String(month));
	date = date.replace("M", new String(month));
	date = date.replace("YYYY", p_year);
	date = date.replace("YY", p_year.toString().substr(2,2));

	return (date);
}

function setVal(p_ItemDay, p_ItemMonthYear,p_ItemDate,p_day,p_month,p_year,p_format) {
	if (p_ItemDay != "null" && p_ItemMonthYear != "null")
	{
		// p_ItemDay=DD and p_ItemMonthYear=YYYYMM
		var oDay = getObject(p_ItemDay);
		var oMonthYear = getObject(p_ItemMonthYear);
		oDay.value=(p_day.toString().length < 2) ? "0" + p_day : p_day;
		p_month++;
		var xMonth = (p_month.toString().length < 2) ? "0" + p_month: p_month;
		oMonthYear.value= p_year.toString() + xMonth;
	}
	
	if (p_ItemDate != "null")
	{
		// p_ItemDate=formatted like p_format
		var oDate = getObject(p_ItemDate);
		oDate.value = formateDate(p_day,p_month,p_year,p_format);
	}
}

function Build(p_ItemDay, p_ItemMonthYear, p_ItemDate, p_month, p_year, p_format) {
	var p_WinCal = ggWinCal;
	gCal = new Calendar(p_ItemDay, p_ItemMonthYear, p_ItemDate, p_WinCal, p_month, p_year, p_format);

	// Customize your Calendar here..

	// Choose appropriate show function
	gCal.show();
}

function show_calendar() {
	/* 
		p_month : 0-11 for Jan-Dec; 12 for All Months.
		p_year	: 4-digit year
		p_ItemMonthYear	: Return Item.
	*/

	p_ItemDay = arguments[0];
	p_ItemMonthYear = arguments[1];	

	var oDay = getObject(p_ItemDay);
	var oMonthYear = getObject(p_ItemMonthYear);
	var year = parseInt(oMonthYear.value.toString().substr(0, 4), 10);
	var month = parseInt(oMonthYear.value.toString().substr(4, 2), 10)-1;
	var day = parseInt(oDay.value.toString(), 10)
	gCurrent = new Date (year, month, day);
	p_format = "MM/DD/YYYY";
	
	// Lookat
	p_month = new String(gCurrent.getMonth());
	p_year = new String(gCurrent.getFullYear());
	
	// Start at...
	Calendar.dayMin = gNow.getDate();
	Calendar.monthMin = gNow.getMonth();
	Calendar.yearMin = gNow.getFullYear();
		
	Calendar.dayNow = gNow.getDate();
	Calendar.monthNow = gNow.getMonth();
	Calendar.yearNow = gNow.getFullYear();
	
	Calendar.yearMax = Calendar.yearNow + 1;

	vWinCal = window.open("", "HelRes_Calendar", 
		"width=280,height=248,status=no,resizable=no,top=30,left=250");
	vWinCal.focus();

	//vWinCal.opener = self;
	ggWinCal = vWinCal;
       
	Build(p_ItemDay, p_ItemMonthYear, null, p_month, p_year, p_format);
}

function parseDate(date,format) 
{
	if (date == "")	
	{ // empty date passed!! return todays date
		var today = new Date();
		var month = today.getMonth()+1;
		ret = new String(today.getFullYear());
		ret += month < 10 ? "0"+month : month;
		ret += today.getDate() < 10 ? "0"+today.getDate() : today.getDate();
		return ret;
	}
	else
	{
		temp = new String();	day = "00";	month = "00";	year = "0000";
		
		// parse the format string to finde out the order m, d and y
		var j = 0;	var order = "";
		for(i=0; i<format.length;i++) {
			charat = format.charAt(i);
			if (temp != charat && (charat == "M" || charat == "D" || charat == "Y")) {	// valid format characters
				order += charat;
			}
			temp = charat;
		}
		
		// now parse the date string
		j=0; temp = "";
		for(i=0; i<date.length;i++) {
			charat = date.charAt(i);
			if (charat == "0" 
 			|| charat == "1"  
			|| charat == "2"  
			|| charat == "3"
			|| charat == "4"
			|| charat == "5"
			|| charat == "6"
			|| charat == "7"
			|| charat == "8"
			|| charat == "9") 
			{
				temp = temp + date.charAt(i);
			}
			
			if (!(charat == "0"
			|| charat == "1"
			|| charat == "2"
			|| charat == "3"
			|| charat == "4"
			|| charat == "5"
			|| charat == "6"
			|| charat == "7"
			|| charat == "8"
			|| charat == "9") // no digit
			|| i==date.length-1) // last digit
			{
				// a delimiter was found.. and with date part was recognized?
				if (order.charAt(j) == "M")
					month = temp.length < 2 ? "0"+temp:temp;
				else if (order.charAt(j) == "D")	
					day = temp.length < 2 ? "0"+temp:temp;
				else if (order.charAt(j) == "Y")
					year = temp.length < 2 ? "0"+temp:temp;
					
				j++;
				temp = "";
			}
		}
	}

	ret = new String(year);
	ret += month;
	ret += day;
	return (ret);
}

function show_calendar2() {
	/* 
		p_format: Date format (MM/DD/YYYY, DD.MM.YY, ...)
		p_Item	: Return Item.
	*/

	p_Item = arguments[0];
	p_format = new String(arguments[1]);

	var oItem = getObject(p_Item);
	var current = parseDate(oItem.value.toString(),p_format);
	var year = parseInt(current.substr(0, 4), 10);
	var month = parseInt(current.substr(4, 2), 10)-1;
	var day = parseInt(current.substr(6, 2), 10);
	
	// Preselected 
	gCurrent = new Date (year, month, day);
	
	// Lookat
	gNow = new Date(year,month,day);
	
	p_month = new String(gNow.getMonth());
	p_year = new String(gNow.getFullYear());

	// Start at...
	Calendar.dayMin = 1;
	Calendar.monthMin = month;
	Calendar.yearMin = 2003;
		
	Calendar.dayNow = gNow.getDate();
	Calendar.monthNow = gNow.getMonth();
	Calendar.yearNow = gNow.getFullYear();
	
	Calendar.yearMax = Calendar.yearNow + 2;

	vWinCal = window.open("", "HelRes_Calendar", 
		"width=280,height=248,status=no,resizable=no,top=30,left=250");
	vWinCal.focus();
	
	//vWinCal.opener = self;
	ggWinCal = vWinCal;
       
	Build(null, null, p_Item, p_month, p_year, p_format);
}
