// JavaScript Document

$(document).ready(function() {
	
    //Ativa LightBox para 
	$(function() {
        $('#detalheFoto a').lightBox();
    });
	
	$(function() {
        $('#meioNovoDetalhe1 a').lightBox();
    });
	

      (function($){
	      $(
		function(){
			$('input:text').setMask();
		}
	  );
      })(jQuery);


	$('.abrirJanelaBoleto').click(function(){
	   //window.open(this.alt, 'Gerar Boleto', "height = 600, width = 1000, scrollbars = yes");
	   window.open(this.alt, 'Gerar Boleto', 'STATUS=NO, TOOLBAR=NO, LOCATION=NO, DIRECTORIES=NO, RESISABLE=NO, SCROLLBARS=YES, TOP=10, LEFT=10, WIDTH=1000, HEIGHT=600');
	})
	
	
	$('.abrirJanela').click(function(){
		//alert(this.alt);
		var largura = 1000; var altura = 600;
	    //void window.open("index.php", "Gerar Boleto", "scroolbars=no, resiseable=no, width="+largura+", height="+altura+", left=150, top=100");
	    
		window.open(this.alt, 'Pagina', 'STATUS=NO, TOOLBAR=NO, LOCATION=NO, DIRECTORIES=NO, RESISABLE=NO, SCROLLBARS=YES, TOP=10, LEFT=10, WIDTH=1100, HEIGHT=600');
		
		
		//void window.open('index.php',"Fotos","scroolbars=no, resiseable=no, width="+largura+", height="+altura+", left=150, top=100");

	})
	
	


	
/***********************  ADD E REMOVE ICONE DE PRESENTE   ************************
####################################################################################*/	

    // #### Seleciona os eventos 	
	$('.presenteOff , .presenteOn').click(function(){
		
		var value = document.getElementById('prodPresente_'+this.value).value;
		
		if(value == '0' || value == ''){
		   $(this).addClass('presenteOn');
		   $(this).removeClass('presenteOff');
		   $('#prodPresente_'+this.value).val('1');
		} else {
		   $(this).addClass('presenteOff');
		   $(this).removeClass('presenteOn');
		   $('#prodPresente_'+this.value).val('0');
		}
		
		
		upadatePrecosCarrinho(this.value);
	
	});
	
	
	
	// functions
	function upadatePrecosCarrinho(linha){
		
	   	
		
	   document.getElementById('freteSelecionado').value = '';	
	   
	   
	   
	   
	   var totalPorProd_ = document.getElementById('totalPorProd_'+linha).value;
	   
	   var minLinhas_    = document.getElementById('minLinhas_'+linha).value;
	   var maxLinhas_    = document.getElementById('maxLinhas_'+linha).value;
		
	   var prodPresente_ = document.getElementById('prodPresente_'+linha).value;
	   var qtd_          = parseInt(document.getElementById('qtd_'+linha).value);
	   var vUnPresente_  = parseFloat(document.getElementById('vUnPresente_'+linha).value);
	   var valorTotal    = 0.00;
	   
		var soma1 = 0;
	    
	   for(i=minLinhas_; i<=maxLinhas_; i++){
		    
		  qtde =  parseInt(document.getElementById('qtd_'+i).value); 
		 
		  	soma1 = soma1 + qtde;
	      
	   }
	   

	    
	   
	  
	  
		
	   var minP_          = 1;
	   var minM_          = parseInt(document.getElementById('minM_'+linha).value);
	   var minG_          = parseInt(document.getElementById('minG_'+linha).value);
	   
	   var letra         = 'P';
	   
	   
       if(soma1 >= minG_) {letra = 'G';}
	     else if(soma1 >= minM_) {letra = 'M';}
		 else letra = 'P';
	   

       if(prodPresente_ == '1'){
		   valorUnitario = parseFloat(document.getElementById('prodComPresente'+letra+'_'+linha).value);	

	   } else {
	       valorUnitario = parseFloat(document.getElementById('produtoSemPresente'+letra+'_'+linha).value);	

	   }
	   
	   
	   
	   
	   valorTotal        = valorUnitario * qtd_;
	   valorUnitPresente = qtd_ * vUnPresente_;
	   
	   // atualiza valor unitário
	   document.getElementById('valorUn_'+linha).value = valorUnitario.toFixed(2);
	   
	   // atualiza valor total do presente
	   document.getElementById('valorPresente_'+linha).value = valorUnitPresente.toFixed(2);
	   
	   // atualiza valor total
	   document.getElementById('valorTotal_'+linha).value = valorTotal.toFixed(2);
	   
	  
	  
	   for(w=minLinhas_; w<=maxLinhas_; w++){
		    
		  upadatePrecosCarrinho2(w); 
	      
	   }
	   
	   
	}
	
	
	
	
	
	
	// functions2
	function upadatePrecosCarrinho2(linha){
		
	   	
		
	   document.getElementById('freteSelecionado').value = '';	
	   
	   
	   
	   
	   var totalPorProd_ = document.getElementById('totalPorProd_'+linha).value;
	   
	   var minLinhas_    = document.getElementById('minLinhas_'+linha).value;
	   var maxLinhas_    = document.getElementById('maxLinhas_'+linha).value;
		
	   var prodPresente_ = document.getElementById('prodPresente_'+linha).value;
	   var qtd_          = parseInt(document.getElementById('qtd_'+linha).value);
	   var vUnPresente_  = parseFloat(document.getElementById('vUnPresente_'+linha).value);
	   var valorTotal    = 0.00;
	   
		var soma1 = 0;
	    
	   for(i=minLinhas_; i<=maxLinhas_; i++){
		    
		  qtde =  parseInt(document.getElementById('qtd_'+i).value); 
		 
		  	soma1 = soma1 + qtde;
	      
	   }
	   

	    
	   
	  
	  
		
	   var minP_          = 1;
	   var minM_          = parseInt(document.getElementById('minM_'+linha).value);
	   var minG_          = parseInt(document.getElementById('minG_'+linha).value);
	   
	   var letra         = 'P';
	   
	   
       if(soma1 >= minG_) {letra = 'G';}
	     else if(soma1 >= minM_) {letra = 'M';}
		 else letra = 'P';
	   

       if(prodPresente_ == '1'){
		   valorUnitario = parseFloat(document.getElementById('prodComPresente'+letra+'_'+linha).value);	

	   } else {
	       valorUnitario = parseFloat(document.getElementById('produtoSemPresente'+letra+'_'+linha).value);	

	   }
	   
	   
	   
	   
	   valorTotal        = valorUnitario * qtd_;
	   valorUnitPresente = qtd_ * vUnPresente_;
	   
	   // atualiza valor unitário
	   document.getElementById('valorUn_'+linha).value = valorUnitario.toFixed(2);
	   
	   // atualiza valor total do presente
	   document.getElementById('valorPresente_'+linha).value = valorUnitPresente.toFixed(2);
	   
	   // atualiza valor total
	   document.getElementById('valorTotal_'+linha).value = valorTotal.toFixed(2);
	   
	  

	   
	   
	}
	
	
	
	
	  
	  
	// calcula o valor total do carrinho  
    function calculaTotalCarrinho(cont){
	   
	   var total = 0.00;
	   for(i=1; i<=cont; i++){
	      total = total + parseFloat(document.getElementById('valorTotal_'+i).value);
	   }
     	   
     $('#tdTotal').html('R$ '+total.toFixed(2));
   
    }
	   	
	

	
/***********************  FIM-ADD E REMOVE ICONE DE PRESENTE   *********************
####################################################################################*/	



/***********************  AUMENTA E DIMINUI A QUANTIDADE   ************************
####################################################################################*/
   
    // #### Seleciona os eventos 	
	$('.addQtd').click(function(){
		addQtd(this.value);
		upadatePrecosCarrinho(this.value);
	});
	
	$('.removeQtd').click(function(){
		removeQtd(this.value);
		upadatePrecosCarrinho(this.value);
	});
	
	$('.quantidadeInp').blur(function(){

		var explode=this.id.split("_");
		var value=explode[1];
		
		if(this.value == '0') $(this).val(1);
		
	    upadatePrecosCarrinho(value);
	});
	
    $('.quantidadeInp').keyup(function(){

		var explode=this.id.split("_");
		var value=explode[1];
		
		if(this.value == '0') $(this).val(1);
		
	    upadatePrecosCarrinho(value);
	});
	
	
	function isNumeric(valor)
	{
		validChar = '0123456789.,-';
		for(var i=0;i<valor.length;i++)
			if(validChar.indexOf(valor.substr(i,1))<0)
				return false;
		return true;
	}
	
	// functions
	function addQtd(id){
		var cont = parseInt(id);
		var qtd  = parseInt(document.getElementById('qtd_'+cont).value);
		
		if(qtd < 1000){
		  document.getElementById('qtd_'+cont).value = qtd + 1;
		}
	}
	
	function removeQtd(id){
		var cont = parseInt(id);
		var qtd  = parseInt(document.getElementById('qtd_'+cont).value);
		
		if(qtd > 1){
		  document.getElementById('qtd_'+cont).value = qtd - 1;
		}
	}
	
	
	// atualizar carrinho
	
	$('.atualizarCarrinho').click(function(){
		$('#btAtualizar').val(1);
		document.getElementById('formGeral').submit();
	});
	
/*********************** FIM-AUMENTA E DIMINUI A QUANTIDADE   ************************
####################################################################################*/
	
	
    // #### Fechar Freye	
	$('.fecharFrete').click(function(){
       $('#escolhaFrete').hide();
	});
	
	
    // #### Fechar Freye	
	$('.addFrete').click(function(){
		var valorTotalDoCarrinho = parseFloat(document.getElementById('valorTotalDoCarrinho').value);
		var valorFrete = parseFloat(this.value);
		var total = valorTotalDoCarrinho + valorFrete;
       
	    $('#trValorFrete').html('R$ '+this.value);
		$('#tdTotal').html('R$ '+total.toFixed(2));
		
		$('#freteSelecionado').val(this.alt);
		
		
	});
	
	

	
	
	
	
    // #### Seleciona os eventos 	
	$('.produtoCaixaIndex').mouseover(function(){
		$(this).addClass('produtoCaixaIndexOn');


	});
	
    // #### Seleciona os eventos 	
	$('.produtoCaixaIndex').mouseout(function(){
		$(this).removeClass('produtoCaixaIndexOn');


	});
	
	
	
	
	
	// #### Seleciona os eventos 	
	$('.numeracao').click(function(){
		$('#checkNumero'+this.value).css('display', 'block');
		$('#numero').val(this.value);
		
		if($('#num_marcado'+this.value).val()){
		   $('#num_marcado'+this.value).val('');
		   $('#checkNumero'+this.value).css('display', 'none');
		}else{
		   $('#num_marcado'+this.value).val(1);
		}
		
		

	});
	
	
	
	
	
	// #### muda a cor da aba	
	$('.abas').click(function(){
       abreAba(this.value)
	});
	
	
	// Abre aba de avaliaçoes
	$('.linkAvaliacoes').click(function(){
        abreAba(2); // aba 2 é a aba de avaliacoes
	});
	
		
	// Abre aba de comentários
	$('#linkComentarios').click(function(){
        abreAba(3); // aba 3 é a aba de comentários
	});
	
	

	
	
	
	// Funcao para abrir a abadesejada
	function abreAba(aba){
		$('.abas').addClass('abaOFF');
		$('#aba_'+aba).removeClass('abaOFF');
		$('#aba_'+aba).addClass('abaON');
		
		
		$('.painelAba').removeClass('ON');
		$('.painelAba').addClass('OFF');
		$('#painelAba_'+aba).addClass('ON');
	}
	
	
	
	// valida e-mail
	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;
                }
     }
	
	
	

    // valida se o frete foi calculado no carrinho
	$('.radioTpCompra').click(function(){
      
	      if(this.value == '1'){
			 document.getElementById('senha').value    = ''; 
		     document.getElementById('senha').disabled = true;
		  } else {
		     document.getElementById('senha').disabled = false;
		  }
		  

	});
	
	
	
	//Troca Pessoa Fisica e Juridica
	$('.tipoPessoa').click(function(){
	      
		  if(this.value == 'J'){
			  $('#dadosPessoaFisica').hide();
			  $('#dadosPessoaJuridica').show();
			  document.getElementById('tipoPessoaJ').checked = 'checked';
		  } else{
			  $('#dadosPessoaJuridica').hide();
			  $('#dadosPessoaFisica').show();
			  document.getElementById('tipoPessoaF').checked = 'checked';
		  }

	});
	
	
	
	// marca juridica como ISENTO de IE
	$('#isento').click(function(){
	      
		  if(this.checked){
			  document.getElementById('ie').value = 'ISENTO';
			  document.getElementById('ie').disabled = true;
		  } else{
			  document.getElementById('ie').value = '';
			  document.getElementById('ie').disabled = false;
		  }

	});
	
// #############################3 VALIDACAO DE BOTOES ##################

    // valida se o frete foi calculado no carrinho
	$('.btConcluirCompra').mousedown(function(){
       if(!document.getElementById('freteSelecionado').value)
	      alert('Calcule o frete e escolha uma opção de entrega para continuar.');
		  
	   else 
	   $('#formularioFinal').submit();
	});


	// Valida o submit antes de comprar no carrinho
	$('#botaoSubmit').mousedown(function(){
		var passa = false;
		
		for(i=1; i<=5; i++){
		   if($('#num_marcado'+i).val()){
		     passa = true;
		   }
		}
		
	    if(!passa)alert('Antes de comprar selecione um tamanho.');
	});
	
	// Valida o submit no finalizar 
	$('#bt_finalizar').mousedown(function(){
		var mail = document.getElementById('email').value
	    if(!checkMail(mail)){
			document.getElementById('email').focus();
			alert('Para continuar você deve preenchar o campo de e-mail com um e-mail válido.');
			
		} 
		else if(document.getElementById('tipoCompra').checked && !document.getElementById('senha').value){
			alert('Digite sua senha para continuar, caso não possua selecione a opçao "Minha Primeira Compra" para cria-la.');
		}
		

		
	});
	
	
// #############################FIM VALIDACAO DE BOTOES ##################



// ################### VALIDACAO DE PAGAMENTOS ##########
	// Valida o submit antes de comprar no carrinho
	$('.marcaCartao').click(function(){
		
	   var desativaCartao = true;
		
	   if(this.value != '10'){	
	   document.getElementById('nomeCartao').innerHTML = ' - '+this.value;
	   desativaCartao = false;
	   }else {
		   document.getElementById('nomeCartao').innerHTML = '';   
           desativaCartao = true;
		   $('.dadosCartao').val('');
	   }
	   
       if(desativaCartao){
	      $('#boxDadosCartao').hide();
	   }else{
	      $('#boxDadosCartao').show();
	   }
	
	   $('#divBotao').show();
	
	});
	
	


// ################### FIM-VALIDACAO DE PAGAMENTOS ##########

});


