// JavaScript Document
var pathUrl = "";
//
var Com = new Object();
Com.Request = function(compra, path){
	document.getElementById('w_compra').style.visibility = 'visible';
	pathImg = path+"../shop/__imgs/";
	Com.request = Com.createRequestObject();
	Com.request.onreadystatechange = Com.Response;
	Com.request.open("POST", pathAJ+"__js/buscaCOMPRA.php?c="+compra+"&p="+path, true);
	Com.request.send(pathAJ+"__js/buscaCOMPRA.php");
}
Com.Response = function (){
	if(Com.CheckReadyState(Com.request)==200){		
		var response = Com.request.responseXML.documentElement;
		var _data = response.getElementsByTagName('compra');
		if(_data.length == 0){
			// achou nada;	
		}else{
			var localRes = response.getElementsByTagName('id')[0].firstChild.data;
			//
			if(localRes.length>1){
				head = localRes.split("#");
				total = head[0].split(";");
				node = head[1].split("|");
				/*
				Formata tabela de resultado
				*/
				txtResp = '<hr width="100%" size="1" color="#EBEAE0" /><br>';
				txtResp+= '<span class="artigos-subtitulo">Detalhes do pedido '+total[0]+'</span><br><br>';
				/*
				0:cod_pedido, 1:data_pedido, 2:data_vecto, 3:data_pagto, 4:forma_pagto, 
				5:valor, 6:cep, 7:frete, 8:total, 9:status, 10:despacho_por, 11:despacho_data, 
				12:despacho_dias, 13:md5cod_pedido, 14:se_vencido, 15:cod_rastrear, 16:so_assinatura
				*/
				stat = Array("Cancelado por falta de pagamento","Aguardando pagamento","Aguardando despacho","Saiu para Correios","Despachado");
				txtResp+= '<table width="100%" border="0" cellpadding="2" cellspacing="1">';
				txtResp+= '<tr>';
				txtResp+= '<td width="50%"><strong>Data do pedido:</strong> '+total[1]+"</td>";
				txtResp+= '<td width="50%"><strong>Vencimento:</strong> '+total[2]+"</td>";
				txtResp+= '</tr>';
				txtResp+= '<tr>';
				txtResp+= '<tr>';
				txtResp+= '<td><strong>Pagamento por:</strong> '+total[4]+"</td>";
				if(total[3].length>1 && total[3]!="00-00-0000"){
					txtResp+= '<td><strong>Recebido dia:</strong> '+total[3]+"</td>";
				}else{
					txtResp+= '<td>&nbsp;</td>';
				}
				txtResp+= '</tr>';
				if(total[14]==1){
					txtResp+= '<td><strong>Status:</strong> '+stat[0]+'</td>';
				}else{
					txtResp+= '<td><strong>Status:</strong> '+stat[total[9]]+'</td>';
				}
				if(total[9]==1 && total[14]==0 && total[4]=="Boleto" && total[17]=="local"){
					txtResp+= '<td align="left"><a href="./'+total[13]+'&i=i=WW05c1pYUnY=" target="_blank"><img src="../../__img/bt_boleto.gif" border="0" alt="Imprimir o boleto para o dia '+total[2]+' de R$ '+total[8]+'" align="absmiddle" /> imprimir boleto</a></td>';
				}else{
					txtResp+= '<td>&nbsp;</td>';
				}
				if(total[10]>2){
					txtResp+= '<tr>';
					txtResp+= '<td><strong>Despacho por:</strong> '+total[10]+"</td>";
					txtResp+= '<td><strong>Data despacho:</strong> '+total[11]+"</td>";
					txtResp+= '</tr>';
				}
				if(total[10].length>0 && !total[16]){
					txtResp+= '<tr>';
					txtResp+= '<td colspan="2"> <strong>Prazo de entrega: </strong> ';
					txtResp+= '<a href="javascript:correiosPrazo(\''+total[6]+'\',\''+total[10].substr(0,1)+'\');"> '+total[10]+' &raquo; clique aqui para consultar</a>';
					txtResp+= '</tr>';
				}
				if(total[15].length>1){
					txtResp+= '<tr>';
					txtResp+= '<td colspan="2">';
					txtResp+= '<table width="100%" border="0" cellpadding="2" cellspacing="1"><tr>';
					txtResp+= '<td width="1%" valign="middle"><a href="http://websro.correios.com.br/sro_bin/txect01$.QueryList?P_LINGUA=001&P_TIPO=001&P_COD_UNI='+total[15]+'" target="_blank"><img src="../../__img/sedex2.jpg" border="0" alt="Rastrear a entrega via Correios"></a></td>';
					txtResp+= '<td width="99%" valign="middle"><strong>Rastrear entrega n&ordm;:</strong> <a href="http://websro.correios.com.br/sro_bin/txect01$.QueryList?P_LINGUA=001&P_TIPO=001&P_COD_UNI='+total[15]+'" target="_blank">'+total[15]+' &raquo; clique aqui</a></td>';
					txtResp+= '</tr></table>';
					txtResp+= '</td></tr>';
					// <br><a href="javascript:como_rastrear();"><i>Como rastrear?</i></a>
					// javascript:PopUp('http://websro.correios.com.br/sro_bin/txect01$.startup?P_LINGUA=001&P_TIPO=001','SRO','width=550,height=400,scrollbars=yes,resizable=no,toolbar=no')
				}
				txtResp+= '</table>';
				txtResp+= '<br>';
				//
				txtResp+= '<table width="100%" border="0" cellpadding="3" cellspacing="1">';
				txtResp+= '<tr bgcolor="#E7E6D8" height="25">';
				txtResp+= '<td align="center" width="70%"><strong>Descri&ccedil;&atilde;o</strong></td>';
				txtResp+= '<td align="center" width="5%"><strong>Qtd.</strong></td>';
				txtResp+= '<td align="center" width="15%"><strong>Pre&ccedil;o Unit&aacute;rio&nbsp;R$ </strong></td>';
				txtResp+= '<td align="center" width="10%"><strong>Total R$ </strong></td>';
				txtResp+= '</tr>';
				//
				for(n=0;n<node.length;n++){
					resp = node[n].split(";");
					txtResp+= '<tr>';
					txtResp+= '<td class="cesta_divisoria">';
					txtResp+= '<table width="100%">';
					txtResp+= '<tr>';
					if(resp[1].length>0){
						txtResp+= '<td width="1%"><img src="thumb.php?img='+pathImg+resp[1]+'&larg=50" border="0"></td>';
					}else{
						txtResp+= '<td width="1%">&nbsp;</td>';
					}
					txtResp+= '<td width="99%"><span class="bold">'+resp[2]+'</span><br>'+resp[3]+'</td>';
					txtResp+= '</tr>';
					txtResp+= '</table>';
					txtResp+= '</td>';
					txtResp+= '<td align="center" class="cesta_divisoria">'+resp[4]+'</td>';
					txtResp+= '<td align="right" class="cesta_divisoria">R$ '+resp[5]+'</span></td>';
					txtResp+= '<td align="right" class="cesta_divisoria">R$ '+resp[6]+'</span></td>';
                    txtResp+= '</tr>';
				}
                txtResp+= '</tr>';
				if(!total[16]){
					txtResp+= '<tr bgcolor="#F5F5EF">';
					txtResp+= '<td colspan="3" height="15"><strong>subtotal do pedido</strong></td>';
					txtResp+= '<td align="right"><strong>R$&nbsp;'+total[5]+'</strong></td>';
					txtResp+= '</tr>';
					txtResp+= '<tr>';
					txtResp+= '<td colspan="1" valign="middle" height="40">Valor do frete (v&aacute;lido somente para o Brasil):</td>';
					txtResp+= '<td colspan="2" valign="middle" align="center"><strong>CEP:&nbsp;</strong>'+total[6]+'</td>';
					txtResp+= '<td align="right" valign="middle" class="divisoria_cesta"><strong>R$&nbsp;'+total[7]+'</td>';
					txtResp+= '</tr>';
				}
                txtResp+= '<tr bgcolor="#F5F5EF"><td colspan="3" height="15" class="bold">Valor total a Pagar</td>';
				txtResp+= '<td align="right" class="bold"><strong>R$&nbsp;'+total[8]+'</strong></td>';
				txtResp+= '</tr>';
				txtResp+= '</table><br><br>';
				/*
				Escreve resposta na Tela
				*/
				document.getElementById('showCompra').innerHTML = txtResp;
				document.location.href = "#compra";
			}else{
				txtResp = '<hr width="100%" size="1" color="#EBEAE0" />';
				txtResp+= '<font color="#990000">Não encontrado.</font><br><br>';
				document.getElementById('showCompra').innerHTML = txtResp;
				document.location.href = "#compra";
			}
			setTimeout("document.getElementById('w_compra').style.visibility = 'hidden';",700);
		}	
	}
}
Com.createRequestObject = function(){
	var obj;
	if(window.XMLHttpRequest)	{
		obj = new XMLHttpRequest();
	}
	else if(window.ActiveXObject){
		obj = new ActiveXObject("MSXML2.XMLHTTP");
	}
	return obj;
}
Com.CheckReadyState = function(obj){
	if(obj.readyState < 4) {		
		window.status = "Loading...";
	}
	if(obj.readyState == 4){
		if(obj.status == 200){
			window.status = "Loaded!";
			return obj.status;
		}else{
			window.status = "HTTP " + obj.status;
		}
	}
}
function showPag(op){
	if(op==1){
		document.getElementById("ultimo_pagto").style.display = "";
		document.getElementById("proximo_pagto").style.display = "none";
	}else{
		document.getElementById("ultimo_pagto").style.display = "none";
		document.getElementById("proximo_pagto").style.display = "";
	}	
	shoWait(op);
	document.getElementById('showCompra').innerHTML = "";
}
function shoWait(op){
	if(op==1){
		document.getElementById("w_ult").innerHTML = '<img src="../../__img/waiting_pp.gif" align="absbottom"/>';
		document.getElementById("w_prox").innerHTML = '<img src="../../__img/spacer.gif" width="16" height="16" align="absbottom"/>';
		setTimeout('shoWait(0)',500);	
	}
	if(op==2){
		document.getElementById("w_prox").innerHTML = '<img src="../../__img/waiting_pp.gif" align="absbottom"/>';
		document.getElementById("w_ult").innerHTML = '<img src="../../__img/spacer.gif" width="16" height="16" align="absbottom"/>';
		setTimeout('shoWait(0)',500);	
	}else{
		if(op!=1){
			document.getElementById("w_ult").innerHTML = '<img src="../../__img/spacer.gif" width="16" height="16" align="absbottom"/>';
			document.getElementById("w_prox").innerHTML = '<img src="../../__img/spacer.gif" width="16" height="16" align="absbottom"/>';
		}
	}	
}
function como_rastrear(){
	window.open('http://www.correios.com.br/como_rastrear.htm','Correios4','width=254,height=255,menubar=no,toolbar=no,status=no,scrollbars=no,resizable=no');
}
