//
// Mascara generica de campos
//
function txtBoxFormat(objForm, strField, sMask, evtKeyPress) 
{
var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

if(document.all) { // Internet Explorer
nTecla = evtKeyPress.keyCode; }
else if(document.layers) { // Nestcape
nTecla = evtKeyPress.which;
}

sValue = objForm[strField].value;

// Limpa todos os caracteres de formatação que
// já estiverem no campo.
sValue = sValue.toString().replace( "-", "" );
sValue = sValue.toString().replace( "-", "" );
sValue = sValue.toString().replace( ".", "" );
sValue = sValue.toString().replace( ".", "" );
sValue = sValue.toString().replace( "/", "" );
sValue = sValue.toString().replace( "/", "" );
sValue = sValue.toString().replace( "(", "" );
sValue = sValue.toString().replace( "(", "" );
sValue = sValue.toString().replace( ")", "" );
sValue = sValue.toString().replace( ")", "" );
sValue = sValue.toString().replace( " ", "" );
sValue = sValue.toString().replace( " ", "" );
fldLen = sValue.length;
mskLen = sMask.length;

i = 0;
nCount = 0;
sCod = "";
mskLen = fldLen;

while (i <= mskLen) {
bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

if (bolMask) {
sCod += sMask.charAt(i);
mskLen++; }
else {
sCod += sValue.charAt(nCount);
nCount++;
}

i++;
}

objForm[strField].value = sCod;

if (nTecla != 8) { // backspace
if (sMask.charAt(i-1) == "9") { // apenas números...
return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
else { // qualquer caracter...
return true;
} }
else {
return true;
}
}

// FUNÇÃO PARA FORMATAR A DATA
function Unformat(txt,seq)
{
 aux=""
 for (x=0; x<txt.length; x++)
 {
   if (seq.indexOf(txt.charAt(x))>=0)
      aux+=txt.charAt(x)
 }
 return aux
}

function FormatData(obj)
{
 dt=""
 txt=Unformat(obj.value,"0123456789")
 for (x=0; x<txt.length; x++)
 {
   if ((x==2) || (x==4))
      dt+="/" + txt.charAt(x)
   else
      dt+=txt.charAt(x)
 }
 if ((dt.length>0) && (dt.length<10))
 {
  alert("Data Inválida.")
  obj.focus()
  return false
 } 

 var year,month,day
 year=dt.substring(6,10)
 month=dt.substring(3,5)
 if (month.substring(0,1)=="0")
    month=month.substring(1,2)
 
 day=dt.substring(0,2)
 if (day.substring(0,1)=="0")
    day=day.substring(1,2)
 
 var dt_aux= new Date()
 dt_aux.setYear(year)
 dt_aux.setMonth(month - 1)
 dt_aux.setDate(day)
 if ((dt_aux.getFullYear() != parseInt(year)) || (dt_aux.getMonth()+1 != parseInt(month)) || (dt_aux.getDate() != parseInt(day)))
 {
   alert("Data Inválida.")
   obj.focus()
   return false
 }
 obj.value=dt
}

<!--
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.id; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- ( '+nm+' ) deve ser um email válido.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- ( '+nm+' ) deve ser um número.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- ( '+nm+' ) must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- ( '+nm+' ) deve ser preenchido.\n'; }
  }
   if (errors) alert('ATENÇÃO para o(s) seguinte(s) campo(s):\n\n'+errors);
  document.MM_returnValue = (errors == '');
}
-->
