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 create_element(anchor, _name, value) {
	// Get the hook for the doc fragment
	e_parent = get_itemid(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 calc_mag_price(issue) {

/* If these change, remember to change the PHP function over too! */

p = 0;

if(issue <= 79) {
	var p = 1;
}

if(issue > 79 && issue <= 133) {
	var p = 2;
}

if(issue > 134 && issue <= 151) {
	var p = 2.5;
}

if(issue > 151) {
	var p = 3.5;
}

return p;

}

function MTNprice(issue) {

var p = 0

if(issue <= 4) {
	p = 4;
}

if(issue > 4 && issue <= 9) {
	p = 3.25
}

if(issue >= 10) {
	p = 3;
}

return p;

}

function BBAprice(issue) {
	// Function for generating BBA back issue prices. Always assumes they are three quid each...
	
	return 3;
}

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

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

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

function calc(issue) {

// Get the BWW controls...
var h = get_item('issue[]');

// Now get the MTN controls...
var k = get_item('mtnissue[]');

// And now the BBA controls
var b = get_item('bba[]');

// Initiate counters
var gp = 0;
var hh = "";
var p = 0;
var bww_items = 0;
var mtn_items = 0;
var bba_items = 0;

// Find the prices out for BWW...
for(dy = 0; dy < h.length; dy++) {
	var s = h[dy].options[h[dy].selectedIndex].text;
	
	if(s != "None") {
		p += calc_mag_price(s);
		bww_items++;
	}
}

// Now sort out the prices for MTN....
for(dy = 0; dy < k.length; dy++) {
	var s = k[dy].options[k[dy].selectedIndex].text;
	
	if(s != "None") {
		p += MTNprice(s);
		mtn_items++;
	}
}

// Next the bba prices....
for(dy = 0; dy < b.length; dy++) {
	var s = b[dy].options[b[dy].selectedIndex].text;
	
	if(s != "None") {
		p += BBAprice(s);
		bba_items++;
	}
}


// Sort out the post and packaging
pandp = postage(document.ordering.pandp.value);

pgp = (pandp[0] * bww_items) + (pandp[1] * mtn_items) + (pandp[2] * bba_items);

//pgp = document.ordering.pandp.value * p;

// And now, change the labels over	
setLabelText("m", p);
setLabelText("p", pgp);
setLabelText("g", p + pgp);
}

function msubmit(s) {
	if(s == 1) {
	document.ordering.action = 'bn_summary.php';
	}
	
	if(s == 2) {
	document.ordering.method = 'post';
	document.ordering.action = 'back_issues.php#b';
	}
	
	if(s == 3) {
	formppc();
	// https://www.paypal.com/cgi-bin/webscr
	document.ordering.action = 'https://www.paypal.com/cgi-bin/webscr';
	}
	
	if(s == 4) {
	document.ordering.action = 'back_issues.php';
	}
}

function postage(method) {
	// Update this fn id the PHP changes!
	pandp = new Array(2);
	// 0 = BWW / 1 = MTN / 2 = BBA
	
	switch(method) {
		case 'U':
		pandp[0] = 0;
		pandp[1] = 0;
		pandp[2] = 0;
		break;
		case 'E':
		pandp[0] = 1;
		pandp[1] = 1;
		pandp[2] = 1;
		break;
		case 'R':
		pandp[0] = 1.5;
		pandp[1] = 1.5;
		pandp[2] = 1.5;
		break;
		default:
		return false;
		break;
	}
	
	return pandp;
}

function formppc() {
	/* Funciton for forming up the PayPal controls */
	// Counter
	var y = 0;
	
	// BWW Issues
	var h = get_item("issue[]");
	var l = new Array(5);
	
	// MTN issues
	var m = get_item("missue[]");
	var n = new Array(5);
	
	// BBA Issues
	var f = get_item("bba[]");
	var d = new Array(2);
	
	// Drag the values off them
	for(i = 0; i < h.length; i++) {
		l[i] = h[i].value;
	}
	
	for(i = 0; i < n.length; i++) {
		n[i] = m[i].value;
	}
	
	for(i = 0; i < d.length; i++) {
		d[i] = f[i].value;
	}
	
	// Sort out the p and p!
	var pandp = postage(get_itemid("pandp").value);
	
	//paypal_item_control(item_no, title, quantity, amount, shipping)
		
	// Clear the secret element
	e_parent = get_itemid("ordering");
	e_child = get_itemid("s");
	e_parent.removeChild(e_child);

	// Now replace it
	e_parent = get_itemid("ordering");
	e_child = document.createElement("p");
	e_child.setAttribute("id","s");
	e_parent.appendChild(e_child);
	
	// Now append the PayPal controls instead
	// BWW First
	
	// Get the postage array first...
	
	for(i = 0; i < l.length; i++) {
		if(l[i] != "None") {
			y++;
			var c = calc_mag_price(l[i]);
			
			v = pandp[0];
			
			paypal_item_control(y, "BWW Issue "+ l[i], 1, c, v);
		}
	}
	
	// Now for MTN
	for(j = 0; j < n.length; j++) {
		if(n[j] != "None") {
			y++;
			var c = MTNprice(n[j]);
			
			v = pandp[1];
			
			paypal_item_control(y, "MTN Issue "+ n[j], 1, c, v);
		}
	}
	
	// And finally for BBA
	for(k = 0; k < d.length; k++) {
		if(d[k] != "None") {
			y++;
			var c = BBAprice(d[k]);
			
			v = pandp[2];
			
			paypal_item_control(y, "BBA Issue "+ d[k], 1, c, v);
		}
	}
}
