//Author:M.Kostadinov 12.2005.

function ItemsPicker (hidden, source, target) {
	if (typeof source == 'string') source = document.getElementById (source);
	if (typeof target == 'string') target = document.getElementById (target);
	if (typeof hidden == 'string') hidden = document.getElementById (hidden);
	
	if (!(source && target && hidden)) throw Error ("Invalid arguments");
	this.source = source;
	this.target = target;
	this.hidden = hidden;
	this.attachEvents();	
}

ItemsPicker.prototype.attachEvents = function() {
	var thisref=this;

	addEvent (this.target, 'keypress', function (event) {
		if (event.keyCode == 13) {
			thisref.remove();
		}
	}, false);

	addEvent (this.target, 'dblclick', function (event) {
		thisref.remove();
	}, false);

	addEvent (this.source, 'keypress', function (event) {
		if (event.keyCode == 13) {
			thisref.add();
		}
	}, false);

	addEvent (this.source, 'dblclick', function (event) {
		thisref.add();
	}, false);

}

ItemsPicker.prototype.onchange = function() {
	var selectbox = this.target;
	var ops = this.target.options;
	var str = "";
	for (var  i = 0; i < ops.length; i++) str += "," + ops[i].value;
	this.hidden.value = str.substr (1);
	
	if ( ops.length > 1 ) {
		for ( var  i = 0; i < ops.length; i++ ) {
			if ( ops[i].value == 0 ) {
				selectbox.options[i] = null;
				return;
			}
		}
	}
	
	if ( ops.length == 0 ) {
		ops.add (new Option ("ВСИЧКИ РАЙОНИ", "0"));
	}
	
}

ItemsPicker.prototype.add = function() {
	if (this.source.selectedIndex == -1) return;
	
	selectbox = this.source;

	var i;

	for(i=selectbox.options.length-1; i >= 0; i--)
	{
		if(selectbox.options[i].selected && selectbox.options[i].value != 0) {
			oNewOption = document.createElement("OPTION");
			oNewOption.value = selectbox.options[i].value;
			oNewOption.text = selectbox.options[i].text;
			try {
			    this.target.add(oNewOption, null); // standards compliant; doesn't work in IE
			    this.source.options[i] = null;
			}
			catch(oError) {
			    // IE only
				this.target.add(oNewOption); 
			    this.source.options[i] = null;
			}
			//var opt = new Option(selectbox.options[i].text, selectbox.options[i].value);
			 
			//this.target.add (opt);
			
			
			
			}
	}//end for
	
	this.onchange();
}


ItemsPicker.prototype.remove = function() {

	if (this.target.selectedIndex == -1) return;
	
	
	selectbox = this.target;

	var i;

	for(i=selectbox.options.length-1;i>=0;i--)
	{
		if(selectbox.options[i].selected && selectbox.options[i].value != 0) {
		
			oNewOption = document.createElement("OPTION");
			oNewOption.value = selectbox.options[i].value;
			oNewOption.text = selectbox.options[i].text;
			try {
			    this.source.add(oNewOption, null); // standards compliant; doesn't work in IE
			    this.target.options[i] = null;
			}
			catch(oError) {
			    // IE only
				this.source.add(oNewOption); 
			    this.target.options[i] = null;
			}
									
//			var opt = new Option(selectbox.options[i].text, selectbox.options[i].value);
//			
//			this.source.options.add (opt);
//			
//			selectbox.remove(i);
			//document.getElementById(selectbox.id).remove(i);
			}
	}//end for
	
	
	this.onchange();
}

/*

ItemsPicker.prototype.add = function() {
	if (this.source.selectedIndex == -1) return;
	var obj = { 'value' : this.source.value, 'text' : this.source.options [this.source.selectedIndex].text };

	//if (this.source.selectedIndex < this.source.options.length) this.source.selectedIndex++;
	
	for (var i = 0; i < this.target.options.length; i++) {
		if (this.target.options[i].value == obj.value) return;
	}
	var opt = new Option (obj.text, obj.value);
	this.target.options.add (opt);
	this.target.options.selectedIndex = this.target.options.length - 1;
	
	
	this.removeOptions(this.source);
	
	this.onchange();
}

ItemsPicker.prototype.remove = function() {
	var ops = this.target.options;
	var idx = ops.selectedIndex;
	if (idx > -1) {
		this.target.remove (idx);
		if (idx < ops.length) {
			this.target.selectedIndex = idx;
		} else if (ops.length) {
			this.target.selectedIndex = idx - 1;
		}
	}
	this.onchange();
}


*/

ItemsPicker.prototype.clear = function() {
	this.target.options.length = 0;
	this.onchange();
}

if (typeof ($) == 'undefined') $ = document.getElementById;

function addEvent (obj, eventType, afunction, isCapture) { 
	// W3C DOM 
	if (obj.addEventListener) { 
		obj.addEventListener(eventType, afunction, isCapture); return true; 
	} // Internet Explorer 
	else if (obj.attachEvent) { 
		return obj.attachEvent("on"+eventType, afunction); } 
	else return false; 
} 

function removeEvent (obj, eventType, afunction, isCapture) { 
	if (obj.removeEventListener) { 
		obj.removeEventListener(eventType, afunction, isCapture); return true; 
	} else if (obj.detachEvent) { 
		return obj.detachEvent("on"+eventType, afunction); 
	} else return false; 
} 

