﻿/*
Name:     browser.js
Creation: 10-05-2006
LastEdit: 20-03-2007
Author:   Egbert van der Haring
Remarks:  defines functions used by a model browser
          NB: sendCall is defined in browser.aspx
*/

var oGlossaryCurrent = null;
var csGlossary = new circStack(10);
var nMaxHistory = 50;

function Dummy()
{
}

function SetBrowserKind(ddlOption)
{
    var message = "setKind:" + ddlOption.options[ddlOption.selectedIndex].value;
    sendCall(message,"tree");
}

function SetStartPoint(ddlStart)
{
    AddToHistory(ddlStart.options[ddlStart.selectedIndex].value);
    var message = "history:" + ddlStart.options[ddlStart.selectedIndex].value;
    sendCall(message,"jump");
}

function SetModel(ddlModels)
{
    ClearHistory();
    var message = "model:" + ddlModels.options[ddlModels.selectedIndex].value;
    sendCall(message,"model");
}

// called by callback when new model is opened
function GetLanguages()
{
    var message = "getLanguages";
    sendCall(message,"languages");
}

function ShowLanguages(message)
{
    var hlModel = document.getElementById("ctl00_cphTitle_hlModel");
    var lbModels = document.getElementById("ctl00_cphMain_ddlModels");
    var lbLanguages = document.getElementById("ctl00_cphMain_ddlLanguages");
    if (lbLanguages)
    {
        // clear list
        var nIndex = lbLanguages.options.length;
        while (nIndex>0)
        {
            nIndex -= 1;
            lbLanguages.options[nIndex] = null;
        }

        // add languages to list
        var strLangs = message.split('|');
        if (lbModels)
        {
            lbModels.value = strLangs[0];
            if (hlModel)
            {
                hlModel.innerHTML = lbModels.options[lbModels.selectedIndex].text;
            }
        }
        nIndex = 0;
        nLangs = 1;
        while (nLangs<strLangs.length)
        {
            lbLanguages.options[nIndex] = new Option(strLangs[nLangs],strLangs[nLangs]);
            nIndex += 1;
            nLangs += 1;
        }
    }
}

function SetLanguage(ddlLanguages)
{
    var message = "language:" + ddlLanguages.options[ddlLanguages.selectedIndex].value;
    sendCall(message,"jump");
}

// sends request to server to search for text
function Search()
{
    var tbSearch = document.getElementById("tbSearch");
    if (tbSearch.value != "")
    {
        var message = "search:" + tbSearch.value;
        AddToHistory(tbSearch.value);
        HideTokens();
        sendCall(message,"jump");
    }
}

// sends request to change type of returned info
function SelectInfo(strInfo)
{
    var message = 'setInfo:' + strInfo;
    sendCall(message,"info");
}

// sends request to return properties of current model
function GetProperties()
{
    var message = 'getProperties';
    sendCall(message,"info");
}

// sends request to return info for concept
function GetInfo(strConcept)
{
    if (strConcept != "")
    {
        var message = 'info:' + strConcept;
        sendCall(message,"info");
    }
}

// sends request to goto a concept
function GoTo(strConcept)
{
    var message = 'search:' + strConcept;
    HideTokens();
    sendCall(message,"jump");
}

// sends request to jump to concept and return info for concept
function Jump(strConcept)
{
    var message = 'jump:' + strConcept;
    sendCall(message,"model");
}

// sends request for glossary information
function GetGlossary(strEntry)
{
    var ifGlossary = document.getElementById("ifGlossary");
    ifGlossary.style.display = "block";
    if (csGlossary.push(oGlossaryCurrent))
    {
        ifGlossary.contentWindow.document.getElementById("title").innerHTML = "Loading...";
        ifGlossary.contentWindow.document.getElementById("back").style.display = 'inline';
    }
    
    oGlossaryCurrent = {id:strEntry, title:'loading', text:'loading'};
    var message = 'glossary:' + strEntry;
    sendCall(message,'glossary');
}

function BackGlossary()
{
    var oEntry = csGlossary.pop()
    if (oEntry != null)
    {
        oGlossaryCurrent = oEntry;
        var ifGlossary = document.getElementById("ifGlossary");
        if (csGlossary.isEmpty())
        {
            // hide back button
            ifGlossary.contentWindow.document.getElementById("back").style.display = 'none';
        }
        ifGlossary.contentWindow.document.getElementById("title").innerHTML = oGlossaryCurrent.title;
        ifGlossary.contentWindow.document.getElementById("content").innerHTML = oGlossaryCurrent.text;
    }
}

// sends request to return concepts that match the text
function GetTokens(e, strText)
{
    var key = getEventKey(e);
    if (key == 40) // down arrow
    {
        var lbTokens = document.getElementById("lbTokens");
        if ((lbTokens)
        && (lbTokens.options.length>0))
        {
            lbTokens.focus();
        }
    } else if (key == 13) // return key
    {
        document.getElementById("btnSearch").click();
    } else if (strText == "")
    {
        HideTokens();
    } else
    {
        var nIndex = lastIndexOf(strText, ' ');
        var strLast = strText.substr(nIndex,strText.length-nIndex);
        if (strLast == "")
        {
            HideTokens();
        }
        else
        {
            var message = 'tokens:' + strLast;
            sendCall(message,"tokens");
        }
    }
}

// hides tokens listbox
function HideTokens()
{
    var lbTokens = document.getElementById("lbTokens");
    if (lbTokens)
    {
        if (lbTokens.style.display != "none")
        {
            lbTokens.style.display = "none";
            document.getElementById("ifTree").style.top = lbTokens.style.top;
        }
    }
}

// shows tokens listbox
function ShowTokens(message)
{
    var lbTokens = document.getElementById("lbTokens");
    if (lbTokens)
    {
        // clear list
        var nIndex = lbTokens.options.length;
        while (nIndex>0)
        {
            nIndex -= 1;
            lbTokens.options[nIndex] = null;
        }

        // add tokens to list
        nIndex = 0;
        var strTokens = message.split('|');
        while (nIndex<strTokens.length)
        {
            lbTokens.options[nIndex] = new Option(strTokens[nIndex],strTokens[nIndex]);
            nIndex += 1;
        }

        if (lbTokens.style.display == "none")
        {
            var ifTree = document.getElementById("ifTree");
            lbTokens.style.display = "block";
            lbTokens.style.top = parseInt(ifTree.style.top)+"px";
            var nTree = parseInt(ifTree.style.top);
            nTree += parseInt(lbTokens.style.height) + 4;
            ifTree.style.top = nTree+"px";
        }
    }
}

// adds token to search text from tokens listbox
function AddToken(lbTokens)
{
    var tbSearch = document.getElementById("tbSearch");
    var strText = tbSearch.value;
    var nIndex = lastIndexOf(strText, ' ');
    tbSearch.value = strText.substr(0, nIndex) + lbTokens.options[lbTokens.selectedIndex].value;
    tbSearch.focus();
    HideTokens();
}

// handles keypress in tokens listbox
// return: adds token to listbox
// uparrow: moves foxus to search textbox
function TokensKeyDown(e, lbTokens)
{
    var key = getEventKey(e);
    if (key == 13) // return
    {
        AddToken(lbTokens);
    }
    else if ((key == 38)  // up arrow
    && (lbTokens.selectedIndex == 0))
    {
        var control = document.getElementById("tbSearch");
        control.focus();
    }
}

// handles callback from server
function HandleCallBack(message, context)
{
    if (message == "error")
    {
        document.location.replace("errorpage.aspx");
    } else if (message == "logout")
    {
        document.location.replace("logout.aspx");
    } else if (context == "info")
    {
        ShowInfo(message);
    } else if (context == "tree")
    {
        document.getElementById("ifTree").contentWindow.document.location.reload();
    } else if (context == "jump")
    {
        document.getElementById("ifTree").contentWindow.document.location.reload();
        ShowInfo(message);
        setTimeout("GetLanguages()", 100);
    } else if (context == "model")
    {
        document.getElementById("ifTree").contentWindow.document.location.reload();
        ShowInfo(message);
        setTimeout("GetLanguages()", 100);
    } else if (context == 'languages')
    {
        ShowLanguages(message);
    } else if (context == 'glossary')
    {
        ShowGlossary(message);
    } else if (context == "tokens")
    {
        ShowTokens(message);
    }
}

// shows info
function ShowInfo(strText)
{
    var strValue = strText.split('|');
    AddToHistory(strValue[0]);
    document.getElementById("ifInfo").contentWindow.document.body.innerHTML = strValue[1];
}

// hides glossary
function HideGlossary()
{
    csGlossary.clear();
    oGlossaryCurrent = null;
    document.getElementById("ifGlossary").style.display = 'none';
    document.getElementById("ifInfo").style.height = getAvailHeight(66);
}

// shows information from glossary in popup window
function ShowGlossary(message)
{
    var strValue = message.split('|');
    var ifGlossary = document.getElementById("ifGlossary");
    oGlossaryCurrent.title = strValue[0];
    oGlossaryCurrent.text = strValue[1];
    ifGlossary.contentWindow.document.getElementById("title").innerHTML = strValue[0];
    ifGlossary.contentWindow.document.getElementById("content").innerHTML = strValue[1];
    document.getElementById("ifInfo").style.height = getAvailHeight(200);
}

// clears history
function ClearHistory()
{
    var lbHistory = document.getElementById("ddlHistory");
    if (lbHistory)
    {
        var nIndex = lbHistory.options.length;
        while (nIndex>0)
        {
            nIndex -= 1;
            lbHistory.options[nIndex] = null;
        }
    }
}

// adds concept or search text to history
function AddToHistory(strItem)
{
    var lbHistory = document.getElementById("ddlHistory");
    if (lbHistory)
    {
        var nIndex = 0;
        while (nIndex < lbHistory.length)
        {
            if (lbHistory.options[nIndex].value == strItem)
            {
                lbHistory.options[nIndex] = null;
            }
            else
            {
                lbHistory.options[nIndex].selected = false;
                nIndex += 1;
            }
        }
        var option = document.createElement("option");
        option.text = strItem;
        option.value = strItem;
        option.selected = true;
        lbHistory.options.add(option,0);
        lbHistory.options[0].selected = true;
        if (lbHistory.length > nMaxHistory)
        {
            lbHistory.options.remove(nMaxHistory);
        }
    }
}

// called when callback fails
function OnError(message, context) {
   alert('An unhandled exception has occurred:\n' + message);
}

