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.@_-ABCDEFGHIJKLMNOPQRSTUSWXYZ";
  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 (CompID == 'vide')
	{
		if (!TailleMin(document.form.Name.value,3))
			error(document.form.Name, "Naam Apotheek niet ingevuld.");
		if (Type == 'phar')
		{
			if (!TailleMin(document.form.pharNum.value,6))
				error(document.form.pharNum, "Apotheek Nummer niet ingevuld.");
		}
		/*
		if (document.form.LandPer.options[document.form.LandPer.selectedIndex].value == 'BELGIUM')
		{
			if (!TailleMin(document.form.Btw.value,11))
			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...).");
		}
		*/
		if (!TailleMin(document.form.Address.value,3))
			error(document.form.Address, "Adres & Huisnummer niet ingevuld.");
		if (!TailleMin(document.form.ZipCode.value,4))
			error(document.form.ZipCode, "PostCode niet ingevuld.");
		if (!TailleMin(document.form.Locality.value,3))
			error(document.form.Locality, "Gemeente niet ingevuld.");
		if (!TailleMin(document.form.Phone.value,7))
			{	error(document.form.Phone, "Telefoon niet ingevuld.");
			}
		else
			{ 
				if (!CharValid(document.form.Phone.value))
				error(document.form.Phone, "Telefoon niet geldig.");
			}
	}		
	if (!TailleMin(document.form.FirstName.value,3))
		error(document.form.FirstName, "Voornaam niet ingevuld.");
	if (!TailleMin(document.form.LastName.value,2))
		error(document.form.LastName, "Naam niet ingevuld.");
	if (CompID == 'vide')
	{
	if (!TailleMin(document.form.EMail.value,6))
		error(document.form.EMail, "EMail niet ingevuld.");
		if (document.form.EMail.value.length > 0)
		{
			if (!ValidEmail(stripSpaces(document.form.EMail.value)))
			error(document.form.EMail, "Email niet geldig.");
		}
	}

return !errfound;
}
function Others(what,selected,other)
{
	if (selected == other) 
	{
		eval(what).style.display='inline';
	}
	else
	{
		eval(what).style.display='none';
		eval(what).value='';
	}
}
