var $j = jQuery.noConflict();
$j(document).ready(function(){
    $j('.MenuCat li > ul > li > ul ').hide();
    $j('.MenuCat li > ul > li > a').click(function(){
           $j(this).next().slideToggle('show', function() {
                return false;
           });
        }
    )
// $j().jSnow();
//    $j().jSnow({
//	flakes:500, // número de flocos na tela;
//	flakeMaxSize:15, //tamanho máximo dos flocos de neve
//	flakeMinSize:5, // Tamanho mínimo dos flocos de neve
//	flakeCode:["•"],  // elementoo a ser colocado, pode colocar fotos também
//	flakeColor:["#fff"], // cor do elemento
//	interval:10, // intervalo de tempo na reprodução dos flocos
//	fallingSpeedMin:15, // velocidade mínima de de queda de neve
//	fallingSpeedMax:30, //velocidade máxima de de queda de neve
//	swingMin:100, //movimento mínimo
//	swingMax:500 //movimento máximo
//    });

});

function mascara(e,src,mask) {

    if(window.event) {_TXT = e.keyCode;} 

    else if(e.which) {_TXT = e.which;}

    if(_TXT > 47 && _TXT < 58) { 

 var i = src.value.length;var saida = mask.substring(0,1);var texto = mask.substring(i)

 if (texto.substring(0,1) != saida) {src.value += texto.substring(0,1);} 

    return true;} else {if (_TXT != 8) {return false;} 

 else {return true;}

    }

}

function validaCpf(){
   if($F('txtcpf')){
   var cpf = $F('txtcpf');
   var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
   if(!filtro.test(cpf)){
    showDialog('Aten&ccedil;&atilde;o','<br /><br /><b>CPF</b> inv&aacute;lido. Tente novamente.<br /><br />','warning');
	 return false;
   }

   cpf = remove(cpf, ".");
   cpf = remove(cpf, "-");

   if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
	  cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
	  cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
	  cpf == "88888888888" || cpf == "99999999999"){
	   showDialog('Aten&ccedil;&atilde;o','<br /><br /><b>CPF</b> inv&aacute;lido. Tente novamente.<br /><br />','warning');
	  return false;
   }

   soma = 0;
   for(i = 0; i < 9; i++)
   	 soma += parseInt(cpf.charAt(i)) * (10 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(9))){
	 showDialog('Aten&ccedil;&atilde;o','<br /><br /><b>CPF</b> inv&aacute;lido. Tente novamente.<br /><br />','warning');
	 return false;
   }
   soma = 0;
   for(i = 0; i < 10; i ++)
	 soma += parseInt(cpf.charAt(i)) * (11 - i);
   resto = 11 - (soma % 11);
   if(resto == 10 || resto == 11)
	 resto = 0;
   if(resto != parseInt(cpf.charAt(10))){
     showDialog('Aten&ccedil;&atilde;o','<br /><br /><b>CPF</b> inv&aacute;lido. Tente novamente.<br /><br />','warning');
	 return false;
   }
   return true;
 }
 }

  function remove(str, sub) {
   i = str.indexOf(sub);
   r = "";
   if (i == -1) return str;
   r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
   return r;
 }




function buscacep(cep){
	if($F('txtcep')){
		var url = '/include/requestAjax.php';
		var params = 'request=cep&cep='+$F('txtcep');
		new Ajax.Request( url, {method: 'get', parameters: params, onLoading: carregando, onComplete: escreve}); 
	}else{
		
		$('txtrua').value = '';
		$('txtcidade').value = '';
		$('txtbairro').value = '';
		$('txtestado').value = '';
	}
}


function cadnewsletter(){
        
       if($F('newsnome') == "Digite seu nome..." ){
          
			showDialog('Aten&ccedil;&atilde;o',"<br />Campo nome deve ser preenchido!",'warning');
		
        }else{
            if($F('newsemail') == "Digite seu e-mail..." ){
			showDialog('Aten&ccedil;&atilde;o',"<br />Campo e-mail deve ser preenchido!",'warning');
		
            }else{
                if(!checkMail($F('newsemail'))){
                      showDialog('Aten&ccedil;&atilde;o',"<br /><b>E-mail</b> inv&aacute;lido, verifique o e-mail digitado!<br />",'warning');
                }else{
                    var url = '/include/requestAjax.php';
                    var params = 'request=newsletter&newsnome='+$F('newsnome')+'&newsemail='+$F('newsemail');
                    new Ajax.Request( url, {method: 'get', parameters: params, onLoading: carregandonews, onComplete: function(request){
                        if(request.responseText == 0){
                            $("newsletter").innerHTML="<span>E-mail já cadastrado!</span>";
                        }else{
                            $("newsletter").innerHTML="<span>E-mail cadastrado com sucesso!</span>";
                        }

                        }
                    })
                }

            }
            
        }

}

function carregandonews(){
	$("carregando").innerHTML="<img src='/image/loading.gif' style='padding: 12px;'>";
}



function escreve(request){
	eval(unescape(request.responseText));
	if(resultadoCEP['resultado'] == 1){ 
		$("carregando").style.display="none";
		$('txtrua').value = resultadoCEP['logradouro'];
		$('txtcidade').value = resultadoCEP['cidade'];
		$('txtbairro').value = resultadoCEP['bairro'];
		$('txtestado').value = resultadoCEP['uf'];
		$('txtnumero').focus();
	}else{
  	   showDialog('Aten&ccedil;&atilde;o','<br /><b>CEP</b> n&atilde;o encontrado, verifique o <b>CEP</b> digitado!<br />','warning');
		$("carregando").style.display="none";
	}
}


function carregando(){
	$("carregando").innerHTML="<img src='/image/loading.gif'>";
}
function carregandoemail(){
	$("carregandoemail").innerHTML="<img src='/image/loading.gif'>";
}
function carregandologin(){
	$("carregandologin").innerHTML="<img src='/image/loading.gif'>";
}

function validaLogin(){
	
	if($F('txtlogin').length < 6){
		if($F('txtlogin') != ""){
			showDialog('Aten&ccedil;&atilde;o',"<br />O login deve ter no m&iacute;mino 6  caracteres.",'warning');
			$('txtlogin').value = '';
			$('txtlogin').focus();
		}
	}else{
	
	var url = '/include/requestAjax.php';
	var params = 'request=buscalogin&login='+$F('txtlogin');
	new Ajax.Request( url, {method: 'get', parameters: params, onLoading: carregandologin, onComplete: function(request){
		if(request.responseText == 1){
			 showDialog('Aten&ccedil;&atilde;o',"<br /><b>Login</b> j&aacute; existe!<br />Caso tenha esquecido a senha <a href='http://cachacaria.setequatro.com.br/esqueci'><b>Clique Aqui</b></a><br />",'warning');

			$('txtlogin').value = '';
			$('txtlogin').focus();
	

		}
			$("carregandologin").style.display="none";
	

	}

	}); 	
	}

}

function checkMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
        if(er.test(mail)){return true;}
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){ 
                    return true; 
                }
    }else{
        return false;
    }
}

function validaEmail(){
	
	if($F('txtemail') != ""){
	
	if(checkMail($F('txtemail'))){
	
	var url = '/include/requestAjax.php';

	var params = 'request=buscaemail&email='+$F('txtemail');
	new Ajax.Request( url, {method: 'get', parameters: params, onLoading: carregandoemail, onComplete: function(request){

		if(request.responseText == 1){

		 showDialog('Aten&ccedil;&atilde;o',"<br /><b>E-mail</b> j&aacute; existe!<br />Caso tenha esquecido a senha <a href='http://cachacaria.setequatro.com.br/esqueci'><b>Clique Aqui</b></a><br />",'warning');

			$('txtemail').value = '';
			$('txtemail').focus();
		}
		$("carregandoemail").style.display="none";
	}

	}); 	
	
	}else{
		 showDialog('Aten&ccedil;&atilde;o',"<br /><b>E-mail</b> inv&aacute;lido, verifique o e-mail digitado!<br />",'warning');
		$('txtemail').value = '';
		$('txtemail').focus();
	}
	}
}


function confsenha(){
	
	if($F('txtsenha').length < 6){
		if($F('txtsenha') != ""){
			showDialog('Aten&ccedil;&atilde;o',"<br />A senha deve ter no m&iacute;mino 6  caracteres.",'warning');
		$('txtconfsenha').value = "";
		$('txtsenha').value = "";
		}
	
	}else{
	 
	 if($F('txtsenha') != $F('txtconfsenha')){
		 
		 
		$('txtconfsenha').value = "";
		$('txtsenha').value = "";
//		$('txtsenha').focus();
		 showDialog('Aten&ccedil;&atilde;o','<br />A <b>Senha</b> digitada n&atilde;o coencide<br /><br />','warning');

	}
	}
 }
 
 

String.prototype.trim=function(){
  nullo = this.replace(/^ +| +$/g,"")
  if(nullo == ""){
	  return true;
  }else{
	  return false;
  }
}

function validaForm(nomeform){

    var form = $(nomeform);
    var inputs = form.getElements();
    var result = true;
    var msg = '';
	var cont = 0;
	
    for (i in inputs) {

        if (inputs[i].className) {				
            if (inputs[i].className == 'obr-vazio') {
 		        if (inputs[i].value.trim()) {
					if(cont <= 11){		   
	                	msg = msg + " - "+ inputs[i].title + "<br />";
                    	result = false;
						cont = cont + 1;
					}
                }
				 
            }
			        
        }
    }


	 var msg = msg + '<br />';
	 
	if(!result){
		 showDialog('Campo abaixo s&atilde;o obrigat&oacute;rios:',msg,'warning');
		return result;
	}else{
	    return result;
	}
} 


