// Global variables

// Delegate package pricing variables ("euros","gbp")
var professional_fees 	= new Array(320.00,270.00);
var parent_fees 		= new Array(160.00,135.00);
// single room rates per night
var accommodation_fees_single 	= new Array(96.00,86.00);
var accommodation_taxes_single	= new Array(12.96,11.61);
// double room rates per night
var accommodation_fees_double 	= new Array(116.00,100.00);
var accommodation_taxes_double	= new Array(15.66,13.50);

var currency_choice;
var currency_code;
var price;


/*function codeCheck(){
	var promo_code 	= $("#promo_code").val().toUpperCase().replace(/^\s+|\s+$/g, '');
	
	if (promo_code != ''){
		
		// display 'Checking...' text, disable button, hide all other related text blocks
		$("#check_button").attr("disabled", true);
		$("#checking_text").show();
		$("#fee_paragraph").hide();
		$("#error_paragraph").hide();
		$("#unchecked_paragraph").hide();
		
		// reset 'amount' reduction figure
		$("#amount").data('reduction', '');
		
		var dummy 		= "dummy=" + new Date().getTime();
		var url 		= "../scripts/codecheck_ver2.aspx?" + dummy;		
				
		$.post(url, { promo_code: promo_code }, function(xml){
			
			$(xml).find('results').each(function(){
				var discount_returned = $(this).find('discount').text();

				// if a discount is offered then display info in 'fee_holder' span element
				$("#fee_holder").empty();
				
				// possible returned values
				// no discount applied: 	0.00, NA
				// discount applied:		1.00 or values between 0.01 and 0.99
				
				if ((discount_returned == '0.00') || (discount_returned == 'NA')){
					$("#error_paragraph").show();
				}else{
					
					var delegate_fee 		= $("#amount").data('delegate');
					
					var discount_multiple 	= (1 - discount_returned)*1;
					var reduced_rate 		= formatAsMoney((delegate_fee * discount_multiple)*1);
					var fee_text 			= currency_code + reduced_rate;
						
					// record amount's reduction figure
					$("#amount").data('reduction', reduced_rate);
					
					$("#fee_holder").append(fee_text);
					$("#fee_paragraph").show();
				}
				
				// enable button, hide checking text, 
				$("#check_button").attr("disabled", false);
				$("#checking_text").hide();

			});
		});		
	}
}*/

function packageCheckSimple(){
	// check that at least one of the checkboxes 'professional_package' and 'parent_package' has been ticked
	var choices = 0;
	if (($("#professional_package").attr('checked') == false) && ($("#parent_package").attr('checked') == false)){
		$("#delegate_package_error").show();
		return false;
	}else{
		$("#delegate_package_error").hide();
		return true;
	}
}
function currencyCheck(){
	// currency and price index settings
	currency_choice = $("#currency").val();
	if (currency_choice == "EUR"){
		priceArrayIndex = 0;
		currency_code = "\u20AC";
	}else if (currency_choice == "GBP"){
		priceArrayIndex = 1;
		currency_code = "\u00A3";
	}
}
function packageCheck(element){

	// reset 'amount' reduction figure
	$("#amount").data('reduction', '');
	var price = 0.00;
	
	packageCheckSimple();
	
	currencyCheck();
	if (currency_choice == "") return false;
	
	var choices = 0;
	if ($("#professional_package").attr('checked') == true){
		choices++;
		price = (price*1) + professional_fees[priceArrayIndex];
		// show 'professional_row1' and 'parent_row2' rows if not already visible
		$("#professional_row1").show();
		$("#parent_row1").show();		
	}else{
		// hide 'professional_row1' and 'parent_row2' rows if not already hidden, and reset dropdown
		$("#professional_row1").hide();
		$("#parent_row1").hide();		
		$("#workshop_thurs_fri").val('');
	}
	if ($("#parent_package").attr('checked') == true){
		choices++;
		price = (price*1) + parent_fees[priceArrayIndex];
		// show 'parent_row1' if not already visible
		$("#parent_row2").show();
	}else{
		// hide 'parent_row1' if not already hidden, and reset dropdown
		$("#parent_row2").hide();
		$("#workshop_sat").val('');
	}
	
	if (choices > 0){
		// enable 'Check code' button
		$("#check_button").attr("disabled", false);
	}else{
		// disable 'Check code' button
		$("#check_button").attr("disabled", true);
		
		// show all workshop option rows if not already visible
		$("#professional_row1").show();
		$("#parent_row1").show();
		$("#parent_row2").show();		
	}

	// record delegate package choice price in amount field data
	$("#amount").data('delegate',price);
	
}
function calculateFee(){
	
	var payable 		= 0.00;
	var payable_tax 	= 0.00;
	
	// delegate fee
		// has a reduced amount been defined? use it if so
		if ($("#amount").data('reduction') != ''){
			payable 	= (payable*1) + ($("#amount").data('reduction')*1);
		}else{
			payable 	= (payable*1) + ($("#amount").data('delegate')*1);	
		}
		
	// Accommodation
		// how many days have been ticked?
		var dates = new Array("accom_6th","accom_7th","accom_8th");
		var dates_ticked = 0;
		for (var i=0; i<dates.length; i++){
			if ($("#" + dates[i]).attr('checked') == true) dates_ticked++;
		}
		
		if (dates_ticked > 0){			
			// which room/rate option has been chosen?
			var room_option = $("input[name='room_type']:checked").val();
			if (room_option == 'Single'){
				var feeArray = accommodation_fees_single;
				var taxArray = accommodation_taxes_single;
			}else if (room_option == 'Double'){
				var feeArray = accommodation_fees_double;
				var taxArray = accommodation_taxes_double;			
			}
			
			payable 	= (payable*1) + (dates_ticked * feeArray[priceArrayIndex]);
			payable_tax	= (payable_tax*1) + (dates_ticked * taxArray[priceArrayIndex]);		
		}
	
//		alert('payable: '+payable);
//		alert('payable_tax: '+payable_tax);
	
	// record not including and including tax values against 'amount' field
	// (used to display confirmation message)
	$("#amount").data('amount_nontax', formatAsMoney(payable));
	$("#amount").data('amount_tax', formatAsMoney(payable_tax));
	
	// fill hidden amount field
//	$("#amount").val(formatAsMoney(payable));
	$("#amount").val(formatAsMoney(payable + payable_tax));
	
}

$(document).ready(function(){

	// initialise 'amount' reduction figure and tax and non-tax figures
	$("#amount").data('reduction', 0.00);
	$("#amount").data('amount_nontax', 0.00);
//	$("#amount").data('amount_tax', 0.00);

	// reset currency dropdown and delegate package checkboxes
	$("#currency").val('');
	$("#professional_package").attr('checked', false);
	$("#parent_package").attr('checked', false);
	
	// reset accommodation choice checkboxes
	$("#accom_6th").attr('checked', false);
	$("#accom_7th").attr('checked', false);
	$("#accom_8th").attr('checked', false);
	
	// hide 'room_type_rows'
	$("#room_type_row_1").hide();
	$("#room_type_row_2").hide();
	$("#room_type_row_3").hide();
	$("#room_type_row_4").hide();
	$("input[name='room_type']").attr("checked",false);

	// add actions to currency dropdown and delegate package checkboxes
	$("#currency").change(function(){
		packageCheck();
	});
	$("#professional_package").click(function(){
		packageCheck();
	});
	$("#parent_package").click(function(){
		packageCheck();
	});
	
	$("#accom_6th, #accom_7th, #accom_8th").click(function(){
		// any of these 3 checkboxes now checked?
		if (($("#accom_6th").attr('checked') == false) && ($("#accom_7th").attr('checked') == false) && ($("#accom_8th").attr('checked') == false)){
			$("#room_type_row_1").hide();
			$("#room_type_row_2").hide();
			$("#room_type_row_3").hide();
			$("#room_type_row_4").hide();
			$("input[name='room_type']").attr("checked",false);
		}else{
			$("#room_type_row_1").show();
			$("#room_type_row_2").show();
			$("#room_type_row_3").show();
			$("#room_type_row_4").show();				
		}
	});
	
/*	// add action to 'Check code' button
	$("#check_button").click(function(){
		codeCheck();						 
	});
	
	// disable 'Check code' button
	$("#check_button").attr("disabled", true);
	// reset 'promo_code' field
	$("#promo_code").val('');*/
	
	function workshopCheck(value){
				
		if (value == "2 day In-Depth Track"){
			// check 'workshop_thurs_fri' dropdown
			if ($("#workshop_thurs_fri").val() != "") return true;
		}else if (value == "1 day Essentials Track"){
			// check 'workshop_sat' dropdown
			if ($("#workshop_sat").val() != "") return true;			
		}
		
	}
	jQuery.validator.addMethod("workshopCheck", function(value,element){
		return this.optional(element) || (workshopCheck(value));
	}, "Please choose a Workshop option for your chosen Delegate package");
	
	function roomRateCheck(value){
		if ($("input[name='room_type']").is(':checked')) {
			return true;
		}
	}
	jQuery.validator.addMethod("roomRateCheck", function(value,element){
		return this.optional(element) || (roomRateCheck(value));
	}, "Please choose a room/rate option for your accommodation");
	
	// form checking
	$("#registration_form").validate({
		rules: {
			first_name: "required",
			last_name: "required",
			address1: "required",
			city: "required",
			tel_no: "required",
			email: {
				required: true,
				email: true
			},
			currency: "required",
			professional_package: {
				required: false,
				workshopCheck: true
			},
			parent_package: {
				required: false,
				workshopCheck: true
			},
			accom_6th: {
				required: false,
				roomRateCheck: true
			},
			accom_7th: {
				required: false,
				roomRateCheck: true
			},
			accom_8th: {
				required: false,
				roomRateCheck: true
			},
			tandc: "required"
		},
		messages: {
			email: "Please enter a valid email address",
			tandc: "You must read and agree to the terms and conditions before continuing"
		},
		errorContainer: "#errors_message",
		errorElement: "div",
		errorPlacement: function(error, element){
			var fieldname = element.attr('name');
			if ((fieldname == "accom_6th")||(fieldname == "accom_7th")||(fieldname == "accom_8th")){
				error.insertAfter(element.parent());
			}else{
				error.insertAfter(element);
			}
		},
		submitHandler: function(form){
			
			// check that at least one of the checkboxes 'professional_package' and 'parent_package' has been ticked
			if (!packageCheckSimple()) return false;

			calculateFee();
						
			// confirmation message
			if ($("#amount").val() > 0){
				var msg = "The payable amount is " + currency_code + $("#amount").data('amount_nontax');
				if ($("#amount").data('amount_tax') > 0.00){
					msg += " (+ " + currency_code + $("#amount").data('amount_tax') + " tax)";
				}
				msg += ". Continue?";
				if (confirm(msg)) form.submit();
			}else{
				form.submit();
			}
		},
		invalidHandler: function(){
			invalidResult();
		}
	});
	
	function invalidResult(){
//		alert('form is invalid');	
	}	
	
});

// round off amount to pounds and pence
function formatAsMoney(mnt){
	mnt -= 0;
	mnt = (Math.round(mnt*100))/100;
	return (mnt == Math.floor(mnt)) ? mnt + '.00' : ( (mnt*10 == Math.floor(mnt*10)) ? mnt + '0' : mnt);
}
