

function defocus(x) {
	document.BuyForm.qty2.focus();
	}

// roundOff
// rounds off value to precision decimal places
function roundOff(value, precision) {
	return OKStrOfMN(value,1,precision);
	}


// OKStrOfMN
// rounds off X to N decimal places, ensuring there are at least M leading 0's
// http://www.merlyn.demon.co.uk/js-maths.htm
function OKStrOfMN(X, M, N) {
	var T, S=new String(Math.round(X*Number("1e"+N)))
	while (S.length<M+N) S='0'+S
	var y = S.substr(0, T=(S.length-N));
	if(N>0) {
		y += '.' + S.substr(T, N);
		}
	return y;
	}




function CheckNull(value) {
	if (value == "") {
		value = "0";
		}

	return value;
	}

function typeOfCarriage(x,whereabouts) {
	x.carriage_amount.value = whereabouts;
	}

function calculate(x) {
	basicprice = calc(x);

	if(Number(basicprice) > 0) {

		switch (x.carriage_amount.value) {
			case "uk" :
				x.postage_and_packaging.value = 2.50;
				break;
			}

		x.amount.value = Number(basicprice) + Number(x.postage_and_packaging.value);

		// Code to generate a dynamic description!
		x.desc.value = add_items(x);
		} else {
			x.amount.value = "0";
			}

	x.amount.value = roundOff(x.amount.value,2);

	}


function calc(x) {
	x.amount.value = 0;
	var y = x.price.length;
	var z = x.qty.length;
	var a = Number(x.amount.value);
	var b,c,d;
	d = true;
	while(y > 0) {
		b = Number(CheckNull(x.price[y-1].value));
		c = Number(CheckNull(x.qty[y-1].value));
		if(c < 0) {
			d = false;
			c = 0;
			x.qty[y-1].value = c;
			}
		a += (b * c);
		// alert("x.price["+eval(y-1)+"].value = "+x.price[y-1].value+"\nx.qty["+eval(y-1)+"].value = "+x.qty[y-1].value+"\na = "+a);
		y--;
		}
	if(d == false) {
		alert("Negative quantities not permitted; these have been set to zero.");
		}
	return a;
	}


function add_items(x) {
	var y = x.price.length;
	var z = x.code.length;
	var d = "";
	if(y != z) alert("Missing items in form; found "+y+" occurrences of price and "+z+" of code.");
	var c = 0;
	while (c < y) {
		if(Number(CheckNull(x.qty[c].value)) > 0) {
			d += add_item(d,x.qty[c].value,x.code[c].value);
			}
		c++;
		}
	return d;
	}

function add_item(d,q,c) {
	var r = "";
	if(d != "") {
		r += ", ";
		}
	r += q + " " + c;
	if(Number(CheckNull(q)) > 1) {
		r += "'s";
		}
	// alert("q = "+q+"\nc = "+c+"\n r = "+r);
	return r;
	}


function validate_form(x) {
	// here is where you may wish to include more sophisticated checking, particularly of the email address.
	var e = "";
	var r = true;
	var z,n,q,t;

	t = true;
	q = 0;
	n = 0;
	z = 0;

	// alert("x.qty.length = "+x.qty.length);

	while(z < x.qty.length) {
	
		// alert("z = "+z);
		if(Number(CheckNull(x.qty[z].value)) < 0) {
			n++;
			} else {
				q += Number(CheckNull(x.qty[z].value));
				}
		z++;
		}

	if(n > 0) {
		e += "\nYou have entered "+n+" negative quantit";
		if(n == 1) {
			e += "y";
			} else {
				e += "ies";
				}
		r = false;
		}

	if(q == 0) {
		e += "\nYou need to select at least one item";
		r = false;
		}

	if(x.name.value == "") {
		e += "\nPlease enter a name";
		r = false;
		}

	if(x.address.value == "") {
		e += "\nPlease enter an address";
		r = false;
		}
	if(x.country.value == "") {
		e += "\nPlease select a country";
		r = false;
		}
	if(x.postcode.value == "") {
		e += "\nPlease enter a postcode or ZIP code";
		r = false;
		}
	if(x.tel.value == "") {
		e += "\nPlease enter a telephone number";
		r = false;
		}
	if(x.fax.value == "") {
		e += "\nPlease enter a fax number";
		r = false;
		}
	if(x.email.value == "") {
		e += "\nPlease enter an email address";
		r = false;
		}
	if(r == false) {
		alert("The following must be completed first:\n"+e);
		}
	return r;
	}
