var theUndefined;

function testOpt(theOpt, theName)
{
	if (theOpt.selectedIndex == 0 && theOpt.options[theOpt.selectedIndex].text.indexOf('Choose') != -1)
	{
		alert('Please choose a '+theName);
		return false;
	}
}

function removeItemFromBasket(f,i)
{
	var qty;

	with (f)
	{
		if (eval('f.QTY' + i))
		{
			qty = eval('f.QTY' + i);
			qty.value = "0";
		}
		else
		{
			return false;
		}
	}
	f.submit();
}
function removeItemFromQuote(f,i)
{
	with (f)
	{
		var qty = eval('f.QQTY' + i);

	}
	qty.value = "0";
	f.submit();
}
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"+i;
		var inputValue = eval('document.' + formName + '.' + inputName);
		inputValue.value = "0";
	}

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



function addtobasket(f)
{
	with (f)
	{
		var CountUp		= 0;
 		var hasInput	= false;

		var	prod 		= eval('f.PROD0');
		var price 		= eval('f.PRICE0');
		var qty 		= eval('f.QTY0');
		var	opt1 		= eval('f.OPT10');
		var	opt2 		= eval('f.OPT20');
		var	opt3 		= eval('f.OPT30');
		var	opt4 		= eval('f.OPT40');
		var	opt5 		= eval('f.OPT50');
		var	opt6 		= eval('f.OPT60');
		var	opt7 		= eval('f.OPT70');
		var	opt8 		= eval('f.OPT80');

		var	opt1Label	= eval('f.opt1Label');
		var	opt2Label	= eval('f.opt2Label');
		var	opt3Label	= eval('f.opt3Label');
		var	opt4Label	= eval('f.opt4Label');
		var	opt5Label	= eval('f.opt5Label');
		var	opt6Label	= eval('f.opt6Label');
		var	opt7Label	= eval('f.opt7Label');
		var	opt8Label	= eval('f.opt8Label');

		if (opt1 && opt1Label) { if (testOpt(opt1, opt1Label.value) == false) return false; }
		if (opt2 && opt2Label) { if (testOpt(opt2, opt2Label.value) == false) return false; }
		if (opt3 && opt3Label) { if (testOpt(opt3, opt3Label.value) == false) return false; }
		if (opt4 && opt4Label) { if (testOpt(opt4, opt4Label.value) == false) return false; }
		if (opt5 && opt5Label) { if (testOpt(opt5, opt5Label.value) == false) return false; }
		if (opt6 && opt6Label) { if (testOpt(opt6, opt6Label.value) == false) return false; }
		if (opt7 && opt7Label) { if (testOpt(opt7, opt7Label.value) == false) return false; }
		if (opt8 && opt8Label) { if (testOpt(opt8, opt8Label.value) == false) return false; }

		if (qty.value.length > 0)
		{
			if (isNaN(qty.value/2))
			{
				window.alert('One or more of the fields contains invalid characters');
				return false;
			}
		}
		else
		{
			alert('Please choose a quantity for product ID:'+prod.value);
			return false;
		}

		CountUp += (qty.value - 0);

		if (qty.value < 0)
		{
			alert('Surprisingly you cannot purchase a negative number of items');
			return false;
		}
	}
	if (CountUp == 0)
	{
		window.alert('You must select more than 0 items');
		return false;
	}

	if (ADDBASKETTYPE == "to basket")
	{
		return true;
	}
	else
	{
		var optFields = "";

		if (opt1) optFields +="&OPT10="+escape(opt1.value)
		if (opt2) optFields +="&OPT20="+escape(opt2.value)
		if (opt3) optFields +="&OPT30="+escape(opt3.value)
		if (opt4) optFields +="&OPT40="+escape(opt4.value)
		if (opt5) optFields +="&OPT50="+escape(opt5.value)
		if (opt6) optFields +="&OPT60="+escape(opt6.value)
		if (opt7) optFields +="&OPT70="+escape(opt7.value)
		if (opt8) optFields +="&OPT80="+escape(opt8.value)

		$.ajax({
			type: "POST",
			url: website+"product/addBasket.php",
			data: "productcount=1&PROD0="+escape(prod.value)+"&PRICE0="+price.value+"&QTY0="+qty.value+optFields,
			success: function(shoppingCartHTML)
			{
	   			var addedToBasketForm		= $("#"+f.id);
				var addedToBasketStatus		= addedToBasketForm.position();
				var subTotal			   	= Math.round(parseFloat(qty.value)*parseFloat(price.value)*100)/100;

				subTotal 					+= "";
				subTotalArray 				= subTotal.split('.');

				if (subTotalArray.length > 1)
				{
					if (subTotalArray[1].length == 1) subTotal += "0";
				}
				else
				{
					subTotal += ".00";
				}

			  	$("#addedToBasketStatus").text("You have added "+qty.value+" item"+((qty.value != 1) ? "s" : "")+" to your basket to the value of $"+subTotal);
				$("#addedToBasketStatus").css("top", addedToBasketStatus.top+"px");
				$("#addedToBasketStatus").show("fast").delay(2500).fadeOut("slow");

				html = shoppingCartHTML.split('|');

			  	$("#rightShoppingCart").html(html[0]+' | '+html[1]);
			  	$("#basketLoadCheckout").html(html[2]);
			}
		});
		return false;
	}
}


function updatebasket(f, i)
{
	with (f)
	{
		var CountUp		= 0;
		var qty 		= 0;
		var hasInput	= false;
		var qty 		= eval('f.QTY'+i);
		var	opt1 		= eval('f.OPT1'+i);
		var	opt2 		= eval('f.OPT2'+i);
		var	opt3 		= eval('f.OPT3'+i);
		var	opt4 		= eval('f.OPT4'+i);
		var	opt5 		= eval('f.OPT5'+i);
		var	opt6 		= eval('f.OPT6'+i);
		var	opt7 		= eval('f.OPT7'+i);
		var	opt8 		= eval('f.OPT8'+i);

		var	opt1Label	= eval('f.opt1'+i+'Label');
		var	opt2Label	= eval('f.opt2'+i+'Label');
		var	opt3Label	= eval('f.opt3'+i+'Label');
		var	opt4Label	= eval('f.opt4'+i+'Label');
		var	opt5Label	= eval('f.opt5'+i+'Label');
		var	opt6Label	= eval('f.opt6'+i+'Label');
		var	opt7Label	= eval('f.opt7'+i+'Label');
		var	opt8Label	= eval('f.opt8'+i+'Label');

		var	prod 		= eval('f.PROD'+i);
		var qtyTagType  = document.getElementById("QTY0");

		if (opt1 && opt1Label) { if (testOpt(opt1, opt1Label.value) == false) return false; }
		if (opt2 && opt2Label) { if (testOpt(opt2, opt2Label.value) == false) return false; }
		if (opt3 && opt3Label) { if (testOpt(opt3, opt3Label.value) == false) return false; }
		if (opt4 && opt4Label) { if (testOpt(opt4, opt4Label.value) == false) return false; }
		if (opt5 && opt5Label) { if (testOpt(opt5, opt5Label.value) == false) return false; }
		if (opt6 && opt6Label) { if (testOpt(opt6, opt6Label.value) == false) return false; }
		if (opt7 && opt7Label) { if (testOpt(opt7, opt7Label.value) == false) return false; }
		if (opt8 && opt8Label) { if (testOpt(opt8, opt8Label.value) == false) return false; }

		if (qtyTagType)
		{
			if (qtyTagType.tagName == "INPUT")  	qty = eval('f.QTY'+i);
			if (qtyTagType.tagName == "SELECT")  	qty = eval('f.QTY'+i+'.options[f.QTY'+i+'.selectedIndex]');
		}

		if (qty.value.length > 0)
		{
			if (isNaN(qty.value/2))
			{
				window.alert('One or more of the fields contains invalid characters');
				return false;
			}
		}
		else
		{
			alert('Please choose a quantity for product ID:'+prod.value);
			return false;
		}

		CountUp += (qty.value - 0);

		if (qty.value < 0)
		{
			alert('Surprisingly you cannot purchase a negative number of items');
			return false;
		}
	}
	if (CountUp == 0)
	{
		window.alert('You must select more than 0 items');
		return false;
	}
}
