function CMultiDDL(name)
{
	this.name = name;
	this.options = Array();
	this.selectedColor = '#0A246A';
	this.bgcolor = '#FFFFFF';
	this.textColor = '#000000';
	this.selectedTextColor = '#FFFFFF';
	this.rowHeight = '15px';	
	this.extraWidth = 19;	
	this.textWidth = '281px';	
	this.width = '300px'; 
	this.maxRow = 10;
	this.atRow = 0;
	this.isopen = false;
	this.pathImage = '/IMAGES/MultiDDL/';
	this.textbox = ' -- hold ctrl to select many -- ';
	this.lastSelected = 0;
	this.beforeLastSelected = -1;
	this.size = 7;
	this.lastMouseDown = new Date();
	this.lastMouseUp = new Date();
	this.isMouseDown = false;
	this.dbg = '';
	
	this.refresh = null;
	this.draw = CMultiDDL_draw;
	this.open = CMultiDDL_open;
	this.close = CMultiDDL_close;
	this.onmousedown = CMultiDDL_onmousedown;	
	this.onmouseup = CMultiDDL_onmouseup;	
	this.onmousemove = CMultiDDL_onmousemove;
	this.addOption = CMultiDDL_addOption;
	this.pressArrow = CMultiDDL_pressArrow;
	this.pressOption = CMultiDDL_pressOption;
	this.clearOption = CMultiDDL_clearOption;
	this.setWidth = CMultiDDL_setWidth;	
	this.setText = CMultiDDL_setText;
	this.range = CMultiDDL_range;
	this.scroll = CMultiDDL_scroll;
	this.update = CMultiDDL_update;
	this.updateOption = CMultiDDL_updateOption;
	this.updateSlider = CMultiDDL_updateSlider;
	this.dragScroll = CMultiDDL_dragScroll;	
}

function CMultiDDL_setWidth(width)
{
	this.width = width + 'px';
	this.textwidth = (width - this.extraWidth) + 'px';
}

function CMultiDDL_setText(text)
{
	this.textbox = text;	
}

function CMultiDDL_draw()
{
	document.writeln("<div id='CMultiDDL_" + this.name + "_main'><table border='0' width='" + this.width + "' cellpadding='0' cellspacing='0'><tr><td width='2'><img width='2px'  height='21px' id='CMultiDDL_" + this.name + "_leftbar_img' src='" + this.pathImage + "ddl_leftbar.jpg'></td><td id='CMultiDDL_" + this.name + "_text' width='100%' height='21px' background='" + this.pathImage + "ddl_text.jpg'>&nbsp;" + this.textbox + "</td><td width='15' id='CMultiDDL_" + this.name + "_arrow'><img width='17px'  height='21px' id='CMultiDDL_" + this.name + "_arrow_img' src='" + this.pathImage + "ddl_arrow.jpg'></td></tr></table><div  style='position:relative;'></div><div style='position:absolute; overflow:hidden' id='CMultiDDL_" + this.name + "_list'></div></div>");
	document.getElementById('CMultiDDL_' + this.name + '_main').onmousedown = this.onmousedown;
	document.getElementById('CMultiDDL_' + this.name + '_main').onmouseup = CMultiDDL_onmouseup;		
}

function CMultiDDL_onmousedown(e)
{	
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();	
	
	var tg = (e.target) ? e.target : e.srcElement
	var arrow = /CMultiDDL_([A-Za-z0-9]+)_/;
	var option = /CMultiDDL_([A-Za-z0-9]+)_list_([0-9]+)/;
	var slider = /CMultiDDL_([A-Za-z0-9]+)_slider_/;
	var sliderup = /CMultiDDL_([A-Za-z0-9]+)_slider_up_([0-9]+)/;
	var sliderdown = /CMultiDDL_([A-Za-z0-9]+)_slider_down_([0-9]+)/;	
	var scrollslider = /CMultiDDL_([A-Za-z0-9]+)_scrolldown_/;
	var obj = null;
	var m = null;
	var newdate = new Date();		
		
	if(m = tg.id.match(option))
	{				
		eval('obj = ' + m[1]);								
		obj.pressOption(parseInt(m[2]),e.ctrlKey,e.shiftKey);		
		obj.lastMouseDown = newdate;
	}		
	else if(m = tg.id.match(sliderup))
	{
		eval('obj = ' + m[1]);
		obj.isMouseDown = true;
		obj.scroll(-1);
	}
	else if(m = tg.id.match(sliderdown))
	{	
		eval('obj = ' + m[1]);
		obj.isMouseDown = true;
		obj.scroll(1);		
	}
	else if(m = tg.id.match(slider))
	{		
		eval('obj = ' + m[1]);
		obj.isMouseDown = true;		
		obj.dragScroll(e);				
	}
	else if(m = tg.id.match(arrow))
	{						
		var tmp_isopen; 
	
		eval('obj = ' + m[1]);			
		tmp_isopen = obj.isopen;
		if(closeall != undefined) closeall();		
		obj.isopen = tmp_isopen;
		obj.pressArrow();		
	}	
	
	return false;
}

function CMultiDDL_onmousemove(e)
{
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();	
	
	var tg = (e.target) ? e.target : e.srcElement	
	var slider = /CMultiDDL_([A-Za-z0-9]+)_slider_/;
	var obj = null;
	var m = null;

	if(m = tg.id.match(slider))
	{		
		eval('obj = ' + m[1]);		
		obj.dragScroll(e);				
	}
}

function CMultiDDL_dragScroll(e)
{		
	var posy = 0;	
	var posup = 0;
	var posdown = 0;	
	var percent = 0.00;

	if(this.isMouseDown != true) 
	{
		return false;
	}

	if (e.pageY)
	{	
		posy = e.pageY;
	}
	else if (e.clientY)
	{		
		posy = e.clientY + document.body.scrollTop;
	}
			
	posup = CMultiDDL_posy(document.getElementById("CMultiDDL_" + this.name + "_slider_up_1"));	
	posdown = CMultiDDL_posy(document.getElementById("CMultiDDL_" + this.name + "_slider_down_2"));
	
	percent = ((posy - posup - 16)/(posdown - posup - 16));
	
	if(percent > 1.00)
	{	
		this.atRow = this.options.length - this.maxRow;
	}
	else if(percent < 0.00)
	{	
		this.atRow = 0;
	}
	else
	{				
		this.atRow = parseInt(this.options.length * percent) - (this.maxRow/2);
		if((this.atRow + this.maxRow) > this.options.length)
		{
			this.atRow = this.options.length - this.maxRow;
		}
		else if(this.atRow < 0.00)
		{	
			this.atRow = 0;
		}
	}
	this.update();
}

function CMultiDDL_posy(obj)
{
	var curtop = 0;
	
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function CMultiDDL_scroll(num)
{
	var row = this.atRow + num;
	if(row <= (this.options.length - this.maxRow) && row >= 0 && (this.isMouseDown))
	{			
		this.atRow = row;	
		this.update();		
		setTimeout(this.name + '.scroll(' + num + ')', 100);
	}		
}

function CMultiDDL_onmouseup(e)
{
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();		
	
	var tg = (e.target) ? e.target : e.srcElement	
	var arrow = /CMultiDDL_([A-Za-z0-9]+)_/;
	var option = /CMultiDDL_([A-Za-z0-9]+)_list_([0-9]+)/;	
	var obj = null;
	var m = null;
	var newdate = new Date();		
			
	if(m = tg.id.match(option))
	{	
		eval('obj = ' + m[1]);			
		if(((newdate - obj.lastMouseDown) < 500) && ((newdate - obj.lastMouseUp) < 500)  && (obj.lastSelected == m[2]) && (obj.beforeLastSelected == m[2]))
		{			
			obj.pressOption(parseInt(m[2]),e.ctrlKey,e.shiftKey);
			obj.close();				
			if(obj.refresh)obj.refresh();		
		}			
		obj.beforeLastSelected = obj.lastSelected;
		obj.lastMouseUp = newdate;		
	}		
	else if(m = tg.id.match(arrow))
	{	
		eval('obj = ' + m[1]);			
		obj.isMouseDown = false;		
	}	
	return false;
}

function CMultiDDL_pressArrow()
{
	if(this.isopen)
	{
		this.close();
	}
	else
	{
		this.open();
	}	
}

function CMultiDDL_pressOption(option, ctrl, shift)
{ 
	if(this.options[option] != undefined)
	{
		if(shift)
		{
			if(this.lastSelected > option)
			{
				this.range(option, this.lastSelected - 1);				
			}
			else
			{
				this.range(this.lastSelected + 1, option);				
			}
		}
		else if(ctrl)
		{
			this.options[option].selected = this.options[option].selected ? false : true;
		}
		else
		{
			this.clearOption(false);
			this.options[option].selected = true;
			this.textbox = this.options[option].text;
		}				
		this.lastSelected = option;
		this.update();
	}
}

function CMultiDDL_update()
{
	this.updateOption();
	this.updateSlider();
}

function CMultiDDL_updateOption()
{
	var	len = this.options.length;
	var s = "<table border='0' width='100%' cellpadding='1' height='" + (len * this.rowHeight) + "' cellspacing='0'>";
	
	if(len > this.maxRow)
	{		
		len = this.maxRow + this.atRow;
	}
		
	for(var i=this.atRow; i < len; i++)
	{	
		if(this.options[i].selected)
		{
 			s += "<tr valign='top' height='" + this.rowHeight + "'><td id='CMultiDDL_" + this.name + "_list_" + i + "' style='color:" + this.selectedTextColor + "' bgcolor='" + this.selectedColor + "'>" + this.options[i].text + "</td></tr>";
 		}
 		else
 		{
 			s += "<tr valign='top' height='" + this.rowHeight + "'><td id='CMultiDDL_" + this.name + "_list_" + i + "' style='color:" + this.textColor + "' bgcolor='" + this.bgcolor + "'>" + this.options[i].text + "</td></tr>";
 		} 		
	}	
	s += "</table>";
	document.getElementById('CMultiDDL_' + this.name + '_list_option').innerHTML = s;
}

function CMultiDDL_updateSlider()
{
	if(this.options.length > this.maxRow)
	{
		document.getElementById('CMultiDDL_' + this.name + '_slider_slider').style.top = ((this.atRow/(this.options.length)) * 100) + '%';
	}
}

function CMultiDDL_open()
{
	var	len = this.options.length;
	var s = '';
	var t = '';
	var box = '';

	if(len > this.maxRow)
	{		
		len = this.maxRow + this.atRow;		
		t = "<td height='100%'><table border='0' cellpadding='0' cellspacing='0' height='100%' background='" + this.pathImage + "ddl_slider_bg.jpg'>"
		t += "<tr><td id='CMultiDDL_" + this.name + "_slider_up_1'><img id='CMultiDDL_" + this.name + "_slider_up_10' src='" + this.pathImage + "ddl_slider_arrow_up.jpg'></td></tr>"
		t += "<tr><td height='100%' id='CMultiDDL_" + this.name + "_slider_slider_td' valign='top'><div id='CMultiDDL_" + this.name + "_slider_slider' style='position:relative; top:" + ((this.atRow/(this.options.length)) * 100) + "%; height:100%'>";			
		
			t += "<table border='0' cellpadding='0' cellspacing='0' height='" + ((this.maxRow / this.options.length)*100) + "%' background='" + this.pathImage + "ddl_slider_middle.jpg'>";	// top ((this.atRow/(this.options.length)) * 100)
			t += "<tr><td id='CMultiDDL_" + this.name + "_slider_slider_1'><img id='CMultiDDL_" + this.name + "_slider_slider_1' src='" + this.pathImage + "ddl_slider_top.jpg'></td></tr>";														// bot ((this.atRow + this.maxRow/(this.options.length)) * 100)
			t += "<tr><td height='100%' id='CMultiDDL_" + this.name + "_slider_slider_1'><img id='CMultiDDL_" + this.name + "_slider_slider_2' src='" + this.pathImage + "ddl_slider_middle.jpg'></td></tr>";	
			t += "<tr><td id='CMultiDDL_" + this.name + "_slider_slider_1'><img id='CMultiDDL_" + this.name + "_slider_slider_3' src='" + this.pathImage + "ddl_slider_bottom.jpg'></div></td></tr>";
			t += "</table>";
		
		t += "</div></td></tr>";
		t += "<tr><td id='CMultiDDL_" + this.name + "_slider_down_2'><img id='CMultiDDL_" + this.name + "_slider_down_20' src='" + this.pathImage + "ddl_slider_arrow_down.jpg'></td></tr></table></td>";		
	}
		
	box = "<table border='0' style='border:solid 1px black' cellpadding='0' cellspacing='0' width='" + this.width + "' height='" + (len * this.rowHeight) + "'>";						
	box += "<tr><td valign='top' width='100%'><div id='CMultiDDL_" + this.name + "_list_option'></div></td>" + t + "</tr></table>";
	this.isopen = true;
	document.getElementById('CMultiDDL_' + this.name + '_list').innerHTML = box;		
	if(this.options.length > this.maxRow)
	{
		document.getElementById('CMultiDDL_' + this.name + '_slider_slider').onmousemove = this.onmousemove;
		document.getElementById('CMultiDDL_' + this.name + '_slider_slider_td').onmousemove = this.onmousemove;
	}
	this.update();
}

function CMultiDDL_close()
{	
	this.isopen = false;
	document.getElementById('CMultiDDL_' + this.name + '_list').innerHTML = '';	
}

function CMultiDDL_addOption(text, value)
{
	var	len = this.options.length;
	this.options[len] = new Option(text, value);
	this.options[len].selected = false;	
}

function CMultiDDL_clearOption(set)
{
	if(set == undefined) set = false;
	var	len = this.options.length;
	for(var i=0; i < len; i++)
	{
		this.options[i].selected = set;			
	}
	this.textbox = '';
}

function CMultiDDL_range(from, to)
{
	for(var i=from; i <= to; i++)
	{	
		this.options[i].selected = true;			
	}
}
