var government_prices 			= new Array("895","630");
var commercial_prices 			= new Array("1295","900");
var pre_conf_workshop_prices 	= new Array("450","375");
var currency_choice;
var currency_code;
var price;

function currencyPackageCheck(){
	
	// firstly reset 'Promotional Code' section
	$("#check_button").attr("disabled", true);
	$("#checking_text").hide();
	$("#fee_paragraph").hide();
	$("#error_paragraph").hide();
	$("#unchecked_paragraph").hide();
	$("#promo_code").val('');

	// reset 'amount' reduction figure
	$("#amount").data('reduction', '');

	currency_choice 	= $("#currency_choice").val();
	if (currency_choice != ''){
		$("#currency").val(currency_choice);
		if (currency_choice == "EUR"){
			currency_code = "\u20AC";			
		}else if (currency_choice == "USD"){
			currency_code = "\u0024";			
		}
		
		// now check chosen package
		var delegate_package = $("input[name='delegate_package']:checked").val();
		if (delegate_package){
			
			var priceArrayIndex = 0;
			if (currency_choice == "EUR") priceArrayIndex = 1;
			
			var priceArray;
			if (delegate_package == "government"){
				priceArray = government_prices;
			}else if (delegate_package == "commercial"){
				priceArray = commercial_prices;
			}
			price = priceArray[priceArrayIndex];
			// record delegate package choice price in amount field data
			$("#amount").data('delegate',price);
			
			
			// enable 'Check code' button
			$("#check_button").attr("disabled", false);
		}	
	}
}
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_delegate.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{
					
					var delegate_fee = $("#amount").data('delegate');
					
					if (discount_returned == 'FREE'){
						var reduced_rate = '0.00';
						var fee_text = "FREE";
					}else if (discount_returned == '10%'){
						// calculate 90% of hidden 'amount' field value
						var reduced_rate = formatAsMoney(delegate_fee * 0.9);
						var fee_text = currency_code + reduced_rate;					
					}else if (discount_returned == '20%'){
						// calculate 80% of hidden 'amount' field value
						var reduced_rate = formatAsMoney(delegate_fee * 0.8);
						var fee_text = currency_code + reduced_rate;			
					}else if (discount_returned == 'Guest'){
						var priceArray = government_prices;
						if (currency_choice == "EUR"){
							priceArrayIndex = 1;
						}else if (currency_choice == "USD"){
							priceArrayIndex = 0;
						}
						var reduced_rate = priceArray[priceArrayIndex];
						var fee_text = currency_code + reduced_rate;
					}else if (discount_returned == 'Editor'){
						if (currency_choice == "EUR"){
							var reduced_rate = '350.00';
						}else if (currency_choice == "USD"){
							var reduced_rate = '500.00';
						}
						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 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{
			payable = (payable*1) + ($("#amount").data('delegate')*1);	
		}

	// Pre-Conference Workshop
		if ($("#pre_conf_workshop").attr('checked') == true){
			if (currency_choice == "EUR"){
				payable = (payable*1) + (pre_conf_workshop_prices[1]*1);
			}else if (currency_choice == "USD"){
				payable = (payable*1) + (pre_conf_workshop_prices[0]*1);		
			}		
		}
	
	// fill hidden amount field
	$("#amount").val(formatAsMoney(payable));

}
$(document).ready(function(){

	// initialise 'amount' reduction figure
	$("#amount").data('reduction', 0.00);
	
	// reset 'currency option' dropdown
	$("#currency_choice").val('');
		
	// add action to 'currency_choice' dropdown
	$("#currency_choice").change(function(){
		currencyPackageCheck();
	});
	
	// add action to 'delegate_package' radio buttons
	$("input[name='delegate_package']").change(function(){
		currencyPackageCheck();						 
	});
	
	// 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('');
	
	// form checking
	$("#registration_form").validate({
		rules: {
			title: "required",
			first_name: "required",
			last_name: "required",
			job_title: "required",
			organisation: "required",
			address1: "required",
			city: "required",
			postcode: "required",
			country: "required",
			tel_no: "required",
			email: {
				required: true,
				email: true
			},
			currency_choice: "required",
			delegate_package: "required",			
			how_hear: "required",
			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();
			
			$('#items_0_amount').val($('#amount').val());
			$('#items_0_price').val($('#amount').val());
			if ($("input[name='delegate_package']:checked").val() == 'government') {
				$('#items_0_description').val('Delegate Package - Civil/Military/Government Responders');	
			} else {
				$('#items_0_description').val('Delegate Package - Commercial Enterprises');
			}
			
			// 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);
}
