/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}

function applyCoupon() {
	document.frmCart.action = "cart.php?action=appcoupon";
	document.frmCart.submit();
} // function applyCoupon()

function checkAddToCart() {
	
	var fldOps = document.getElementsByName('txtopMandatory[]');
	var fldTxtIds = document.getElementsByName('txtopID[]');
	
	var valChecked = true;
	for (i=0; i<fldOps.length; i++) {
		var fldMan = fldOps[i].value;
		if (fldMan == 1) {
			var varopID = fldTxtIds[i].value;
			var fldMultiple = document.getElementById("hidMultipleFlag" + varopID);
			var fldTitle = document.getElementById("opTitle" + varopID);
			
			if (fldMultiple.value == 1) {
				var fldName = document.getElementsByName('txtop' + varopID + '[]');
				var opChecked = false;
				
				for (j=0; j<fldName.length; j++) {
					if (fldName[j].checked) {
						opChecked = true;
					}
				} // for (j=0; j<fldName.lenght; j++)
				
				if (!opChecked) {
					valChecked = false;
					alert("Please specify your choice for " + fldTitle.innerHTML);
					return false;
				}
			}
			else {
				var fldName = document.getElementById('txtop' + varopID);
				if (fldName.value == "") {
					valChecked = false;
					alert("Please specify your choice for " + fldTitle.innerHTML);
					return false;
				}
			}
		} // if (fldMan)
	} // for (i=0; i<fldOps.length; i++)
	
	if (valChecked) {
		return true;
		//document.frmAddToCart.submit();
	}
	
} // function execAddToCart()