function Mascara (formato, objeto, e){
	campo = eval (objeto);

     var key;
     if(window.event){
          key = window.event.keyCode;     //IE
     }else{
          key = e.which;     //firefox
     }    
     
	// DATA(dd/mm/aaaa)
	if (formato=='DDMMYYYY'){
		separador = '/'; 
		conjunto1 = 2;
		conjunto2 = 5;
		if (key >= 48 && key <= 57){
			if (campo.value.length == conjunto1){
				campo.value = campo.value + separador;
			}
			if (campo.value.length == conjunto2){
				campo.value = campo.value + separador;
			}
		}else{
			key = 0;}
	}

	// DDMM (dd/mm)
	if (formato=='DDMM'){
		separador = '/'; 
		conjunto1 = 2;
		conjunto2 = 5;
		if (key >= 48 && key <= 57){
			if (campo.value.length == conjunto1){
				campo.value = campo.value + separador;
			}
		}else{
			key = 0;}
	}
	
	// HORA(hh:mm)
	if (formato=='HHMM'){
		separador = ':'; 
		conjunto1 = 2;
		if (key >= 48 && key <= 57){
			if (campo.value.length == conjunto1){
				campo.value = campo.value + separador;
			}
		}else{
			key = 0;}
	}

	// CEP(00000-000)
	if (formato=='CEP'){
		separador = '-'; 
		conjunto1 = 5;
		if (key >= 48 && key <= 57){
			if (campo.value.length == conjunto1){
				campo.value = campo.value + separador;
			}
			return true;
		}else{
		    //alert('key 2: '+key);
			key = 0;
			return false;
		}
	}

	// CPF(000.000.000-00)
	if (formato=='CPF'){
		separador1 = '.'; 
		separador2 = '-'; 
		conjunto1 = 3;
		conjunto2 = 7;
		conjunto3 = 11;
		if (key >= 48 && key <= 57){
			if (campo.value.length == conjunto1){
				campo.value = campo.value + separador1;
			}
			if (campo.value.length == conjunto2){
				campo.value = campo.value + separador1;
			}
			if (campo.value.length == conjunto3){
				campo.value = campo.value + separador2;
			}
			return true;
		}else{
			key = 0;
			return false;
		}
	}

	// CNPJ(000.000.000-00)
	if (formato=='CNPJ'){
		separador1 = '.'; 
		separador2 = '/'; 
		separador3 = '-'; 		
		conjunto1 = 2;
		conjunto2 = 6;
		conjunto3 = 10;
		conjunto4 = 15;		
		if (key >= 48 && key <= 57){
			if (campo.value.length == conjunto1){
				campo.value = campo.value + separador1;
			}
			if (campo.value.length == conjunto2){
				campo.value = campo.value + separador1;
			}
			if (campo.value.length == conjunto3){
				campo.value = campo.value + separador2;
			}
			if (campo.value.length == conjunto4){
				campo.value = campo.value + separador3;
			}
			return true;
		}else{
			key = 0;
			return false;
		}
	}


	// TELEFONE(00)0000-0000
	if (formato=='TELEFONE'){
		separador1 = '('; 
		separador2 = ')'; 
		separador3 = '-'; 		
		conjunto0 = 0;
		conjunto1 = 3;
		conjunto2 = 8;
		if (key >= 48 && key <= 57){
			if (campo.value.length == conjunto0){
				campo.value = campo.value + separador1;
			}
			if (campo.value.length == conjunto1){
				campo.value = campo.value + separador2;
			}
			if (campo.value.length == conjunto2){
				campo.value = campo.value + separador3;
			}
		}else{
			key = 0;}
	}


	// PLANOCONTAS(0.00.00)
	if (formato=='PLANOCONTAS'){
		separador1 = '.'; 
		conjunto1 = 2;
		conjunto2 = 5;
		conjunto3 = 8;
		conjunto4 = 11;
		if (key >= 48 && key <= 57){
			if (campo.value.length == conjunto1){
				campo.value = campo.value + separador1;
			}
			if (campo.value.length == conjunto2){
				campo.value = campo.value + separador1;
			}
			if (campo.value.length == conjunto3){
				campo.value = campo.value + separador1;
			}
			if (campo.value.length == conjunto4){
				campo.value = campo.value + separador1;
			}
		}else{
			key = 0;}
	}
}

function limpa_string(S){
// Deixa so' os digitos no numero
var Digitos = "0123456789";
var temp = "";
var digito = "";
    for (var i=0; i<S.length; i++){
      digito = S.charAt(i);
      if (Digitos.indexOf(digito)>=0){
        temp=temp+digito;
      }
    }
    return temp;
}
function valida_CPF(s)
{
	var i;
	s = limpa_string(s);
	var c = s.substr(0,9);
	var dv = s.substr(9,2);
	var d1 = 0;
	for (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 (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;
}

function valida_CNPJ(s){
	/*s = eval(s);*/
	var i;
	s = limpa_string(s);
	var c = s.substr(0,12);
	var dv = s.substr(12,2);
	var d1 = 0;
	for (i = 0; i < 12; i++)
	{
		d1 += c.charAt(11-i)*(2+(i % 8));
	}
        if (d1 == 0) return false;
        d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(0) != d1)
	{
		return false;
	}

	d1 *= 2;
	for (i = 0; i < 12; i++)
	{
		d1 += c.charAt(11-i)*(2+((i+1) % 8));
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(1) != d1)
	{
		return false;
	}
        return true;
}


/* EXIBE UM TEXTO EM UM ID */
function ajuda(texto){
    document.getElementById("ajuda").innerHTML = texto;
}

/* EXIBE ICONE DE OK NO CAMPO DIGITADO */
function campo_ok(msg){
    document.getElementById(msg).innerHTML = "<img src='imgs/icone_ok.gif'>";
    val = form1.validacao.value;
    val = parseInt(val) + 1;
    form1.validacao.value = val;    
}

/* EXIBE ICONE DE ERRO NO CAMPO DIGITADO */
function campo_erro(msg){
    document.getElementById(msg).innerHTML = "<img src='imgs/icone_erro.gif'>";
    val = form1.validacao.value;
    val = parseInt(val)- 1;
    form1.validacao.value = val;
}

/* FAZ A VALIDAÇÃO DO CAMPO INDICADO */
function validar(campo,msg)
{
    obj = eval (campo);
	if (obj.value.length == 0){
		campo_erro(msg); 
	}else{
	    campo_ok(msg); 
	}
}
function encontrar_cep() {
    window.open ("http://www.cep.com.br","nova","hotkeys=0,resizable=yes,height=450,width=550,screenx=1,screeny=1,scrollbars=yes"); 
}
function lembrar_senha() {
    window.open ("lembrar_senha.aspx","nova1","hotkeys=0,resizable=no,height=180,width=400,screenx=1,screeny=1,scrollbars=no"); 
}

/***********************************************************************
06.07.07 | VALIDAÇÃO DO FORMULÁRIO DE LOGIN
************************************************************************/

function login(Formulario){	

    var msg='';
    
	// valida Email
 	if(Formulario.email.value.indexOf("@",0) == -1 || Formulario.email.value.indexOf(".", 0) == -1){
		msg += "* O Email não é válido. Digite o seu e-mail corretamente.\n";
	}
	
	// valida A senha
	if (Formulario.senha.value.length == 0){
	    msg += "* Digite a senha.\n";
	}
	
	if (msg==''){
	    return true;
	}else{
	    alert(msg);
	    return false;
	}  	
}
	

/***********************************************************************
28.06.07 | VALIDAÇÃO DO FORMULÁRIO DE CADASTRO EM JS
************************************************************************/

function cadastro(Formulario){

    var msg='';
    
	// valida Email
 	if(Formulario.email.value.indexOf("@",0) == -1 || Formulario.email.value.indexOf(".", 0) == -1){
		msg += "* O Email não é válido. Digite o seu e-mail corretamente.\n";
	}
	
	// valida A senha
	if (Formulario.senha.value.length == 0){
	    msg += "* Digite a senha.\n";
	}
	if (Formulario.senha.value != Formulario.confirma_senha.value){
	    msg += "* A senha digitada não confere.\n";
	}

	// valida o Nome
	if (Formulario.nome.value.length == 0 && Formulario.pj.checked == true){
	    msg += "* Digite o nome da empresa.\n";
	}		
	
	// valida o Nome
	if (Formulario.nome.value.length == 0 && Formulario.pf.checked == true){
	    msg += "* Digite o nome.\n";
	}
	if (Formulario.nome.value.length == 0 && Formulario.pj.checked == true){
	    msg += "* Digite o nome do contato.\n";
	}	
	
	// valida o CNPJ / CPF
	if (Formulario.pj.checked == true){
	    if (valida_CNPJ(Formulario.cnpj.value) == false){
	        msg += "* Digite o CNPJ corretamente.\n";
	    }
	    if (Formulario.ie.value.length == 0) {
	        msg += "* Digite a inscrição estadual.\n";
	    }	    
	}else{
	    if (valida_CPF(Formulario.cnpj.value) == false){
	        msg += "* Digite o CPF corretamente.\n";
	    }
	    if (Formulario.ie.value.length == 0) {
	        msg += "* Digite o RG.\n";
	    }
	}	

	// valida o telefone
	if (Formulario.telefone.value.length == 0){
	    msg += "* Digite o telefone.\n";
	}
	
	// valida a data de nascimento
	var diaI = Formulario.dia.selectedIndex;
	var diaV = Formulario.dia.options[diaI].text;	
	var mesI = Formulario.mes.selectedIndex;
	var mesV = Formulario.mes.options[mesI].value;	
	var anoI = Formulario.ano.selectedIndex;
	var anoV = Formulario.ano.options[anoI].text;
	if (Formulario.pf.checked == true){		
	    if (validaData(diaV+'/'+mesV+'/'+anoV)==false){
	        msg += "* Selecione corretamente a data de nascimento\n";
	    }
	}
	
	// valida o Sexo
	if (Formulario.pf.checked == true){		
	    if (Formulario.sexo[0].checked == false && Formulario.sexo[1].checked == false){
	        msg += "* Digite o sexo.\n";
	    }
	}
	
	// valida o endereco
	if (Formulario.endereco.value.length == 0){
	    msg += "* Digite o endereço.\n";
	}	
	
	// valida o número
	if (Formulario.numero.value.length == 0){
	    msg += "* Digite o nº.\n";
	}	
	
	// valida o cidade
	if (Formulario.cidade.value.length == 0){
	    msg += "* Digite a cidade.\n";
	}
		
	// valida o estado	
	var ufI = Formulario.estado.selectedIndex;
	var uf = Formulario.estado.options[ufI].text;
	if (uf.length == 0){
	    msg += "* Selecione o estado.\n";
	}
			
	// valida o bairro
	if (Formulario.bairro.value.length == 0){
	    msg += "* Digite o bairro.\n";
	}	
	
	// valida o cep
	if (Formulario.cep.value.length == 0){
	    msg += "* Digite o CEP.\n";
	}	
		
	if (msg==''){
	    return true;
	}else{
	    alert(msg);
	    return false;
	}  
	  	
}

/***********************************************************************
28.06.07 | VALIDAÇÃO DE DATAS
************************************************************************/

function validaData(digData) 
{
    var bissexto = 0;
    var data = digData; 
    var tam = data.length;
    if (tam == 10) 
    {
        var dia = data.substr(0,2)
        var mes = data.substr(3,2)
        var ano = data.substr(6,4)
        if ((ano > 1900)||(ano < 2100))
        {
            switch (mes) 
            {
                case '01':
                case '03':
                case '05':
                case '07':
                case '08':
                case '10':
                case '12':
                    if  (dia <= 31) 
                    {
                        return true;
                    }
                    break
                
                case '04':        
                case '06':
                case '09':
                case '11':
                    if  (dia <= 30) 
                    {
                        return true;
                    }
                    break
                case '02':
                    /* Validando ano Bissexto / fevereiro / dia */ 
                    if ((ano % 4 == 0) || (ano % 100 == 0) || (ano % 400 == 0)) 
                    { 
                        bissexto = 1; 
                    } 
                    if ((bissexto == 1) && (dia <= 29)) 
                    { 
                        return true;                 
                    } 
                    if ((bissexto != 1) && (dia <= 28)) 
                    { 
                        return true; 
                    }            
                    break                        
            }
        }
    }    
    return false;
}


/*******************************************************
28.06.07 | MUDA O FOCO NO CAMPO CEP DO CARRINHO
********************************************************/

function cep() {

    if (form1.cep.value.length == 9) { 
          form1.calcular.focus();
    }  
}

/*****************************************************************
28.06.07 | MUDA A AÇÃO DO ENTER PARA O CLICK DO BOTÃO INFORMADO
******************************************************************/

/*function EnterClick(btn){ 
    if (event.keyCode == 13){ 
        event.returnValue=false; 
        event.cancel = true; 
        btn.click(); 
    } 
} */

function EnterClick(btn,e){ 

     var key;
     if(window.event){
          key = window.event.keyCode;     //IE
     }else{
          key = e.which;     //firefox
     }     
    if (key == 13){ 
        if(window.event){ //IE
            event.returnValue=false; 
        }else{ //firefox
            key.preventDefault = false;
        }             
        e.cancel = true; 
        btn.click(); 
    }
} 

function url(){                
    var campo = document.form1.Topo2_busca.value.length;
    if (campo==0){
        return false;
    }else{    
        parent.location = 'produtos.aspx?busca='+document.form1.Topo2_busca.value;
        return false;
    }
}


/*****************************************************************
28.06.07 | BLOQUEIA A TECLA ENTER EM UM TEXTBOX
******************************************************************/

function bloqueiaEnter(){
    if(window.event.keyCode == 13)
    return false;
}

/*****************************************************************
05.07.07 | PERMITE APENAS CARACTERES NUMÉRICOS
******************************************************************/

function apenasNumeros() {
    /*if (event.keyCode == 13) {
        form1.calcular.focus();
        return true
    }*/
    if (!(event.keyCode >= 48 && event.keyCode <= 57)){
        return false
    }else{
        return true
    }
    
}

/*****************************************************************
ABRE JANELA PARA GRAVAR EMAIL PARA NEWSLETTER
******************************************************************/
function gn() {
    window.open('grava_newsletter.aspx?email=' + document.form1.uc2_email.value, 'gn', 'hotkeys=0,resizable=yes,height=200,width=350,screenx=1,screeny=1,scrollbars=no');
}


function marcarEndereco() {
    if (document.form1.enderecoIgual.checked == true) {
        document.form1.eEndereco.value = document.form1.endereco.value;
        document.form1.eNumero.value = document.form1.numero.value;
        document.form1.eCidade.value = document.form1.cidade.value;
        document.form1.eBairro.value = document.form1.bairro.value;
        document.form1.eCep.value = document.form1.cep.value;
        document.form1.ePais.value = document.form1.pais.value;
        document.form1.eEstado.value = document.form1.estado.value;
    }
}
