/* ----------------------------------------------------------------------------
tips - 1o. caracter : 'L' campo com texto livre (default)
                      'T' campo com texto so de caracteres (sem numeros)
                      'E' campo de email
                      'N' campo numerico
                Obs : No caso de ser 'N' e 'E' o 2o. caracter será ignorado
       2o. caracter : 'S' Pode conter espaços (default)
                      'N' Não pode conter espaços
       3o. caracter : 'M' maior que (default)
                      'O' igualdade (do tamanho) entre um dos numeros
                      'C' igualdade entre campos

valors - podem estar separados por '|' no caso do 3o. caracter de tips ser 'O'
         no caso do 3o. caracter ser 'C' no lugar do valor vira um campo
         nos outros casos sempre vem um valor so
---------------------------------------------------------------------------- */
function ValidaCampos(Formul, tips, cmps, valors, nome)
  {
  var i,j,k;
  var cond = "";
  var msg = "";
  var campo = cmps.split(",");
  var nomes = nome.split(",");
  var tipos = tips.split(",");
  var valores = valors.split(",");

  if (campo.length != nomes.length || campo.length != tipos.length || campo.length != valores.length)
    { alert("Falta de parametros na função");  return false; }

  for (i=0; i < campo.length; i++)
    {
    // Define os default
    if (tipos[i].charAt(0) == ' ') tipos[i] = 'L'+tipos[i].charAt(1)+tipos[i].charAt(2);
    if (tipos[i].charAt(1) == ' ') tipos[i] = tipos[i].charAt(0)+'S'+tipos[i].charAt(2);
    if (tipos[i].charAt(2) == ' ') tipos[i] = tipos[i].charAt(0)+tipos[i].charAt(1)+'M';
    if (tipos[i].charAt(0) == 'E') tipos[i] = tipos[i].charAt(0)+'S'+tipos[i].charAt(2);
    if (tipos[i].charAt(0) == 'N') tipos[i] = tipos[i].charAt(0)+'N'+tipos[i].charAt(2);

    var cmp = eval(Formul+'.'+Trim(campo[i]));
    cmp.value = Trim(cmp.value);
    msg = "";

    if (cmp.value.length > 0)
      switch(tipos[i].charAt(0))
        {
        case 'E': if (!VerificaEmail(cmp.value)) msg = "O campo " + Trim(nomes[i]) + " deve ser um e-mail valido";
                  break;
        case 'N': if (!VerificaNumerico(cmp.value)) msg = "O campo " + Trim(nomes[i]) + " deve conter somente números";
                  break;
        case 'T': if (TemNumero(cmp.value)) msg = "O campo " + Trim(nomes[i]) + " não deve conter números";
                  break;
        }

    if (msg != "") { alert(msg); cmp.focus(); return false; }

    if (tipos[i].charAt(1) != 'S' && VerificaEspacos(cmp.value) && cmp.value.length > 0)
      msg = "O campo " + Trim(nomes[i]) + " não deve conter espaços";

    if (msg != "") { alert(msg); cmp.focus(); return false; }

    switch(tipos[i].charAt(2))
      {
      case 'M': if (cmp.value.length < valores[i] || (valores[i] == 0 && cmp.value.length == 0))
                  if (valores[i] == 0) msg = "O campo " + Trim(nomes[i]) + " é obrigatório";
                  else msg = "O campo " + Trim(nomes[i]) + " deve ter no mínimo " + valores[i] + " caracteres";
                break;
      case 'C': var valaux = valores[i].split("|");
                cond = eval(Formul+"."+valaux[0]+".value");
                if (!(cmp.value == cond)) msg = "O campo " + Trim(nomes[i]) + " deve ser igual ao campo " + valaux[1];
                break;
      case 'O': var valaux = valores[i].split("|");
                var msg = "O campo " + Trim(nomes[i]) + " deve ter ";
                k = 0;
                for (j=0; j < valaux.length; j++)
                  {
                  msg += valaux[j];
                  if (cmp.value.length != valaux[j]) k++;
                  if (j+1 < valaux.length) msg += " ou ";
                  }
                msg += " caracteres";
                if (j == k) msg = msg; else msg = "";
                break;
      }

    if (msg != "") { alert(msg); cmp.focus(); return false; }
    }

  return true;
  }

var vazio = false;
// espaco, tab, enter, recuo
var whitespace = " \t\n\r";

function Trim(str)
  {
  var ret = str;
  var i;

  for(i=0; whitespace.indexOf(ret.charAt(0)) != -1 && ret.length > 0; i++)
    ret = ret.slice(1,ret.length);
  for(i=ret.length-1; whitespace.indexOf(ret.charAt(ret.length-1)) != -1 && ret.length > 0; i++)
    ret = ret.slice(0,ret.length-1);

  return ret;
  }

function VerificaVazio(s)
  {
  return ((s == null) || (s.length == 0))
  }

function VerificaEspacos(s)
  {
  var i,c,j = 0;
  if (VerificaVazio(s)) return true;
  for (i = 0; i < s.length; i++)
    {
    c = s.charAt(i);
    if (whitespace.indexOf(c) == -1) j++;
    }
  if (j == i) return false;
  else return true;
  }

function VerificaEmail (s)
  {
  if (VerificaVazio(s))
    if (VerificaEmail.arguments.length == 1) return vazio;
    else return (VerificaEmail.arguments[1] == true);
  if (VerificaEspacos(s)) return false;
  var i = 1;
  var sLength = s.length;
  while (i < sLength && s.charAt(i) != "@") i++;
  if (i >= sLength || s.charAt(i) != "@") return false;
  else i += 2;
  while (i < sLength && s.charAt(i) != ".") i++;
  if (i >= sLength - 1 || s.charAt(i) != ".") return false;
  else return true;
  }

function VerificaNumerico(s)
  {
  for (cont=0; cont < s.length; cont++)
    {
    Letra = s.charAt(cont);
    if ((Letra < "0") || (Letra > "9")) return false;
    }
  return true;
  }

function TemNumero(s)
  {
  for (cont=0; cont < s.length; cont++)
    {
    Letra = s.charAt(cont);
    if ((Letra > "0") && (Letra < "9")) return true;
    }
  return false;
  }
  
function FormataValor(campo,tammax,teclapres)
  {
  var tecla = teclapres.keyCode;
  vr = campo.value;

  tira = Array('/','/',',','.','.','.','.','.','.');	
  for (x=0; x<=tira.length; x++)
    vr = vr.replace(tira[x],''); 

  tam = vr.length;

  if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }

  if (tecla == 8 ){	tam = tam - 1 ; }
		
  if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
    {
	if ( tam <= 2 ){ 
	  campo.value = vr ; }
 	if ( (tam > 2) && (tam <= 5) ){
	  campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
  	if ( (tam >= 6) && (tam <= 8) ){
  	  campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	if ( (tam >= 9) && (tam <= 11) ){
  	  campo.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	if ( (tam >= 12) && (tam <= 14) ){
  	  campo.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	if ( (tam >= 15) && (tam <= 17) ){
	  campo.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;}
	}		
  }