var combodropimage = '/templates/news/template2images/dhtmlselect.gif';
var combodropimage_up = '/templates/news/template2images/dhtmlselect_up.gif';
var currentDHTMLSelectId = "";
var dhtmlSelectWait = false;
var dhtmlSelectWaitOldStyleOverFlow = "visible";

function dhtmlselect(selectid, optionwidth, onDoneFunction, chpX) {
    var selectbox = document.getElementById(selectid);
    var title = selectbox.title;
    if (title.length == 0) {
        for (var i = 0; i < selectbox.options.length; i++) {
            if (selectbox.options[i].selected) {
                title = selectbox.options[i].text;
                break;
            }
        }
    }
    document.write('<div id="dhtml_' + selectid + '" class="dhtmlselect" sid="' + selectid + '"><table cellpadding=0 cellspacing=0><tr><td><b>' + title + "</b></td>" + (combodropimage != "" ? '<td><img class="downimage" src="' + combodropimage + '" ></td>' : '') + '</tr></table></div>');
    document.write("<div id='dhtml_options_" + selectid + "' class='dhtmlselectdropdown'></div>");
    var optionsDiv = document.getElementById("dhtml_options_" + selectid);
    if (typeof optionwidth != "undefined") {
        optionsDiv.style.width = optionwidth + "px";
    }
    var topDiv = document.createElement("DIV");
    topDiv.style.height = "1px";
    topDiv.style.fontSize = "1px";
    topDiv.style.paddingLeft = 0;
    topDiv.style.paddingRight = 0;
    topDiv.style.paddingTop = 0;
    topDiv.style.paddingBottom = 0;
    topDiv.id = "dhtml_top_div_" + selectid;
    topDiv.innerHTML = "<table cellpadding=0 cellspacing=0 width='100%'><tr><td style='font-size: 1px'>&nbsp;</td><td style='font-size: 1px; border-top: 1px solid black'>&nbsp;</td><tr></table>"
    optionsDiv.appendChild(topDiv);
    for (var i = 0; i < selectbox.options.length; i++) {
        var optionDiv = document.createElement("DIV");
        optionDiv.className = selectbox.options[i].selected ? "selected" : "";
        optionDiv.innerHTML = selectbox.options[i].text;
        optionDiv.setAttribute("selected", selectbox.options[i].selected);
        optionDiv.setAttribute("value", selectbox.options[i].value);
        optionDiv.onmouseover = function() {
            this.style.backgroundColor = "#eeeeee";
        }
        optionDiv.onmouseout = function() {
            this.style.backgroundColor = "#ffffff";
        }
        if (typeof onDoneFunction != "undefined") {
            optionDiv.setAttribute("sid", selectid);
            optionDiv.tempFunction = onDoneFunction;
            optionDiv.onclick = clickOnDHTMLSelectOption;
        }
        optionsDiv.appendChild(optionDiv);
    }
    selectbox.style.display = "none";
    var dhtmlselectbox = document.getElementById("dhtml_" + selectid);
    dhtmlselectbox.setAttribute("chpX", chpX);
    dhtmlselectbox.onclick = function() {
        if (dhtmlSelectWait) {
            return;
        }
        showDHTMLSelect(this.getAttribute("sid"));
    }
}

function showDHTMLSelect(id) {
    if (!loadPageComplete) {
        setTimeout("showDHTMLSelect('" + id + "')", 100);
        dhtmlSelectWait = true;
        return;
    }
    dhtmlSelectWait = false;
    showSplashForDHTMLSelect();
    var optionsDiv = document.getElementById("dhtml_options_" + id);
    var dhtmlBox = document.getElementById("dhtml_" + id);
    dhtmlBox.className = "dhtmlselect_sel";
    dhtmlBox.getElementsByTagName("IMG")[0].src = combodropimage_up;
    optionsDiv.style.display = "block";
    var pos = getElementPosition(dhtmlBox, new ElementPosition(0, 0));
    optionsDiv.style.top = pos.y + dhtmlBox.offsetHeight;
    optionsDiv.style.left = (pos.x + (document.all ? parseInt(dhtmlBox.getAttribute("chpX")) : 0)) + "px";
    var table = optionsDiv.getElementsByTagName("DIV")[0].getElementsByTagName("TABLE")[0];
    table.rows.item(0).cells.item(0).style.width = dhtmlBox.offsetWidth - 2 - (dhtmlBox.offsetParent.offsetWidth-dhtmlBox.offsetWidth);
    currentDHTMLSelectId = id;
}

function clickOnDHTMLSelectOption() {
    var selId = this.getAttribute("sid");
    var dhtmlBox = document.getElementById("dhtml_" + selId);
    var dhtmlOptionsBox = document.getElementById("dhtml_options_" + selId);
    for (var i = 0; i < dhtmlOptionsBox.getElementsByTagName("DIV").length; i++) {
        dhtmlOptionsBox.getElementsByTagName("DIV")[i].className = "";
        dhtmlOptionsBox.getElementsByTagName("DIV")[i].setAttribute("selected", "false");
    }
    this.className = "selected";
    this.setAttribute("selected", "true");
    dhtmlBox.getElementsByTagName("TABLE")[0].rows.item(0).cells.item(0).innerHTML = this.innerHTML;
    hiddenOption(selId);
    this.tempFunction.call(this);

}

function hiddenOption(selId) {
    if (document.getElementById("dhtmlSelectSplash") != null) {
        document.getElementById("dhtmlSelectSplash").style.display = "none";
    }
    var dhtmlBox = document.getElementById("dhtml_" + selId);
    dhtmlBox.getElementsByTagName("IMG")[0].src = combodropimage;
    dhtmlBox.className = "dhtmlselect";
    var dhtmlBoxOption = document.getElementById("dhtml_options_" + selId);
    dhtmlBoxOption.style.display = "none";
    document.body.style.overflow = dhtmlSelectWaitOldStyleOverFlow;
}

function showSplashForDHTMLSelect() {
    if (document.getElementById("dhtmlSelectSplash") != null) {
        document.getElementById("dhtmlSelectSplash").style.display = "block";
        return;
    }
    var splashDiv = document.createElement("DIV");
    splashDiv.id = "dhtmlSelectSplash";
    splashDiv.style.backgroundImage = "url(/images/none.gif)";
    splashDiv.style.position = "absolute";
    splashDiv.style.top = 0;
    splashDiv.style.left = 0;
    splashDiv.style.width = getBodyWidth() + "px";
    splashDiv.style.height = getBodyHeight() + "px";
    splashDiv.innerHTML = "&nbsp;";
    splashDiv.onclick = function () {
        if (currentDHTMLSelectId != "") {
            if (document.getElementById("dhtmlSelectSplash") != null) {
                document.getElementById("dhtmlSelectSplash").style.display = "none";
                hiddenOption(currentDHTMLSelectId);
            }
        }
    }
    document.body.appendChild(splashDiv);
    dhtmlSelectWaitOldStyleOverFlow = document.body.style.overflow; 
    document.body.style.overflow = "hidden";
}
