/***********************************************************************************
Autor: Bruno Estevão MOnteiro Guida
Data : 29/11/2006
Função: Criar e remover options dentro de objetos Select
***********************************************************************************/
function ComboBoxObject(){
	this.path        ="/library/visualobjects/ComboBoxObject";
	this.loadingImage=this.path+"/images/loading_small2.gif";
	this.libraryPath =this.path+"/library/library.xml";
	this.oLIBXML=null;
	//--
	this.name        ="ComboBox";
	this.AJAX        ="AJAX_"+this.name;
	this.oAJAX_SCRIPT=null;
	this.oAJAX       =null;
	this.LIB         ="LIB_"+this.name;
	this.oLIB_SCRIPT=null;
	//--
	this.doInit();
};
ComboBoxObject.prototype.doInit=function(){
	this.loadAjaxScript();
	//this.loadAjaxObject();
	this.loadLibrary();
};
ComboBoxObject.prototype.loadLibrary=function(){
	var AjaxFunction = typeof(AJAX)+"";
	if(AjaxFunction=="null" || AjaxFunction=="undefined" || AjaxFunction==""){
		setTimeout(this.name+".loadLibrary()", 200);
		return false;
	};
	var oComboBox   =this;
	var oAJAX       =new AJAX();
	oAJAX.onLoad    =function(STRING){ oComboBox.oLIBXML=STRING; };
	oAJAX.onError   =function(STRING){ alert("Ocorreu erro ao tentar carregar as bibliotecas!"); };
	oAJAX.returnType="xml";
	oAJAX.open(this.libraryPath);
};
ComboBoxObject.prototype.loadAjaxObject=function(){
	if(AJAX==null || (AJAX+"")=="undefined"){
		setTimeout(this.name+".loadAjaxObject()", 200);
		return false;
	};
	this.oAJAX=new AJAX();
};
ComboBoxObject.prototype.loadAjaxScript=function(){
	if(window.document.body==null){
		setTimeout(this.name+".loadAjaxScript()", 200);
		return false;
	};
	if(this.oAJAX_SCRIPT==null){
		this.oAJAX_SCRIPT         =document.createElement("script");
		this.oAJAX_SCRIPT.id      =this.AJAX;
		this.oAJAX_SCRIPT.language="JavaScript";
		this.oAJAX_SCRIPT.src     =this.path+"/AJAX/AJAX.js";
		document.body.insertAdjacentElement("beforeEnd",this.oAJAX_SCRIPT);
	};
};
ComboBoxObject.prototype.loadingIcon=function(object){
	var oLoadingIcon=document.getElementById(object.getAttribute("LoadingIcon"));
	if(oLoadingIcon==null){
		var oLoadingIcon=document.createElement("div");
		oLoadingIcon.id="LoadingIcon_"+object.name;
		oLoadingIcon.style.font="normal 10 verdana";
		oLoadingIcon.innerHTML="<img src='"+this.loadingImage+"' border=0 align='middle' hspace='2'>Carregando";
		oLoadingIcon.style.position="absolute";
		oLoadingIcon.style.display="none";
		object.insertAdjacentElement("afterEnd", oLoadingIcon);
		object.setAttribute("LoadingIcon", oLoadingIcon.id);
	};

	oLoadingIcon.style.left=(object.offsetLeft+object.offsetWidth+2);
	oLoadingIcon.style.top =object.offsetTop;
	oLoadingIcon.style.display="";
};
ComboBoxObject.prototype.closeLoadingIcon=function(object){
	var oLoadingIcon=document.getElementById(object.getAttribute("LoadingIcon"));
	if(oLoadingIcon){
		oLoadingIcon.style.display="none";
	};
};
ComboBoxObject.prototype.loadOptions=function(params){

	params.object=(""+params.object).trim();
	params.url=(""+params.url).trim();
	params.library=(""+params.library).trim();

	if(params.object=="" || params.object=="undefined" || params.object=="null"){
		alert("Informe o objeto ComboBox(select) na propriedade object!\n\nExemplo: ComboBox.loadOptions({object:'ID do Objeto', url:'Url'});");
		return false;
	};
	var oObject=document.getElementById(params.object);
	if(oObject==null){
		alert("O objeto ComboBox(select) \""+params.object+"\" definido na propriedade object não foi encontrado!");
		return false;
	};

	if(params.url=="" || params.url=="undefined" || params.url=="null"){
		params.url="";
	};
	if(params.library=="" || params.library=="undefined" || params.library=="null"){
		params.library="";
	};

	if(this.oLIBXML==null){
		var paramsAux="";
		paramsAux+="object:'"+oObject.id+"'";
		paramsAux+=", url:'"+params.url+"'";
		paramsAux+=", library:'"+params.library+"'";
		setTimeout(this.name+".loadOptions({"+paramsAux+"})",200);
		return false;
	};

	if(params.url=="" && params.library==""){
		var Confirm=window.confirm("Informe a biblioteca a ser executada na propriedade 'library' ou a URL na propriedade 'url' no Objeto ComboBox.loadOptions!\n\nExemplo:\n- ComboBox.loadOptions({object:Seu_Combo_Box, library:'Nome da Biblioteca'});\n- ComboBox.loadOptions({object:Seu_Combo_Box, url:'Url'});\n\nGostaria de Abrir a lista de Bibliotecas existentes?");
		if(Confirm) window.open(this.path+"/library/library_list.asp");
		return false;
	};
	
	if(params.library!=""){
		var arrLib=params.library.split("?");
		params.url=this.oLIBXML.getElementsByTagName(arrLib[0])[0].getElementsByTagName("url")[0].firstChild.nodeValue;
		if(params.url==""){
			var Confirm=window.confirm("Biblioteca '"+params.library+"' inválida!\n\n Gostaria de Abrir a lista de Bibliotecas existentes?");
			if(Confirm) window.open(this.path+"/library/library_list.asp");
			return false;
		}
		if(arrLib.length>1)params.url+="?"+arrLib[1];
	} 

	this.loadingIcon(oObject);
	var oComboBox   =this;
	var oAJAX       =new AJAX();
	oAJAX.onLoad    =function(STRING){ oComboBox.execLoadOptions(STRING, oObject); };
	oAJAX.onError   =function(STRING){ oComboBox.closeLoadingIcon(oObject); alert("Ocorreu erro ao tentar carregar os options do ComboBox "+oObject.id+"!"); };
	oAJAX.returnType="xml";
	oAJAX.open(params.url);
};
ComboBoxObject.prototype.execLoadOptions=function(STRING, object){
	var option=STRING.getElementsByTagName("option");
	for(var i=0;i<option.length;i++){
		this.insertOption(object, option[i].getAttribute("value"), option[i].firstChild.nodeValue);
	};
	this.closeLoadingIcon(object);
};
ComboBoxObject.prototype.insertOption=function(vObject, vValue, vText, vBeforeAfter){  
   	  if(typeof(vObject) != "object") return false;
	  var IE = (navigator.appName.toUpperCase() == "MICROSOFT INTERNET EXPLORER");
	  var re = / /g;
	  vBeforeAfter = vBeforeAfter + "";
	  vBeforeAfter = vBeforeAfter.toUpperCase();
	  vBeforeAfter = vBeforeAfter.replace(re, "");
	  
	  var vOption   = document.createElement("OPTION");
	  vOption.value = vValue;
	  vOption.text  = vText; 
			
	  if(vBeforeAfter=="BEFORE"){ 
	     if(IE){
			 //-- Item a ser Inserido
			 var vOption   = new Array(vObject.options.length+1);
			 vOption[0]    = document.createElement("OPTION");
			 vOption[0].value = vValue;
			 vOption[0].text  = vText;
			 //-- Guarda os Valores Existentes no Combo  
			 for(i=0;i<vObject.options.length;i++) {
				  vOption[i+1]       = document.createElement("OPTION");
				  vOption[i+1].value = vObject.options[i].value;
				  vOption[i+1].text  = vObject.options[i].text; 
			 };
			 //-- Remove todos os Valores do Combo
			 SelectRemoveOptions(vObject);
			 //-- Adiciona todos os Valores
			  for(i=0;i<vOption.length;i++) vObject.add(vOption[i]);
		 }
		 //-- MOZILA
		 else vObject.insertAdjacentElement("afterBegin",vOption);
	  }else{
			if(IE) vObject.add(vOption);
			//-- Mozila
			else vObject.insertAdjacentElement("beforeEnd",vOption);
	  };
};
ComboBoxObject.prototype.removeOption=function(vObject, OptionIndex){
  if(typeof(vObject) != "object") return false;
  OptionIndex+="";
  //-- Deleta Todos
  if(OptionIndex=="" || OptionIndex=="undefined"){
  	while (vObject.options.length > 0)
  	    vObject.remove(0);
  }else if(isNaN(OptionIndex) == false) vObject.remove(OptionIndex);
};
ComboBoxObject.prototype.selectValue=function(vObject, vValue){  
      var i;
	  if(typeof(vObject) != "object") return false;
	  vValue = vValue.toUpperCase();
	  for(i=0;i<vObject.options.length;i++){
		 var vCurrentValue = vObject.options[i].value.toUpperCase(); 
		 //alert(vCurrentValue+"=="+ vValue); 
		 if(vCurrentValue== vValue){
		   vObject.selectedIndex = i;
		   break;
		 };
	  };
};
ComboBoxObject.prototype.selectText=function(vObject, vText){  
      var i;
	  if(typeof(vObject) != "object") return false;
	  vText = vText.toUpperCase();
	  for(i=0;i<vObject.options.length;i++){
		 var vCurrentText = vObject.options[i].text.toUpperCase(); 
		 if(vCurrentText== vText){
		   vObject.selectedIndex = i;
		   break;
		 };
	  };
};

//-- Prototipa o trim do vbScript
if(!String.prototype.trim) String.prototype.trim=function(){ return this.replace(/(^\s*)|(\s*$)/g, "");}

if(typeof HTMLElement!="undefined" && ! HTMLElement.prototype.insertAdjacentElement){
	HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode){
		switch (where){
		case 'beforeBegin': this.parentNode.insertBefore(parsedNode,this); break;
		case 'afterBegin':	this.insertBefore(parsedNode,this.firstChild);	break;
		case 'beforeEnd':	this.appendChild(parsedNode); break;
		case 'afterEnd':
			if (this.nextSibling) this.parentNode.insertBefore(parsedNode,this.nextSibling);
			else this.parentNode.appendChild(parsedNode);
			break;
		};
	};
	HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr){
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML)
	};
	HTMLElement.prototype.insertAdjacentText = function(where,txtStr){
		var parsedText = document.createTextNode(txtStr)
		this.insertAdjacentElement(where,parsedText)
	};
};
//-------------------------------------------------------------------------------
//--Inicialização
var ComboBox=new ComboBoxObject();