/*  SBcode.js
*   by Giorgio Braga. v1.0 050908
*   Copyright (c) 2005 Giorgio Braga. All Rights Reserved.
*/
//searchFilter define filtro de búsqueda en tiempo de ejecución
var searchFilter;
//
var SBok;
with (navigator) {
    var SBisIE = navigator.appName=="Microsoft Internet Explorer";
    SBok = SBisIE && userAgent.indexOf("MSIE 6.0")>0;
    SBok = SBok || (product=="Gecko" && appVersion.substring(0,1)=="5");
}
var SBZindex = 0;
var SBoxes = [];
var SBMore = String.fromCharCode(9);

if (window.addEventListener){
    window.addEventListener("load", SBcheck, false);
    window.addEventListener("resize", SBresize, false);
} else if (window.attachEvent){
    window.attachEvent("onload", SBcheck);
    window.attachEvent("onresize", SBresize);
} else {
    alert("SuggestBoxes disabled!");
} 

function SBcheck() {
    if (!SBok) {
        return;
    }
    var spans = document.getElementsByTagName("SPAN");
    for (var i=0;i<spans.length;i++) {
        if (spans[i].getAttributeNode("suggest")) {
            var info = SBinfo[spans[i].id];
            eval("ctrl = "+info.type+"Box(spans[i])");
            for (var k in info) {
                if (k!="type") {
                    if (k=="style") {
                        for (var s in info[k]) {
                            eval("ctrl.style."+s+"=\""+info[k][s]+"\"");
                        }
                    }
                    else {
                        eval("ctrl."+k+"=\""+info[k]+"\"");
                    }
                }
            }
        }
    }
}

function SBresize() {
    for (var i=0;i<SBoxes.length;i++) {
        SBoxes[i].style.height = SBsetH(SBoxes[i]);
    }
}

function SBelement(tag,parent,before) {
    var x = document.createElement(tag);
	if (before) {
		parent.insertBefore(x,before);
	}
	else {
		parent.appendChild(x);
	}
    return x;
}

function SBsetH(el) {
	retVal = el.scrollHeight;
	var SB = el.offsetParent;
	if (SB.opts && SB.opts.length) {
		var each = retVal/SB.opts.length;
        var extra = 0;
        if (SB.lst.scrollWidth>SB.clientWidth) {
            extra = 16;
            retVal += extra;
        }
	}
	else {
		return retVal;
	}
    var height = SBisIE?document.body.clientHeight:self.innerHeight;
	var free = height-(el.absTop-document.body.scrollTop)-4;
	if (SB.opts.length<SB.SB.size) {
		if (retVal<free) {
			retVal = "";
		}
		else {
			retVal = Math.floor(free/each)*each+extra;
		}
	}
	else {
		if (retVal>free) {
			retVal = Math.min(SB.SB.size,Math.floor(free/each))*each+extra; 
		}
	}
    if (retVal) {
        retVal+=SBisIE?2:0;
        retVal = Math.min(retVal,free);
    }  
	return (retVal);
}
       
var httpObjs = [];

function SBnoFn() {}

function SBdata(url,callback,noRand) {
    if (!window.XMLHttpRequest) { 
        window.XMLHttpRequest = function() { 
            var xmlHttp; 
            try { 
                xmlHttp = new ActiveXObject("Msxml2.XMLHTTP.4.0"); 
                return xmlHttp;
            } 
            catch (ex) {}
            try { 
                xmlHttp = new ActiveXObject("MSXML2.XMLHTTP"); 
                return xmlHttp;
            } 
            catch (ex){}
            try { 
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); 
                return xmlHttp;
            } 
            catch (ex) {}
            return null;
        }
    }
    if (XMLHttpRequest) {
        if( !noRand ) { url += ( ( url.indexOf('?') + 1 ) ? '&' : '?' ) + ( new Date() ).getTime(); }
        callback = callback || "SBnoFn";
        var n = httpObjs.length;
        httpObjs[n] = new XMLHttpRequest(); 
        httpObjs[n].onreadystatechange = new Function('if( httpObjs['+(n)+'].readyState == 4 && httpObjs['+(n)+'].status < 300 ) {'+callback+'(httpObjs['+(n)+'].responseText); }' );
        httpObjs[n].open("GET", url, true);
		httpObjs[n].send(null);
		return true;
    }
}
function SBrnm(a,directives) {
    for (i=0;i<a.length;i++) {
        for (var d in directives) {
            with (a[i]) {
                a[i][d] = eval(directives[d]);
            }
        }
    }
    return a;
}

function suggestBox(parent) {
    var obj = SB(parent,true);
    return obj;
}

function SB(parent,suggest) {
    if (!SBisIE) document.body.focus = function() {}
    var x = SBelement("SPAN",parent);
    obj = SBelement("DIV",document.body);
    obj.id = parent.id;
    parent.id = "";
    parent.control = obj;
    obj.suggest = suggest;
    obj.container = parent;
    x.id = "SB"+x.sourceIndex
    x.align = "left";
    x.SB = obj;
    obj.ph = SBelement("IMG",parent);
    obj.ph.absPosition = SBabs; 
    with (obj.ph.style) {
        width = 1;
        height = 1;
        visibility = "hidden";
    }
//		obj.ph.style["z-index"] = 10;
    obj.ph.align = "absmiddle";
    obj.SB = x;
    obj.key = "";
    obj.block = "";
    with (obj.style) {
        display = "none";
        if (SBisIE) {
            fontFamily = parent.currentStyle.fontFamily;
            fontSize = parent.currentStyle.fontSize;
            fontWeight = parent.currentStyle.fontWeight;
            position = "static";
            top = 0;
            left = 0;
        }
        else {
            fontFamily = window.getComputedStyle(parent,null).fontFamily;
            fontSize = window.getComputedStyle(parent,null).fontSize;
            fontWeight = window.getComputedStyle(parent,null).fontWeight;
        }
    }
    obj.size = 11;
    obj.dataSrc = [];
    obj.value = null;
    obj.style.obj = obj;
    obj.optSelBg = "highlight";
    obj.optSelFg = "highlighttext";
    obj.optUnselBg = "window";
    obj.optUnselFg = "windowtext";
    obj.curOptBg = "white";
    obj.curOptFg = "black";
    obj.curOptBorder = "2px inset threedhighlight";
    obj.onchange = function() {}
    if (suggest) {
        x.inp = SBelement("INPUT",x);
        with (x.inp.style) {
            position = "absolute";
            top = 0;
            left = 0;
            width = "200px";
            padding = 1;
            border = "1px solid gray";
            overflow = "hidden";
            backgroundColor = x.curOptBg;
            cursor = "default";
            if (SBisIE) {
                fontFamily = parent.currentStyle.fontFamily;
                fontSize = parent.currentStyle.fontSize;
                fontWeight = parent.currentStyle.fontWeight;
            }
            else {
                fontFamily = window.getComputedStyle(parent,null).fontFamily;
                fontSize = window.getComputedStyle(parent,null).fontSize;
                fontWeight = window.getComputedStyle(parent,null).fontWeight;
            }
        }
        x.inp.SB = obj;
        x.inp.onkeydown = function(e) {
//searchFilter define filtro de búsqueda en tiempo de ejecución
						searchFilter = document.getElementById('select' + parent.title).value;
            var k = SBisIE?event.keyCode:e.keyCode;
            var SB = this.SB.SB;
            var curOpt = SB.hiOpt;
            switch (k) {
            case 38:
                if (curOpt && curOpt.previousSibling) {
                    SB.hili(curOpt.previousSibling);
                }
                break;
            case 40:
                if (curOpt) {
                    if (curOpt.nextSibling) { 
                        SB.hili(curOpt.nextSibling);
                    }
                }
                else {
                    SB.hili(SB.lst.firstChild);
                }
                break;
            case 13:
                SB.hiOpt.onmousedown();
                break;
            default:
                if (!SBisIE) {
                    if (k==8) {
                        var val = this.value.substring(0,this.value.length-1);
                    }
                    else {
                        var val = this.value+String.fromCharCode(k);
                    }
                    this.SB.value = "";
                    if (val) {
                        curSuggest = this.SB;
                        var like = this.SB.likeMatch.split(",");
                        with (this.SB) {
                            var sz = parseFloat(size)+1
                            if (key) {
                                var URL = searchURL+"?key="+key+"&val="+val+"&rows="+sz;
                                }
                            else {
                                var URL = searchURL+"?tbl="+table+"&cols="+columns+"&like="+likeMatch+"&val="+val+"&ord="+order+"&rows="+sz+"&filter="+searchFilter;
                            }
document.form1['hdkeyword' + parent.title].value = this.text;
document.form1['hdcui' + parent.title].value = this.value;
//FOR MOZILLA
                            block = parseFloat(size);
                            SBdata(URL,"SBresult")
                        }
                    }
                    else {
                        this.SB.SB.lst.style.display = "none";
                    }
                }
            }
        }
        x.inp.onpropertychange = function() {
						searchFilter = document.getElementById('select' + parent.title).value;
            if (event.propertyName=="value"&& this.SB.searchURL) {
                if (!this.selected) {
                    this.SB.value = "";
                    if (this.value) {
                        curSuggest = this.SB;
                        with (this.SB) {
                            var sz = parseFloat(size)+1
                            if (key) {
                                var URL = searchURL+"?key="+key+"&val="+this.value+"&rows="+sz;
                                }
                            else {
                                var URL = searchURL+"?tbl="+table+"&cols="+columns+"&like="+likeMatch+"&val="+this.value+"&ord="+order+"&rows="+sz+"&filter="+searchFilter;
                            }
document.form1['hdkeyword' + parent.title].value = this.text;
document.form1['hdcui' + parent.title].value = this.value;
//FOR IE
                            block = parseFloat(size);
                            SBdata(URL,"SBresult")
                        }
                    }
                    else {
                        this.SB.SB.lst.style.display = "none";
                    }
                }
                this.selected = false;
            }
        }
        x.SB.ph.style.height = x.inp.offsetHeight;
        obj.topEl = x.inp;
    }
    x.SB.ph.absPosition();
    with (x.style) {
        position = "absolute";
        fontFamily = x.SB.style.fontFamily;
        fontSize = x.SB.style.fontSize;
        top = x.SB.ph.absTop;
        left = x.SB.ph.absLeft;
        width = "200px";
    }
    x.lst = SBelement("DIV",x);
    if (x.sel) x.sel.lst = x.lst;
    with (x.lst.style) {
        position = "absolute";
        top = x.SB.ph.offsetHeight;
        left = 0;
        border = "1px solid black";
        display = "none";
        backgroundColor = obj.optUnselBg;
        overflow = "auto";
    }
    SBoxes.push(x.lst);
    x.lst.chkHeight = function() {return SBsetH(this)};
    x.lst.chkWidth = function() {
        var max = 0;
        var SB = this.offsetParent;
        var xx = [];
        for (var i=0;i<SB.opts.length;i++) {
            max = Math.max(max,SB.opts[i].offsetWidth);
        }
        if (!SBisIE && SB.SB.suggest) {
            max -= 20;
        }
        for (var i=0;i<SB.opts.length;i++) {
            SB.opts[i].style.width = max;
        }
    }
    x.opts = [];
    obj.newOption = function(dati,before) {
        var text = dati.text;
        var value = dati.value;
        var selected = dati.selected;
        var icon = dati.icon;
        var SB = this.SB;
        SB.lst.style.width = this.style.width;
        if (SB.opts.length==0) {
            selected = true; 
        }
        if (SB.opts.length==this.size) {
            for (var i=0;i<this.size;i++) {
                SB.opts[i].style.paddingRight = "1px";
            }
        }
        var opt = SBelement("DIV",SB.lst,before);
        opt.appendChild(document.createTextNode(text));
        opt.value = value;
        opt.text = text;
        opt.selected = selected;
        opt.SB = x;
        SB.opts.push(opt);
        with (opt.style) {
            cursor = "default";
            paddingLeft = "1px";
            whiteSpace = "nowrap";
            if (SB.opts.length<=this.size) {
                paddingRight = "18px";
            }
        }
        opt.style.backgroundColor = dati.bgColor || this.optUnselBg;
        opt.style.color = dati.fgColor || this.optUnselFg;
        opt.onmouseover = function() {
            this.SB.hili(this);
        }
        if (this.suggest) {
            opt.onmousedown = function() {
                if (this.value.substring(0,1)!=SBMore) {
                this.SB.inp.selected = true;
                this.SB.inp.value = this.text;
//additional code for indexSuggest in Medline
document.form1['hdkeyword' + parent.title].value = this.text;
document.form1['hdcui' + parent.title].value = this.value;
//
//alert(document.form1['hdcui' + parent.title].value.toString() + ': '+document.form1['hdkeyword' + parent.title].value.toString());
//end additional code
                this.SB.SB.value = this.SB.parentNode.value = this.value;
                }
                else {
                    with (this.SB.SB) {
												searchFilter = document.getElementById('select' + parent.title).value;
                        var sz = parseFloat(size)+1
                        if (key) {
                            var URL = searchURL+"?key="+key+"&val="+this.value+"&rows="+sz+"&more="+this.SB.inp.value.substring(1);
                        }
                        else {
                            var URL = searchURL+"?tbl="+table+"&cols="+columns+"&like="+likeMatch+"&val="+this.SB.inp.value+"&ord="+order+"&rows="+sz+"&more="+this.value.substring(1)+"&filter="+searchFilter;
                        }
//												alert(URL);
document.form1['hdkeyword' + parent.title].value = this.text;
document.form1['hdcui' + parent.title].value = this.value;
                        block += (sz-1);
                        SBdata(URL,"SBresult")
                    }
                    
                }
                this.SB.lst.style.display = "none";
            }
        }
    }
    x.hili = function(opt) {
        this.hiOpt = opt;
        for (var i=0;i<this.opts.length;i++) {
            this.opts[i].style.backgroundColor = this.opts[i]==opt?this.SB.optSelBg:this.SB.optUnselBg || this.opts[i].style.bgColor;
            this.opts[i].style.color = this.opts[i]==opt?this.SB.optSelFg:this.opts[i].style.fgColor || this.SB.optUnselFg;
        }
    }
    x.setPosition = function() {
        if (this.SB.suggest) {
            var topEl = this.inp;
        }
        if (this.SB.style.position=="absolute") {
            with (this.SB.ph.style) {
                height = 0;
                width = 0;
            }
            with (this.style) {
                top = this.SB.style.top || 0;
                if (this.SB.style.right) {
                    left = this.parentElement.clientWidth - topEl.offsetWidth - parseFloat(this.SB.style.right);
                }
                else {
                    left = this.SB.style.left || 0;
                }
            }
        }
        else {
            with (this.SB.ph.style) {
                height = topEl.offsetHeight;
                width = topEl.offsetWidth;
            }
            this.SB.ph.absPosition();
            with (this.style) {
                top = this.SB.ph.absTop;
                left = this.SB.ph.absLeft;
            }
            this.lst.style.top = this.SB.ph.offsetHeight;
        }
        t = this.offsetTop+this.SB.ph.offsetHeight
        this.lst.absTop = t;
    }
    with (x.lst.style) {
        visibility = "hidden";
        display = "block";
    }
    x.setPosition();
    with (x.lst.style) {
        visibility = "inherit";
        display = "none";
    }
    obj.onpropertychange = function() {
        var topEl = this.SB.inp;
        switch (event.propertyName) {
        case "style.position":
            this.SB.setPosition();
            break;
        case "style.top":
            this.SB.style.top = this.style.top;
            this.SB.setPosition();
            break;
        case "style.left":
            this.SB.style.left = this.style.left;
            this.SB.setPosition();
            break;
        case "style.right":
            this.SB.style.right = this.style.right;
            this.SB.setPosition();
            break;
        case "style.width":
            this.SB.style.width = this.style.width;
            break;
        case "style.fontFamily":
            this.SB.lst.style.width = ""; 
            this.SB.style.fontFamily = topEl.style.fontFamily = this.style.fontFamily;
            this.SB.setPosition();
            break;
        case "style.fontSize":
            this.SB.lst.style.width = ""; 
            this.SB.style.fontSize = topEl.style.fontSize = this.style.fontSize;
            this.SB.setPosition();
            break;
        case "style.fontWeight":
            this.SB.lst.style.width = ""; 
            this.SB.style.fontWeight = topEl.style.fontWeight = this.style.fontWeight;
            this.SB.setPosition();
            break;
        case "style.curOptBg":
            topEl.style.backgroundColor = this.style.curOptBg;
            break;
        case "style.curOptFg":
            topEl.style.color = this.style.curOptFg;
            break;
        case "style.color":
            topEl.style.color = this.style.color;
            break;
        case "style.backgroundColor":
            topEl.style.backgroundColor = this.style.backgroundColor;
            break;
        case "style.curOptBorder":
            this.SB.lst.style.width = ""; 
            this.SB.inp.style.border = this.style.curOptBorder;
            this.SB.lst.style.left = parseFloat(this.style.curOptBorder)-1;
            break;
        case "style.border":
            this.SB.lst.style.width = "";
            with (topEl.style) {
                border = this.style.border;
            }
            this.SB.lst.style.left = parseFloat(this.style.borderWidth)-1;
            this.SB.setPosition();
            break;
            
        case "style.optSelBg":
            this.optSelBg = this.style.optSelBg;
            break;
        case "style.optUnselBg":
            this.optUnselBg = this.style.optUnselBg;
            break;
        case "style.optSelFg":
            this.optSelFg = this.style.optSelFg;
            break;
        case "style.optUnselFg":
            this.optUnselFg = this.style.optUnselFg;
            break;
        case "data": 
            this.SB.lst.innerHTML = "";
            this.SB.opts = [];
            this.SB.hiOpt = null;
            with (this.SB.lst.style) {
                display = "none";
                height = "";
                width = "";
            }
            if (this.data.length) {
                var sz = parseFloat(this.size);
                for (var i=0;i<Math.min(this.data.length,sz);i++) {
                    this.newOption(this.data[i]);
                }
                if (this.data.length>sz && this.more) {
                    this.newOption({text:this.more,value:SBMore+this.block});
                }
                this.SB.lst.style.display = "block";
                curList = this.SB.lst;
                document.onmousedown = function() {
                    if (event.srcElement!=curList) {
                        curList.style.display = "none";
                    }
                }
            }
            this.SB.lst.style.width = this.SB.clientWidth; 
            this.SB.lst.chkWidth();
            this.SB.lst.style.height = this.SB.lst.chkHeight();
            break;
        case "value":
            eval(this.action+"()");
            break
        }
    }                                                                         
    if (obj.watch) {
        obj.style.watch("position",
                function(id,oldval,newval) {
                    this.obj.SB.SB.style.position = newval;
                    this.obj.SB.setPosition();
                    return newval;
                }
        )
        obj.style.watch("top",
                function(id,oldval,newval) {
                    this.obj.SB.style.top = newval;
                    this.obj.SB.setPosition();
                    return newval;
                }
        )
        obj.style.watch("left",
                function(id,oldval,newval) {
                    this.obj.SB.style.left = newval;
                    this.obj.SB.setPosition();
                    return newval;
                }
        )
        obj.style.watch("right",
                function(id,oldval,newval) {
                    this.obj.SB.style.right = newval;
                    this.obj.SB.setPosition();
                    return newval;
                }
        )
        obj.style.watch("width",
                function(id,oldval,newval) {
                    this.obj.SB.style.width = newval;
                    this.obj.SB.lst.style.width = newval;
                    this.obj.topEl.style.width = newval;
                    return newval;
                }
        )
        obj.style.watch("fontFamily",
                function(id,oldval,newval) {
                    this.obj.SB.lst.style.width = ""; 
                    this.obj.SB.style.fontFamily = this.obj.topEl.style.fontFamily = newval;
                    this.obj.SB.setPosition();
                    return newval;
                }
        )
        obj.style.watch("fontSize",
                function(id,oldval,newval) {
                    this.obj.SB.lst.style.width = ""; 
                    this.obj.SB.style.fontSize = this.obj.topEl.style.fontSize = newval;
                    this.obj.SB.setPosition();
                    return newval;
                }
        )
        obj.style.watch("fontWeight",
                function(id,oldval,newval) {
                    this.obj.SB.lst.style.width = ""; 
                    this.obj.SB.style.fontWeight = this.obj.topEl.style.fontSize = newval;
                    this.obj.SB.setPosition();
                    return newval;
                }
        )
        obj.style.watch("backgroundColor",
                function(id,oldval,newval) {
                    this.obj.topEl.style.backgroundColor = newval;
                    return newval;
                }
        )
        obj.style.watch("color",
                function(id,oldval,newval) {
                    this.obj.topEl.style.color = newval;
                    return newval;
                }
        )
        obj.style.watch("border",
                function(id,oldval,newval) {
                    this.obj.SB.lst.style.width = "";
                    this.obj.topEl.style.border = newval;
                    this.obj.SB.lst.style.left = parseFloat(this.borderWidth)-1;
                    this.obj.SB.setPosition();
                    return newval;
                }
        )
        obj.watch("data",
                function(id,oldval,newval) {
                    this.SB.lst.innerHTML = "";
                    this.SB.opts = [];
                    this.SB.hiOpt = null;
                    this.SB.lst.style.display = "none";
                    if (newval.length) {
                        var sz = parseFloat(this.size);
                        for (var i=0;i<Math.min(newval.length,sz);i++) {
                            this.newOption(newval[i]);
                        }
                        if (newval.length>sz && this.more) {
                            this.newOption({text:this.more,value:SBMore+this.block});
                        }
                        this.SB.lst.style.display = "block";
                        curList = this.SB.lst;
                        document.body.onmousedown = function(e) {
                            if (!(e.target==curList || e.target.parentNode==curList)) {
                                curList.style.display = "none";
                            }
                        }
                    }
                    if (SBisIE || suggest) this.SB.lst.style.width = this.SB.clientWidth-parseFloat(this.topEl.style.borderWidth)*2;
                    this.SB.lst.chkWidth();
                    this.SB.lst.style.height = this.SB.lst.chkHeight();
                    return newval;
                }
        )
        obj.watch("value",
            function(id,oldval,newval) {
                this.value = newval;
                eval(this.action+"()");
                return newval;
                }
        )
    }
    return obj;
}

function SBabs() {
    var t = this.offsetTop;
    var l = this.offsetLeft;
    var obj = this.offsetParent;
    while (true) {
        var goOn = obj != document.body;
        goOn = goOn && obj.tagName !="DIV";
        if (SBisIE) goOn = goOn && obj.style.position != "absolute";
        if (goOn) {
            t += obj.offsetTop;
            l += obj.offsetLeft;
            obj = obj.offsetParent;
        }
        else break;
    }
    this.absTop = t;
    this.absLeft = l;
}

function SBresult(source) {
    eval(source);
    curSuggest.data = SBrnm(RS.rows,{text:curSuggest.optText,value:curSuggest.optValue});
}

function getId() {
//alert(document.getElementById("keyword1").value);
/*	with(document.form1){
		hdcui1.value = document.getElementById("keyword1").value;
		hdcui2.value = document.getElementById("keyword2").value;
		hdcui3.value = document.getElementById("keyword3").value;
	}*/
}