/* COPYRIGHT (C) Paul Davis 2005. All rights reserved. */
 

function dateConform (datestring) {
	if (!datestring)
		return "";
	var future = (arguments.length > 1) ? arguments[1] : true; // automically adjust future dates to last century if 2 digit
	monthnames = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
	var now  = new Date();
	// convert mysql format dates
	if (datestring.replace(/([0-9][0-9][0-9][0-9]\-[0-9][0-9]\-[0-9][0-9])/, "") != datestring)
		datestring = datestring.replace(/([0-9][0-9][0-9][0-9])\-([0-9][0-9])\-([0-9][0-9])/, "$3 $2 $1");
	// sort out the datestring and get a date to work with
	datestring = datestring.replace(/[\.\/\-]/g, " ");
	var day   = getNumber(datestring, 1);
	var month = getNumber(datestring, 2);
	var year  = getNumber(datestring, 3);
	if (day == 0) {// no datestring
		day   = now.getDate();
		month = now.getMonth() + 1;
		year  = now.getFullYear();
	} else {
		// check month supplied as text
		var monthtext = datestring.match(/[a-zA-z]+/g);
		if (monthtext) {
			if (monthtext.length == 1) { 
				monthtext = monthtext[0];
				for (var i = 0; i < monthnames.length; i++) {
					var reg = new RegExp(monthnames[i], "i")
					if (monthtext.match(reg))
						month = i + 1;
				}
				year  = getNumber(datestring, 2);
			}
		} 
		if (month == 0)
			month = now.getMonth() + 1;
		if ((year < 1) || isNaN(year))
			year = now.getFullYear();
		while (month < 0){
			month += 12;
			year--;
		}
		while (month > 12){
			month -= 12;
			year++;
		}
	}
	if (year < 1)
		year = now.getFullYear();
	year = (year < 1000) ? (2000 + getNumber(year)) : year;
	while ((year > now.getFullYear()) && (!future)) 
		year -= 100;
	return day + "/" + month + "/" + year;
}

// ---------------------------------------------------------------------

function calendarDraw (day, month, year, fieldid) {
	monthnames = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
	var html = '\n\t<table class="cal" cellspacing="0">';
	// year
	html += '\
		<tr>\
			<td colspan="2" class="cal year colour"><a class="cal year" href="javascript:void(0)" onMouseDown="calendarDraw(' + day + ', ' + month + ', ' + (year - 1) + ', \'' + fieldid + '\');">&laquo;</a></td>\
			<td colspan="3" class="cal year colour">' + year + '</td>\
			<td colspan="2" class="cal year colour"><a class="cal year" href="javascript:void(0)" onMouseDown="calendarDraw(' + day + ', ' + month + ', ' + (year + 1) + ', \'' + fieldid + '\');">&raquo;</a></td>\
		</tr>';
	// month
	html += '\
		<tr>\
			<td colspan="2" class="cal month colour"><a class="cal month" href="javascript:void(0)" onMouseDown="calendarDraw(' + day + ', ' + ((month == 1) ? 12 : (month - 1)) + ', ' + ((month == 1) ? (year - 1) : year) + ', \'' + fieldid + '\');">&laquo;</a></td>\
			<td colspan="3" class="cal month colour">' + monthnames[month - 1] + '</td>\
			<td colspan="2" class="cal month colour"><a class="cal month" href="javascript:void(0)" onMouseDown="calendarDraw(' + day + ', ' + ((month == 12) ? 1 : (month + 1)) + ', ' + ((month == 12) ? (year + 1) : year) + ', \'' + fieldid + '\');">&raquo;</a></td>\
		</tr>';
	// day names
	var daynames = new Array('S', 'M', 'T', 'W', 'T', 'F', 'S');
	html += '\n\t\t<tr>';
	for (var i = 0; i < daynames.length; i++)
		html += '<td class="cal dayname">' + daynames[i] + '</td>';
	html += '</tr>';
	// days
	// previous month days
	var first = new Date(year, month - 1, 1);
	html += '<tr>';
	var weekstart = first.getDay();
	for(var i = weekstart; i > 0; i--)
		html += '<td class="cal day">&nbsp;</td>';
	// current month days
	var thismonth = new Date(year, month - 1, 1);
	var nextmonth = new Date(year, month, 1);
	var monthdays = Math.round((nextmonth.getTime() - thismonth.getTime()) / 86400000);
	var today = new Date();
	for(var i = 1; i <= monthdays; i++) {
		var todayclass = ((i == today.getDate()) && (month == (today.getMonth() + 1)) && (year == today.getFullYear())) ? ' today colour' : '';
		html += '<td class="cal day' + todayclass + '"><a href="javascript:void(0)" class="cal' + todayclass + '" onclick="calendarSet(\'' + i + '/' + month + '/' + year + '\', \'' + fieldid + '\')">' + i + '</a></td>';
		if ((i + weekstart - 1) % 7 == 6)
			html += '</tr><tr>';
	}
	// next month days
	var last = new Date(year, month - 1, monthdays);
	var weekfinish = last.getDay();
	for(var i = 6; i > weekfinish; i--)
		html += '<td class="cal day">&nbsp;</td>';
	html += '</tr>';
	// finish
	html += '</table>';
	document.getElementById('calendarpopup').innerHTML = html;
}

// ---------------------------------------------------------------------

function calendarMakePopUp (fieldid, aclass, whereid) { 
	aclass = (aclass) ? aclass : '';
	var where = document.getElementById(whereid);
	var field = document.getElementById(fieldid);
	if (field && where) {
		calendarfieldid = fieldid;
		var caldiv = document.createElement('div');
		caldiv.id = 'calendarpopup';
		caldiv.className = aclass;
		caldiv.style.position = 'absolute';
		caldiv.style.left = findPos(where, 'x');
		caldiv.style.top  = findPos(where, 'y') + Math.max(where.clientHeight, where.offsetHeight);
		document.body.appendChild(caldiv);
		var today = new Date();
		calendarDraw(today.getDate(), today.getMonth() + 1, today.getFullYear(), fieldid);
	} 
}

// ---------------------------------------------------------------------

function calendarPopUp (fieldid, aclass, whereid) {
	if (document.getElementById('calendarpopup'))
		destroy('calendarpopup');
	else
		setTimeout('calendarMakePopUp(\'' + fieldid + '\', \'' + aclass + '\', \'' + whereid + '\')', 100);
}

// ---------------------------------------------------------------------

function calendarSet (datestring, fieldid) {
	var field = document.getElementById(fieldid);
	if (field)
		field.value = dateConform(datestring);
	destroy('calendarpopup');
}

