/*
 *	ComboBox
 *	By Jared Nuzzolillo
 *
 *	Updated by Erik Arvidsson
 *	http://webfx.eae.net/contact.html#erik
 *	2002-06-13	Fixed Mozilla support and improved build performance
 *
 *  Modfied by Michael T. Lederer for Atos Origin
 *  mtl@onemail.at
 *  2003-10-31 
 *  	- Customized appearance
 *		- Support hide on tab key
 *		- Support different styles (for a customizable length)
 *
 */

Global_run_event_hook = true;
Global_combo_array    = new Array();

Array.prototype.remove=function(dx)
{ 
    if(isNaN(dx)||dx>this.length){self.status='Array_remove:invalid request-'+dx;return false}
    for(var i=0,n=0;i<this.length;i++)
    {  
        if(this[i]!=this[dx])
        {
            this[n++]=this[i]
        }
    }
    this.length-=1
}

function ComboBox_make()
{
    var bt,nm;
    nm = this.name + "txt";
    nv = this.name + "val";

	if (this.leftborderimage != "")
	{
		var tmp = document.createElement("IMG");
		tmp.src = this.virtpath + this.leftborderimage; // AO: added virtual path
	/*  tmp.style.width = "3px";
		tmp.style.height = "18px"; <AO> */
		tmp.className = "combo-left-border";
		this.view.appendChild(tmp);
	}
	else
	{
		var tmp = document.createElement("IMG");
		tmp.src = this.virtpath + "/Images/spacer.gif";
		tmp.className = "combo-left-border";
		this.view.appendChild(tmp);
	}

/* <AO>	
    this.txtview = document.createElement("INPUT");
    this.txtview.type = "text";
    this.txtview.name = nm;
    this.txtview.id = nm;
    this.txtview.className = String("combo-input"+this.classaddon);
	//this.txtview.readOnly = true;
<AO> */
    this.txtview = document.getElementById(nm); // AO: getting generated element from the .Net-application
    this.view.appendChild(this.txtview);

	this.valcon = document.getElementById(nv); // AO: getting generated element from the .Net-application
    this.view.appendChild(this.valcon)

	tmp = document.createElement("IMG");
    tmp.src = this.virtpath + this.buttonimage;  // AO: added virtual path
    tmp.style.width =  this.buttonwidth; // "22px";
    tmp.style.height = this.buttonheight; // "18px";
	tmp.className = "combo-image-button";
    
	this.view.appendChild(tmp);
   	tmp.onfocus = function () { this.blur(); };
	tmp.onclick = new Function ("", this.name + ".toggle()");
}

function ComboBox_choose(realval,txtval)
{
    this.value = realval;
    var samstring = this.name+".view.childNodes[1].value='"+txtval+"'"
    // window.setTimeout(samstring,1)
    eval(samstring);
    this.valcon.value  = realval;
}

function ComboBox_mouseDown(e)
{
    var obj,len,el,i;
    el = e.target ? e.target : e.srcElement;
    while (el.nodeType != 1) el = el.parentNode;
    var elcl = el.className;
    if(elcl.indexOf("combo-")!=0)
    {
				
        len=Global_combo_array.length
        for(i=0;i<len;i++)
        {
        
            curobj = Global_combo_array[i]
            
            if(curobj.opslist)
            {
                curobj.opslist.style.display='none'
            }
        }
    }
}

function ComboBox_handleKey(e)
{	
    var key,obj,eobj,el,strname;
    eobj = e;
    key  = eobj.keyCode;
    el = e.target ? e.target : e.srcElement;
    while (el.nodeType != 1) el = el.parentNode;
		
    elcl = el.className
    if(elcl.indexOf("combo-")==0)
    {
        if(elcl.split("-")[1]=="input")
        {		
            strname = el.id.split("txt")[0]	// MTL
            obj = window[strname];
			
            obj.expops.length=0
            obj.update();
            obj.build(obj.expops);
			
            if(obj.expops.length==1&&obj.expops[0].text=="(No matches)"){}//empty
            else {
                obj.view.style.zIndex = ++ComboBox.prototype.COMBOBOXZINDEX // MTL
				obj.opslist.style.display='block';
			}
            obj.value = el.value;
            obj.valcon.value = el.value;
        }
     }
}

function ComboBox_update()
{
    var opart,astr,alen,opln,i,boo;
    boo=false;
    opln = this.options.length
    astr = this.txtview.value.toLowerCase();
    alen = astr.length
    if(alen==0)
    {
        for(i=0;i<opln;i++)
        {
            this.expops[this.expops.length]=this.options[i];boo=true;
        }
    }
    else
    {
        for(i=0;i<opln;i++)
        {
            opart=this.options[i].text.toLowerCase().substring(0,alen)
            if(astr==opart)
            {
                this.expops[this.expops.length]=this.options[i];boo=true;
            }
        }
    }
    if(!boo){this.expops[0]=new ComboBoxItem("(No matches)","")}
}


function ComboBox_remove(index)
{
    this.options.remove(index)
}

function ComboBox_add()
{
    var i,arglen;
    arglen=arguments.length
    for(i=0;i<arglen;i++)
    {
        this.options[this.options.length]=arguments[i]
    }
}

function ComboBox_build(arr)
{
    var str,arrlen
    arrlen=arr.length;
    str = '<table class="combo-list-width'+this.classaddon+'" cellpadding="0" cellspacing="0" border="0">';
    var strs = new Array(arrlen);
    for(var i=0;i<arrlen;i++)
    {
        strs[i] = '<tr>' +
			'<td class="combo-item" onClick="'+this.name+'.choose(\''+arr[i].value+'\',\''+arr[i].text+'\');'+this.name+'.opslist.style.display=\'none\';' + this.onitemchosen + ';"' +
			'onMouseOver="this.className=\'combo-hilite\';" onMouseOut="this.className=\'combo-item\'" >&nbsp;'+arr[i].text+'&nbsp;</td>' +
			'</tr>';
    }
    str = str + strs.join("") + '</table>'
    
    if(this.opslist){this.view.removeChild(this.opslist);}
    
    this.opslist = document.createElement("DIV")
    this.opslist.innerHTML=str;
    this.opslist.style.display='none';
    this.opslist.className = String("combo-list"+this.classaddon);
    this.opslist.onselectstart=returnFalse;
    this.view.appendChild(this.opslist);
}

function ComboBox_toggle()
{
    if(this.opslist)
    {
        if(this.opslist.style.display=="block")
        {
            this.opslist.style.display="none"
        }
        else
        {
            this.update();
            this.build(this.options);
            this.view.style.zIndex = ++ComboBox.prototype.COMBOBOXZINDEX
            this.opslist.style.display="block"
        }
    }
    else
    {
        this.update();
        this.build(this.options);
        this.view.style.zIndex = ++ComboBox.prototype.COMBOBOXZINDEX
        this.opslist.style.display="block"
    }
}

function ComboBox_tab(e) { // MTL - Copy of ComboBox_handleKey, changed behaviour for tabulator key
	var key,obj,eobj,el,strname;
    eobj = e;
    key  = eobj.keyCode;
    el = e.target ? e.target : e.srcElement;
    while (el.nodeType != 1) el = el.parentNode;
	
    elcl = el.className
    if(elcl.indexOf("combo-")==0)
    {
        if(elcl.split("-")[1]=="input")
        {		
            strname = el.id.split("txt")[0]
            obj = window[strname];
			
            obj.expops.length=0
            obj.update();
            obj.build(obj.expops);
			
			if(key==9) return false; // Tabulatortaste
									
            /*if(obj.expops.length==1&&obj.expops[0].text=="(No matches)"){}//empty
            else {
                obj.view.style.zIndex = ++ComboBox.prototype.COMBOBOXZINDEX
				obj.opslist.style.display='block';
			}
            obj.value = el.value;
            obj.valcon.value = el.value;*/
        }
     }
}

function ComboBox()
{
    if(arguments.length==0)
    {
        self.status="ComboBox invalid - no name arg"
    }

    this.name     = arguments[0];
    this.par      = arguments[1]||document.body;
    this.virtpath = arguments[2]||"";    // AO: new parameter 'virtpath'
	this.classaddon = arguments[3]||""; // MTL // AO: increased 2 to 3
	this.leftborderimage = arguments[4]||"/Images/comboleftborder.gif";
	if (this.leftborderimage == "-")
		this.leftborderimage = "";
	this.buttonimage = arguments[5]||"/Images/combobutton.gif";
	if (this.buttonimage == "-")
		this.buttonimage = "";
	this.buttonwidth = arguments[6]||"22px";
	if (this.buttonwidth == "")
		this.buttonwidth = "22px";
	this.buttonheight = arguments[7]||"18px";
	if (this.buttonheight == "")
		this.buttonheight = "18px";
    this.view     = document.createElement("DIV");
    this.view.style.position='absolute';
	this.view.className = "combo-div";
    this.options  = new Array();
    this.expops   = new Array();
    this.value    = "";

    this.build  = ComboBox_build;
    this.make   = ComboBox_make;
    this.choose = ComboBox_choose;
    this.add    = ComboBox_add;
    this.toggle = ComboBox_toggle;
    this.update = ComboBox_update;
    this.remove = ComboBox_remove;
	this.tab	= ComboBox_tab; // MTL

    this.make()
    this.txtview = this.view.childNodes[1]
    this.valcon  = this.view.childNodes[2]
    
    this.onitemchosen = arguments[8]||"void(0);";
    
    this.par.appendChild(this.view)

    Global_combo_array[Global_combo_array.length]=this;
    if(Global_run_event_hook){ComboBox_init()}
}

ComboBox.prototype.COMBOBOXZINDEX = 100 //change this if you must

function ComboBox_init() 
{
	if (document.addEventListener) {
		document.addEventListener("keydown", ComboBox_tab, false); // MTL
		document.addEventListener("keyup", ComboBox_handleKey, false );
		document.addEventListener("mousedown", ComboBox_mouseDown, false );
	}
	else if (document.attachEvent) {
		document.attachEvent("onkeydown", function () { ComboBox_tab(window.event); } ); // MTL
		document.attachEvent("onkeyup", function () { ComboBox_handleKey(window.event); } );
		document.attachEvent("onmousedown", function () { ComboBox_mouseDown(window.event); } );
	}
	
    Global_run_event_hook = false;
}

function returnFalse(){return false}

function ComboBoxItem(text,value)
{
    this.text  = text;
    this.value = value;
}

