function txtBoxFormat(objeto, sMask, evtKeyPress) {
    var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;


if(document.all) { 
    nTecla = evtKeyPress.keyCode;
} else if(document.layers) { 
    nTecla = evtKeyPress.which;
} else {
    nTecla = evtKeyPress.which;
    if (nTecla == 8) {
        return true;
    }
}

    sValue = objeto.value;

    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( ")", "" );
    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) == "/") || (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++;
    }

    objeto.value = sCod;

    if (nTecla != 8) {
      if (sMask.charAt(i-1) == "9") {
        return ((nTecla > 47) && (nTecla < 58)); } 
      else { 
        return true;
      } 
    }
    else {
      return true;
    }
  }
 function verificaCaracter(objnome){
  caracter = objnome.value;
  if (caracter.length == 0){
    return true;
  }
  return false;
}
function verificaEmail(objnome){
  invalid = Array("ç","~","!","@","#","$","%","^","&","*","(",")","+","=","[","]",":",";",",","\"","'","|","{","}","\\","/","<",">","?"," ");
  xemail = objnome.value;
  if (xemail.indexOf("@")==-1){
    return true;
  }else{
    partes=xemail.split("@");
    if(partes[0]=="" || partes[0].length < 2){
      return true;
    }else{
      for(i=0;i < invalid.length;i++){
        if(partes[0].indexOf(invalid[i])!=-1){ return true }
      }
    }
    if(partes[1]==""){
      return true;
    }else{
      if (partes[1].indexOf(".")==-1){
        return true;
      }else{
        ponto=partes[1].split(".")
        if(ponto[0]=="" || ponto[0].length < 2){
          return true;
        }else{
          for(i=0;i < invalid.length;i++){
            if(ponto[0].indexOf(invalid[i])!=-1){ return true }
          }
        }
        if(ponto[1]==""){ return true; }
      }
    }
  }
  return false
}
function SomenteNumeros(input){
	if ((event.keyCode<48)||(event.keyCode>57))
		event.returnValue = false;
}
function FormataValor(campo,tammax,teclapres) {

	var tecla = teclapres.keyCode;
	var vr = campo.value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	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 ; }
	 	tam = tam - 1;
	 	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 ) ;}
	}
}


function FormataInteiro(campo,tammax,teclapres) {
	
	var tecla = teclapres.keyCode;
	var vr = campo.value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	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 <= 3 ){ 
	 		campo.value = vr ; }
	 	tam = tam - 1;
		if(tam >= 4 && tam <= 6){
			campo.value = vr.substr( 0, tam - 3 ) + '.' + vr.substr( tam - 3, tam ) ;
		}
		if(tam >= 7 && tam <= 9){
			campo.value = vr.substr( 0, tam - 6 ) + '.' + vr.substr( tam - 6, 3 ) + '.' + vr.substr( tam - 3, tam ) ;
		}
	}
}

function formataHora(campo){ 
	var hora01 = ''; 
	var nomeCampo = campo;
	hora01 = hora01 + campo.value;
	if (hora01.length == 2){ 
	hora01 = hora01 + ':'; 
	nomeCampo.value = hora01; 
	} 
	if (hora01.length == 5){ 
		Verifica_Hora(campo);
	} 
} 
           
function Verifica_Hora(campo){
	hrs = (campo.value.substring(0,2)); 
	minu = (campo.value.substring(3,5));
				   
	estado = ""; 
	if ((hrs < 00 ) || (hrs > 23) || ( minu < 00) || ( minu > 59)){ 
		estado = "errada"; 
	}
				   
	if (campo.value == "") { 
		estado = "errada"; 
	}
	
	if (estado == "errada") {
		alert("Hora inválida!");
		campo.focus();
		campo.value = '';
	}
}

function mostra(condicao, camada){
	var div = document.getElementById(camada);
	if(condicao.value == ""){
		div.innerHTML = '<strong style=\'color:red;\'>Incorreto</strong>';
	}else{
		div.innerHTML = '<strong style=\'color:green;\'>OK</strong>';
	}
}
function mostraqtd(condicao, camada, qtd){
	var div = document.getElementById(camada);
	if(condicao.value.length < qtd){
		div.innerHTML = '<strong style=\'color:red;\'>Incorreto</strong>';
	}else{
		div.innerHTML = '<strong style=\'color:green;\'>OK</strong>';
	}	
}
function veremail(condicao, camada){
	if(verificaEmail(condicao)){
		document.getElementById(camada).innerHTML = '<strong style=\'color:red;\'>Incorreto</strong>';
	}else{
		document.getElementById(camada).innerHTML = '<strong style=\'color:green;\'>OK</strong>';
	}
}
function conta(condicao, camada){
	var div = document.getElementById(camada);
	div.innerHTML = condicao.value.length + ' caracteres.';
}
function abre(id){
	var div=document.getElementById(id);
	if(div.className=='esconde'){
		div.className='mostra';
	}
	return(false);
}
function fecha(id){
	var divf=document.getElementById(id);
	if(divf.className=='mostra'){
		divf.className='esconde';
	}
	return(false);
}
function verifica_campos(botao){
	//seleciona todos os campos
	var todosCampos = document.getElementsByTagName('input');
	var todosCampos =+ document.getElementsByTagName('select');
	//inicializa o array obrigatorios
	var obrigatorios = Array();
	//percorre todos os elementos do array dos inputs
	for (var i=0; i<todosCampos.length; i++){
		//input recebe o valor do item atual do array todosCampos
		var input = todosCampos[i];
		if (input.className == 'obrigatorio'){
			obrigatorios.push(input);
		}
	}
	//seta o botao que sera manipulado
	var botao = document.getElementById(botao);
	
    // faz um loop pelos inputs do form
    for(i=0;(a=obrigatorios[i]);i++) {
			
		//verifica se o campo e menor que 1(vazio)
		if(a.value.lenght <= 1) {
			//desabilita o botao
			botao.disabled = true;
		} else {
			//habilita o botao
			botao.disabled =  false;
		}
	}
}

function valida(form) {
    // deve-se passar qual formulario a validar
	
	//voce deve acrescentar nos campos obrigatorios:
	//Ex.: <input type="text" name="nome" id="nome" valida="ok" ver="Seu Nome">
	
    for(i=0;(a=form.getElementsByTagName("*")[i]);i++) {
        // Aki fazemos um loop pelos inputs do form
        if(a.getAttribute("valida") == "ok") {
            // se o atribute valida é ok
            if(a.value.length == 0) {
                // Se nao foi preenchido com mais de uma letra
                alert("O campo "+ a.getAttribute("ver") +" é obrigatório");
				a.focus();
				return false;
                // retorna falso, cancelando o envio e terminando a funcao
            }
        }
    }
    return true ;
}
function verificaCampo(campo1, campo2){
	if(campo1.value != campo2.value){
		alert('Confirmação de senha não coincide.');
		return false;
	}
	return true;
	
}

// a função principal de validação
function validarDoc(obj) { // recebe um objeto
	var s = (obj.value).replace(/\D/g,'');
	var tam=(s).length; // removendo os caracteres não numéricos
	if (!(tam==11 || tam==14)){ // validando o tamanho
		alert("'"+s+"' Não é um CPF ou um CNPJ válido!" ); // tamanho inválido
		obj.focus();
		return false;
	}
	
// se for CPF
	if (tam==11 ){
		if (!validaCPF(s)){ // chama a função que valida o CPF
			alert("'"+s+"' Não é um CPF válido!" ); // se quiser mostrar o erro
			obj.select();  // se quiser selecionar o campo em questão
			obj.focus();
			return false;
		}
		//alert("'"+s+"' É um CPF válido!" ); // se quiser mostrar que validou		
		obj.value=maskCPF(s);	// se validou o CPF mascaramos corretamente
		return true;
	}
	
// se for CNPJ			
	if (tam==14){
		if(!validaCNPJ(s)){ // chama a função que valida o CNPJ
			alert("'"+s+"' Não é um CNPJ válido!" ); // se quiser mostrar o erro
			obj.select();	// se quiser selecionar o campo enviado
			obj.focus();
			return false;			
		}
		//alert("'"+s+"' É um CNPJ válido!" ); // se quiser mostrar que validou				
		obj.value=maskCNPJ(s);	// se validou o CNPJ mascaramos corretamente
		return true;
	}
}
// fim da funcao validar()

// função que valida CPF
// O algorítimo de validação de CPF é baseado em cálculos
// para o dígito verificador (os dois últimos)
// Não entrarei em detalhes de como funciona
function validaCPF(s) {
	var c = s.substr(0,9);
	var dv = s.substr(9,2);
	var d1 = 0;
	for (var i=0; i<9; i++) {
		d1 += c.charAt(i)*(10-i);
 	}
	if (d1 == 0) return false;
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(0) != d1){
		return false;
	}
	d1 *= 2;
	for (var i = 0; i < 9; i++)	{
 		d1 += c.charAt(i)*(11-i);
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(1) != d1){
		return false;
	}
    return true;
}

// Função que valida CNPJ
// O algorítimo de validação de CNPJ é baseado em cálculos
// para o dígito verificador (os dois últimos)
// Não entrarei em detalhes de como funciona
function validaCNPJ(CNPJ) {
	var a = new Array();
	var b = new Number;
	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
	for (i=0; i<12; i++){
		a[i] = CNPJ.charAt(i);
		b += a[i] * c[i+1];
	}
	if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
	b = 0;
	for (y=0; y<13; y++) {
		b += (a[y] * c[y]);
	}
	if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
	if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
		return false;
	}
	return true;
}

//	função que mascara o CPF
function maskCPF(CPF){
	return CPF.substring(0,3)+"."+CPF.substring(3,6)+"."+CPF.substring(6,9)+"-"+CPF.substring(9,11);
}

//	função que mascara o CNPJ
function maskCNPJ(CNPJ){
	return CNPJ.substring(0,2)+"."+CNPJ.substring(2,5)+"."+CNPJ.substring(5,8)+"/"+CNPJ.substring(8,12)+"-"+CNPJ.substring(12,14);	
}

function desabilitado(){
	alert('Opção desabilitada no momento, em breve teremos novidades.\n\nEntre em contato conosco pelo telefone:\n\n(44)3034-6734');
	return false;
}