/*
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)
{
	textBox.value = textBox.value.replace(',','.')
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	textBox.value = textBox.value.replace('.',',')
	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;
		}
	}	
}

function checkKundeInfo()
{ 
	with (window.document.frmCheckout) {
		if (isEmpty(dp_1, 'Indtast dato')) {
			return false;
		}else if (isEmpty(cNavn, 'Indtast navn')) {
			return false;			
		}else if (isNaN(cMob.value) || (cMob.value.length < 8)  ) {
			alert('Indtast et mobil-nummer på mindst 8 cifre');
			return false;						
		} else if (isEmpty(cEmail, 'Indtast email-adresse') || checkEmail(cEmail)) {
			return false;
		} else {
			return true;
		}
	}
}

function checkCustomerInfo() 
{ 
	with (window.document.frmCheckout) {
		if (isEmpty(cNavn, 'Name missing')) {
			return false;
		}else if (isNaN(cMob.value) || (cMob.value.length < 8)  ) {
			alert('Cell-phone number must have at least 8 digits.');
			return false;						
		} else if (isEmpty(cEmail, 'Email-address') || checkEmail(cEmail)) {
			return false;
		} else {
			return true;
		}
	}
}


function checkKontaktInfo()
{ 
	with (window.document.frmCheckout) {
		if (isEmpty(cNavn, 'Indtast navn')) {
			return false;
		}else if (isNaN(cMob.value) || (cMob.value.length < 8)  ) {
			alert('Indtast et mobil-nummer på mindst 8 cifre');
			return false;			
		} else if (isEmpty(cEmail, 'Indtast email-adresse') || checkEmail(cEmail)) {
			return false;
		} else {
			return true;
		}
	}
}

function checkTyskKontaktInfo()
{ 
	with (window.document.frmCheckout) {
		if (isEmpty(cNavn, 'Name?')) {
			return false;
		}else if (isNaN(cMob.value) || (cMob.value.length < 8)  ) {
			alert('Mobil-numer soll min. 8 haben.');
			return false;			
		} else if (isEmpty(cEmail, 'Email-adresse?') || checkTyskEmail(cEmail)) {
			return false;
		} else {
			return true;
		}
	}
}

function checkEngKontaktInfo()
{ 
	with (window.document.frmCheckout) {
		if (isEmpty(cNavn, 'Name?')) {
			return false;
		}else if (isNaN(cMob.value) || (cMob.value.length < 8)  ) {
			alert('Cell-phone number must have at least 8 digits.');
			return false;			
		} else if (isEmpty(cEmail, 'Email-adress?') || checkEngEmail(cEmail)) {
			return false;
		} else {
			return true;
		}
	}
}


/* Check e-mail for @ and . */
function checkEmail(myForm) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.value)){
		return false;
	}else{
	alert("Email-adresse er ikke korrekt.")
	return true;}
}

function checkEngEmail(myForm) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.value)){
		return false;
	}else{
	alert("Email-address is not valid.")
	return true;}
}

function checkTyskEmail(myForm) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(myForm.value)){
		return false;
	}else{
	alert("Email-addresse ist falsch.")
	return true;}
}
