// Price variables which can be updated whenever required 
// Changes here will not break the calculations below

// delegate rates
var public_sector_rate 		= 490.00;
var private_sector_rate 	= 790.00;

// Conference Dinner rate and tax
var conference_dinner_rate 	= 65.00;
var conference_dinner_tax 	= 7.73;

// Accommodation rates (including tax (VAT))
var single_occupancy_rate	= 119.00;
var double_occupancy_rate	= 139.00;
var shared_occupancy_rate	= 70.00;
// Accommodation tax rates
var single_occupancy_tax	= 14.15;
var double_occupancy_tax	= 16.53;
var shared_occupancy_tax	= 8.33;

// Upgrade to superior room rate (per room per night), including tax (VAT)
var upgrade_rate 			= 22.70;
// Upgrade to superior room tax rate
var upgrade_tax 			= 2.70;

// Carbon offset rate (no tax payable)
var carbon_offset_rate		= 13.00;


// 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);
}
function packageCheck(){
	
	// 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', '');

	// which delegate package chosen (fill "items_0..." hidden invoice fields)
	if ($("#delegate_package_public").attr('checked') == true){
		
		$("#items_0_description").val("Delegate Package - Public Sector");
		$("#items_0_tax").val("");
		$("#items_0_price").val(public_sector_rate);
		$("#items_0_amount").val(public_sector_rate);
		
		// record delegate package choice price in amount field data
		$("#amount").data('delegate',public_sector_rate);		
		
	}else if ($("#delegate_package_private").attr('checked') == true){
		
		$("#items_0_description").val("Delegate Package - Private Sector");
		$("#items_0_tax").val("");
		$("#items_0_price").val(private_sector_rate);
		$("#items_0_amount").val(private_sector_rate);
		
		// record delegate package choice price in amount field data
		$("#amount").data('delegate',private_sector_rate);		
		
	}
	
	// enable 'Check code' button
	$("#check_button").attr("disabled", false);
	
}
function codeCheck(){
	var promo_code 	= $("#promo_code").val().toUpperCase().replace(/^\s+|\s+$/g, '');
	
	var currency_code 	= "\u20AC"; // EUR
	
	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.php?" + 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 == '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 == 'EUR400'){
						// calculate 90% of hidden 'amount' field value
						var reduced_rate = formatAsMoney(400);
						var fee_text = currency_code + reduced_rate;										
					} else if (discount_returned == 'EUR600'){
						// calculate 90% of hidden 'amount' field value
						var reduced_rate = formatAsMoney(600);
						var fee_text = currency_code + reduced_rate;										
					}
					
					// record amount's reduction figure
					$("#amount").data('reduction', reduced_rate);
					
					// update hidden invoice delegate package pricing fields
					$("#items_0_price").val(reduced_rate);
					$("#items_0_amount").val(reduced_rate);					
					
					$("#fee_holder").append(fee_text);
					$("#fee_paragraph").show();
				}
				
				// enable button, hide checking text, 
				$("#check_button").attr("disabled", false);
				$("#checking_text").hide();

			});
		});		
	}
}
function calculatePrices(){

	var payable 		= 0.00;
	var payable_tax 	= 0.00;
	
	// set default descriptions
//	$("#items_0_description").val("Delegate Package");
	$("#items_1_description").val("Conference Gala Dinner");
	$("#items_2_description").val("Accommodation");
	$("#items_3_description").val("Superior room upgrade");
	$("#items_4_description").val("Carbon Offset");
	// reset all other invoice related hidden fields
	// not including 'items_0' delegate package fields which are set when the radio buttons are clicked
	for (var i=1; i<=4; i++){
		$("#items_" + i + "_tax").val('0.00');
		$("#items_" + i + "_price").val('0.00');
		$("#items_" + i + "_quantity").val('0');
		$("#items_" + i + "_amount").val('0.00');
	}

	// get reduced or normal delegate package rate
	if ($("#amount").data('reduction') != ''){
		payable += ($("#amount").data('reduction') * 1);
	}else{
		payable += ($("#amount").data('delegate') * 1);	
	}
	
	// Conference Dinner (fill "items_1..." hidden invoice fields)
	if ($("#conference_dinner").attr('checked') == true){
		var dinner_total 	= 0;
		var dinner_tax 		= 0;
		// add 1 dinner
		dinner_total 	+= conference_dinner_rate;
		dinner_tax 		+= conference_dinner_tax;
		// any additionals?
		dinner_total 	+= ($("#additional_dinner_attendees").val() * conference_dinner_rate);
		dinner_tax 		+= ($("#additional_dinner_attendees").val() * conference_dinner_tax);
		
		// add amount to running total
		payable 		+= dinner_total;
		payable_tax 	+= dinner_tax;
		
		var dinners_chosen = 1 + ($("#additional_dinner_attendees").val()*1);
		
		$("#items_1_tax").val(dinner_tax);
		$("#items_1_price").val(dinner_total - dinner_tax);
		$("#items_1_quantity").val(dinners_chosen);
		$("#items_1_amount").val(dinner_total);		
	}
	
	// Accommodation (fill "items_2..." hidden invoice fields (and "items_3..." hidden invoice fields if superior room upgrade has been selected))
	
		// how many nights checked
		var nights = 0;
		if ($("#accom_9th").attr('checked') == true) nights++;
		if ($("#accom_10th").attr('checked') == true) nights++;
		if ($("#accom_11th").attr('checked') == true) nights++;
		if ($("#accom_12th").attr('checked') == true) nights++;
		if ($("#accom_13th").attr('checked') == true) nights++;
		
		// which accommodation option selected
		if ($("#room_type_none").attr('checked') == true){
			// do nothing
		}else{
			
			if ($("#room_type_single").attr('checked') == true){
				payable 	+= (single_occupancy_rate * nights);
				payable_tax += (single_occupancy_tax * nights);
				
				$("#items_2_description").val("Double room, single occupancy");
				$("#items_2_tax").val((single_occupancy_tax * nights));
				$("#items_2_price").val((single_occupancy_rate * nights) - (single_occupancy_tax * nights));
				$("#items_2_quantity").val(nights);
				$("#items_2_amount").val((single_occupancy_rate * nights));

			}else if ($("#room_type_double").attr('checked') == true){
				payable 	+= (double_occupancy_rate * nights);
				payable_tax	+= (double_occupancy_tax * nights);

				$("#items_2_description").val("Double room, double occupancy");
				$("#items_2_tax").val((double_occupancy_tax * nights));
				$("#items_2_price").val((double_occupancy_rate * nights) - (double_occupancy_tax * nights));
				$("#items_2_quantity").val(nights);
				$("#items_2_amount").val((double_occupancy_rate * nights));
								
			}else if ($("#room_type_shared").attr('checked') == true){
				payable 	+= (shared_occupancy_rate * nights);
				payable_tax	+= (shared_occupancy_tax * nights);

				$("#items_2_description").val("Double room, shared occupancy");
				$("#items_2_tax").val((shared_occupancy_tax * nights));
				$("#items_2_price").val((shared_occupancy_rate * nights) - (shared_occupancy_tax * nights));
				$("#items_2_quantity").val(nights);
				$("#items_2_amount").val((shared_occupancy_rate * nights));
								
			}
			
			// upgrade to superior room selected?
			if ($("#upgrade").attr('checked') == true){
				// if double or shared option selected then apply for 2 people
				payable 	+= (upgrade_rate * nights);
				payable_tax += (upgrade_tax * nights);
				
				$("#items_3_tax").val((upgrade_tax * nights));
				$("#items_3_price").val((upgrade_rate * nights) - (upgrade_tax * nights));
				$("#items_3_quantity").val(nights);
				$("#items_3_amount").val((upgrade_rate * nights));				
				
			}
		}
		
	// Carbon offset
	if ($("#carbon_offset").attr('checked') == true){
		// add amount to running total (no tax payable)
		payable += carbon_offset_rate;
		
		// fill invoice fields where needed
		$("#items_4_price").val(carbon_offset_rate);
		$("#items_4_quantity").val('1');
		$("#items_4_amount").val(carbon_offset_rate);
	}
	
	// round off amounts to display in money format
	payable 	= formatAsMoney(payable);
	payable_tax = formatAsMoney(payable_tax);
	
	// fill hidden amount fields with calculated values
	document.getElementById("amount").value 	= payable;
	document.getElementById("amount_tax").value = payable_tax;
}

$(document).ready(function(){

	$("#amount").data('amount_nontax', 0.00);
	$("#amount").data('amount_tax', 0.00);

	// reset delegate package radio buttons
	$("input[name='delegate_package']").attr("checked", false);

	// add action to 'delegate_package' radio buttons
	$("input[name='delegate_package']").change(function(){
		packageCheck();						 
	});
	
	// add action to 'Check code' button
	$("#check_button").click(function(){
		codeCheck();						 
	});
	
	// disable 'Check code' button
	$("#check_button").attr("disabled", true);
	$("#checking_text").hide();
	$("#fee_paragraph").hide();
	$("#error_paragraph").hide();
	
	// reset 'promo_code' field
	$("#promo_code").val('');
	
	// table rows to be hidden once the page has loaded
	var table_rows = new Array("row_additional_dinner_attendees","row_additional_dinner_attendees_header","row_additional_dinner_attendees_1","row_additional_dinner_attendees_2","row_additional_dinner_attendees_3");
	for (var i=0; i<table_rows.length; i++){
		$("#"+table_rows[i]).hide();
	}
	// form elements to be reset once the page has loaded
	var form_elements = new Array("additional_dinner_attendees","additional_dinner_1_name","additional_dinner_1_dietary","additional_dinner_2_name","additional_dinner_2_dietary","additional_dinner_3_name","additional_dinner_3_dietary");
	for (var i=0; i<form_elements.length; i++){
		$("#"+form_elements[i]).val('');
	}
	
	// Dinner choices functionality
		
		var rowNameStem 		= "#row_additional_dinner_attendees_";
		// set max number of allowed additional attendees in the dropdown
		var additionalsCount	= 3;

		// reset 'conference_dinner' field
		$("#conference_dinner").attr('checked', false);
		
		// reset accommodation option
		$("input[name=room_type]").attr('checked', false); 
		
		// add action to 'conference_dinner' checkbox
		$("#conference_dinner").click(function(){
			if ($(this).attr('checked') == true){
				$("#row_additional_dinner_attendees").show();
			}else{
				// firstly reset 'additional_dinner_attendees' dropdown
				$("#additional_dinner_attendees").val('');
				// reset and then each additional attendee name and dietary requirement field
				for (var i=1; i<=additionalsCount; i++){
					$("#additional_dinner_" +i+ "_name").val('');
					$("#additional_dinner_" +i+ "_dietary").val('');
					$(rowNameStem + i).hide();
				}
				
				// hide 'row_additional_dinner_attendees' and 'row_additional_dinner_attendees_header'
				$("#row_additional_dinner_attendees").hide();
				$("#row_additional_dinner_attendees_header").hide();
			}
		});		
		
		// add action to 'additional_dinner_attendees' dropdown
		$("#additional_dinner_attendees").change(function(){
			var additional_choice = $("#additional_dinner_attendees").val();
			// if choice is '0' hide 'row_additional_dinner_attendees_header' row, otherwise show the row
			(additional_choice == 0) ? ($("#row_additional_dinner_attendees_header").hide()) : ($("#row_additional_dinner_attendees_header").show());

			// show all additional attendees questions for <= this choice, hide all others
			for (var i=1; i<=additionalsCount; i++){
				if (i <= additional_choice) $(rowNameStem + i).show();
				else{
					// firstly reset the name and dietary requirements fields for this row
					$("#additional_dinner_" +i+ "_name").val('');
					$("#additional_dinner_" +i+ "_dietary").val('');
					$(rowNameStem + i).hide();
				}
			}
		});
		
		// add actions to accommodation options radio buttons
		$("input[name=room_type]").click(function(){
			if ($("input[name=room_type]:checked").val() == 'none'){
				// hide all 'accommodation_options' table rows
				for (var i=1; i<=10; i++){					
					$("#accommodation_options_row_" + i).hide();
				}
			}else{
				// show all 'accommodation_options' table rows
				for (var i=1; i<=10; i++){					
					$("#accommodation_options_row_" + i).show();
				}
			}
		});			
		
});
