function fecharAjax(obj){
	var jAjax = new ajax();
		jAjax.fecharSugestao(obj);	
}

function consultarAjax(obj,objEvent,relOpcoes){
	if (relOpcoes != ''){
		var jAjax = new ajax();
			jAjax.inicializar(obj.id,KeyPressAscCode(objEvent),relOpcoes);
			jAjax.montarDiv();
	}
}

function KeyPressAscCode(objEvent) {
	var Code;
	if (navigator.appName == 'Microsoft Internet Explorer') {
		Code = objEvent.keyCode;
	} else if (navigator.appName == 'Netscape') {
		Code = objEvent.which; 
	}
	return Code;
}

function ajax(){
	var divName;
	var valueDiv;
	var tecla;
	this.inicializar = inicializar;
	this.montarDiv = montarDiv;
	this.selecionarSugestao = selecionarSugestao;
	this.listarOpcoes = listarOpcoes;
	this.fecharSugestao = fecharSugestao;
	this.selecionarMouse = selecionarMouse;
	
	function fecharSugestao(obj){
		document.getElementById('div'+obj.id).innerHTML = '';
		document.getElementById('div'+obj.id).style.border = '';
	}
	
	function inicializar(jDivName,jTecla,jValueDiv){
		this.valueDiv = jValueDiv;
		this.divName = jDivName;
		this.tecla = jTecla;
	}
	
	function listarOpcoes(){
		jListaSugestao = '';
		qtdeSugestoes = 6;
		v = this.valueDiv.split(';');
		j = 1;
		jEx = 1;
		for(var i=1; i<=v.length; i++){
			if (v[i-1].toUpperCase().indexOf(document.getElementById(this.divName).value.toUpperCase()) > -1){
				if (j <= qtdeSugestoes){
					if (v[i-1].indexOf('|') > -1){
						arValor = v[i-1].split('|');		
						valorExibir = arValor[1];
						jListaSugestao += '<span id="spanIdSugestao_'+ j +'" style="display:none">'+ arValor[0] +'</span>';
					}
					else{
						valorExibir = v[i-1];
					}
					valorSpanFormatado = valorExibir.replace(document.getElementById(this.divName).value,'<span class=divSugestaoParteDigitada>'+document.getElementById(this.divName).value+'</span>');
					jListaSugestao += '<div id="divSugestao_'+ j +'" class="sugestaoNormal" onClick="selecionarMouseAjax('+ j +')">'+ valorSpanFormatado +'</div>';
					jEx++;
				}
				else{
					i = v.length + 1;
				}
				j++;
			}
		}
		jTotal = j - 1;
		jTotalEx = jEx - 1;
		if (jTotalEx > 0 ){
		//	jListaSugestao += '<div id="divSugestaoRodape" class="sugestaoRodape">sugestões: '+ jTotalEx +' de '+ jTotal +'</div>';
		}
		return jListaSugestao;
	}
	
	function montarDiv(){	
			if (document.getElementById(this.divName).style.width != ''){
				document.getElementById('div'+this.divName).style.width = document.getElementById(this.divName).style.width;
			}
			
			if ((this.tecla == 40) || (this.tecla == 38) || (this.tecla == 13) || (this.tecla == 27)){
				this.selecionarSugestao();
			}
			else{			
				var opc = this.listarOpcoes();
				document.getElementById('div'+this.divName).innerHTML = opc;
				document.getElementById('div'+this.divName).style.border = '';
				
				if (opc != '')
					document.getElementById('div'+this.divName).style.border = '1px solid #abc670';
				else
					document.getElementById('div'+this.divName).style.border = '';
				
				this.selecionarSugestao();
			}
	}	
	
	function selecionarSugestao(){
		var i = 1;
		var jLoop = true;
		var retorno = 0;
		do{
			try{
				if (document.getElementById('divSugestao_'+i).className == 'sugestaoSelecionada'){
					retorno = i;
				}
			}
			catch(e){
				jLoop = false;
			}
		i++;
		}while(jLoop);
		i = i -2;

		try{
			if (this.tecla == 40)
				numProx = retorno+1;
			if (this.tecla == 38)
				numProx = retorno-1;
				
			document.getElementById('divSugestao_'+numProx).className = 'sugestaoSelecionada';
		}
		catch(e){}	
		
		try{
			if (this.tecla == 40)
				numAnt = numProx-1;
			if (this.tecla == 38)
				numAnt = numProx+1;
				
			document.getElementById('divSugestao_'+numAnt).className = 'sugestaoNormal';
		}
		catch(e){}	
		
		if (this.tecla == 13){
			this.selecionarMouse(retorno);
			document.getElementById('div'+this.divName).style.border = '';
		}
		if (this.tecla == 27){
			document.getElementById('div'+this.divName).innerHTML = '';
			document.getElementById('div'+this.divName).style.border = '';
		}
		
		if ((this.tecla != 40) && (this.tecla != 38) && (this.tecla != 13) && (this.tecla != 27)){
			for (var j=1; j<=i; j++){
				document.getElementById('divSugestao_'+j).className = 'sugestaoNormal';
			}
			document.getElementById('divSugestao_1').className = 'sugestaoSelecionada';
		}
	}
	
	function selecionarMouse(jRetorno){
		if (jRetorno != ''){
			document.getElementById(this.divName).value = document.getElementById('divSugestao_'+jRetorno).innerHTML;
			document.getElementById(this.divName).value = document.getElementById(this.divName).value.replace('<span class="divSugestaoParteDigitada">','');
			document.getElementById(this.divName).value = document.getElementById(this.divName).value.replace('</span>','');
			document.getElementById(this.divName).value = document.getElementById(this.divName).value.replace('<SPAN class=divSugestaoParteDigitada>','');
			document.getElementById(this.divName).value = document.getElementById(this.divName).value.replace('</SPAN>','');
			
			try{
				document.getElementById('id'+this.divName).value = document.getElementById('spanIdSugestao_'+jRetorno).innerHTML;
			}
			catch(e){}
			
			document.getElementById('div'+this.divName).innerHTML = '';
		}
	}
}

