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; }

    }

}




var expDays = 365;
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));
function bgelcio(bg){
	var bgfundo = GetCookie('bgelcio');
	if (bgfundo == null) {
		SetCookie('bgelcio', bg, exp);
		document.body.className = bg;
	}else{
		document.body.className = bgfundo;
	}
}

function set(bg){
		SetCookie ('bgelcio', bg, exp);
		if(navigator.appName.indexOf("Microsoft") != -1){ //IE
	    //pegando a versão...
    	var arr = navigator.appVersion.split(";");
	    if(arr[1].indexOf("6.0") != -1){
			history.go();
		}else{
			$('site').className = bg;
		}
		}
		$('site').className = bg;
}

function getCookieVal (offset) {  
		var endstr = document.cookie.indexOf (";", offset);  
	if (endstr == -1)    
		endstr = document.cookie.length;  
		return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {  
		var arg = name + "=";  
		var alen = arg.length;  
		var clen = document.cookie.length;  
		var i = 0;  
		while (i < clen) {    
			var j = i + alen;    
			if (document.cookie.substring(i, j) == arg)      
				return getCookieVal (j);    
				i = document.cookie.indexOf(" ", i) + 1;    
			if (i == 0) break;   
			}  
			return null;
}
function SetCookie (name, value) {  
		var argv = SetCookie.arguments;  
		var argc = SetCookie.arguments.length;  
		var expires = (argc > 2) ? argv[2] : null;  
		var path = (argc > 3) ? argv[3] : null;  
		var domain = (argc > 4) ? argv[4] : null;  
		var secure = (argc > 5) ? argv[5] : false;  
		document.cookie = name + "=" + escape (value) + 
		((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
		((path == null) ? "" : ("; path=" + path)) +  
		((domain == null) ? "" : ("; domain=" + domain)) +    
		((secure == true) ? "; secure" : "");
}
function DeleteCookie (name) {  
	var exp = new Date();  
	exp.setTime (exp.getTime() - 1);  
	var cval = GetCookie (name);  
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}


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 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://www.elciofernando.com.br/esqueci_senha'><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;
	}
} 


