var transactions;
var average_value;
var monthly_amount;

function calculate_cost()
	{
	companies = new Array();
	companies['Skypay'] = new Array();
	companies['Protx'] = new Array();
	companies['Paypal'] = new Array();
	companies['Worldpay'] = new Array();
	companies['PayPoint'] = new Array();
	companies['Google_Checkout'] = new Array();
	companies['ePDQ'] = new Array();
	companies['SecureTrading'] = new Array();
	companies['NetBanx'] = new Array();
	companies['Nochex'] = new Array();

	transactions = document.getElementById('transactions').value;
	average_value = document.getElementById('value').value;
	monthly_amount = transactions * average_value;

	// Calculate cost for Skypay.
	companies['Skypay']['setup'] = 0;
	companies['Skypay']['plan'] = "Standard";
	companies['Skypay']['monthly'] = 19.5;
	companies['Skypay']['transaction'] = 0.099;
	companies['Skypay']['free_transaction'] = 1000 / 3;
	companies['Skypay']['total'] = calculate_total(companies['Skypay']);
	companies['Skypay']['free_transaction'] = "1000 per quarter";
	companies['Skypay']['notes'] = "";
	companies['Skypay']['transaction'] = format_as_pence(companies['Skypay']['transaction'] * 100);
	cheapest = companies['Skypay']['total'];

	// Calculate cost for Protx.
	companies['Protx']['setup'] = 0;
	companies['Protx']['free_transaction'] = 0;
	if (transactions > 333)
		{
		companies['Protx']['plan'] = "Corporate Business Service";
		companies['Protx']['monthly'] = 0;
		companies['Protx']['transaction'] = 0.1;
		companies['Protx']['notes'] = "";
		}
	else
		{
		companies['Protx']['plan'] = "Small Business Service";
		companies['Protx']['monthly'] = 20;
		companies['Protx']['transaction'] = 0;
		companies['Protx']['notes'] = "Maximum 1,000 transactions per quarter";
		}
	companies['Protx']['total'] = calculate_total(companies['Protx']);
	companies['Protx']['free_transaction'] = (companies['Protx']['free_transaction'] > 0?companies['Protx']['free_transaction'] + " per month":"None");
	companies['Protx']['transaction'] = format_as_pence(companies['Protx']['transaction'] * 100);
	cheapest = Math.min(cheapest, companies['Protx']['total']);

	// Calculate cost for Paypal.
	companies['Paypal']['setup'] = 0;
	companies['Paypal']['plan'] = "Pro";
	companies['Paypal']['monthly'] = 0;
	paypal_rate = 0.034;
	if (monthly_amount > 1500)
		paypal_rate = 0.029;
	if (monthly_amount > 6000)
		paypal_rate = 0.024;
	if (monthly_amount > 15000)
		paypal_rate = 0.019;
	if (monthly_amount > 55000)
		paypal_rate = 0.014;

	companies['Paypal']['transaction'] = average_value * paypal_rate + 0.2;
	companies['Paypal']['free_transaction'] = 0;
	companies['Paypal']['total'] = calculate_total(companies['Paypal']);
	companies['Paypal']['free_transaction'] = "None";
	companies['Paypal']['notes'] = "above £50,000.00 through Express Checkout only";
	companies['Paypal']['transaction'] = format_as_pence(companies['Paypal']['transaction'] * 100);
	cheapest = Math.min(cheapest, companies['Paypal']['total']);

	// Calculate cost for Worldpay.
	companies['Worldpay']['setup'] = 200;
	companies['Worldpay']['plan'] = "Small/Medium Business";
	companies['Worldpay']['monthly'] = 30;
	companies['Worldpay']['transaction'] = Math.min(0.5,average_value * 0.045);
	companies['Worldpay']['free_transaction'] = 0;
	companies['Worldpay']['total'] = calculate_total(companies['Worldpay']);
	companies['Worldpay']['free_transaction'] = "None";
	companies['Worldpay']['notes'] = "One free currency provided, £50 per additional currency thereafter";
	companies['Worldpay']['transaction'] = format_as_pence(companies['Worldpay']['transaction'] * 100);
	cheapest = Math.min(cheapest, companies['Worldpay']['total']);

	// Calculate cost for PayPoint.
	companies['PayPoint']['setup'] = 0;
	companies['PayPoint']['plan'] = "Lite 10";
	companies['PayPoint']['monthly'] = 10;
	companies['PayPoint']['transaction'] = 0.39;
	companies['PayPoint']['free_transaction'] = 100;
	if (transactions > 125)
		{
		companies['PayPoint']['plan'] = "Lite 20";
		companies['PayPoint']['monthly'] = 20;
		companies['PayPoint']['transaction'] = 0.20;
		companies['PayPoint']['free_transaction'] = 200;
		}
	if (transactions > 600)
		{
		companies['PayPoint']['plan'] = "Lite 100";
		companies['PayPoint']['monthly'] = 100;
		companies['PayPoint']['transaction'] = 0.10;
		companies['PayPoint']['free_transaction'] = 1000;
		}
	companies['PayPoint']['total'] = calculate_total(companies['PayPoint']);
	companies['PayPoint']['free_transaction'] += " per month";
	companies['PayPoint']['notes'] = "";
	companies['PayPoint']['transaction'] = format_as_pence(companies['PayPoint']['transaction'] * 100);
	cheapest = Math.min(cheapest, companies['PayPoint']['total']);

	// Calculate cost for Google_Checkout.
	companies['Google_Checkout']['setup'] = 0;
	companies['Google_Checkout']['plan'] = "Standard";
	companies['Google_Checkout']['monthly'] = 0;
	companies['Google_Checkout']['transaction'] = average_value * 0.015 + 0.15;
	companies['Google_Checkout']['free_transaction'] = 0;
	companies['Google_Checkout']['total'] = calculate_total(companies['Google_Checkout']);
	companies['Google_Checkout']['free_transaction'] = "None";
	companies['Google_Checkout']['notes'] = "For every £1 spent on AdWords, get £10 of free transactions";
	companies['Google_Checkout']['transaction'] = format_as_pence(companies['Google_Checkout']['transaction'] * 100);
	cheapest = Math.min(cheapest, companies['Google_Checkout']['total']);

	// Calculate cost for ePDQ.
	companies['ePDQ']['setup'] = 0;
	companies['ePDQ']['plan'] = "Standard";
	companies['ePDQ']['monthly'] = 25;
	companies['ePDQ']['transaction'] = average_value * 0.02;
	if (monthly_amount > 10000)
		companies['ePDQ']['transaction'] = average_value * 0.01;
	companies['ePDQ']['free_transaction'] = 0;
	companies['ePDQ']['total'] = calculate_total(companies['ePDQ']);
	companies['ePDQ']['free_transaction'] = "None";
	companies['ePDQ']['notes'] = "";
	companies['ePDQ']['transaction'] = format_as_pence(companies['ePDQ']['transaction'] * 100);
	cheapest = Math.min(cheapest, companies['ePDQ']['total']);

	// Calculate cost for SecureTrading.
	companies['SecureTrading']['setup'] = 200;
	companies['SecureTrading']['plan'] = "Standard";
	companies['SecureTrading']['monthly'] = 15;
	companies['SecureTrading']['transaction'] = 0.015 * average_value;
	companies['SecureTrading']['free_transaction'] = 0;
	companies['SecureTrading']['total'] = calculate_total(companies['SecureTrading']);
	companies['SecureTrading']['free_transaction'] = "None";
	companies['SecureTrading']['notes'] = "";
	companies['SecureTrading']['transaction'] = format_as_pence(companies['SecureTrading']['transaction'] * 100);
	cheapest = Math.min(cheapest, companies['SecureTrading']['total']);

	// Calculate cost for NetBanx.
	companies['NetBanx']['setup'] = 0;
	companies['NetBanx']['plan'] = "Merchant Account";
	companies['NetBanx']['monthly'] = 0;
	companies['NetBanx']['transaction'] = 0.1;
	companies['NetBanx']['free_transaction'] = 0;
	companies['NetBanx']['total'] = calculate_total(companies['NetBanx']);
	companies['NetBanx']['free_transaction'] = "None";
	companies['NetBanx']['notes'] = "";
	companies['NetBanx']['transaction'] = format_as_pence(companies['NetBanx']['transaction'] * 100);
	cheapest = Math.min(cheapest, companies['NetBanx']['total']);

	// Calculate cost for Nochex.
	if (average_value <= 100)
		{
		companies['Nochex']['plan'] = "Seller Account";
		companies['Nochex']['setup'] = 0;
		companies['Nochex']['notes'] = "£100 maxiumum transaction value, UK payments only";
		}
	else
		{
		companies['Nochex']['plan'] = "Merchant Account";
		companies['Nochex']['setup'] = 50;
		companies['Nochex']['notes'] = "";
		}
	companies['Nochex']['monthly'] = 0;
	companies['Nochex']['transaction'] = average_value * 0.029 + 0.2;
	companies['Nochex']['free_transaction'] = 0;
	companies['Nochex']['total'] = calculate_total(companies['Nochex']);
	companies['Nochex']['free_transaction'] = "None";
	companies['Nochex']['transaction'] = format_as_pence(companies['Nochex']['transaction'] * 100);
	cheapest = Math.min(cheapest, companies['Nochex']['total']);

	for (var company in companies)
		{
		for (var attribute in companies[company])
			document.getElementById(company + "_" + attribute).innerHTML = format_as_pounds(check_defined(companies[company][attribute]));
		document.getElementById(company + "_total").className = (companies[company]['total'] == cheapest?"cheapest":"");
		}
	}

function calculate_total(company)
	{
	return (company['setup'] * 1 + 12 * (company['monthly'] + Math.max(0,transactions - company['free_transaction']) * company['transaction']));
	}

function format_as_pounds(amount)
	{
	if(isNaN(amount) || amount === "") { return(amount); }
	var i = parseFloat(amount);
	i = parseInt((i + .0005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	return "£" + s;
	}

function format_as_pence(amount)
	{
	if(isNaN(amount) || amount === "") { return(amount); }
	var i = parseFloat(amount);
	i = Math.round(i * 10)/10	
	return i + "p";
	}

function Comma(number)
	{
	number = '' + number;
	if (number.length > 3)
		{
		var mod = number.length % 3;
		var output = (mod > 0 ? (number.substring(0,mod)) : '');
		for (i=0 ; i < Math.floor(number.length / 3); i++)
			{
			if (((mod == 0) && (i == 0)) || i > Math.floor(number.length / 3) - 2)
				output += number.substring(mod+ 3 * i, mod + 3 * i + 3);
			else
				output+= ',' + number.substring(mod + 3 * i, mod + 3 * i + 3);
			}
		return (output);
		}
	return number;
	}

function check_defined(string)
	{
	return (string == undefined?"&nbsp;":string);
	}