// --------
//
// CALENDAR

var maxmonthsahead = 24;
var oneday = 86400000;
var caldate = null;
var curcalid = '';
var calnextid;
var calnextdays;
var blockprev = true;
var blnSpecial = false;

function dmy(d) 
{
	//MRE Added 05/Jul/07:  If being called by the new specials wizard then return date in dd mmm yyyy otherwise return dd/mm/yyyy
	if(blnSpecial == true)
	{
		var intDay = d.getDate();
		var strDay;
		if (intDay < 10)
		{
			strDay = '0' + intDay;
		}
		else
		{
			strDay = intDay;
		}
		var Mths = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
		var Mth = Mths[d.getMonth()];
		blnSpecial = false;
		return strDay + ' ' + (Mth) + ' ' + d.getFullYear();
	}
	else
	{
		return d.getDate() + '/' + (d.getMonth()+1) + '/' + 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 ;
}

var closeCal = true;


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 = new Date(caldate.getYear(), newmonth, 1);	//move to the 1st of the next month
	caldate.setMonth(newmonth);

	closeCal = false;
}

function calclick(txtid, iframeid, cell) {
	caldate = new Date(caldate.getFullYear(), (caldate.getMonth()), cell.innerHTML);
	document.getElementById(txtid).value = dmy(caldate);

	closeCal = true;

	calclose(iframeid);
}

function calclose(iframeid) 
{

	if(closeCal == false)
        {
		closeCal = true;
		return;
        }
	
	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, special) 
{

//MRE Added 05/Jul/07: special is an optional variable which is only passed by the new specials wizard	
	if (typeof special != 'undefined')
	{
		blnSpecial = true;
	}
	if (curcalid != '') calclose(curcalid);
	caldate = ripdmy(document.getElementById(txtid).value);
	calnextid = nextid;
	calnextdays = nextdays;
	cal(txtid, iframeid);
	curcalid = iframeid;
}

function cal(txtid, iframeid) {
	var intDay
	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;
					}
					//MRE This is a dirty hack! Because there were two 6th April's in 2008 and two 5th April's in 2009!!
					if((curdate.getDate()==6 && calmonth==3 && intDay==6 && caldate.getYear()==2008)|(curdate.getDate()==5 && calmonth==3 && intDay==5 && caldate.getYear()==2009))
					{
					   curdate = new Date(curdate.valueOf() + oneday);
					}
					intDay = curdate.getDate();

					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';

}


