

function LimparFormataReais(valor, validos) {
// retira caracteres invalidos da string
var result = "";
var aux;
for (var i=0; i < valor.length; i++) {
aux = validos.indexOf(valor.substring(i, i+1));
if (aux>=0) {
result += aux;
}
}
return result;
}


function FormataReais(campo,tammax,teclapres,decimal) {
var tecla = teclapres.keyCode;
vr = LimparFormataReais(campo.value,"0123456789");
tam = vr.length;
dec=decimal

if (tam < tammax && 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 <= dec )
{ campo.value = vr ; }

if ( (tam > dec) && (tam <= 5) ){
campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; }
if ( (tam >= 6) && (tam <= 8) ){
campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, 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 - dec, tam ) ; }
if ( (tam >= 12) && (tam <= 14) ){
campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
if ( (tam >= 15) && (tam <= 17) ){
campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ;}
} 

}

/*function FormataReais(fld, milSep, decSep, e) {
// chamada da funcao = onKeyPress="return(FormataReais(this,'.',',',event))"

var sep = 0;
var key = '';
var i = j = 0;
var len = len2 = 0;
var strCheck = '0123456789';
var aux = aux2 = '';
var whichCode = (window.Event) ? e.which : e.keyCode;
if (whichCode == 13) return true;
key = String.fromCharCode(whichCode);  // Valor para o código da Chave
if (strCheck.indexOf(key) == -1) return false;  // Chave inválida
len = fld.value.length;
for(i = 0; i < len; i++)
if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
aux = '';
for(; i < len; i++)
if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
aux += key;
len = aux.length;
if (len == 0) fld.value = '';
if (len == 1) fld.value = '0'+ decSep + '0' + aux;
if (len == 2) fld.value = '0'+ decSep + aux;
if (len > 2) {
aux2 = '';
for (j = 0, i = len - 3; i >= 0; i--) {
if (j == 3) {
aux2 += milSep;
j = 0;
}
aux2 += aux.charAt(i);
j++;
}
fld.value = '';
len2 = aux2.length;
for (i = len2 - 1; i >= 0; i--)
fld.value += aux2.charAt(i);
fld.value += decSep + aux.substr(len - 2, len);
}
return false;
}*/



function calculaDescontoCL() {
	  
	  
	  
	  var valorCredito = document.formCarrinho.valor_credito.value;
	  
	  if(valorCredito=="") valorCredito = "0";
	  
	  valorCredito = valorCredito.replace(".", ""); 
	  valorCredito = valorCredito.replace(".", "");
	  valorCredito = parseFloat(valorCredito.replace(",", ".")); 
	  
	  
	  if((document.formCarrinho.total_carrinho.value>=valorCredito)&&(document.formCarrinho.saldo_credito.value>=valorCredito))
	  {
//		   {
		valorDescontoPercentual =  document.formCarrinho.total_carrinho.value/valorCredito;
		valorDescontoPercentual = 100-(100/valorDescontoPercentual);
		fatorMultiplicador = valorDescontoPercentual/100;
		
		document.getElementById("iframe_aux").src = "paginas_compras_coletivas/carrinho/ifr_Carrinho.php?acao=adiciona_credito&valor_credito="+document.formCarrinho.valor_credito.value+"&fatorMultiplicador="+fatorMultiplicador;	  
	  
	  }else alert("Você não pode usar de crédito um valor maior que sua compra.");
	
	
	}

function realizaCadastroEnderecoEntrega31(tipo){
	
	if(tipo=="novo"){	
		if(validateForm('mensagem_ganhador:1:Nome dos befeficiados'))
			{ 
			document.formEnderecoEntrega.endereco_entrega_outro.value = 1;
			document.formEnderecoEntrega.submit(); 
			}
	}
	
}


function realizaCadastroEnderecoEntregaCL(tipo){
	
	if(tipo=="novo"){	
		if(validateForm('destinatario:1:Nome completo,emailc:1:e-mail'))
			{ 
			document.formEnderecoEntrega.endereco_entrega_outro.value = 1;
			document.formEnderecoEntrega.submit(); 
			}
	}else{
	document.formEnderecoEntrega.endereco_entrega_outro.value = 0;	
	document.formEnderecoEntrega.submit();
	}
	
}


function gerarPedidoAnuncio(forma_pagamento) 
	{ 
	
		
		
		var answer = confirm ("Confirmar a compra ?");
		
		if (answer){
			
			
			if((forma_pagamento=="sempagto")||(forma_pagamento=="sempagtovc"))
				{
				if(forma_pagamento=="sempagtovc")
					{
					document.getElementById("idFinalizaCompraVcMesmo").style.display = "none";
					document.getElementById("idFinalizaCompraAmigo").style.display = "none";
					document.getElementById("idAguardeLoading").style.display = "";		
					document.getElementById("iframe_aux").src = "paginas_compras_coletivas/finaliza_compra/ifr_cadastra_pedido.php?forma_pagamento="+forma_pagamento;
					}
				else
					{
					if(validateForm('destinatario:1:Nome completo,emailc:1:e-mail'))	
						{
						document.getElementById("idFinalizaCompraVcMesmo").style.display = "none";
						document.getElementById("idFinalizaCompraAmigo").style.display = "none";
						document.getElementById("idAguardeLoading2").style.display = "";		
						document.getElementById("iframe_aux").src = "paginas_compras_coletivas/finaliza_compra/ifr_cadastra_pedido.php?forma_pagamento="+forma_pagamento;
						}
					}
				}
			else
				{
				if(forma_pagamento=="envia_email")
					{					
					document.getElementById("idLogoDep").style.display = "none";
					document.getElementById("idLogoPagseguro").style.display = "none";
					document.getElementById("idAguardeLoadingDep").style.display = "";		
					document.getElementById("iframe_aux").src = "paginas_compras_coletivas/finaliza_compra/ifr_cadastra_pedido.php?forma_pagamento="+forma_pagamento;
					}
				else
					{
					document.getElementById("idLogoPagseguro").style.display = "none";
					document.getElementById("idAguardeLoading").style.display = "";		
					document.getElementById("iframe_aux").src = "paginas_compras_coletivas/finaliza_compra/ifr_cadastra_pedido.php?forma_pagamento="+forma_pagamento;
					}
				
				
				}
		   }	   
		
	}
	
	
function anuncioCarrinhoAcoes(id,acao)
 { 
 document.getElementById("iframe_aux").src = "paginas_compras_coletivas/carrinho/ifr_Carrinho.php?id_anuncio="+id+"&acao="+acao;
 }
 
 

