var acHighLightedID = -1;
var acNormalFg = "#0066cc";
var acNormalBg = "#ffffff";
var acHighLightFg = "#ffffff";
var acHighLightBg = "#0066cc";
var acCounter = -1;
var acUrl = "";
var acForm = null;
var acField = null;
var acBox = null;
var acVisible = false;
var acCallCounter = 0;
var acDelay = 250;

function acSetup(url, input) {
   
    acUrl = url;
    acForm = input.form;
    acField = input;
    
    input.setAttribute("autocomplete", "off");
    input.onblur = acBlur;
    input.onkeydown = acFieldKeyDown;
    input.onmousedown = acBlur;    
    
    acBox = document.getElementById("acBox");
    
    acSetSize();
    
    window.onresize = acSetSize;
}

function acResponse(response) {	
	response = unescape(response);	
	if(response != "null" && response.length > 2) {
		acWrite(response);
		acShow();
	} else {
		acHideBox();
	}
}

function acShow() {
	
 	var iTop = getParentProps(acField, "offsetTop") + acField.offsetHeight;
 	var iLeft = getParentProps(acField, "offsetLeft"); 	
 	var iHeight = acCounter*18+45;
	
 	if(acVisible==false) { 	
 		fadeElement('acBox',95,0,0,iLeft,iTop,264,iHeight);
 		acVisible = true;
 	} else { 		
 		setElPosition('acBox',iLeft,iTop,264,iHeight);
 	} 	
}

function acHideBox() {
    acBox.style.visibility="hidden";    
    acVisible = false;
}

function acSetSize() {
    if (acBox) {
        acBox.style.top = getParentProps(acField, "offsetTop") + acField.offsetHeight + "px";
        acBox.style.left = getParentProps(acField, "offsetLeft") + "px";                   
    }
}

function acBlur(event) {
 
    if (!event && window.event) {
        event = window.event;
    }    
    acHideBox();
}

function acWrite(result) {
	
	var kwList = result.split("&");
	   
    while (acBox.hasChildNodes()) {
        acBox.removeChild(acBox.firstChild);
    }
    // user input
    iDiv = document.createElement("div");
    iDiv.onmouseover = new Function("omoUser", "acMouseEnter('User')");
    iDiv.onmousedown = new Function("omdUser", "acFormSubmit('" + acField + "')");
    iDiv.id = "acRowUser";
    iDiv.className = "acRow";
    iDiv.style.cursor = "pointer";        
    iDiv.innerHTML = "<div class='acItem'><div class='acLeftColumn'><nobr>" + acField.value + "&nbsp;&nbsp;</nobr></div><div class='acRightColumn'><nobr>" + strAC1 + "</nobr></div></div>";    
    acBox.appendChild(iDiv);

    // headline
    iDiv = document.createElement("div");
    iDiv.id = "acRowHelp";
    iDiv.className = "acRowHelp";
    iDiv.style.cursor = "pointer";
    iDiv.innerHTML = "<div class='acItem'><nobr><span style='color:#000000;font-family: Verdana, Arial;font-size:12px;font-weight:bold;'>" + strAC2 + "</span></nobr></div>";
    acBox.appendChild(iDiv);

    // autocomp values
    for(i = 0; i < kwList.length;i++) {
    	var kwValue = kwList[i].split("=");
    	
        iDiv = document.createElement("div");
        iDiv.onmouseover = new Function("omo" + i, "acMouseEnter(" + i + ")");
        iDiv.onmousedown = new Function("omd" + i, "acFormSubmit('" + kwValue[0] + "')");
        iDiv.id = "acRow" + i;
        iDiv.className = "acRow";        
        iDiv.style.cursor = "pointer";
        iDiv.innerHTML = "<div class='acItem'><nobr>" + kwValue[1] + "</nobr></div>";
        
        acBox.appendChild(iDiv);
    }
    acHighLightedID = -1;
    acCounter = kwList.length;	    
}

function acMouseEnter(id) {
    acRowSelect(id);
}

function acRowSelect(row) {
	
    if ((acHighLightedID > -1) || (acHighLightedID == 'User')) {
        rowDiv = document.getElementById("acRow" + acHighLightedID);
        rowDiv.style.backgroundColor = acNormalBg;
        rowDiv.style.color = acNormalFg;
    }
    acHighLightedID = row;
    rowDiv = document.getElementById("acRow" + row);
    if (rowDiv) {
        rowDiv.style.backgroundColor = acHighLightBg;
        rowDiv.style.color = acHighLightFg;
    }
}

function acFormSubmit(value) {      
    acField.value = value;
    acForm.submit();
}

function acSearch() {	
    acCallCounter++;
    setTimeout("acRequest()", acDelay);
}

function acRequest() {   
    if (acCallCounter > 0) acCallCounter--;
    if (acCallCounter > 0) return;
    if (acField.value.length < 2) {
        acHideBox();
        return;
    }
   
    try {	    
	    var processId = Math.round(Math.random() * 100); 	    
        var sParameter = acField.name+"="+encodeURI(acField.value);
	    ajSendRequest(acUrl, sParameter, 2, processId, "acResponse");
	    
	} catch (ex) {
        acHideBox();
    }
}

function acFieldKeyDown(event) {
    if (!event && window.event) {
        event = window.event;
    }
 
    switch (event.keyCode) {
        case 37: {// left            
            break;
        }
        case 39: {// right            
            break;
        }
        case 40: {// down            
            if (!acVisible) {
                setTimeout("acRequest()", 10);
            } else if (acCounter > 0) {
                if ((acHighLightedID < acCounter - 1) || (acHighLightedID == 'User')) {
                    if (acHighLightedID == -1) {
                        acRowSelect('User');
                    } else if (acHighLightedID == 'User') {
                        acRowSelect(0);
                    } else {
                        acRowSelect(acHighLightedID + 1);
                    }                    
                }
                if (event.cancelBubble) event.cancelBubble();
                break;
            }
        }
        case 38: {// up            
            if (acCounter > 0) {
                if (acHighLightedID >= 0) {
                    var rowIndex = acHighLightedID - 1;
                    if (rowIndex < 0) {
                        acRowSelect('User');
                    } else {
                        acRowSelect(acHighLightedID - 1);
                    }                    
                }
                if (event.cancelBubble) event.cancelBubble();
                break;
            }
        }
        case 33: {// PgUp            
            if (acVisible && (acCounter > 0)) {
                acRowSelect(0);
                if (event.cancelBubble) event.cancelBubble();                
                break;
            }
        }
        case 9: //TAB
        case 34: {// PgDn            
            if (acVisible && (acCounter > 0)) {
                acRowSelect(acCounter - 1);
                if (event.cancelBubble) event.cancelBubble();
                break;
            }
        }
        case 35: {// End            
            if (acVisible && (acCounter > 0)) {
                acRowSelect(acCounter - 1);
                if (event.cancelBubble) event.cancelBubble();
                break;
            }
        }
        case 36: {// Home            
            if (acVisible && (acCounter > 0)) {
                acRowSelect(0);
                if (event.cancelBubble) event.cancelBubble();
                break;
            }
        }
        case 13: {// Enter            
            if (acVisible && (acHighLightedID >= 0)) {
                acHideBox();
                var row = document.getElementById("acRow" + acHighLightedID);
                if (event.cancelBubble) event.cancelBubble();
                if (row) {
                    var fx = row.onmousedown;
                    fx();
                }
            }
            break;
        }
        case 27: {// Escape            
            if (acVisible) {
                acRowSelect(-1);
                acHideBox();
                if (event.cancelBubble) event.cancelBubble();                
                backupValue = acField.value;
                setTimeout("acField.value = backupValue", 180);
            }
            break;
        }
        default: {
            acSearch();
        }
    }
}