var m_DateTimeString;
var m_TimeString;
var m_DateString;
var m_ServerDateTime = new Date();
var m_StartOfYearDateTime = new Date();

//Constructor
function calendar(id,date,timezone,hiddenFieldId,dateFieldId,timeFieldId){
	this.id = id;
	// use date object if passed, get UTC date from hidden form element if not
	if (date=='')
	{
		d=new Date(document.getElementById(hiddenFieldId).value);
	}
	else 
	{
		d=date;
	}
	this.dateObject = d;
	this.write = writeCalendar;
	this.length = getLength;
	this.month = d.getMonth();
	this.date = d.getDate();
	this.day = d.getDay();
	this.hours = d.getHours();
	this.minutes = d.getMinutes();

	this.changeMonth=changeMonth;
	this.timeZone = "+01:00";
	
	this.hiddenFieldId=hiddenFieldId;
	this.dateFieldId=dateFieldId;
	this.timeFieldId=timeFieldId;

	this.seconds = d.getSeconds();
	this.year = d.getFullYear();
	//get the first day of the month's day
	d.setDate(1);
	this.firstDay = d.getDay();
	//then reset the date object to the correct date
	d.setDate(this.date);
}
function toggleSelects()
{
	// assumes last element in inported stylesheet is select
	for ( i = 0; i < document.styleSheets.length; i++ )
	{
		if (document.styleSheets[i].imports!=undefined){
			for ( j = 0; j < document.styleSheets(i).imports.length; j++ )
			{
				var theRules = new Array();
				if (document.styleSheets(1).imports(j).rules)
				{	
					theRules = document.styleSheets(1).imports(j).rules;
					// set to visible if hidden
					if (theRules[theRules.length-1].style.visibility=='hidden')
					{
						theRules[theRules.length-1].style.visibility = 'visible';
					}
					else 
					{
						theRules[theRules.length-1].style.visibility = 'hidden';
					}
				}			
			}
		}
	}
}
function showCalendar(textElem,calendarElem, toggle, hiddenFieldId, dateFieldId, datePassed, timeFieldId){
	var calendarObjectContainer=document.getElementById(calendarElem + "Container");
	if (calendarObjectContainer.className=="calendarContainer"){
		// we may not always wish to hide select boxes - IE may actually fix their bug
		if (toggle==true){
			toggleSelects();
		}
		var date;
		if (document.getElementById(textElem).value=="")
		{
			date = datePassed;
		}
		else 
		{
			date = document.getElementById(textElem).value;			
		}		

		// instantiate new calendar and write into old calendar's container
		
		newcal = new calendar(calendarElem,new Date(date),'',hiddenFieldId,dateFieldId,timeFieldId);
		
		var html=newcal.write(true);
		calendarObjectContainer.innerHTML=html;
		calendarObjectContainer.className=calendarObjectContainer.className + "Active";
	}
	return false;
}
function hideCalendar(calendarElem){
	var calendarObjectContainer=document.getElementById(calendarElem + "Container");
	calendarObjectContainer.className=calendarObjectContainer.className.replace(new RegExp("( ?|^)Active\\b"), "");
	toggleSelects();
	return false;
}

function writeCalendar(isAlreadyContained){
    var calString=''
	var strHours=this.hours;
	var strMinutes=this.minutes;
	
	// get hours  / minutes strings
	//SM: Need to rationalise for localisation of times. Some regions will want to display 8:10pm so no 0 needed
	if (this.hours <10)
	{
		strHours="0" + this.hours;	
	}
	if (this.minutes <10)
	{
		strMinutes="0" + this.minutes;
	}
	
	//write month and year at top of table
    // only write out the container if calendar doesn't exist
    if (isAlreadyContained!=true)
    {
		calString +='<div id="' + this.id + 'Container" class="calendarContainer">';
	}
	calString += '<div class="closeLink" title="' + messageCloseText +'" onClick="hideCalendar(\'' + this.id  + '\');" href="javascript:;">X</div>';
	calString += '<table class="calendar" id="' + this.id + '" sumnmary="' + messageTableSummary + '" cellspacing="0" width="200">';

	//write the month
	calString += '<tr><th colspan="7" class="month">' + cultureMonths[this.month] + '&nbsp;' + this.year + '</th></tr>';
	//write a row containing days of the week
	calString += '<tr>';
	
	for(i=0;i<cultureDays.length;i++){
		calString += '<th class="dayHeader">' + cultureDays[i].substring(0,3) + '</th>';
	}
	
	//write the body of the calendar
	calString += '<tr>';
	//create 6 rows so that the calendar doesn't resize
	for(j=0;j<42;j++){
		var displayNum = (j-this.firstDay+1);
		if(j<this.firstDay){
			//write the leading empty cells
			calString += '<td class="empty">&nbsp;</td>';
		}else if(displayNum==this.date){
			calString += '<td class="days"><a href="javascript:;" class="selected" id="' + this.id + 'selected" title="' + messageDateSelect + '" onClick="javascript:changeDate(this,\'' + this.id + '\', \'' + this.year + '\', \'' + this.month + '\', \'' + this.date + '\', \'' + this.hours + '\', \'' + this.minutes + '\', \'' + this.seconds + '\', \'' + this.timeZone + '\')">' + displayNum + '</a></td>';
		}else if(displayNum > this.length()){
			//Empty cells at bottom of calendar
			calString += '<td class="empty">&nbsp;</td>';
		}else{
			//the rest of the numbered cells
			calString += '<td class="days"><a href="javascript:;" onClick="javascript:changeDate(this,\'' + this.id + '\', \'' + this.year + '\', \'' + this.month + '\', \'' + this.date + '\', \'' + this.hours + '\', \'' + this.minutes + '\', \'' + this.seconds + '\', \'' + this.timeZone + '\')" title="' + messageDateSelect + '">' + displayNum + '</a></td>';
		}
		if(j%7==6){
			calString += '</tr><tr>';
		}
	}
	//close the last number row
	calString += '</tr>';
	//write the nav row
	calString += '<tr>';
	calString += '<td class="nav"><a href="javascript:javascript:;" title="' + messageYearBack + '" onClick="changeMonth(-12,\'' + this.id + '\', \'' + this.year + '\', \'' + this.month + '\', \'' + this.date + '\', \'' + this.hours + '\', \'' + this.minutes + '\', \'' + this.timeZone  + '\', \'' + this.hiddenFieldId  + '\', \'' + this.dateFieldId + '\', \'' + this.timeFieldId + '\')">&lt;</a></td>';
	calString += '<td class="nav"><a href="javascript:javascript:;" title="' + messageMonthBack + '"  onClick="changeMonth(-1,\'' + this.id + '\', \'' + this.year + '\', \'' + this.month + '\', \'' + this.date + '\', \'' + this.hours + '\', \'' + this.minutes + '\', \'' + this.timeZone  + '\', \'' + this.hiddenFieldId  + '\', \'' + this.dateFieldId + '\', \'' + this.timeFieldId + '\')">&lt;</a></td>';
	calString += '<td class="month" colspan="3">&nbsp;</td>';
	calString += '<td class="nav"><a href="javascript:javascript:;" title="' + messageMonthForward + '"  onClick="changeMonth(1,\'' + this.id + '\', \'' + this.year + '\', \'' + this.month + '\', \'' + this.date + '\', \'' + this.hours + '\', \'' + this.minutes + '\', \'' + this.timeZone  + '\', \'' + this.hiddenFieldId  + '\', \'' + this.dateFieldId + '\', \'' + this.timeFieldId + '\')">&gt;</a></td>';
	calString += '<td class="nav"><a href="javascript:javascript:;" title="' + messageYearForward + '"  onClick="changeMonth(12,\'' + this.id + '\', \'' + this.year + '\', \'' + this.month + '\', \'' + this.date + '\', \'' + this.hours + '\', \'' + this.minutes + '\', \'' + this.timeZone  + '\', \'' + this.hiddenFieldId  + '\', \'' + this.dateFieldId + '\', \'' + this.timeFieldId + '\')">&gt;</a></td>';
	calString += '</tr>';
	if (this.timeFieldId !=''){
		calString += '<tr><td colspan="7" class="timeContainer"><label for="' + this.id + 'time">' + labelTime + '</label><input id="' + this.id + 'Time" type="text" class="time "name="time" title="Time" value="' + strHours + ':' + strMinutes + '" /></td></tr>';
	}
	calString += '<tr><td colspan="7" class="buttonContainer"><input type="button" onClick="setDateTime(\'' + this.id + '\', \'' + this.year + '\', \'' + this.month + '\', \'' + this.date + '\', \'' + this.id + 'Time' + '\', \'' + this.hiddenFieldId  + '\', \'' + this.dateFieldId + '\', \'' + this.timeFieldId + '\')" value="' + messageDateSelect + '"></td></tr>';

	calString += '</table>';
	if (isAlreadyContained!=true)
	{
		calString += '</div>';
	}
	//var test="<textarea>" + calString + "</textarea>";
	//calString+=test;
	return(calString);
	//return test;
}

function getLength(){
	//thirty days has September...
	switch(this.month){
		case 1:
			if((this.dateObject.getFullYear()%4==0&&this.dateObject.getFullYear()%100!=0)||this.dateObject.getFullYear()%400==0)
				return 29; //leap year
			else
				return 28;
		case 3:
			return 30;
		case 5:
			return 30;
		case 8:
			return 30;
		case 10:
			return 30
		default:
			return 31;
	}
}
function changeDate(anchor,calid,year,month,day,hours,minutes)
{
	// take 'selected' id off old value
	document.getElementById(calid + "selected").className = "";
	document.getElementById(calid + "selected").id = "";
	// add 'selected'item 
	anchor.className = "selected";
	anchor.id = calid + "selected";
}

function changeMonth(mo,calid,year,month,day,hours,minutes,timezone,hiddenFieldId,dateFieldId,timeFieldId)
{
	// get selected day
	var node=document.getElementById(calid + "selected");
	selectedDay=node.innerHTML;
	
	// turn strings into ints
	month=eval(month);
	mo=eval(mo);
	
	// new month
	newmonth=month+mo;
	
	// get containing div
	calCont=document.getElementById(calid + "Container");
	
	// instantiate new calendar and write into old calendar's container
	newcal = new calendar(calid,new Date(year,newmonth,selectedDay,hours,minutes),timezone,hiddenFieldId,dateFieldId,timeFieldId);
	var html=newcal.write(true);
	calCont.innerHTML=html;
}
function setDateTime(calid,year,month,day,timeId,hiddenFieldId,dateFieldId,timeFieldId)
{
	// get selected day
	var node=document.getElementById(calid + "selected");
	selectedDay=node.innerHTML;
	if(timeFieldId!=''){
		var time=document.getElementById(timeId).value;
	}
	//SM: What rules are there about default time. Will anything ever be of a fixed time which is never displayed?
	else {
		time='';
	}
	//adjust to zero indexing
	month=eval(month) + 1;
	if (month < 10){
		month="0" + month;
	}
	
	selectedDay=eval(selectedDay);
	if (selectedDay <10){
		selectedDay="0" + selectedDay;
	}
	//SM: need to adjust to DateTimeFormatInfo passed in
	document.getElementById(hiddenFieldId).value=selectedDay + "/" + month + "/" + year;
	document.getElementById(dateFieldId).value=selectedDay + "/" + month + "/" + year;
	
	if (timeFieldId!=''){
		document.getElementById(timeFieldId).value=time;
	}
	hideCalendar(calid);
}


