document.is_ie = !(navigator.appName.indexOf("Netscape") != -1);

function multipleGuideInput(id, field_id, new_text) {
	if(!id) {
		return alert("[multipleGuideInput] Incorrect id given.");
	}
	this.id = id;
	this.field_id = field_id;
	this.new_text = new_text;
	this.is_updated = false;
	this.old_html = "";
	this.init();
	this.collectValues();
	this.bindEvents();
	this.update_complete = false;
}

multipleGuideInput.prototype.id = null;
multipleGuideInput.prototype.selectObj = null;
multipleGuideInput.prototype.new_text = null;
multipleGuideInput.prototype.inputObj = null;
multipleGuideInput.prototype.fid = null;
multipleGuideInput.prototype.old_html = null;
multipleGuideInput.prototype.values = new Array();
multipleGuideInput.prototype.is_updated = false;
multipleGuideInput.update_completed = false;

multipleGuideInput.prototype.init = function () {
	var selectObj = document.getElementById("multipleGuideSelect_" + this.id);
	if(typeof selectObj != "object") {
		return alert("[multipleGuideInput] Can't find correct selectbox.");
	}
	this.selectObj = selectObj;

	var inputObj = document.getElementById("multipleGuideInput_" + this.id);
	if(typeof inputObj != "object") {
		return alert("[multipleGuideInput] Cant find correct input.");
	}
	this.inputObj = inputObj;
	this.fid = this.field_id;
	this.is_updated = false;
};


multipleGuideInput.prototype.collectValues = function () {
	var options = this.selectObj.options, l = options.length, i;
	var values = new Array();

	for(i = 0; i < l; i++) {
		values.push(options[i].innerHTML);
	}
	this.values = values;
};



multipleGuideInput.prototype.bindEvents = function () {
	var __self = this;
	if (this.selectObj.multiple){
		if (this.inputObj){
			this.inputObj.onkeypress = function (keyEvent) {
				var keyCode = (document.is_ie) ? event.keyCode : keyEvent.keyCode;
				__self.checkInput(val);
				if(keyCode == 13) {
					var val = this.value;
					if(val) {
						__self.addItem(val);
						this.value = "";
					}
					return false;
				} else {
					return true;
				}
			}
		}	
	}else{
		if (this.inputObj){	
			this.inputObj.onkeypress = function (keyEvent) {
				var keyCode = (document.is_ie) ? event.keyCode : keyEvent.keyCode;
				__self.checkInput(val);
				if(keyCode == 13 && __self.update_complete) {
					var val = this.value;
					if(val) {
						__self.addItem(val);
						this.value = "";
					}
					return false;
				}else{
					return true;
				}
			};
			this.inputObj.onfocus = function (){
				__self.RequestXML();	
			}
			this.inputObj.onkeyup = function (keyEvent){
				var val = this.value;
				var keyCode = (document.is_ie) ? event.keyCode : keyEvent.keyCode;
				if (keyCode != 13){	
					__self.checkInput(val);
				}	
			}
			
		}
		this.selectObj.onclick = function (){
			__self.RequestXML();
		}
	}
};
multipleGuideInput.prototype.RequestXML = function(){
	var __self = this;
	var select_class = __self.selectObj.className;
	if (!__self.is_updated){
		this.old_html = this.selectObj.innerHTML;
		if (!document.is_ie){
			this.selectObj.innerHTML = "<option>" + getLabel('js-guide-load') + "</option>";
		}
		var url = "/admin/data/guide_items_all/"+this.fid+".xml";
		var params = "";
		var self = this;
		__self.is_updated = true;
		var request = new Ajax.Request(url,
			{method: 'get', 
			parameters: params, 
			onComplete: function (a) {
				self.RequestRes(a);
			}
		}
		)
	}
}
multipleGuideInput.prototype.RequestRes = function (Response){
	var Ajax_response = Response.responseXML;
	var item_list = Ajax_response.getElementsByTagName("object");
	var list_length = item_list.length;
	if (this.selectObj.length>1){
		var selected_opt = this.selectObj.options[1].value;
	}
	var __self = this;
	if (!document.is_ie){
		__self.selectObj.innerHTML = this.old_html;
	}
	if (true){
		for (i = 0; i < list_length; i++){
			this_text = item_list[i].attributes[2].nodeValue;
			this_id = item_list[i].attributes[1].nodeValue;
			option = document.createElement("option");
			option.value = this_id;
			option.innerHTML = this_text;
			var l = this.selectObj.options.length;
			var sel_id = this.selectObj.options[this.selectObj.options.selectedIndex].value;
			if ((this_id != sel_id)){
				this.selectObj.appendChild(option);		
			}	
		}
		if (this.inputObj.value!=""){
			__self.checkInput(val);	
		}
		__self.update_complete = true;
	}
		
}
multipleGuideInput.prototype.checkIfExists = function (value) {
	var i, l = this.values.length;
	for(i = 0; i < l; i++) {
		if(this.values[i] == value) {
			return true;
		}
	}
	return false;
};
multipleGuideInput.prototype.checkInput = function() {
	var inputed = this.inputObj.value;
	for (i = 0; i < this.selectObj.options.length; i++){
		reg = new RegExp("^("+inputed+")", "i");
		res = reg.test(this.selectObj.options[i].text);
		if (res == true){
			this.selectObj.options.selectedIndex = i;	
		}	
	}
}
multipleGuideInput.prototype.addItem = function (value) {
	if(this.checkIfExists(value)) {
		//TODO
	} else {
		var option = document.createElement("option");
		option.innerHTML = "&rarr;&nbsp;&nbsp;" + value;
		option.value = value;
		if(this.selectObj.options.length == 0) {
			this.selectObj.appendChild(option);
		} else {
			this.selectObj.insertBefore(option, this.selectObj.firstChild);
		}
		this.values.push(value);
	}
	this.selectItem(value);
};


multipleGuideInput.prototype.selectItem = function (value) {
	var options = this.selectObj.options, l = options.length, i;
	
	for(i = 0; i < l; i++) {
		var option = options[i];
		if(option.innerHTML == value || option.value == value) {
			option.selected = true;
			option.checked = true;
			return true;
		}
	}

	return false;
}

