var theUndefined;
// used for evaluating if arguments exist. Can't use 'if (!arguments[x])' because
// if arguments[x] exists but it's value is '0', then 'if (!arguments[x])' will
// evaluate to true. Such are the ways of javascript

function CheckIt(formName) {

	if (arguments[0]==theUndefined)
	{
		formName = document.forms[0].name;
	}
	var	productCount	= eval('document.' + formName + '.productcount');

	if (viewBasketQtyCheck(productCount.value) == false) return false;
}

function viewCartFormCheckIt()
{
	var	productCount	= eval('document.viewCartForm.productcount');

	if (viewCartFormQtyCheck(productCount.value) == false) return false;
}

function emptyBasket(formName)
{
	if (arguments[0]==theUndefined)
	{
		formName = document.forms[0].name;
	}
	var	productCount	= eval('document.' + formName + '.productcount');

	for (var i = 0; i < productCount.value; i++) {

		var inputName = "QTY";
		if (i < 10)		inputName += "0";
		if (i < 100)	inputName += "0";
		inputName		+= i;

		var inputValue = eval('document.' + formName + '.' + inputName);

		inputValue.value = "0";
	}

	eval('document.'+formName).submit();
}

function emptyBasketSelectQty(formName)
{
	if (arguments[0]==theUndefined)
	{
		formName = document.forms[0].name;
	}
	var	productCount	= eval('document.' + formName + '.productcount');

	for (var i = 0; i < productCount.value; i++) {

		var inputName = "QTY";
		if (i < 10)		inputName += "0";
		if (i < 100)	inputName += "0";
		inputName		+= i;

		var inputValue = eval('document.' + formName + '.' + inputName);

		inputValue.selectedIndex = 0;
	}

	eval('document.'+formName).submit();
}

function removeFromBasket(i, formName)
{
	if (arguments[0]==theUndefined)
	{
		i = 1;
	}
	if (arguments[1]==theUndefined)
	{
		formName = document.forms[0].name;
	}
	var	productCount	= eval('document.' + formName + '.productcount');

	var inputName = "QTY";
	if (i < 10)		inputName += "0";
	if (i < 100)	inputName += "0";
	inputName		+= i;

	var inputValue = eval('document.' + formName + '.' + inputName);

	inputValue.value = "0";

	eval('document.'+formName).submit();
}

function removeItemFromBasket(i)
{
	var	productCount	= eval('document.viewCartForm.productcount');

	var inputName	= "QTY";
	if (i < 10)		inputName += "0";
	if (i < 100)	inputName += "0";
	inputName		+= i;

	var inputValue = eval('document.viewCartForm.' + inputName);

	inputValue.value = "0";

	eval('document.viewCartForm').submit();

}
function removeItemFromPHPBasket(i)
{
	eval('document.viewCartForm.QTY' + i).value = "0";
//	alert('qty: '+eval('document.viewCartForm.QTY' + i).value);
	eval('document.viewCartForm').submit();
}
function removeSelectItemFromBasket(i)
{
	// var	productCount	= eval('document.viewCartForm.productcount');

	var inputName	= "QTY";
	if (i < 10)		inputName += "0";
	if (i < 100)	inputName += "0";
	inputName		+= i;

	var inputValue = eval('document.viewCartForm.' + inputName);

	inputValue.selectedIndex = 0;

	eval('document.viewCartForm').submit();
}

function CheckForm(formName)
{
	if (arguments[0]==theUndefined)
	{
		formName = document.forms[0].name;
	}
	var	productCount	= eval('document.' + formName + '.productcount');

	var	opt1Label		= eval('document.' + formName + '.opt1Label');
	var	opt2Label		= eval('document.' + formName + '.opt2Label');
	var	opt3Label		= eval('document.' + formName + '.opt3Label');
	var	opt4Label		= eval('document.' + formName + '.opt4Label');
	var	opt5Label		= eval('document.' + formName + '.opt5Label');
	var	opt6Label		= eval('document.' + formName + '.opt6Label');

	var CountUp			= 0;
	var validCharacters = "0123456789.";

	for (var n = 0; n < productCount.value; n++) {

		var textBit			= "";
		if (n < 10)			textBit += "0";
		if (n < 100)		textBit += "0";
		textBit				+= n;

		var stringContent	= eval('document.' + formName + '.QTY' + textBit + '.value');

		var	opt1			= eval('document.' + formName + '.OPT1' + textBit);
		var	opt2			= eval('document.' + formName + '.OPT2' + textBit);
		var	opt3			= eval('document.' + formName + '.OPT3' + textBit);
		var	opt4			= eval('document.' + formName + '.OPT4' + textBit);
		var	opt5			= eval('document.' + formName + '.OPT5' + textBit);
		var	opt6			= eval('document.' + formName + '.OPT6' + textBit);

		var	prod			= eval('document.' + formName + '.PROD' + textBit);

		CountUp += (stringContent - 0);

		var j = stringContent.length;

		if (stringContent != "") {

			for (i = 0; i < j; i++) {

				var thisCharacter = stringContent.charAt(i);
				if (validCharacters.indexOf(thisCharacter) == -1) {
					window.alert('One or more of the fields contains invalid characters');
					return false;
				}
			}
			if (opt1) {
				if (stringContent == "0" && opt1.selectedIndex > 0) {
					alert('Please choose a quantity for product ID:'+prod.value);
					return false;
				}
			}
			if (opt2) {
				if (stringContent == "0" && opt1.selectedIndex > 0) {
					alert('Please choose a quantity for product ID:'+prod.value);
					return false;
				}
			}
			if (opt3) {
				if (stringContent === "0" && opt3.selectedIndex > 0) {
					alert('Please choose a quantity for product ID:'+prod.value);
					return false;
				}
			}
			if (opt4) {
				if (stringContent == "0" && opt4.selectedIndex > 0) {
					alert('Please choose a quantity for product ID:'+prod.value);
					return false;
				}
			}
			if (opt5) {
				if (stringContent == "0" && opt5.selectedIndex > 0) {
					alert('Please choose a quantity for product ID:'+prod.value);
					return false;
				}
			}
			if (opt6) {
				if (stringContent == "0" && opt6.selectedIndex > 0) {
					alert('Please choose a quantity for product ID:'+prod.value);
					return false;
				}
			}
		}

		if (opt1) {
			if (stringContent != "0" && opt1.selectedIndex == 0) {
				alert('Please choose a '+opt1Label.value+' for product ID:'+prod.value);
				return false;
			}
		}
		if (opt2) {
			if (stringContent != "0" && opt2.selectedIndex == 0) {
				alert('Please choose a '+opt2Label.value+' for product ID:'+prod.value);
				return false;
			}
		}
		if (opt3) {
			if (stringContent != "0" && opt3.selectedIndex == 0) {
				alert('Please choose a '+opt3Label.value+' for product ID:'+prod.value);
				return false;
			}
		}
		if (opt4) {
			if (stringContent != "0" && opt4.selectedIndex == 0) {
				alert('Please choose a '+opt4Label.value+' for product ID:'+prod.value);
				return false;
			}
		}
		if (opt5) {
			if (stringContent != "0" && opt5.selectedIndex == 0) {
				alert('Please choose a '+opt5Label.value+' for product ID:'+prod.value);
				return false;
			}
		}
		if (opt6) {
			if (stringContent != "0" && opt6.selectedIndex == 0) {
				alert('Please choose a '+opt6Label.value+' for product ID:'+prod.value);
				return false;
			}
		}

	}
	if (CountUp == 0) {
		window.alert('You must select more than 0 items to add to your cart');
		return false;
	}
}

