/*
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;
}

/*
	Check if form element looks like a PO Box
	if True, display alert box and focus
*/
function isPOBox (formElement) {
	  var poregex = new RegExp("[pP]{1}[.]*[oO]{1}[.]*[ ]*[bB]{1}[oO]{1}[xX]{1}");
	 
	  _isPOBox = false;
      
	  if (formElement.value.match(poregex)   != null) {
         _isPOBox = true;
		 alert("Sorry, we do not ship to PO Boxes, please enter a street address.");
		 formElement.focus();
	 }
	 
     return _isPOBox;
}
/*
	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;
		}
	}	
}

/*
	Change image out for product detail page
*/
function changeimg(img) {
	document.imgMain.src=img;
}
function changeOption(id,img,selindex) {
	changeimg(img);
	document.frmAddCart.pd_option.selectedIndex = selindex;
	changeOptionText(id);
}
function changeOptionDD() {
		id = document.frmAddCart.pd_option.value;
		if(id != 0) {
			img = document.frmAddCart.pd_option.options[document.frmAddCart.pd_option.selectedIndex].title
			//alert(img);
			changeOptionText(id);
			changeimg(img);
		}
}
function changeOptionText(id) {
	old_opt = document.getElementById("disp_text").title;
	document.getElementById("disp_text").style.display = "none";
	document.getElementById("disp_text").id = old_opt;
	document.getElementById(id).style.display = "block";
	document.getElementById(id).id = "disp_text";
	document.getElementById("disp_text").title = id;	
}
/*product Detail Page - check to see that the form was filled in*/
function checkAddCartForm()
{
	with (window.document.frmAddCart) {
		if (isEmpty(pd_qty, 'Please input a quantity to purchase in the quantity field.')) {
			return false;
		} else if (isEmpty(pd_option, 'Please select your treatment from the drop down list.')) {
			return false;
		} else {
			btnAddToCart.value='Adding Item... Please Wait';
			submit();
		}
	}
}

