/*** DROP DOWN LISTS ***/

// Object for building dropdown lists
var AjaxDropDownList = Class.create();
AjaxDropDownList.prototype = {
	// Default constructor.  All arguments are optional.
	initialize: function(id, data, textField, valueField) {
		this.id = id;
		this.data = data;
		this.textField = textField;
		this.valueField = valueField;
		this.value = '';		
		if (data != null) this.bind();
	},
	// Binds the text and values for a select tag to an array of objects 
	bind: function(data, textField, valueField) {	
		if (textField != null) this.textField = textField;
		if (valueField != null) this.valueField = valueField;
		if (data != null) this.data = data;
		if (this.ddl == null) this.ddl = $(this.id);
		if (this.ddl == null) return;
		this.ddl.options.length = 0;
		if (this.useNone == true) {
		    this.ddl.options[this.ddl.options.length] = new Option(this.noneText,'');
		}
		for (var i = 0; i < this.data.length; i++) {
			var t = this.data[i][this.textField];
			var v = this.data[i][this.valueField];
			var index = this.ddl.options.length;
			this.ddl.options[index] = new Option(t, v);
			if (v == this.value) {
			    this.ddl.options[index].selected = true;
			}
		}
	},
	showLoading: function() {
		if (this.ddl == null) this.ddl = $(this.id);
		if (this.ddl == null) return;
		this.ddl.options.length = 0;
	    this.ddl.options[this.ddl.options.length] = new Option('-- loading --','-1');
	},
	noneText: 'None',
	useNone: true
}

// Object for automatically calling ajax.net pro in the background
// to populate a drop down list
var AjaxAutoDropDownList = Class.create();
Object.extend(Object.extend(AjaxAutoDropDownList.prototype, AjaxDropDownList.prototype), {
	// Default constructor
	initialize: function() {
		// id for select tag
		this.id = null;
		// method to call to retrieve data
		this.method = null;
		// arguments to send to method
		this.methodArgs = null;
		// field from results to use as option text
		this.textField = null;
		// field from results to use as option values
		this.valueField = null;
	},
	// Override these methods to receive ajax notification events.
	onLoadSuccess: function(value) {},
	onLoadError: function(error) {},
	// Loads drop down list data from ajax.net Pro
	load: function() {
		if (this.method != null) {
		    this.showLoading();		    
		    
		    var argInsert = '';
		    if(this.methodArgs != null)
		    {
		        argInsert = (this.methodArgs[0] == null) ? '' : '\'' + escape(this.methodArgs[0]) + '\', '; 
		        argInsert += (this.methodArgs[1] == null) ? '' : '\'' + escape(this.methodArgs[1]) + '\', '; 
		    }

			var js = (this.methodArgs == null)
				? this.method + '(this.loadCallback.bind(this))'
				: this.method + '(' + argInsert + 'this.loadCallback.bind(this))';									
			try { eval(js); } catch (e) { this.onLoadError(e); }
		}			
		else {
			this.onLoadError('AjaxAutoDropDownList.loadMethod not defined');
		}			
	},
	// Binds data to drop down list upon ajax callback.
	loadCallback: function(res) {
		if (res.error) {
			this.onLoadError(res.error);
		}
		else {
			this.bind(res.value, this.textField, this.valueField);
			this.onLoadSuccess(res.value);
		}
	}	
});	

// Creates a new instance of AjaxAutoDropDownList using named arguments.
AjaxAutoDropDownList.createInstance = function(args) {
	var ret = Object.extend(new AjaxAutoDropDownList(), args);
	ret.load();
	return ret;		
}

