/*******************************************************************************
* Software: JSAjaxSelect                                                       *
* Version:  1.0.1                                                              *
* Date:     2007-06-18                                                         *
* Author:   Fernando Vargas L.                                                 *
* License:  Freeware                                                           *
*                                                                              *
* You may use and modify this software as you wish.                            *
*******************************************************************************/

function JSAjaxSelect(){
	this.parentNoData	= false;
	this.isSubordinate	= false;
	this.isdisabled		= false;
	this.hasSubordinate = false;
	this.hasOtherOption = false;
	
	this.selected		= null;
	
	this.url;
	this.vars;
	
	this.parent;
	this.selectedIndex;
	this.subordinate;
	this.otherChangeFunction;

	this.cmb = document.createElement("select");
	
	
	this.cmb.style.width = "150px";
	this.cmb.disabled = true;
	this.cmb.parentClass = this;
	this.cmb.onchange = function(){
		if(this.parentClass.onChange){
			this.parentClass.onChange();
		}		
	};
}

JSAjaxSelect.prototype.populate = function(_url,_vars){
	this.url = _url;
	this.vars = _vars;
	
	var globalAjax = this;
	
	if(this.ajax && this.ajax.readyState() < 4){ this.ajax.abort(); }
	
	this.cmb.options.length = 0;
	
	this.ajax = new Ajax();
	this.ajax.open("POST",_url, true);
	this.ajax.setParentClass(this);
	this.ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	this.ajax.send(_vars);	
	this.ajax.onreadystatechange(function(){	   	
		if(globalAjax.ajax.readyState() < 4){			
			/*this.option = document.createElement("option");
			this.option.appendChild(document.createTextNode("Cargando..."));
			
			globalAjax.ajax.parentClass.cmb.options.length = 0;
			globalAjax.ajax.parentClass.cmb.appendChild(this.option);*/			
		}else if (globalAjax.ajax.readyState() == 4){ 			
			if(globalAjax.ajax.status() == 200){
				opciones = eval(globalAjax.ajax.responseText());
				
				if(globalAjax.ajax.responseText() != ""){					
					globalAjax.ajax.parentClass.cmb.options.length = 0;
					
					if(opciones.length>0){
						
						for(var i=0;i<opciones.length;i++){
							option = document.createElement("option");
							option.setAttribute("value",opciones[i][0]);
							option.appendChild(document.createTextNode(opciones[i][1]));
							globalAjax.ajax.parentClass.cmb.appendChild(option);
						}
						
						if(globalAjax.ajax.parentClass.hasOtherOption){
							option = document.createElement("option");
							option.setAttribute("value",-2);
							option.appendChild(document.createTextNode("Otro..."));
							globalAjax.ajax.parentClass.cmb.appendChild(option);
						}
					}else{						
						option = document.createElement("option");
						
						if(globalAjax.ajax.parentClass.hasOtherOption){
							option = document.createElement("option");
							option.setAttribute("value",-2);
							option.appendChild(document.createTextNode("Otro..."));
							globalAjax.ajax.parentClass.cmb.appendChild(option);
						}
					}
				}else{
					globalAjax.ajax.parentClass.cmb.options.length = 0;
					
					option = document.createElement("option");
					option.setAttribute("value",-1);
					option.appendChild(document.createTextNode("No se optubieron datos..."));					
					globalAjax.ajax.parentClass.cmb.appendChild(option);
				}
				
				if(!globalAjax.ajax.parentClass.isDisabled){
					globalAjax.ajax.parentClass.cmb.disabled = false;
				}				
				
				if(globalAjax.ajax.parentClass.selected != null){
					if(navigator.appName.indexOf("Microsoft")==0){
						setTimeout(function(){globalAjax.ajax.parentClass.setIndex(globalAjax.ajax.parentClass.selected[0],
														     globalAjax.ajax.parentClass.selected[1])},1000);
					}else{
						globalAjax.ajax.parentClass.setIndex(globalAjax.ajax.parentClass.selected[0],
														     globalAjax.ajax.parentClass.selected[1]);
					}
				}
				
				if(globalAjax.ajax.parentClass.isSubordinate){
					globalAjax.ajax.parentClass.cmb.disabled = globalAjax.ajax.parentClass.parent.cmb.disabled;
					
					if(navigator.appName.indexOf("Microsoft")==0){						
						setTimeout(function(){globalAjax.ajax.parentClass.setIndex(globalAjax.ajax.parentClass.selectedIndex);},1000);
					}else{
						globalAjax.ajax.parentClass.setIndex(globalAjax.ajax.parentClass.selectedIndex);
					}
				}
				
				if(globalAjax.ajax.parentClass.onLoad){
					if(navigator.appName.indexOf("Microsoft")==0){
						setTimeout(function(){globalAjax.ajax.parentClass.onLoad();},1000);
					}else{
						globalAjax.ajax.parentClass.onLoad();
					}
				}
			}else if(globalAjax.ajax.status() == 404){
				alert("Error: No se encuentra el archivo.");
			}
		}
	});
}

JSAjaxSelect.prototype.disabled = function(_bool){
	this.isDisabled = _bool;
	this.cmb.disabled = _bool;	
}

JSAjaxSelect.prototype.reset = function(){
	this.cmb.selectedIndex = 0;
}

JSAjaxSelect.prototype.reload = function(){
	this.cmb.options.length = 0;
	this.populate(this.url,this.vars);
	this.cmb.selectedIndex = 0;
}

JSAjaxSelect.prototype.value = function(){
	return this.cmb.options[this.cmb.selectedIndex].value;
}

JSAjaxSelect.prototype.text = function(){
	return this.cmb.options[this.cmb.selectedIndex].text;
}

JSAjaxSelect.prototype.setParent = function(_parent,_url,_vars){	
	_parent.setSubordinate(this,_url,_vars);	
}

JSAjaxSelect.prototype.setSubordinate = function(_subordinate,_url,_vars){
	this.hasSubordinate = true;	
	this.subordinate 	= _subordinate;	
	this.subordinate.url 			= _url;
	this.subordinate.vars 			= _vars;
	this.subordinate.parent 		= this;
	this.subordinate.isSubordinate 	= true;
	
	var _parent = this.cmb;
	var div = this.div;
	if(!this.parentNoData){
		this.cmb.onchange = function(){
			optionValue = this.options[this.selectedIndex].value;
			if(optionValue != -1 & optionValue != -2){
				_subordinate.populate(_url,_vars+"&val="+optionValue);
				if(this.parentClass.hasOtherOption){
					_subordinate.div.style.display = "none";
					div.style.display = "none";
				}
				_subordinate.cmb.disabled = true;				
			}else if(optionValue == -1){
				if(this.parentClass.hasOtherOption){ 
					div.style.display = "none"; 
					_subordinate.div.style.display = "none";
				}
				
				_subordinate.cmb.disabled = true;			
			}
		};
	}
}

JSAjaxSelect.prototype.setIndex = function(_optionValue, _subValue){
	/*if(this.ajax && this.ajax.readyState() < 4){ 		
		this.selected = new Array(_optionValue, _subValue); 
	}else{*/
		if(!this.isSubordinate){
			for(var i=0; i < this.cmb.length; i++){
				if(this.cmb.options[i].value == _optionValue){
					this.cmb.selectedIndex = i;
					if(this.hasSubordinate){
						this.subordinate.selectedIndex = _subValue;
						this.subordinate.populate(this.subordinate.url,this.subordinate.vars+"&val="+_optionValue);						
					}					
					break;
				}
			}
		}else{
			for(var i=0; i < this.cmb.length; i++){
				newValue = this.cmb.options[i].value.split("|");
				if(newValue.length > 0){
					if(newValue[0] == _optionValue){
						this.cmb.selectedIndex = i;				
						break;
					}
				}else{
					if(this.cmb.options[i].value == _optionValue){
						this.cmb.selectedIndex = i;				
						break;
					}
				}
			}
		}
		
		this.selected = null;
	//}
}

JSAjaxSelect.prototype.setSelectedIndex = function(_optionValue, _subValue){
	this.selected = new Array(_optionValue, _subValue); 
}

JSAjaxSelect.prototype.onLoad;

JSAjaxSelect.prototype.onChange;

JSAjaxSelect.prototype.setOtherChangeFunction;

JSAjaxSelect.prototype.setId = function(_id){
	this.cmb.setAttribute("id",_id);
	this.cmb.setAttribute("name",_id);
}

JSAjaxSelect.prototype.setParentNoData = function(_bool){ this.parentNoData = _bool; }
JSAjaxSelect.prototype.append = function(_id){ 
	element = document.getElementById(_id);
	if(element){
		element.appendChild(this.cmb);		
	}else{ 
		alert("No se encuentra el contenedor.");
	}
}
JSAjaxSelect.prototype.get = function(){ return this.cmb; }