// Requisitos prototype + biblioteca


function LoadXML(url, prm, thr,msg,tipo,key,sel)
	{
	        this.url=url;
		this.tipo=tipo;
		this.key=key;
		this.sel =sel;
		this.msg =msg;
		this.prm =prm;		
                if (thr==undefined) thr=false;		
		var myAjax = new Ajax.Request(
			url, 
			{
				method: 'get', 
				parameters: prm, 
				asynchronous: thr,
                                responseType: 'XML',
				onComplete: mostraResposta
				
			});
	}

function mostraResposta(resposta,tipo){
            var XMLtags = resposta.responseXML.getElementsByTagName("li");
            if ((XMLtags.length==0)&&(msg!=""))  { alert (this.msg+" : "+this.prm); return; }
            var tagid= this.key;
            var tipo = this.tipo;
            var sel  = this.sel;
            var valor= XMLtags;
                        
            if (tipo!=undefined)
             {
               setCampos(valor,tagid,tipo,sel);
             } else
             {
              var l;
              for (l=0; l < XMLtags[0].childNodes.length; l++)
              {
              var data1 = XMLtags[0].childNodes[l];
              tagid   = data1.nodeName;               
              try {
                      valor = data1.firstChild.data;
                    } catch (e) { valor=""; };
              var obj=MM_findObj(tagid);  // Busca o objeto do formulario;
              if (obj==null)
              {
                 if (tagid!="#text") alert("Nao achou o objeto no formulario "+tagid+"  "+valor);
              } else 
                {
                tipo=obj.tagName; 
                //alert(tagid+tipo+data1);
                if (tipo==undefined) tipo="RADIO";  
                if (tipo=="INPUT") tipo=obj.type.toUpperCase();
                if (tipo=="SELECT") {  
                    try {
                    var xml=data1.getElementsByTagName("ls");
                    if (xml.length>0) {    setCampos(xml,tagid,"SELECT","");
                      }
                      else {
                         setSelect(obj,valor,tagid);
                      }
                     } catch (ee) {
                         setSelect(obj,valor,tagid);
                      }
                   }
                    
                if (tipo=="DIV") {  setDiv(obj,valor,tagid); } 
                if (tipo=="IMG") {  setImg(obj,valor,tagid); }
                if (tipo=="A") {  setLink(obj,valor,tagid); }
                if (tipo=="TEXT") {  setText(obj,valor,tagid); } 
                if (tipo=="HIDDEN") {  setText(obj,valor,tagid); }                 
                if (tipo=="TEXTAREA") {  setText(obj,valor,tagid); }
                if (tipo=="RADIO") {  setRadio(obj,valor,tagid); }
                if (tipo=="CHECKBOX") {  setCheckbox(obj,valor,tagid); }
                } 
              }
             }
            
}
               
function setCampos(XMLdados,campo,tipo,selecionado){
              var obj=MM_findObj(campo);
              if ((tipo!="RADIO")&&(obj==null)) alert("setCampos objeto nulo para "+objchamador);
              if (tipo="SELECT") clearSelect(obj);
              var texto1="";
              var valor1="";
              for (var i=0; i < XMLdados.length; i++)
              {
               texto1="";
               valor1="";
               try {
                 texto1   = XMLdados[i].getElementsByTagName("texto").item(0).firstChild.data;                
               } catch(ee) { texto1=""}
               try {
                 valor1   = XMLdados[i].getElementsByTagName("valor").item(0).firstChild.data;
                 if (texto1=="") texto1=valor1;
               }  catch(ee) {
                 valor1   = "valor SELECT erroXML";
               }
               
               if (tipo=="SELECT") 
                  {
                    try {
                        var novo = document.createElement("option");
                        novo.value = valor1;
                        novo.text  = texto1;
                        if ((valor1==selecionado) || ( (selecionado=="") && (i==0) ) ) { novo.selected=true; }
                        obj.options.add(novo);
                      } catch(ee) {
                        elemento=$(obj);
                        var opt = new Option(texto1,valor1);
                        elemento.appendChild(opt);
                      }
                  }
               else {
                    if (tipo=="DIV") {  setDiv(obj,valor,texto); } 
                    if (tipo=="IMG") {  setImg(obj,valor,tagid); }
                    if (tipo=="A") {  setLink(obj,valor,tagid); }
                    if (tipo=="TEXT") {  setText(obj,valor,texto); } 
                    if (tipo=="HIDDEN") {  setText(obj,valor,tagid); }                                     
                    if (tipo=="TEXTAREA") {  setText(obj,valor,texto); }
                    if (tipo=="RADIO") {  setRadio(obj,valor,texto); }
                    if (tipo=="CHECKBOX") {  setCheckbox(obj,valor,texto); }                                    
                  }
                
              }  
 }





