var is_explorer = window.navigator.appName.match(/msie/i) ? true : false
var is_mozilla = window.navigator.appName.match(/netscape/i) ? true : false
function c_dblselect(id_prefix,items,limit_reached,maximum_items) {
	this.id_prefix = id_prefix
	this.items = items
	this.limit_reached = limit_reached.replace("@@@",maximum_items)
	this.maximum_items = maximum_items;
	this.isSingle = false;
	this.half_size = 0
	this.clear_button_text = 'נקה'
}
c_dblselect.prototype.sort_items = function() {
	this.items = this.items.sort(
		function(x,y) {
			var xs = x[1].replace(/\'/g,"")
			var ys = y[1].replace(/\'/g,"")
			for(var i=0; i < xs.length ;i++) {
				if (i > ys.length) return 1
				var xc = xs.charCodeAt(i)
				var yc = ys.charCodeAt(i)
				if (xc < yc) {return -1}
				if (xc > yc) {return  1}
			}
			return -1
		}
	)
}
c_dblselect.prototype.show = function(size,_class) {
	this.half_size = Math.floor(size / 2)
	var reset_width = 60
	var reset_color = "909090"
	return '<table width="100%" cellspacing="0" cellpadding="0" border="0">'
		+ '<tr>'
		+	'<td><input id=' + this.id_prefix + '_control class="' + _class + '"></td>'
		+	'<td align="left" colspan=2>'
		+		'<table dir="ltr" width="100%" cellspacing="0" cellpadding="0" border="0">'
		+		'<tr>'
		+			'<td>'
		+				'<div id="' + this.id_prefix + '_btn_clear" style="width:' + reset_width + ';border:1px solid #' + reset_color + ';cursor:pointer">'
		+					'<div style="width:' + (reset_width-2) + 'px;border:1px solid #ffffff;background-color:#' + reset_color + ';font:13px arial;color:#ffffff">'
		+						'<div style=" margin-top:-2px; text-align:center;width:' + (reset_width-4) + 'px">'
		+							this.clear_button_text
		+						'</div>'
		+					'</div>'
		+				'</div>'
		+			'</td>'
		+			'<td width="90%"></td>'
		+			'<td>'
		+				'<div id="' + this.id_prefix + '_btn_addall" style="width:' + reset_width + ';border:1px solid #' + reset_color + ';cursor:pointer;visibility:hidden">'
		+					'<div style="width:' + (reset_width-2) + ';border:1px solid #ffffff;background-color:' + reset_color + ';font:13px arial;color:#ffffff">'
		+						'<div style="position:relative;top:-1;text-align:center;width:' + (reset_width-4) + '">'
		+							'a"e"i`'
		+						'</div>'
		+					'</div>'
		+				'</div>'
		+			'</td>'
		+		'</tr>'
		+		'</table>'
		+	'</td>'
		+ '</tr>'
		+ '<tr><td><img width="1" height="3"></td></tr>'
		+ '<tr>'
		+	'<td rowspan="3"><select id="' + this.id_prefix + '_all" size="' + size + '" class="' + _class + '"></select></td>'
		+	'<td width="50%" align="right" valign="bottom"><img style="cursor:pointer" id="' + this.id_prefix + '_add" src="/serve/images/select/add.gif"></td>'
		+	'<td rowspan="3"><select id="' + this.id_prefix + '_selected" size="' + size + '" class="' + _class + '"></select></td>'
		+ '</tr>'
		+ '<tr><td><img width="1" height="4"></td></tr>'
		+ '<tr><td align="left" valign="top"><img style="cursor:pointer" id="' + this.id_prefix + '_remove" src="/serve/images/select/remove.gif"></td></tr>'
		+ '</table>'
}
c_dblselect.prototype.showSingle = function(size,classSelect,classInput,classTitle,message,width) {
	this.half_size = Math.floor(size / 2)
	this.isSingle = true;
	var reset_width = 60
	var reset_color = "909090"
	return '<table cellspacing="0" cellpadding="0" width="100%" border="0">'
		+ '<tr>'
		+	'<td class="'+classTitle+'" nowrap><b>'+message+'</b></td>'
		+ '</tr>'
		+	'<td align=left style="padding-left: 10px;"><input id=' + this.id_prefix + '_control class="'+classInput+'"  style="WIDTH: ' + width + 'px;"></td>'
		+ '</tr>'
		+ '<tr>'
		+ '	<td align=left style="padding-left: 10px;"><select id="' + this.id_prefix + '_all" name="' + this.id_prefix + '_all" size="'+ size +'" style="WIDTH: ' + width + 'px;" class="' + classSelect + '"></select>'
		+		'<select id="' + this.id_prefix + '_selected" style="display:none"></select>'
		+	'</td>'
		+ '</tr>'
		+ '</table>'
}
c_dblselect.prototype.get_selected_by_index = function(index) {
	for(var i = 0; i < this.select_selected.options.length ;i++) {
		if (index == this.select_selected.options[i].value) {
			return this.items[index]
		}
	} return null
}
c_dblselect.prototype.goto_selected_by_id = function(id) {
	for(var i=0; i <  this.items.length ;i++) {
		if (id == this.items[i][0]) {
			this.control.value = this.items[i][1]
			this.find_highlighted()
			return i
		}
	}
}
c_dblselect.prototype.get_selected_by_id = function(id) {
	for(var i=0; i <  this.items.length ;i++) {
		if (id == this.items[i][0]) {return this.get_selected_by_index(i)}
	} return null
}
c_dblselect.prototype.add_all = function() {for(var i=0; i < this.items.length ;i++) {this.add(i,true)}}
c_dblselect.prototype.add_by_id = function(id) {for(var i=0; i < this.items.length ;i++) {if (id == this.items[i][0]) {this.add(i,true)}}}
c_dblselect.prototype.add_by_ids = function(list) {
	for(var i=0; i < list.length ;i++) {
		var id = parseInt(list[i],10)
		if (!isNaN(id)) {this.add_by_id(id)}
	}
}
c_dblselect.prototype.alert = function(m) {
	switch(m) {
		case 'limit_reached' :
			alert(this.limit_reached)
			break
		default :
			alert(m)
			break
	}
}
c_dblselect.prototype.add = function(v,silent) {
	if(this.isSingle) {this.remove_all();}
	if (arguments.length < 2) {
		silent = false
		if (arguments.length < 1) {v = this.select_all.selectedIndex}
	}
	if (v > -1) {
		var t = this.select_all.options[v].innerHTML
		for(var i = 0; i < this.select_selected.options.length ;i++) {if (this.select_selected.options[i].value == v) {return}}
		if (this.select_selected.options.length >= this.maximum_items) {
			if (!silent) {this.alert("limit_reached")}
			return
		}
		this.select_selected.options[this.select_selected.options.length] = new Option(t,v)
		this.select_selected.timestamp = new Date()
		if (this.isSingle) {
			this.control.value = t
		} else {
			if (true != this.description_set) {this.control.value = ""}
		}
	}
}
c_dblselect.prototype.remove_all = function() {this.select_selected.options.length = 0;}
c_dblselect.prototype.remove = function() {
	if (this.select_selected.selectedIndex > -1) {
		this.select_selected.options[this.select_selected.selectedIndex] = null
	}
}
c_dblselect.prototype.select_all_keydown = function(e) {
	switch(e.keyCode) {
		case 10 :
		case 13 :
			this.control.value = this.select_all.options[this.select_all.selectedIndex].innerHTML
			this.add()
			return
		case 38 : // up
			this.control.value = this.select_all.options[Math.max(0,this.select_all.selectedIndex-1)].innerHTML
			return
		case 40 : // down
			this.control.value = this.select_all.options[Math.min(this.select_all.options.length-1,this.select_all.selectedIndex+1)].innerHTML
			return
	}
}
c_dblselect.prototype.control_keydown = function(e) {
	switch(e.keyCode) {
		case 10 :
		case 13 :
			this.add()
			return

		case 38 : // up
			this.select_all.selectedIndex = Math.max(0,this.select_all.selectedIndex-1)
			this.control.value = this.select_all.options[this.select_all.selectedIndex].innerHTML
			return

		case 40 : // down
			this.select_all.selectedIndex = Math.min(this.select_all.options.length-1,this.select_all.selectedIndex+1)
			this.control.value = this.select_all.options[this.select_all.selectedIndex].innerHTML
			return
	}
}
c_dblselect.prototype.cnv_array = function(s) {
	var sa = new Array()
	for(var i=0; i < s.length ;i++) {sa[sa.length] = s.charCodeAt(i)}
	return sa
}
c_dblselect.prototype.control_keyup = function(e) {
	switch(e.keyCode) {
		case 10 :
		case 13 : // enter
		case 38 : // up
		case 40 : // down
			return
	}
	this.find_highlighted()
}
c_dblselect.prototype.find_highlighted = function() {
	var va = this.cnv_array(this.control.value.replace(/^[\s]*/,"").replace(/[\s]*$/,"").replace(/[\s]+/g," ").replace(/\'/g,""))
	va[va.length] = 0
	var t = (va.length > 1) ? this.find_quickly(0,this.items.length - 1,va) : 0
	this.select_all.selectedIndex = t + this.half_size
	this.select_all.selectedIndex = t
}
c_dblselect.prototype.compar = function(va,ma) {
	function r(s,p) {this.smaller = s; this.position = p}
	for(var i=0; i < va.length ;i++) {
		if (i == ma.length) {return new r((va[i] == 0) ? 'equal' : 'ma',i)}
		if (va[i] > ma[i])  {return new r('ma',i)}
		if (va[i] < ma[i])  {return new r('va' ,i)}
	}
	return new r('equal',va.length)
}
c_dblselect.prototype.find_quickly = function(f,l,va) {
	if (f > l) return Math.min(Math.max(0,f),this.items.length-1)
	var m = Math.floor((f+l) / 2)
	var ma = this.cnv_array(this.items[m][1].replace(/\'/g,""))
	var r = this.compar(va,ma)
	if (r.smaller == 'equal') {return m}
	return (r.smaller == 'va') ? this.find_quickly(f,m-1,va) : this.find_quickly(m+1,l,va)
}
c_dblselect.prototype.get_selected_text = function(separator) {
	var s = ""
	for(var i = 0; i < this.select_selected.options.length ;i++) {
		s += ((i > 0) ? separator : "") + this.items[this.select_selected.options[i].value][1]
	} return s
}
c_dblselect.prototype.get_selected_ids = function(separator) {
	var s = ""
	for(var i = 0; i < this.select_selected.options.length ;i++) {
		s += ((i > 0) ? separator : "") + this.items[this.select_selected.options[i].value][0]
	} return s
}
c_dblselect.prototype.get_selected = function() {
	var a = new Array()
	for(var i = 0; i < this.select_selected.options.length ;i++) {
		a[a.length] = this.items[this.select_selected.options[i].value]
	} return a
}
c_dblselect.prototype.activate = function(selected,columns,handler_for_selected_onpropertychanged) {
	function concatenate(data,columns) {
		var s = []
		for(var i=0; i < columns.length ;i++) {
			var p = parseInt(columns[i],10)
			if (isNaN(p)) {
				if (columns[i].length == 0) {continue}
				p = (columns[i].substring(0,1) == "@") ? 1 : 0
				s.push(columns[i].substring(p))
			} else {
				if (p < data.length) {
					s.push(data[p])
				}
			}
		} return s.join('')
	}
	if (arguments.length < 3) {handler_for_selected_onpropertychanged = null}
	if (arguments.length < 2) {columns = [1]}
	if (arguments.length < 1) {selected = []}
	this.control = document.getElementById(this.id_prefix + "_control")
	this.control.container = this

	this.select_selected = document.getElementById(this.id_prefix + "_selected")
	this.select_selected.container = this

	this.select_all = document.getElementById(this.id_prefix + "_all")
	this.select_all.container = this

	for(var i=0; i < this.items.length ;i++) {
		try {
			this.select_all.options[i] = new Option(concatenate(this.items[i],columns), this.items[i][0])
			for(var j=0; j < selected.length ;j++) {
				if (selected[j] == this.items[i][0]) {
				//	try {
						this.select_selected.options[this.select_selected.options.length] = new Option(concatenate(this.items[i],columns),i)
				//	} catch(ex) {alert(i + ':' + j + ':' + this.items[i])}
				}
			}
		} catch(ex) {alert(i + ':' + typeof(this.items[i]))}
	}
	if (!this.isSingle) {
		var img_add = document.getElementById(this.id_prefix + "_add")
		img_add.container = this
		img_add.onclick = function(e,t) {
			t = (arguments.length == 0) ? event.srcElement : e.target
			t.container.add()
		}
		img_add.style.cursor = 'pointer'
	
		var img_remove = document.getElementById(this.id_prefix + "_remove")
		img_remove.container = this
		img_remove.onclick = function(e,t) {
			t = (arguments.length == 0) ? event.srcElement : e.target
			t.container.remove()
		}
		img_remove.style.cursor = 'pointer'
	
		var btn_clear = document.getElementById(this.id_prefix + "_btn_clear")
		btn_clear.container = this
		btn_clear.onclick = function(e,t) {
			t = (arguments.length == 0) ? event.srcElement : e.target
			for(var p = t; p ; p = p.parentNode) {
				if (p.container) {
					p.container.remove_all()
					return
				}
			}
		}
		var btn_addall = document.getElementById(this.id_prefix + "_btn_addall")
		btn_addall.container = this
		btn_addall.onclick = function(e,t) {
			t = (arguments.length == 0) ? event.srcElement : e.target
			for(var p = t; p ; p = p.parentNode) {
				if (p.container) {
					p.container.add_all()
					return
				}
			}
		}
	}
	this.select_selected.onpropertychange = handler_for_selected_onpropertychanged
	this.select_selected.ondblclick = function(e,t) {
		t = (arguments.length == 0) ? event.srcElement : e.target
		for(var p = t; p ; p = p.parentNode) {
			if (p.container) {
				p.container.remove()
				return
			}
		}		
	}
	this.select_all.onfocus = function(e,t) {
		t = (arguments.length == 0) ? event.srcElement : e.target
		for(var p = t; p ; p = p.parentNode) {
			if (p.container) {
				try {p.container.control.value = p.options[p.selectedIndex].innerHTML} catch(ex) {}
				return
			}
		}
	}
	this.select_all.onclick = function(e,t) {
		t = (arguments.length == 0) ? event.srcElement : e.target
		for(var p = t; p ; p = p.parentNode) {
			if (p.container) {
				p.container.control.value = p.options[p.selectedIndex].innerHTML
			//	p.container.add()
				return
			}
		}
	}
	this.select_all.ondblclick = function(e,t) {
		t = (arguments.length == 0) ? event.srcElement : e.target
		for(var p = t; p ; p = p.parentNode) {
			if (p.container) {
			//	p.container.control.value = p.options[p.selectedIndex].innerHTML
				p.container.add()
				return
			}
		}
	}

	this.select_all.onkeydown = function(e,t) {
		e = arguments.length == 0 ? event : e
		t = arguments.length == 0 ? event.srcElement : e.target
		t.container.select_all_keydown(e)
	}
	this.control.value = ""
	this.control.container = this
	this.control.onkeydown = function(e,t) {
		e = arguments.length == 0 ? event : e
		t = arguments.length == 0 ? event.srcElement : e.target
		t.container.control_keydown(e)
	}
	this.control.onkeyup = function(e,t) {
		e = arguments.length == 0 ? event : e
		t = arguments.length == 0 ? event.srcElement : e.target
		t.container.control_keyup(e)
	}
}
c_dblselect.prototype.set_control_description = function(control_description,background_color) {
	this.description_set = true
	this.control.style_background_color = this.control.style.backgroundColor
	this.control.style.backgroundColor = background_color
	this.control.value = control_description
	this.control.onfocus = function(e,c) {
		c = arguments.length == 0 ? event.srcElement : e.target
		c.value = ""
	}
	this.control.onchange = function(e,t) {
		e = arguments.length == 0 ? event : e
		t = arguments.length == 0 ? event.srcElement : e.target
		if (e.propertyName == "value") {
			t.container.description_set = false
			t.onpropertychange = null
			t.onfocus = null
			t.style.backgroundColor = event.srcElement.style_background_color
		}
	}
	this.control.onblur = function(e,c) {
		c = arguments.length == 0 ? event.srcElement : e.target
		if (!c.value) {c.container.set_control_description(control_description,background_color)}
	}
}


