/* Based on Swazz Javascript Calendar */
/* By Oliver Bryant */
/* http://calendar.swazz.org */

var calendar = {

getObject: function(objID) {
    if (document.getElementById) return document.getElementById(objID);
    else if (document.all) return document.all[objID];
    else if (document.layers) return document.layers[objID];
},

eventTarget: function(e) {
	var result;
	if (e.target) result = e.target;
	else if (e.srcElement) result = e.srcElement;
	if (result.nodeType == 3) result = result.parentNode; // defeat Safari bug
	return result;
},

eventObject: function(e) {
	if(!e) e = window.event;
	return e;
},

isChild: function(s, d) {
	while(s) {
		if (s == d) return true;
		s = s.parentNode;
	}
	return false;
},

leftPosition: function(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else if (obj.x) curleft += obj.x;
	return curleft;
},

topPosition: function(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else if (obj.y) curtop += obj.y;
	return curtop;
},

// Calendar script

selected: null,
month: 0,
year: 0,
target_element: null,

time_enabled: true,

month_aliases: new Array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'),
nonleap_year: new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
leap_year: new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
idx_to_day: new Array(42),

color_bg: 'white',
color_frame: '#a0a0a0',
color_day: '#f0f0ee',
color_button: '#cccccc',
color_daymouse: '#b2bbd0',

raise: function(obj, time_enabled) {
	calendar.time_enabled = time_enabled;
	calendar.target_element = obj;
	
	calendar.getObject('calendar').style.left = ''+calendar.leftPosition(obj)+'px';
	calendar.getObject('calendar').style.top = ''+(calendar.topPosition(obj) + obj.offsetHeight)+'px';
	calendar.getObject('calendar').style.display = '';
	if (!calendar.time_enabled) calendar.getObject('time').style.display = 'none';

	var parsed = calendar.parse(obj);
	if (parsed) {
		calendar.selected = parsed;
		calendar.year = parsed.getFullYear();
		calendar.month = parsed.getMonth();
	}
	
	calendar.redraw();
},

parse: function(obj) {
	var result = new Date();
	if (calendar.time_enabled) {
		var splitted_value = obj.value.split(' ');

		if (splitted_value.length < 2) return null;
		var splitted_date = splitted_value[0].split('-');
		var splitted_time = splitted_value[1].split(':');
		
		if (splitted_date.length != 3) return null;
		if (!isNaN(splitted_date[0])) result.setFullYear(splitted_date[0]);
		else return null;
		if (!isNaN(splitted_date[1])) result.setMonth(splitted_date[1]-1);
		else return null;
		if (!isNaN(splitted_date[2])) result.setDate(splitted_date[2]);
		else return null;

		if (splitted_time.length != 3) return null;
		if (!isNaN(splitted_time[0])) result.setHours(splitted_time[0]);
		else return null;
		if (!isNaN(splitted_time[1])) result.setMinutes(splitted_time[1]);
		else return null;
		if (!isNaN(splitted_time[2])) result.setSeconds(splitted_time[2]);
		else return null;
	} else {
		var splitted_date = obj.value.split('-');

		if (splitted_date.length != 3) return null;
		if (!isNaN(splitted_date[0])) result.setFullYear(splitted_date[0]);
		else return null;
		if (!isNaN(splitted_date[1])) result.setMonth(splitted_date[1]-1);
		else return null;
		if (!isNaN(splitted_date[2])) result.setDate(splitted_date[2]);
		else return null;
	}

	return result;
},

redraw: function() {
	var first_day_in_month = new Date();
	first_day_in_month.setDate(1);
	first_day_in_month.setFullYear(calendar.year);
	first_day_in_month.setMonth(calendar.month);
	var offset = first_day_in_month.getDay();

	var days_in_month = ((calendar.year%4) == 0) ? calendar.leap_year : calendar.nonleap_year;
	
	calendar.getObject('month').innerHTML = calendar.month_aliases[calendar.month] + ' ' + calendar.year;
	for (var i = 0; i < 42; ++i) {
		
		calendar.getObject('day'+i).style.fontWeight = '';
		
		if (i >= 0 + offset && i < days_in_month[calendar.month] + offset) {
			
			calendar.getObject('day'+i).style.cursor = 'pointer';
			if (calendar.selected != null && calendar.selected.getFullYear() == calendar.year && calendar.selected.getMonth() == calendar.month && calendar.selected.getDate() == i-offset+1) {
				calendar.getObject('day'+i).style.fontWeight = 'bold';
			}
			calendar.getObject('day'+i).innerHTML = i-offset+1;	
			calendar.getObject('day'+i).onmouseover = calendar.dayMouseOver;
			calendar.getObject('day'+i).onmouseout = calendar.dayMouseOut;
			calendar.getObject('day'+i).onclick = calendar.dayClick;
			
			calendar.idx_to_day[i] = i-offset+1;
		} else {
			calendar.getObject('day'+i).style.cursor = 'default';
			calendar.getObject('day'+i).innerHTML = '&nbsp;';
			calendar.getObject('day'+i).onmouseover = null;
			calendar.getObject('day'+i).onmouseout = null;
		}

	}
	calendar.getObject('hour').innerHTML = calendar.selected.getHours();
	calendar.getObject('minute').innerHTML = calendar.selected.getMinutes();
	calendar.getObject('second').innerHTML = calendar.selected.getSeconds();
},

click: function(e) {
	var target = calendar.eventTarget(calendar.eventObject(e));
	
	if (calendar.getObject('calendar'))
		if (!calendar.isChild(target, calendar.getObject('calendar')))
			calendar.getObject('calendar').style.display = 'none';
},

monthNext: function() {
	calendar.month++;
	if (calendar.month >= 12) {
		calendar.month = 0;
		calendar.year++;
	}
	
	calendar.redraw();
},

monthBack: function() {
	calendar.month--;
	if (calendar.month < 0) {
		calendar.month = 11;
		calendar.year--;
	}

	calendar.redraw();
},

dayMouseOver: function(e) {
	calendar.eventTarget(calendar.eventObject(e)).style.background = calendar.color_daymouse;
},

dayMouseOut: function(e) {
	calendar.eventTarget(calendar.eventObject(e)).style.background = calendar.color_day;
},

output: function() {
	
	calendar.target_element.value = ''+calendar.year+'-';
	if (calendar.month+1 < 10) calendar.target_element.value += '0'+(calendar.month+1)+'-';
	else calendar.target_element.value += (calendar.month+1)+'-';
	if (calendar.selected.getDate() < 10) calendar.target_element.value += '0'+calendar.selected.getDate();
	else calendar.target_element.value += calendar.selected.getDate();
		
	if (calendar.time_enabled) {
		calendar.target_element.value += ' ';
		if (calendar.selected.getHours() < 10) calendar.target_element.value += '0'+calendar.selected.getHours()+':';
		else calendar.target_element.value += calendar.selected.getHours()+':';
		if (calendar.selected.getMinutes() < 10) calendar.target_element.value += '0'+calendar.selected.getMinutes()+':';
		else calendar.target_element.value += calendar.selected.getMinutes()+':';
		if (calendar.selected.getSeconds() < 10) calendar.target_element.value += '0'+calendar.selected.getSeconds();
		else calendar.target_element.value += calendar.selected.getSeconds();
	}
},

dayClick: function(e) {
	
	var idx = parseInt(calendar.eventTarget(calendar.eventObject(e)).id.substring(3, calendar.eventTarget(calendar.eventObject(e)).id.length));
	calendar.selected.setFullYear(calendar.year);
	calendar.selected.setMonth(calendar.month);
	calendar.selected.setDate(calendar.idx_to_day[idx]);
	
	calendar.output();
	calendar.redraw();
},

hourPlus: function() {
	var hours = calendar.selected.getHours();
	hours++;
	if (hours > 23) hours = 0;
	calendar.selected.setHours(hours);

	calendar.output();
	calendar.redraw();
},

hourMinus: function() {
	var hours = calendar.selected.getHours();
	hours--;
	if (hours < 0) hours = 23;
	calendar.selected.setHours(hours);

	calendar.output();
	calendar.redraw();
},

minutePlus: function() {
	var minutes = calendar.selected.getMinutes();
	minutes++;
	if (minutes > 59) minutes = 0;
	calendar.selected.setMinutes(minutes);

	calendar.output();
	calendar.redraw();
},

minuteMinus: function() {
	var minutes = calendar.selected.getMinutes();
	minutes--;
	if (minutes < 0) minutes = 59;
	calendar.selected.setMinutes(minutes);

	calendar.output();
	calendar.redraw();
},

secondPlus: function() {
	var seconds = calendar.selected.getSeconds();
	seconds++;
	if (seconds > 59) seconds = 0;
	calendar.selected.setSeconds(seconds);

	calendar.output();
	calendar.redraw();
},

secondMinus: function() {
	var seconds = calendar.selected.getSeconds();
	seconds--;
	if (seconds < 0) seconds = 59;
	calendar.selected.setSeconds(seconds);

	calendar.output();
	calendar.redraw();
},

init: function() {
	calendar.selected = new Date();
	calendar.month = calendar.selected.getMonth();
	calendar.year = calendar.selected.getFullYear();

	document.write('<table id="calendar" style="position: absolute; z-index: 1000; border-collapse: collapse; background: '+calendar.color_bg+'; border: 1px solid '+calendar.color_frame+'; font-family: verdana, arial, helvetica, sans-serif; font-size: 10pt; display: none" cellpadding="2">');
	document.write('<tr><td style="cursor: pointer" onclick="calendar.monthBack()">&lt;&lt;</td><td colspan="5" id="month" style="font-weight: bold; text-align: center"></td><td style="cursor: pointer; text-align: right" onclick="calendar.monthNext()">&gt;&gt;</td></tr>');
	document.write('<tr style="background: '+calendar.color_frame+'; text-align:center"><td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td></tr>');
	for(var i = 0; i < 6; ++i) {
		document.write('<tr style="text-align: center">');
		for(var j = 0; j < 7; ++j) document.write('<td id="day' + (7*i+j) + '" style="border: 1px solid '+calendar.color_frame+'; background: '+calendar.color_day+'"></td>');
		document.write('</tr>');
	}
	document.write('<tr id="time"><td colspan="7" style="padding: 0px;">');
	
	document.write('<table cellpadding="0" style="text-align: center; border-collapse: separate; width: 100%;">');
	
	document.write('<tr style="font-size: 60%"><td id="hourPlus" style="border: 1px solid '+calendar.color_frame+'; background: '+calendar.color_button+'; cursor: pointer" onclick="calendar.hourPlus()">+</td>');
	document.write('<td id="minutePlus" style="border: 1px solid '+calendar.color_frame+'; background: '+calendar.color_button+'; cursor: pointer" onclick="calendar.minutePlus()">+</td>');
	document.write('<td id="secondPlus" style="border: 1px solid '+calendar.color_frame+'; background: '+calendar.color_button+'; cursor: pointer" onclick="calendar.secondPlus()">+</td></tr>');

	document.write('<tr><td id="hour" style="width: 33%"></td><td id="minute"></td><td id="second" style="width: 33%"></td></tr>');
	
	document.write('<tr style="font-size: 60%"><td style="border: 1px solid '+calendar.color_frame+'; background: '+calendar.color_button+'; cursor: pointer" onclick="calendar.hourMinus()">-</td>');
	document.write('<td style="border: 1px solid '+calendar.color_frame+'; background: '+calendar.color_button+'; cursor: pointer" onclick="calendar.minuteMinus()">-</td>');
	document.write('<td style="border: 1px solid '+calendar.color_frame+'; background: '+calendar.color_button+'; cursor: pointer" onclick="calendar.secondMinus()">-</td></tr>');

	document.write('</table>');
	
	document.write('</td></tr>');
	document.write('</table>');

	document.all ? document.attachEvent('onclick', calendar.click) : document.addEventListener('click', calendar.click, false);
}

};
