/*
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);
}
/*
*/
function checkAddCartFormM0(blabla)
{
	with (window.document.frmAddToCartPerM0) {
		if (!form_input_is_int(txtQty.value)){
			aantal = 1;
		} else {
			aantal = txtQty.value;
		}
		loc_href = blabla + '&txtQty=' + aantal +'&txtReferentie=' + txtReferentie.value;
		window.location.href = loc_href;
		setTimeout('submit()',2000);
		return true;
		}
}


function checkAddCartFormM1(blabla)
{
	with (window.document.frmAddToCartPerM1) {
		if (isEmpty(txtWidth, 'Voer lengte in')) {
//			document.write(blabla);
			return false;
		} else {
			if (!form_input_is_int(txtQty.value)){
				aantal = 1;
			} else {
					aantal = txtQty.value;
			}
			loc_href = blabla + '&txtwidth=' + txtWidth.value + '&txtQty=' + aantal + '&txtReferentie=' + txtReferentie.value;
			window.location.href = loc_href;
			setTimeout('submit()',2000);
			return true;
		}
	}
}


function checkAddCartFormM2(blabla)
{
	with (window.document.frmAddToCartPerM2) {
		if (isEmpty(txtWidth, 'Voer breedte in') || isEmpty(txtHeight, 'Voer hoogte in')) {
			return false;
		} else {
			if (max_oppervlakte.value != 0 && txtHeight.value * txtWidth.value / 1000000 > max_oppervlakte.value) {
				alert("Maximumoppervlak voor deze dikte = " + max_oppervlakte.value + 
				"m2\n\nAndere dikte's:\n\n4-4  max 2,46 m2       5-6  max 4,50 m2\n4-5  max 2,94 m2       6-6  max 5,74 m2\n4-6  max 3,69 m2       6-8  max 7,23 m2\n5-5  max 3,94 m2       8-8  max 10,13 m2\n\nLet op, deze waarden zijn fabricagemaxima.\nNaleving van NEN2608 is de verantwoordelijkheid\n van de klant.\n");
				formElement.focus();
				return false;
			}
//			if (typeof (window.document.frmAddToCartPerM2.cboSpouwdikte.options[cboSpouwdikte.selectedIndex].value) != 'undefined')  {
			if (typeof cboSpouwdikte.options[cboSpouwdikte.selectedIndex].value != 'undefined')  {
					if (!(window.document.frmAddToCartPerM2.cboSpouwdikte.options[cboSpouwdikte.selectedIndex].value == '')) {
					if (!form_input_is_int(txtQty.value)){
						aantal = 1;
					} else {
						aantal = txtQty.value;
					}
					loc_href = blabla + '&txtwidth=' + txtWidth.value + '&txtheight=' + txtHeight.value + '&cboSpouwdikte=' + cboSpouwdikte.options[cboSpouwdikte.selectedIndex].value +'&txtQty=' + aantal + '&txtReferentie=' + txtReferentie.value;
					window.location.href = loc_href;
					setTimeout('submit()',2000);
					return true;
				} else {
	/*				loc_href = blabla + '&txtwidth=' + txtWidth.value + '&txtheight=' + txtHeight.value + '&cboSpouwdikte=' + '0';
					window.location.href = loc_href;
					setTimeout('submit()',2000);
	*/				alert("Kies een spouwdikte!");
					formElement.focus();
					return false;
				}
			alert("undefined!");
			formElement.focus();
			if (!form_input_is_int(txtQty.value)){
				aantal = 1;
			} else {
				aantal = txtQty.value;
			}
			loc_href = blabla + '&txtwidth=' + txtWidth.value + '&txtheight=' + txtHeight.value + '&cboSpouwdikte=' + '0' + '&txtQty=' + aantal +'&txtReferentie=' + txtReferentie.value;
			window.location.href = loc_href;	
			setTimeout('submit()',2000);
			return true;
			}
		}
	}
}

function checkAddCartFormM3(blabla)
{
	with (window.document.frmAddToCartPerM3) {
		if (isEmpty(txtWidth, 'Voer lengte in') || isEmpty(txtHeight, 'Voer breedte in') || isEmpty(txtDepth, 'Voer hoogte in')) {
			return;
		} else {
			loc_href = blabla + '&txtwidth=' + txtWidth.value + '&txtheight=' + txtHeight.value + '&txtdepth=' + txtDepth.value;
//			document.write(loc_href);			
			window.location.href = loc_href;
			setTimeout('submit()',2000);
			return true;
		}
	}
}


/*
	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 popUp(URL)
{
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=600,height=400,left = 600,top = 400');");
}

function form_input_is_int(input){
    return !isNaN(input)&&parseInt(input)==input;
}



