
/**
 * ******************** UNCOMPRESSED VERSION ********************
 *
 * THIS IS AN UNCOMPRESSED VERSION OF THE theatreCalendar.js FILE
 * THIS FILE SHOULD BE ONLY USED FOR AMMENDMENTS & SHOULD NOT
 * BE USED AS THE VERSION SERVED TO USER AGENTS
 *
 * ONCE ALL CHANGES ARE COMPLETED THIS FILE MUST BE COMPRESSED 
 * USING THE javascriptCompress.cfm UTILITY - TO SAVE AS THE 
 * SERVED VERSION - theatreCalendar.js
 *
 * ******************** UNCOMPRESSED VERSION ********************
 */
LibManager.require( 'dom' );
LibManager.require( 'Scheduler' );

/* use scheduler to call init function, to ensure that the calendar html is rendered before the js executes.
   this stops everyone's favourite browser from throwing the wonderfully informative 
   'object expected' error when the page is loaded over a slow connection */
LibManager.isLoaded(
	['Scheduler', 'getElementsByClassName', 'xGetElementsByTagName'], 
	function() {
		Scheduler.schedule('showPicker', 'initCalendar();');
	}
);

function initCalendar() {
	// 1. remove all the form buttons
	var els = getElementsByClassName('fullDateButton', $('calendarWrap'));
	for(var i=0; i < els.length; i++) {
		els[i].style.display = 'none';
	}
	
	// 2. attach click events to each td with availabilty
	var els = getElementsByClassName('highlight', $('calendarWrap'));
	for(var i=0; i < els.length; i++) {
		Event.observe(els[i], 'click', setCalDate);
	}
	
	// 3. attach submit event to the form
	Event.observe($('showDateSelector'), 'submit', checkTheForm);
	
	activeEl = '';
	activeElOldClass = '';
}

/**
 * checkTheForm()
 *
 * checks to make sure a user has selected a show date before submitting the form
 */
function checkTheForm(e) {
	if(!e) var e = window.event;
	
	var theForm = $('showDateSelector');
	var isChecked = false;
	// make sure they have selected something
	//if only one radiobutton
	if(!theForm.fullDate.length) {
		if(theForm.fullDate.checked == true) {
			isChecked = true;
			// turn the element display back on just before submitting otherwise NS 6.2 PC won't submit it
			theForm.fullDate.style.display = 'block'; 
		}
	} else {
		for(i=0; i < theForm.fullDate.length; i++) {
			if(theForm.fullDate[i].checked == true) { 
				// turn the element display back on just before submitting otherwise NS 6.2 PC won't submit it
				theForm.fullDate[i].style.display = 'block'; 
				isChecked = true; 
				break; 
			}
		}
	}
	
	if(!isChecked) {
		alert('Please select a date');
		// stop the submit from firing
		Event.stop( e );	
	} 
}
	

/**
 * setCalDate(e)
 *
 * attached as an onclick to the table cells which have availability, selects
 * the radio button, which is now hidden, within that table cell when a user clicks
 * on the cell.  It also applies the class of 'selected' to the cell
 */
function setCalDate(e) {
	if(activeEl != '') activeEl.className = activeElOldClass;
	
	// pickup the source of the event 
	var targ;
	if(!e) var e = window.event;
	if(e.target) targ = e.target;
	else if(e.srcElement) targ = e.srcElement;
	if(targ.nodeType == 3) targ = targ.parentNode; // defeat Safari bug
	
	// backup this cells details for undoing when another cell is selected 
	activeEl = targ;
	activeElOldClass = targ.className;
	
	// set this cell to be the one selected
	targ.className = targ.className + ' selected';
	targ.className = targ.className + ' bgcolour2';
	
	// select the radio button
	children = targ.childNodes;
	children[0].checked = true;
}