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

/*
	Add to cart
*/

function addToCart(url){ 
	payment_method = document.getElementById("payment_method").selectedIndex;
	payment_method = document.getElementById("payment_method").options[payment_method].value;
	
	size = document.getElementById("item_size").selectedIndex;
	size = document.getElementById("item_size").options[size].value;
	
	url += "&pm="+payment_method+"&size="+size;
	window.location.href=url;
	
}
/*Simple SlideShow*/
function slide(action){
    var $active = $('#product_picture img.active');


	if(action == "next"){
		if ( $active.length == 0 ) $active = $('#product_picture img:last');
		var $next =  $active.next().length ? $active.next()
			: $('#product_picture img:first');
	
		$next.removeClass('noactive');
		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 1000, function() {
				$active.removeClass('active last-active');
			});
	}else{
		if ( $active.length == 0 ) $active = $('#product_picture img:first');
		var $prev =  $active.prev().length ? $active.prev()
			: $('#product_picture img:last');
	
		$prev.removeClass('noactive');
		$prev.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 1000, function() {
				$active.removeClass('active last-active');
			});
	}

    $active.removeClass('active');
	$active.addClass('noactive');
	
}

