﻿//Funções para a página de serviços

var browser = navigator.appName;

var dsplTableRow = 'block';
if(browser == 'Netscape') dsplTableRow = 'table-row';

//Variáveis globais
var pathFile;
var intervalo;
var x = -1;

var palavraChave;
var currentSession = 'Principal';
var lastSession = 'Principal';
var results;
var links = '';
var description;
var structure;

//Porcentagem
var contador = 1;
var total;
var umPorcento;
var porcentagemAtual;
var barra;

var paginasErro = '';

//execFind()
function execFind(){
	palavraChave = document.getElementById('palavraChave').value;
	document.getElementById('bPalavraChave').innerHTML = palavraChave;

	pathFile = document.getElementById('pathFile').value;
	results = 0;

	if(pathFile.indexOf(', ')){
		pathFile = pathFile.split(', ');
		
		total = pathFile.length;
		umPorcento = 100/total;
		
		findNxtPage();		
	}else{
		sendXmlHttpRequest(pathFile, true, false);
		returnValue = treatReturnValue(returnValue);

		document.getElementById('divResult').innerHTML += '<b>'+pathFile.lastIndexOf('/')+'</b><br />';
		document.getElementById('divResult').innerHTML += x+' de '+pathFile.length;
		document.getElementById('divResult').innerHTML += '<br /><br />';
	}
}

//findNxtPage()
function findNxtPage(){

	//Calcula a porcentagem
	porcentagemAtual = document.getElementById('porcentagemAtual').innerHTML;
	porcentagemAtual = contador*umPorcento;

	document.getElementById('barra').style.width = porcentagemAtual + '%';					
	document.getElementById('porcentagemAtual').innerHTML = parseInt(porcentagemAtual);

	if(contador == total){
		if(document.getElementById('divResult').innerHTML.length == 0){
			document.getElementById('divResult').innerHTML = 'Nenhum registro encontrado!';
		}
		//alert('Erro no processamento das páginas:\n' + paginasErro);
		return;
	}else{
		contador++;
		x++;
	}

	//Define a sessao
	currentSession = getSession(pathFile[x]);
	//document.getElementById('divResultPath').innerHTML += pathFile[x] + '<br />';

	//Envia a requisicao ajax
	sendXmlHttpRequest(pathFile[x], true, false);
	
	intervalo = window.setInterval(
		function(){
			if(intervalProcessingAjax == 'ok'){

				clearInterval(intervalo);

				if(returnValue != 'erro' && returnValue != ''){

					//Se a sessao corrente for diferente da ultima sessao, cria uma nova sessao
					if(currentSession != lastSession){

						//Se houver mais de um resultado na sessao
						if(results > 0){
							structure = createStructure(lastSession, results, links)
							document.getElementById('divResult').innerHTML += structure;
							results = 0;
						}

						lastSession = currentSession;
						links = '';
					}

					//Trata o retorno, se retornar "true" adiciona um link
					if(treatReturnValue(returnValue)){
						links += '<li><a href="'+pathFile[x]+'">' + description + '</a></li>';
						results++;
					}

					returnValue = '';

				}else{
					paginasErro += '- ' + pathFile[x] + '\n';
				}

				findNxtPage();
			}
		}
	, 100);
}

//getSession(strPathFile)
function getSession(strPathFile){
	var strCurrentSession;
	
	if(strPathFile.lastIndexOf('/') == 0){
		strCurrentSession = "Principal";	
	}else{
		strCurrentSession = strPathFile.substr(0, strPathFile.lastIndexOf('/'))
		strCurrentSession = strCurrentSession.substr(strCurrentSession.lastIndexOf('/') + 1, strCurrentSession.length - strCurrentSession.lastIndexOf('/'))
	
		if(strCurrentSession == 'cadastre_se'){
			strCurrentSession = 'Cadastre-se';
		}else if(strCurrentSession == 'fale_conosco'){
			strCurrentSession = 'Fale Conosco';
		}else if(strCurrentSession == 'inovacao'){
			strCurrentSession = 'Inova&ccedil;&atilde;o';			
		}else if(strCurrentSession == 'meio_ambiente'){
			strCurrentSession = 'Meio Ambiente';
		}else if(strCurrentSession == 'patrocinios'){
			strCurrentSession = 'Patroc&iacute;nios';
		}else if(strCurrentSession == 'relatorio_anual'){
			strCurrentSession = 'Relat&oacute;rio Anual';
		}else if(strCurrentSession == 'servicos'){
			strCurrentSession = 'Servi&ccedil;os';
		}
	}
	
	return strCurrentSession;
}

//createStructure(strSession, intResults, strLinks)
function createStructure(strSession, intResults, strLinks){
	var strStructure = 	'<div class="busca">' +
						'	<div class="buscaBordaTop"></div>' +
						'	<div class="sessao">' + strSession + '</div>' +
						'	<div class="ocorrencias">' + intResults + ' ocorr&ecirc;ncia(s)</div>' +
						'	<div class="buscaBordaBottom"></div>' +
						'</div>' +
						'<div class="resultadoBusca">' +
						'	<ul>' +
								strLinks +
						'	</ul>' +
						'</div>';

	return strStructure;
}

//treatReturnValue(val)
function treatReturnValue(val){
	var ini = val.indexOf('<!--Inicio Conteudo-->');
	var qtd = val.indexOf('<!--Fim Conteudo-->');
	var iniDescription;
	var fimDescription;
	var qtdDescription;

	if(ini > 0 && qtd > 0){ 
		qtd = qtd - ini
		val = val.substr(ini, qtd);

		iniDescription = val.indexOf('<!--@DescricaoInicio');
		fimDescription = val.indexOf('@DescricaoFim-->')
		qtdDescription = fimDescription - iniDescription;

		if(iniDescription > 0 && fimDescription > 0){
			description = val.substr(iniDescription + 20, qtdDescription - 20);
		}else{
			description = 'Não há descrição na página!';
		}

		val = removeHTMLTags(val);
		val = replaceHTMLChrCod(val);

		if(val.indexOf(palavraChave) > 0 || (currentSession == 'produtos' && pathFile[x].indexOf('index') > 0 && val.indexOf('Nenhum registro encontrado!') == -1)){
			return true;
		}
	}

	return false;		
}

//removeHTMLTags(strCode)
function removeHTMLTags(strCode){
 	if(strCode.length > 0){
 	 	strCode = strCode.replace(/&(lt|gt);/g, function (strMatch, p1){
 		 	return (p1 == "lt")? "<" : ">";
 		});
		
 		strCode = strCode.replace(/<\/?[^>]+(>|$)/g, "");
		return strCode;
 	}
}

//replaceHTMLChrCod(strVal)
function replaceHTMLChrCod(strVal){
	if(strVal.indexOf('&quot;') > -1) strVal = strVal.replace(/&quot;/g, '"');
	if(strVal.indexOf('&amp;') > -1) strVal = strVal.replace(/&amp;/g, '&');
	if(strVal.indexOf('&acute;') > -1) strVal = strVal.replace(/&acute;/g, '´');	
	if(strVal.indexOf('&Ccedil;') > -1) strVal = strVal.replace(/&Ccedil;/g, 'Ç');
	if(strVal.indexOf('&ccedil;') > -1) strVal = strVal.replace(/&ccedil;/g, 'ç');
	if(strVal.indexOf('&Ntilde;') > -1) strVal = strVal.replace(/&Ntilde;/g, 'Ñ');
	if(strVal.indexOf('&ntilde;') > -1) strVal = strVal.replace(/&ntilde;/g, 'ñ');
	
	//Letra A
	if(strVal.indexOf('&Aacute;') > -1) strVal = strVal.replace(/&Aacute;/g, 'Á');
	if(strVal.indexOf('&aacute;') > -1) strVal = strVal.replace(/&aacute;/g, 'á');
	if(strVal.indexOf('&Agrave;') > -1) strVal = strVal.replace(/&Agrave;/g, 'À');
	if(strVal.indexOf('&agrave;') > -1) strVal = strVal.replace(/&agrave;/g, 'à');
	if(strVal.indexOf('&Atilde;') > -1) strVal = strVal.replace(/&Atilde;/g, 'Ã');
	if(strVal.indexOf('&atilde;') > -1) strVal = strVal.replace(/&atilde;/g, 'ã');
	if(strVal.indexOf('&Acirc;') > -1) strVal = strVal.replace(/&Acirc;/g, 'Â');
	if(strVal.indexOf('&acirc;') > -1) strVal = strVal.replace(/&acirc;/g, 'â');
	
	//Letra E
	if(strVal.indexOf('&Eacute;') > -1) strVal = strVal.replace(/&Eacute;/g, 'É');
	if(strVal.indexOf('&eaacute;') > -1) strVal = strVal.replace(/&eaacute;/g, 'é');
	if(strVal.indexOf('&Ecirc;') > -1) strVal = strVal.replace(/&Ecirc;/g, 'Ê');
	if(strVal.indexOf('&ecirc;') > -1) strVal = strVal.replace(/&ecirc;/g, 'ê');
	
	//Letra I
	if(strVal.indexOf('&Iacute;') > -1) strVal = strVal.replace(/&Iacute;/g, 'Í');
	if(strVal.indexOf('&iacute;') > -1) strVal = strVal.replace(/&iacute;/g, 'í');	
	
	//Letra O
	if(strVal.indexOf('&Oacute;') > -1) strVal = strVal.replace(/&Oacute;/g, 'Ó');
	if(strVal.indexOf('&oacute;') > -1) strVal = strVal.replace(/&oacute;/g, 'ó');
	if(strVal.indexOf('&Otilde;') > -1) strVal = strVal.replace(/&Otilde;/g, 'Õ');
	if(strVal.indexOf('&otilde;') > -1) strVal = strVal.replace(/&otilde;/g, 'õ');
	if(strVal.indexOf('&Ocirc;') > -1) strVal = strVal.replace(/&Ocirc;/g, 'Ô');
	if(strVal.indexOf('&ocirc;') > -1) strVal = strVal.replace(/&ocirc;/g, 'ô');
	
	//Letra U
	if(strVal.indexOf('&Uacute;') > -1) strVal = strVal.replace(/&Uacute;/g, 'Ú');
	if(strVal.indexOf('&uacute;') > -1) strVal = strVal.replace(/&uacute;/g, 'ú');
	if(strVal.indexOf('&Uuml;') > -1) strVal = strVal.replace(/&Uuml;/g, 'Ü');
	if(strVal.indexOf('&uuml;') > -1) strVal = strVal.replace(/&uuml;/g, 'ü');
	
	return strVal;
}
