function GetXMLHttp() {
    if(navigator.appName == "Microsoft Internet Explorer") {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else {
        xmlHttp = new XMLHttpRequest();
    }
    return xmlHttp;
}

var xmlRequest = GetXMLHttp();

function abrirPag(valor){
    var url = valor;

    xmlRequest.open("GET",url,true);
    xmlRequest.onreadystatechange = function(mudancaEstado){
	    if (xmlRequest.readyState == 4){
        document.getElementById('boxtoreturn').innerHTML = xmlRequest.responseText;
    };
	};
    xmlRequest.send(null);

    return url;
}

function abrirPag2(valor){
    var url = valor;

    xmlRequest.open("GET",url,true);
    xmlRequest.onreadystatechange = function(mudancaEstado){
	    if (xmlRequest.readyState == 4){
        document.getElementById('texturas').innerHTML = xmlRequest.responseText;
    };
	};
    xmlRequest.send(null);

    return url;
}

function thumbs(id,v){
if(v == "h"){
document.getElementById(id).style.display = "block";
document.getElementById("select").style.display = "none";
}else{
document.getElementById(id).style.display = "none";
document.getElementById("select").style.display = "block";
}
}

function futons(t,m){
	for(i = 1; i < t+1; i++){
		if(m == i){
		document.getElementById("c"+i).style.display = "block";
		}else{
		document.getElementById("c"+i).style.display = "none";
		}
	}
}

function ver_futons(v){
//document.getElementsByClassName("bloco_futons").style.display = "none";
total = document.getElementsByName("bloco_futons");
for(i=0;i<total.length;i++){
	total[i].style.display = "none";
}
document.getElementById("c"+v).style.display = "block";
}


function destaque(v){ document.getElementById("destaque").innerHTML = "<img src='http://www.futonandhome.com.br/produtos/"+v+"' />"; }
function medidas() { document.getElementById("me").checked=true; }
function info(c) { document.getElementById("info").style.display = c; }
function info2(c) { document.getElementById("info2").style.display = c; }

//-------------------------------------------------------------------------------funcoes de login e carrinho
function bemvindo(){
//$("#retorno").html(<? include("bemvindo.php");?>);

	$.post('/bemvindo.php',{},function(response){
		utf8: "sim"
		$("#retorno").html(response);
	});

	// $("#bt_incluir").show();
	document.getElementById('bt_incluir').style.display = 'block';
	$("#bt_disable").hide();
}

$(document).ready(function() {
       $("#ok").click(function() {
	   $.post('/login_ajax.php',{

		email: $('#email').val()
			
	 	},function(response){
			
						
			if(response == 1){
				//window.location.reload(true);
				
				bemvindo();
			}else{
				$("#erro_email").show();
				setTimeout(function(){$("#erro_email").fadeOut();}, 5000);
			}
		});
      });
});

$(document).ready(function() {
       $("#ok_modal").click(function() {
	   $.post('/login_ajax.php',{

		email: $('#email_modal').val()
			
	 	},function(response){
			if(response == 1){
				$('.window').hide();
				bemvindo();
				alert('Voce está logado. Pode incluir itens em seu orçamento.');
				$('#mask').fadeOut(1000);
			}else{
				$("#erro_email_modal").show();
				setTimeout(function(){$("#erro_email_modal").fadeOut();}, 5000);
			}
		});
      });
});

// Funcao que ao clicar no botao incluir item, vai exibir o select para selecionar a loja preferida
function lojapreferencia() {
	document.getElementById('loja_preferencia').style.visibility = 'visible';
}

// LOJA DE PREFERENCIA
$(document).ready(function()
{
	$("#loja_preferencia").change(function()
	{
		$.post('/loja_preferencia.php',{
			loja : $("option:selected", this).val()
 			}
		);
		alert('Loja atualizada. Obrigado.');
		document.getElementById('loja_preferencia').style.visibility = 'hidden';
    });
});



function incluir(){
	
	if($('#medida').val() > 0){
		v_medida = $('#medida').val() 		
		
		
	}else if($('#medida').val() == ""){
		
		v_medida = "";
		
	}else{
		
		t_medida = document.form.medida.length;
	
		med = 0;
	
		for(i=0; i< t_medida; i++){
			if (document.form.medida[i].checked == true) {
			v_medida = document.form.medida[i].value;
			med = 1;
			}
		
		}	
	
		if(med == 0){
			v_medida = "";
		}else{
			v_medida = v_medida
		}

	}
	
	if (document.form.textura) {
	
		t_textura = document.form.textura.length;
		
		tem = 0;
		
		for (i = 0; i < t_textura; i++) {
			if (document.form.textura[i].checked == true) {
				v_textura = document.form.textura[i].value;
				tem = 1;
			}
			
		}
		
		if (tem == 0) {
			v_textura = 0
		}
		else {
			v_textura = v_textura
		}
		
	}else{
		
		v_textura = 0
		
	}
	
	$.post('/orcamento.php',{
	   	
		produto: $('#produto').val(), 
		cesta: $('#cesta').val(), 
		textura: v_textura,
		medida: v_medida,
		medida2: $('#medida2').val()
			
	},function(response){
					
		$("#pedido").html(response);
		ver_futons();
			
	});

}

function excluir(id){
	if(confirm("Tem certeza que deseja excluir este item?")){
		
		$.post('/orcamento.php',{
	   
			Excluir: "Excluir",
			codigo: id
			
	 	},function(response){
			
			$("#pedido").html(response)
			
		});
		
	}
}

function altera(v,id){
	
		
		$.post('/orcamento.php',{
	   
			Atualizar: "Alterar",
			codigo: id,
			qtde_alterar: v
			
	 	},function(response){
			
			setTimeout(function(){$("#pedido").html(response)},3000);
			
		});
			
}

function finalizar(){
	window.location.href = "/visualizar_orcamento.php";
}

//-------------------------------------------------------------------------------fim funcoes de login e carrinho


function valida(){
	
	var d = document.flores;
	if(d.nome.value == "nome"){
		alert("Preencha o campo nome")
		d.nome.focus();
		return false;
	}
	if(d.email.value == "e-mail"){
		alert("Preencha o campo e-mail")
		d.email.focus();
		return false;
	}
	if(d.msg.value == "mensagem"){
		alert("Preencha o campo mensagem")
		d.msg.focus();
		return false;
	}
	parte1 = d.email.value.indexOf("@");
    parte2 = d.email.value.indexOf(".");
    parte3 = d.email.value.length;
   if (!(parte1 >= 2 && parte2 >= 2 && parte3 >= 2)) {
   alert ("E-mail Inválido!");
   d.email.focus();
   return false;
   }
}