var undefined;
var calpopup, bIE = false;
if (typeof(window.createPopup) != "undefined")
{
	bIE = true;
	calpopup = window.createPopup();
}

var CalendarMonths = new Array("January","February","March","April","May","June",
							"July","August","September","October","November","December");
							
// BEGIN WebModel support							
function popupcalendar_Load()
{
	
}						

function PopupCalendar()
{
	this.Render = PopupCalendar_Render;	
}	

function PopupCalendar_Render(container)
{
	if (this.textbox == undefined)
		return;
	if (container == undefined)
		container = this.textbox.container.parentElement;
	var img = document.createElement("<img src='img/popupcalendar.gif' style='position:absolute;'>");
	var textboxid = this.textbox.id;
	function OpenCalendar() {OpenPopupCalendar(textboxid,undefined,'css');};
	if (bIE)
		img.attachEvent('onclick',OpenCalendar);
	else
		img.addEventListener('click', OpenCalendar, false);
	img.style.top = this.textbox.top + 2;
	img.style.left = this.textbox.left + this.textbox.width + 2;
	container.appendChild(img);
	this.container = img;
}

// END WebModel support
							
function OpenPopupCalendar(fieldname,scriptdir,cssdir)
{
	if (!bIE)
	{
		calpopup = window.open('','_blank','dialog=yes,chrome=no,titlebar=no,alwaysRaised=yes,width=212,height=162,dependent=yes,scrollbars=no,resizable=no,location=no,menubar=no,toolbar=no,directories=no');
		var baseurl = window.location.href;
		baseurl = baseurl.substring(0,baseurl.lastIndexOf('/')+1);
		if (scriptdir == undefined)
			scriptdir = baseurl + 'script';
		else
			scriptdir = baseurl + scriptdir;
		if (cssdir == undefined)
			cssdir = baseurl + 'style';
		else
			cssdir = baseurl + cssdir;
	}
	var link = calpopup.document.createElement('LINK');
	if (cssdir == undefined)
		cssdir = 'style';
	link.href= cssdir + '/popupcalendar.css';
	link.rel='stylesheet';
	link.type='text/css';
	if (bIE)
		calpopup.document.body.parentNode.insertBefore(link,calpopup.document.body); //Old code; this is good enough for IE.
	else
	{
		//Otherwise do it properly: add <LINK> as child of <HEAD>
		if (calpopup.document.body.parentNode.firstChild.nodeName == "HEAD") //e.g. Firefox
			calpopup.document.body.parentNode.firstChild.appendChild(link);
		else //e.g. Safari
		{
			var head = calpopup.document.createElement('HEAD');
			calpopup.document.body.parentNode.insertBefore(head,calpopup.document.body);
			head.appendChild(link);
		}
	}
	calpopup.document.body.style.border = '1 solid black';
	
	var cal = new CPopupCalendar(fieldname);
	cal.Render(calpopup.document.body);				
	var script = calpopup.document.createElement('script');
	script.language='javascript';
	if (scriptdir == undefined)
		scriptdir = 'script';
	script.src=scriptdir+'/popupcalendar.js';
	calpopup.document.body.appendChild(script);
	if (bIE)	
		calpopup.document.body.onunload= new Function('CPopupCalendar_UpdateField();');
	else
		calpopup.onunload= new Function('CPopupCalendar_UpdateField();');

	try
	{
		if (bIE)
		{
			var field = document.getElementById(fieldname);
			var rect = field.getBoundingClientRect();
			var x = 0, y = 21;
			var over = (rect.top+152) - document.body.scrollHeight;
		
			if (over > 0)
				calpopup.show(100,-142,202,152,field);
			else
				calpopup.show(x,y,202,152,field);
		}
	}
	catch (e) {alert("Couldn't load calendar - Error name: " + e.name + ". Error message: " + e.message);}
}

function CPopupCalendar(field)
{
	var today = new Date();
	this.day = today.getDate();
	this.month = today.getMonth()+1;
	this.year = today.getFullYear();
	this.field = field;
	var value = '';
	var field = document.getElementById(field);
	if (field != undefined)
		value = field.value;
	
	if (value.length > 0)
	{
		var re1 = /^(\d{1,2})\/(\d{1,2})\/(\d|\d{2}|\d{4})$/
		var re2 = /^(\d\d)(\d\d)(\d\d\d\d)$/
		var re3 = /^(\d{1,2}) (\d{1,2}) (\d|\d{2}|\d{4})$/
		if (re1.test(value))
		{
			this.day = RegExp.$1;
			this.month = RegExp.$2;
			this.year = RegExp.$3;
		}
		else if (re2.test(value))
		{
			this.day = RegExp.$1;
			this.month = RegExp.$2;
			this.year = RegExp.$3;
		}
		else if (re3.test(value))
		{
			this.day = RegExp.$1;
			this.month = RegExp.$2;
			this.year = RegExp.$3;
		}
		this.day = (new Number(this.day)).valueOf();
		this.month = (new Number(this.month)).valueOf();
	}
	if (this.month < 1)
		this.month = 1;
	if (this.month > 12)
		this.month = 12;
	if (this.day < 1)
		this.day = 1;
	if (this.day > CalendarDaysInMonth(this.month-1, this.year))
	{
		this.day = CalendarDaysInMonth(this.month-1, this.year);
	}
						
	this.Render = CPopupCalendar_Render;
	this.Redraw = CPopupCalendar_Redraw;
	this.ClickDay = CPopupCalendar_ClickDay;
	this.NextMonth = CPopupCalendar_NextMonth;
	this.PreviousMonth = CPopupCalendar_PreviousMonth;
	this.FixLastDay = CPopupCalendar_FixLastDay;
	this.ChangeMonth = CPopupCalendar_ChangeMonth;
	this.ChangeYear = CPopupCalendar_ChangeYear;
}

// returns the number of days in a given month (0=January, 11=December) for a given year
function CalendarDaysInMonth(month,year)
{
	switch (month)
	{
	case 0://jan
	case 2://mar
	case 4://may
	case 6://jul
	case 7://aug
	case 9://oct
	case 11://dec
		return 31;

	case 3://apr
	case 5://jun
	case 8://sep
	case 10://nov
		return 30;

	case 1://feb
	{
		if (year%4 == 0)
			return 29;
		else
			return 28;
	}

	default:
		//alert('There is no such month. The value of month should be between 0 and 11.');
		return 0;
	}
	return 0;
}

function CalendarMonthOver(sender)
{
	if (sender.className == 'CalendarMonth')
		sender.className = 'CalendarMonthOver';
	else
		sender.className = 'CalendarMonth';
}

function CalendarDayOver(sender)
{
	if (sender.className == 'CalendarSelectedDay' || sender.className == 'CalendarNonDay')
		return;
	if (sender.className == 'CalendarBaseDay')
		sender.className = 'CalendarHoverDay';
	else
		sender.className = 'CalendarBaseDay';
}

function CPopupCalendar_NextMonth()
{
	document.getElementById('month').value = document.getElementById('month').value % 12 + 1;
	if (document.getElementById('month').value == 1)
		document.getElementById('year').value++;
	CPopupCalendar_FixLastDay();
	CPopupCalendar_Redraw();
}

function CPopupCalendar_PreviousMonth()
{
	document.getElementById('month').value = document.getElementById('month').value - 1;
	if (document.getElementById('month').value == 0)
	{
		document.getElementById('month').value = 12;
		document.getElementById('year').value--;
	}
	CPopupCalendar_FixLastDay();
	CPopupCalendar_Redraw();
}

function CPopupCalendar_ChangeMonth(month)
{
	document.getElementById('month').value = month;
	CPopupCalendar_FixLastDay();
	CPopupCalendar_Redraw();
}

function CPopupCalendar_ChangeYear(direction)
{
	var newyear = document.getElementById('year').value - (-direction);
	if (newyear < 1902 || newyear > 9998)
	{
		alert('You can not select the year ' + newyear);
		return;
	}
	document.getElementById('year').value = newyear;
	CPopupCalendar_FixLastDay();
	CPopupCalendar_Redraw();
}

function CPopupCalendar_FixLastDay()
{
	var days = CalendarDaysInMonth(document.getElementById('month').value-1, document.getElementById('year').value);
	if (document.getElementById('day').value > days)
	{
		document.getElementById('day').value = days;
	}
}

function CPopupCalendar_Render(target)
{
//	document.body.ondeactivate = new Function('CPopupCalendar_UpdateField();');
	var dtDate = new Date(this.year, this.month-1, 1);
	var firstDay = dtDate.getDay();
	var lastDay = CalendarDaysInMonth(this.month-1,this.year);
	var lastMonthLastDay;
	if (this.month == 1)
		lastMonthLastDay = CalendarDaysInMonth(11,this.year-1);
	else
		lastMonthLastDay = CalendarDaysInMonth(this.month-2,this.year);
	var apos = "'";
	var i;
	var html;
	html = '<span class="Calendar" id="Calendar">' +
			'<input type="hidden" id="day" value="' + this.day + '"/>' + 
			'<input type="hidden" id="month" value="' + this.month + '"/>' + 
			'<input type="hidden" id="year" value="' + this.year + '"/>' + 
			'<input type="hidden" id="field" value="' + this.field + '"/>' + 
		   '<table width=200 cellspacing="0"><tr>' + 
		   '<td class="CalendarMonth" onmouseover="CalendarMonthOver(this)" onmouseout="CalendarMonthOver(this)" UNSELECTABLE="on" align="center" id="CalendarPrevYear" onclick="CPopupCalendar_ChangeYear(-1);">' + (this.year - 1) + '</td>'
	for (i = 0; i < 12; i++)
	{
		html += '<td class="CalendarMonth" onmouseover="CalendarMonthOver(this)" onmouseout="CalendarMonthOver(this)" UNSELECTABLE="on" align="center" onclick="CPopupCalendar_ChangeMonth(' + (i+1) + ');">' + CalendarMonths[i].substr(0,3) + '</td>'
		if (i == 5)
			html += '</tr><tr>';
	}
	html += '<td class="CalendarMonth" onmouseover="CalendarMonthOver(this)" onmouseout="CalendarMonthOver(this)" UNSELECTABLE="on" align="center"  id="CalendarNextYear" onclick="CPopupCalendar_ChangeYear(1);">' + (this.year - (-1)) + '</td>'
	html += '</tr></table>' +
			'<table width=200 cellspacing="0"><tr>' +
			'<td align=center class="CalendarHeader" id="CalendarHeader">' + this.day + ' ' + CalendarMonths[this.month-1] + ' ' + this.year + '</td>' + 
			'</tr></table>' +
			'<table cellspacing=0 cellpadding=0 width=200>' +
			'<tr><td align="center" width="14%"  class="CalendarDayTitle">Sun</td><td align="center" width="14%"  class="CalendarDayTitle">Mon</td><td align="center" width="14%"  class="CalendarDayTitle">Tue</td><td align="center" width="14%"  class="CalendarDayTitle">Wed</td><td align="center" width="14%"  class="CalendarDayTitle">Thu</td><td align="center" width="14%"  class="CalendarDayTitle">Fri</td><td align="center" width="14%"  class="CalendarDayTitle">Sat</td></tr>' +
			'<tr>'
			for (i = 0; i < 42; i++)
			{
				var index = i - firstDay;
				if (i < firstDay)
					html += '<td align="center" class="CalendarNonDay" UNSELECTABLE="on" onmouseover="CalendarDayOver(this);" onmouseout="CalendarDayOver(this);" onclick="CPopupCalendar_ClickDay(this,' + i + ');" id="CalendarDayBtn:' + i + '">' + (lastMonthLastDay - firstDay + i + 1) + '</td>';
				else if (index + 1> lastDay)
					html += '<td align="center" class="CalendarNonDay" UNSELECTABLE="on" onmouseover="CalendarDayOver(this);" onmouseout="CalendarDayOver(this);" onclick="CPopupCalendar_ClickDay(this,' + i + ');" id="CalendarDayBtn:' + i + '">' + (i - firstDay - lastDay + 1) + '</td>';
				else
				{
					var style = 'CalendarBaseDay';
					if (index+1 == this.day)
						style = 'CalendarSelectedDay';
					html += '<td align="center" UNSELECTABLE="on" class="' + style + '" id="CalendarDayBtn:' + i + '" ';
					html += 'onmouseover="CalendarDayOver(this);" onmouseout="CalendarDayOver(this);" ';
					html += 'onclick="CPopupCalendar_ClickDay(this,' + i + ');"';
					html += '>' + (index + 1) + '</td>';
				}
				if (i % 7 == 6)
					html +=	'</tr>';
			}
	html +=	'</tr>';
	html +=	'</table>' + 
			'</span>';
	if (target == undefined)
	{
		document.write(html);
	}
	else
	{
		target.innerHTML = html;
	}	
}

function CPopupCalendar_Redraw()
{
	var thisday = document.getElementById('day').value;
	var thismonth = document.getElementById('month').value;
	var thisyear = document.getElementById('year').value;
	var dtDate = new Date(thisyear, thismonth-1, 1);
	var firstDay = dtDate.getDay();
	var lastDay = CalendarDaysInMonth(thismonth-1,thisyear);
	var lastMonthLastDay;
	if (thismonth == 1)
		lastMonthLastDay = CalendarDaysInMonth(11,thisyear-1);
	else
		lastMonthLastDay = CalendarDaysInMonth(thismonth-2,thisyear);
	
	if (bIE)
	{	
		document.all('CalendarHeader').innerText = thisday + ' ' + CalendarMonths[thismonth-1] + ' ' + thisyear;
		document.all('CalendarNextYear').innerText = thisyear - (-1);
		document.all('CalendarPrevYear').innerText = thisyear - 1;
	}
	else
	{
		document.getElementById('CalendarHeader').textContent = thisday + ' ' + CalendarMonths[thismonth-1] + ' ' + thisyear;
		document.getElementById('CalendarNextYear').textContent = thisyear - (-1);
		document.getElementById('CalendarPrevYear').textContent = thisyear - 1;
	}
	
	for (i = 0; i < 42; i++)
	{
		var index = i - firstDay;
		var dayCell = document.getElementById('CalendarDayBtn:' + i);
		if (i < firstDay)
		{
			dayCell.className = 'CalendarNonDay';
			if (bIE)
				dayCell.innerText = (lastMonthLastDay - firstDay + i + 1);
			else
				dayCell.textContent = (lastMonthLastDay - firstDay + i + 1);
		}
		else if (index + 1> lastDay)
		{
			dayCell.className = 'CalendarNonDay';
			if (bIE)
				dayCell.innerText = (i - firstDay - lastDay + 1);
			else
				dayCell.textContent = (i - firstDay - lastDay + 1);
		}
		else
		{
			var style = 'CalendarBaseDay';
			if (index+1 == thisday)
				style = 'CalendarSelectedDay';
			dayCell.className = style;
			if (bIE)
				dayCell.innerText = (index + 1);
			else
				dayCell.textContent = (index + 1);
		}
	}
//	CPopupCalendar_UpdateField();
}

function CPopupCalendar_ClickDay(sender, index)
{
	var thisday = document.getElementById('day').value;
	var thismonth = document.getElementById('month').value;
	var thisyear = document.getElementById('year').value;
	var dtDate = new Date(thisyear, thismonth-1, 1);
	var firstDay = dtDate.getDay();
	var day = index - firstDay + 1;
	if (day < 1)
	{
		var month = thismonth - 1;
		var year = thisyear;
		if (month == 0)
		{
			month = 12;
			year--;
		}
		document.getElementById('day').value = CalendarDaysInMonth(month-1,year) + day;
		CPopupCalendar_PreviousMonth();
		if (bIE)
			parent.calpopup.hide();
		else
			window.close();
		return;
	}
	else if (day > CalendarDaysInMonth(thismonth-1,thisyear))
	{
		document.getElementById('day').value = day - CalendarDaysInMonth(thismonth-1,thisyear);
		CPopupCalendar_NextMonth();
		if (bIE)
			parent.calpopup.hide();
		else
			window.close();
		return;
	}
	document.getElementById('day').value = day;
	thisday = day;
	for (i = 0; i < 42; i++)
	{
		var dayCell = document.getElementById('CalendarDayBtn:' + i);
		if (dayCell.className == 'CalendarSelectedDay')
			dayCell.className = 'CalendarBaseDay';
	}
	
	if (sender.className == 'CalendarSelectedDay')
		sender.className = 'CalendarBaseDay';
	else
		sender.className = 'CalendarSelectedDay';
		
//	CPopupCalendar_UpdateField();
	if (bIE)
	{
		document.getElementById('CalendarHeader').innerText = thisday + ' ' + CalendarMonths[thismonth-1] + ' ' + thisyear;
		parent.calpopup.hide();
	}
	else
	{
		document.getElementById('CalendarHeader').textContent = thisday + ' ' + CalendarMonths[thismonth-1] + ' ' + thisyear;
		window.close();
	}
}

function CPopupCalendar_UpdateField()
{
	var fieldid = document.getElementById('field');
	if (fieldid == undefined)
		fieldid = calpopup.document.getElementById('field');
	if (fieldid != undefined)
	{
		var field = parent.document.getElementById(fieldid.value);
		if (field == undefined)
			field = document.getElementById(fieldid.value);
		if (field != undefined)
		{
			var thisday = document.getElementById('day');
			if (thisday == undefined)
				thisday = calpopup.document.getElementById('day');
			thisday = thisday.value;
			var thismonth = document.getElementById('month');
			if (thismonth == undefined)
				thismonth = calpopup.document.getElementById('month');
			thismonth = thismonth.value;
			var thisyear = document.getElementById('year');
				thisyear = calpopup.document.getElementById('year');
			thisyear = thisyear.value;
			if (thisday.length == 1)
				thisday = '0' + thisday;
			if (thismonth.length == 1)
				thismonth = '0' + thismonth;
			field.value = thisday + '/' + thismonth + '/' + thisyear;
			if (bIE)
			{
				field.fireEvent('onchange');
				field.fireEvent('onblur');
			}
			else
			{
				var changeEvent = window.document.createEvent("HTMLEvents");
				changeEvent.initEvent("change", false, true);
				field.dispatchEvent(changeEvent);
				
				var blurEvent = window.document.createEvent("HTMLEvents");
				blurEvent.initEvent("blur", false, true);
				field.dispatchEvent(blurEvent);
			}
		}
	}
}

