// Global variables

// US Dollars currency code for Javascript display use
var currency_code 		= "\u0024";
// Delegate package pricing variables
var professional_fee 	= 320.00;
var parent_fee 			= 160.00;


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 packageCheck(element){

	// reset 'amount' reduction figure
	$("#amount").data('reduction', '');
	var price = 0.00;
	
	packageCheckSimple();
	
	var choices = 0;
	if ($("#professional_package").attr('checked') == true){
		choices++;
		price = (price*1) + professional_fee;
		// 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_2_day").val('');
	}
	if ($("#parent_package").attr('checked') == true){
		choices++;
		price = (price*1) + parent_fee;
		// 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_1_day").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);	
		}
		
	
//	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 + payable_tax));

	// now also fill hidden invoice related price fields
//	$("#items_0_price").val(formatAsMoney(payable));
//	$("#items_0_amount").val(formatAsMoney(payable));
}

$(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 delegate package checkboxes
	$("#professional_package").attr('checked', false);
	$("#parent_package").attr('checked', false);

	// add actions to delegate package checkboxes
	$("#professional_package").click(function(){
		packageCheck();
	});
	$("#parent_package").click(function(){
		packageCheck();
	});
	
	// 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_2_day' dropdown
			if ($("#workshop_2_day").val() != "") return true;
		}else if (value == "1 day Essentials Track"){
			// check 'workshop_1_day' dropdown
			if ($("#workshop_1_day").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");
	
	
	// form checking
	$("#registration_form").validate({
		rules: {
			first_name: "required",
			last_name: "required",
			address1: "required",
			city: "required",
			postcode: "required",
			tel_no: "required",
			email: {
				required: true,
				email: true
			},
			professional_package: {
				required: false,
				workshopCheck: true
			},
			parent_package: {
				required: false,
				workshopCheck: true
			},
			accommodation_breakfast: {
				required: true,
				accommodationCheck: 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",
		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);
}
