/*

Domnick Hunter iFoot Intranet Core
Calendar / DateTimePicker
Author: Karl Dyson
Last Modified: 27/2/2006

*/

/* **************************************************************************************************** */
/* Constructor 																							*/

var iCalendar = function(name)
{
	this.name = name;
	this.parent = window.opener;
	this.passback = null;
	this.width = 212;
	this.height = 210;
	this.scrolling = "no";
	this.window = null;
	this.title = "Calendar";
	this.date = new Date();
	this.selectedDate = new Date();
	
	this.months = new Array(12);
	this.months[0] = 'January';
	this.months[1] = 'February';
	this.months[2] = 'March';
	this.months[3] = 'April';
	this.months[4] = 'May';
	this.months[5] = 'June';
	this.months[6] = 'July';
	this.months[7] = 'August';
	this.months[8] = 'September';
	this.months[9] = 'October';
	this.months[10] = 'November';
	this.months[11] = 'December';
	
	this.setDate(this.date);

}

/* ****************************************************************************************************	*/
/* Date Manipulation Methods																			*/

iCalendar.prototype.setSelectedDate = function(which)
{
	this.selectedDate.setFullYear(which.getFullYear(),which.getMonth(),which.getDate());		
}

iCalendar.prototype.setStartDate = function(which)
{
	this.selectedDate.setFullYear(which.getFullYear(),which.getMonth(),which.getDate());
	this.setDate(which);
}

iCalendar.prototype.setDate = function(which)
{
	this.date.setFullYear(which.getFullYear(),which.getMonth(),which.getDate());
	
	this.grid = new Array(31);
	for (var counter1 = 0; counter1 < 42; counter1++)
		this.grid[counter1] = -1;
		
	start = this.date;
	start.setDate(1);
	this.startOfMonth = start.getDay() - 1;
	if (this.startOfMonth < 0)
		this.startOfMonth = 6;
		// quick fix 

	counter2 = 1;
	for (var counter1 = this.startOfMonth; counter1 < this.startOfMonth + this.getMonthDays(start.getMonth(),start.getFullYear()) ; counter1++) {
		this.grid[counter1] = counter2++;
	}		
}

function isLeapYear(yr) {
  return new Date(yr,2-1,29).getDate()==29;
}

iCalendar.prototype.getMonthDays = function(tMonth, tYear)
{
	var res = 0;
	
	daysInMonth = new Array(12);
	daysInMonth[0] = 31;
	daysInMonth[1] = 28;
	daysInMonth[2] = 31;
	daysInMonth[3] = 30;
	daysInMonth[4] = 31;
	daysInMonth[5] = 30;
	daysInMonth[6] = 31;
	daysInMonth[7] = 31;
	daysInMonth[8] = 30;
	daysInMonth[9] = 31;
	daysInMonth[10] = 30;
	daysInMonth[11] = 31;
	
	if ((tMonth == 1) && (isLeapYear(tYear)))
		res = 29;
	else
		res = daysInMonth[tMonth];
		
	return res;
}

/* ****************************************************************************************************	*/
/* Display Methods																						*/

iCalendar.prototype.open = function()
{
	this.window = window.open("","","width=" + this.width + ", height=" + this.height + ", scrollbars=" + this.scrolling);
	this.display();
}

iCalendar.prototype.display = function()
{
	this.window.document.open();
	this.window.document.writeln('<?xml version="1.0" encoding="iso-8859-1"?>');
	this.window.document.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">');
	this.window.document.writeln("<html>");
	this.window.document.writeln("<head>");
	this.window.document.writeln('<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />');
	this.window.document.writeln("<title>" + this.title + "</title>");
	this.window.document.writeln(this.createCSS());	
	this.window.document.writeln("</head>");
	this.window.document.writeln("<body onBlur='window.focus()'>");
	this.window.document.writeln(this.createCalendar());
	this.window.document.writeln(this.createButtons());	
	this.window.document.writeln("</body>");
	this.window.document.writeln("</html>");
	this.window.document.close();
}

iCalendar.prototype.createCSS = function() {
	var css = "";
	css = "<style>";
	css += 'body {margin:0; padding: 5px; background-color: #FFFFFF;}';
	css += '#calendarFrame {width: 187px; height: 190px; border: 1px solid #000000; padding: 5px;}';
	css += '#calendarTable {font-family: Arial, Helvetica, sans-serif; font-size: 11px;	font-weight: bold; text-align: center;}';
	css += '#calendarTable th {background-color: #999999; color: #FFFFFF; height: 20px; width: 20px;}';	
	css += '#calendarTable td {border: 1px solid #EBEBEB; height: 20px; width: 20px;}';
	css += '#calendarTable td A {text-decoration: underline; font-weight: normal; color: #000000; font-size: 12px;}';
	css += '#calendarTable .weekend {border: 1px solid #EBEBEB; height: 20px; width: 20px; background-color: #EBEBEB; color: #000000;}';
	css += '#calendarTable .selected {border: 1px solid #EEEEEE; height: 20px; width: 20px; background-color: #FFB91D; color: #FFFFFF;}';	
	css += '#calendarTable .selected A {text-decoration: underline; font-weight: normal; color: #FFFFFF; font-size: 12px;}';
	css += "caption {margin:0; padding: 2px; background-color: #FFB91D; color: #FFFFFF;}";
	css += 'caption A {text-decoration: none; font-weight: bolder; color: #FFFFFF; font-size: 12px;}';
	css += 'caption .lArrow {width:10px; float:left;}';
	css += 'caption .rArrow {width:10px; float:right;}';
	css += 'caption .calHeader {width:155px; float:left;}';
	css += '#buttons {position:absolute; top:170px; left:90px; width:110px; height:20px;}';
	css += '#buttons .button {cursor:pointer; background-color: #FFFFFF; width:50px; height:20px; font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight:normal; border: 1px solid #EBEBEB;}';
	css += "</style>"
	return css;
}

iCalendar.prototype.createButtons = function(){
	var buts = "";
	buts = '<div id="buttons">';
	buts += '<input class="button" type="button" name="ok" value="ok" onClick="window.opener.' + this.name + '.ok()"> ';
	buts += '<input class="button" type="button" name="cancel" value="cancel" onClick="window.opener.' + this.name + '.cancel()">';
	buts += '</div>';
	return buts;
}

iCalendar.prototype.createCalendar = function() {
	var cal = "";
	var flop = false;
	var tmp = new Date();
	var selectedDay;
	
	cal = '<div id="calendarFrame">';
	cal += "<!-- " + this.selectedDate + "//-->"
	cal += '<table id="calendarTable">';
	cal += '<caption>';
	cal += '<div class="lArrow"><a href="javascript:window.opener.' + this.name + '.prevMonth()">&lt;</a></div> ';
	cal += '<div class="calHeader">' + this.months[this.date.getMonth()] + ' ' +  this.date.getFullYear() + '</div> ';
	cal+= ' <div class="rArrow"><a href="javascript:window.opener.' + this.name + '.nextMonth()">&gt;</a></div>';
	cal += '</caption>';
  	cal += '<tr>';
		cal += '<th>M</td>';
		cal += '<th>T</td>';
		cal += '<th>W</td>';
		cal += '<th>T</td>';
		cal += '<th>F</td>';
		cal += '<th>S</td>';
		cal += '<th>S</td>';
  	cal += '</tr>';
		
	tmp.setFullYear(this.date.getFullYear(),this.date.getMonth(),this.date.getDate());	
	for (var counter1 = 0; counter1 < this.startOfMonth + this.getMonthDays(this.date.getMonth(),this.date.getFullYear()); counter1++) {
		selectedDay = false;
		
		if ((counter1 % 7) == 0) {
			cal += '<tr>';
		}
				
		if (this.grid[counter1] > -1) {
			tmp.setDate(this.grid[counter1]);
			if ( (tmp.getDate() == this.selectedDate.getDate()) && (tmp.getMonth() == this.selectedDate.getMonth()) && (tmp.getFullYear() == this.selectedDate.getFullYear()) )
				selectedDay = true;
		}
		
		if (selectedDay)
			cal += '<td class="selected">';
		else if (((counter1 % 7) == 5) || ((counter1 % 7) == 6))
			cal += '<td class="weekend">';
		else
			cal += '<td>';
			
		if (this.grid[counter1] == -1)
			cal += '&nbsp;';		
		else
			cal += '<a href="javascript:window.opener.' + this.name + '.changeSelection(' + this.grid[counter1] + ',' + this.date.getMonth() + ',' + this.date.getFullYear() + ')">' + this.grid[counter1] + '</a>';
			
		cal += '</td>'
	}
	cal += '</tr>';
		
	cal += '</table>';
		
	cal += '</div>';
	return cal;
}

/* ****************************************************************************************************	*/
/* Navigation / Interface Methods																		*/

iCalendar.prototype.prevMonth = function() {
	this.date.setMonth(this.date.getMonth()-1);
	this.setDate(this.date);
	this.display();
}

iCalendar.prototype.nextMonth = function() {
	this.date.setMonth(this.date.getMonth()+1);
	this.setDate(this.date);
	this.display();
}

iCalendar.prototype.changeSelection = function(d,m,y) {
	var tmp = new Date();
	tmp.setFullYear(y,m,d);
	this.selectedDate = tmp;
	this.display();
}

iCalendar.prototype.setReturn = function(which) {
	this.passback = which;
}

iCalendar.prototype.cancel = function() {
	this.window.close();
}

iCalendar.prototype.ok = function() {
	if (this.passback != null) {
		this.passback(this.selectedDate);
	}
	this.window.close();
}

