var VAT_rate = 1.15;

function checkMulti(){
	if (Request.QueryString("multi").Count > 0) { 
		document.Form1.multi.value = "yes";
		document.Form1.multiID.value = Request.QueryString("multiID");
		document.Form1.amount.value = Request.QueryString("amount");
		// increment hidden multiCount field
		document.Form1.multiCount.value = (Request.QueryString("multiCount")*1) + 1;
	}else{
		document.Form1.amount.value = 0.00;
	}
}

function jobTypeCheck(dropdown){
	var chosen = dropdown.options[dropdown.selectedIndex].value;
	var jobType_other_holder = document.getElementById("jobType_other_holder");
	while(jobType_other_holder.hasChildNodes()){
		jobType_other_holder.removeChild(jobType_other_holder.lastChild);
	}
	if (chosen == "Other"){
		var newLabel = document.createElement("label");
			newLabel.setAttribute("for","jobType_other");
				var newTxt = document.createTextNode("Please provide details:");
			newLabel.appendChild(newTxt);
		var newInput = document.createElement("input");
			newInput.setAttribute("id","jobType_other");
			newInput.setAttribute("name","jobType_other");
			newInput.setAttribute("type","text");
		var newSpan = document.createElement("span");
			newSpan.setAttribute("class","redbig");
			newSpan.setAttribute("className","redbig");
				var newTxt = document.createTextNode("\u00A0\u002A");
			newSpan.appendChild(newTxt);
		var newBR = document.createElement("br");
		jobType_other_holder.appendChild(newLabel);
		jobType_other_holder.appendChild(newInput);
		jobType_other_holder.appendChild(newSpan);
		jobType_other_holder.appendChild(newBR);
	}
}
function organisationCheck(dropdown){
	var chosen = dropdown.options[dropdown.selectedIndex].value;
	var organisation_other_holder = document.getElementById("organisation_other_holder");
	while(organisation_other_holder.hasChildNodes()){
		organisation_other_holder.removeChild(organisation_other_holder.lastChild);
	}
	if (chosen == "Other"){
		var newLabel = document.createElement("label");
			newLabel.setAttribute("for","organisation_other");
				var newTxt = document.createTextNode("Please provide details:");
			newLabel.appendChild(newTxt);
		var newInput = document.createElement("input");
			newInput.setAttribute("id","organisation_other");
			newInput.setAttribute("name","organisation_other");
			newInput.setAttribute("type","text");
		var newSpan = document.createElement("span");
			newSpan.setAttribute("class","redbig");
			newSpan.setAttribute("className","redbig");
				var newTxt = document.createTextNode("\u00A0\u002A");
			newSpan.appendChild(newTxt);			
		var newBR = document.createElement("br");
		organisation_other_holder.appendChild(newLabel);
		organisation_other_holder.appendChild(newInput);
		organisation_other_holder.appendChild(newSpan);
		organisation_other_holder.appendChild(newBR);
	}	
}

var errors;
var popup;

function submitForm(type){
	
	errors = 0;
	popup="\nYour form entries are incomplete:\n";
	checkMulti();
	
	// regions dropdown check for regions form
	if (document.getElementById("region")){
		dropdownCheck("region");
	}
	
	// check all mandatory regular text fields
	var textFieldNames = new Array("title","first_name","last_name","job_title","organisation","address1","city","postcode","tel_no");
	for (var i=0; i<textFieldNames.length; i++){
		textCheck(textFieldNames[i]);
	}
	// check email field
	if (!emailCheck("email")){
		popup+="\nPlease check that all email addresses are valid\n";
	}
	
	// check mandatory dropdowns
	var dropdownNames = new Array("job_type","organisation_type","streams_wed1","streams_wed2","streams_thur","how_hear");
	for (var i=0; i<dropdownNames.length; i++){
		dropdownCheck(dropdownNames[i]);
	}
	
	// if jobType_other field exists, check it's not blank
	if ((document.getElementById("jobType_other"))&&(document.getElementById("jobType_other").value == "")) {
		errors++;
	}
	// if organisation_other field exists, check it's not blank
	if ((document.getElementById("organisation_other"))&&(document.getElementById("organisation_other").value == "")) {
		errors++;
	}
	
	// check that a delegate_package has been chosen
	radioSetCheck("delegate_package");

	// check that 'Attend International Seminar', and the 2 permission questions have been answered
	radioSetCheck("international_seminar");
	radioSetCheck("permission_exhibitors_sponsors");
	radioSetCheck("permission_handbook");
	
	// check that Terms and Conditions checkbox has been checked
	if (!checkboxCheck("tandc")){
		popup+="\nPlease tick to confirm that you have read and\nagree with the Terms and Conditions\n";
	}	
	
	if (type=="multi") {
		document.getElementById("multi").value = "yes";
	}else{
		document.getElementById("multi").value = "no";
	}
	
	if (errors > 0){
		popup+="\nPlease complete the fields marked\nwith a red * and then re-submit.\n";
		alert(popup);
	}else{
		
		// free promo code workaround
		if (document.getElementById("buttons_standard").style.display == 'none'){
			// set all amount fields to 0.00
			document.getElementById("IndividualAmount").value 	= 0.00;
			document.getElementById("amount").value 			= 0.00;
			document.getElementById("net_amount").value 		= 0.00;
			document.getElementById("vat_amount").value 		= 0.00;
			document.getElementById("total_amount").value 		= 0.00;
			var msg = "The total payable amount is £0.00. Continue?";
			if (confirm(msg)){
				document.forms.Form1.submit();
			}			
		}else{
			// calculate sum from this submission only
			var IndividualAmount_net = formatAsMoney(calculateFee(type));
			var IndividualAmount = formatAsMoney((IndividualAmount_net)*(VAT_rate));
			// populate hidden 'IndividualAmount' field with net value
			document.getElementById("IndividualAmount").value = IndividualAmount_net;
			// create text string with this info
			var IndividualAmount_str = "£" + IndividualAmount_net + " + VAT = £" + IndividualAmount;
			
			if (type=="multi") {
				var msg = "The payable amount for this delegate is " + IndividualAmount_str + ". Continue?";
				if (confirm(msg)){
					document.forms.Form1.submit();
				}
			}else{
				var payable_amount = document.getElementById("amount").value;
				// for total_amount field we need figure inc VAT
				document.getElementById("total_amount").value = formatAsMoney(payable_amount * VAT_rate);
	
				// create total amount payable string
				var totalAmount_str = payable_amount + " + VAT = £" + document.getElementById("total_amount").value; 
				
				// make sure 'amount' field has inc VAT value now that form is being fully submitted 
				document.getElementById("amount").value = document.getElementById("total_amount").value;
				// also fill 'net_amount' and 'vat_amount' fields accordingly
				document.getElementById("net_amount").value = payable_amount;
				document.getElementById("vat_amount").value = formatAsMoney(document.getElementById("total_amount").value - payable_amount);
				
				var msg = "The payable amount for this delegate is " + IndividualAmount_str + ".\n";			
				msg += "The total payable amount is £" + totalAmount_str + ". Continue?";
				if (confirm(msg)){
					document.forms.Form1.submit();
				}
			}			
		}		
	}	
}

// pricing arrays
var members_full_prices    = new Array(740.00,680.00,580.00);
var members_wed_prices     = new Array(540.00,480.00,430.00);
var members_thur_prices    = new Array(540.00,480.00,430.00);
var private_full_prices    = new Array(1600.00,1600.00,1600.00);
var retired_full_prices    = new Array(75.00,75.00,75.00);
var CIPFA_NW_Region_prices = new Array(250.00,250.00,250.00);

function calculateFee(type){
	
	// initialise sum value
	var sum = 0;
	
	if (document.getElementById("multiCount").value <= 3){
		var priceArrayIndex = (document.getElementById("multiCount").value*1) - 1;
	}else{
		var priceArrayIndex = 2;
	}

	// chosen delegate_package
	if (document.getElementById("members_full").checked == true){
		delegate_package = "members_full";
		sum = members_full_prices[priceArrayIndex];
	}else if (document.getElementById("members_wed").checked == true){
		delegate_package = "members_wed";
		sum = members_wed_prices[priceArrayIndex];
	}else if (document.getElementById("members_thur").checked == true){
		delegate_package = "members_thur";
		sum = members_thur_prices[priceArrayIndex];
	}else if (document.getElementById("private_full").checked == true){
		delegate_package = "private_full";
		sum = private_full_prices[priceArrayIndex];
	}else if (document.getElementById("retired_full").checked == true){
		delegate_package = "retired_full";
		sum = retired_full_prices[priceArrayIndex];
	}
	
	sum = formatAsMoney(sum);

	// add to 'amount' field
	document.getElementById("amount").value = formatAsMoney((document.getElementById("amount").value*1) + (sum*1));

	return sum;	
}

function init(){
	checkMulti();
	resetForm();
	
	// now populate members_full_price_holder, members_wed_price_holder and members_thur_price_holder spans
	// with the correct price, dependent on the hidden multiCount value
	if (document.getElementById("multiCount").value > 1){
		function clearSpan(spanName){
			var dom_element = document.getElementById(spanName);
			while(dom_element.hasChildNodes()){
				dom_element.removeChild(dom_element.lastChild);
			}	
		}
		clearSpan("members_full_price_holder");
		clearSpan("members_wed_price_holder");
		clearSpan("members_thur_price_holder");

		if (document.getElementById("multiCount").value <= 3){
			var priceArrayIndex = (document.getElementById("multiCount").value*1) - 1;
		}else{
			var priceArrayIndex = 2;
		}
		// create price string
		var members_full_price_str = document.createTextNode(members_full_prices[priceArrayIndex]);
		var members_wed_price_str  = document.createTextNode(members_wed_prices[priceArrayIndex]);
		var members_thur_price_str = document.createTextNode(members_thur_prices[priceArrayIndex]);
		
		// fill price holders
		document.getElementById("members_full_price_holder").appendChild(members_full_price_str);
		document.getElementById("members_wed_price_holder").appendChild(members_wed_price_str);
		document.getElementById("members_thur_price_holder").appendChild(members_thur_price_str);
	}
	
	// if the regional form is accessing this script then hide '#formarea' by default 
	// but not if a multi-booking is in progress
	if (document.getElementById("region")){
		if (document.Form1.multi.value == "yes"){
			// show '#formarea' and hide '#discount_code_entry'
			document.getElementById("formarea").style.display = 'block';
			document.getElementById("discount_code_entry").style.display = 'none';			
		}else{
			// hide '#formarea' and show '#discount_code_entry'
			document.getElementById("formarea").style.display = 'none';
			document.getElementById("discount_code_entry").style.display = 'block';
		}
	}
}
window.onload = init;
