function getInnerText(theElement) {
	if(document.all) {
		return document.getElementById(theElement).innerText;
	} else {
		return document.getElementById(theElement).textContent;
	}
}

function setInnerText(theElement, value) {
	if(document.all) {
		theElement.innerText = value;
	} else {
		theElement.textContent = value;
	}
}

function getTotal()
{
	var theTotal = 0;
	for (var x = 1; x<=document.pageForm.productCount.value; x++)
	{

		var price = getInnerText('price' + x);
		if (price != '') {
			theTotal += parseFloat(price);
		}
	}
	theTotal = (Math.round(100 * theTotal) / 100).toFixed(2);
	setInnerText(document.getElementById('totalOrder'), theTotal);
}

function ticked(foodQty, tick, priceBox, poundBox, price)
{
	if (tick.checked)	
	{
		foodQty.value = '1';
		setInnerText(priceBox, (Math.round(100 * price) / 100).toFixed(2));
		poundBox.style.display = '';
	}
	else 
	{
		foodQty.value = '0';
		setInnerText(priceBox, '');
		poundBox.style.display = 'none';
	}
	getTotal();
	
}

function plus(foodQty, tick, priceBox, poundBox, price)
{
	if(isNumeric(foodQty.value)) foodQty.value++;
	else foodQty.value = 1;
	tick.checked = true;
	setInnerText(priceBox, (Math.round(100 * price * foodQty.value) / 100).toFixed(2));
	poundBox.style.display = '';
	getTotal();
}

function minus(foodQty, tick, priceBox, poundBox, price)
{
	if(isNumeric(foodQty.value) && foodQty.value != 0) foodQty.value--;
	else foodQty.value = 0;
	if (foodQty.value == 0)
	{
		tick.checked = false;
		setInnerText(priceBox, '');
		poundBox.style.display = 'none';
	}
	else 
	{
		setInnerText(priceBox, (Math.round(100 * price * foodQty.value) / 100).toFixed(2));
		poundBox.style.display = '';
	}
	getTotal();
}

function checkNumber(field, priceBox, poundBox, price)
{
	var newValue = '';
	if (!isNumeric(field.value))
	{
		for (var x=0; x<field.value.length; x++)
		{
			if(isNumeric(field.value(x))) newValue += field.value(x);
		}
		field.value = newValue;
	}
	if (field.value == '' || field.value == 0)
	{
		setInnerText(priceBox, '');
		poundBox.style.display = 'none';
	}
	else
	{
		setInnerText(priceBox, (Math.round(100 * price * field.value) / 100).toFixed(2));
		poundBox.style.display = '';
	}
	getTotal();
}


function validateForm(theForm) {
	if(!theForm.terms.checked) {
		alert("You must accept the Terms and Conditions");
		return false;
	}
	return true; 
}


