// -*- coding: utf-8 -*-

function spaInqCriaAjax() {
	var xmlhttp;
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (ie1) {
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (ie2) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				xmlhttp = false;
			}
		}
	}
	return xmlhttp;
}

function spaInqTrataAjax(oObj) {
	lRet = true;
	
	if (oAjax.readyState == 4) {
		if (oAjax.status == 200) {
			// Estou usando json
			//window.alert(oAjax.responseText);
			oJson = eval("(" + oAjax.responseText + ")");
			if (oAjax.responseText.indexOf("itens") != -1) {
				lRet = spaInqGeraResult(oObj,oJson);
			}
		}
	}
	
	return lRet;
}

function spaInqVerifLogin() {
	nStatus = 0;
	oUsuario = document.getElementById("spa-inq-usuario");
	oSenha = document.getElementById("spa-inq-senha");
	
	if (oUsuario.value.length == 0 || oSenha.value.length == 0) {
		nStatus = 1;
	}
	
	if (nStatus == 0) {
		oAjax = spaInqCriaAjax();
		if (oAjax) {
			cURL = "spa_query.php?tp=12&" + "us=" + oUsuario.value + "&" + "se=" + oSenha.value + "&sr=" + gcSpaSrInqPro;
			oAjax.open('GET',cURL,false);
			oAjax.send(null);
			
			oResultado = document.getElementById("spa-inq-resultado");
			nStatus = spaInqTrataAjax(oResultado) ? 0 : 2;
		}
	}
	
	switch (nStatus) {
		case 0:
			gcSpaInqUsu = oUsuario.value;
			gcSpaInqSen = oSenha.value;
			document.getElementById("spa-inq-login").style.display = "none";
			document.getElementById("spa-inq-resultado").style.display = "block";
			break;
		
		case 1:
			window.alert("Você deve informar o usuário e a senha.");
			oUsuario.select();
			oUsuario.focus();
			break;
		
		case 2:
			window.alert("Usuário e/ou senha inválidos.");
			oUsuario.select();
			oUsuario.focus();
			break;
	}
}

function spaInqGeraResult(oObj,oJson) {
	lRet = true;
	
	if (oJson.status == 0) {
		oSubTit = document.getElementById("spa-inq-subtit");
		cSubTit = oSubTit.innerHTML;
		//cConteudo = '<div id="spa-inq-res"><br /><b>INQUILINO - BOLETO BANC&Aacute;RIO</b><br /><br />' + 
		cConteudo = '<div id="spa-inq-res"><br /><span id="spa-inq-subtit">' + cSubTit + '</span><br /><br />' + 
			'<b>Para consultar ou imprimir o boleto clique no vencimento ou boleto desejado</b><br /><br /><br />' + 
			'<div align="left" width="100%"><b>Inquilino:</b> ' + oJson.inq[0].cod + ' - ' + oJson.inq[0].nome + '</div><br /><br />';
		
		nImv = -1;
		nItTb = 0;
		for (i = 0; i < oJson.itens.length; i++) {
			if (nImv != oJson.itens[i].imov) {
				for (j = 0; j < oJson.imov.length; j++) {
					if (oJson.imov[j].cod == oJson.itens[i].imov) {
						break;
					}
				}
				
				if (nImv != -1) {
					cConteudo += "</table><br /><br />";
				}
				cConteudo += '<div class="spa-inq-cab-imv" align="left" width="100%">' + 
					'<table cellspacing="0" cellpadding="0" border="0"><tr>' + 
					'<td valign="top"><b>Im&oacute;vel:</b>&nbsp;' + oJson.imov[j].cod + '&nbsp;-&nbsp;</td><td>' + 
					oJson.imov[j].end + ' - ' + oJson.imov[j].bai + ' - ' + oJson.imov[j].cid + ' - ' + oJson.imov[j].est + 
					'</td></tr></table></div><br />' + 
					'<table  class="spa-inq-tab-itens" width="50%" border="0" cellspacing="0">' +
					'<tr><td class="spa-inq-tab-itens-cab-e" align="center">Vencimento</td>' + 
					'<td class="spa-inq-tab-itens-cab-d" align="center">Boleto</td></tr>';
				
				nImv = oJson.itens[i].imov;
				nItTb = 0;
			}
			cChave = oJson.inq[0].cod + "," + oJson.itens[i].ndoc;
			
			cConteudo += '<tr>';
			
			cClasseEsq = (nItTb % 2 == 0) ? "spa-inq-tab-itens-det-e-p" : "spa-inq-tab-itens-det-e-i";
			cClasseDir = (nItTb % 2 == 0) ? "spa-inq-tab-itens-det-d-p" : "spa-inq-tab-itens-det-d-i";
			
			cConteudo += '<td class="' + cClasseEsq + '" align="center"><a id="spa-inq-a_' + i + '" onmouseover="spaInqLinkOver(this)" ' + 
				'onmouseout="spaInqLinkOut(this)" onclick="spaInqExibeBoleto(this)"><div style="width: 100%; height: 100%;">' + 
				oJson.itens[i].venc + '</div></a><input type="hidden" ' + 'id="spa-inq-chave_' + i + '" value="' + cChave + '" /></td>' + 
				'<td class="' + cClasseDir + '" align="center">' + '<a id="spa-inq-a_' + i + '" onmouseover="spaInqLinkOver(this)" ' + 
				'onmouseout="spaInqLinkOut(this)" onclick="spaInqExibeBoleto(this)"><div style="width: 100%; height: 100%;">' + 
				oJson.itens[i].ndoc + '</div></a><input type="hidden" id="spa-inq-chave_' + i + '" value="' + cChave + '" /></td></tr>';
			
			nItTb ++;
		}
		cConteudo += "</table><br />";
		
		/*
		if (i > 0) {
			oObj.innerHTML = cConteudo;
		}
		else {
		*/
		if (i < 1) {
			//oObj.innerHTML = '<div id="spa-inq-res"><center><b>Nenhum boleto em aberto foi encontrado.</b></center>';
			cConteudo = '<div id="spa-inq-res"><center><b>Nenhum boleto em aberto foi encontrado.</b></center>';
		}
		cConteudo += "<br />" + 
			'<center><input type="button" value="Fechar" name="spa-inq-btn_fecha" id="spa-inq-btn_fecha" onclick="spaInqFechaPesquisa()" /></center>' + 
			'</div>';
		oObj.innerHTML = cConteudo;
	}
	else {
		lRet = false;
	}
	
	return lRet;
}

function spaInqFechaPesquisa() {
	document.getElementById("spa-inq-resultado").style.display = "none";
	document.getElementById("spa-inq-login").style.display = "block";
	
	document.getElementById("spa-inq-usuario").value = "";
	document.getElementById("spa-inq-senha").value = "";
	document.getElementById("spa-inq-usuario").focus();
}

function spaInqLinkOver(oObj) {
	//oObj.style.color = "#0000CC";
	oObj.style.cursor = "hand";
	/*
	* Um "hack" maluco necessário para o Firefox. Seria um bug ou uma feature?
	* Curioso é que no IE, com ou sem esta linha, tudo funciona perfeitamente.
	*/
	oObj.style.cursor = "pointer";
}

function spaInqLinkOut(oObj) {
	//oObj.style.color = "#000000";
	oObj.style.cursor = "pointer";
}

function spaInqExibeBoleto(oObj) {
	/*
	* O Firefox só obedece width e height. A janela traz barra de endereço, statusbar e permite redimensionamento.
	* Só é possível mudar este comportamento em about:config.
	*/
	cID = oObj.id;
	cItem = cID.substring(cID.indexOf("_") + 1);
	cURL = "spa_query.php?tp=13&id=" + document.getElementById("spa-inq-chave_" + cItem).value + "&sr=" + gcSpaSrInqPro + 
		"&us=" + gcSpaInqUsu + "&se=" + gcSpaInqSen;
	window.open(cURL,"_blank","width=830,height=" + window.clientHeight + ",resizable=1,scrollbars=1,menubar=1,toolbar=1");
	/*
	// Usar  IFrame
	oIFrame = parent.document.createElement("iframe");
	if (oIFrame) {
		oIFrame.id = "frm_bol";
		oIFrame.src = cURL;
		cNav = navigator.userAgent;
		lMSIE = (cNav.indexOf("MSIE") != -1);
		if (lMSIE) {
			oIFrame.id = "frm_bol_ie";
		}
		else {
			oIFrame.style.position = "fixed";
			oIFrame.style.top = "5px";
		}
		oIFrame.style.width = "800px";
		oIFrame.style.height = "520px"; //"515px";
		window.alert(top.availHeight);
		oIFrame.style.left = "97px";
		if (lMSIE) {
			nCurScrollOffset = cumulativeScrollOffset(parent.document.getElementById("wrapper"));
			oIFrame.style.top = nCurScrollOffset + 5;
			window.alert(oIFrame.style.top);
		}
		oIFrame.style.padding = "0px";
		oIFrame.style.border = "solid 4px #74653D";
		oIFrame.frameBorder = "0";
		oBody = parent.document.body;
		oBody.appendChild(oIFrame);
	}
	*/
}

function spaInqFrmSen() {
	oAjax = spaInqCriaAjax();
	if (oAjax) {
		cURL = "spa_query.php?tp=22&sr=" + gcSpaSrInqPro;
		oAjax.open('GET',cURL,false);
		oAjax.send(null);

		if (oAjax.readyState == 4) {
			if (oAjax.status == 200) {
				oDiv = document.getElementById(gcSpaIDCtn);
				oDiv.innerHTML = oAjax.responseText;
				document.getElementById("spa-inq-usuario").focus();
				cNav = navigator.userAgent;
				lMSIE = (cNav.indexOf("MSIE") != -1);
				if (lMSIE) {
					document.getElementById("spa-inq-usuario").focus();
				}
			}
		}
	}
}

function spaInqAltSen() {
	nStatus = 0;
	oUsuario = document.getElementById("spa-inq-usuario");
	oSenha = document.getElementById("spa-inq-senha");
	oNovSen = document.getElementById("spa-inq-nov-sen");
	oConfSen = document.getElementById("spa-inq-conf-sen");
	
	if (oUsuario.value.length == 0 || oSenha.value.length == 0) {
		nStatus = 1;
	}
	else if (oNovSen.value.length == 0 || oConfSen.value.length == 0) {
		nStatus = 2;
	}
	else if (oNovSen.value != oConfSen.value) {
		nStatus = 3;
	}
	
	if (nStatus == 0) {
		oAjax = spaProCriaAjax();
		if (oAjax) {
			cURL = "spa_query.php?tp=23&us=" + oUsuario.value + "&se=" + oSenha.value + "&sr=" + gcSpaSrInqPro + 
				"&nse=" + oNovSen.value;
			oAjax.open('GET',cURL,false);
			oAjax.send(null);
			
			if (oAjax.readyState == 4) {
				if (oAjax.status == 200) {
					if (oAjax.responseText.indexOf("*NE*") != -1) {
						nStatus = 4;
					}
					else if (oAjax.responseText.indexOf("*ER*") != -1) {
						nStatus = 5;
					}
					else {
						oDiv = document.getElementById(gcSpaIDCtn);
						oDiv.innerHTML = oAjax.responseText;
					}
				}
			}
		}
	}
	
	switch (nStatus) {
		/*
		case 0:
			gcSpaProUsu = oUsuario.value;
			gcSpaProSen = oSenha.value;
			document.getElementById("spa-pro-login").style.display = "none";
			document.getElementById("spa-pro-resultado").style.display = "block";
			break;
		*/
		case 1:
			window.alert("Você deve informar o usuário e a senha atual.");
			oUsuario.select();
			oUsuario.focus();
			break;
		
		case 2:
			window.alert("Você deve digitar a nova senha 2 vezes para confirmá-la.");
			oNovSen.select();
			oNovSen.focus();
			break;
		
		case 3:
			window.alert("A nova senha digitada não confere com a confirmação.");
			oNovSen.select();
			oNovSen.focus();
			break;
		
		case 4:
			window.alert("Usuário e/ou senha inválidos.");
			oUsuario.select();
			oUsuario.focus();
			break;
		
		case 5:
			window.alert("Erro desconhecido. A senha não pôde ser alterada!");
			oUsuario.select();
			oUsuario.focus();
			break;
	}
}

function spaInqVoltSen() {
	oAjax = spaProCriaAjax();
	if (oAjax) {
		cURL = "spa_query.php?tp=11&sr=" + gcSpaSrInqPro;
		oAjax.open('GET',cURL,false);
		oAjax.send(null);
		
		if (oAjax.readyState == 4) {
			if (oAjax.status == 200) {
				oDiv = document.getElementById(gcSpaIDCtn);
				oDiv.innerHTML = oAjax.responseText;
				document.getElementById("spa-inq-usuario").focus();
				cNav = navigator.userAgent;
				lMSIE = (cNav.indexOf("MSIE") != -1);
				if (lMSIE) {
					document.getElementById("spa-inq-usuario").focus();
				}
			}
		}
	}
}

function spaInqEsqSen() {
	oAjax = spaProCriaAjax();
	if (oAjax) {
		cURL = "spa_query.php?tp=24&sr=" + gcSpaSrInqPro;
		oAjax.open('GET',cURL,false);
		oAjax.send(null);
		
		if (oAjax.readyState == 4) {
			if (oAjax.status == 200) {
				oDiv = document.getElementById(gcSpaIDCtn);
				oDiv.innerHTML = oAjax.responseText;
			}
		}
	}
}

