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

<!-- The next two functions round numbers to numerical formatting. -->
function roundOff(value, precision)
{
	return places(value,1,precision);
}

function places(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;
}

<!-- This function checks for empty quantities. -->
function CheckNull(value)
{
	if (value == "")
	{
		value = "0";
	}

	return value;
}

<!-- This function defines the postage and packaging location. -->
function typeOfCarriage(x,whereabouts)
{

	x.carriage_amount.value = whereabouts;
}

<!-- This function addedsno postage and packaging to the total price of the products. To see an example of adding postage and packaging rates to this page see example 0.4 -->
function calculate_no(x)
{
	basicprice = calc(x);

	if(Number(basicprice) > 0)
	{


		x.amount.value = Number(basicprice);
		x.desc.value = add_items(x);
	}
	else
	{
		x.amount.value = "0";
	}

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

<!-- This function addeds the postage and packaging to the total price of the products. To see an example of adding postage and packaging rates to this page see example 0.4 -->
function calculate(x)
{
	basicprice = calc(x);

	if(Number(basicprice) > 0)
	{

		switch (x.carriage_amount.value)
		{
			case "none" :
				x.postage_and_packaging.value = 0.00;
				break
			case "de" :
				x.postage_and_packaging.value = 2.90;
				break
			case "europe" :
				x.postage_and_packaging.value = 5.90;
				break
			default :
				x.postage_and_packaging.value = 9.90;
				break;
		}

		x.amount.value = Number(basicprice) + Number(x.postage_and_packaging.value);
		x.desc.value = add_items(x);
	}
	else
	{
		x.amount.value = "0";
	}

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

<!-- The standard price, exluding postage and packaging is calculated here. -->
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);
		y--;
	}

	if(d == false)
	{
		alert("Negative Mengenangaben sind nicht erlaubt; diese wurden zurückgesetzt.");
	}

	return a;
}

<!-- These two functions add the items descriptions and prices, to be sent to the WorldPay secure server. -->
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";
	}

	return r;
}

<!-- This validates the details input by the customer. This is where you may wish to include more sophisticated checking in the 'if' arguments if you wish. -->
function validate_form(x)
{
	var e = "";
	var r = true;
	var z,n,q,t;

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

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

		z++;
	}

	if(n > 0)
	{
		e += "\nSie haben "+n+" negative Mengen eingegeben";
		if(n == 1)
		{
			e += "y.";
		}
		else
		{
			e += "ies.";
		}

		r = false;
	}

	if(q == 0)
	{
		e += "\nBitte wählen Sie mindestens ein Produkt aus.";
		r = false;
	}
	
	<!-- In these 'if' statements if the customer has not entered anything in the named field, then an error message will be displayed. -->
	if(x.name.value == "") 
	{
		e += "\nBitte geben Sie Ihren Namen ein.";
		r = false;
	}


	if(x.address.value == "") 
	{
		e += "\nBitte geben Sie Ihre Adresse ein.";
		r = false;
	}

	if(x.country.value == "") 
	{
		e += "\nBitte wählen Sie ein Land aus.";
		r = false;
	}

	if(x.postcode.value == "") 
	{
		e += "\nBitte geben Sie Ihre Postleitzahl ein.";
		r = false;
	}

	if(x.tel.value == "")
	{
		e += "\nBitte geben Sie Ihre Telefonnummer ein.";
		r = false;
	}

		if(x.email.value == "")
	{
		e += "\nBitte geben Sie Ihre Email-Adresse ein.";
		r = false;
	}

	if(r == false) 
	{
		alert("Bitte ergänzen Sie Ihre Angaben:\n"+e);
	}

	return r;
}
