/**
 * $Id: handbook.js 6834 2007-03-01 17:50:43Z alex $
 *
 * Copyright (C) 2006 Logic Land Ltd.
 */

// helper functions

function isUndef(o) {
    return (typeof(o) == 'undefined') || (o === '') || (o == null);
};

function isDef(o) {
    return !isUndef(o);
};

function find(d, f) {
    if (d.getElementById && isDef(d.getElementById(f))) {
        return d.getElementById(f);
    } else {
        if (d.forms.item) {
            for (var i = 0; d.forms.item(i) != null; ++ i) {
                if (d.forms.item(i).namedItem && d.forms.item(i).namedItem(f)) {
                    return d.forms.item(i).namedItem(f);
                }
                if (d.forms.item(i).elements && d.forms.item(i).elements[f]) {
                    return d.forms.item(i).elements[f];
                }
                for (var form in d.forms) {
                    if (d.forms[form].elements && d.forms[form].elements[f]) {
                        return d.forms[form].elements[f];
                    }
                }
            }
        }
    }
    return null;
};

// Interchange Manager

function InterchangeManager(remoteDocument, id) {
    this.remoteDocument = remoteDocument;
    this.id = id;

    this.getRemoteControl = function() {
        return find(this.remoteDocument, this.id);
    };

    this.getInterchangeRequest = function() {
        var control = this.getRemoteControl();
        return control.value;
    };

    this.setInterchangeResponse = function(value) {
        var control = this.getRemoteControl();
        control.value = value;
    };
}

function getInterchangeManager() {
    var doc = (window.opener || this.parent).document;
    var interchangeElement = find(doc, window.name);
    return interchangeElement.interchangeManager;
}


// Handbook Window

function HandbookWindow(id, url, width, height) {
    this.id = id;
    this.url = baseUrl + url;
    this.width = width;
    this.height = height;

    this.show = HandbookWindow_show;
    this.close = HandbookWindow_close;
    this.getInterchangeManager = HandbookWindow_getInterchangeManager;
}

function HandbookWindow_show() {
    var wnd = window.open('', '', 'scrollbars=yes,resizable=yes,width=' +
        this.width + ',height=' + this.height + ',top=100,left=100');
    wnd.name = this.id;
    wnd.location.href = this.url;
    wnd.focus();
    this.window = wnd;

    var interchangeElement = find(document, this.id);
    interchangeElement.interchangeManager = new InterchangeManager(document, this.id);
}

function HandbookWindow_close() {
    this.window.close();
}

function HandbookWindow_getInterchangeManager() {
    var interchangeElement = find(document, this.id);
    return interchangeElement.interchangeManager;
}

function closeHandbook() {
    window.close();
}

function isHandbook() {
    return ("" != window.name);
};

