//  Importeres til: contents <head>
//
//  Author: Thomas Himmelstrup
//
//  Methods to automate hyperLinks in tables
//
//-------------------------------------------------------------------------


var submenusplit   = '#';
var submenuindent  = '&nbsp&nbsp';
var submenuprename = 'submenu';



// -------- MenuItem Object ---------
function MenuItem(amenu, acaption, ahyperlink, amsg, areceiver, aparam, ainfo, astatus) {
    this.menu = amenu;
    this.caption = acaption;
    this.hyperlink = ahyperlink;
    this.msg = amsg;
    this.receiver = areceiver;
    this.param = aparam;
    this.info = ainfo;
    this.status = astatus;
    this.getHref = getHref;
    this.getTarget = getTarget;
    this.setMenuRec = setMenuRec;
    this.getStatus = getStatus;
    this.detectSubMenu = detectSubMenu;
    this.detectSubMenu();
}

function getHref() {
    if (!this.hyperlink) {
        return "javascript:top.perform(" +
                ((this.receiver) ? this.receiver: ((this.menu.receiver) ? this.menu.receiver: 'parent')) + ", '" +
                ((this.msg) ? this.msg: ((this.menu.msg) ? this.menu.msg: 'defaultMsg')) + "', '" +
                ((this.param) ? this.param: ((this.caption) ? this.caption: '')) + "', '" +
                ((this.info) ? this.info: '') +
                "');";
    } else
        return this.hyperlink;
}

function getTarget() {
    if (!this.hyperlink) return '_self'
    else return ((this.hypertarget) ? this.hypertarget: ((this.menu.hypertarget) ? this.menu.hypertarget: 'detail'));
}

function setMenuRec(amenurec) {
    this.caption = amenurec[menucaption];
    this.hyperlink = amenurec[menuhyperlink];
    this.msg = amenurec[menumsg];
    this.receiver = amenurec[menureceiver];
    this.param = amenurec[menuparam];
    this.info = amenurec[menuinfo];
    this.status = amenurec[menustatus];
    this.detectSubMenu();
}

function getStatus() {
    if (this.status) return this.status;
    return this.caption;
}

function detectSubMenu() {
    var s = new String(this.caption);
    if (s.indexOf(submenusplit) != -1) {
        c = s.split(submenusplit);
        this.issubmenu = true;
        this.submenuowner = this.menu.itemByName(c[0]);
        this.submenuowner.msg = 'toggleSubMenu';
        this.submenuowner.receiver = 'self';
        this.submenuowner.param = this.submenuowner.caption;
        this.caption = c[1];
    }
}

function toggleSubMenu(aparam) {
    if (top[submenuprename + window.name] == aparam) {
        top[submenuprename + window.name] = null;
    } else {
        top[submenuprename + window.name] = aparam;
    }
    window.location.reload();
}


// ------ Menu Object--------

function Menu(alist, asplit) {
    this.menuitems = new Array();
    this.insertMenuItem = insertMenuItem;
    this.addMenuItem = addMenuItem;
    this.insertMenuRec = insertMenuRec;
    this.addMenuRec = addMenuRec;
    this.insertHyperLink = insertHyperLink;
    this.addHyperLink = addHyperLink;
    this.itemByName = itemByName;
    this.asHtmlRows = asHtmlRows;
    this.asHtmlLink = asHtmlLink;
    this.asHtmlTable = asHtmlTable;
    this.linkid = 'menulink';
    this.textid = 'menutekst';
    this.width = "100%";
    this.cellpadding = '2';
    this.cellspacing = '0';
    this.border = '0';
    for (var i in alist) {
        var item = alist[i].split((asplit) ? asplit: '*');
        if (!item[menucondition] || eval(item[menucondition])) {
            this.addMenuRec(item);
            if (item[menutarget]) this.itemByName(item[menucaption]).hypertarget = item[menutarget];
        }
    }
}

function insertMenuItem(aindex, amenuitem) {
    if (aindex > this.menuitems.length) aindex = this.menuitems.length;
    if (aindex < 0) aindex = 0;
    for (var i = this.menuitems.length; i > aindex; i--) {
        this.menuitems[i] = this.menuitems[i - 1];
    }
    this.menuitems[aindex] = amenuitem;
}

function addMenuItem(amenuitem) {
    for (var i in this.menuitems) {
        if (this.menuitems[i].caption == amenuitem.caption) {
            return false;
        }
    }
    this.insertMenuItem(this.menuitems.length, amenuitem);
    return true;
}

function insertMenuRec(aindex, amenurec) {
    var mitem = new MenuItem(this);
    mitem.setMenuRec(amenurec);
    this.insertMenuItem(aindex, mitem);
}

function addMenuRec(amenurec) {
    var mitem = new MenuItem(this);
    mitem.setMenuRec(amenurec);
    return this.addMenuItem(mitem);
}

function insertHyperLink(aindex, acaption, ahyperlink, ahypertarget, astatus) {
    var mitem = new MenuItem(this, acaption, ahyperlink);
    mitem.hypertarget = ahypertarget;
    mitem.status = astatus;
    this.insertMenuItem(aindex, mitem);
}

function addHyperLink(acaption, ahyperlink, ahypertarget, astatus) {
    var mitem = new MenuItem(this, acaption, ahyperlink);
    mitem.hypertarget = ahypertarget;
    mitem.status = astatus;
    return this.addMenuItem(mitem);
}

function itemByName(aname) {
    for (var i in this.menuitems) {
        if (this.menuitems[i].caption == aname) { return this.menuitems[i]; }
    }
    return null;
}

function asHtmlTable(awindow) {
    awindow.document.writeln(
        '<table' +
        ((this.bgcolor) ? ' bgcolor="' + this.bgcolor + '"': '') +
        ((this.width) ? ' width="' + this.width + '"': '') +
        ((this.cellpadding) ? ' cellpadding="' + this.cellpadding + '"': '') +
        ((this.cellspacing) ? ' cellspacing="' + this.cellspacing + '"': '') +
        ((this.border) ? ' border="' + this.border + '"': '') +
        '>');
    this.asHtmlRows(awindow);
    awindow.document.writeln('</table>');
}

function asHtmlRows(awindow) {
    for (var i in this.menuitems) {
        var m = this.menuitems[i];
        if (!m.issubmenu) {
            awindow.document.writeln('<tr><td' +
            ((this.rowclass) ? ' class="' + this.rowclass + '"': '') +
            '>');
            this.asHtmlLink(awindow, i);
            awindow.document.writeln('</td></tr>');
        } else
        if (top[submenuprename + window.name] == m.submenuowner.caption) {
            awindow.document.writeln('<tr><td>' + submenuindent);
            this.asHtmlLink(awindow, i);
            awindow.document.writeln('</td></tr>');
        }

    }
}

function asHtmlLink(awindow, aitemindex) {
    var m = this.menuitems[aitemindex];
    if (m.hyperlink || m.msg) {
        awindow.document.writeln(
            '<a' +
            ((this.linkid) ? ' id="' + this.linkid + '"' : '') +
            ' href="' + m.getHref() + '" ' +
            'target="' + m.getTarget() + '" ' +
            '>' +
            m.caption +
            '</a>');
    } else {
        awindow.document.writeln(
            '<p' +
            ((this.textid) ? ' id="' + this.textid + '"' : '') +
            '>' + m.caption + '</p>');
    }
}



// ------ Globals --------
function hyperLink(awindow, acaption, ahyperlink, ahypertarget, astatus, alinkid) {
    var m = new Menu();
    m.linkid = alinkid;
    m.addHyperLink(acaption, ahyperlink, ahypertarget, astatus);
    m.asHtmlLink(awindow, 0);
}

function msgLink(awindow, acaption, amsg, areceiver, aparam, ainfo, alinkid) {
    var m = new Menu();
    m.linkid = alinkid;
    var mitem = new MenuItem(m, acaption, null, amsg, areceiver, aparam, ainfo);
    m.addMenuItem(mitem);
    m.asHtmlLink(awindow, 0);
}


top.msgLink = msgLink;
top.hyperLink = hyperLink;




