function isNull(x)
{
	return (typeof x == 'object' && !x);
}

function isFunction(x)
{
	return (typeof x == 'function');
}

function isObject(x)
{
	return (x && typeof x == 'object') || isFunction(x);
}

function isArray(x)
{
	return isObject(x) && x.constructor == Array;
}

function check_cart_add_form(theform)
{
	var i,f,v,e=null;
	for (i=1;f=document.getElementById('id_qty_'+i);++i)
	{
		v = f.value.match(/^\s*$/) ? 0 : parseInt(f.value);
		if (isNaN(v)||v<0)
		{
			// value is negative.
			if (!isArray(e))
			{
				e = Array(f);
			}
			else
			{
				e.push(f);
			}
			v = 0;
		}
		else if (v>0&&isNull(e))
		{
			// value is positive and we don't have any qualifiers, yet.
			e = true;
		}
		f.value = v;
	}
	if (isNull(e))
	{
		alert('You must specify at least one quantity to buy before clicking the "Add to Cart" button.');
	}
	else if (isArray(e))
	{
		if (e.length==1)
		{
			alert('You have entered an invalid quantity. It has been replaced with zero.');
		}
		else
		{
			alert('You have entered invalid quantities. They have been replaced with zero.');
		}
	}
	else
	{
		return true;
	}
	return false;
}
