// JavaScript Document
preLoadImg = new Image();
preLoadImg.src = "img/loading.gif";

function get(elemento){
	return document.getElementById(elemento);
}

function splash_alert(alerta){
	get(alerta).style.display='block';
	get('fade').style.display='block';
}

function hid_splash_alert(alerta){
	get(alerta).style.display='none';
	get('fade').style.display='none';
}

function up(campo){
	var xx =  Number(campo.value);
	campo.value = xx + 1;
	//alert(xx+1);
}

function down(campo){
	var xx =  Number(campo.value);
	if(campo.value > 0){
		campo.value = xx - 1;
	}
}

function act(action, cam, preco){	
	var camp = get('tp_boton_' + cam);
	var bot = get('id_boton_' + cam).value;
	//alert(bot+" / "+preco);
    var valor = get('meu_primeiro_preco_' + cam);	//alert(camp.value);
	
	camp.value = action;
	//alert(camp.value);
	var bt = get('bota_carrim_'+cam);
	valor.innerHTML = preco;
	//alert(bt.disabled);
	bt.disabled = false;
	//alert(bt.disabled);
	var texqt = get('qt_'+cam);
	if (texqt.value < 1) {
		//alert(texqt.value);
		texqt.value = 1;
	}
}

function PermiteNumeros(e)// Block 
{
    var tecla=new Number();
    if(window.event) {
		 tecla = e.keyCode; // cod asc
	//	 alert(tecla);
	//	 tecla = String.fromCharCode(tecla);
	//	 alert(tecla);
    }else{ 
		if(e.which) {
 			tecla = e.which; // cod asc
	//		alert(tecla);
	//		tecla = String.fromCharCode(tecla);
	//		alert(tecla);
	     }else {
			 return true;
    	 }
	}
	if(! ( (tecla >= 48) && (tecla <= 57) || (tecla == 8) ) )
    {
 return false;
    }
}

function valida_digitos(Ncampo)
{	
	var er = /[a-z\.\-\*\\\/&%$#@!\,\+?§\°\ª\º\[\]\)\(\{\}]/;	
	var digito = Ncampo.value;
	var tempor;
	for (var i=0;i<digito.length; i++) {
		tempor = digito.substring(i,i+1); 
		//se digitos não igual aos caracteres informado na variavel digitos, então é deletado
		  if (er.test(digito)) {
		  	 Ncampo.value="";
		 	 return false;
		  break;
    	}
   	}
		
}

function ajax()
{
    try {
        return new ActiveXObject("Microsoft.XMLHTTP");
    } catch(e) {
        try {
            return new ActiveXObject("Msxml2.XMLHTTP");
        } catch(ex) {
            try {
                return new XMLHttpRequest();
            } catch(exc) {
                alert("Esse browser não tem recursos para uso do AJAX");
                return false;
            }
        }
    }
}

var ajax1 = new ajax();
function comprar_bt(boton)
{
    // captura o valor do cep de destino
    var id = get("id_boton_"+boton).value;
   	var quant = get("qt_"+boton).value;
	var tp = get("tp_boton_"+boton).value;
	
	//alert(id+" - "+quant+" - "+tp);
	
	//alert("boton:"+id+" / Quant: "+quant+" / Tp: "+tp );
	if( (id!=null && id!="") && (quant!=null && quant!="") && (tp !=null && tp!="") )  {

		//get("teste").innerHTML = 
		splash_alert('add_light');
		setTimeout(function (){ 
		//alert(cep_encontrado);
			hid_splash_alert('add_light');
		},5000);
    	// abro o arquivo que vai calcular nosso frete
    	ajax1.open("POST", "purchase.php", true );
    	// seto o header de resposta
    	ajax1.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    	// envia os dados para o php
    	ajax1.send("id_boton="+id+"+&str_qt="+quant+"&tp_boton="+tp);
    
    	// aqui vamos testar o estado da nossa requisição no nosso caso:
    	// status = 4 concluido
    	// != de 4 em andamento.
		
	    ajax1.onreadystatechange = function() {
    	    if (ajax1.readyState == 4) {
        	    // jogo o resultado encontrado no DIV em nosso html
            	//get("teste").innerHTML = 
				//alert(ajax1.responseText);
				atualiza_cart();
        	} else {
            	// por enquanto, aguardando status 4....
            	//get("teste").innerHTML = 
			//	alert("Aguarde, calculando...");
        	}
    	}
	}else{
    	//get("teste").innerHTML = 
		alert("ERRO tente denovo");
	}
}

var ajax1 = new ajax();
var ajax2 = new ajax();
function atualiza_cart()
{
    	// abro o arquivo que vai calcular nosso frete
    	ajax1.open("POST", "atualiza_cart.php", true );
    	// seto o header de resposta
    	ajax1.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    	// envia os dados para o php
    	ajax1.send("mod=quant");
    	// aqui vamos testar o estado da nossa requisição no nosso caso:
    	// status = 4 concluido
    	// != de 4 em andamento.
	    ajax1.onreadystatechange = function() {
    	    if (ajax1.readyState == 4) {
        	    // jogo o resultado encontrado no DIV em nosso html
            	//get("teste").innerHTML = 
				var div_quant = get('cart_quantidade');
				div_quant.innerHTML = ajax1.responseText;
				//alert(ajax1.responseText);
				//alert("quant="+ajax1.responseText);
			}
    	}
		
		    	// abro o arquivo que vai calcular nosso frete
    	ajax2.open("POST", "atualiza_cart.php", true );
    	// seto o header de resposta
    	ajax2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    	// envia os dados para o php
    	ajax2.send("mod=val");
    	// aqui vamos testar o estado da nossa requisição no nosso caso:
    	// status = 4 concluido
    	// != de 4 em andamento.
	    ajax2.onreadystatechange = function() {
    	    if (ajax2.readyState == 4) {
        	    // jogo o resultado encontrado no DIV em nosso html
            	//get("teste").innerHTML = 
				var div_val = get('cart_valor');
				div_val.innerHTML = "R$: "+ajax2.responseText;
				var val = ajax2.responseText;
				val = val.replace(/,/,".");
				//alert(val);
				//alert("val="+Number(ajax2.responseText));
				get('sacola').SetVariable("carWidth", Number(val) );
			}
    	}
}

function color_baby(par, stive) {
var a = document.getElementById('Jb7_' + par + '_' + stive);
	
	for (i = 1; i <= 6; i++) {
		
		//document.getElementById('Jb7_' + i + '_' + stive).className = 'bt_tamanho left';
		
		}
	a.className = 'bt_tamanho_h left';
	
}

 
