﻿// JScript File

Type.register(
	"BDC.OptionDD",
	function(p_el){
		if(p_el){this.base(p_el);}
	},
	True.Behavior
);

BDC.OptionDD.prototype.handleSelectChange = function(e){
	var t = e.target || e.src;
	if(t.value == "Other"){
		this.value.val("");
		this.other.show();
		this.other.get(0).focus();
		var self = this;
		this.other.bind("change", function(e){self.onOtherChange(e);});
	}
	else{
		this.value.val(t.value);
		this.other.unbind("change");
		this.other.val("");
		this.other.hide();
	}
};

BDC.OptionDD.prototype.handleReChange = function(e){
	try{
		var t = e.target || e.src;
		this.setList(t.value);
	}catch(err){
		this.select.empty();
		this.reset();
	}
};

BDC.OptionDD.prototype.onOtherChange = function(e){
	var t = e.target || e.src;
	this.value.val(t.value);
};


BDC.OptionDD.prototype.setList = function(p_v){
	var options = $("div.js-" + p_v, this.element).html().split(",");
	options.splice(0, 0, "SELECT ONE");
	options.push("Other");
	this.select.empty();
	for(var i = 0; i < options.length; i ++){
		this.select.append("<option value='" + options[i] + "'>" + options[i] + "</option>");
    }
    this.reset();
};

BDC.OptionDD.prototype.reset = function(){
    this.value.val("");
    this.other.val("");
    this.other.hide();
    this.select.get(0).selectedIndex = 0;
};


BDC.OptionDD.addFunction("init", function(){
	this.base.init.apply(this);
	var self = this;
	var r = $("select#ctl00_pageBody_ticket_regardingDropDown").bind("change", function(e){self.handleReChange(e);}).get(0);
	this.select = $("select", this.element);
	this.value = $("input:hidden", this.element);
	this.other = $("input:text", this.element);
	this.select.bind("change", function(e){self.handleSelectChange(e);});
	this.setList(r.value);
});
	
