// JavaScript Document

var req;
var strUrl;
var zOrder = 11;

var IE = document.all?true:false

if (!IE) document.captureEvents(Event.click)

document.onclick = hidePopup;


function hidePopup(e)
{
    popups = new Array();
    popups = document.getElementsByTagName('div');
    for (i = 0; i < popups.length; i++)
    {
        popId = new String(popups[i].getAttribute("id"));
        if (popId.indexOf('qs') == 0 || popId.indexOf('switch') == 0)
        {
            popups[i].style.display = 'none';
        }
    }
}


function keyUp(id)
{
    search = document.getElementById('input' + id).value;
    typeId = document.getElementById('type' + id).value;
    objId = document.getElementById('id' + id);
    if (search == '')
    {
        objId.value = '';
    }
	loadXMLDoc('qsearch.do?objectType='+typeId+'&search='+ encodeURIComponent(search)+'&language='+window.lang+'&id='+id);

    popups = new Array();
    popups = document.getElementsByTagName('div');
    for (i = 0; i < popups.length; i++)
    {
        popId = new String(popups[i].getAttribute("id"));
        if (popId != 'qs' + id && popId.indexOf('qs') == 0)
        {
            popups[i].style.display = 'none';
        }
    }

}


function confirmChanges(id)
{
    search = document.getElementById('input' + id);
    objId = document.getElementById('id' + id);

}


function loadXMLDoc(url)
{

    strUrl = url;
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest)
    {
        req = new XMLHttpRequest();
        if (url.indexOf('qsearch') == -1)
        {
            req.onreadystatechange = processReqChange;
        } else
        {
            req.onreadystatechange = processSearchReqChange;
        }
        req.open("GET", url, true);
        req.send(null);
        // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject)
    {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req)
        {
            if (url.indexOf('qsearch') == -1)
            {
                req.onreadystatechange = processReqChange;
            } else
            {
                req.onreadystatechange = processSearchReqChange;
            }
            req.open("GET", url, true);
            req.send();
        }
    }
}

function processReqChange()
{
    // only if req shows "complete"
    if (req.readyState == 4)
    {
        // only if "OK"
        if (req.status == 200 && (null != req.responseXML) && (null != req.responseXML.documentElement))
        {
            // ...processing statements go here...
            response = req.responseXML.documentElement;

            if (response.getElementsByTagName('itemid')[0].firstChild)
            {
                id = 1 * response.getElementsByTagName('itemid')[0].firstChild.data;
            } else
            {
                id = 1 * response.getElementsByTagName('itemid')[0].data.parentNode.textContent;
            }
            menudata = response.getElementsByTagName('menudata')[0];

            menucontainer = document.getElementById("menuitem" + id.toString());

            zOrder++;
            menucontainer.zIndex = zOrder;

            if (menudata.childNodes.length > 0)
            {
                containerCount = menucontainer.getElementsByTagName('UL').length;
                if (0 == containerCount)
                {
                    menucontainer = menucontainer.appendChild(document.createElement("UL"));
                } else
                {
                    menucontainer = menucontainer.getElementsByTagName('UL')[containerCount - 1];
                }

                menucontainer.className = "menu";
                menucontainer.setAttribute("style", "display:block");
            }

            for (i = 0; i < menudata.childNodes.length; i++)
            {
                node = menudata.childNodes[i];
                if (node.nodeName == "menuitem")
                {
                    itemId = node.getAttribute("id");
                    itemHref = node.getAttribute("link");
                    itemText = node.childNodes[0].nodeValue;
                    itemText = itemText.replace(/\./g, ". ");

                    if (null == document.getElementById("menuitem"+itemId))
                    {
                        itemNode = document.createElement("LI");
                        itemNode.setAttribute("id", "menuitem"+itemId);
                        itemNode.setAttribute("style", "display:block");

                        itemNode = menucontainer.appendChild(itemNode);
                        if (document.all)
                        {
                            itemNode.onmouseover = mouseOverHandler1;
                            itemNode.onmouseout = function()
                            {
                                //							this.className=this.className.replace("over", "");
                                nodeLI = this;
                                if (nodeLI.lastChild && nodeLI.lastChild.tagName == "UL")
                                {
                                    nodeLI.lastChild.style.display = "none";
                                }

                            }
                        } else
                        {
                            itemNode.onmouseover = mouseOverHandler2;
                        }


                        itemLinkNode = document.createElement("A");
                        itemLinkNode.setAttribute("href", itemHref);

                        itemLinkNode = itemNode.appendChild(itemLinkNode);
                        itemLinkNode.appendChild(document.createTextNode(itemText));
                    }

                }
            }

        } else
        {
            //			alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}

function processSearchReqChange()
{

    // only if req shows "complete"
    if (req.readyState == 4)
    {
        // only if "OK"
        if (req.status == 200)
        {
            // ...processing statements go here...
            response = req.responseXML.documentElement;

            id = strUrl.substr(strUrl.indexOf('&id=') + 4, strUrl.length - strUrl.indexOf('&id=') - 4);

            menudata = response.getElementsByTagName('menudata')[0];

            menucontainer = document.getElementById('qs' + id);
            menucontainer.innerHTML = '';

            if (menudata.childNodes.length > 0)
            {
                menucontainer.style.display = "block";

                for (i = 0; i < menudata.childNodes.length; i++)
                {
                    node = menudata.childNodes[i];
                    if (node.nodeName == "menuitem")
                    {
                        itemId = node.getAttribute("id");
                        itemText = node.childNodes[0].nodeValue;
                        menucontainer.innerHTML += '<a ' +
                                                   'style="display: block" ' +
                                                   'onClick="document.getElementById(\'id' + id + '\').value=' + itemId + ';' + "\n" +
                                                   'document.getElementById(\'input' + id + '\').value=this.innerHTML;document.getElementById(\'qs' + id + '\').style.display=\'none\';">' +
                                                   itemText + '</a><br>';
                    }
                }
                menucontainer.style.height = (15 * (i + 1) > 200)?200:(15 * (i + 1) > 200);
            }
        }
    }
}

//IE Handler
mouseOverHandler1 = function()
{
    hidePopup();
    this.className = "over";

    if (!this.lastChild || (this.lastChild.tagName != "UL") || (this.lastChild.getElementsByTagName("LI").length < 1))
    {
        url = 'submenu.do?' +
                (window.menuObjectType?'objectType=' +window.menuObjectType+'&':'') +
                'language=' + window.lang +
                '&object=' + (1 * this.getAttribute("id").replace("menuitem",""));
        loadXMLDoc(url);
    }


    for (i = 0; i < this.parentNode.childNodes.length; i++)
    {
        if (this.parentNode.childNodes[i].tagName == "LI")
        {
            nodeLI = this.parentNode.childNodes[i];
            if ((1 * nodeLI.getAttribute('id').replace("menuitem","")) != (1 * this.getAttribute('id').replace("menuitem","")))
            {
                nodeLI.className = "";
                if (nodeLI.lastChild.tagName == "UL")
                    nodeLI.lastChild.style.display = "none";
            } else
            {
                nodeLI.className = nodeLI.className.replace("over", "");
                nodeLI.className = "over";
                if (nodeLI.lastChild.tagName == "UL")
                    nodeLI.lastChild.style.display = "block";
            }
        }
    }


}

//Other Browsers
mouseOverHandler2 = function()
{
    hidePopup();
    if (!this.childNodes[1] || (this.childNodes[1].tagName != "UL"))
    {
        url = 'submenu.do?'+
                (window.menuObjectType?'objectType=' +window.menuObjectType+'&':'') +
                'language=' + window.lang +
                '&object=' + (1 * this.getAttribute("id").replace("menuitem",""));
        loadXMLDoc(url);
    }
    for (i = 0; i < this.parentNode.childNodes.length; i++)
    {
        nodeLI = this.parentNode.childNodes[i];
        if (nodeLI.lastChild && nodeLI.lastChild.tagName == "UL")
        {
            if ((1 * nodeLI.getAttribute('id').replace("menuitem","")) != (1 * this.getAttribute('id').replace("menuitem","")))
            {
                nodeLI.lastChild.style.display = "none";
            } else
            {
                nodeLI.lastChild.style.display = "block";

            }
        }
    }

}
startList = function()
{
    if (document.all && document.getElementById)
    {
        navRoot = document.getElementById("nav");
        for (i = 0; i < navRoot.childNodes.length; i++)
        {
            node = navRoot.childNodes[i];
            if (node.nodeName == "LI")
            {
                node.onmouseover = mouseOverHandler1;
                node.onmouseout = function()
                {
                    this.className = this.className.replace("over", "");
                    nodeLI = this;
                    if (nodeLI.lastChild && nodeLI.lastChild.tagName == "UL")
                    {
                        nodeLI.lastChild.style.display = "none";
                    }
                }
            }
        }
    } else
    {
        navRoot = document.getElementById("nav");
        for (i = 0; i < navRoot.childNodes.length; i++)
        {
            node = navRoot.childNodes[i];
            if (node.nodeName == "LI")
            {
                node.onmouseover = mouseOverHandler2;
            }
            node.onmouseout = function()
            {
                this.className = this.className.replace("over", "");
                nodeLI = this;
                if (nodeLI.lastChild && nodeLI.lastChild.tagName == "UL")
                {
                    nodeLI.lastChild.style.display = "none";
                }

            }

        }
    }
}

function ClearForm(formName)
{
    form = document.forms[formName];

    for (var loop = 0; loop < form.elements.length; loop++)
    {
        if ( form.elements[loop].name == "textValue" || form.elements[loop].name == "dictionaryValue" || ((form.elements[loop].name == "") && (form.elements[loop].type == "text")) )
        {
            form.elements[loop].value = "";
        }
    }
}

if(window.isChildObjectsPopupEnabled)
    window.onload = startList;
