// purchaseValidation.js
// ©2001 AEI Security Limited 
// author Daniel Gorst

// all price are multiplied by 100 because of stupid floating point rounding errors

var UK_Shipping = 499;			//Price for shipping to UK destinations in pounds
var US_Shipping = 1499;			//Price for shipping to worldwide destinations in US dollars
var EU_Shipping = 999;			//Price for shipping to european countries in euros

var priceSBB01UK = 3599;		//SBB-01 software price in pounds
var priceSBB01US = 4999;		//price in US dollars
var priceSBB01EU = 5499;		//price in EUROs

var priceSBB0104UK = 10999;		//SBB-01-04 software price in pounds
var priceSBB0104US = 15999;		//price in US dollars
var priceSBB0104EU = 17999;		//price in EUROs

var priceSBB0404UK = 16999;		//SBB-04-04 software price in pounds
var priceSBB0404US = 23999;		//price in US dollars
var priceSBB0404EU = 27499;		//price in EUROs

var priceSBB0404SPAUK = 21999;		//SBB-04-04SPA software price in pounds
var priceSBB0404SPAUS = 30999;		//price in US dollars
var priceSBB0404SPAEU = 35599;		//price in EUROs

var priceSBB0404SPBUK = 26999;		//SBB-04-04SPA software price in pounds
var priceSBB0404SPBUS = 37999;		//price in US dollars
var priceSBB0404SPBEU = 43999;		//price in EUROs

//set default for GBP

var isUKorder = true;
var isUSorder = false;
var isEUorder = false;
var currency = '£';
var netTotal = 0;
var postagePacking = 0;
var quantitySBB01 = 0;
var quantitySBB0104 = 0;
var quantitySBB0404 = 0;
var quantitySBB0404SPA = 0;
var quantitySBB0404SPB = 0;

//set list of countries which use the euro. special case is GB which uses pound. All others use dollar.

euroCountries = new Array("BE","BG","DK","FR","FX","DE","IE","IT","LU","NL","NO","PT","ES","SE","CH","TR","YU");

function update() {

	// Confirm the number of SBB-01s to be ordered

	if (isNaN(parseInt(document.OrderForm.C_SBB01_QUANTITY.value))) {
		window.alert("This must be a whole number");
	} else {
		quantitySBB01 = Math.abs(parseInt(document.OrderForm.C_SBB01_QUANTITY.value));
	}

	// Confirm the number of SBB-01-04s to be ordered

	if (isNaN(parseInt(document.OrderForm.C_SBB0104_QUANTITY.value))) {
		window.alert("This must be a whole number");
	} else {
		quantitySBB0104 = Math.abs(parseInt(document.OrderForm.C_SBB0104_QUANTITY.value));
	}
	
	// Confirm the number of SBB-04-04s to be ordered

	if (isNaN(parseInt(document.OrderForm.C_SBB0404_QUANTITY.value))) {
		window.alert("This must be a whole number");
	} else {
		quantitySBB0404 = Math.abs(parseInt(document.OrderForm.C_SBB0404_QUANTITY.value));
	}
	
	// Confirm the number of SBB-04-04SPAs to be ordered

	if (isNaN(parseInt(document.OrderForm.C_SBB0404SPA_QUANTITY.value))) {
		window.alert("This must be a whole number");
	} else {
		quantitySBB0404SPA = Math.abs(parseInt(document.OrderForm.C_SBB0404SPA_QUANTITY.value));
	}

	// Confirm the number of SBB-04-04SPBs to be ordered

	if (isNaN(parseInt(document.OrderForm.C_SBB0404SPB_QUANTITY.value))) {
		window.alert("This must be a whole number");
	} else {
		quantitySBB0404SPB = Math.abs(parseInt(document.OrderForm.C_SBB0404SPB_QUANTITY.value));
	}
	
	tempCountry = document.OrderForm.country.value;
	if (tempCountry == 'GB') {
		isUKorder = true;
		isUSorder = false;
		isEUorder = false;
	} else {
		isUKorder = false;
		isUSorder = true;
		isEUorder = false;
	}
	
	//Check for europian union countries and update currency if appropriate
	
	for(i=0;i<euroCountries.length;i++) {
		if (euroCountries[i] == tempCountry) {
			isEUorder = true;
			isUSorder = false;
			isUKorder = false;
			continue;
		}
	}
	
	//Set localisation parameters such as currency setting, shipping amounts and prices
	
	if (isUKorder) {
		priceSBB01 = priceSBB01UK;
		priceSBB0104 = priceSBB0104UK;
		priceSBB0404 = priceSBB0404UK;
		priceSBB0404SPA = priceSBB0404SPAUK;
		priceSBB0404SPB = priceSBB0404SPBUK;
		currency = '£';
		postagePacking = UK_Shipping;
		document.OrderForm.currency.value = 'GBP';
	}
	if (isUSorder) {
		priceSBB01 = priceSBB01US;
		priceSBB0104 = priceSBB0104US;
		priceSBB0404 = priceSBB0404US;
		priceSBB0404SPA = priceSBB0404SPAUS;
		priceSBB0404SPB = priceSBB0404SPBUS;
		currency = '$';
		postagePacking = US_Shipping;
		document.OrderForm.currency.value = 'USD';
	}
	if (isEUorder) {
		priceSBB01 = priceSBB01EU;
		priceSBB0104 = priceSBB0104EU;
		priceSBB0404 = priceSBB0404EU;
		priceSBB0404SPA = priceSBB0404SPAEU;
		priceSBB0404SPB = priceSBB0404SPBEU;
		currency = '';
		postagePacking = EU_Shipping;
		document.OrderForm.currency.value = 'EUR';
	}
	
	//Set shipping to 0 if there is no shipped products
		
	if ((quantitySBB0104 == 0)&&(quantitySBB0404 == 0)&&(quantitySBB0404SPA == 0)&&(quantitySBB0404SPB == 0)) {
		postagePacking = 0;
	}

	//Set totals
	
	netTotal = postagePacking + (quantitySBB01 * priceSBB01) + (quantitySBB0104 * priceSBB0104) + (quantitySBB0404 * priceSBB0404) + (quantitySBB0404SPA * priceSBB0404SPA) + (quantitySBB0404SPB * priceSBB0404SPB);
	
	tempTotal = netTotal/100;
	tempPandP = postagePacking/100;

	//Update the physical form and relevent hidden fields for WorldPay

	document.OrderForm.C_SBB01_QUANTITY.value = quantitySBB01;
	document.OrderForm.C_SBB0104_QUANTITY.value = quantitySBB0104;
	document.OrderForm.C_SBB0404_QUANTITY.value = quantitySBB0404;
	document.OrderForm.C_SBB0404SPA_QUANTITY.value = quantitySBB0404SPA;
	document.OrderForm.C_SBB0404SPB_QUANTITY.value = quantitySBB0404SPB;
	document.OrderForm.C_net_total.value = (currency + tempTotal + ' ' + document.OrderForm.currency.value);
	document.OrderForm.C_p_and_p.value = currency + (postagePacking/100) + ' ' + document.OrderForm.currency.value;
	
	document.OrderForm.desc.value = ("Stealth Big Brother: "+document.OrderForm.currency.value + " Order");
	
	document.OrderForm.amount.value = tempTotal;
	
	document.OrderForm.cartId.value = ("STEALTHORDER_SBB01x"+quantitySBB01+"_SBB0104x"+quantitySBB0104+"_SBB0404x"+quantitySBB0404+"_"+document.OrderForm.currency.value);
}





