function mascara(o,f){
	v_obj=o
	v_fun=f
	setTimeout("execmascara()",1)
}
function execmascara(){
	v_obj.value=v_fun(v_obj.value)
}

function ajaxInit(){
var xmlhttp ;
	try {
 		xmlhttp = new XMLHttpRequest ( ) ;
 	}catch (ee){
		try { 
		xmlhttp = new ActiveXObject ("Msxml2.XMLHTTP") ;
 		}catch( e ) {
 			try {
 				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP" ) ;
 			}catch(E) {
 				xmlhttp= false ;
 			}
 		}
 	}
 return xmlhttp ;
}
function busca(valor,teclapres){
   var tecla = 0;
   if(document.all){
        tecla = teclapres.keyCode;
    }
    else{
      tecla = teclapres.which;
      if(tecla==1){tecla=0}
    }
    if (tecla == 13 || tecla == 0){
       if(document.formBusca.txtcriterio.value==''){
			alert('Informe um critério de busca!');
			document.formBusca.txtcriterio.focus();
		}
		else{
			document.formBusca.action='?ac=busca';
			document.formBusca.submit();
		}
   } 
}
function acessarSistema(){
	if(document.form.txtCPF.value==''||document.form.txtSenha.value==''){
		alert('Informe seu CPF e Senha!');
	}else{
		document.form.action='acesso?ac=verificar_acesso';
		document.form.submit();
	}
}
function escondeDiv(div){
	document.getElementById(div).innerHTML="";
	document.getElementById(div).style.visibility="hidden";
}

function CheckNums(campo,teclapres) {
	var tecla = 0;
	
	if(document.all){
        tecla = teclapres.keyCode;
    }
    else{
      tecla = teclapres.which;
      if(tecla==1){tecla=0}
    }
	
	if ((tecla < 48) || (tecla > 57)) {
		return false;
	}
}

function validaData(pValor,nome){
	if(document.form.elements[nome].value!='' && validarDataYYYY(pValor)=='F'){
		alert('Data incorreta!');
		document.form.elements[nome].value='';
		document.form.elements[nome].focus();
	}
}

function validaEmail(email){
	exp = /^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/
	if(email.value==''||!exp.test(email.value)){
		email.value='';
				document.formulario.txtEmail.focus();
	}
}

function CaixaBaixa(fsValor, foForm, foNome) { 
	if (window.event.keyCode >= 65 && window.event.keyCode <= 90){
		x = new String(document.forms[foForm].elements[foNome].value);
		document.forms[foForm].elements[foNome].value = x.toLowerCase();
	}
} 
function enviarEmail(){
	var nome=document.formulario.txtNome.value;
	var email=document.formulario.txtEmail.value;
	var telefone=document.formulario.txtTelefone.value;
	var assunto=document.formulario.txtAssunto.value;
	var msg=document.formulario.txtMsg.value;
	
	if(nome==''){
		alert('Informe um nome!');
		document.formulario.txtNome.focus();
	}
	else if(email==''){
		alert('Informe um e-mail!');
		document.formulario.txtEmail.focus();
	}
	else if(telefone==''){
		alert('Informe um telefone!');
		document.formulario.txtTelefone.focus();
	}
	else if(assunto==''){
		alert('Informe um assunto!');
		document.formulario.txtAssunto.focus();
	}
	else if(msg==''){
		alert('Digite uma mensagem!');
		document.formulario.txtMsg.focus();
	}
	else{
		document.formulario.action='?ac=contato';
		document.formulario.submit();
	}
}

function validarDataYYYY(vValor){ 
    var checkStr = ''; 
	checkStr = vValor; 
	var err=0; 
    var psj=0; 
	var dia = 0; 
	var mes = 0; 
	var ano = 0; 
	var barra1 = ''; 
	var barra2 = ''; 
       	dia = checkStr.substring(0, 2); 
       	barra1 = checkStr.substring(2, 3); 
       	mes = checkStr.substring(3, 5); 
       	barra2 = checkStr.substring(5, 6); 
	    ano = checkStr.substring(6, 10); 
    	    //verifica??es b?sicas 
    	    if (checkStr.length != 10) 
				err = 1; 
    	    if (mes<1 || mes>12) 
				err = 1; 
    	    if (barra1 != '/') 
				err = 1; 
    	    if (dia<1 || dia>31) 
				err = 1; 
    	    if (barra2 != '/') 
				err = 1; 
    	    if (ano<1900 || ano>2900) 
				err = 1; 
    	    //verifica??es avan?adas 
    	    // m?s com 30 dias 
    	    if (mes==4 || mes==6 || mes==9 || mes==11){ 
    		   if (dia==31) 
			   		err=1; 
    	    } 
    	    // fevereiro e ano bissexto 
    	    if (mes==2){ 
    		    var g = parseInt(ano/4); 
    		    if (isNaN(g)) { 
    			    err=1; 
    		    } 
    		    if (dia>29) 
					err=1; 
    		    if (dia==29 && ((ano/4)!=parseInt(ano/4))) 
					err=1; 
    	    } 
    if (err==1) 
       return 'F'; 
	else 
	   return 'T'; 
}

function validarCPF(CPF){ 
	if (CPF != ''){ 
		if (!verificarCPF(CPF)){ 
			alert('CPF Inválido!'); 
			document.form.txtCPF.value=''; 
			document.form.txtCPF.focus(); 
		} 
	} 
}  

function verificarCPF(c){ 
	c = c.replace('.','');	
	c = c.replace('.','');	
	c = c.replace('-','');	
	var i;  
	s = c; 
	var c = s.substr(0,9);  
	var dv = s.substr(9,2);  
	var d1 = 0;  
	var v = false; 
	for (i = 0; i < 9; i++)  
	{  
		d1 += c.charAt(i)*(10-i);  
	}  
	if (d1 == 0){  
	v = true;  
	return false;  
	}  
	d1 = 11 - (d1 % 11);  
		if (d1 > 9) d1 = 0;  
			if (dv.charAt(0) != d1)  
			{  
		v = true; 
		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)  
	{  
	v = true; 
	return false;  
	}  
	if (!v) { 
	return true; 
	} 

	
}

function formatarCPF (campo,teclapres) { 
	var tecla = teclapres.keyCode;  
	if ((tecla < 48) || (tecla > 57)) {
		return false;
	} 
	vr = campo.value;  
	vr = vr.replace( '-', '' );  
	vr = vr.replace( '.', '' );  
	vr = vr.replace( '.', '' );  
	tam = vr.length; 
	 
	if (tam < 11 && 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 ; }  
	 	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 ) ; }  
	}
}

function formatarDataYYYY(campo, teclapres) { 
	var tecla = teclapres.keyCode;  
	vr = campo.value;  
	vr = vr.replace( '/', '' );  
	vr = vr.replace( '/', '' );  
	tam = vr.length + 1;  
	if ( tecla != 9 && tecla != 8 ) {  
	   if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) {  
		   if ( tam <= 2 ) campo.value = vr ;  
		   if ( tam > 2 && tam < 5 ) campo.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, tam );  
		   if ( tam >= 5 && tam <= 10 ) campo.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, tam );   
      }  
   }  
}

function paginaBanner(cont,imagem,href){
	document.getElementById("DivImgBanner").innerHTML="<img src=imagens/carregando.gif>";
	var tam = document.getElementById("cont").value;
	for(i=1;i<=tam;i=i+1){
		if(i==cont){
			document.getElementById("Href"+i).setAttribute((document.all ? "className" : "class"), "linkBannerSel");
		}else{
			document.getElementById("Href"+i).setAttribute((document.all ? "className" : "class"), "linkBanner");
		}
	}
	document.getElementById("DivImgBanner").innerHTML="<a href="+href+"><img src=imagensUpload/"+imagem+"></a>";
}

function salvarEmail(){
	if(document.form.txtNomePessoa.value == ''||document.form.txtNomePessoa.value == 'Nome'){
		alert('Informe o seu nome');
		document.form.txtNomePessoa.focus();
	}
	else if(document.form.txtEmailNewsLetter.value == ''||document.form.txtEmailNewsLetter.value == 'Email'){
		alert('Informe o email');
		document.form.txtEmailNewsLetter.focus();
	}
	else if(validaEmail(document.form.txtEmailNewsLetter.value)==false){
		alert('E-mail inválido');
		document.form.txtEmailNewsLetter.value = '';
	}
	else{
		document.form.action='?ac=cadastrar_email';
		document.form.submit();
	}
}
function cpf(v){
	v=v.replace(/\D/g,"")                    //Remove tudo o que não é dígito
	v=v.replace(/(\d{3})(\d)/,"$1.$2")       //Coloca um ponto entre o terceiro e o quarto dígitos
	v=v.replace(/(\d{3})(\d)/,"$1.$2")       //Coloca um ponto entre o terceiro e o quarto dígitos
	//de novo (para o segundo bloco de números)
	v=v.replace(/(\d{3})(\d{1,2})$/,"$1-$2") //Coloca um hífen entre o terceiro e o quarto dígitos
	return v
}
function validaEmail(email){
	exp = /^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/
	if(email.value==''||!exp.test(email.value)){
		email.value='';
		alert('E-mail Inválido!'); 
	}
}
function telefone(v){
	v=v.replace(/\D/g,"")                 //Remove tudo o que não é dígito
	v=v.replace(/^(\d\d)(\d)/g,"($1) $2") //Coloca parênteses em volta dos dois primeiros dígitos
	v=v.replace(/(\d{4})(\d)/,"$1-$2")    //Coloca hífen entre o quarto e o quinto dígitos
	return v
}
function carregaPorcoes(id){
	var ajax=ajaxInit();
	document.getElementById("DivPorcoes").innerHTML="<center><img src=\"imagens/carregando.gif\"></center>";
	if(ajax){
		ajax.open('GET','?ac=formPorcao&id='+id,true);
		ajax.onreadystatechange=function(){
			if(ajax.readyState==4){
				if(ajax.status==200){
					document.getElementById("DivPorcoes").innerHTML = ajax.responseText;
				}
			}
		}
	}
	ajax.send(null);
}
function adicionaPorcao(id){
	var ajax=ajaxInit();
	var idPeso;
	var radios = document.getElementsByName("rd"+id);
	for(i=0;i<radios.length;i++){
		if(radios[i].checked){
			idPeso = radios[i].value;
			break;
		}
	}
	document.getElementById("DivPedido").innerHTML="<center><img src=\"imagens/carregando.gif\"></center>";
	if(ajax){
		ajax.open('GET','?ac=add&id='+id+'&number='+RandomNumber()+'&idPeso='+idPeso,true);
		ajax.onreadystatechange=function(){
			if(ajax.readyState==4){
				if(ajax.status==200){
					document.getElementById("DivPedido").innerHTML = ajax.responseText;
					location.href='#scroll';
				}
			}
		}
	}
	ajax.send(null);
}
function excluirPorcao(id){
	var ajax=ajaxInit();
	document.getElementById("DivPedido").innerHTML="<center><img src=\"imagens/carregando.gif\"></center>";
	if(ajax){
		ajax.open('GET','?ac=remove&cont='+id,true);
		ajax.onreadystatechange=function(){
			if(ajax.readyState==4){
				if(ajax.status==200){
					document.getElementById("DivPedido").innerHTML = ajax.responseText;
					location.href='#scroll';
				}
			}
		}
	}
	ajax.send(null);
}
function fecharPedido(){
	var nome=document.formPedido.txtNome.value;
	var email=document.formPedido.txtEmail.value;
	var telefone=document.formPedido.txtTelefone.value;
	var endereco=document.formPedido.txtEndereco.value;
	var complemento=document.formPedido.txtComplemento.value;
	var bairro=document.formPedido.txtBairro.value;
	var cidade=document.formPedido.txtCidade.value;
	
	if(nome==''){
		alert('Informe o seu nome!');
		document.formPedido.txtNome.focus();
	}
	else if(email==''){
		alert('Informe um e-mail!');
		document.formPedido.txtEmail.focus();
	}
	else if(telefone==''){
		alert('Informe um telefone!');
		document.formPedido.txtTelefone.focus();
	}
	else if(endereco==''){
		alert('Informe o seu endereço!');
		document.formPedido.txtEndereco.focus();
	}
	else if(complemento==''){
		alert('Informe o complemento do seu endereço (casa, apartamento...)!');
		document.formPedido.txtComplemento.focus();
	}
	else if(bairro==''){
		alert('Informe o bairro!');
		document.formPedido.txtBairro.focus();
	}
	else if(cidade==''){
		alert('Informe a sua cidade!');
		document.formPedido.txtCidade.focus();
	}
	else{
		document.formPedido.action='?ac=fecharPedido';
		document.formPedido.submit();
	}
}
function RandomNumber() {
	today = new Date();
	num= Math.abs(Math.sin(today.getTime()));
	return num;  
}
