function GetXmlHttpObject(){
	
	if (window.XMLHttpRequest) { a=new XMLHttpRequest(); } /*Objeto nativo (FF/Safari/Opera7.6+)*/
	else {  
		try { a = new ActiveXObject("Msxml2.XMLHTTP");  /*activeX (IE5.5+/MSXML2+)*/ }	 
		catch(e) {  
			try { a = new ActiveXObject("Microsoft.XMLHTTP"); /*activeX (IE5+/MSXML1)*/ }	 
			catch(e) { /* O navegador não tem suporte */ a = false; }  
		}  
	}   
	return a;  
}

function calcula_quantidades(id_produto, quantidade, quantidade_anterior){
	
	var xmlHttp		= new GetXmlHttpObject();
	var url			= "ajax/carrinho.asp";
	var params		= "produto=" + id_produto + "&quantidade=" + quantidade + "&quantidade_anterior=" + quantidade_anterior;
	var frete_anter = document.getElementById("frete_produto").innerHTML;
	
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");	// Setando Content-type
	xmlHttp.setRequestHeader("Content-length", params.length); // Comprimento do conteúdo=comprimento dos dados a enviar
	xmlHttp.onreadystatechange = AchouConteudo;
	xmlHttp.send(params);
	
	function AchouConteudo() { 
		if (xmlHttp.readyState == 4) {
			
			if(frete_anter != "R$ 0,00"){
				calcula_frete(frete_anter.replace("R$ ", ""));
			}
			else {
				location.href="index.asp?id_pag=C1SLV";
			}
		}
	}

}

function calcula_frete(valor){
	
	var frete_cep 	= document.getElementById("CEP").value;
	var licenca 	= document.getElementById("CodSite").value;
	var frete_anter = document.getElementById("frete_produto").innerHTML;
	var preco_total	= document.getElementById("produto_total").innerHTML;
	var frete_tipo	= document.getElementsByName("frete_tipo");
	var resultado 	= float2moeda(parseFloat(preco_total.replace("R$ ", "")) - parseFloat(frete_anter.replace("R$ ", "")));
	var indice		= 0;
	var valida_frete= 0;
	
	while (indice < frete_tipo.length){
		if(frete_tipo[indice].checked == true){ valida_frete++; }
		indice++;
	}
	
	if(valida_frete == 0){
		alert("Selecione uma forma de entrega");
		return false;
	}
	
	document.getElementById("frete_produto").innerHTML = "R$ 0,00";
	document.getElementById("produto_total").innerHTML = "R$ " + resultado;
	
	if(document.getElementById("frete_tipo_s").checked == true){
		var frete_tipo_c = document.getElementById("frete_tipo_s").value;
	}
	
	if(document.getElementById("frete_tipo_p").checked == true){
		var frete_tipo_c = document.getElementById("frete_tipo_p").value;
	}
	
	if(frete_cep.length != 9){
		alert("Cep inválido");
		document.getElementById("CEP").focus();
		return false;
	}
	
	var params	 = "";
	var xmlHttp	 = new GetXmlHttpObject();
	var ajax_url = "ajax/calculo-frete.php";
	
	params += "tipo_frete=" + frete_tipo_c;
	params += "&cep_destino=" + frete_cep;
	params += "&altura=6";
	params += "&largura=25";
	params += "&comprimento=16";
	params += "&valor_declarado=" + valor;
	params += "&licenca=" + licenca;
	
	xmlHttp.open("POST", ajax_url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");	// Setando Content-type
	xmlHttp.setRequestHeader("Content-length", params.length); // Comprimento do conteúdo=comprimento dos dados a enviar
	xmlHttp.onreadystatechange = AchouConteudo;
	xmlHttp.send(params);
	
	if (xmlHttp.readyState == 1){
	  document.getElementById("frete_produto").innerHTML = "<img src='img/loading_bar.gif' style='width:80px; height:auto;'>";
	}
	
	function AchouConteudo() { 
		if (xmlHttp.readyState == 4) {
			
			var preco_total = document.getElementById("produto_total").innerHTML;
			var preco_frete = xmlHttp.responseText;
			
			preco_total = preco_total.replace("R$ ", "");
			preco_total = preco_total.replace(".", "");
			preco_total = preco_total.replace(",", ".");
			preco_frete = preco_frete.replace("R$ ", "");
			preco_frete = preco_frete.replace(".", "");
			preco_frete = preco_frete.replace(",", ".");
			
			document.getElementById("frete_produto").innerHTML = xmlHttp.responseText;
			document.getElementById("produto_total").innerHTML = "R$ " + float2moeda(parseInt(preco_total) + parseInt(preco_frete));
			
			setTimeout("grava_tipo_frete('')", 100);
		}
	}

}

function grava_tipo_frete(valor){
	
	if(valor == "loja"){ document.getElementById("CEP").disabled = 1; }
	else{ document.getElementById("CEP").disabled = 0; }
	
	var xmlHttp = new GetXmlHttpObject();
	var frete 	= document.getElementById("frete_produto").innerHTML;
	var cep 	= document.getElementById("CEP").value;
	var url		= "ajax/frete_tipo.asp";
	url 		= url + "?sid=" + Math.random();
	url		 	= url + "&valor="+ valor;
	url 		= url + "&frete="+ frete;
	url 		= url + "&cep="+ cep;
	
	xmlHttp.onreadystatechange = grava_tipo_completo;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

	function grava_tipo_completo(){ 
		if (xmlHttp.readyState == 4) { location.href = "index.asp?id_pag=C1SLV"; }
	}
}

function carrega_informacao_pagamento(id_pagamento){

	var xmlHttp = new GetXmlHttpObject();
	var url	= "ajax/pagamento.asp";
	url = url + "?sid=" + Math.random();
	url	= url + "&id_pagamento="+ id_pagamento;
	
	xmlHttp.onreadystatechange = carregar_completo;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

	function carregar_completo(){ 
		if (xmlHttp.readyState == 4) { document.getElementById("div_pagamento").innerHTML = xmlHttp.responseText;}
	}

}

function float2moeda(num) {

   x = 0;

   if(num<0) {
      num = Math.abs(num);
      x = 1;
   }
   if(isNaN(num)) num = "0";
      cents = Math.floor((num*100+0.5)%100);

   num = Math.floor((num*100+0.5)/100).toString();

   if(cents < 10) cents = "0" + cents;
      for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
         num = num.substring(0,num.length-(4*i+3))+'.'
               +num.substring(num.length-(4*i+3));
   ret = num + ',' + cents;
   if (x == 1) ret = ' - ' + ret;return ret;

}

function lembrar_senha(){
	window.open("enviar_senha.asp", "Lembrar minha senha", "width=500, height=150");
}

function load_endereco(cep){
	
	var params	 = "cep=" + cep;
	var xmlHttp	 = new GetXmlHttpObject();
	var ajax_url = "ajax/carrega_endereco.asp";
	
	xmlHttp.open("POST", ajax_url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");	// Setando Content-type
	xmlHttp.setRequestHeader("Content-length", params.length); // Comprimento do conteúdo=comprimento dos dados a enviar
	xmlHttp.onreadystatechange = achou_endereco;
	xmlHttp.send(params);
	
	if (xmlHttp.readyState == 1){
	  document.getElementById("progressbar").innerHTML = "<img src='img/loading_bar.gif' style='width:auto; height:auto;'>";
	}
	
	function achou_endereco() { 
		if (xmlHttp.readyState == 4) {
			
			if(xmlHttp.responseText.indexOf("#") > 0){
				endereco = xmlHttp.responseText.split("#");
				
				document.getElementById("logradouro").value = endereco[0];
				document.getElementById("bairro").value = endereco[1];
				document.getElementById("cidade").value = endereco[2];
				
				var uf = document.getElementById("UF");
				var indice = 0;
				var contagem = 0;
				
				while (indice < uf.length){
					if(uf.options[indice].value == endereco[3]){ uf.options[indice].selected = true; }
					indice++;
				}
			}
			
			document.getElementById("progressbar").innerHTML = "&nbsp;";
		}
	}
	
}

// VALIDAÇÃO DOS FORMULÁRIOS DO CARRINHO

function valida_carrinho(){
	
	var frete_s	= document.getElementById("frete_tipo_s").checked;
	var frete_p	= document.getElementById("frete_tipo_p").checked;
	var frete_r	= document.getElementById("frete_tipo_r").checked;
	var cep 	= document.getElementById("CEP").value;
	var frete	= document.getElementById("frete_produto").innerHTML;
	
	if(!frete_r){
		if(cep.length != 9){
			alert("Cep inválido");
			document.getElementById("CEP").focus();
			return false;
		}
		
		if(frete == "R$ 0,00"){
			alert("Para prosseguir clique em ''Calcular Frete'' !");
			document.getElementById("CEP").focus();
			return false;
		}
	}
	
	document.getElementById("verif_form").value = "validado";
	document.frm_carrinho.submit();	
}

function val_login(){
	
	var email = document.getElementById("email").value;
	var senha = document.getElementById("senha").value;
	
	if(email.match(/(\w+)@(.+)\.(\w+)$/) == null){
		alert("E-mail inválido");
		document.getElementById("email").focus();
		return false;
	}
	
	if(senha == ""){
		alert("Digite sua senha");
		document.getElementById("senha").focus();
		return false;
	}
	
	document.frm_continuar_login.submit();
}

function val_cadastro(){
	
	var email = document.getElementById("email_cad").value;
	var cep = document.getElementById("cep_cad").value;
	
	if(email.match(/(\w+)@(.+)\.(\w+)$/) == null){
		alert("E-mail inválido");
		document.getElementById("email_cad").focus();
		return false;
	}
	
	if(cep.length != 9){
		alert("Cep inválido");
		document.getElementById("cep_cad").focus();
		return false;
	}
	
	document.frm_continuar_cadastro.submit();
	
}

function val_form_email(){
	
	var nome = document.getElementById("Nome").value;
	var email = document.getElementById("Email").value;
	
	if(email.match(/(\w+)@(.+)\.(\w+)$/) == null){
		alert("E-mail inválido");
		document.getElementById("Email").focus();
		return false;
	}
	
	if(nome == ""){
		alert("Digite seu nome");
		document.getElementById("Nome").focus();
		return false;
	}
	
	document.lembrar_senha.submit();
}

function valida_endereco(){
	
	var uf 			= document.getElementById("UF").value;
	var cep 		= document.getElementById("CEP").value;
	var cidade 		= document.getElementById("cidade").value;
	var bairro 		= document.getElementById("bairro").value;
	var logradouro 	= document.getElementById("logradouro").value;
	var numero 		= document.getElementById("numero").value;
	
	if(cep.length != 9){
		alert("Cep Inválido !");
		document.getElementById("CEP").focus();
		return false;
	}
	
	if(cidade == ""){
		alert("Cidade não preenchida !");
		document.getElementById("cidade").focus();
		return false;
	}
	
	if(bairro == ""){
		alert("Bairro não preenchido !");
		document.getElementById("bairro").focus();
		return false;
	}
	
	if(logradouro == ""){
		alert("Logradouro não preenchido !");
		document.getElementById("logradouro").focus();
		return false;
	}
	
	if(numero == ""){
		alert("Número não preenchido !");
		document.getElementById("numero").focus();
		return false;
	}
	
	document.getElementById("val_cli").value = "validado";
	document.pedido_entrega.submit();
	
}

function valida_cadastro(){
	
	var nome		= document.getElementById("Nome").value;
	var email 		= document.getElementById("Email").value;
	var email2 		= document.getElementById("Email2").value;
	var telefone	= document.getElementById("Telefone").value;
	var telefone2 	= document.getElementById("Telefone2").value;
	
	if(nome == ""){
		alert("Nome não preenchido !");
		document.getElementById("Nome").focus();
		return false;
	}
	
	if(email.match(/(\w+)@(.+)\.(\w+)$/) == null){
		alert("E-mail incorreto !");
		document.getElementById("Email").focus();
		return false;
	}
	
	if(email != "" && email.match(/(\w+)@(.+)\.(\w+)$/) == null){
		alert("E-mail alternativo incorreto !");
		document.getElementById("Email2").focus();
		return false;
	}
	
	if(telefone.length != 14){
		alert("Telefone incorreto !");
		document.getElementById("Telefone").focus();
		return false;
	}
	
	if(telefone2 != "" && telefone2.length != 14){
		alert("Celular incorreto !");
		document.getElementById("Telefone2").focus();
		return false;
	}
	
	document.getElementById("val_cli2").value = "validado";
	document.pedido_cadastro.submit();
	
}

function valida_cpf(cpf){
	
	var xmlHttp = new GetXmlHttpObject();
	var url	= "ajax/valida_cpf.asp";
	url = url + "?sid=" + Math.random();
	url	= url + "&cpf="+ cpf;
	
	xmlHttp.onreadystatechange = mostra_resultado_cpf;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
	if (xmlHttp.readyState == 1){
	  document.getElementById("statuscpf").innerHTML = "<img src='img/loading_bar.gif' style='width:60px; height:auto;'>";
	}

	function mostra_resultado_cpf(){ 
		if (xmlHttp.readyState == 4) {
			if(xmlHttp.responseText == "True"){ document.getElementById("statuscpf").innerHTML = "<span style='color:#060;'>CPF V&aacute;lido</span>"; }
			else { document.getElementById("statuscpf").innerHTML = "<span style='color:#900;'>CPF Inv&aacute;lido</span>"; }
		}
	}

}

function grava_tipo_pagamento(){
	
	var tipo_pagamento = document.getElementsByName("forma_pagamento");
	var indice = 0;
	var contagem = 0;
	
	while (indice < tipo_pagamento.length){
		if(tipo_pagamento[indice].checked == true){ contagem++; }
		indice++;
	}
	
	if(contagem == 0){
		alert("Selecione uma forma de Pagamento !");
		return false;
	}
	
	document.carrinho_pagamento.submit();
}

function nivel_senha(senha){
	
	var xmlHttp = new GetXmlHttpObject();
	var url	= "ajax/nivel_senha.asp";
	url = url + "?sid=" + Math.random();
	url	= url + "&senha="+ senha;
	
	xmlHttp.onreadystatechange = mostra_resultado;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
	if (xmlHttp.readyState == 1){
	  document.getElementById("statusbar").innerHTML = "<img src='img/loading_bar.gif' style='width:auto; height:auto;'>";
	}

	function mostra_resultado(){ 
		if (xmlHttp.readyState == 4) {
			
			function mostra_resultado(){
				document.getElementById("statusbar").innerHTML = xmlHttp.responseText;
			}
			
			setTimeout(mostra_resultado, 1000);
		}
	}

}
