// JavaScript Document


function htmlOut(txt){
  ctr=0;
  while ((txt.indexOf("<")>-1)&&(ctr<4)){
   pos=txt.indexOf("<");
   txt=txt.substring(0,pos)+"&lt;"+txt.substring(pos+1,txt.length);
   ctr++;
   }
  while (txt.indexOf(">")>-1){
   pos=txt.indexOf(">");
   txt=txt.substring(0,pos)+"&gt;"+txt.substring(pos+1,txt.length);
   }
  return (txt);
}


 function nCh(nr1){
  flg=0;
  str="";
  spc=""
  arw="";
  for (var i=0;i<nr1.length;i++){
   cmp="0123456789"
   tst=nr1.substring(i,i+1)
   if (cmp.indexOf(tst)<0){
    flg++;
    str+=" "+tst;
    spc+=tst;
    arw+="^";
   }
   else{arw+="_";}
  }
  if (flg!=0){
   if (spc.indexOf(" ")>-1) {
    str+=" and a space";
    }
   return (false);
  }
  return (true);
 }

function checkn(val,nome){
   if (val=="")
    return (true);

   if (nCh(val))
	 return (true);
   else
	 alert(nome+' inválido!\nPor favor corrija-o e tente novamente!');
   return (false);
} 

function checkall(){
for (i=0;i< document.forms[0].elements.length; i++) {
document.forms[0].elements[i].value= htmlOut (document.forms[0].elements[i].value);
}

var f = document.forms[0];
if (!checkn(f.cp1.value, "Código Postal"))
return false;

if (!checkn(f.cp2.value, "Código Postal"))
return false;

if (!checkn(f.telef.value, "Número de telefone"))
return false;

if (!checkn(f.telem.value, "Número de telemóvel"))
return false;

if (!checkn(f.bi.value, "Número de BI"))
return false;

if (!checkn(f.contrib.value, "Número de Contribuinte"))
return false;

if (f.mail.value!="")
{validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
  strEmail = f.mail.value;

   // search email text for regular exp matches
if (strEmail.search(validRegExp) == -1) 
{     alert('E-Mail inválido!\nPor favor corrija-o e tente novamente!');
      return false;
} 
}
return (true);
}

