function removeSpaces(string) {
	//removes all spaces from a given string
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	return tstring;
}


function validateDirectCode() {
	var pass=false;
	var errors='';
    var directcode;
	directcode = document.directCodeForm.directcode.value;
	if (removeSpaces(directcode)=="") {
		alert('Code mag niet leeg zijn');
	}
	else {
		document.directCodeForm.submit();
	}

}

function validateUserPass() {
	//validates if user/password has been filled in + gives javascript alert
    var userName;
	var userPass;
	userName = document.loginForm.userName.value;
	userPass = document.loginForm.userPass.value;

	if (removeSpaces(userName)=="" || removeSpaces(userPass)=="") {
		alert('Gelieve je e-mail adres en wachtwoord in te vullen');
		return false;
	}
	else {

		document.loginForm.submit();
	}
}

function validateCustomer() {
	var pass=true;
	var errors='';
	var firstName='';
	var password='';
	var firstname='';
	var lastname='';
	var address='';
	var postal_code='';
	var city='';
	var email='';
	var tel_nr='';
	var mob_nr='';


	document.getElementById("errorMessage").style.display='none';

	firstName=document.frmCustomer.firstName;
	lastName=document.frmCustomer.lastName;
	adress=document.frmCustomer.adress;
	city=document.frmCustomer.city;
	zip=document.frmCustomer.zip;
	phone=document.frmCustomer.phone;
	email=document.frmCustomer.email; //special validation there
	action = document.frmCustomer.action;
	password=document.frmCustomer.password;


	if (firstName.value == ''){document.getElementById("errorMessage").style.display="";pass=false;}
	if (lastName.value == ''){document.getElementById("errorMessage").style.display="";pass=false;}
	if (adress.value == ''){document.getElementById("errorMessage").style.display="";pass=false;}
	if (city.value == ''){document.getElementById("errorMessage").style.display="";pass=false;}
	if (zip.value == ''){document.getElementById("errorMessage").style.display="";pass=false;}
	if (phone.value == ''){document.getElementById("errorMessage").style.display="";pass=false;}
	if (firstName.value == ''){document.getElementById("errorMessage").style.display="";pass=false;}
	if (!emailCheck(email.value)){document.getElementById("errorMessage").style.display="";pass=false;}
	if (password.value == ''  && action.value == 'insert' ){document.getElementById("errorMessage").style.display="";pass=false;}


	if (pass)
	{
		document.frmCustomer.submit();
	}
}

function validateCart(redirLocation, cartIDs) {
	var pass=true;

	document.getElementById("errorMessage").style.display="none";

	for (var i=0;i<cartIDs.length;i++) {
		//format
		if(document.getElementById("size_"+cartIDs[i]).value==0) {
			document.getElementById("errorMessage").style.display="";
			pass=false;
			continue;
		}

		//type
		val1 = document.getElementById("r_"+cartIDs[i]+"_1").checked;
		val2 = document.getElementById("r_"+cartIDs[i]+"_2").checked;

		if(!(val1==true || val2 == true)) {
			document.getElementById("errorMessage").style.display="";
			pass=false;
			continue;
		}

		//quantity
		if(document.getElementById("qty_"+cartIDs[i]).value==0) {
			document.getElementById("errorMessage").style.display="";
			pass=false;
			continue;
		}
	}


	if (pass)
	{
		//redirects to right page
		document.frmCart.url_redir.value=redirLocation;
		document.frmCart.submit();
	}
}




// Radio Button Validation
// copyright Stephen Chapman, 15th Nov 2004,14th Sep 2005
// you may copy this function but please keep the copyright notice with it
function valButton(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return btn[cnt].value;
    else return null;
}


function emailCheck (emailStr)
{
	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var quotedUser="(\"[^\"]*\")";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + "|" + quotedUser + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);
	if (matchArray==null) {
	//alert("Email address seems incorrect (check @ and .'s)");
	return false;
	}
	var user=matchArray[1];
	var domain=matchArray[2];
	for (i=0; i<user.length; i++) {
	if (user.charCodeAt(i)>127) {
	//alert("Ths username contains invalid characters.");
	return false;
	   }
	}
	for (i=0; i<domain.length; i++) {
	if (domain.charCodeAt(i)>127) {
	//alert("Ths domain name contains invalid characters.");
	return false;
	   }
	}
	if (user.match(userPat)==null) {
	//alert("The username doesn't seem to be valid.");
	return false;
	}
	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {
	for (var i=1;i<=4;i++) {
	if (IPArray[i]>255) {
	//alert("Destination IP address is invalid!");
	return false;
	   }
	}
	return true;
	}
	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;
	for (i=0;i<len;i++) {
	if (domArr[i].search(atomPat)==-1) {
	//alert("The domain name does not seem to be valid.");
	return false;
	   }
	}
	if (checkTLD && domArr[domArr.length-1].length!=2 &&
	domArr[domArr.length-1].search(knownDomsPat)==-1) {


	//alert("The address must end in a well-known domain or two letter " + "country.");
	return false;
	}
	if (len<2) {
	//alert("This address is missing a hostname!");
	return false;
	}
	return true;
}