var timeSelected = '';

// Original date, so the date can be reset when switching between alternate dates.
var originalDay,
		originalMonth,
		originalYear;


updateDayOfWeek(true);

/**
 *	Add triggers to our date element. Note that this is 
 *	the more elegant way than adding them as functionality
 *	to the date select DOM elements, and that we couldn't do
 *	that anyway since it already has different default triggers.
 *
 *	Note the onKeyUp: this is because FireFox is an anoyingly 
 *	crappy browser that won't invoke the change event when you 
 *	change a selectbox's value using keystrokes.
**/
$('currentDay').onchange 		= updateDayOfWeek;
$('currentMonth').onchange 	= updateDayOfWeek;
$('currentYear').onchange 	= updateDayOfWeek;
$('currentDay').onkeyup 		= updateDayOfWeek;
$('currentMonth').onkeyup 	= updateDayOfWeek;
$('currentYear').onkeyup 		= updateDayOfWeek;


function updateDayOfWeek (firstUpdate) {
	if (firstUpdate !== true)
		hideTimes();

	var actualDate	=	new Date;
	var	dayName			=	'';

	actualDate.setDate($('currentDay').value);
	actualDate.setMonth(($('currentMonth').value - 1));
	actualDate.setDate($('currentDay').value);
	actualDate.setFullYear($('currentYear').value);

	switch(actualDate.getDay()) {
		case 0:
			dayName = 'zondag';
			break;
		case 1:
			dayName = 'maandag';
			break;
		case 2:
			dayName = 'dinsdag';
			break;
		case 3:
			dayName = 'woensdag';
			break;
		case 4:
			dayName = 'donderdag';
			break;
		case 5:
			dayName	=	'vrijdag';
			break;
		case 6:
			dayName	=	'zaterdag';
			break;
	}
	$('dayName').innerHTML = dayName;
}


/**
 *	When a user selects a time, the date and employee for that time need
 *	to reflect that change. This function calls other methods that do just that.
**/
function selectAlternateDateEmployee (employeeId) {
	selectAlternateDate(employeeId);
	selectAlternateEmployee(employeeId);
}


/**
 *	Select an alternate employee in the dropdown box. This updates not
 *	also the picture for extra confirmation.
 *
 *	Note that if our user did not select an employee in his previous 
 *	request, it is possible he does not want to know which employee he'll 
 *	get - for some mysterious reason beyond me but evident to the client
 *	apparently. Anyhow, in that case (employeeId is not set) we only
 *	set the hidden employee ID field.
 *
 *	@param	employeeId		Int		The ID of the employee to be selected.
 *	@return	Void.
**/
function selectAlternateEmployee (employeeId) {
	var employeeIdInput = $('employeeId') != null ? $('employeeId') : null;
	
	if (employeeIdInput == null || !employeeIdInput.value)
		return selectHiddenAlternateEmployee(employeeId);

	if ($('lockEmployeeLabel') != null)
		$('lockEmployeeLabel').style.display 	= 'block';
	$('employeeId').value									=	employeeId;
	$('hiddenEmployeeId').value						=	employeeId;
	$('employeePreference').value					=	true;
	updateEmployeePicture();
}


/**
 *	When the user has "no preference" selected for employees and 
 *	selects a time, that time is automatically linked to an employee
 *	but we cannot let the user know this. Thus, this sets the value
 *	for employee ID to a hidden field.
 *
 *	Also make sure the 'lock' is disabled.
**/
function selectHiddenAlternateEmployee (employeeId) {
	if ($('lockEmployeeLabel') != null)	 {
		$('lockEmployeeLabel').style.display 	= 'none';
		$('lockEmployeeInput').checked 				= false;
	}
	$('hiddenEmployeeId').value						=	employeeId;
	$('employeePreference').value					=	false;
}


/**
 *	When we want an alternate date (the selected employee is not 
 *	available at the preferred date) we must select a time on another
 *	day. This function ensures that the date select fields are updated
 *	automatically to reflect this change. 
 *
 *	If we select a different employee, make sure the 'perse' button is 
 *	no longer checked.
 *
 *	Note that we need to cast each value to an int to eliminate problems
 *	with '08' not working. A simple math operation seems to suffice for this.
**/
function selectAlternateDate (employeeId) {
	if (employeeId != $('postEmployee').value && 
			!$('treatmentNotAllowed').value)
		return false;
	
	if (!originalDay)
		setOriginalDateValues();
	
	var		newDay		=	$('alternateDay').value 	* 1;
	var		newMonth	=	$('alternateMonth').value * 1;
	var		newYear		=	$('alternateYear').value 	* 1;
	
	
	if (employeeId != $('postEmployee').value && $('postEmployee').value > 0) {
		$('currentDay').value			=	originalDay;
		$('currentMonth').value		=	originalMonth;
		$('currentYear').value		=	originalYear;
		if ($('lockEmployeeInput') != null)
			$('lockEmployeeInput').checked = false;
	}
	else {
		$('currentDay').value			=	newDay;
		$('currentMonth').value		=	newMonth;
		$('currentYear').value		=	newYear;
	}
}


/**
 *	Store the original date values so the date can
 *	be reset seperately if required.
**/
function setOriginalDateValues () {
	originalDay			=	$('currentDay').value;
	originalMonth		=	$('currentMonth').value;
	originalYear		=	$('currentYear').value;
}