// gCalendar 2.0.1

var LANG='en';
 
var gNow = new Date();
var ggWinCal;

isNav = (navigator.appName.indexOf('Netscape') != -1) ;
isIE = (navigator.appName.indexOf('Microsoft') != -1) ;

Calendar.LANG=LANG;
 
Calendar.i18nMonths = {
	'en': 'January,February,March,April,May,June,July,August,September,October,November,December',
	'es': 'Enero,Febrero,Marzo,Abril,Mayo,Junio,Julio,Agosto,Septiembre,Octubre,Noviembre,Diciembre',
	'fr': 'janvier,février,mars,avril,mai,juin,juillet,août,septembre,octobre,novembre,décembre'
}

Calendar.i18nDays = {
	'en': 'Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday',
	'es': 'Domingo,Lunes,Martes,Miércoles,Jueves,Viernes,Sábado',
	'fr': 'Dimanche,Lundi,Mardis,Mercredi,Jeudi,Vendredi,Samedi'
}

Calendar.Formats={
	'default'	: 'YYYY-MM-DD'
}

Calendar.monthNames=Calendar.i18nMonths[Calendar.LANG].split(','); 
Calendar.dayNames=Calendar.i18nDays[Calendar.LANG].split(',');

// DOMonth=>Non-Leap year Month days..
 
Calendar.DOMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31] ;
 
function Calendar(p_item, 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;

 this.gWinCal.gCal=this;
 
 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.gFGColor = 'black';
 this.gTextColor = 'black';
 this.pReturnItem = p_item;
}

Calendar.get_month = function (monthNo) {
 return Calendar.monthNames[monthNo];
}

Calendar.get_daysofmonth = 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.
*/
 
 var leapYear= (monthNo==1) && ((p_year % 4) == 0) && ( ((p_year % 100) != 0) || ((p_year % 400) == 0) ) ? 1 : 0 ;
 return Calendar.DOMonth[monthNo] + leapYear;
}


Calendar.prototype.returnVal=function (v) {
	this.pReturnItem.value=this.format_data(v);
}

Calendar.prototype.reBuild=function (dmonth,dyear) {
 if (!dmonth) dmonth=0;
 if (!dyear) dyear=0;

 Build(this.pReturnItem,
	parseInt(this.gMonth)+dmonth,
	parseInt(this.gYear)+dyear,
	this.gFormat);
}

Calendar.prototype.getMonthlyCalendarCode = function() {
 var res='';
 
// Begin Table Drawing code here..
 res += '<table class="calendar month" border=0 cellpadding=2 cellspacing=1>';
 
 res += this.cal_header() + this.cal_data();
 
 res += '</table>';
 
 return res;
}
 
Calendar.prototype.show = function() {

 var res='<html>' +
	'<head><title>Calendar</title>' +
		'<link rel="stylesheet" type="text/css" href="http://www.abcevents.com/calendar.css">' +
		'</head>'+
	'<body class=calendar>' +

	'<h3>' + this.gMonthName + ' ' + this.gYear + '</h3><br>' +

	'<table width=100% border=0 cellspacing=2 cellpadding=0 class=hdrBtns><tr>'+
		'<td align=center><a href="javascript:gCal.reBuild(0,-1);">&lt;Year</a>'+
		'<td><a href="javascript:gCal.reBuild(-1,0);">&lt;Month</a>'+
		'<td><a href="javascript:gCal.reBuild(+1,0)">Month&gt;</a>'+
		'<td><a href="javascript:gCal.reBuild(0,+1);">Year&gt</a>' +
	'</table><br>' +
 
	// Get the complete calendar code for the month..
		this.getMonthlyCalendarCode();
 
	'</font></body></html>';

 this.gWinCal.document.open();
 this.gWinCal.document.write(res);
 this.gWinCal.document.close();
}
 
Calendar.prototype.showY = function() {
 var i;
 var vr, vc, vx, vy;		// Row, Column, X-coord, Y-coord
 var vxf = 285;			// X-Factor
 var vyf = 200;			// Y-Factor
 var vxm = 10;			// X-margin
 var vym=isIE ? 75 : 25;	// Y-margin
 
 this.gWinCal.document.open();
 
 var res='<html>' +
		'<head><title>Calendar</title>' +
		'<style type="text/css">\n<!--';

 for (i=0; i<12; i++) {
	vc = i % 3;

	vr=Math.floor(i/3);

	vx = parseInt(vxf * vc) + vxm;
	vy = parseInt(vyf * vr) + vym;

	res+='.lclass' + i + ' {position:absolute;top:' + vy + ';left:' + vx + '}\n';
 }
 res+='-->\n</style></head>';
 
 res+='<body class=calendar>' +

	'<h3>Year : ' + this.gYear + '</h3>' +

	// Show navigation buttons
 
	'<TABLE WIDTH=100% BORDER=1 CELLSPACING=0 CELLPADDING=0 BGCOLOR=#e0e0e0><TR><TD ALIGN=center>'+
	'[<A HREF="javascript:gCal.reBuild(0,-1);" alt="Prev Year"><<</A>]<TD ALIGN=center>'+
	'[<A HREF="javascript:gCal.reBuild(0,+1);" alt="Next Year">>></A>]</TABLE><BR>';

 // Get the complete calendar code for each month..
 var j;
 for (i=11; i>=0; i--) {

	this.gMonth = i;
	this.gMonthName = Calendar.get_month(this.gMonth);
	res+='<DIV ID=layer' + i + ' CLASS=lclass' + i + '>'+
		this.gMonthName + '/' + this.gYear + '<BR>' +
		this.getMonthlyCalendarCode()+
		'</DIV>';
 
 }
 res+='</font><BR></body></html>';

 this.gWinCal.document.open();
 this.gWinCal.document.write(res);
 this.gWinCal.document.close();
}
 
Calendar.prototype.wwrite = function(wtext) {
	this.gWinCal.document.write(wtext);
}
 
Calendar.prototype.cal_header = function() {
 var dnames=Calendar.dayNames, res='<tr>';

 for (var i=0; i<dnames.length; i++)
	res += '<th width=14%>'+dnames[i].substr(0,3);
 
 return res;
}
 
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;
 
// Get day for the 1st of the requested month/year..
// Place as many blank cells before the 1st day of the month as necessary.
 
 var res = '<tr>';
 for (i=0; i<vFirstDay; i++)
	res+='<td width=14% class=dow' + i + '>&nbsp;';

// Write rest of the 1st week
 for (j=vFirstDay; j<7; j++) {
	res+='<td width=14% class=dow' + j + '>' +
		'<a href="#" onclick="gCal.returnVal('+vDay+'); window.close();">' +
		this.format_day(vDay) +	'</a>';
	vDay+=1;
 }
 
// Write the rest of the weeks
 for (k=2; k<7; k++) {
	res+='<tr>';
	for (j=0; j<7; j++) {
		res+='<td width=14% class=dow' + j + '>' +
			'<a href="#" onclick="gCal.returnVal('+vDay+'); window.close();">' +
			this.format_day(vDay) +	'</a>';
		vDay+=1;
 
		if (vDay > vLastDay) {
			vOnLastDay = 1;
			break;
		}
	}
 
	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++) 
	res+='<td width=14% class=dow' + (j+m) + '>' +
		'<FONT COLOR=gray>' + (this.gYearly?' ':m) + '</FONT>';

 return res;
}
 
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 '<b class=red>' + vday + '</b>';
 else
	return vday;
}
 
Calendar.prototype.format_data = function(p_day) {

 var vMonth = 1 + this.gMonth;

 var dParts= {
	'MM':		(vMonth.toString().length < 2) ? '0' + vMonth : vMonth,
	'MON':		Calendar.get_month(this.gMonth).substr(0,3).toUpperCase(),
	'MONTH':	Calendar.get_month(this.gMonth).toUpperCase(),
	'YYYY':		new String(this.gYear),
	'YY':		new String(this.gYear.toString().substr(2,2)),
	'DD':		(p_day.toString().length < 2) ? '0' + p_day : p_day
 }
 
 var res=this.gFormat;
// alert(res);

 if (Calendar.Formats[res]) res=Calendar.Formats[res];

 if (res=='*') res=this.gFormat;

 res=res.replace(/(MM|MONTH|MON|YYYY|YY|DD)/g, function (str,p1,p2,offset,s) {
		return dParts[p1];
	} );

 return res;
}
 
function Build(p_item, p_month, p_year, p_format) {
 var p_WinCal = ggWinCal;

 if (p_month<0) { p_month=11; p_year--; }
 if (p_month>11) { p_month=0; p_year++; }

 gCal = new Calendar(p_item, p_WinCal, p_month, p_year, p_format);

 // Customize your Calendar here..

 // Choose appropriate show function
 if (gCal.gYearly)
	gCal.showY();
 else    
	gCal.show();

 p_WinCal.gCal=gCal;

}
 
function show_calendar(p_item,p_month,p_year,p_format) {
/*
	p_month : 0-11 for Jan-Dec; 12 for All Months.
	p_year  : 4-digit year
	p_format: Date format (dd/mm/yyyy, ...)
	p_item  : Return Item.
*/
 
 p_item = arguments[0];
 if (p_month == null) p_month = new String(gNow.getMonth());
 if (!p_year) p_year = new String(gNow.getFullYear().toString());
 if (!p_format) p_format = 'DD/MM/YYYY';
 
 vWinCal = window.open('', 'Calendar','width=250,height=230,top=200,left=200,location=NO');
 vWinCal.opener = self;
 ggWinCal = vWinCal;
 
 Build(p_item, p_month, p_year, p_format);
 return false;
}

// Yearly Calendar Code Starts here
 
function show_yearly_calendar(p_item, p_year, p_format) {
// Load the defaults..
 if (p_year == null || p_year == '')
	p_year = new String(gNow.getFullYear().toString());

 if (p_format == null || p_format == '') p_format = 'MM/DD/YYYY';

 var vWinCal = window.open('', 'Calendar', 'scrollbars=yes');
 vWinCal.opener = self;
 ggWinCal = vWinCal;
 
 Build(p_item, null, p_year, p_format);
}

