function validQty(q) {	

	if (q.length == undefined){
		v = q.value;
		if (!isValidQuantity(v)) {
			alert("Please enter a valid quantity!")
			q.focus();
			return false;
		}
	}
	else{
		for (var x = 0;x < q.length; x++)
		{
			v = q[x].value;
			if (!isValidQuantity(v)) {
			alert("Please enter a valid quantity!")
			q[x].focus();
			return false;
			}
		}		
	}
	var numitems;
	numitems = parseInt(document.basketitems.NumOfItems.value) + parseInt(q.value)
	if(numitems > 600){
		alert('The basket is limited to 600 items');
		return false;
	}
}

function validUpdateQty(q) {
	var t = 0;
	var u = 0;	

	if (q.productcode.length == undefined)
	{
			t = eval('q.OriginalQuantity1.value');
			u = eval('q.quantity1.value');
			if (!isValidQuantityForUpdate(t,u))
			{
				alert("Please enter a valid quantity!");
				q.quantity1.focus();
				return false;
			}
		
	}
	else
	{

		for (var x = 1;x < q.productcode.length + 1; x++)
		{
			t = eval('q.OriginalQuantity' + x + '.value');
			u = eval('q.quantity' + x + '.value');
				if (!isValidQuantityForUpdate(t,u)) 
				{
					alert("Please enter a valid quantity!");
					eval('q.quantity' + x + '.focus()');
					return false;
				}
		}

	}
}

function isValidQuantity(quantity){
    if(isNaN(quantity.replace(/ /g,"")) || quantity=='' || quantity=='0' || quantity< 0 || quantity> 600){
        return false;
    } else {
        return true;
    }
}

function isValidQuantityForUpdate(oldquantity,quantity){
    if(isNaN(quantity.replace(/ /g,"")) || quantity=='' || quantity=='0' || quantity< 0 || quantity> 600){
      return false;
    } else {
				var oldnumitems, numitems;
				oldnumitems = parseInt(document.basketitems.NumOfItems.value) - parseInt(oldquantity)
				numitems = parseInt(oldnumitems) + parseInt(quantity)
				if(numitems > 600){
					alert('The basket is limited to 600 items');
					return false;
				} else {
        	return true;
        }
    }
}
