// L10N
var monthArray;
var monthArrayShort;
var dayArray;
var weekString;
var todayString;
var calendarDateFomat;
var useFunction=false;

// Format of current day at the bottom of the calendar in PHP strftime format
// %a = abbreviated weekday name
// %e = day of the month
// %b = abbreviated month name
// %B = full month name
// %Y = Current year
var todayStringFormat = '%a. %e, %B %Y';

var SelectBoxScrollInterval = 200;	// n Milliseconds between year/hour scroll when mouse hovers "-" and "+"
var SelectBoxMinutesInterval = 5;	// n Minutes between each select box option

var daysInMonthArray = [31,28,31,30,31,30,31,31,30,31,30,31];

var calendarDiv = null, calendarContentDiv = null;
var califrame = null; // MSIE only to bypass z-index bug

var returnDateTo;
var returnFormat;

var calIDate = [0,0,0,0,0]; // input year,month,day,hour,minute
var calArray = ['year','month','hour','minute']; // selectBox names
var calCDate = [0,0,0,0]; // current selected year,month,hour,minute
var activeSelect = [null,null,null,null]; // current selected element
var activeSelectItem = null;
var calendarDisplayTime = false;
var selectBoxMovementInProgress = false;

function cancelCalendarEvent() { return false; }
function isLeapYear(aYear) { return (aYear%400==0||(aYear%4==0&&aYear%100!=0)); }

/* mouse movement functions */

function highlightSelectBox(e,obj)
{
	if(!obj)obj=this;
	activeSelectItem = obj;
	if(obj.className=='ActiveSelBox'){
		obj.className='';
	}else{
		obj.className = 'ActiveSelBox';
		activeSelect[1] = obj;
	}
	selectBoxMovementInProgress = false;
}

function highlightDiv()
{
	if(this.className.indexOf('Over')>0){
		this.className = this.className.substr(0,this.className.indexOf('Over'));
	}else{
		this.className += 'Over';
	}
}

/* mouse onclick functions */

function showDropDown()
{
	var e = document.getElementById(this.id.substr(0,this.id.indexOf('Select'))+'DropDown');
	if(e.style.display=='block'){
		e.style.display='none';
	}else{
		hideDropDowns();
		e.style.display='block';
	}
}
function selectDateItem()
{
	name = this.id.substr(0,this.id.indexOf('Div')); // minute|hour
	for(i=0;i<4;++i) { if(name == calArray[i]){ break; } }
	document.getElementById(name+'Select').innerHTML = this.innerHTML
	calCDate[i] = (i!=1)?this.innerHTML.replace(/[^\d]/g,''):this.id.replace(/[^\d]/g,'');
	document.getElementById(name+'DropDown').style.display='none';
	if(activeSelect[i]){ activeSelect[i].className=''; }
	this.className='ActiveSelBox';
	activeSelect[i]=this;
	if(i<2){ writeCalendarContent(); }
}
function switchMonth()
{
	hideDropDowns();
	if(this.className.indexOf('prev')>=0){
		--calCDate[1];
		if(calCDate[1]<0){
			calCDate[1]=11;
			--calCDate[0];
		}
	}else{
		++calCDate[1];
		if(calCDate[1]>11){
			calCDate[1]=0;
			calCDate[0]=calCDate[0]/1+1;
		}
	}
	writeCalendarContent();
	//resizeIframe();
}

function switchYear()
{
	hideDropDowns();
	
	if(this.className.indexOf('prev')>=0)
	{
		--calCDate[0];
	}
	else
	{
		calCDate[0]=calCDate[0]/1+1;
	}
	writeCalendarContent();
	//resizeIframe();
}

function pickDate(e,inputDay)
{
	if(returnFormat)
	{
		try
		{
			returnDateTo=document.getElementById(returnDateTo.id);
			
			var month = calCDate[1]/1 +1;
			if(month<10) month = '0' + month;
			var day = (!inputDay && this)?this.innerHTML:inputDay;
			if(day/1<10) day = '0' + day;
			returnFormat = returnFormat.replace('%Y',calCDate[0]);
			returnFormat = returnFormat.replace('%m',month);
			returnFormat = returnFormat.replace('%d',day);
			returnFormat = returnFormat.replace('%H',calCDate[2]);
			returnFormat = returnFormat.replace('%M',calCDate[3]);
			returnDateTo.value = returnFormat;
		}
		catch(e)
		{
			
		}
	}
	
	if (useFunction)
	{
		year=calCDate[0];
		HRCCalendarReturnFunc(year,month,day);
	}
	
	closeCalendar();
}
function pickTodaysDate()
{
	var d = new Date();
	calCDate[1] = d.getMonth();
	calCDate[0] = d.getFullYear();
	pickDate(false,d.getDate());
}

/* sliding related functions */

function slideCalendarSelectBox()
{
	if(selectBoxMovementInProgress && activeSelectItem){
		changeSelectBox(false,activeSelectItem);
	}
	setTimeout('slideCalendarSelectBox()',SelectBoxScrollInterval);
}
function changeSelectBox(e,obj)
{
	if(!obj)obj = this;
	var items = obj.parentNode.getElementsByTagName('div');
	var start = items[1].innerHTML/1;
	var type = 0;
	if(obj.parentNode.id=='hourDropDown'){
		type = 2;
		if(obj.innerHTML.indexOf('-')>=0){
			if(--start<0)start=0;
			if(activeSelect[2]){ activeSelect[2].className=''; }
		}else{
			if(++start>14){ start = 14; }
			if(activeSelect[2]){ activeSelect[2].className=''; }
		}
		var prefix = '';
	}else{
		if(obj.innerHTML.indexOf('-')>=0){
			--start;
			if(activeSelect[0]){ activeSelect[0].className=''; }
		}else{
			++start;
			if(activeSelect[0]){ activeSelect[0].className=''; }
		}
	}
	for(var i=1;i<items.length-1;++i){
		items[i].innerHTML = (((start/1 + i/1) < 11)?'0':'') + (start+i-1);
		items[i].id = calArray[type]+'Div' + (start/1+i/1-1);
	}
	if(activeSelect[type]){
		activeSelect[type].className='';
		if(document.getElementById(calArray[type]+'Div'+calCDate[type])){
			activeSelect[type] = document.getElementById(calArray[type]+'Div'+calCDate[type]);
			activeSelect[type].className='ActiveSelBox';
		}
	}
}
function setSelectBoxMovement()
{
	highlightSelectBox(false,this);
	if(this.innerHTML.indexOf('-')>=0 || this.innerHTML.indexOf('+')>=0){
		selectBoxMovementInProgress = (this.className=='ActiveSelBox');
	}
}

/* update functions */

function updateYearDiv()
{
	var div = document.getElementById('yearDropDown');
	var yearItems = div.getElementsByTagName('div');
	for(var i=1;i<yearItems.length-1;++i){
		yearItems[i].innerHTML = calCDate[0]/1 -6 + i;
		if(calCDate[0]==(calCDate[0]/1 -6 + i)){
			yearItems[i].className='ActiveSelBox';
			activeSelect[0] = yearItems[i];
		}else{
			yearItems[i].className = '';
		}
	}
}
function updateMonthDiv()
{
	for(i=0;i<12;++i){ document.getElementById('monthDiv'+i).className = ''; }
	document.getElementById('monthDiv'+calCDate[1]).className = 'ActiveSelBox';
	activeSelect[1] = document.getElementById('monthDiv'+calCDate[1]);
}
function updateHourDiv()
{
	var div = document.getElementById('hourDropDown');
	var hourItems = div.getElementsByTagName('div');
	var addHours = 0;
	if((calCDate[2]/1 -6 + 1)<0){ addHours = (calCDate[2]/1 -6 + 1)*-1; }
	for(var i=1;i<hourItems.length-1;++i){
		hourItems[i].innerHTML = (((calCDate[2]/1 -6 + i + addHours) < 10)?'0':'') + (calCDate[2]/1 -6 + i + addHours);
		if(calCDate[2]==(calCDate[2]/1 -6 + i)){
			hourItems[i].className='ActiveSelBox';
			activeSelect[2] = hourItems[i];
		}else{
			hourItems[i].className='';
		}
	}
}
function updateMinuteDiv()
{
	for(i=0;i<60;i+=SelectBoxMinutesInterval){
		document.getElementById('minuteDiv' + i).className='';
	}
	if(document.getElementById('minuteDiv' + calCDate[3])){
		document.getElementById('minuteDiv' + calCDate[3]).className='ActiveSelBox';
		activeSelect[3] = document.getElementById('minuteDiv' + calCDate[3]);
	}
}

/* create functions */

function createYearDiv()
{
	var i;
	if(!document.getElementById('yearDropDown')){
		var div = document.createElement('div');
		div.className='selectbox';
	}else{
		var div = document.getElementById('yearDropDown');
		var subDivs = div.getElementsByTagName('div');
		for(i=0;i<subDivs.length;++i){ subDivs[i].parentNode.removeChild(subDivs[i]); }
	}
	var d = new Date();
	if(calCDate[0]){ d.setFullYear(calCDate[0]); }
	var startYear = d.getFullYear()/1 - 5;
	createScrollDiv(div, '-')
	for(i=startYear;i<(startYear+10);++i){ createCalsubDiv(div, 0, i); }
	createScrollDiv(div, '+')
	return div;
}
function createMonthDiv(){
	var div = document.createElement('div');
	div.className='selectbox';
	div.id = 'monthPicker';
	for(var i=0;i<monthArray.length;++i){ createCalsubDiv(div, 1, i, monthArray[i]); }
	return div;
}
function createHourDiv()
{
	var i;
	if(!document.getElementById('hourDropDown')){
		var div = document.createElement('div');
		div.className='selectbox';
	}else{
		var div = document.getElementById('hourDropDown');
		var subDivs = div.getElementsByTagName('div');
		for(i=0;i<subDivs.length;++i){ subDivs[i].parentNode.removeChild(subDivs[i]); }
	}
	if(!calCDate[2])calCDate[2]=0;
	var startHour = calCDate[2]/1;
	if(startHour>14)startHour=14;
	createScrollDiv(div, '-')
	for(i=startHour;i<startHour+10;++i){ createCalsubDiv(div, 2, i); }
	createScrollDiv(div, '+')
	return div;
}
function createMinuteDiv()
{
	var i;
	if(!document.getElementById('minuteDropDown')){
		var div = document.createElement('div');
		div.className='selectbox';
	}else{
		var div = document.getElementById('minuteDropDown');
		var subDivs = div.getElementsByTagName('div');
		for(i=0;i<subDivs.length;++i){ subDivs[i].parentNode.removeChild(subDivs[i]); }
	}
	for(i=0;i<60;i+=SelectBoxMinutesInterval){ createCalsubDiv(div, 3, i); }
	return div;
}
function createCalsubDiv(parent, id, i, html)
{
	var subDiv = document.createElement('div');
	subDiv.innerHTML = (html?html:((i/1<10)?'0'+i:i));
	subDiv.onmouseover = highlightSelectBox;
	subDiv.onmouseout = highlightSelectBox;
	subDiv.onclick = selectDateItem;
	subDiv.id = calArray[id]+'Div'+i;
	subDiv.onselectstart = cancelCalendarEvent;
	parent.appendChild(subDiv);
	if(calCDate[id] && calCDate[id]==i){
		subDiv.className='ActiveSelBox';
		activeSelect[id] = subDiv;
	}
}
function createScrollDiv(parent, move)
{
	var subDiv = document.createElement('div');
	subDiv.innerHTML = '&#160;&#160;'+move;
	subDiv.onclick = changeSelectBox;
	subDiv.onmouseover = setSelectBoxMovement;
	subDiv.onmouseout = setSelectBoxMovement;
	subDiv.onselectstart = cancelCalendarEvent;
	parent.appendChild(subDiv);
}

/* write document content functions */

function initCalendar()
{
	if (!dayArray || !monthArray) {
		monthArray = ['January','February','March','April','May','June','July','August','September','October','November','December'];
		monthArrayShort = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
		dayArray = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'];
		weekString = 'Week';
		todayString = 'Today';
	}
	/* use iframe in IE */
	if(navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('Opera')<0){
		califrame = document.createElement('IFRAME');
		califrame.style.position = 'absolute';
		califrame.border='0px';
		califrame.style.border = '0px';
		califrame.src='blank.html';
		califrame.style.backgroundColor = '#FF0000';
		//califrame.contentDocument.open();
		document.body.appendChild(califrame);
	}
	calendarDiv = document.createElement('div');
	calendarDiv.id = 'calendarDiv';
	calendarDiv.style.zIndex = 1000;
	slideCalendarSelectBox();
	document.body.appendChild(calendarDiv);
	writeBottomBar();
	writeTopBar();
	if(!calCDate[0]){
		var d = new Date();
		calCDate[1] = d.getMonth();
		calCDate[0] = d.getFullYear();
	}
	writeCalendarContent();
	if (califrame)
	{
		//califrame.contentDocument.close();
	}
}
function writeTopBar()
{
	var topBar2 = document.createElement('div');
	topBar2.className = 'topBar';
	topBar2.id = 'topBar2';
	calendarDiv.appendChild(topBar2);

	var topBar = document.createElement('div');
	topBar.className = 'topBar';
	topBar.id = 'topBar';
	calendarDiv.appendChild(topBar);

	// Left arrow month
	var leftDiv = document.createElement('div');
	leftDiv.className = 'prevMonth';
	leftDiv.onclick = switchMonth;
	leftDiv.onmouseover = highlightDiv;
	leftDiv.onmouseout = highlightDiv;
	leftDiv.innerHTML = '&#160;'; // ◄
	topBar.appendChild(leftDiv);

	// Month selector
	var monthDiv = document.createElement('div');
	monthDiv.className = 'selectBox';
	monthDiv.id = 'monthSelect';
	monthDiv.innerHTML = monthArray[calCDate[1]];
	monthDiv.onmouseover = highlightDiv;
	monthDiv.onmouseout = highlightDiv;
	monthDiv.onclick = showDropDown;
	topBar.appendChild(monthDiv);
	
	// Right arrow
	var rightDiv = document.createElement('div');
	rightDiv.className = 'nextMonth';
	rightDiv.onclick = switchMonth;
	rightDiv.onmouseover = highlightDiv;
	rightDiv.onmouseout = highlightDiv;
	rightDiv.innerHTML = '&#160;'; // ►
	topBar.appendChild(rightDiv);

	

	var monthPicker = createMonthDiv();
	monthPicker.style.left = '20px';
	monthPicker.style.width = '143px';
	monthPicker.style.top = monthDiv.offsetTop + monthDiv.offsetHeight + 1 + 'px';
	monthPicker.id = 'monthDropDown';

	calendarDiv.appendChild(monthPicker);

	// Left arrow year
	var leftDiv = document.createElement('div');
	leftDiv.className = 'prevMonth';
	leftDiv.onclick = switchYear;
	leftDiv.onmouseover = highlightDiv;
	leftDiv.onmouseout = highlightDiv;
	leftDiv.innerHTML = '&#160;'; // ◄
	topBar2.appendChild(leftDiv);
	
	// Year selector
	var yearDiv = document.createElement('div');
	yearDiv.className = 'selectBox';
	yearDiv.id = 'yearSelect';
	yearDiv.innerHTML = calCDate[0];
	yearDiv.onmouseover = highlightDiv;
	yearDiv.onmouseout = highlightDiv;
	yearDiv.onclick = showDropDown;
	topBar2.appendChild(yearDiv);

	var yearPicker = createYearDiv();
	yearPicker.style.left = '20px';
	yearPicker.style.width = '143px';
	yearPicker.style.top = yearDiv.offsetTop + yearDiv.offsetHeight + 1 + 'px';
	yearPicker.id = 'yearDropDown';
	calendarDiv.appendChild(yearPicker);

	// Right arrow year
	var rightDiv = document.createElement('div');
	rightDiv.className = 'nextMonth';
	rightDiv.onclick = switchYear;
	rightDiv.onmouseover = highlightDiv;
	rightDiv.onmouseout = highlightDiv;
	rightDiv.innerHTML = '&#160;'; // ►
	topBar2.appendChild(rightDiv);
	
	// Close img
	var closeDiv = document.createElement('div');
	closeDiv.className = 'closeCalendar';
	closeDiv.onclick = closeCalendar;
	closeDiv.onmouseover = highlightDiv;
	closeDiv.onmouseout = highlightDiv;
	closeDiv.innerHTML = '&#160;';
	topBar2.appendChild(closeDiv);
}
function writeCalendarContent()
{
		calCurrentDate=new Array();
		var currentDate = new Date();
		calCurrentDate[0] = currentDate.getFullYear();
		calCurrentDate[1] = currentDate.getMonth();
		calCurrentDate[2] = currentDate.getDate();
		
	var calendarContentDivExists = true;
	if(!calendarContentDiv){
		calendarContentDiv = document.createElement('div');
		calendarDiv.appendChild(calendarContentDiv);
		calendarContentDivExists = false;
	}
	calCDate[1] = calCDate[1]/1;
	var d = new Date();

	d.setFullYear(calCDate[0]);
	d.setDate(1);
	d.setMonth(calCDate[1]);

	var dayStartOfMonth = d.getDay();
	if(dayStartOfMonth==0)dayStartOfMonth=7;
	dayStartOfMonth--;

	document.getElementById('yearSelect').innerHTML = calCDate[0];
	document.getElementById('monthSelect').innerHTML = monthArray[calCDate[1]];
	document.getElementById('hourSelect').innerHTML = calCDate[2];
	document.getElementById('minuteSelect').innerHTML = calCDate[3];

	var existingTable = calendarContentDiv.getElementsByTagName('table');
	if(existingTable.length>0){
		calendarContentDiv.removeChild(existingTable[0]);
	}

	var calTable = document.createElement('table');
	calendarContentDiv.appendChild(calTable);
	var calTBody = document.createElement('tbody');
	calTable.appendChild(calTBody);
	var row = calTBody.insertRow(-1);
	row.id = 'weekdays';
	var cell = row.insertCell(-1);
	cell.innerHTML = weekString;
	cell.className = 'weekno';

	for(var no=0;no<dayArray.length;++no){
		var cell = row.insertCell(-1);
		cell.innerHTML = dayArray[no];
		if (no > 4) cell.className = 'weekendDay';
	}

	var row = calTBody.insertRow(-1);
	var cell = row.insertCell(-1);
	cell.className = 'weekno';
	var week = getWeek(calCDate[0],calCDate[1],1);
	cell.innerHTML = week;		// Week
	for(var no=0;no<dayStartOfMonth;++no){
		var cell = row.insertCell(-1);
		cell.innerHTML = '&#160;';
		if(no == 5){ cell.className='weekendDay'; }
	}
	var colCounter = dayStartOfMonth;
	var daysInMonth = daysInMonthArray[calCDate[1]];
	if(daysInMonth==28){
		if(isLeapYear(calCDate[0]))daysInMonth=29;
	}
	for(var no=1;no<=daysInMonth;++no)
	{
		d.setDate(no-1);
		if(colCounter>0 && colCounter%7==0){
			var row = calTBody.insertRow(-1);
			var cell = row.insertCell(-1);
			var week = getWeek(calCDate[0],calCDate[1],no);
			cell.innerHTML = week;		// Week
			cell.className = 'weekno';
		}
		var cell = row.insertCell(-1);
		
		if(calCDate[0]==calCurrentDate[0] && calCDate[1] == calCurrentDate[1] && no==calCurrentDate[2])
		{
			cell.className='activeToday';
		}
		else if(calCDate[0]==calIDate[0] && calCDate[1] == calIDate[1] && no==calIDate[2])
		{
			cell.className='activeDay';
		}
		else if(colCounter>0 && (colCounter%7==5 || colCounter%7==6)){
			cell.className='weekendDay';
		}
		cell.innerHTML = no;
		cell.onclick = pickDate;
		++colCounter;
	}
	while(colCounter>0 && colCounter%7!=0){
		var cell = row.insertCell(-1);
		cell.innerHTML = '&#160;';
		if(colCounter%7==5 || colCounter%7==6){
			cell.className='weekendDay';
		}
		++colCounter;
	}

	if(!document.all){
		if(calendarContentDiv.offsetHeight)
		{
			document.getElementById('topBar').style.top = calendarContentDiv.offsetHeight + document.getElementById('timeBar').offsetHeight + document.getElementById('topBar').offsetHeight -1 + 'px';
		}
		else{
				
			document.getElementById('topBar').style.top = '';
			document.getElementById('topBar').style.bottom = '0';
			
			document.getElementById('topBar2').style.top = '';
			document.getElementById('topBar2').style.bottom = '0';
		}
	}
	if(califrame){ setTimeout('resizeIframe()',(calendarContentDivExists?350:10)); }
}
function writeTimeBar()
{
	var timeBar = document.createElement('div');
	timeBar.id = 'timeBar';
	timeBar.className = 'timeBar';
/*
	var subDiv = document.createElement('div');
	subDiv.innerHTML = 'Time:';
	timeBar.appendChild(subDiv);
*/
	// Hour selector
	var hourDiv = document.createElement('div');
	hourDiv.className = 'selectBoxTime';
	hourDiv.id = 'hourSelect';
	hourDiv.innerHTML = calCDate[2];
	hourDiv.onmouseover = highlightDiv;
	hourDiv.onmouseout = highlightDiv;
	hourDiv.onclick = showDropDown;
	timeBar.appendChild(hourDiv);

	var hourPicker = createHourDiv();
	hourPicker.style.left = '130px';
	hourPicker.id = 'hourDropDown';
	calendarDiv.appendChild(hourPicker);

	// Add Minute picker

	// Minute selector
	var minuteDiv = document.createElement('div');
	minuteDiv.className = 'selectBoxTime';
	minuteDiv.id = 'minuteSelect';
	minuteDiv.innerHTML = calCDate[3];
	minuteDiv.onmouseover = highlightDiv;
	minuteDiv.onmouseout = highlightDiv;
	minuteDiv.onclick = showDropDown;
	timeBar.appendChild(minuteDiv);

	var minutePicker = createMinuteDiv();
	minutePicker.style.left = '167px';
	minutePicker.id = 'minuteDropDown';
	calendarDiv.appendChild(minutePicker);

	return timeBar;
}
function writeBottomBar()
{
	var d = new Date();
	var bottomBar = document.createElement('div');
	bottomBar.id = 'bottomBar';
	bottomBar.className = 'todaysDate';

	var subDiv = document.createElement('div');
	subDiv.onclick = pickTodaysDate;
	subDiv.id = 'todaysDateString';
	subDiv.style.width = (calendarDiv.offsetWidth - 95) + 'px';
	var day = d.getDay();
	if(day==0)day = 7;
	day--;

	var bottomString = todayStringFormat;
	bottomString = bottomString.replace('%b',monthArrayShort[d.getMonth()]);
	bottomString = bottomString.replace('%B',monthArray[d.getMonth()]);
	bottomString = bottomString.replace('%e',d.getDate());
	bottomString = bottomString.replace('%Y',d.getFullYear());
	bottomString = bottomString.replace('%a',dayArray[day]);
	subDiv.innerHTML = todayString;
	subDiv.title = bottomString;
	bottomBar.appendChild(subDiv);
	var timeDiv = writeTimeBar();
	bottomBar.appendChild(timeDiv);
	calendarDiv.appendChild(bottomBar);
}

/* other functions */

function hideDropDowns()
{
	for(i=0;i<4;++i){
		document.getElementById(calArray[i]+'DropDown').style.display='none';
	}
}

function closeCalendar()
{
	hideDropDowns();
	calendarDiv.style.display='none';
	if(califrame)califrame.style.display='none';
	for(i=0;i<4;++i){
		if(activeSelect[i])activeSelect[i].className='';
	}
}

function resizeIframe()
{
	califrame.style.width = calendarDiv.offsetWidth + 'px';
	califrame.style.height = calendarDiv.offsetHeight + 'px' ;
}

// http://www.codeproject.com/csharp/gregorianwknum.asp
function getWeek(year,month,day){
	month += 1; //use 1-12
	var a = Math.floor((14-month)/12);
	var y = year+4800-a;
	var m = (month)+(12*a)-3;
	var jd = day + Math.floor(((153*m)+2)/5) + 
			(365*y) + Math.floor(y/4) - Math.floor(y/100) + 
			Math.floor(y/400) - 32045;	  // (gregorian calendar)
	var d4 = (jd+31741-(jd%7))%146097%36524%1461;
	var L = Math.floor(d4/1460);
	var d1 = ((d4-L)%365)+L;
	NumberOfWeek = Math.floor(d1/7) + 1;
	return NumberOfWeek;		
}

function getTopPos(obj)
{
	var returnValue = obj.offsetTop + obj.offsetHeight;
	while((obj = obj.offsetParent) != null)returnValue += obj.offsetTop;
	return returnValue;
}
function getleftPos(obj)
{
	var returnValue = obj.offsetLeft;
	while((obj = obj.offsetParent) != null)returnValue += obj.offsetLeft;
	return returnValue;
}
function positionCalendar(obj)
{
	calendarDiv.style.left = getleftPos(obj) + 'px';
	calendarDiv.style.top = getTopPos(obj) + 'px';
	if(califrame){
		califrame.style.left = calendarDiv.style.left;
		califrame.style.top  = calendarDiv.style.top;
	}
}

function setTimeProperties()
{
	if(!calendarDisplayTime){
		document.getElementById('timeBar').style.display='none';
		document.getElementById('timeBar').style.visibility='hidden';
		document.getElementById('todaysDateString').style.width = '100%';
	}else{
		document.getElementById('timeBar').style.display='block';
		document.getElementById('timeBar').style.visibility='visible';
		document.getElementById('hourDropDown').style.top = document.getElementById('minuteSelect').parentNode.offsetHeight + calendarContentDiv.offsetHeight + document.getElementById('topBar').offsetHeight + 'px';
		document.getElementById('minuteDropDown').style.top = document.getElementById('minuteSelect').parentNode.offsetHeight + calendarContentDiv.offsetHeight + document.getElementById('topBar').offsetHeight + 'px';
		document.getElementById('minuteDropDown').style.right = '50px';
		document.getElementById('hourDropDown').style.right = '50px';
		document.getElementById('todaysDateString').style.width = '115px';
	}
}

function displayCalendar(fieldid, buttonObj, displayTime)
{
	inputField = document.getElementById(fieldid);
	calendarDisplayTime = (displayTime);
	format = (!calendarDateFomat)?'%Y/%m/%d':calendarDateFomat;
	if (calendarDisplayTime) format += ' %H:%M';

	var fregex = format.replace('-','\\-');
	fregex = fregex.replace('.','\\.');
	fregex = fregex.replace('(','\\(');
	fregex = fregex.replace(')','\\)');
	fregex = fregex.replace('%Y','(\\d{4})');
	fregex = fregex.replace('%m','(\\d{2})');
	fregex = fregex.replace('%d','(\\d{2})');
	fregex = fregex.replace('%H','(\\d{2})');
	fregex = fregex.replace('%M','(\\d{2})');
	fregex = RegExp(fregex);

	var match = inputField.value.match(fregex);
	if (match){
		var yearPos = format.indexOf('%Y');
		var monthPos = format.indexOf('%m');
		var dayPos = format.indexOf('%d');
		if(yearPos<monthPos){
			calCDate[0] = (yearPos<dayPos)?match[1]:match[2];
		}else{
			calCDate[0] = (yearPos<dayPos)?match[2]:match[3];
		}
		if(monthPos<yearPos){
			calCDate[1] = (monthPos<dayPos)?match[1]:match[2];
		}else{
			calCDate[1] = (monthPos<dayPos)?match[2]:match[3];
		}
		--calCDate[1];
		if(dayPos<monthPos){
			tmpDay = (dayPos<yearPos)?match[1]:match[2];
		}else{
			tmpDay = (dayPos<yearPos)?match[2]:match[3];
		}
		calCDate[2] = (match[4])?match[4]:0;
		calCDate[3] = (match[5])?match[5]:0;
	}else{
		var d = new Date();
		calCDate[0] = d.getFullYear();
		calCDate[1] = d.getMonth();
		calCDate[2] = d.getHours();
		calCDate[3] = d.getMinutes();
		tmpDay = d.getDate();
	}
	calIDate[0] = calCDate[0];
	calIDate[1] = calCDate[1];
	calIDate[2] = tmpDay/1;

	if(!calendarDiv){
		initCalendar();
	}else{
		if(calendarDiv.style.display=='block'){
			closeCalendar();
			return false;
		}
		writeCalendarContent();
	}

	returnFormat = format;
	returnDateTo = inputField;
	positionCalendar(buttonObj);
	calendarDiv.style.visibility = 'visible';
	calendarDiv.style.display = 'block';
	if(califrame){
		califrame.style.display = '';
		califrame.style.height = '140px';
		califrame.style.width = '200px';
	}

	setTimeProperties();
	updateYearDiv();
	updateMonthDiv();
	updateMinuteDiv();
	updateHourDiv();
}

function gotoNextMonthAvailability(bedrijfid,cat,maand,jaar)
{
	resultdiv=document.getElementById("calendar_"+bedrijfid+"_"+cat);  
	showLoadingScreen('calendar_'+bedrijfid+'_'+cat);
	
	//resultdiv.innerHTML='TEST'; 
	
	/* Fetch some html depending on which tab was clicked */
	var url = 'index.php';
	var pars = '&sid={sid}&external=1&action=make_calendar&bedrijfid=' + bedrijfid + '&cat=' + cat + '&maand=' + maand + '&jaar=' + jaar;
	var myAjax = new Ajax.Updater(resultdiv, url, {method:'get',parameters:pars});
}

function gotoPreviousMonthAvailability(bedrijfid,cat,maand,jaar)
{
	resultdiv=document.getElementById("calendar_"+bedrijfid+"_"+cat);  
	showLoadingScreen('calendar_'+bedrijfid+'_'+cat);
	
	/* Fetch some html depending on which tab was clicked */
	var url = 'index.php';
	var pars = '&sid={sid}&external=1&action=make_calendar&bedrijfid=' + bedrijfid + '&cat=' + cat + '&maand=' + maand + '&jaar=' + jaar;
	var myAjax = new Ajax.Updater(resultdiv, url, {method:'get',parameters:pars});
}