var currency_choice;
var currency_code;

function resetPromo(){
	// firstly reset 'Promotional Code' section
	$("#checking_text").hide();
	$("#fee_paragraph").hide();
	$("#error_paragraph").hide();
	$("#unchecked_paragraph").hide();
	$("#promo_code").val('');

	// reset 'amount' reduction figure
	$("#amount").data('reduction', '');
}

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_exhibitor.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();
				if (discount_returned == 'NONE'){
					$("#error_paragraph").show();
				}else{
					
					if (discount_returned == 'FREE'){
						var reduced_rate = '0.00';
						var fee_text = "FREE";
					}
					// 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 calculateFee(){
	
	var payable = 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{
			currency_choice = $("input[name=currency_choice]:checked").val();
			if (currency_choice == "EUR"){
				payable = (payable*1) + (2258*1);
				currency_code 	= "\u20AC";
			}else if (currency_choice == "USD"){
				payable = (payable*1) + (3600*1);
				currency_code 	= "\u0024";
			}
		}
	// how many additional attendees are being booked
		var additionalCount = $("#additional_attendees").val();
		if (additionalCount > 0){
			currency_choice = $("input[name=currency_choice]:checked").val();
			if (currency_choice == "EUR"){
				payable = (payable*1) + (additionalCount*100);
				currency_code 	= "\u20AC";
			}else if (currency_choice == "USD"){
				payable = (payable*1) + (additionalCount*150);
				currency_code 	= "\u0024";
			}
		}
	
	// fill hidden amount field
	$("#amount").val(formatAsMoney(payable));
//	alert($("#amount").val());
}
$(document).ready(function(){

	// initialise 'amount' reduction figure
	$("#amount").data('reduction', '');
	
	// add action to 'currency_choice' radio buttons
	$("input[name='currency_choice']").change(function(){
		resetPromo();
	});
	
	// add action to 'Check code' button
	$("#check_button").click(function(){
		codeCheck();						 
	});
	
	// reset 'promo_code' field
	$("#promo_code").val('');
	
	// set max number of additional attendees
	var additional_attendees_max = 4;
	// hide additional attendee fields initially
	for (var i=1; i<=additional_attendees_max; i++){
		$("#additional_attendee" + i).hide();
	}
	
	// reset and add action to 'additional_attendees' dropdown
	$("#additional_attendees").val('0');
	$("#additional_attendees").change(function(){
		var choice = $(this).val();
		// show
		for (var i=0; i<=choice; i++){
			$("#additional_attendee" + i).show();
		}
		// hide
		for (var i=additional_attendees_max; i>choice; i--){
			$("#additional_attendee" + i).hide();
		}
	});
	
	// form checking
	// add method to check that a firstname and lastname has been provided for each additional Awards Dinner attendee
	function additionalNameFieldsCheck(value){
		if (value != ''){
			var nameErrors = 0;
			for (var i=1; i<=value; i++){
				
				firstNameField 	= "#additional" + i + "_first_name";
				lastNameField 	= "#additional" + i + "_last_name";

				if (($(firstNameField).val() == '') || ($(lastNameField).val() == '')) nameErrors++;
			}
			if (nameErrors == 0) return true;
		}		
	}
	jQuery.validator.addMethod("additionalAttendees", function(value,element){
		return this.optional(element) || (additionalNameFieldsCheck(value));
	}, "Please enter names below of each additional stand attendee");	
	
	$("#registration_form").validate({
		rules: {
			title: "required",
			first_name: "required",
			last_name: "required",
			job_title: "required",
			organisation: "required",
			stand_number: "required",
			stand_number_2ndchoice: "required",
			address1: "required",
			city: "required",
			postcode: "required",
			country: "required",
			tel_no: "required",
			email: {
				required: true,
				email: true
			},
			nature_of_business: "required",
			currency_choice: "required",
			stand_attendee_first_name: "required",
			stand_attendee_last_name: "required",
			conference_attendee_first_name: "required",
			conference_attendee_last_name: "required",
			additional_attendees: { additionalAttendees: 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: "label",
		submitHandler: function(form){
			calculateFee();
			// confirmation message
			if ($("#amount").val() > 0){
				var msg = "The payable amount is " + currency_code + $("#amount").val() + ". 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);
}
