/*
window.onkeydown = checkKeyCodeAuCo
function checkKeyCodeAuCo(e){
    AutoCompKC  = (window.event) ? event.keyCode : e.keyCode;
}
*/

window.onresize = autoCompleteOnResize
function autoCompleteOnResize(){
	var div = ge("div_AutoCompletion");
	var refEl = ge(globalAutoCompFID);
	if (div){
		if (div.display = 'block'){
			div.style.left = getLeft(refEl) + globalAutoCompDistanceX;
			div.style.top = getTop(refEl) + globalAutoCompDistanceY;
		}
	}
}

/*
window.onkeypress = function(evt){
    var keyCode;
    if (evt) {
        keyCode = evt.keyCode ? evt.keyCode :
        evt.charCode ? evt.charCode : evt.which;
    }
    else if (window.event) {
        keyCode = event.keyCode;
    } 
    if (keyCode == 13){
        if (globalAutoCompIsOpen)
            return false;
    }
    if (keyCode == 9){
        if (globalAutoCompIsOpen)
            return false;
    }
};*/

var globalAutoCompFID = "";
var globalAutoCompDistanceX = 0;
var globalAutoCompDistanceY = 16;
var globalAutoCompIsOpen = false;

function AutoCompletion() {
    var AutoCompObjName = "";
    var AutoCompFID = "";
    var AutoCompWidth = 0;
    var AutoCompStillAlive = false;
    var AutoCompKC = null;
    var AutoCompUrl = "";
    var AutoCompRowWidth = 0;
    var AutoCompRowWidthSum;
    var AutoCompRowName;
    var AutoCompRowAlign;
    var AutoCompRowText = "";
    var AutoCompRowHeight = 0;
    var AutoCompColText = 0;
    var AutoCompRowCount = 0;
    var AutoCompClassSelectRow = "ac_rowSelect";
    var AutoCompClassRow = "ac_row";
    var AutoCompClassTbl = "ac_tb";
    var AutoCompMaxHeight = 0;
    var AutoCompMaxWidth = 0;
    var AutoCompMaxRows = 0;
    var AutoCompDiv;
    var AutoCompDistanceY = 16;
    var AutoCompDistanceX = 0;
    var AutoCompSBarWidth = 20;
    var AutoCompColDiff = 0;
    var AutoCompShowAll = true;
    var AutoCompWriteAllRow = true; 
    var AutoCompMaxRowCount = 20;
    var AutoCompColValue = 0;
    var AutoCompSumRowHeight = 0;
    var AutoCompColSpan = 0;
    var AutoCompSuppress = false;
    var AutoCompSelectedCol = 0;
    var AutoCompReset = true;
    var AutoCompIsOpen = false;
    var AutoCompNoKeyUpDown = false;
    var AutoCompShowAllText = "alle Einträge";
    
	this.setObjName = function(sVal){
        AutoCompObjName = sVal;
    }
	
	this.setRefID = function(sVal){
        AutoCompFID = sVal;
        globalAutoCompFID = sVal;
    }
	this.getRefID = function(){
        return AutoCompFID;
    }
    
	this.setColText = function(sVal){
        AutoCompColText = sVal;
    }
	
	this.setColValue = function(sVal){
        AutoCompColText = sVal;
    }
	
	this.setDistanceY = function(sVal){
        AutoCompDistanceY = sVal;
        globalAutoCompDistanceY = sVal;
    }
    
	this.setDistanceX = function(sVal){
        AutoCompDistanceX = sVal;
        globalAutoCompDistanceX = sVal;
    }
    
	this.setRowHeight = function(sVal){
        AutoCompRowHeight = sVal;
    }
	
	this.setAutoCompMaxRowCount = function(sVal){
	    AutoCompMaxRowCount = sVal;
	}
	this.getAutoCompMaxRowCount = function(){
	    return AutoCompMaxRowCount;
	}
	
	this.setAutoCompSuppress = function(sVal){
	    AutoCompSuppress = sVal;
	}
	this.getAutoCompSuppress = function(){
	    return AutoCompSuppress;
	}
	
    this.onStillAlive = function(sVal){
        AutoCompStillAlive = sVal;
    }
    
	this.setColDiff = function(sVal){
        AutoCompColDiff= sVal;
    }
	
	this.setMaxRows = function(sVal){
        AutoCompMaxRows = sVal;
    }
	
	this.setClassSelectRow = function(sVal){
        AutoCompClassSelectRow = sVal;
    }
	
	this.setClassRow = function(sVal){
        AutoCompClassRow = sVal;
    }
	
	this.setClassTable = function(sVal){
        AutoCompClassTbl = sVal;
    }
	
    this.setAutoCompUrl = function(sVal){
        AutoCompUrl = sVal;
    }
     
    this.setNoKeyUpDown = function(sVal){
        AutoCompNoKeyUpDown = sVal;
    }
    
    this.setShowAllText = function(sVal){
        AutoCompShowAllText = sVal;
    }
    
    this.reset = function (){
		AutoCompRowText = "";
		AutoCompRowCount = 0;
		AutoCompShowAll = true;
		AutoCompWriteAllRow = true; 
	}
    
    this.setReset = function (sVal){
		AutoCompReset = sVal; 
	}
    
    this.clear = function(){
		AutoCompRowCount = 0;
        AutoCompRowText = "";
    }
    
    this.isOpen = function(){
	    return AutoCompIsOpen;
	}
    
	this.setShowAll = function(sVal){
		AutoCompSuppress = sVal;
		ge(AutoCompFID).focus();
		this.showAllAutoComp(sVal);
	}
	
	this.addRowWidth = function(sVal){
		if (AutoCompShowAll){
			AutoCompRowWidth = new Array();
			sVal += ";"
			var Arr = sVal.split(";")
			var Str = "";
			for (var i=0; i<Arr.length;i++)
				AutoCompRowWidth.push(Arr[i]);
		}
	}
	
	this.addRowName = function(sVal){
		if (AutoCompShowAll){
			AutoCompRowName = new Array();
			sVal += ";"
			var Arr = sVal.split(";")
			var Str = "";
			for (var i=0; i<Arr.length;i++)
				AutoCompRowName.push(Arr[i]);
		}
	}
	
	this.addRowAlign = function(sVal){
		if (AutoCompShowAll){
			AutoCompRowAlign = new Array();
			sVal += ";"
			var Arr = sVal.split(";")
			var Str = "";
			for (var i=0; i<Arr.length;i++)
				AutoCompRowAlign.push(Arr[i]);
		}
	}
	
	this.init = function (sVal){
		this.setRefID(sVal);
		this.createDiv();
	}
	
	this.setSelectedRow = function(sVal){
		if (ge("ac_row_"+sVal)){
			this.setRowClassNameAuCo("ac_row_"+sVal);
			ge("ac_row_"+sVal).scrollIntoView(true);
		}
    }
	
	this.createDiv = function(){
		var html = '';
		var div;
		html =  '<table id="tbl_AutoCompletion" cellpadding=0 cellspacing=0 border=0 class="'+AutoCompClassTbl+'">'
		html += '<tr><td id="td_AutoCompletion" class="ac_row"></td></tr></table>'
		
		div = document.createElement('DIV');
		div.id = 'div_AutoCompletion'; 
		div.style['position'] = 'absolute';
		div.style['display'] = 'none';
		
		div.innerHTML = html;
		
		AutoCompDiv = document.body.appendChild(div);
	}
	
	this.addRow = function(sVal){
		var colText = "";
		var colWidth = 0;
		var colSpan = 0;
		var selectColClass = AutoCompClassSelectRow;
		var colAlign = "";
		var colCount = 0;
		
		if ((AutoCompRowCount < AutoCompMaxRowCount) || (AutoCompSuppress)){
			AutoCompColSpan = 0;
			AutoCompRowWidthSum = 0;
			for (var i=0; i<AutoCompRowWidth.length;i++){
				colWidth = 0;
				if (AutoCompRowWidth[i]!='')
					colWidth = parseInt(AutoCompRowWidth[i]);
				if (AutoCompRowAlign[i]!='')
					colAlign = AutoCompRowAlign[i];
				AutoCompRowWidthSum += colWidth
				if (colWidth > 0){
					colText += '<td class="ac_td" id="c_'+i+'_'+AutoCompRowCount+'" style="width:'+AutoCompRowWidth[i]+'px"'
					if (colAlign != '')
						colText += ' '+ colAlign
					colText += '><div id="d_'+i+'_'+AutoCompRowCount+'" style="overflow:hidden;width:'+AutoCompRowWidth[i]+'px">&nbsp;<nobr>'+AutoCompRowName[i]+'</nobr>&nbsp;</div></td>'
					colSpan += 1;
				}
			}
			
			if (AutoCompRowCount > 0){
				AutoCompRowText += '<tr>';
					AutoCompRowText += '<td colspan='+colSpan+'><img src="/b.gif" width=1 height=1></td>'
				AutoCompRowText += '</tr>';
			}
			AutoCompColSpan = colSpan;
			
			AutoCompRowText += '<tr '
			if (AutoCompRowCount == 0)
				AutoCompRowText += 'class="'+selectColClass+'"'
			AutoCompRowText += ' id="ac_row_'+AutoCompRowCount+'" title="'+AutoCompRowName[AutoCompColText]+'" rowBez="'+AutoCompRowName[AutoCompColText]+'" rowVal="'+AutoCompRowName[AutoCompColValue]+'" style="cursor:pointer;height:'+AutoCompRowHeight+'px" onclick="'+AutoCompObjName+'.onClickAuCo(\''+AutoCompRowName[AutoCompColText]+'\',\'' + AutoCompRowName[AutoCompColValue] + '\');'+AutoCompObjName+'.onCloseAuCo();" onmouseover="'+AutoCompObjName+'.setRowClassNameAuCo(this.id)" onmouseout="'+AutoCompObjName+'.onStillAlive(false)">';
			AutoCompRowText += colText;
			AutoCompRowText += '</tr>';
			
			AutoCompRowCount += 1
			AutoCompSumRowHeight = AutoCompRowCount * AutoCompRowHeight;
		}
		else{
			AutoCompShowAll = false;
		}
		if ((!AutoCompShowAll) && (AutoCompWriteAllRow)){
			AutoCompRowText += '<tr>';
				AutoCompRowText += '<td class="ac_bg" colspan='+AutoCompColSpan+'><img src="/b.gif" width=1 height=1></td>'
			AutoCompRowText += '</tr>';
			
			AutoCompRowText += '<tr>';
				AutoCompRowText += '<td colspan='+AutoCompColSpan+' style="cursor:pointer;height:'+AutoCompRowHeight+'px" onclick="'+AutoCompObjName+'.setShowAll(true)">&nbsp;<b>'+ AutoCompShowAllText +'</td>'
			AutoCompRowText += '</tr>';
			AutoCompWriteAllRow = false;
		}
	}
	
    this.onClickAuCo = function(sText, sVal){
        var elAutoComp = ge(AutoCompFID);
        elAutoComp.value = sText;
		elAutoComp.focus();
        AutoCompStillAlive = false;
		this.setAutoCompValue(sVal);
    }
	
	this.setCol_0 = function(sVal){
		var temp;
		
		for (var i=0; i<AutoCompRowCount;i++){
			if (ge("c_"+AutoCompColDiff+"_"+i)){
				temp = parseInt(ge("c_"+AutoCompColDiff+"_"+i).style.width);
				temp = temp - sVal;
			
				ge("c_"+AutoCompColDiff+"_"+i).style.width = temp;
				ge("d_"+AutoCompColDiff+"_"+i).style.width = temp;
			}
		}
	}
	
	this.showDivAutoComp = function(KC){
		var divAutoComp = ge("div_AutoCompletion");
		var divScroll = ge("div_ac_erg");
		var lText = "";
		var lVal = "";
		var lTempCount = AutoCompRowCount;
            
		if (AutoCompShowAll)
			lTempCount -= 1;
			
		switch(KC){
			case 13: case 9:
				if (ge("ac_row_"+AutoCompSelectedCol)){
					if (ge("ac_row_"+AutoCompSelectedCol).getAttribute("rowShowAll"))
						this.setShowAll(true);
					else
						this.onClickAuCo(ge("ac_row_"+AutoCompSelectedCol).getAttribute("rowBez"), ge("ac_row_"+AutoCompSelectedCol).getAttribute("rowVal"));
					this.onCloseAuCo();
					AutoCompSelectedCol = 0;
				}
				break
			case 27:
				AutoCompSelectedCol = 0;
				this.onCloseAuCo();
				return false;
				break;
			case 38:
				if ((divAutoComp) && (!(AutoCompNoKeyUpDown))){
					if (AutoCompSelectedCol >= 0){
						AutoCompSelectedCol -= 1;
						if (AutoCompSelectedCol < 0)
							divAutoComp.style.display = "none";
						else{
							this.setSelectedRow(AutoCompSelectedCol);
						}
					}
				}
				break;
			case 40:
				if ((divAutoComp) && (!(AutoCompNoKeyUpDown))){
					if (AutoCompSelectedCol < lTempCount){
						if (AutoCompSelectedCol < 0)
							divAutoComp.style.display = "block";
						AutoCompSelectedCol += 1;
						this.setSelectedRow(AutoCompSelectedCol);
					}
				}
				break;
			default:
				if ((KC!=39) && (KC!=37)){
					AutoCompSelectedCol = 0;
					this.onShowDivAutoComp();
				}
				break;
		}
		AutoCompStillAlive = false;
    }
	
	this.onShowDivAutoComp = function(){
		if (!ge("div_AutoCompletion"))
			this.createDiv();
        var divAutoComp = ge("div_AutoCompletion");
        var tdAutoComp = ge("td_AutoCompletion");
        var elAutoComp = ge(AutoCompFID);
		elAutoComp.AutoComplete = "off";
		var tblAutoCompText = "";
		var divAutoCompErg = ge("div_ac_erg");
		var tblAutoCompErg = ge("tbl_ac_erg");
		//alert(AutoCompRowText)
		
		if (parseInt(AutoCompRowCount) > parseInt(AutoCompMaxRows)){
			AutoCompMaxHeight = AutoCompMaxRows * (AutoCompRowHeight+1) - 1;
			AutoCompMaxWidth = AutoCompRowWidthSum - AutoCompRowHeight;
		}
        else{
			AutoCompMaxHeight = AutoCompRowCount * (AutoCompRowHeight+1) - 1;
            //AutoCompMaxWidth = AutoCompRowWidthSum - 4;
		}
		//alert(AutoCompMaxHeight)
		
		tblAutoCompText = '<div id="div_ac_erg" style="overflow:auto;height:'+AutoCompMaxHeight+'px;width:'+AutoCompRowWidthSum+'px" onmouseover="'+AutoCompObjName+'.onStillAlive(true)" onmouseout="'+AutoCompObjName+'.onStillAlive(false)" onblur="'+AutoCompObjName+'.onCloseAuCo()">'
        tblAutoCompText += '<table id="tbl_ac_erg" width="100%" cellpadding=0 cellspacing=0 border=0>'
        tblAutoCompText += AutoCompRowText
        tblAutoCompText += '</table>'
		tblAutoCompText += '</div>'
		
        if (divAutoComp){
            tdAutoComp.innerHTML = tblAutoCompText;
			
			if (AutoCompRowCount > AutoCompMaxRows)
				this.setCol_0(AutoCompSBarWidth + 4);
			else
				this.setCol_0(8);
			
			
            divAutoComp.style.width = AutoCompRowWidthSum;
            divAutoComp.style.top = getTop(elAutoComp) + AutoCompDistanceY;
            divAutoComp.style.left = getLeft(elAutoComp) + AutoCompDistanceX;
            divAutoComp.style.display = "block";

        }
        AutoCompIsOpen = true;
        globalAutoCompIsOpen = true;
		AutoCompRowText = "";
		AutoCompRowCount = 0;
		AutoCompShowAll = true;
		AutoCompWriteAllRow = true; 
    }
	
    this.onCloseAuCo = function(){
        var divAutoComp = ge("div_AutoCompletion");
        if (!AutoCompStillAlive){
			if(divAutoComp){
				if (divAutoComp.style.display == "block"){
				    var element = ge(AutoCompFID);
		            var temp = element.value;
					//element.focus();
					if (document.all)
					    element.value = temp;
				}
	            divAutoComp.style.display = "none";
	            //this.clear();
	            //AutoCompRowText = "";
				this.afterCloseAutoComp();
				AutoCompRowText = "";
				AutoCompRowCount = 0;
				AutoCompShowAll = true;
				AutoCompWriteAllRow = true; 
				AutoCompIsOpen = false;
				globalAutoCompIsOpen = false;
			}
		}
    }
    
    this.setRowClassNameAuCo = function(sID){
        AutoCompStillAlive = true;
        for(var li=0;li<document.getElementsByTagName("tr").length;li++){
		    if (document.getElementsByTagName("tr")[li].id.indexOf("ac_row_")!=-1){
		        document.getElementsByTagName("tr")[li].className = AutoCompClassRow;
		    }
	    }
        ge(sID).className = AutoCompClassSelectRow
    }
    
    this.suppressKeycode = function(e){
        var eventKC  = (window.event) ? event.keyCode : e.keyCode;
		if ((eventKC == 38) || (eventKC == 40)){
			return false;
			alert(false)
		}
		else{
			return true;
		}
    }
    
    this.setAutoCompValue = function(sVal){
	    
    }

    this.afterCloseAutoComp = function(){
        
    }

    this.showAllAutoComp = function(sVal){
        
    }
}



/*
var objAutoComp = new AutoCompletion();

objAutoComp.setObjName("objAutoComp");

objAutoComp.setRefID("text1");
objAutoComp.setMaxRows(4);
objAutoComp.setRowHeight(25);
objAutoComp.setColText(1);
objAutoComp.setClassSelectRow('std_bgl')
objAutoComp.setClassRow('lay_arcon')
objAutoComp.addRowWidth('0;110;25');
objAutoComp.addRowAlign(';;align=right');

objAutoComp.addRowName('text1;text1;1');
objAutoComp.addRow();

objAutoComp.addRowName('text2;text2;2');
objAutoComp.addRow();

objAutoComp.addRowName('text3;text3;3');
objAutoComp.addRow();

objAutoComp.addRowName('text4;text4;4');
objAutoComp.addRow();

objAutoComp.addRowName('text5;text5;5');
objAutoComp.addRow();
*/
