

var DropdownLoader = function (){

this.loaders = [];
this.dropdowns = [];
this.autoLoadIndex=0;

}
DropdownLoader.prototype.add = function (srcElement, targetElement, url, id, allOption){



this.loaders.push([srcElement, targetElement, url, id, allOption]);


}
DropdownLoader.prototype.load = function (){

for (var i=0; i<this.loaders.length; i++){

if ($(this.loaders[i][0]) && $(this.loaders[i][1]) ) {

allOption = this.loaders[i][4];
if(hasElementClass($(this.loaders[i][1]),"no_all_option")){
allOption=false;

}


this.dropdowns.push( new Dropdown($(this.loaders[i][0]), $(this.loaders[i][1]), this.loaders[i][2], this.loaders[i][3], allOption));
}
}



}

DropdownLoader.prototype.changed = function(obj,index){

for(var i=0; i<this.dropdowns.length;i++){
if(this.dropdowns[i].srcElement==obj){
this.dropdowns[i].sourceChanged(this.autoLoadIndex);
this.dropdowns[i].targetIndex = index;
}

}
}

var dropdownLoader = new DropdownLoader();

var Dropdown = function(srcElement, targetElement, url, id, allOption){

MochiKit.Base.bindMethods(this);
this.srcElement = srcElement;
this.targetElement = targetElement;
this.targetIndex=-1;
this.url = url;
this.id = id;
this.allOption=allOption;
this.autoLoadIndex=0;
this.connect();

}

Dropdown.prototype.connect = function(){
if(this.srcElement){
connect(this.srcElement, "onchange", this, this.sourceChanged);
}
}

Dropdown.prototype.loadNewValues = function(values){

if(values){
while(this.targetElement.hasChildNodes()){
this.targetElement.removeChild(this.targetElement.childNodes[0])
}
var indexToLoad = -1;
if ((typeof(this.allOption)=="boolean" && this.allOption==true) || typeof(this.allOption)=="string"){

if(typeof(this.allOption)=="string"){
appendChildNodes(this.targetElement, OPTION({"value":""}, this.allOption));
} else {
appendChildNodes(this.targetElement, OPTION({"value":""}, "All"));
}


//appendChildNodes(this.targetElement, OPTGROUP({"label":"Select from below"}));
}

for (var i=0; i<values.length; i++){
if(values[i].id==this.targetIndex){
if(this.allOption){
indexToLoad = i+1;
} else {
indexToLoad = i;
}
}

	var oOption = new Option(values[i].name, values[i].id);
	this.targetElement.options[this.targetElement.options.length] = oOption;




}
if(indexToLoad>-1){
this.targetElement.selectedIndex = indexToLoad;
}
}
}

Dropdown.prototype.sourceChanged = function(){



while(this.targetElement.hasChildNodes()){
this.targetElement.removeChild(this.targetElement.childNodes[0])
}

if (this.allOption){
appendChildNodes(this.targetElement, OPTION({"value":""}, "Loading"));
appendChildNodes(this.targetElement, OPTGROUP({"label":"Loading new values"}));
}




var element = this.srcElement;
var me = this;
var val = element.options[element.selectedIndex].value;

if(val!=""){
var q = queryString({"id":val});

		doXHR(this.url, {method:"POST", sendContent:q,headers:{"Content-Type":"application/x-www-form-urlencoded"}}).addCallback(
			
				function(response) {
			
				try {
				
				eval(response.responseText);
				me.loadNewValues(result.values);
				
				} catch (ex){
				
				me.loadNewValues([]);
				
				}
				}
			);
			
			
		}	

}



addLoadEvent(function(){dropdownLoader.load();});