<!--

// weekend day numbers array
var weekend = [6,5];
// show link in calendar
var links=true;
var gNow = new Date();
var ggWinCal;
var gField;
// date format
var format = "YYYY-MM-DD";
var sep="-";
// server context
var contextPath = "..";


// month names array
Calendar.Months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September","October", "November", "December"];

// days abbrevations array
Calendar.Days = ["Mo", "Tu", "We", "Th", "Fr", "Sa","Su"];

isNav = (navigator.appName.indexOf("Netscape") != -1) ? true : false;
isIE = (navigator.appName.indexOf("Microsoft") != -1) ? true : false;


// 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_item, p_WinCal, p_month, p_year) {

	this.getMonth = Calendar_get_month;
//	this.getDaysOfMonth = Calendar_get_daysofmonth;
	this.calcMonthYear = Calendar_calc_month_year;

	if ((p_month == null) && (p_year == null)) return;
	if (p_WinCal == null)
		this.gWinCal = ggWinCal;
	else
		this.gWinCal = p_WinCal;

	this.gMonthName = this.getMonth(p_month);
	this.gMonth = new Number(p_month);
	this.gYear = p_year;
	this.gReturnItem = p_item;
}

//Calendar.get_month = Calendar_get_month;
//Calendar.get_daysofmonth = Calendar_get_daysofmonth;
//Calendar.calc_month_year = Calendar_calc_month_year;

// Returns month name
function Calendar_get_month(monthNo) {
	return Calendar.Months[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;
}

Calendar.prototype.getDaysOfMonth = function(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];
}

Calendar.prototype.getMonthlyCalendarCode = function() {
	var vCode = "";
	var vHeader_Code = "";
	var vData_Code = "";
	// Begin Table Drawing code here..
	vCode = vCode + "<TABLE  WIDTH='100%' BORDER=0>";
	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 = "";
	var src="";
	// Setup the page...
	src="<html>";
	src+="<head><title>Kalendar</title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">";
            src+="<link rel=stylesheet type=\"text/css\" href=\""+contextPath+"/css/cal.css\">";                                                                           
	src+="</head>";
	src+="<body>";
	var prevMMYYYY = this.calcMonthYear(this.gMonth, this.gYear, -1);
	var prevMM = prevMMYYYY[0];
	var prevYYYY = prevMMYYYY[1];
	var nextMMYYYY = this.calcMonthYear(this.gMonth, this.gYear, 1);
	var nextMM = nextMMYYYY[0];
	var nextYYYY = nextMMYYYY[1];
	var prevYY = parseInt(this.gYear)-1;
	var nextYY = parseInt(this.gYear)+1;
	var out="<TABLE width=\"100%\" border=0 cellpadding=2 cellspacing=0>";
	out+="<tr><td><img src='"+contextPath+"clear.gif' width='1' height='5' border='0'/></td></tr>";
	out+="<tr><td><img src='"+contextPath+"/adminImages/clear.gif' width='5' height='1' border='0'/></td><td>";
	out+="<TABLE width=\"100%\" border=0 cellpadding=2 cellspacing=0>";
	out+="<TR><TD ALIGN=center width=\"5%\"><A HREF=\"" +
		"javascript:window.opener.Build(" +
		"'" + this.gReturnItem + "', '" + prevMM + "', '" + prevYYYY + "');" +
		"\"><IMG SRC=\""+contextPath+"/adminImages/icon_left.gif\" border=0></A></TD>";
	out+="<TD ALIGN=center width=\"50%\">&nbsp;<FONT id=\"mmyy_label\">"+this.gMonthName + "</FONT>&nbsp;</TD>";
	out+="<TD ALIGN=center width=\"5%\"><A HREF=\"" +
		"javascript:window.opener.Build(" +
		"'" + this.gReturnItem + "', '" + nextMM + "', '" + nextYYYY + "');" +
		"\"><IMG SRC=\""+contextPath+"/adminImages/icon_right.gif\" border=0></A></TD>";
	out+="<TD>&nbsp;</TD><TD ALIGN=center width=\"5%\"><A HREF=\"" +
		"javascript:window.opener.Build(" +
		"'" + this.gReturnItem + "', '" + this.gMonth + "', '" + prevYY + "');" +
		"\"><IMG SRC=\""+contextPath+"/adminImages/icon_left.gif\" border=0></A></TD>";
	out+="<TD ALIGN=center width=\"30%\">&nbsp;<FONT id=\"mmyy_label\">" + this.gYear+"</FONT>&nbsp;</td>";
	out+="<TD ALIGN=center width=\"5%\"><A HREF=\"" +
		"javascript:window.opener.Build(" +
		"'" + this.gReturnItem + "', '" + this.gMonth + "', '" + nextYY + "');" +
		"\"><IMG SRC=\""+contextPath+"/adminImages/icon_right.gif\" border=0></A></TD></tr></table>";


	// Get the complete calendar code for the month..
	vCode = this.getMonthlyCalendarCode();
	this.gWinCal.document.open("text/html","replace");
                this.wwrite(src);
	this.wwrite(out)
	this.wwrite(vCode);
	this.wwrite("</td></tr></table>");
	this.wwrite("</body></html>");
	this.gWinCal.document.close();
}
Calendar.prototype.wwrite = function(wtext) {
	this.gWinCal.document.writeln(wtext);
}
Calendar.prototype.cal_header = function() {
	var vCode = "";
	vCode = vCode + "<TR id=\"days_header_bg\">";
	vCode = vCode + "<TD align=center WIDTH='14%'><P><FONT id=\"day_label\">"+Calendar.Days[0]+"</FONT></P></TD>";
	vCode = vCode + "<TD align=center WIDTH='14%'><P><FONT id=\"day_label\">"+Calendar.Days[1]+"</FONT></P></TD>";
	vCode = vCode + "<TD align=center WIDTH='14%'><P><FONT id=\"day_label\">"+Calendar.Days[2]+"</FONT></P></TD>";
	vCode = vCode + "<TD align=center WIDTH='14%'><P><FONT id=\"day_label\">"+Calendar.Days[3]+"</FONT></P></TD>";
	vCode = vCode + "<TD align=center WIDTH='14%'><P><FONT id=\"day_label\">"+Calendar.Days[4]+"</FONT></P></TD>";
	vCode = vCode + "<TD align=center WIDTH='14%'><P><FONT id=\"day_label\">"+Calendar.Days[5]+"</FONT></P></TD>";
	vCode = vCode + "<TD align=center WIDTH='16%'><P><FONT id=\"day_label\">"+Calendar.Days[6]+"</FONT></P></TD>";
	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()==0)?6:vDate.getDay()-1;
	var vDay=1;
	var vLastDay=this.getDaysOfMonth(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>";
	for (i=0; i<vFirstDay; i++) {
		vCode = vCode + "<TD WIDTH='14%'" + this.write_weekend_string(i) + "> </TD>";
	}
	// Write rest of the 1st week
	for (j=vFirstDay; j<7; j++) {
		vCode = vCode + "<TD align=\"center\" WIDTH='14%'" + this.write_weekend_string(j) + ">" 
			if (links){
			vCode+="<A HREF='#' " +
				"onClick=\"self.opener.document." + this.gReturnItem + ".value='" +
				this.format_data(vDay) +
				"';window.close();\">" +
				this.format_day(vDay) +
			"</A>"} else{vCode+=this.format_day(vDay);} +
			"</TD>";
		vDay=vDay + 1;
	}
	vCode = vCode + "</TR>";
	// Write the rest of the weeks
	for (k=2; k<7; k++) {
		vCode = vCode + "<TR>";
		for (j=0; j<7; j++) {
			vCode = vCode + "<TD align=\"center\" WIDTH='14%'" + this.write_weekend_string(j) + ">" 
				if (links){
				vCode+="<A HREF='#' " +
					"onClick=\"window.opener.document." + this.gReturnItem + ".value='" +
					this.format_data(vDay) +
					"';window.close();\">" +
				this.format_day(vDay) +
				"</A>" }else {vCode+=this.format_day(vDay);}
				"</TD>";
			vDay=vDay + 1;
			if (vDay > vLastDay) {
				vOnLastDay = 1;
				break;
			}
		}
		if (j == 6)
			vCode = vCode + "</TR>";
		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 align=\"center\" WIDTH='14%'" + this.write_weekend_string(j+m) +
			"><FONT id=\"day\" style=\"color:gray\">" + m + "</FONT></TD>";
	}
	return vCode;
}
Calendar.prototype.format_day = function(vday) {
	var vNowDay = gNow.getDate();
	var vNowMonth = gNow.getMonth();
	var vNowYear = gNow.getFullYear();
	if (vday == vNowDay && this.gMonth == vNowMonth && this.gYear == vNowYear)
		return ("<FONT id=\"today\">" + vday + "</FONT>");
	else
		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 (" id=\"weekend_bg\" ");
	}
	return "";
}
Calendar.prototype.format_data = function(p_day) {
	var str;
	var vData;
	var vMonth = 1 + this.gMonth;
	vMonth = (vMonth.toString().length < 2) ? "0" + vMonth : vMonth;
	var vY4 = new String(this.gYear);
	var vDD = (p_day.toString().length < 2) ? "0" + p_day : p_day;
	if (format.indexOf("DD",0)==0) str = vDD;
	if (format.indexOf("MM",0)==0)  str = vMonth;
	if (format.indexOf("YYYY",0)==0) str = vY4;
	var tmp2part = format.substring(format.indexOf(sep, 0)+1, format.indexOf(sep, format.indexOf(sep, 0)+1));
	if (tmp2part == "DD") str += sep+vDD;
	if (tmp2part == "MM") str += sep+vMonth;
	if (tmp2part == "YYYY")  str += sep+vY4;
	var tmp3part = format.substring(format.lastIndexOf(sep)+1);
	if (tmp3part == "DD") str += sep+vDD;
	if (tmp3part == "MM") str += sep+vMonth;
	if (tmp3part == "YYYY") str += sep+vY4;
//	return str;
	return vDD + "/" + vMonth + "/" + vY4;  // format: DD.MM.YYYY
//	return vY4 + "-" + vMonth + "-" + vDD;  // format: YYYY-MM-DD
}

function Build(p_item, p_month, p_year) {
	var p_WinCal = ggWinCal;
	gCal = new Calendar(p_item, p_WinCal, p_month, p_year);
	gCal.show();
}

function show_calendar(winId,fieldInp) {
	var d= new Date();
	var month=d.getMonth()+1;
	month-=1;
	var year=d.getYear();
	if (isNav) year+=1900;
	links=true;
            vWinCal=winId;
	ggWinCal = vWinCal;
	Build(fieldInp, String(month),String(year));
}
function runCal(fieldInp, _contextPath)
{
	contextPath = _contextPath;
	vWinCal = window.open("", "calendar","width=250,height=200,status=no,resizable=no,top=180,left=180");
	vWinCal.opener = self;
	ggWinCal = vWinCal;
	gField = fieldInp;
	show_calendar(vWinCal,fieldInp);
}

function getMonthDays(m,y) {
	if ((y % 4) == 0) {
		if ((y % 100) == 0 && (y % 400) != 0)
			return Calendar.DOMonth[m-1];
		return Calendar.lDOMonth[m-1];
	} else
		return Calendar.DOMonth[m-1];
}

/*
* Allows user to separate digits by '.', '-', '/', '\\' (or mixed).
*/
function isDateOk(fieldInp){
	var str = fieldInp.value;
	if (str=="") return true;

	var index=0;

	var a = str.indexOf(".", index) == -1 ? 9999 : str.indexOf(".", index);
	var b = str.indexOf("-", index) == -1 ? 9999 : str.indexOf("-", index);
	var c = str.indexOf("/", index) == -1 ? 9999 : str.indexOf("/", index);
	var d = str.indexOf("\\", index) == -1 ? 9999 : str.indexOf("\\", index);
	
	var occ1 = Math.min(d,Math.min(c,Math.min(a,b)));
	var part1 = str.substring(index,occ1);

	a = str.indexOf(".", occ1+1) == -1 ? 9999 : str.indexOf(".", occ1+1);
	b = str.indexOf("-", occ1+1) == -1 ? 9999 : str.indexOf("-", occ1+1);
	c = str.indexOf("/", occ1+1) == -1 ? 9999 : str.indexOf("/", occ1+1);
	d = str.indexOf("\\", occ1+1) == -1 ? 9999 : str.indexOf("\\", occ1+1);

	var occ2 = Math.min(d,Math.min(c,Math.min(a,b)));
	var part2 = str.substring(occ1+1, occ2);

	a = str.lastIndexOf(".");
	b = str.lastIndexOf("-");
	c = str.lastIndexOf("/");
	d = str.lastIndexOf("\\");

	var occ3 = Math.max(d,Math.max(c,Math.max(a,b)));
	var part3 = str.substring(occ3+1);

	var fDD;
	var fMM;
	var fYYYY;
	var formatStr;

	if (format.indexOf("DD",0)==0) {
		fDD = part1.length < 2 ? "0"+part1 : part1;
		formatStr = fDD;
	}
	if (format.indexOf("MM",0)==0)  {
		fMM = part1.length < 2 ? "0"+part1 : part1;
		formatStr = fMM;
	}
	if (format.indexOf("YYYY",0)==0) {
		fYYYY = part1.length == 2 ? "20"+part1 : part1;
		formatStr = fYYYY;
	}

	var tmp2part = format.substring(format.indexOf(sep, index)+1, format.indexOf(sep, format.indexOf(sep, index)+1));
	if (tmp2part == "DD") {
		fDD = part2.length < 2 ? "0"+part2 : part2;
		formatStr += sep+fDD;
	}
	if (tmp2part == "MM") {
		fMM = part2.length < 2 ? "0"+part2 : part2;
		formatStr += sep+fMM;
	}
	if (tmp2part == "YYYY")  {
		fYYYY = part2.length == 2 ? "20"+part2 : part2;
		formatStr += sep+fYYYY;
	}

	var tmp3part = format.substring(format.lastIndexOf(sep)+1);
	if (tmp3part == "DD") {
		fDD = part3.length < 2 ? "0"+part3 : part3;
		formatStr += sep+fDD;
	}
	if (tmp3part == "MM") {
		fMM = part3.length < 2 ? "0"+part3 : part3;
		formatStr += sep+fMM;
	}
	if (tmp3part == "YYYY") {
		fYYYY = part3.length == 2 ? "20"+part3 : part3;
		formatStr += sep+fYYYY;
	}
	
	if(isNaN(fYYYY) || isNaN(fMM) || isNaN(fDD)){
		return false;
	}

	d = parseInt(fDD, 10);
	m = parseInt(fMM, 10);
	y = parseInt(fYYYY, 10);

	if (d<=0 || d > getMonthDays(m,y) || m<=0 || m>12 || y<=0)
		return false;
	
	// format date
	fieldInp.value = formatStr;
	return true;
}

//-->
