function infraAjaxGetElementsByTagName(xml,tag){
  try{
    var arr = xml.getElementsByTagName(tag);
  }catch(exc){
    arr = new Array();
  }  
  return arr;
}

function infraAjaxCriarRequest() {
  request = null;
  try {
    request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = false;
      }
    }
  }

  if (!request)
     alert('Este navegador não possui recursos para uso do AJAX.');
  else
  	return request;
}


function infraAjaxMontarPostPadraoSelect(primeiroItemValor,primeiroItemDescricao,valorItemSelecionado){
  var post = 'primeiroItemValor='+primeiroItemValor;
  if (infraTrim(primeiroItemDescricao)==''){
     primeiroItemDescricao = ' ';
  }
  post += '&primeiroItemDescricao='+primeiroItemDescricao;
  post += '&valorItemSelecionado='+valorItemSelecionado;  
  return post;
}

function infraAjaxImagemVerificado(obj){
	var imgId = 'imgInfraAjaxVerificado'+obj.id;
	imgObj = document.getElementById(imgId);
	if (imgObj!=null)return imgObj;
	imgObj = document.createElement('img');	
	imgObj.id = imgId;
	imgObj.style.position = 'absolute';
  imgObj.src =  '/infra_css/imagens/verificado.gif'
	imgObj.style.visibility = 'hidden';
	//obj.parentNode.appendChild(imgObj);
	document.body.appendChild(imgObj);	
	return imgObj
}

function infraAjaxMostrarImg(obj,img,offsetx,offsety){
	var el = obj;
	var x = el.offsetWidth+offsetx;
	var y = offsety;

	//Walk up the DOM and add up all of the offset positions.
	while (el.offsetParent && el.tagName.toUpperCase() != 'BODY'){
		x += el.offsetLeft;
		y += el.offsetTop;
		el = el.offsetParent;
	}

	x += el.offsetLeft;
	y += el.offsetTop;

	img.style.left = x + 'px';
	img.style.top = y + 'px'; 
	img.style.visibility = 'visible';
	
	el = null;		
	obj = null;
	img = null;
}

function infraAjaxOcultarImg(img){
  img.style.visibility = 'hidden';
}

function infraAjaxPost(obj){
  
  obj.executou = false;
  
 if (obj.ajaxReq != undefined){
   //Cancelar requisicao antiga  
   obj.ajaxReq.abort();
   while(1){
     if (obj.ajaxReq.readyState == 0){
       
       var post = null;
       
        if (typeof(obj.prepararExecucao)=='function'){
           post = obj.prepararExecucao();
        }
        
        //Execucao cancelada
        if (post == false){
          return;
        }
    
        if (post != null){
              //A comunicação é feita via UTF-8, para tratar corretamente a acentuação
              //usar encodeURIComponent e no lado do servidor utf8_decode
              post = encodeURIComponent(post);
              post = post.replace(/%26/g,'&');   
              post = post.replace(/%3D/g,'=');
              post = post.replace(/%20/g,' ');
        }

        /*
        alert('URL: '+url)
        */
        var metodo = "POST";
        if (post==null){
          metodo = "GET"
        }
        /*
        else{
          alert('POST: '+post);
        }
        */

        obj.iniciarExecucao();
        
        if (obj.mostrarAviso){
          infraExibirAviso();
        }
        
        obj.executou = true;
        
        async = ((obj.async == undefined) ? true : obj.async);
        
        obj.ajaxReq.open(metodo, obj.ajaxTarget, async);
        obj.ajaxReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
        obj.ajaxReq.onreadystatechange = obj.processarAjax;
        obj.ajaxReq.send(post);
        
        //if (!async){
        //if (!async && INFRA_IE == 0){
        if (!async && INFRA_FF > 0){ 
          obj.processarAjax();
        }
        if (obj.mostrarAviso){
          self.setTimeout('infraOcultarAviso()', obj.tempoAviso);
        }
        
        obj.finalizarExecucao();
           
        break;
     }
   }
 }
}


function infraAjaxProcessarXML(obj){
  
  if (obj.ajaxReq.readyState == 4){
    
    bolErro = true;
    try{
      if(obj.ajaxReq.status == 200) {
        bolErro=false;
      }
    }catch(componentfailure){}
    
    if (bolErro){
  	  if (obj.mostrarAviso){ 
  	    infraOcultarAviso();  
  	  }
      return false;
    }
    
    //alert(obj.ajaxReq.responseText); //DEBUG
  	
    var texto = obj.ajaxReq.responseText;
  
    
    //Erro que não retornou dentro de um XML
    //Ocorre quando o PHP aborta
    if (texto.substring(1,5)!='?xml'){
    	 if (obj.mostrarAviso){
    	   infraOcultarAviso();
    	 }
       //alert(texto);
       return false; 
    }

    
    
    //Verifica se o XML contém erros
    var nroErros = 0;
    var xml = obj.ajaxReq.responseXML;
    
    
    //var itens = xml.getElementsByTagName('erro');
    var itens = infraAjaxGetElementsByTagName(xml,'erro');
   
	  if (itens.length>0){
	    
  	  if (obj.mostrarAviso){
  	    infraOcultarAviso();
  	  }
	    
	    itErros = itens.length;
		  for (i=0; i<itErros; i++){
		    var desc = itens[i].getAttribute("descricao");
		    desc = desc.infraReplaceAll("\\n","\n");
	      alert(desc);
	      nroErros = nroErros + 1;
		  }
		  obj.processarErro();
	  }
	  itens = null;
	  
	  if (nroErros > 0){
	    xml = null;
	    return false;
	  }
  	return xml;
  }
  
  return false;
}

function infraAjaxAutoCompletar(hdnId, txtDesc, url){
	var me = this;
	//Define que o texto selecionado não deve ser removido do campo
	this.hdn = infraGetElementById(hdnId);
	this.elem = infraGetElementById(txtDesc);
	this.highlighted = null;
	this.arrItens = new Array();
	this.ajaxTarget = url;
	this.div = null;
	this.prepararExecucao = null;
	this.processarResultado = null;
	this.mostrarImagemVerificado = true;
	this.mostrarImagemAjuda = false;
	this.tamanhoMinimo = 1;
	this.limparCampo = true;
	this.mostrarAviso = false;
	this.tempoAviso = 0;
	this.executou = false;
	this.maiusculas = false;
	this.permitirSelecaoGrupo = false;
	this.permitirSelecaoFilho = true;
	this.bolExecucaoAutomatica = true;
	this.offsetX = 0;
	this.offsetY = 0;
	
	
	//Keycodes que devem ser monitorados
	var TAB = 9;
	var ESC = 27;
	var KEYUP = 38;
	var KEYDN = 40;
	var ENTER = 13;
	
	var HOME = 36;
	var END = 35;
	var SHIFT = 16;
	
	//Desabilitar autocomplete IE
	this.elem.setAttribute("autocomplete","off");
	
	//Crate AJAX Request
	this.ajaxReq = infraAjaxCriarRequest();
	//if (me.mostrarImagemAjuda){
	  //infraAjaxImagemAjuda(me.elem,'Digite os caracteres iniciais do campo solicitado, após escolha na lista o item desejado.');
	//}
	this.img = infraAjaxImagemVerificado(me.elem);
	
	this.inicializarDiv = function(){
  	var divId = 'divInfraAjax'+me.elem.id;
  	me.div = document.getElementById(divId);
  	if (me.div == null){
    	divObj = document.createElement('div');	
    	divObj.id = divId;
    	divObj.className = 'infraAjaxAutoCompletar';
    	//Tamanho da div igual ao tamanho do campo
    	divObj.style.width = me.elem.style.width;
    	document.body.appendChild(divObj);	
    	me.div = divObj;
  	}
  	divId = null;
  	divObj = null;
	}

  me.inicializarDiv();

	this.setElemValue = function(){
		var a = me.highlighted.firstChild;
		me.elem.value = a.innerTEXT;
		a = null;
	}
	
	//marca lista pelo uso do mouse
	this.highlightThis = function(obj,yn){
		if (yn = 'y' && obj.indice != undefined){
		  
		  if (me.highlighted != null){
		    me.highlighted.className = '';
		  }
		  
			me.highlighted = obj;
			me.highlighted.className = 'selected';
			me.setElemValue();
			
		}else{
			obj.className = '';
			me.highlighted = null;
		}
	}
	
	//marca lista pelo uso do teclado
	this.changeHighlight = function(way){
		
	  if (me.highlighted != null){
			me.highlighted.className = '';
			switch(way){
				case 'up':
					if(me.highlighted.parentNode.firstChild == me.highlighted){
						me.highlighted = me.highlighted.parentNode.lastChild;
					}else{
						me.highlighted = me.highlighted.previousSibling;
					}
				break;
				case 'down':
					if(me.highlighted.parentNode.lastChild == me.highlighted){
						me.highlighted = me.highlighted.parentNode.firstChild;
					}else{
						me.highlighted = me.highlighted.nextSibling;
					}
				break;
				
			}
		}else{
			switch(way){
				case 'up':
					me.highlighted = me.div.firstChild.lastChild;
				break;
				case 'down':
					me.highlighted = me.div.firstChild.firstChild;
				break;
			}
		}
		
		
		//grupo não selecionável
		if (me.highlighted != null){
  		if (me.highlighted.indice==undefined){
  		  
  		  //verifica se tem algum item passível de marcação
  		  var lis = me.div.getElementsByTagName('li');
  		  for(var i=0;i<lis.length;i++){
  		    if (lis[i].indice != undefined){
  		      me.changeHighlight(way);  
  		      break;
  		    }
  		  }
  		}else{
  		  me.highlighted.className = 'selected';
  		  me.setElemValue();
  		}
		}
	}
	
	//Ação a ser executada no KEYDOWN (funções de navegação)
	this.elem.onkeydown = function(ev)
	{
	  
		var key = infraGetCodigoTecla(ev);
		
		switch(key)
		{
			case ENTER:
				//if (me.highlighted != null && me.highlighted.indice != undefined){
				//	me.escolher(me.highlighted.indice);
				//}
				//me.hideDiv();
				return false;

			case ESC:
				me.hideDiv();
				return false;

			case KEYUP:
				me.changeHighlight('up');
				return false;

			case KEYDN:
				me.changeHighlight('down');
				return false;
		}
		
	};
	
	//Rotina no KEYUP (pegar input)
	this.elem.onkeyup = function(ev) 
	{
	  
		var key = infraGetCodigoTecla(ev);
		
		switch(key)
		{
		//The control keys were already handled by onkeydown, so do nothing.
		case TAB:
		    //evita que ao passar com o TAB perca os dados
        me.elem.value = me.elem.value;  
    break;		
		
		case ESC:
		case KEYUP:
		case KEYDN:
	  case HOME:
	  case END:
	  case SHIFT:
			return;
			
		case ENTER:
		
			if (me.highlighted==null){
			  if (!me.procurar()){
  		    if (!me.bolExecucaoAutomatica && me.elem.value.length >= me.tamanhoMinimo){
            me.executar();
            me.procurar();
  			  }
    	  }
			}else{
			  me.escolher(me.highlighted.indice);
			}
			
      me.hideDiv();
			
			return false;
			
		default:
		 
		  //limpa tudo menos campo texto
		  me.limpar(false);
			
			//Verificar tamanho mínimo
			if (me.bolExecucaoAutomatica && me.elem.value.length >= me.tamanhoMinimo){
        me.executar();
			}	else{
 			  me.hideDiv();
				return false;	
			}
			//Remover elementos highlighted
			me.highlighted = null;
		}
	};
	
	this.procurar = function(){
	  for(var i=0;i<me.arrItens.length;i++){
	    if (infraRetirarAcentos(me.arrItens[i]['descricao'].toUpperCase())==infraRetirarAcentos(me.elem.value.toUpperCase())){
	      me.escolher(i);
	      me.highlighted = null;
	      return true;
	    }
	  }
	  return false;
	}
	
	this.iniciarExecucao = function(){}
	this.processarErro = function(){}
	this.finalizarExecucao = function(){}
	this.verificarExecucao = function(){return me.executou;}
	
	this.executar = function(){
	   infraAjaxPost(me);
	}
	
	//Sumir com autosuggest
	this.elem.onblur = function() {
	  if (me.hdn.value==''){
	    //limpa tudo
	    me.limpar();
	  }
		me.hideDiv();
	}
	
	//Ajax return function
	this.processarAjax = function(){
	  
	  //xml = infraAjaxProcessarXML(me.ajaxReq,me.processarErro);
	  xml = infraAjaxProcessarXML(me);
	  
	  if (typeof(xml) == 'object'){
	    
			//me.showDiv();
			
			//var itens = xml.getElementsByTagName('item');
			var itens = infraAjaxGetElementsByTagName(xml,'item');
			var itCnt = itens.length;
	
			//Pegar primeiro filho
			me.div.innerHTML = '';
			var ul = document.createElement('ul');
			me.div.appendChild(ul);
			
			if (itCnt > 0){
			  
			  if (itCnt==1){
			    if (itens[0].getAttribute("id")==me.hdn.value){
			      return;
			    }
			  }
			  
			  me.showDiv();  

			  var arrGrupo = new Array();
				var tam = null;
			  var a = null;
			  var li = null;
			  var fmtGrupoIni = null;
			  var fmtGrupoFim = null;
			  var fmtIdentacao = null;
			  
				for (i=0; i<itCnt; i++){

				  fmtGrupoIni = '';
			    fmtGrupoFim = '';
			    fmtIdentacao = '';
				  
				  
				  var descricao = itens[i].getAttribute('descricao');
				  var complemento = itens[i].getAttribute('complemento');
				  var grupo = itens[i].getAttribute('grupo');
				  
				  if (me.maiusculas){
  				  descricao = descricao.toUpperCase();
  				  
  				  if (complemento!=null){
  				    complemento = complemento.toUpperCase();
  				  }
  				  
  				  if (grupo!=null){
  				    grupo = grupo.toUpperCase();
  				  }
				  }
					
					tam = me.arrItens.length;
				  li = document.createElement('li');
				  
				  
					//lendo grupo
					if (grupo!=null && arrGrupo[grupo]==undefined){
					  
					  arrGrupo[grupo] = grupo;
					  
				    if (me.permitirSelecaoGrupo){
   					  li.indice = tam;
    					me.arrItens[tam] = new Array();
    					me.arrItens[tam]['id'] = itens[i].getAttribute("id");
    					me.arrItens[tam]['descricao'] = descricao;
    					me.arrItens[tam]['complemento'] = complemento;
    					me.arrItens[tam]['grupo'] = grupo;
    					
    					li.onmouseover = function(){  this.className = 'selected'; /*me.highlightThis(this,'y') */ }
    					li.onmouseout  = function(){  this.className = '';  /*me.highlightThis(this,'n') */}
    					li.onmousedown = function() {
    						me.escolher(this.indice);
    						me.hideDiv();
    						return false;
    					}
    					
					  }
					  
					  a = document.createElement('a');
  					a.href = '#';
  					a.onclick = function() { return false; }
  					
  					fmtGrupoIni = '<span class="infraAjaxAutoCompletarGrupo">';
  					fmtGrupoFim = '</span>';
  					
					}else{
						
  					if (me.permitirSelecaoFilho){
    					li.indice = tam;
    					
    					me.arrItens[tam] = new Array();
    					me.arrItens[tam]['id'] = itens[i].getAttribute("id");
    					me.arrItens[tam]['descricao'] = descricao;
    					me.arrItens[tam]['complemento'] = complemento;
    					me.arrItens[tam]['grupo'] = grupo;
    					
    					li.onmouseover = function(){ this.className = 'selected'; /*me.highlightThis(this,'y')*/}
    					li.onmouseout  = function(){ this.className = '';  /*me.highlightThis(this,'n')*/}
    					li.onmousedown = function() {
    						me.escolher(this.indice);
    						me.hideDiv();
    						return false;
    					}
  					}
  					  					
  					a = document.createElement('a');
  					a.href = '#';
  					a.onclick = function() { return false; }
  
  					var tab = '';
  					if (grupo!=null){
  					  fmtIdentacao = '<pre style="display:inline">    </pre>';
  					}
  					
					}

					if(complemento != null){
					  a.innerHTML = unescape(descricao) + ' - ' + unescape(complemento);
					}else{
					  a.innerHTML = unescape(descricao);
					}
					
					//digitou algo e o item é selecionável
					if (me.elem.value.length > 0 && li.indice != undefined){
					  
					  var palavras = infraRetirarAcentos(me.elem.value).toUpperCase().split(' ');
					  
					  for(var j=0;j<palavras.length;j++){
  					  
					    var valor = palavras[j];
					    
					    if (infraTrim(valor)!=''){
  					  
    					  var pos = infraRetirarAcentos(a.innerHTML.toUpperCase()).indexOf(valor);
    					  
    					  if (pos > -1){
    					    a.innerHTML = a.innerHTML.substr(0,pos) + '<b>' + a.innerHTML.substr(pos,valor.length) + '</b>' + a.innerHTML.substr(pos+valor.length);
    					  }
					    }
					  }
					}
					
					a.innerHTML = fmtGrupoIni + fmtIdentacao + a.innerHTML + fmtGrupoFim;
					a.innerTEXT = unescape(descricao);
					li.appendChild(a);
					ul.appendChild(li);	
					
					a = null;
					li = null;
				}
			}else{
				me.hideDiv();	
			}
			xml = null;
			itens = null;
			ul = null;
		}
	}
	
	this.escolher = function (indice){
	  me.selecionar(unescape(me.arrItens[indice]['id']),unescape(me.arrItens[indice]['descricao']),unescape(me.arrItens[indice]['complemento']));
		me.hideDiv();
	}

	this.selecionar = function(id,descricao,complemento){
	  if (complemento==undefined){
	    complemento = null;
	  }

	  if (id!='' && descricao!=''){
  	  me.hdn.value = id;
  	  me.elem.value = descricao;
  		if (me.mostrarImagemVerificado){
  		  var offset = 0;
  		  if (me.mostrarImagemAjuda){
  		    offset = 25;
  		  }else{
  		    offset = 5;
  		  }
  	    infraAjaxMostrarImg(me.elem,me.img,me.offsetX + offset,me.offsetY);
  		}
	  }
	  
 		if (me.processarResultado!=null){
		  me.processarResultado(id,descricao,complemento);
		}
		
	}
	
	this.limpar = function(bolTexto){
 	  me.hdn.value = '';
 	  if (bolTexto==undefined || bolTexto==true){
 	    if (me.limparCampo){
 	      me.elem.value='';
 	    }
 	  }
 	  infraAjaxOcultarImg(me.img);
 	  if (me.processarResultado!=null){
 	    me.processarResultado('','','');
 	  }
	}
	
	this.posicionar = function()
	{
		var el = me.elem;
		var x = me.offsetX;
		var y = me.offsetY + el.offsetHeight;
		

		//Walk up the DOM and add up all of the offset positions.
		while (el.offsetParent && el.tagName.toUpperCase() != 'BODY')
		{
			x += el.offsetLeft;
			y += el.offsetTop;
			el = el.offsetParent;
		}

		x += el.offsetLeft;
		y += el.offsetTop;
		
		me.div.style.left = x + 'px';
		me.div.style.top = y + 'px'; 

		el = null;
	};

	this.hideDiv = function(){
		me.highlighted = null;
		me.div.style.display = 'none';
		me.handleSelects('');
	}

	this.showDiv = function(){
		me.highlighted = null;
		me.posicionar();
		me.handleSelects('none');
		me.div.style.display = 'block';
		me.div.style.zIndex = 1000;
		
	}
	
	this.handleSelects = function(state){
	  if (INFRA_IE > 0 && INFRA_IE < 7){
  		var selects	= document.getElementsByTagName('SELECT');
  		for (var i = 0; i < selects.length; i++) {
        selects[i].style.display = state;
      }
      selects = null;
	  }
	}
	
  if (window.attachEvent) { //Limpar as referências do IE
    window.attachEvent("onunload", function(){
      me.hdn = null;
      me.elem = null;
   	  me.highlighted = null;
      me.arrItens = null;
      me.ajaxReq = null;
  	  me.div = null;
  	  me.img = null;
  	  me = null;
    });
  }
  
}

function infraAjaxMontarSelectDependente(selPai,selFilho,url){
  var me = this;
  this.objSelPai = infraGetElementById(selPai);
  this.objSelFilho = infraGetElementById(selFilho);
  this.ajaxTarget = url;
	this.prepararExecucao = null;
	this.processarResultado = null;
	this.mostrarAviso = false;
	this.tempoAviso = 0;
	this.executou = false;
	

  
  this.ajaxReq = infraAjaxCriarRequest();  
  
  /*
  this.objSelPai.onchange = function(){
    me.executar();
  }
  */
  
	this.iniciarExecucao = function(){}
	this.processarErro = function(){}
	this.finalizarExecucao = function(){}  
	this.verificarExecucao = function(){return me.executou;}
	
	
	this.executar = function(){
	   infraSelectLimpar(me.objSelFilho);    
	   infraAjaxPost(me);
	}
	
	infraAdicionarEvento(this.objSelPai,"change",this.executar);
  
  this.processarAjax = function(){
    
    //xml = infraAjaxProcessarXML(me.ajaxReq,me.processarErro);
    xml = infraAjaxProcessarXML(me);
    
    if (typeof(xml) == 'object'){
  
      var itens = infraAjaxGetElementsByTagName(xml,'option');
      //var itens = xml.getElementsByTagName('option'); 
      var itCnt = itens.length;
      
      //infraSelectLimpar(me.objSelFilho);    
      
    	if (itCnt > 0){
    		for (i=0; i<itCnt; i++){				    
    		  if (itens[i].firstChild!=null){
    		    id = itens[i].getAttribute("value");
    		    texto = itens[i].firstChild.nodeValue;
    		    //No IE tags com valor em branco retornam firstChild nulo
    		    //Na InfraAjax.php as tags com branco foram substituitas por %20
    		    if (texto=='%20'){
    		      texto = ' ';
    		    }
      			infraSelectAdicionarOption(me.objSelFilho,texto,id);
      			if(itens[i].getAttribute("selected")=="selected"){
      			   infraSelectSelecionarItem(me.objSelFilho,id);
      			}
    		  }
    		}
    	}
    	itens = null;
    	xml = null;
			
    	if (me.processarResultado!=null){
    	  me.processarResultado();
    	}
	  }
  }  
  
  if (window.attachEvent) { //Limpar as referências do IE
    window.attachEvent("onunload", function(){
      me.objSelPai = null;
      me.objSelFilho = null;
      me.ajaxReq = null;
      me = null;
    });
  }
}

function infraAjaxMontarSelect(sel,url){
  var me = this;
  this.objSel = infraGetElementById(sel);
  this.ajaxTarget = url;
	this.prepararExecucao = null;
	this.processarResultado = null;
  this.ajaxReq = infraAjaxCriarRequest();  
  this.mostrarAviso = false;
  this.tempoAviso = 0;
  this.executou = false;
  this.limparSelect = true;

	this.iniciarExecucao = function(){}
	this.processarErro = function(){}
	this.finalizarExecucao = function(){}  
	this.verificarExecucao = function(){return me.executou;}
	
	
	this.executar = function(){
	   if (me.limparSelect){
       infraSelectLimpar(me.objSel);    
	   }
     infraAjaxPost(me);
	}
  
  
  this.processarAjax = function(){
    var i,j;
    
    //var xml = infraAjaxProcessarXML(me.ajaxReq,me.processarErro);
    xml = infraAjaxProcessarXML(me);
    
    if (typeof(xml) == 'object'){    
      var itens = infraAjaxGetElementsByTagName(xml,'option');
      //var itens = xml.getElementsByTagName('option'); 
      var itCnt = itens.length;
      //infraSelectLimpar(me.objSelFilho);    
    	if (itCnt > 0){
    		for (i=0; i<itCnt; i++){				    
    		  if (itens[i].firstChild!=null){
    		    id = itens[i].getAttribute("value");
    		    texto = itens[i].firstChild.nodeValue;
    		    
    		    //No IE tags com valor em branco retornam firstChild nulo
    		    //Na InfraAjax.php as tags com branco foram substituitas por %20
    		    if (texto=='%20'){
    		      texto = ' ';
    		    }
          
    		    //Somente adiciona se não existe um item com o mesmo id
            for (j=0; j<me.objSel.length; j++) {
              if (me.objSel.options[j].value == id) {
                break;
              }
            }
    		    
            if (j == me.objSel.length){
        			infraSelectAdicionarOption(me.objSel,texto,id);
        			if(itens[i].getAttribute("selected")=="selected"){
        			   infraSelectSelecionarItem(me.objSel,id);
        			}
            }
    		  }
    		}
    	}
    	itens = null;
    	xml = null;
    	
    	if (me.processarResultado!=null){
    	  me.processarResultado(itCnt);
    	}
	  }
  }  
 
  if (window.attachEvent) { //Limpar as referências do IE
    window.attachEvent("onunload", function(){
      me.objSel = null;
      me.ajaxReq = null;
      me = null;
    });
  }
}

/////////////////////////////////////////////////

function infraAjaxComplementar(obj, url){
  
	var me = this;
	this.prepararExecucao = null;
	this.processarResultado = null;
	this.complementos = null;
	this.ajaxTarget = url;
	this.mostrarImagemVerificado = true;
	this.mostrarAviso = false;
	this.tempoAviso = 0;
	this.executou = false;
	this.limparCampo = true;
	this.ultimaExecucao = null;
	this.tamanhoMinimo = 1;
	this.offsetX = 0;
	this.offsetY = 0;
  
	//Crate AJAX Request
	this.ajaxReq = infraAjaxCriarRequest();
	
	if (obj==null){
	  this.elem = null;
	}else{
	  this.elem = infraGetElementById(obj);
	  
    this.img = infraAjaxImagemVerificado(me.elem);	
    
  	//Keycodes que devem ser monitorados
  	var TAB = 9;
  	var ESC = 27;
  	var KEYUP = 38;
  	var KEYDN = 40;
  	var KEYLEFT = 37;
  	var KEYRIGHT = 39;
  	var ENTER = 13;
  	var KEY_V = 86
  	
  	
  	//Desabilitar autocomplete IE
  	
  	if (this.elem.type=='text'){
  	  this.elem.setAttribute("autocomplete","off");
  	}
	
  	//Rotina no KEYUP (pegar input)
  	//this.elem.onkeyup = function(ev) 
  	if (this.elem.type == 'text'){
  	
    	this.elem.onkeydown = function(ev) 
    	{
        me.executou = false;
    	  
    		var key = infraGetCodigoTecla(ev);
    
    		switch(key)
    		{
    		case ESC:
    		case KEYUP:
    		case KEYDN:
    		case KEYLEFT:
    		case KEYRIGHT:
    			return;
    			
    		case ENTER:
    		case TAB:
    		/*
    		case KEY_V:
    
    		
    		  if (key==KEY_V){
    		    if (INFRA_IE > 0){
    		      if (!window.event.ctrlKey){
    		        return true;
    		      }
    		    }else{
    		      return true;
    		    }
    		  }
    		*/
    		  if (me.elem.value.length < me.tamanhoMinimo){
    		    return true;
    		  }
    		  
    		  var novaExecucao = null;
      	  if (typeof(me.prepararExecucao)=='function'){
        	  novaExecucao = me.prepararExecucao();
        	  if (!novaExecucao || novaExecucao == me.ultimaExecucao){
        	    if (key==TAB){ 
        	      return true;
        	    }else{
        	      return false;
        	    }
        	  }
      	  }
          
    	    //Limpa tudo menos texto
    		  me.limpar(false);
    		   
    			//Verificar tamanho mínimo
    			if (me.elem.value.length >= 1){
    			  me.executar();
    			}
    			
    			me.ultimaExecucao = novaExecucao;
    			
    			if (key==TAB) {
    			  return true;
    			}else{
    			  return false;
    			}
    			
    			break;
    			
    		default:
    		  //limpa tudo menos texto
      	  me.limpar(false);
    	  }
    	};
    }else{
      
    	this.mudouValor = function(){
    	  
        me.executou = false;
    	  var novaExecucao = null;
    	  if (typeof(me.prepararExecucao)=='function'){
      	  novaExecucao = me.prepararExecucao();
      	  if (!novaExecucao || novaExecucao == me.ultimaExecucao){
      	    return;
      	  }
    	  }
    	  me.limpar(false);
    	  me.executar();
    		me.ultimaExecucao = novaExecucao;
    	}
      
    	infraAdicionarEvento(this.elem,"change",this.mudouValor);
    }
	}
	
	this.iniciarExecucao = function(){}
	this.processarErro = function(){}
	this.finalizarExecucao = function(){}
	this.verificarExecucao = function(){return me.executou;}
	
	this.executar = function(){
    infraAjaxPost(me);
	}
	/*
	if (this.elem.type=='text'){
  	this.elem.onblur = function() {
  	  if (me.complementos==null){
  	    //limpa tudo
  	    me.limpar();
  	  }
  	}
  }
	*/
	//Ajax return function
	this.processarAjax = function(){
	  
	  //xml = infraAjaxProcessarXML(me.ajaxReq,me.processarErro);
	  xml = infraAjaxProcessarXML(me);
	  
    if (typeof(xml) == 'object'){	  
      
      me.complementos = null;
      var itens = infraAjaxGetElementsByTagName(xml,'complemento');
	    //var itens = xml.getElementsByTagName('complemento');
	    var itCnt = itens.length;
	    if (itCnt > 0){
	      me.complementos = new Array();
		    for (i=0; i<itCnt; i++){
		      if (itens[i].firstChild!=null){
		        me.complementos[itens[i].getAttribute("nome")]=infraRemoverFormatacaoXML(itens[i].firstChild.nodeValue);
		      }
		    }
	    }
		  me.selecionar(me.complementos);
			xml = null;
		}
	}
	
	this.limpar = function(bolTexto){
	  
	  if (bolTexto==undefined || bolTexto==true){
      if (me.limparCampo && me.elem != null){
 	      me.elem.value='';
 	    }
	  }
	  me.complementos = null;
 	  infraAjaxOcultarImg(me.img);
 	  me.ultimaExecucao = null;
 	  if (typeof(me.processarResultado)=='function'){
 	    me.processarResultado(null,false);
 	  }
	}
	
	this.selecionar = function(complementos){
	  if (complementos!=null){
	    me.complementos = complementos;
  		if (me.mostrarImagemVerificado){
		    var offset = 0;
  		  if (me.mostrarImagemAjuda){
  		    offset = 25;
  		  }else{
  		    offset = 5;
  		  }
  		  if (me.elem!=null){
    		  infraAjaxMostrarImg(me.elem,me.img,me.offsetX + offset, me.offsetY);
  		  }
  		}
	  }
	  if (typeof(me.processarResultado)=='function'){
		  me.processarResultado(complementos,true);
		}
	}
	

  if (window.attachEvent) { //Limpar as referências do IE
    window.attachEvent("onunload", function(){
      me.elem = null;
   	  me.complementos = null;
      me.ajaxTarget = null;
      me.ajaxReq = null;
  	  me.img = null;
  	  me = null;
    });
  }
}

function infraAjaxMontarCheckboxGrupo(div,url){
	// div, elemento pai dos checkboxes
	var me = this;
	this.objDiv = infraGetElementById(div);
	this.ajaxTarget = url;
	this.prepararExecucao = null;
	this.processarResultado = null;
	this.ajaxReq = infraAjaxCriarRequest();  
	this.mostrarAviso = false;
	this.tempoAviso = 0;
	this.executou = false;
	this.limparCheckbox = true;
	this.async = false;
	
	this.iniciarExecucao = function(){}
	this.processarErro = function(){}
	this.finalizarExecucao = function(){}  
	this.verificarExecucao = function(){return me.executou;}

	this.executar = function(){
		if (me.limparCheckbox){
			infraCheckboxLimpar(me.objDiv);    
		}
		infraAjaxPost(me);
	}
	
	this.processarAjax = function(){
		
		var i,j;
	    
	    //var xml = infraAjaxProcessarXML(me.ajaxReq,me.processarErro);
	    xml = infraAjaxProcessarXML(me);
	    
		if (typeof(xml) == 'object'){
			
			var itens = infraAjaxGetElementsByTagName(xml,'item');
			var itCnt = itens.length;
			if (itCnt > 0){
				for (i=0; i<itCnt; i++){
					if (itens[i].firstChild!=null){
						// <item checked="checked" name="name" value="value">text</item> // modelo XML
						value = itens[i].getAttribute("value");
						name = itens[i].getAttribute("name");
						texto = itens[i].firstChild.nodeValue;
						//No IE tags com valor em branco retornam firstChild nulo
						//Na InfraAjax.php as tags com branco foram substituitas por %20
						if (texto=='%20'){
							texto = ' ';
						}
						
						infraCheckboxAdicionarItem(me.objDiv,texto,value, name);
						if(itens[i].getAttribute("checked")=="checked"){
							infraCheckboxSelecionarItem(me.objDiv,value);
						}
					}
				}
			}
			itens = null;
			xml = null;
			
			if (me.processarResultado!=null){
				me.processarResultado(itCnt);
			}
			
		}
		
	}  
 
	if (window.attachEvent) { //Limpar as referências do IE
		window.attachEvent("onunload", function(){
			me.objDiv = null;
			me.ajaxReq = null;
			me = null;
		});
	}
}

function infraCheckboxLimpar(obj){
	area = infraGetElementById(obj);
	area.innerHTML = '';
}

function infraCheckboxAdicionarItem(obj, text, value, name){
	area = infraGetElementById(obj);
	div = document.createElement('div');
	
	input = document.createElement('input');
	input.type = "checkbox";
	input.className = "infraCheckbox";
	input.name = name;
	input.value = value;
	
	label = document.createElement('label');
	label.className = "infraLabelCheckbox";
	label.setAttribute('for', name);
	label.innerHTML = text;
	
	div.appendChild(input);
	div.appendChild(label);
	area.appendChild(div);
}

function infraCheckboxSelecionarItem(obj,valor){
  var checks = infraGetElementById(obj).getElementsByTagName('input');
  for (var i=0; i<checks.length; i++) {
    if (checks[i].value == valor) {
      checks[i].checked = 'checked';
    }
  }
}

function infraAjaxMontarPostPadraoCheckbox(valorItensSelecionados){
	// valores dos checkboxes selecionados separados por vírgula (,)
	var post = '';
	if (valorItensSelecionados) {
		post += 'valorItensSelecionados='+valorItensSelecionados;  
	}
	return post;
}

function infraCheckboxSelecionado(checkbox){
  objInput = document.getElementsByTagName('input');
  selecionados = 0;
  if (objInput.length > 0) {
	for (i = 0; i < objInput.length; i++) {
		if (objInput[i].type == 'checkbox' && objInput[i].name == checkbox) {
			if (objInput[i].checked) selecionados++;
		}
	}
  }
  if (selecionados > 0) return true;
  return false;
}

