var $j = jQuery.noConflict();

var $attendeeList = new Array();


var $bookingTotal = 0;
var $isValid = false;

$j(function() {

	$j("#cc_info").hide();
	$j("#cc_info_toggle").click(function() {$j("#cc_info").toggle();});

	$j("#eventful_tabs").tabs({ disabled: [0, 1, 2] });
	// show these elements that the js form requires for process flow 
	$j("#eventful_tab_headers").show();
	$j("#continue_to_billing").show();
	$j("#add_attendee").show();
	$j("#continue_to_payment").show();
	$j("#process_payment").show();
	$j("#process_payment_submit").hide();

	$j("#eventful_form").validate();

});

	
function validateTab($tab) {

	switch ($tab) {
		case 'attendee':
			// if there is anything in the attendeeList, and the 'required' fields are blank,
			// then it prob means that the booker is done, and ready to move on...
			if (($attendeeList.length) && ($j("#attendee_name_first").val()==''))  {
				removeAttendeeRules();
			} else {
				addAttendeeRules();
			}
			break;
		
		case 'billing':
				removeAttendeeRules();
				addBillingRules();
			break;
		
		case 'payment':
				removeBillingRules();
				addPaymentRules();
			break;
	}

	$isValid = $j("#eventful_form").valid();
	return $isValid;
}

function addAttendeeRules() {
	// see here for more http://docs.jquery.com/Plugins/Validation#List_of_built-in_Validation_methods
	$j("#attendee_name_first").rules("add", "required");
	$j("#attendee_name_last").rules( "add", "required");
	$j("#attendee_email").rules("add", "required");
	$j("#attendee_email").rules("add", "email");	
	$j("#attendee_phone").rules("add", "required");
	$j("#attendee_position").rules("add", "required");
	$j("#attendee_address_one").rules("add", "required");
	$j("#attendee_address_city").rules("add", "required");
	$j("#attendee_address_postcode").rules("add", "required");
	$j("#attendee_address_postcode").rules("add", "digits");	
}
function removeAttendeeRules() {
	$j("#attendee_name_first").rules("remove", "required");
	$j("#attendee_name_last").rules("remove", "required");
	$j("#attendee_email").rules("remove", "required");
	$j("#attendee_email").rules("remove", "email");	
	$j("#attendee_phone").rules("remove", "required");
	$j("#attendee_position").rules("remove", "required");
	$j("#attendee_address_one").rules("remove", "required");
	$j("#attendee_address_city").rules("remove", "required");
	$j("#attendee_address_postcode").rules("remove", "required");
	$j("#attendee_address_postcode").rules("remove", "digits");
	
}
function addBillingRules() {
	$j("#billing_name_first").rules("add", "required");
	$j("#billing_name_last").rules("add", "required");
	$j("#billing_email").rules("add", "required");
	$j("#billing_email").rules("add", "email");	
	$j("#billing_phone").rules("add", "required");
	$j("#billing_address_one").rules("add", "required");
	$j("#billing_address_city").rules("add", "required");
	$j("#billing_address_postcode").rules("add", "required");
	$j("#billing_address_postcode").rules("add", "digits");	
}
function removeBillingRules() {
	$j("#billing_name_first").rules("remove", "required");
	$j("#billing_name_last").rules("remove", "required");
	$j("#billing_email").rules("remove", "required");
	$j("#billing_email").rules("remove", "email");	
	$j("#billing_phone").rules("remove", "required");
	$j("#billing_address_one").rules("remove", "required");
	$j("#billing_address_city").rules("remove", "required");
	$j("#billing_address_postcode").rules("remove", "required");
	$j("#billing_address_postcode").rules("remove", "digits");	
}
function addPaymentRules() {
	$j("#payment_amount").rules("add", "required");
}

function bookingContinue() {
	if ( validateTab('attendee') ) {
		addAttendee();
		$j('#eventful_tabs').tabs( 'enable', 1 );
		$j('#eventful_tabs').tabs( 'select', 1 );
		$j('#eventful_tabs').tabs( 'disable', 0 );
		buildAttendeeTable(false);
		buildAttendeeSelect(); // this is to autopopulate booking info with an attendees details
		return true;
	} else {
		return false;
	}
}

function billingContinue() {
	if ( validateTab('billing') ) {
		buildBillingTable();
		$j("#payment_amount").val($bookingTotal);
		$j('#eventful_tabs').tabs( 'enable', 2 );
		$j('#eventful_tabs').tabs( 'select', 2 );
		$j('#eventful_tabs').tabs( 'disable', 1 );
		return true;
	} else {
		return false;
	}
}

function addAttendee(){
	// only do this if there is data.. 
	
	if ( $j("#attendee_name_first").val() == "" ) { return true;}

	var $attendee = new Array();
	$attendee["id"] = $attendeeList.length;
	$attendee = getAttendeeDetail($attendee, "name_first");
	$attendee = getAttendeeDetail($attendee, "name_last");
	$attendee = getAttendeeDetail($attendee, "company");
	$attendee = getAttendeeDetail($attendee, "position");
	$attendee = getAttendeeDetail($attendee, "email");
	$attendee = getAttendeeDetail($attendee, "phone");
	$attendee = getAttendeeDetail($attendee, "mobile");
	$attendee = getAttendeeDetail($attendee, "address_one");
	$attendee = getAttendeeDetail($attendee, "address_two");
	$attendee = getAttendeeDetail($attendee, "address_city");
	$attendee = getAttendeeDetail($attendee, "address_state");
	$attendee = getAttendeeDetail($attendee, "address_postcode");
	$attendee = getAttendeeDetail($attendee, "price");
	$attendee = getAttendeeDetail($attendee, "diet");

	$attendeeList[$attendeeList.length] = $attendee;

	buildAttendeeTable(true);

	return true;
} 





function buildAttendeeSelect() {
	var $options ='';
	for($i=0; $i<$attendeeList.length; $i++) {
		$options += singleAttendeeOptionGenerator($attendeeList[$i]) ;
	}
	$j("#attendee_select").html($options);
	return true;
}


function getAttendeeDetail($attendeeDetail, $field)
{
	
	if ($field=='price') {
		// radio buttons need special treatment .. unique ids
		$attendeeDetail[$field] = $j("input:radio[name=attendee_"+$field+"]:checked").val();
	} else {
		// otherwise use id
		$attendeeDetail[$field] = $j("#attendee_"+$field).val();
		$j("#attendee_"+$field).val("");
	}
	return $attendeeDetail;
}

function buildAttendeeTable($show_tools)
{	
	$bookingTotal = 0;
	var $orderSummary = "";
	
	$orderSummary = '<h4>Order Details</h4><h5>Attendees</h5>';
	$orderSummary += '<table class="summary"><tr>';
	$orderSummary += '<th>Name</th>';
	$orderSummary += '<th>Position</th>';
	$orderSummary += '<th>Email</th>';
	$orderSummary += '<th>Price</th>';
	if ($show_tools) $orderSummary += '<th></th>';
	$orderSummary += '</tr>';
	
	for($x=0; $x<$attendeeList.length; $x++) {
//	for($x in $attendeeList) {	
		$orderSummary += '<tr><td class="name">';
		
		if ($show_tools) $orderSummary += '<a onclick="editAttendee('+$x+'); return false;">';
		$orderSummary += $attendeeList[$x]["name_first"]+' '+ $attendeeList[$x]["name_last"];
		if ($show_tools) $orderSummary += '</a>';
		
		$orderSummary += '</td><td>'+$attendeeList[$x]["position"] + '</td>';
		$orderSummary += '<td>'+$attendeeList[$x]["email"] + '</td>';
		$orderSummary += '<td>$'+$attendeeList[$x]["price"] + '</td>';
		if ($show_tools) $orderSummary += '<td><a onclick="deleteAttendee('+$x+'); return false;">X</a></td>';
		$orderSummary +='</tr>';
		$bookingTotal = $bookingTotal + ($attendeeList[$x]["price"] * 1);
	}
	$orderSummary += '</table>';
	
	$html = ($attendeeList.length==0) ? '': $orderSummary;
	$j("#attendee_summary").html($html);
}








function buildBillingTable()
{	
	$billingInfo = Array();

	$b_fields=Array(
		'name_first',
		'name_last',
		'company',
		'email',
		'phone',
		'address_one',
		'address_two',
		'address_city',
		'address_state',
		'address_postcode'
		);
	for($x=0; $x<$b_fields.length; $x++) {
//	for($x in $b_fields) {	
		$billingInfo[$b_fields[$x]] = $j("#billing_"+$b_fields[$x]).val();
	}
	
	var $orderSummary = "";
	$orderSummary = '<h5>Billing Contact</h5>';
	$orderSummary += '<table class="summary"><tr>';
	$orderSummary += '<td>' + $billingInfo["name_first"] + '</td>';
	$orderSummary += '<td>' + $billingInfo["name_last"] + '</td>';
	if ($billingInfo["email"] != undefined) $orderSummary += '<td>' + $billingInfo["email"] + '</td>';
	if ($billingInfo["phone"] != undefined) $orderSummary += '<td>' + $billingInfo["phone"] + '</td>';
	$orderSummary += '</tr></table>';
	
	$j("#billing_summary").html($orderSummary);

	return true;
}


function processPayment($type) {

	// if its a free event, skip billing details and come straight here
	if ( validateTab('attendee') ) {
		removeAttendeeRules();
		addAttendee();
		buildAttendeeTable(false);
		populateBilling(0);
		$j('#attendee_details').fadeTo("slow", 0.33);
	}

	// depending on the type, select the appropriate hidden radio button
	$j('input[name="payment_type"]')[$type].checked = true;

	// make the payment form uneditable ? or at least fade it a bit. 
	$j('#payment_details').fadeTo("slow", 0.33);
	attendeeHiddenFieldGenerator();
	$j('#eventful_form').submit();
}

function attendeeHiddenFieldGenerator()
{
	var $attendeeHiddenFields = "";
	var $hiddenfields = "";
	
	
	for($y=0; $y<$attendeeList.length; $y++) {
//	for($y in $attendeeList) {
		$attendeeHiddenFields += singleAttendeeHiddenFieldGenerator($attendeeList[$y], "name_first");
		$attendeeHiddenFields += singleAttendeeHiddenFieldGenerator($attendeeList[$y], "name_last");
		$attendeeHiddenFields += singleAttendeeHiddenFieldGenerator($attendeeList[$y], "position");
		$attendeeHiddenFields += singleAttendeeHiddenFieldGenerator($attendeeList[$y], "company");
		$attendeeHiddenFields += singleAttendeeHiddenFieldGenerator($attendeeList[$y], "email");
		$attendeeHiddenFields += singleAttendeeHiddenFieldGenerator($attendeeList[$y], "phone");
		$attendeeHiddenFields += singleAttendeeHiddenFieldGenerator($attendeeList[$y], "mobile");
		$attendeeHiddenFields += singleAttendeeHiddenFieldGenerator($attendeeList[$y], "address_one");
		$attendeeHiddenFields += singleAttendeeHiddenFieldGenerator($attendeeList[$y], "address_two");
		$attendeeHiddenFields += singleAttendeeHiddenFieldGenerator($attendeeList[$y], "address_city");
		$attendeeHiddenFields += singleAttendeeHiddenFieldGenerator($attendeeList[$y], "address_state");
		$attendeeHiddenFields += singleAttendeeHiddenFieldGenerator($attendeeList[$y], "address_postcode");
		$attendeeHiddenFields += singleAttendeeHiddenFieldGenerator($attendeeList[$y], "price");
		$attendeeHiddenFields += singleAttendeeHiddenFieldGenerator($attendeeList[$y], "diet");
	}
	$j("#attendee_hidden_fields").html($attendeeHiddenFields);
	return true;
}

function singleAttendeeHiddenFieldGenerator($attendee, $field)
{
	return '<input type="hidden" class="hide" name="attendee['+$attendee["id"]+']['+$field+']" value="'+$attendee[$field]+'" />';
}


function singleAttendeeOptionGenerator($attendee)
{
	return '<option value="'+$attendee["id"]+'">'+$attendee["name_first"]+' '+$attendee["name_last"]+'</option>';
}

function editAttendee($id) {
	$fields=Array(
		'name_first',
		'name_last',
		'position',
		'company',
		'email',
		'phone',
		'mobile',
		'address_one',
		'address_two',
		'address_city',
		'address_state',
		'address_postcode',
		'price',
		'diet'
		);
	// populate the attendee form fields
	for($x=0; $x<$fields.length; $x++) {
//	for($x in $fields) {	
		$j("#attendee_"+$fields[$x]).val( $attendeeList[$id][$fields[$x]] );
	}
	// cleanup
	deleteAttendee($id);
	return true;
}

function deleteAttendee($id) {
	// remove from the attendeeList
	$attendeeList.splice($id,1);
	// rebuild the attendee table
	buildAttendeeTable(true);
	return true;
}


function populateBilling($id) {
	// which person has been selected? 
	if ($id=='select') $id = $j("#attendee_select").val() ;
	
	// fields don't quite match up !
	$fields=Array(
		'name_first',
		'name_last',
		'company',
		'email',
		'phone',
		'mobile',
		'address_one',
		'address_two',
		'address_city',
		'address_state',
		'address_postcode'
		);
	// populate the billing details form fields
	for($x=0; $x<$fields.length; $x++) {
//	for($x in $fields) {	
		$j("#billing_"+$fields[$x]).val( $attendeeList[$id][$fields[$x]] );
	}
	return true;
}