

var calendars = new Array();
var previousEvent = null;
var basePath = 'book/';
var currentPopUp = null;
var currentMonth = null;

// Events
function onBodyLoad()
{
	for(i = 0; i < calendars.length; i++)
	{
		reload(calendars[i]);
	}
}

// Reload
function reload(eventId)
{
	previousEvent = eventId;
	
	showWorker(eventId);
	
	//$(eventId + '-calendar').innerHTML = '';

	if(currentMonth != null)
	{
		var month = currentMonth.split('|');
		
		if(month[0] == eventId)
			month = month[1];
		else
			month = '';
	}
	else
		month = '';
		
	new Ajax.Updater(eventId + '-calendar', basePath + 'getdates.php?eventId=' + eventId + '&month=' + month, {onSuccess: reloadComplete});
}

function reloadComplete(result)
{
	var matches = result.responseText.match(/id="eventId"\>(.*)\</);
	
	hideWorker(matches[1]);
}

// Worker
function showWorker(eventId)
{
	$(eventId + '-worker').style.visibility = 'visible';
}

function hideWorker(eventId)
{
	$(eventId + '-worker').style.visibility = 'hidden';
}

// Month Changing
function showMonth(eventId)
{
	previousEvent = eventId;
	
	showWorker(eventId);
	
	var monthId = $(eventId + '-month').value;

	currentMonth = eventId + '|' + monthId;
	
	new Ajax.Updater(eventId + '-calendar', basePath + 'getdates.php?eventId=' + eventId + '&month=' + monthId, {onSuccess: reloadComplete});
}

// Booking

var popupHtml = '<div id="close"><a href="javascript:reset();"><img src="book/images/popup_close.gif" width="8" height="8" title="Close" /></a></div>Booking<div class="date">POPUP_DATE</div><form action="#" method="post" onsubmit="return doBook(\'POPUP_EVENT\', \'POPUP_ID\')" >Seats: <input type="hidden" name="date" id="POPUP_ID-date" value="POPUP_ID" /><input type="text" name="seats" id="POPUP_ID-seats" value="1" maxlength="2" class="input"/> <input type="submit" name="submit" value="OK" /></form>';

function reset()
{
	if(currentPopUp != null)
	{
		currentPopUp.className = 'hidden';
	}
}

function doClick(eventId, dateId)
{
	var obj = $(dateId);
	var popup = $(eventId + '-' + dateId + '-popup');
	var form = popupHtml;
	
	reset();
		
	if(popup.className == 'hidden')
	{
		popup.className = 'popup';
		
		var date = dateId.split('-');
		
		var time = ((date[3] > 12) ? date[3]-12 : date[3]) + ':' + date[4] + ((date[3] > 12) ? 'pm' : 'am');
		
		date = date[2] + '/' + date[1] + '/' + date[0] + ' ' + time;
		
		form = form.replace(/POPUP_DATE/i, date);
		form = form.replace(/POPUP_ID/g, dateId);
		form = form.replace(/POPUP_EVENT/g, eventId);
		
		popup.innerHTML = form;
		currentPopUp = popup;
		
		if(Prototype.Browser.WebKit || navigator.userAgent.indexOf('Chrome') > -1 || navigator.userAgent.indexOf('MSIE 8.0') > -1)
		{
			var offsets = popup.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.cumulativeScrollOffset();
			
			popup.style.marginLeft = '-' + offsets.left + 'px';
		}
		
		$(dateId + '-seats').focus();
		//$(dateId + '-seats').select();
	}
	else
	{
		popup.className = 'hidden';
	}
}

function showCurrentBookings(eventId, data)
{
	var response = data.split('|');
	
	var seats = response[0];
	var cost = response[1];
	
	if(seats == '0' || cost == '0')
	{
		$(eventId + '-order-summary').innerHTML = '';
	}
	else
	{
		$(eventId + '-order-summary').innerHTML = 'Your booking: ' + seats + ' ' + ((seats > 1) ? 'seats' : 'seat') + ', NZD $' + cost + ' <input type="submit" name="submit" value="Cancel" onClick="javascript:cancelBookings(\'' + eventId + '\');" /> <input type="submit" name="purchase" value="Purchase" onClick="javascript:purchase();"/>';
	}
}

function bookingComplete(result)
{	
	showCurrentBookings(previousEvent, result.responseText);
	
	reload(previousEvent);
}

function bookingFailure(result)
{
	alert('There was a problem connecting to our booking server. Please try again.');
	
	hideWorker(lastId);	
}

function doBook(eventId, id)
{
	var seats = parseInt($(id + '-seats').value);
	var date = id;
	
	var availableSeats = parseInt($(eventId + '-' + id + '-seatcount').innerHTML);
	
	var minPax = parseInt($(eventId + '-min-pax').innerHTML);
	
	previousEvent = eventId;
	
	if(minPax > seats)
	{
		alert('The minimum booking for this activity is ' + minPax + ' people');
	}
	else if(seats > availableSeats)
	{
		alert('There are only ' + availableSeats + ' seat' + ((availableSeats > 1) ? 's' : '') + ' available in this time slot.');
	}
	else if(seats < 1)
	{
		alert('You must book at least 1 seat.');
	}
	else
	{
		new Ajax.Request(basePath + 'dobook.php', {onComplete: bookingComplete, method: 'post', postBody: 'date=' + date + '&seats=' + seats + '&event=' + eventId});
		
		reset();
	}
	
	return false;
}

function cancelBookings(eventId)
{
	showWorker(eventId);
	previousEvent = eventId;
	
	new Ajax.Request(basePath + 'dobook.php?cancel=true&event=' + eventId, {onSuccess: cancelBookingComplete});
	showCurrentBookings(eventId, '0|0');
}

function cancelBookingComplete(result)
{
	reload(previousEvent);
}

// Purchase
function purchase()
{
	document.location.href = basePath + 'confirm.php?return=' + escape(document.location.href);
}
