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

function spaProCriaAjax() {
	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 spaProTrataAjax(oObj) {
	lRet = true;
	
	if (oAjax.readyState == 4) {
		if (oAjax.status == 200) {
			// Estou usando json
			oJson = eval("(" + oAjax.responseText + ")");
			if (oAjax.responseText.indexOf("status") != -1) {
				lRet = spaProGeraResult(oObj,oJson);
			}
		}
	}
	
	return lRet;
}

function spaProVerifLogin() {
	nStatus = 0;
	oUsuario = document.getElementById("spa-pro-usuario");
	oSenha = document.getElementById("spa-pro-senha");
	
	if (oUsuario.value.length == 0 || oSenha.value.length == 0) {
		nStatus = 1;
	}
	
	if (nStatus == 0) {
		oAjax = spaProCriaAjax();
		if (oAjax) {
			// **cURL = "principal.php?" + "us=" + oUsuario.value + "&" + "se=" + oSenha.value;
			cURL = "spa_query.php?tp=8&us=" + oUsuario.value + "&se=" + oSenha.value + "&sr=" + gcSpaSrInqPro;
			oAjax.open('GET',cURL,false);
			oAjax.send(null);
			
			oResultado = document.getElementById("spa-pro-resultado");
			nStatus = spaProTrataAjax(oResultado) ? 0 : 2;
		}
	}
	
	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.");
			oUsuario.select();
			oUsuario.focus();
			break;
		
		case 2:
			window.alert("Usuário e/ou senha inválidos.");
			oUsuario.select();
			oUsuario.focus();
			break;
	}
}

function spaProGeraResult(oObj,oJson) {
	lRet = true;
	
	if (oJson.status == 0) {
		cConteudo = '<div id="spa-pro-res"><br /><span id="spa-pro-subtit"><b>CONSULTA PROPRIET&Aacute;RIO</b></span><br /><br />' + 
			'<div align="left" width="100%"><br />' + 
			'<b>Propriet&aacute;rio:</b> ' + oJson.pro[0].cod + ' - ' + oJson.pro[0].nome + '</div>' + 
			'<br /><br />';
		if (oJson.servs == 0) {
			cConteudo += '<div id="spa-pro-opcoes" align="center" width="100%">' + 
				'<table border="0" cellspacing="0" width="90%">' + 
				'<tr>' + 
				'<td width="50%">' + 
				'<input type="radio" name="opcao" id="spa-pro-op_ext" value="E" onclick="spaProExibeConteudo(this)">' + 
				'<span class="spa-pro-txt_opc">Extrato</span>' + '</input>' + 
				'</td>' + 
				'<td>' + 
				'<input type="radio" name="opcao" id="spa-pro-op_inf" value="I" onclick="spaProExibeConteudo(this)">' + 
				'<span class="spa-pro-txt_opc">Informe de rendimentos</span></input></td>' + 
				'</tr>' + 
				'</table>' + 
				'</div><br /><br />';
		}
		
		cExibe = (oJson.servs == 1) ? "block" : "none";
		cVisivel = (oJson.servs == 1) ? "visible" : "hidden";
		cConteudo += '<div id="spa-pro-extrato" style="display: ' + cExibe + '; visibility: ' + cVisivel + ';" align="center" width="100%">';
		
		/*
		* Extratos
		*/
		if (oJson.servs == 0 || oJson.servs == 1) {
			cExtrato = '<b>Para consultar ou imprimir o extrato clique sobre sua data ou saldo</b><br /><br /><br />' + 
				'<table  class="spa-pro-tab-itens" width="50%" border="0" cellspacing="0">' + 
				'<tr><td class="spa-pro-tab-itens-cab-e" align="center">Atualizado em</td>' + 
				'<td class="spa-pro-tab-itens-cab-d" align="center">Saldo dispon&iacute;vel</td></tr>';
			for (i = 0; i < oJson.extratos.length; i++) {
				cChave = oJson.pro[0].cod;
				
				cExtrato += '<tr>';
				
				cClasseEsq = (i % 2 == 0) ? "spa-pro-tab-itens-det-e-p" : "spa-pro-tab-itens-det-e-i";
				cClasseDir = (i % 2 == 0) ? "spa-pro-tab-itens-det-d-p" : "spa-pro-tab-itens-det-d-i";
				
				cExtrato += '<td class="' + cClasseEsq + '" align="center"><a id="spa-pro-a_' + i + '" onmouseover="spaProLinkOver(this)" ' + 
					'onmouseout="spaProLinkOut(this)" onclick="spaProExibeExtrato(this)"><div style="width: 100%; height: 100%;">' + 
					oJson.extratos[i].datu + '</div></a><input type="hidden" id="spa-pro-chave-e_' + i + '" value="' + cChave + '" /></td>' + 
					'<td class="' + cClasseDir + '" align="center"><a id="spa-pro-a_' + i + '" onmouseover="spaProLinkOver(this)" ' + 
					'onmouseout="spaProLinkOut(this)" onclick="spaProExibeExtrato(this)"><div style="width: 100%; height: 100%;">' + 
					oJson.extratos[i].saldo + '</div></a><input type="hidden" id="spa-pro-chave-e_' + i + '" value="' + cChave + '" /></td></tr>';
			}
			if (i > 0) {
				cExtrato += "</table>";
				cConteudo += cExtrato;
			}
			else {
				cConteudo += "<center><b>N&atilde;o h&aacute; saldo em aberto.</b></center>";
			}
		}
		
		cExibe = (oJson.servs == 2) ? "block" : "none";
		cVisivel = (oJson.servs == 2) ? "visible" : "hidden";
		cConteudo += '</div>' + 
			'<div id="spa-pro-informe" style="display: ' + cExibe + '; visibility: ' + cVisivel + ';" align="center" width="100%">';
		
		/*
		* Informes de rendimentos
		*/
		if (oJson.servs == 0 || oJson.servs == 2) {
			cInforme = '<b>Para consultar ou imprimir um informe de rendimentos clique sobre o ano desejado</b><br /><br /><br />' + 
				'<table  class="spa-pro-tab-itens" width="25%" border="0" cellspacing="0">' + 
				'<tr><td class="spa-pro-tab-itens-cab-d" align="center">Ano</td></tr>'; // + 
				//'<td class="tab-itens-cab-d" align="center">Saldo dispon&iacute;vel</td></tr>';
			for (i = 0; i < oJson.informes.length; i++) {
				cChave = oJson.pro[0].cod + "," + oJson.informes[i].ano;
				
				cInforme += '<tr>';
				
				cClasseEsq = (i % 2 == 0) ? "spa-pro-tab-itens-det-e-p" : "spa-pro-tab-itens-det-e-i";
				cClasseDir = (i % 2 == 0) ? "spa-pro-tab-itens-det-d-p" : "spa-pro-tab-itens-det-d-i";
				
				cInforme += '<td class="' + cClasseDir + '" align="center"><a id="spa-pro-a_' + i + '" onmouseover="spaProLinkOver(this)" ' + 
					'onmouseout="spaProLinkOut(this)" onclick="spaProExibeInforme(this)"><div style="width: 100%; height: 100%;">' + 
					oJson.informes[i].ano + '</div></a><input type="hidden" ' + 
					'id="spa-pro-chave-i_' + i + '" value="' + cChave + '" /></td></tr>';
			}
			if (i > 0) {
				cInforme += "</table>";
				cConteudo += cInforme;
			}
			else {
				cConteudo += '<center><b>Nenhum informe de rendimentos dispon&iacute;vel.</b></center>';
			}
		}
		
		cConteudo += '</div>';
		
		oObj.innerHTML = cConteudo + '<br />' + 
			'<center><input type="button" value="Fechar" name="spa-pro-btn_fecha" id="spa-pro-btn_fecha" onclick="spaProFechaPesquisa()" /></center>' + 
			'</div>';
	}
	else {
		lRet = false;
	}
	
	return lRet;
}

function spaProExibeConteudo(oObj) {
	oExtrato = document.getElementById("spa-pro-extrato");
	oInforme = document.getElementById("spa-pro-informe");
	if (oObj.value == "E") {
		oInforme.style.visibility = "hidden";
		oInforme.style.display = "none";
		oExtrato.style.visibility = "visible";
		oExtrato.style.display = "block";
	}
	else if (oObj.value == "I") {
		oExtrato.style.visibility = "hidden";
		oExtrato.style.display = "none";
		oInforme.style.visibility = "visible";
		oInforme.style.display = "block";
	}
}

function spaProFechaPesquisa() {
	document.getElementById("spa-pro-extrato").style.visibility = "hidden";
	document.getElementById("spa-pro-informe").style.visibility = "hidden";
	document.getElementById("spa-pro-resultado").style.display = "none";
	
	document.getElementById("spa-pro-login").style.display = "block";
	
	document.getElementById("spa-pro-usuario").value = "";
	document.getElementById("spa-pro-senha").value = "";
	document.getElementById("spa-pro-usuario").focus();
}

function spaProLinkOver(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 spaProLinkOut(oObj) {
	//oObj.style.color = "#000000";
	oObj.style.cursor = "pointer";
}

function spaProExibeExtrato(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=9&id=" + document.getElementById("spa-pro-chave-e_" + cItem).value + "&sr=" + gcSpaSrInqPro + 
		"&us=" + gcSpaProUsu + "&se=" + gcSpaProSen;
	window.open(cURL,"_blank","width=850,height=" + window.clientHeight + ",resizable=1,scrollbars=1,menubar=1,toolbar=1");
}

function spaProExibeInforme(oObj) {
	cID = oObj.id;
	cItem = cID.substring(cID.indexOf("_") + 1);
	/**
	cURL = "informe.php?id=" + document.getElementById("chave-i_" + cItem).value;
	window.open(cURL,"_blank","width=850,height=" + window.clientHeight + ",resizable=1,scrollbars=1,menubar=1,toolbar=1");
	**/
	cURL = "spa_query.php?tp=10&id=" + document.getElementById("spa-pro-chave-i_" + cItem).value + "&sr=" + gcSpaSrInqPro + 
		"&us=" + gcSpaProUsu + "&se=" + gcSpaProSen;
	window.open(cURL,"_blank","width=850,height=" + window.clientHeight + ",resizable=1,scrollbars=1,menubar=1,toolbar=1");
}

function spaProFrmSen() {
	oAjax = spaProCriaAjax();
	if (oAjax) {
		cURL = "spa_query.php?tp=19&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-pro-usuario").focus();
				cNav = navigator.userAgent;
				lMSIE = (cNav.indexOf("MSIE") != -1);
				if (lMSIE) {
					document.getElementById("spa-pro-usuario").focus();
				}
			}
		}
	}
}

function spaProAltSen() {
	nStatus = 0;
	oUsuario = document.getElementById("spa-pro-usuario");
	oSenha = document.getElementById("spa-pro-senha");
	oNovSen = document.getElementById("spa-pro-nov-sen");
	oConfSen = document.getElementById("spa-pro-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=20&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 spaProVoltSen() {
	oAjax = spaProCriaAjax();
	if (oAjax) {
		cURL = "spa_query.php?tp=7&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-pro-usuario").focus();
				cNav = navigator.userAgent;
				lMSIE = (cNav.indexOf("MSIE") != -1);
				if (lMSIE) {
					document.getElementById("spa-pro-usuario").focus();
				}
			}
		}
	}
}

function spaProEsqSen() {
	oAjax = spaProCriaAjax();
	if (oAjax) {
		cURL = "spa_query.php?tp=21&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;
			}
		}
	}
}

