/*
 *
 * This file is part of the sfDependentSelect package.
 * (c) 2010 Sergio Flores <sercba@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 *
 */
var SelectDependiente=function(a){if(this.instancias[a.id] instanceof SelectDependiente){return this.instancias[a.id]}this.instancias[a.id]=this;defecto={id:"",opciones:{},dependiente:"",vacio:false,ajax:false,cache:true,url:"",params:{},varref:"_ref",varsoloref:"_solo_ref"};for(var b in defecto){this[b]=typeof a[b]==="undefined"?defecto[b]:a[b]}if(typeof this.dependiente==="string"&&this.dependiente.length>0){this.dependiente=new SelectDependiente({id:this.dependiente})}this.grupo="";this.limpio=false;this.fnsCambio=[];this.select=document.getElementById(this.id);this.iniciar();return this};SelectDependiente.prototype.instancias=[];SelectDependiente.prototype.iniciar=function(){var b=this;for(var d in this.params){if(this.params[d]===null){this.params[d]=undefined}}this.select.onchange=function(){b.cambio(b.select.options[b.select.selectedIndex].value)};if(typeof this.dependiente==="object"){this.dependiente.agregarFnCambio(function(e){b.mostrar(e)})}var a=false;for(var d=0;d<this.select.options.length;d++){var c=this.select.options[d];if(typeof this.opciones.html==="undefined"){this.opciones.html={}}if(c.value==""){this.vacio=c.text}else{this.opciones.html[c.value]=c.text}a=true}if(a===true){this.mostrar("html")}};SelectDependiente.prototype.agregarFnCambio=function(a){this.fnsCambio.push(a)};SelectDependiente.prototype.cambio=function(a){for(var b in this.fnsCambio){this.fnsCambio[b](a)}};SelectDependiente.prototype.agregarOpcion=function(d,a){var b=document.createElement("option");b.value=d;b.text=a;try{this.select.add(b,null)}catch(c){this.select.add(b)}this.limpio=false};SelectDependiente.prototype.buscarGrupoDesdeValor=function(c){var d=undefined;if(typeof this.dependiente==="object"&&this.ajax===true){this.params[this.varsoloref]=true;this.params[this.varref]=c;jQuery.ajax({url:this.url,data:this.params,type:"POST",async:false,dataType:"json",context:this,success:function(e){if(typeof e!=="undefined"){d=e}}})}else{for(var b in this.opciones){for(var a in this.opciones[b]){if(a==c){d=b}}}}return d};SelectDependiente.prototype.limpiar=function(a){var a=typeof a==="undefined"?false:a;var c=this.select.options.length;for(var b=0;b<c;++b){this.select.remove(0)}if(this.vacio!==false){this.agregarOpcion("",this.vacio)}this.limpio=true;this.grupo="";if(a===false){this.cambio()}};SelectDependiente.prototype.mostrar=function(c,a){this.select.disabled=true;var a=typeof a==="undefined"?false:a;if(this.limpio===false){this.limpiar(a)}if(typeof c!=="undefined"&&c.toString().length==0){c=undefined}if(typeof c==="undefined"&&typeof this.dependiente==="object"){if(this.dependiente.select.selectedIndex>=0){var b=this.dependiente.select.options[this.dependiente.select.selectedIndex].value;if(b.toString().length>0){c=b}}}if(typeof c!=="undefined"||(this.ajax===true&&typeof this.dependiente!=="object")){this.grupo=c;if(this.ajax===true&&typeof jQuery!=="undefined"){if(!(this.cache===true&&typeof this.opciones[this.grupo]!=="undefined")){this.params[this.varsoloref]=false;this.params[this.varref]=c;jQuery.ajax({url:this.url,data:this.params,type:"POST",async:false,dataType:"json",context:this,success:function(d){this.opciones[this.grupo]=d}})}}for(var b in this.opciones[this.grupo]){this.agregarOpcion(b,this.opciones[this.grupo][b])}}if(a===false&&this.select.options.length>0){this.select.selectedIndex=0;this.cambio(this.select.options[this.select.selectedIndex].value)}this.select.disabled=false;return this};SelectDependiente.prototype.seleccionar=function(b,a){var a=typeof a==="undefined"?false:a;for(i=0;i<this.select.options.length;i++){if(this.select.options[i].value==b){this.select.selectedIndex=i;if(a===false){this.cambio(b)}break}}if(typeof this.dependiente==="object"){this.dependiente.mostrar(this.dependiente.buscarGrupoDesdeValor(this.grupo),true);this.dependiente.seleccionar(this.grupo,true)}};
