/*
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;
}

function echeckfail(epost, message) {

	str = trim(epost.value);
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	var fail=false;
	if (str.indexOf(at)==-1){
		ok=false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		fail=true;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		fail=true;
	}

	if (str.indexOf(at,(lat+1))!=-1){
		fail=true;
	}

	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		fail=true;
	}

	if (str.indexOf(dot,(lat+2))==-1){
		fail=true;
	}
		
	if (str.indexOf(" ")!=-1){
		fail=true;
	}

 	if (fail==true) {
 	 	alert(message);
		epost.focus();
 	}
	return fail;				
}

/*
	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 visPris()
{
	var temp = new Array();
	if (form1.storrelse.selectedIndex==0)
	{
		form1.pris.value='';
		form1.total.value=''
	}
	else
	{
		str = form1.storrelse[form1.storrelse.selectedIndex].value;
        	temp = str.split(':');
		form1.ProduktId.value=temp[0];
		form1.pris.value=temp[1];
		Beregn();
	}
}

function settFarge()
{
	form1.fargeIdValgt.value=form1.farge[form1.farge.selectedIndex].value;
}

function Beregn()
{
	form1.total.value=form1.antall.value*form1.pris.value + ".00";
}

function bestille()
{
	//alert("ProduktId:" + form1.ProduktId.value + ", Antall: " + form1.antall.value + ", Farge: " + form1.fargeIdValgt.value); 
	if (form1.storrelse.selectedIndex == 0)
	{
		alert("Velg størrelse");
		form1.storrelse.focus();
	}
	else if (form1.antall.value == "" || form1.antall.value == "0")
	{
		alert("Oppgi et antall")
		form1.antall.focus();
	}
	else if (form1.farge.selectedIndex == 0)
	{
		alert("Velg farge");
		form1.farge.focus();
	}
	else
	{
		//alert("," + form1.storrelse.selectedIndex + "," + form1.farge.selectedIndex); 
		window.location="cart.php?action=add&p=" + form1.ProduktId.value + "&f=" + form1.fargeIdValgt.value + "&a=" + form1.antall.value;
	}
	
}

