// --------
//
//  CALENDAR

var maxmonthsahead = 36;
var oneday = 86400000;
var caldate = null;
var curcalid = '';
var calnextid;
var calnextdays;
var blockprev = true;

var d=new Date()
var month=new Array(12)
month[0]="Jan"
month[1]="Feb"
month[2]="Mar"
month[3]="Apr"
month[4]="May"
month[5]="Jun"
month[6]="Jul"
month[7]="Aug"
month[8]="Sep"
month[9]="Oct"
month[10]="Nov"
month[11]="Dec"

function dmy(d) {
	return d.getDate() + '-' + month[d.getMonth()] + '-' + d.getFullYear();
}

function ripdmy(s) {
	ripd = new Date();
	re = /(\d+)\/(\d+)\/(\d+)/;
	darr = re.exec(s);
	if (darr) {
		if (darr.length > 0) {
			if (darr[3].length == 2) darr[3] = ripd.getFullYear().toString().substr(0,2) + darr[3];
			ripd = new Date(darr[3], (darr[2]-1), darr[1]);
		} 
	}
	return ripd;
}

function calover(cell) {
	cell.style.backgroundColor = '#CCCCCC';
}

function calout(cell, col) {
	cell.style.backgroundColor = col ;
}

function calnav(monthoffset) {
	newmonth = (caldate.getMonth() + monthoffset);
	if (newmonth < 0) { caldate.setFullYear(caldate.getFullYear()-1); newmonth = 11 }
	if (newmonth > 11) { caldate.setFullYear(caldate.getFullYear()+1); newmonth = 0 }
	caldate.setMonth(newmonth);
}

function calclick(txtid, iframeid, cell) {
	caldate = new Date(caldate.getFullYear(), (caldate.getMonth()), cell.innerHTML);
	document.getElementById(txtid).value = dmy(caldate);
	calclose(iframeid);
}

function calclose(iframeid) {
	curcalid = '';
	document.getElementById(iframeid).style.visibility = 'hidden';
	if (calnextid != null) {
		next = document.getElementById(calnextid);
		if (next) {
			if (next.value == '' || ripdmy(next.value) - caldate < (calnextdays * oneday))  {
				next.value = dmy(new Date(caldate.valueOf() + (oneday * calnextdays)));	
				next.focus();
			}
		}
	}
}

function calopen(txtid, iframeid, nextid, nextdays) {
	if (curcalid != '') calclose(curcalid);
	caldate = ripdmy(document.getElementById(txtid).value);
	calnextid = nextid;
	calnextdays = nextdays;
	cal(txtid, iframeid);
	curcalid = iframeid;
}

function cal(txtid, iframeid) {

	calmonth = caldate.getMonth();
	curdate  = new Date(caldate.valueOf());
	curdate.setDate(1);

	mindate  = new Date();
	maxdate  = (new Date()).setMonth(((new Date()).getMonth()+maxmonthsahead));

	over     = ' onmouseover="calover(this)" ';
	outwhite = ' onmouseout="calout(this, \'#FFFFFF\')" ';

	html = '\
	<tr class="arrows">\
		<td ' + over + outwhite + ' onmousedown="calnav(-1);cal(\'' + txtid + '\', \'' + iframeid + '\')">&lt;</td>\
		<td class="cols" colspan="4" align="center">' + curdate.toString().substr(4,3) + ' ' + curdate.getFullYear() + '</td>\
		<td ' + over + outwhite + ' onmousedown="calnav( 1);cal(\'' + txtid + '\', \'' + iframeid + '\')">&gt;</td>\
		<td ' + over + outwhite + ' onmousedown="calnextid=null;calclose(\'' + iframeid + '\')">X</td>\
	</tr>\
	<tr class="cols"><td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td></tr>';

	started = false;
	for (week = 1; week <= 6; week++) {
		html += '<tr>';
		for (weekday = 0; weekday <= 6; weekday++) {
			if (curdate.getDay() == weekday) started = true;
			if (curdate.getMonth() != calmonth) started = false;
			if (started) { 
				if (blockprev && ((mindate.valueOf() - curdate.valueOf() > oneday) || (curdate.valueOf() > maxdate.valueOf()))) { 
					html += '<td class=grey>' + curdate.getDate() + '</td>';
				} else {
					html += '<td' + over;
					if (curdate.getDate() == caldate.getDate()) { 
						html += ' onmouseout="calout(this, \'#e4e4e4\')" class="sel"' ;
					} else {	
						html += outwhite;
					}
					html += ' onmousedown="calclick(\'' + txtid + '\', \'' + iframeid + '\', this)">' + curdate.getDate() + '</td>';
				}
				curdate = new Date(curdate.valueOf() + oneday);
			} else { 
				html += '<td class=none>&nbsp;</td>';
			}
		}
		html += '</tr>';
	}

	html = '<table cellpadding="0" cellspacing="0" id="cal"><tr><td class=outer>\
	<table width="150" height="130" cellpadding="3" cellspacing="1">' 
		+ html 
		+ '</table>\
	</td></tr></table>';

	derek = document.getElementById(iframeid);
	derek.innerHTML = html
	derek.style.visibility = 'visible';

}

