/**
 * ARENA AIRSOFT JAVASCRIPT
 * 
 * @author 		Mark Thompson
 * @version		[1.01]
 * 
 * @changeLog	[
 * 
 * Version		Date		Comment
 * 
 * 1.01			03/01/11	Added the booking button handlers
 * 1.00			22/11/10	First Build
 * ]
 */


$(function(){
	// Run Loader
	loader();
	// Run Button Enabler
	bookingButtons();
});

/**
 * Enables PNG's in IE6
 * @return
 */
function loader(){
	$('body').supersleight({shim: '/images/tiles/blank.gif'});
}

/**
 * Enable the booking buttons
 * @return
 */
function bookingButtons(){
	// Unbind the click so we don't get unwanted submissions
	$('.bookingButton').unbind('click');
	// When we click one of the booking buttons
	$('.bookingButton').click(function(){
		// First get the id of it
		var eventId = $(this).attr('id');
		
		$.get('/pages/ajax/bookingform.php',{"eventId":eventId}, function(data){
			  var $dialog = $('<div></div>').html(data).dialog({title:'Please fill in your booking details below', resizable:false, modal:true, width:'600px'});
			  $dialog.dialog();
			  initialiseForm();
			  return false;
		});
		bookingButtons();
	});
}

function initialiseForm(){
	$('#booking').submit(function(){
		// Assume ready
		var ready = true;
		// Check all required inputs
		$('input.requiredText').each(function(index){
			if($(this).val() == ""){
				// Push a message
				alert('You have not filled out all the required information to make your booking.Please fill in the form correctly to continue');
				// Change ready to false
				ready = false;
				// Reutrn false to break loop
				return false;
			}else{
				// Do nothing
			}
		});
		
		// if ready still true
		if(ready){
			// Now check the emails
			if($('input[name=email]').val() != $('input[name=email2]').val()){
				// Push an alert
				alert('The email addresses you have entered do not match');
				// Change ready to false
				ready = false;
				// Reutrn false to break loop
				return false;
			}
		}
		// If still ready
		if(ready){
			// Check it's not blank
			if($('input[name=email]').val()==""){
				// Push an alert
				alert('Please enter an email address');
				// Change ready to false
				ready = false;
				// Reutrn false to break loop
				return false;
			}
		}
		// If still ready
		if(ready){
			// Now check for valid email
			if(checkValidEmail($('input[name=email]').val())== false){
				// Change ready to false
				ready = false;
				// Reutrn false to break loop
				return false;
			}
		}
		// If we're ready then GO!
		if(ready){
			// Disable double booking bug
			$('input[type=submit]').attr('disabled','disabled');
			return true;
		}else{
			return false;
		}
	});
}

//------------------------
//Function to check that email is valid address
//------------------------

function checkValidEmail(str) 
{
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	
	if (str.indexOf(at)==-1)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

	if (str.indexOf(at,(lat+1))!=-1)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

	if (str.indexOf(dot,(lat+2))==-1)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}
		
	if (str.indexOf(" ")!=-1)
	{
		alert("Please check that you have correctly entered your email address");
		return false;
	}

		return true;				
	}

