/*
Strip whitespace from the beginning and end of a string
Input : a string

function trim(str)

return str.replace(/^\s+|\s+$/g,'');
}
*/

String.prototype.trim = function() {
a = this.replace(/^\s+/, '');
return a.replace(/\s+$/, '');
};

/*
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 = formElement.value.trim();
	
	_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 checkAddMultiProdsToCart()
{
	with (window.document.frmAddMultiProds) {
		  submit();
	}
	
	
}
function checkQtyMultiples(mltp)
{
   
	with (window.document.frmAddMultiProds) {
		  var len = elements.length;
		  var i=0;
		  var errorFound = 0;
		  var qtyFound = 0;
		  var zeroFound = 0;
		  		  
		  for( i=0 ; i<len ; i++) {
		   
		   if (elements[i].name.substring(0,7) == "priceId") {
		    qtyFound = 1;
	        if (elements[i].value%mltp > 0) { 
		       errorFound++;
		    } 
		   
		    if (elements[i].value.length > 0) {
		       if (elements[i].value < 1 ) { 
		          zeroFound=1;
		        }
		     } 
		   }
		   
		 }
		 
		 if (errorFound > 0) {
  		 	alert('Quantity must be divisible by '+mltp);
		 } else if (qtyFound < 1) {
		    alert('You must enter a qty');
		 } else if (zeroFound > 0) {
		    alert('You must enter a qty greater than 0');
		 }
		 else {
		 	  submit();
		 }
	}
}

