// AutoDirector purchase form
var ap = {};

ap.Conf = {
	Form: "purchase",
	Ctype: "currency",
	Ctotal: "total",
	PPname: "item_name",
	PPitem: "item_number",
	PPcode: "currency_code",
	PPtotal: "amount",
	UKnote: "uknote",
	Def: "US",
	NameNoInstall: "AutoDirector licence",
	NameInstall: "AutoDirector installation and licence",
	Loc: {
		US: { Cur: "USD", Sym: "$", Name: "US Dollars", Price: 99, TaxP: 0, TaxB: 0 },
		GB: { Cur: "GBP", Sym: "\u00a3", Name: "UK Pounds", Price: 59, TaxP: 17.5, TaxB: 17.5 },
		EU: { Cur: "EUR", Sym: "\u20ac", Name: "Euros", Price: 75, TaxP: 17.5, TaxB: 0 },
		CA: { Cur: "CAD", Sym: "CA$", Name: "Canadian Dollars", Price: 99, TaxP: 0, TaxB: 0 },
		AU: { Cur: "AUD", Sym: "AU$", Name: "Australian Dollars", Price: 99, TaxP: 0, TaxB: 0 },
		NZ: { Cur: "NZD", Sym: "NZ$", Name: "New Zealand Dollars", Price: 125, TaxP: 0, TaxB: 0 }
	},
	Install: "install",
	InstallRate: 0.665
};

// get element
function $(id) { return document.getElementById(id); }

ap.Purchase = function() {

	var $C = ap.Conf;
	var Pform, Ctype, Ctotal, PPname, PPitem, PPcode, PPtotal, UKnote, Install, InstallRate;
	
	// initialise
	function Init() {
		Pform = $($C.Form);
		Ctype = $($C.Ctype).firstChild;
		Ctotal = $($C.Ctotal).firstChild;
		PPname = $($C.PPname);
		PPitem = $($C.PPitem);
		PPcode = $($C.PPcode);
		PPtotal = $($C.PPtotal);
		UKnote = $($C.UKnote);
		Install = $($C.Install);
		InstallRate = $C.InstallRate;
		if (Pform && Ctype && Ctotal && PPitem && PPcode && PPtotal && UKnote && Install) Update(true);
	}
	
	// update
	function Update(addevents) {
		UKnote.style.display = "none";
		if (addevents) Install.onclick = Update;
		var inp = Pform.elements;
		for (var i=0, inpl = inp.length; i < inpl; i++) {
			if (inp[i].type == "radio") {
				if (addevents) inp[i].onclick = CurrencyChange;
				if (inp[i].checked) CurrencyChange(null, inp[i]);
			}
		}
	}
	
	// change currency
	function CurrencyChange(e, target) {
		var t;
		if (target) t = target;
		else {
			e = (e ? e : window.event);
			var t = (e.target ? e.target : e.srcElement);
		}
		if (t.checked) {
			var c = (t.value ? t.value : $C.Def);
			if (typeof $C.Loc[c] != "undefined") {
				var cs = $C.Loc[c];
				// installation
				var price = cs.Price + (Install.checked ? Math.ceil(cs.Price*InstallRate) : 0);
				// update form
				PPname.value = (Install.checked ? $C.NameInstall : $C.NameNoInstall);
				PPitem.value = "AD_"+(Install.checked ? "I_" : "")+cs.Cur+price;
				PPcode.value = cs.Cur;
				PPtotal.value = price;
				// update screen
				Ctype.nodeValue = cs.Name;
				Ctotal.nodeValue = cs.Sym+" "+price;
				UKnote.style.display = (t.value == "GB" ? "block" : "none");
			}
		}
	}

	return { Init: Init };
	
}();

if ($(ap.Conf.Form)) ap.Purchase.Init();
