var actb_timeOut = -1; // Autocomplete Timeout in ms (-1: autocomplete never time out)
var actb_lim = 15;     // Number of elements autocomplete can show (-1: no limit)
var actb_firstText = false; // should the auto complete be limited to the beginning of keyword?
/* ---- Variables ---- */

/* --- Styles --- */
var actb_bgColor = '#B0C4DE';
var actb_textColor = '#FFFFFF';

//SA var actb_hColor = '#000000';
var actb_hColor = '#FFFFFF'; //SA

var actb_fFamily = 'Verdana';
var actb_fSize = '11px';
var actb_hStyle = 'text-decoration:underline;font-weight="bold"';
/* --- Styles --- */

/* ---- Constants ---- */
var actb_keywords = new Array();
var actb_display = false;
var actb_pos = 0;
var actb_total = 0;
var actb_curr = null;
var actb_rangeu = 0;
var actb_ranged = 0;
var actb_bool = new Array();
var rows = new Array();
var cells = new Array();
var prevhighligted = 0;
var actb_pre = 0;
var actb_toid;
var actb_tomake = false;
/* ---- Constants ---- */


function VER5_Check() {
  var b = navigator.appName;
  if (b == "Netscape") this.b = "NS";
  else if (b == "Microsoft Internet Explorer") this.b = "IE";
  this.v = parseInt(navigator.appVersion);
  this.IE5 = (navigator.userAgent.indexOf('MSIE 5') > 0 || navigator.userAgent.indexOf('MSIE 6') > 0);
  this.NS5 = (this.b == "NS" && this.v == 5);
  this.VER5 = (this.IE5 || this.NS5);
}
is = new VER5_Check();

function actb_parse(n){
    var t = escape(actb_curr.value);
    var tobuild = '';
    var i;
    
    if (actb_firstText){
        var re = new RegExp("^" + t, "i");
    }else{
        var re = new RegExp(t, "i");
    }
    var p = n.search(re);
    
    for (i=0;i<p;i++){
        tobuild += n.substr(i,1);
    }
    tobuild += ""
    for (i=p;i<t.length+p;i++){
        tobuild += n.substr(i,1);
    }
    tobuild += "";
    for (i=t.length+p;i<n.length;i++){
        tobuild += n.substr(i,1);
    }
    return tobuild;
}

function buttonDown(e) {
if( ((is.NS5) ? e.which : event.button) != 1) return true;
tmp = (is.NS5) ? e.target.getAttribute("id") : event.srcElement.id;
if(! tmp)
return true;

if (tmp=="scdown") {actb_movedown(); actb_generate(); return;}
else if (tmp=="scup") {actb_moveup(); return;}
else if (tmp.indexOf('tat_td')>=0){
idx=parseInt(tmp.substring(6));
if (!idx) return;

document.forms['mapForm'].action = getLink(actb_keywords[idx]);
document.forms['mapForm'].txtSiteMap.value = '';
document.forms['mapForm'].submit();
}
return;
}

function actb_generate(){
    if (document.getElementById('tat_table')) document.body.removeChild(document.getElementById('tat_table'));
    a = document.createElement('table');
    a.cellSpacing='1px';
    a.cellPadding='2px';
    a.style.position='absolute';
    a.style.top = eval(curTop() + actb_curr.offsetHeight) + "px";
    a.style.left = curLeft() + "px";
    a.style.backgroundColor=actb_bgColor;
    a.id = 'tat_table';

    document.body.appendChild(a);
    var i;
    var first = true;
    var j = 1;

    var counter = 0;
	var highlight = false;
	rows = new Array();
	cells = new Array();
    for (i=0;i<actb_keywords.length;i++){
		highlight = false;
        if (actb_bool[i]){
            counter++;
            r = a.insertRow(-1);
            if (first && !actb_tomake){
                r.style.backgroundColor = actb_hColor;
				highlight = true;
                first = false;
                actb_pos = counter;
            }else if(actb_pre == i){
                r.style.backgroundColor = actb_hColor;
				highlight = true;
                first = false;
                actb_pos = counter;
            }else{
                r.style.backgroundColor = actb_bgColor;
            }
            r.id = 'tat_tr'+(i);
			rows[rows.length]=r;
            c = document.createElement("TD");
			c.setAttribute("id",'tat_td'+(i));
            cells[cells.length]=c;
            c.style.color = actb_textColor;
			if (highlight)
				c.style.color = actb_bgColor;
            c.style.fontFamily = actb_fFamily;
            c.style.fontSize = actb_fSize;
            c.appendChild(document.createTextNode(getExplanation(actb_keywords[i])));
            r.appendChild(c);
            c.onmousedown = buttonDown;
            j++;
        }
        if (j - 1 == actb_lim && j < actb_total){
            r = a.insertRow(-1);
            r.style.backgroundColor = actb_bgColor;
            rows[rows.length]=r;
            c = document.createElement("TD");
			c.setAttribute("id",'scdown');
            cells[cells.length]=c;
            c.style.color = actb_textColor;
            c.style.fontFamily = 'arial narrow';
            c.style.fontSize = actb_fSize;
            c.align='center';
            c.appendChild(document.createTextNode('\\/'));
            r.appendChild(c);
            c.onmousedown = buttonDown;
            break;
        }
    }
    actb_rangeu = 1;
    actb_ranged = j-1;
    actb_display = true;
    if (actb_pos <= 0) actb_pos = 1;

//    if (getAllMessagesDIV)
//       document.body.appendChild(getAllMessagesDIV());
}
function curTop(){
    actb_toreturn = 0;
    obj = actb_curr;
    while(obj){
        actb_toreturn += obj.offsetTop;
        obj = obj.offsetParent;
    }
    return actb_toreturn;
}
function curLeft(){
    actb_toreturn = 0;
    obj = actb_curr;
    while(obj){
        actb_toreturn += obj.offsetLeft;
        obj = obj.offsetParent;
    }
    return actb_toreturn;
}
function actb_remake(){
    if (document.getElementById('tat_table')) document.body.removeChild(document.getElementById('tat_table'));
    a = document.createElement('table');
    a.cellSpacing='1px';
    a.cellPadding='2px';
    a.style.position='absolute';
    a.style.top = eval(curTop() + actb_curr.offsetHeight) + "px";
    a.style.left = curLeft() + "px";
    a.style.backgroundColor=actb_bgColor;
    a.id = 'tat_table';
    document.body.appendChild(a);
    var i;
    var first = true;
    var j = 1;
	rows = new Array();
	cells = new Array();
    if (actb_rangeu > 1){
        r = a.insertRow(-1);
        r.style.backgroundColor = actb_bgColor;
        rows[rows.length]=r;
        c = document.createElement("TD");
        c.setAttribute("id",'scup');
        cells[cells.length]=c;
        c.style.color = actb_textColor;
        c.style.fontFamily = 'arial narrow';
        c.style.fontSize = actb_fSize;
        c.align='center';
        c.appendChild(document.createTextNode('/\\'));
        r.appendChild(c);
        c.onmousedown = buttonDown;
    }
    for (i=0;i<actb_keywords.length;i++){
        if (actb_bool[i]){
            if (j >= actb_rangeu && j <= actb_ranged){
                r = a.insertRow(-1);
                r.style.backgroundColor = actb_bgColor;
                r.id = 'tat_tr'+(i);
                rows[rows.length]=r;
                c = document.createElement("TD");
                c.setAttribute("id",'tat_td'+(i));
                cells[cells.length]=c;
                c.style.color = actb_textColor;
                c.style.fontFamily = actb_fFamily;
                c.style.fontSize = actb_fSize;
                c.appendChild(document.createTextNode(getExplanation(actb_keywords[i])));
                r.appendChild(c);
                c.onmousedown = buttonDown;
                j++;
            }else{
                j++;
            }
        }
        if (j > actb_ranged) break;
    }
    if (j-1 < actb_total){
        r = a.insertRow(-1);
        r.style.backgroundColor = actb_bgColor;
        rows[rows.length]=r;
        c = document.createElement("TD");
        c.setAttribute("id",'scdown');
        cells[cells.length]=c;
        c.style.color = actb_textColor;
        c.style.fontFamily = 'arial narrow';
        c.style.fontSize = actb_fSize;
        c.align='center';
        c.appendChild(document.createTextNode('\\/'));
        r.appendChild(c);
        c.onmousedown = buttonDown;
    }
}
function normalrow(i){
	if (rows[i] && rows[i].style)
       rows[i].style.backgroundColor = actb_bgColor;
	if (cells[i] && cells[i].style)
       cells[i].style.color = actb_hColor;
}
function highlightrow(i){
	if (prevhighligted>=0)
		normalrow(prevhighligted);
	if (rows[i] && rows[i].style)
       rows[i].style.backgroundColor = actb_hColor;
	if (cells[i] && cells[i].style)
       cells[i].style.color = actb_bgColor;
	prevhighligted = i;
}
function actb_goup(){
    if (!actb_display) return;
    if (actb_pos == 1) return;

    actb_pos--;
    if (actb_pos < actb_rangeu) actb_moveup();

	if(actb_pos == 1)
	    highlightrow(0);
	else if (actb_rangeu == 1)
	    highlightrow(actb_pos - 1);
	else if (actb_rangeu > 1)
	    highlightrow(actb_pos - actb_rangeu + 1);

    if (actb_toid) clearTimeout(actb_toid);
    if (actb_timeOut > 0) actb_toid = setTimeout("actb_removedisp()",actb_timeOut);
//if (showMsg) showMsg("actb_pos : " + actb_pos + "\nactb_rangeu : " + actb_rangeu + "\nactb_ranged : " + actb_ranged + "\ncells[actb_pos-1] : " );
}
function actb_godown(){
    if (!actb_display) return;
    if (actb_pos == actb_total) return;
    actb_pos++;
    if (actb_pos > actb_ranged) actb_movedown();

	if(actb_ranged>=actb_pos && actb_rangeu<3)
	    highlightrow(actb_pos-1);
	else if(actb_ranged==actb_pos && actb_rangeu>=3)
	    highlightrow(actb_lim);
		
    if (actb_toid) clearTimeout(actb_toid);
    if (actb_timeOut > 0) actb_toid = setTimeout("actb_removedisp()",actb_timeOut);

//if (showMsg) showMsg("actb_pos : " + actb_pos + "\nactb_rangeu : " + actb_rangeu + "\nactb_ranged : " + actb_ranged);
}
function actb_movedown(){
    actb_rangeu++;
    actb_ranged++;
    actb_remake();
}
function actb_moveup(){
    actb_rangeu--;
    actb_ranged--;
    actb_remake();
}
function actb_penter(){
    if (!actb_display) return;
    actb_display = 0;
    var word = '';
    var c = 0;
    for (var i=0 ; i <= actb_keywords.length; i++){
        if (actb_bool[i]) c++;
        if (c == actb_pos){
            word = actb_keywords[i];
            break;
        }
    }
    a = word;
    actb_curr.value = a;
    //actb_removedisp();
    document.forms['mapForm'].action = getLink(word);
    document.forms['mapForm'].txtSiteMap.value = '';
    document.forms['mapForm'].submit();
    return true;
}
function actb_removedisp(){
    actb_display = 0;
    if (document.getElementById('tat_table')) document.body.removeChild(document.getElementById('tat_table'));
    if (actb_toid) clearTimeout(actb_toid);
}
function actb_checkkey(evt){
    a = evt.keyCode;
    if (a == 38){ // up key
        actb_goup();
    }else if(a == 40){ // down key
        actb_godown();
    }else if(a == 13){
        actb_penter();
    }
}
function actb_tocomplete(sndr,evt,arr){
    if (arr) actb_keywords = arr;
    if (evt.keyCode == 38 || evt.keyCode == 40 || evt.keyCode == 13) return;
    var i;
    if (actb_display){ 
        var word = 0;
        var c = 0;
        for (var i=0;i<=actb_keywords.length;i++){
            if (actb_bool[i]) c++;
            if (c == actb_pos){
                word = i;
                break;
            }
        }
        actb_pre = word;//actb_pos;
    }else{ actb_pre = -1};
    
    if (!sndr) var sndr = evt.srcElement;
    actb_curr = sndr;
	
	actb_changed(sndr, arr);
}
function actb_changed(sndr, arr){
    if (sndr.value == ''){
        actb_removedisp();
        return;
    }
    var t = sndr.value;
    if (actb_firstText){
        var re = new RegExp("^" + t, "i");
    }else{
        var re = new RegExp(t, "i");
    }
    
    actb_total = 0;
    actb_tomake = false;
    for (i=0;i<actb_keywords.length;i++){
        actb_bool[i] = false;
        if (re.test(
        getExplanation( actb_keywords[i] )
        )){
            actb_total++;
            actb_bool[i] = true;
            if (actb_pre == i) actb_tomake = true;
        }
    }
    if (actb_toid) clearTimeout(actb_toid);
    if (actb_timeOut > 0) actb_toid = setTimeout("actb_removedisp()",actb_timeOut);
    actb_generate(actb_bool);
}
function getLink(word){
    start = word.indexOf("a href=\"");
    end = word.indexOf("\">");
    return word.substring(start + 8, end );
}
function getExplanation(word){
    start = word.indexOf("\">");
    end = word.indexOf("</a>");
    return word.substring(start + 2, end );
}
