// Highlight day of interest.

var globalBusy = false;
var globalCurrentId = '';

function show(id) {
	var element
	element = document.getElementById(id);
	element.style.display = 'block';
	return false;
}

function hide(id) {
	var element
	element = document.getElementById(id);
	element.style.display = 'none';
	return false;
}

function showBusy() {
	var element
	globalBusy = true;
	element = document.getElementById(globalCurrentId + 'status');
	element.style.display = 'block';
	element = document.getElementById(globalCurrentId + 'q1');
	element.disabled = true;
}

function hideBusy() {
	var element
	element = document.getElementById(globalCurrentId  + 'status');
	element.style.display = 'none';
	element = document.getElementById(globalCurrentId + 'q1');
	element.disabled = false;
	globalBusy = false;
	globalCurrentId = '';
	Form.enable('results');
}

function setQuantity(id,activityCode,startDate) {
	var element;
		
	if (globalBusy) {
		element = document.getElementById(id + 'q1');	
		element.value = 0;
	} else {
		globalCurrentId = id;
	}
	
	element = document.getElementById(id + 'q2');
	element.style.display = 'none';
	element = document.getElementById(id + 'q3');
	element.style.display = 'none';

	element = document.getElementById(id + 'q1');	
		
	if (element.value != 0) {
		showBusy();
		Form.disable('results');
	
		var pars = ('activityCode=' + $F(id + 'q1ActivityCode') + 
			'&activityLocationCode=' + escape($F(id + 'q1ActivityLocationCode')) +
			'&startDate=' + escape($F(id + 'q1StartDate')) + '&units=' + element.value + '&id=' + id);
		
		if (DEBUG)
			var temp = prompt("AJAX URL",(url + '?' + pars));
		
		var myAjax = new Ajax.Request(
			url, 
			{
				method: 'get', 
				parameters: pars, 
				onComplete: showTimeSlots,
				onFailure: reportError
			});
		
		//var myAjax = new Ajax.Updater( {success: '111q2'}, url, { method: 'get', parameters: pars, onFailure: reportError });

		globalCurrentId = id;
		
	}
}


function reportError(request)
{
	alert('Sorry. There was an error.');
	hideBusy();
}

function showTimeSlots(originalRequest) {
	var element
	//alert(originalRequest.responseText);
	element = document.getElementById(globalCurrentId + 'q2');
	element.style.display = 'block';
	element.innerHTML = originalRequest.responseText;
	hideBusy();
}

function checkUnits(id,position) {

	var i = 1;
	var total = 0;
	var productId;
	var message = 'The number of tickets you have selected is more then you searched for.  Please adjust your ticket selections or change the number of tickets you need.';
	
	productId = id + '_p'+i+'_qty';
	while ($(productId) != null) {
		total += parseInt($F(productId));
		i++;
		productId = id + '_p'+i+'_qty';
	}
	if ($(id+'_units') != null) {
		//alert(total + ' ' + $F(id+'_units'));
		if (total > $F(id+'_units')) {
			alert(message);
			document.getElementById(id + '_p'+position+'_qty').value = 0;	
		}
	} else {
		alert('Error.  Please contact customer support');
	}
}
