function vercgc(obj)
{
if (obj.value!="") {
  var checkOK = "0123456789.-/"; // Caracteres Possiveis no CGC
  var checkStr = obj.value;
  var allValid = true;

  // Verifica a existencia de caracteres invalidos

  for (i = 0; i < checkStr.length; i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }

  if (!allValid)
  {
    alert("O CGC possui caracteres inválidos !\n\nFormato: 99.999.999/0001-99");
    obj.value="";
    obj.focus();
    return (false);
  }

  // Elimina todos os caracteres nao numericos
  
  var cad="";
  for (i=0;i<checkStr.length;i++) {
    if ((checkStr.charAt(i)!=".") && (checkStr.charAt(i)!="-") && (checkStr.charAt(i)!="/"))
      cad += checkStr.charAt(i);
  }

// Inicio do teste com CGC

    if (cad.length!=14)
    {
      alert("O número do CGC precisa ter 14 dígitos !");
      obj.value="";
      obj.focus();
      return (false);
    }
    else
    {
        return(obj.value=cad.charAt(0)+cad.charAt(1)+"."+
	                 cad.charAt(2)+cad.charAt(3)+cad.charAt(4)+"."+
		         cad.charAt(5)+cad.charAt(6)+cad.charAt(7)+"/"+
		         cad.charAt(8)+cad.charAt(9)+cad.charAt(10)+cad.charAt(11)+"-"+
		         cad.charAt(12)+cad.charAt(13));
    }

}

return(true);
}

function tirarespacos(obj) {

  while (obj.value.indexOf('  ') != -1) {
    obj.value = obj.value.substring(0,obj.value.indexOf('  ')) + obj.value.substring(obj.value.indexOf('  ')+1,obj.value.length);
  }

  if (obj.value.indexOf(' ') == 0) {
    obj.value = obj.value.substring(1,obj.value.length);
  }

  if (obj.value.charAt(obj.value.length-1) == ' ') {
    obj.value = obj.value.substring(0,obj.value.length-1);
  }

  return(true);
}

function sonumeros(obj,cad) {
  var chvalidos = cad;
  var i=0;
  var valid=true;

  while ((i<obj.value.length) && (valid)) {
    valid=false;
    for(j=0;j<chvalidos.length;j++) {
      if (obj.value.charAt(i) == chvalidos.charAt(j)) {valid=true;}
    }
    i++;
  }
  if (!valid) {
    alert('Digite somente números!');
    obj.value="";
    obj.focus();
    return(false);
  }
  return(true);
}

// Verifica o telefonte digitado

function verfone(obj) 
{
  var chkstr=obj.value;
  var caracter;

  if (chkstr.length = 0) {
     return(true);
  }


  if (sonumeros(obj,"0123456789") == false) {
     return(false);
  }

  caracter =chkstr.charAt(0);
 
  if (caracter == "") {
    return(true);
  }

  if (caracter == 0) {
     alert("O telefone não deve iniciar com zero!");
     obj.focus();
     return(false);
  }

  if (chkstr.length < 8) {
     alert("Quantidade de números menor que o minimo exigido (8 caracteres)");
     obj.focus();
     return(false);
  }


  return(true);
}

function veremail(obj)
{
if (obj.value!="") {

  while (obj.value.indexOf(' ') != -1) {
    obj.value = obj.value.substring(0,obj.value.indexOf(' ')) + obj.value.substring(obj.value.indexOf(' ')+1,obj.value.length);
  }

  var checkOK = "\@."; // Caracteres Obrigatorios
  obj.value = obj.value.toLowerCase(); // Converte para Lower Case
  var checkStr = obj.value;
  var achar = true;
  var erro = false;
  i=0;

  // Verifica a presenca dos caracteres obrigatorios

  while ((i<checkOK.length)&&(achar)) {
    ch = checkOK.charAt(i);
    t=0;
    achar=false;
    while ((!achar)&&(t<checkStr.length)) {
      if (ch==checkStr.charAt(t))
        achar=true;
      t++;
    }
    i++;
  }

  // Se OK entao prossegue com os testes

  if (achar) {
    cta=0;
    ctpp=0;
    for (i=0;i<checkStr.length;i++)        // Verifica a quantidade de '\@'
      if (checkStr.charAt(i)=='\@') {
        cta++;

        // Verifica se existe algum '.' antes ou depois do '\@'

        if ((checkStr.charAt(i+1)=='.') || 
            (checkStr.charAt(i-1)=='.'))
              cta++;
      } 

    // Verifica a quantidade de '.'


    // Verifica se ha algum '.' junto a outro

    for (i=0;i<checkStr.length;i++)
      if (checkStr.charAt(i)=='.')
        if (checkStr.charAt(i+1)=='.') ctpp++;

    if ((cta>1)||(ctpp!=0)) erro=true;   // Seta esta variavel se houve algum erro ate agora

    // Verifica se ha '.' ou '\@' no inicio ou no fim do E-mail
   
    if ((checkStr.charAt(0)=='\@') ||
        (checkStr.charAt(0)=='.') ||
        (checkStr.charAt(checkStr.length-1)=='\@') ||
        (checkStr.charAt(checkStr.length-1)=='.'))
          erro = true;    // Seta esta variavel se houve algum erro ate agora

    // Certifica que o fim do E-mail possui 2 ou 3 caracteres

    if ((checkStr.charAt(checkStr.length-4)!='.') &&
        (checkStr.charAt(checkStr.length-3)!='.'))
          erro = true;    // Seta esta variavel se houve algum erro ate agora
  }

  if ((erro)||(!achar))
  {
    alert("Digite o E-mail no formato user\@dominio !");
    obj.focus();
    obj.value="";
    return (false);
  }

  var checkOK = "abcdefghijklmnopqrstuvwxyz0123456789\@%_-."; // Caracteres Possiveis
  var allValid = true;

  // Verifica existencia de caracteres invalidos

  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;

    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("O E-mail possui caracteres inválidos !");
    obj.focus();
    obj.value="";
    return (false);
  }
  else
    return(obj.value);   // Retorna o E-mail ja' em Lower Case
 }
}

function verdatapq(obj) {
	if (sonumeros(obj,'0123456789/') == false) {
		return(false);
	}
	
	if (obj.value != '') {
		while (obj.value.indexOf(' ') != -1) {
    		obj.value = obj.value.substring(0,obj.value.indexOf(' ')) + obj.value.substring(obj.value.indexOf(' ')+1,obj.value.length);
  		}
		var ch='/';
		var validform=true;
		var valid=true;
		var chkstr=obj.value;
		var ct=0;
		var dia=''; 
		var mes=''; 
		var ano='';
		
		if ((chkstr.charAt(1)!=ch) && (chkstr.charAt(2)!=ch) || (chkstr.charAt(chkstr.length-1)==ch)) { validform=false; }
		
	  	for (i=0;i<chkstr.length;i++)
    		if (chkstr.charAt(i)==ch) ct++;
			
  		if (ct>1) validform=false;
		
		i=0;
		if (validform) {
    		while (chkstr.charAt(i)!=ch) {
      			dia=dia+chkstr.charAt(i);
      			i++;
    		}
    		i++; 
    		while (i<chkstr.length)	{
      			mes=mes+chkstr.charAt(i);
      			i++;
    		}
			
		if ((dia.length != 2)&&(mes.length != 2)) valid=false;
			
		    if (valid) {
    			if (((dia>31)&&(mes==1))||((dia>31)&&(mes==3))||
			   ((dia>30)&&(mes==4))||((dia>31)&&(mes==5))||
			   ((dia>30)&&(mes==6))||((dia>31)&&(mes==7))||
			   ((dia>31)&&(mes==8))||((dia>30)&&(mes==9))||
			   ((dia>31)&&(mes==10))||((dia>30)&&(mes==11))||
			   ((dia>31)&&(mes==12))||((dia<1)||(dia>31))||
			   ((mes<1)||(mes>12)))
				valid = false;  
			}
			else {
				alert("Digite a data no formato dd/mm.");
				obj.focus();
				obj.value="";
				return (false);
			}
		
			if (!valid) {
				alert("Data Inválida!");
				obj.focus();
				obj.value="";
				return (false);
			}
			
			if (dia.length == 1) { dia = "0" + dia; }
  			if (mes.length == 1) { mes = "0" + mes; }

			return (obj.value=dia+'/'+mes);
		}
	}
}


// Verifica formato da Data

function verdata(obj)
{

if (sonumeros(obj,"0123456789/") == false) {
	return(false);
}

if (obj.value != '') {

  while (obj.value.indexOf(' ') != -1) {
    obj.value = obj.value.substring(0,obj.value.indexOf(' ')) + obj.value.substring(obj.value.indexOf(' ')+1,obj.value.length);
  }

  ch='/';
  validform=true;
  valid=true;
  chkstr=obj.value;
  ct=0;
  dia=''; mes=''; ano='';

  if (((chkstr.charAt(1)!=ch)&&(chkstr.charAt(2)!=ch))||
     ((chkstr.charAt(3)!=ch)&&(chkstr.charAt(4)!=ch)&&(chkstr.charAt(5)!=ch))||
      (chkstr.charAt(chkstr.length-1)==ch))
        validform=false;
  for (i=0;i<chkstr.length;i++)
    if (chkstr.charAt(i)==ch) ct++;
  if (ct>2) validform=false;
  i=0;
  if (validform)
  {
    while (chkstr.charAt(i)!=ch)
    {
      dia=dia+chkstr.charAt(i);
      i++;
    }
    i++; 
    while (chkstr.charAt(i)!=ch)
    {
      mes=mes+chkstr.charAt(i);
      i++;
    }
    i++; 
    while (i<chkstr.length)
    {
      ano=ano+chkstr.charAt(i);
      i++;
    }

    if ((ano.length != 2)&&(ano.length != 4)) {valid=false;}

    if (ano < 100) {
      if ((ano >=0)&&(ano<=49))
        ano = eval(ano)+2000;
      else
        ano = eval(ano)+1900;
    }
 
    if (!(((ano%4)||!(ano%100))&&(ano%400)))
    {
      if ((dia>29)&&(mes==2)) valid=false;
    }
    else
    {
      if ((dia>28)&&(mes==2)) valid=false;
    }

    if (valid)
    if (((dia>31)&&(mes==1))||
        ((dia>31)&&(mes==3))||
        ((dia>30)&&(mes==4))||
        ((dia>31)&&(mes==5))||
        ((dia>30)&&(mes==6))||
        ((dia>31)&&(mes==7))||
        ((dia>31)&&(mes==8))||
        ((dia>30)&&(mes==9))||
        ((dia>31)&&(mes==10))||
        ((dia>30)&&(mes==11))||
        ((dia>31)&&(mes==12))||       
         (dia<1)||(dia>31)||
         (mes<1)||(mes>12)||
         (ano<0)
       )
      valid=false;
  }
  else
  {
    alert("Digite a data no formato dd/mm/aaaa");
    obj.focus();
    obj.value="";
    return (false);
  }
  if (!valid)
  {
    alert("Data Inválida");
    obj.focus();
    obj.value="";
    return (false);
  }

  if (dia.length == 1) {dia = "0" + dia;}
  if (mes.length == 1) {mes = "0" + mes;}

  return (obj.value=dia+'/'+mes+'/'+ano);
}
}

function vercep(obj,muda)
{
if (obj.value!="") {

  while (obj.value.indexOf(' ') != -1) {
    obj.value = obj.value.substring(0,obj.value.indexOf(' ')) + obj.value.substring(obj.value.indexOf(' ')+1,obj.value.length);
  }

  var checkOK = "0123456789-. "; // Caracteres Possiveis
  var checkStr = obj.value;
  var allValid = true;

  // Verifica a existencia de caracteres invalidos

  for (i = 0;  i < checkStr.length;  i++)
  {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++)
      if (ch == checkOK.charAt(j))
        break;
    if (j == checkOK.length)
    {
      allValid = false;
      break;
    }
  }
  if (!allValid)
  {
    alert("O CEP possui caracteres inválidos !\\n\\nFormato: 60000-000");
    obj.focus();
    obj.value="";
    return (false);
  }

  // Elimina todos os caracteres nao numericos

  var cad="";
  for (i=0;i<checkStr.length;i++) {
    if ((checkStr.charAt(i)!="-") && (checkStr.charAt(i)!="."))
      cad += checkStr.charAt(i);
  }

  // Verifica o tamanho do CEP. Caso esteja OK Reformata o CEP

  if (cad.length<8) {
    alert('O CEP precisa ter 8 dígitos !');
    obj.focus();
    obj.value="";
    return(false);
  }
  else { 
    obj.value=cad.charAt(0)+cad.charAt(1)+cad.charAt(2)+cad.charAt(3)+cad.charAt(4)+"-"+
                     cad.charAt(5)+cad.charAt(6)+cad.charAt(7);
  }
  return(obj.value);
}
}

function autotab(obj,obj2) {
	var tam 
	tam = obj.value.length;
	if (tam>=obj.maxLength) {
		obj2.focus();
	}
}

