var errfound = false;

function stripSpaces(item) {
    while (item.substring(0,1) == ' ') item = item.substring(1);
    while (item.substring(item.length-1,item.length) == ' ') item = item.substring(0,item.length-1);
	return item;
}

function TailleMin(item, len) {
	return (item.length >= len);
	}

function ValidEmail(item) {
	if (!TailleMin(item, 5)) return false;
	if (item.indexOf ('@', 0) == -1) return false;
	if (item.indexOf ('.', 0) == -1) return false;
	if (!CharValidMail(item)) return false;
	return true;
	}

function CharValidMail(item) {
  var checkOK = "0123456789abcdefghijklmnopqrstuvwxyz.@_-ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  var checkStr = item;
  var allValid = true;
  for (i = 0;  i < item.length;  i++) {
    ch = item.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length){
      allValid = false;
      break;
    }
  }
  if (!allValid) return false;
  return true;
}
function CharValid(item) {
  var checkOK = "0123456789-+(). \t\r\n\f";
  var checkStr = item;
  var allValid = true;
  for (i = 0;  i < item.length;  i++) {
    ch = item.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length){
      allValid = false;
      break;
    }
  }
  if (!allValid) return false;
  return true;
}	

function error(elem, text) {
	if (errfound) return;
	window.alert(text);
	elem.select();
	elem.focus();
	errfound = true;
	}

function Validate() {
	errfound = false;
	/*if (!TailleMin(document.form.Bedrijf.value,3))
		error(document.form.Bedrijf, "You forgot to write your company name.");
	if (document.form.LandPer.options[document.form.LandPer.selectedIndex].value == 'BELGIUM')
	{
		if (!TailleMin(document.form.Btw.value,11) && (!document.form.Btw.value=='PENDING') && (!document.form.Btw.value=='EXEMPT'))
		error(document.form.Btw, "You forgot to write your BTW number or the one you provide us with, is not valid (e.g. too short...).\Please note that you can use the \'Pending\' or \'Exempt\' checkbox.");
	}*/

	if (!TailleMin(document.form.Adres.value,3))
		error(document.form.Adres, "You forgot to write your address.");
	if (!TailleMin(document.form.PostcodePer.value,4))
		error(document.form.PostcodePer, "You forgot to write your Zip code.");
	if (!TailleMin(document.form.GemeentePer.value,3))
		error(document.form.GemeentePer, "You forgot to write your Locality.");
	if (!TailleMin(document.form.Tel.value,7))
		{	error(document.form.Tel, "You forgot to write your telephone number or the one you provide us with, is not valid.");
		}
	else
		{ 
			if (!CharValid(document.form.Tel.value))
			error(document.form.Tel, "Your telephone number is not valid.");
		}
	if (!TailleMin(document.form.Voornaam.value,3))
		error(document.form.Voornaam, "You forgot to write your firstname.");
	if (!TailleMin(document.form.Naam.value,2))
		error(document.form.Naam, "You forgot to write your lastname.");
	if (!TailleMin(document.form.EMail.value,6))
		error(document.form.EMail, "You forgot to write your E-Mail.");
	if (document.form.EMail.value.length > 0)
	{
		if (!ValidEmail(stripSpaces(document.form.EMail.value)))
		error(document.form.EMail, "Your E-mail Address in not valid.");
	}

return !errfound;
}
function Others(what,selected,other)
{
	if (selected == other) 
	{
		eval(what).style.display='inline';
	}
	else
	{
		eval(what).style.display='none';
		eval(what).value='';
	}
}

function do_val_1(val){
	if (document.form.do_pending.checked == false)
	{
		document.form.Btw.value='';
	}
	else
	{
		if (document.form.do_exempt.checked == true) document.form.do_exempt.checked = false;
		document.form.Btw.value='PENDING';
	}
}
function do_val_2(val){
	if (document.form.do_exempt.checked == false)
	{
		document.form.Btw.value='';
	}
	else
	{
		if (document.form.do_pending.checked == true) document.form.do_pending.checked = false;
		document.form.Btw.value='EXEMPT';
	}
}

