// Copyright (c) 2002 SERPRO
// Developed by Lucas Martins do Amaral, MSc
//
// 15/07/2002
//

function TrimLeft(str)	{
var	strIn;
	strIn = new String(str);
	for(i = 0; i < strIn.length; i++)	{
		if( strIn.charAt(i) != ' ' )
			break;
	}
	return strIn.substr(i);
}

function TrimRight(str)	{
var	strIn;
	strIn = new String(str);
	for(i = strIn.length - 1; i > 0 ; i--)	{
		if( strIn.charAt(i) != ' ' )
			break;
	}
	return strIn.substring(0, i+1);
}

function EliminarANDORNEAR(str)	{
var	strRes = new String(str);
	what = /[ ]AND[ ]/gi;
	strRes = strRes.replace(what, '');
	what = /[ ]OR[ ]/gi;
	strRes = strRes.replace(what, '');
	what = /[ ]NEAR[ ]/gi;
	strRes = strRes.replace(what, '');
	what = /[ ]+/g;
	strRes = strRes.replace(what, ' ');
	strRes = TrimLeft(strRes);
	strRes = TrimRight(strRes);
	return strRes;
}

function EliminarAspas(str)	{
var	strRes = new String(str);
	what = /\"/g;
	strRes = strRes.replace(what, '');
	what = /\'/g;
	strRes = strRes.replace(what, '');
	strRes = TrimLeft(strRes);
	strRes = TrimRight(strRes);
	return strRes;
}

function ORSearch(str)	{
var	strRes = new String(str);
	what = /[ ]+/g;
	return strRes.replace(what, ' OR ');
}

function ANDSearch(str)	{
var	strRes = new String(str);
	what = /[ ]+/g;
	return strRes.replace(what, ' AND ');
}

function ExactSearch(str)	{
var	strRes = new String(str);
	return '"' + strRes + '"';
}

/*
function SubmeterPesquisa() {
		var strText = new String(document.PesquisaSRF.Text2Search.value);
		var strPesquisa = new String('Exata');

		for( i = 0; i < document.PesquisaSRF.TipoPesquisa.length; i++ )	{
			if( document.PesquisaSRF.TipoPesquisa[i].checked == true )	{
				strPesquisa = new String(document.PesquisaSRF.TipoPesquisa[i].value);
				break;
			}
		}
		
		strText = EliminarAspas(strText);
		strText = EliminarANDORNEAR(strText);
		if( strText == '' )
			return false;
		
		if( strPesquisa == 'Todas' )	{
			strText = ANDSearch(strText);
		}
		else if(strPesquisa == 'Qualquer' )	{
			strText = ORSearch(strText);
		}
		else	{
			strText = ExactSearch(strText);
		}

		document.PesquisaSRF.TextRestrictionPlain.value = strText;
		//alert('[' + strText + ']');
		document.PesquisaSRF.action =  '/scripts/srf/pesquisa/srfpesquisa.idq';
		document.PesquisaSRF.submit();
}
*/

function SubmeterPesquisa() {
	var strText = new String(document.BuscaAvancada.Text2Search.value);
	
	strText = EliminarAspas(strText);
	strText = EliminarANDORNEAR(strText);
	if( strText == '' )	{
		return;
	}
	//strText = ExactSearch(strText);
	//document.BuscaAvancada.Text2Search.value = '';
	document.BuscaAvancada.Criteria.value = strText;
	document.BuscaAvancada.action =  '/Aplicacoes/ATBHE/Busca/ResultPesq.asp';
	//document.BuscaAvancada.target = 'main';
	document.BuscaAvancada.submit();
}

function SubmeterPesquisaViaMenu() {
	var strText = new String(document.BuscaSRF.Text2Search.value);	
	strText = EliminarAspas(strText);
	strText = EliminarANDORNEAR(strText);
	
	if( strText == '' )	{
		return;
	}
	
	document.BuscaSRF.Criteria.value = strText;
	
	// Caso exista o campo CriteriaLength preenche com o tamanho da string
	// de busca. Em ResultPesq.asp esse valor é usado para
	// verificar se é necessário converter a string de busca para outro charset.
	// Por exemplo: quando a busca é feita a partir de default.htm, que está
	// em UTF-8.
	var lengthField = document.BuscaSRF.CriteriaLength;
	if (lengthField) {
		lengthField.value = strText.length;
	}
	
	
	//document.BuscaSRF.action =  '/Aplicacoes/ATBHE/Busca/debug.asp';
	document.BuscaSRF.action =  '/Aplicacoes/ATBHE/Busca/ResultPesq.asp';
	document.BuscaSRF.submit();
}

/*
function SubmeterPesquisaAvancada() {
	var MyDate = document.PesquisaSRF.FMModDateAux.value; 
	document.PesquisaSRF.FMModDate.value = MyDate.substring(06,10) + "/" + MyDate.substring(03,05) + "/" + MyDate.substring(00,02);

	var strText = new String(document.PesquisaSRF.Text2Search.value);
	strText = EliminarAspas(strText);
	strText = EliminarANDORNEAR(strText);
	if( strText == '' )	{
		alert('O preenchimento do campo Texto é obrigatório!');
		return;
	}

	var strPesquisa = new String('Exata');
	for( i = 0; i < document.PesquisaSRF.TipoPesquisa.length; i++ )	{
		if( document.PesquisaSRF.TipoPesquisa[i].checked == true )	{
			strPesquisa = new String(document.PesquisaSRF.TipoPesquisa[i].value);
			break;
		}
	}
		
	strText = EliminarAspas(strText);
	strText = EliminarANDORNEAR(strText);
	if( strPesquisa == 'Todas' )	{
		strText = ANDSearch(strText);
	}
	else if(strPesquisa == 'Qualquer' )	{
		strText = ORSearch(strText);
	}
	else	{
		strText = ExactSearch(strText);
	}

	document.PesquisaSRF.TextRestrictionPlain.value = strText;
	document.PesquisaSRF.action =  '/scripts/srf/pesquisa/srfpesquisa.idq';
	document.PesquisaSRF.submit();
}
*/


//-------------------------------
function isEmpty(s){
    return ((s == null) || (s.length == 0))
}

function LeTeclado(action)	{
	if(event.keyCode == 13 )	{
		SubmeterPesquisaViaMenu();
//		document.PesquisaSRF.target = '';
//		document.PesquisaSRF.action = action;
	}
	event.returnValue = true;
}
