function setLabelText(ID, Text){
	var money = Text.toFixed(2);
    	document.getElementById(ID).innerHTML = "&pound;" + money;
}

function get_item(element) {
	return document.getElementById(element);
}

function create_element(anchor, name, value) {
	// Get the hook for the doc fragment
	e_parent = get_item(anchor);
	
	// Create the input element
	e_child = document.createElement("input");
	
	// Set the attributes
	e_child.setAttribute("type","hidden");
	e_child.setAttribute("name",name);
	e_child.setAttribute("value",value);
	
	// Append to fragment
	e_parent.appendChild(e_child);
}
	
function delete_element(anchor, element) {
	// Get the parent element
	e_parent = get_item(anchor);
	e_child = get_item(element);
	
	// Remove the child node
	e_parent.removeChild(e_child);
	
}

function paypal_item_control(item_no, title, quantity, amount, shipping) {	
	create_element("s", "item_name_"+item_no, title);
	create_element("s", "quantity_"+item_no, quantity);
	create_element("s", "amount_"+item_no, amount);
	create_element("s", "shipping_"+item_no, shipping);
}

function checkbox_stat(status) {
	for(i = 0; i < 2; i++) {
		document.addition.sublen[i].disabled = status;
		document.addition.ren[i].disabled = status;
	}
		
	return false;
}

function tot_up(submitted) {
// Clear the secret element
e_parent = get_item("addition");
e_child = get_item("s");

// Remove the child node
e_parent.removeChild(e_child);

// Now replace it
e_parent = get_item("addition");
e_child = document.createElement("p");
e_child.setAttribute("id","s");
e_parent.appendChild(e_child);

var total = 0;
var pandp = 0;

// Set up the postage

var postage = new Array(4);

postage[0] = 0; // UK Base price
postage[1] = 4; // Europe Base Price
postage[2] = 6; // Airmail Base Price
postage[3] = 3; // Seamail Base Price

// Set up base prices for the magazines
// Magazine first
var paypal_item = 0;
var multiplier = 0;
var mag_total = 0;
var pandp_tot = 0;
var subscription = "";
var addp = 0;
var etxt = "";
checkbox_stat(true);

var renewal_text = new Array(2);
renewal_text[0] = " (Renewal)";
renewal_text[1] = " (New Subscription)";

if(document.addition.bww.checked == true || document.addition.mtn.checked == true) {
	checkbox_stat(false);
}

// Find out how long for firstly...
for(i = 0; i < 2; i++) {
	if(document.addition.sublen[i].checked == true) {
		multiplier = document.addition.sublen[i].value;
				
		// For 1 or more years?
		multiplier == 1 ? y = " year" : y = " years";
		subscription = "for "+ multiplier + y;
	}		
}

if(document.addition.bww.checked == true && subscription != "") {
	// Calculate cost for the BWW magazine
	mag_cost = 21 * multiplier;
	mag_total += mag_cost;
	paypal_item++;
	
	// Apply P & P
	if(document.addition.postage.selectedIndex == 2) {
		// If it the RoW, we need to add 3 to the p and p
		addp = 3;	
	}
	
	pandp = (postage[document.addition.postage.selectedIndex] + addp) * multiplier;
	
	pandp_tot += pandp;
	
	// Finally, is this a renewal?
	for(i = 0; i < 2; i++) {
		if(document.addition.ren[i].checked == true) {
			var renewal = " (New Subscription)";
		} else {
			var renewal = " (Renewal)";
		}
	}
	
	// Generate PayPal control
	paypal_item_control(paypal_item, "BWW Subscription " + subscription + renewal, 1, mag_cost, pandp);
	
	var mag = true;
	
}

if(document.addition.mtn.checked == true && subscription) {
	// Now check cost for MTN
	mag_cost = 10 * multiplier;
	mag_total += mag_cost;
	paypal_item++;
	
	// Apply P & P
	pandp = postage[document.addition.postage.selectedIndex] * multiplier;
	pandp_tot += pandp;
	
	// Finally, is this a renewal?
	for(i = 0; i < 2; i++) {
		if(document.addition.ren[i].checked == true) {
			var renewal = renewal_text[i];
		} 
	}
	
	// Generate PayPal control
	paypal_item_control(paypal_item, "MTN Subscription " + subscription + renewal, 1, mag_cost, pandp);
	
	var mag = true;
}

if(document.addition.bba.checked == true && subscription) {
	// Now check cost for BBA
	if(multiplier == 1) {
		// Owing to the uneven number of years...
		mag_cost = 14;
	} else {
		mag_cost = 25;
	}
	
	mag_total += mag_cost;
	paypal_item++;
	
	// Apply P & P
	pandp = postage[document.addition.postage.selectedIndex] * multiplier;
	pandp_tot += pandp;
	
	// Finally, is this a renewal?
	for(i = 0; i < 2; i++) {
		if(document.addition.ren[i].checked == true) {
			var renewal = renewal_text[i];
		} 
	}
	
	// Generate PayPal control
	paypal_item_control(paypal_item, "BBA Subscription " + subscription + renewal, 1, mag_cost, pandp);
	
	var mag = true;
}

if(submitted == true && mag == true && !multiplier) {
	etxt += "\n -Please choose for how long you want the subscription for";
}

if(submitted == true && mag == true && !renewal) {
	etxt += "\n -Please specify whether this is a renewal or new subscription";
}

// Now lets sort out the additional services
var additional_total = 0;
var cd_postage = 0;

// Check to see which check boxes have been selected... For the CDs..
if(document.addition.postage.selectedIndex > 0) {
	cd_postage = 1;
}

// For MBoY
var mboy_postage = new Array(4);

mboy_postage[0] = 0;
mboy_postage[1] = 1;
mboy_postage[2] = 2;
mboy_postage[3] = 2;

// Sort out the cost of the additional services

// Firstly MBoY
if(document.addition.mboy.checked == true) {
	paypal_item++;
	mag_total += 5;
	paypal_item_control(paypal_item, "Maltese Buses of Yesteryear", 1, 5, mboy_postage[document.addition.postage.selectedIndex]);
	pandp_tot += mboy_postage[document.addition.postage.selectedIndex];
	}

// Now for Sri Lankan Buses (SLB)
var slb_postage = new Array(3);

slb_postage[0] = 1;
slb_postage[1] = 3;
slb_postage[2] = 5;

if(document.addition.slb.checked == true) {
	paypal_item++;
	mag_total += 12;
	paypal_item_control(paypal_item, "Sri Lankan Buses - Past and Present", 1, 12, slb_postage[document.addition.postage.selectedIndex]);
	pandp_tot += slb_postage[document.addition.postage.selectedIndex]
}

// Create the grand total
var g_total = total + additional_total + pandp_tot;

// Populate the paragraphs
var renewal = " (Renewal)";
setLabelText("p_tot", mag_total);
setLabelText("c_tot", additional_total);
setLabelText("pp_tot", pandp_tot);
setLabelText("g_tot", mag_total + additional_total + pandp_tot);

if(etxt != "") {
	alert("Please also supply the following information:"+etxt);
	return false;
}

if(submitted == true && mag_total + additional_total + pandp_tot == 0 && etxt == "") {
	alert("Please choose some options from the form!");
	return false;
}

//alert("Publication total: &pound;"+total+" \n Additional stuff: &pound;"+ additional_total);
}

function validate() {
	
}


