
/* Mascaras */
function formatar(objeto, evento, tipo)
{
    switch (tipo)
    {
        case "data": formataData(objeto,evento);
            break;
        case "cep": formataCEP(objeto,evento);
            break;
        case "tel": formataTelefone(objeto,evento);
            break;
    }
}
// Exemplos:

// CEP
// OnKeyPress="formatar(this, event, 'cep')"

// DATA
// OnKeyPress="formatar(this, event, 'cep')"

// TELEFONE
// OnKeyPress="formatar(this, event, 'tel')"

// txtNomeCampo.Attributes.Add("onKeyPress", "formatar(this, event, '')")

/**************************************/

function formataData(objeto, evento) 
{ 
	var tecla = evento.keyCode; 
	var tamanho = objeto.value.length; 

	if(tecla >= 48 && tecla <= 57) 
	{ 
	switch (tamanho) { 
		case 2: 
		objeto.value = objeto.value + "/"; 
		break; 
		case 5: 
		objeto.value = objeto.value + "/"; 
		break; 
		} 
	} 
	else 
	{ 
		evento.keyCode = ""; 
	} 
} 

/**************************************/

function formataCEP(objeto, evento) 
{ 
	var tecla = evento.keyCode; 
	var tamanho = objeto.value.length; 

	if(tecla >= 48 && tecla <= 57) 
	{ 
	switch (tamanho) { 
		case 5: 
		objeto.value = objeto.value + "-"; 
		break; 

		} 

	} 
	else 
		{ 
		evento.keyCode = ""; 
		} 
} 

/**************************************/

function formataTelefone(objeto, evento) 
{ 
    var tecla = evento.keyCode; 
    var tamanho = objeto.value.length; 

    if(tecla >= 48 && tecla <= 57) 
    { 
        switch (tamanho) { 
        case 2: 
        objeto.value = objeto.value + "-"; 
        break; 
        case 7: 
        objeto.value = objeto.value + "-"; 
        break; 
        } 

    } 
    else 
    { 
        evento.keyCode = ""; 
    } 
} 

/**************************************/


function Limpar(valor, validos) { 
// retira caracteres invalidos da string 
var result = ""; 
var aux; 
for (var i=0; i < valor.length; i++) { 
aux = validos.indexOf(valor.substring(i, i+1)); 
if (aux>=0) { 
result += aux; 
} 
} 
return result; 
} 

//Formata nÃºmero tipo moeda usando o evento onKeyDown 

function FormataMoeda(campo,tammax,teclapres,decimal) { 
var tecla = teclapres.keyCode; 
vr = Limpar(campo.value,"0123456789"); 
tam = vr.length; 
dec=decimal 

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 <= dec ) 
{ campo.value = vr ; } 

if ( (tam > dec) && (tam <= 5) ){ 
campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; } 
if ( (tam >= 6) && (tam <= 8) ){ 
campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, 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 - dec, 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 - dec, 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 ) ;} 
} 

} 



// funcao de validar data is date
  function isDate(dateStr) {
    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?
    
    if (matchArray == null) {
//    alert("Please enter date as either mm/dd/yyyy or mm-dd-yyyy.");
    return false;
    }
    
    month = matchArray[1]; // p@rse date into variables
    day = matchArray[3];
    year = matchArray[5];
    
    if (month < 1 || month > 12) { // check month range
//    alert("Month must be between 1 and 12.");
    return false;
    }
    
    if (day < 1 || day > 31) {
//    alert("Day must be between 1 and 31.");
    return false;
    }
    
    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
//    alert("Month "+month+" doesn`t have 31 days!")
    return false;
    }
    
    if (month == 2) { // check for february 29th
    var isleap = (year%4);
    if (day >= 29 && (isleap==1)) {
//    alert("February " + year + " doesn`t have " + day + " days!");
    return false;
    }
    }
    
    return true; // date is valid
  }
// final de is date


//funcao de valida numero
    function numerop (x)
    {
        s = new String(x);
        ___ok = 1;
        for (i = 0; i < s.length; i++)
        {
        r = (s.charAt(i) == "0"
            || s.charAt(i) == "1" 
            || s.charAt(i) == "2" 
            || s.charAt(i) == "3" 
            || s.charAt(i) == "4" 
            || s.charAt(i) == "5" 
            || s.charAt(i) == "6"  
            || s.charAt(i) == "7" 
            || s.charAt(i) == "8" 
            || s.charAt(i) == "9" 
            || s.charAt(i) == "." 
            || s.charAt(i) == "-" );
        ___ok = ___ok & r;
        }
        return ___ok;
    }

//valida campos em branco
function CampoVazio(s){ return ((s == null) || (s.length == 0))}

//funcao de validacao de email
function EmailValidate(emailToValidate){
	if(emailToValidate == "" || emailToValidate.indexOf("@") == -1 || emailToValidate.indexOf("@") == 0 || emailToValidate.indexOf(".") == -1 || emailToValidate.indexOf(".") == (emailToValidate.indexOf("@")+1) || emailToValidate.indexOf(".") == emailToValidate.length -1){ return true;}
  else{ return false};
 }


  // funcao que valida cep
  function ValidaCep(Cep_Pref,Cep_Suf){
    // ***** return 1 = cep invalido
    // ***** return 2 = Prefixo do cep invalido
    // ***** return 3 = cep invalido
    if (CampoVazio(Cep_Pref) || CampoVazio(Cep_Suf) || ! numerop (Cep_Pref) || ! numerop (Cep_Suf)) { return 1; }
    if (!CampoVazio(Cep_Pref) && Cep_Pref.length < 5) { return 2; }
    if (!CampoVazio(Cep_Suf) && Cep_Suf.length < 3) { return 3; }
  }
  
  
function vLogin(){
  f = document.frmCad;
  ook = true;
  msg = "Os seguintes campos devem ser preenchidos, ou apresentaram erros\n\n";
  
  if (CampoVazio(f.login.value)) { ook = false; msg = msg + " Login\n"; }
  if (CampoVazio(f.senha.value)) { ook = false; msg = msg + " Senha\n"; }

  //if (EmailValidate(f.email.value)) { ook = false; msg = msg + " E-Mail em branco ou invalido.\n"; }

  // testa valicao e envia alerta ou formulario
  if (! ook){ alert (msg);}
  else{ f.submit();}
}



function vEdit(){
  f = document.frmCad;
  ook = true;
  msg = "Os seguintes campos devem ser preenchidos, ou apresentaram erros\n\n";
  
  if (CampoVazio(f.nome.value)) { ook = false; msg = msg + " Nome\n"; }
  if (CampoVazio(f.endereco.value)) { ook = false; msg = msg + " endereço\n"; }
  if (CampoVazio(f.uf.value)) { ook = false; msg = msg + " UF\n"; }
  if (CampoVazio(f.cep.value)) { ook = false; msg = msg + " CEP\n"; }
  if (CampoVazio(f.ddd_Tel.value)) { ook = false; msg = msg + " DDD do Telefone\n"; }
  if (CampoVazio(f.tel.value)) { ook = false; msg = msg + " Telefone\n"; }
  if (CampoVazio(f.ddd_cel.value)) { ook = false; msg = msg + " DDD do Celular\n"; }
  if (CampoVazio(f.cel.value)) { ook = false; msg = msg + " Celular\n"; }
  if (EmailValidate(f.email.value)) { ook = false; msg = msg + " E-Mail em branco ou invalido.\n"; }
//  if (!doDate(f.nascimento.value, '5')) { ook = false; msg = msg + " Data de Nascimento inválida\n"; }

  if (CampoVazio(f.estadocivil.value)) { ook = false; msg = msg + " Estado Civil\n"; }
  if (CampoVazio(f.filhos.value)) { ook = false; msg = msg + " Filhos\n"; }
  if (CampoVazio(f.hobby.value)) { ook = false; msg = msg + " Hobby\n"; }
  if (CampoVazio(f.regiao.value)) { ook = false; msg = msg + " Região\n"; }

//  if (!doDate(f.dataEntrada.value, '5')) { ook = false; msg = msg + " Data de Entrada inválida\n"; }

  // testa valicao e envia alerta ou formulario
  if (! ook){ alert (msg);}
  else{ f.submit();}
}

var reDate1 = /^\d{1,2}\/\d{1,2}\/\d{1,4}$/;
var reDate2 = /^[0-3]?\d\/[01]?\d\/(\d{2}|\d{4})$/;
var reDate3 = /^(0?[1-9]|[12]\d|3[01])\/(0?[1-9]|1[0-2])\/(19|20)?\d{2}$/;
var reDate4 = /^((0?[1-9]|[12]\d)\/(0?[1-9]|1[0-2])|30\/(0?[13-9]|1[0-2])|31\/(0?[13578]|1[02]))\/(19|20)?\d{2}$/;
var reDate5 = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{4}$/;
var reDate = reDate4;

function doDate(pStr, pFmt)
{
	eval("reDate = reDate" + pFmt);
	if (reDate.test(pStr)) {
		//alert(pStr + " é uma data válida.");
    return true;
	} else if (pStr != null && pStr != "") {
    //alert(pStr + " nao é uma data válida.");
		return false;
	}
  else{return false}
} // doDate




function vCadClass(){
  f = document.frmCad;
  ook = true;
  msg = "Os seguintes campos devem ser preenchidos, ou apresentaram erros\n\n";
  
  if (CampoVazio(f.titulo.value)) { ook = false; msg = msg + " Título\n"; }
  if (CampoVazio(f.valor.value)) { ook = false; msg = msg + " Valor\n"; }
  if (CampoVazio(f.uf.value)) { ook = false; msg = msg + " UF\n"; }
  if (CampoVazio(f.descricao.value)) { ook = false; msg = msg + " Descrição\n"; }

  // testa valicao e envia alerta ou formulario
  if (! ook){ alert (msg);}
  else{ f.submit();}
}


function vFale(){
  f = document.frmCad;
  ook = true;
  msg = "Os seguintes campos devem ser preenchidos, ou apresentaram erros\n\n";
  
  if (CampoVazio(f.mensagem.value)) { ook = false; msg = msg + " Preencha a mensagem\n"; }

  // testa valicao e envia alerta ou formulario
  if (! ook){ alert (msg);}
  else{ f.submit();}
}

function vDuvidas(){
  f = document.frmCad;
  ook = true;
  msg = "Os seguintes campos devem ser preenchidos, ou apresentaram erros\n\n";
  
  if (CampoVazio(f.mensagem.value)) { ook = false; msg = msg + " Preencha a mensagem\n"; }

  // testa valicao e envia alerta ou formulario
  if (! ook){ alert (msg);}
  else{ f.submit();}
}


function vTopico(){
  f = document.frmCad;
  ook = true;
  msg = "Os seguintes campos devem ser preenchidos, ou apresentaram erros\n\n";
  
  if (CampoVazio(f.titulo.value)) { ook = false; msg = msg + " Título\n"; }
  if (CampoVazio(f.descricao.value)) { ook = false; msg = msg + " Descrição\n"; }

  // testa valicao e envia alerta ou formulario
  if (! ook){ alert (msg);}
  else{ f.submit();}
}

function vTopicoMsg(){
  f = document.frmCad;
  ook = true;
  msg = "Os seguintes campos devem ser preenchidos, ou apresentaram erros\n\n";
  
  if (CampoVazio(f.descricao.value)) { ook = false; msg = msg + " Mensagem\n"; }

  // testa valicao e envia alerta ou formulario
  if (! ook){ alert (msg);}
  else{ f.submit();}
}