// Build 10.00.9277.8
/// <reference path="QvAjax.js" />
if (!Qva.Mgr) Qva.Mgr = {}

function onclick_action(event) {
    if (this.disabled) return;
    var binder = Qva.GetBinder(this.binderid);
    if (!binder.Enabled) return;
    var params = {};
    if (this.Position != null) params.position = this.Position;
    params.action = "";
    params.clientsizeWH = Qva.GetViewportWidth() + ':' + Qva.GetViewportHeight();
    if (this.NeedCursor) {
        if (Qva.ContextMenu) {
            params.cursor = Qva.ContextMenu.X + ':' + Qva.ContextMenu.Y;
        } else {
            if (!event) event = window.event;
            params.cursor = (event.clientX + Qva.GetScrollLeft()) + ':' + (event.clientY + Qva.GetScrollTop());
        }
    }
    binder.Set (this.Action || this.ObjectId, params);
    Qva.HideContextMenu ();
}

function onclick_ContextClientAction(event) { 
    Qva.GetBinder(this.binderid).ContextClientAction (event, this); 
}

Qva.Mgr.show = function (owner, elem, name, prefix, condition) {
    if (!Qva.MgrSplit (this, name, prefix)) return;
    this.Condition = condition;
    owner.AddManager(this);
    this.Element = elem;
    owner.Append (this, this.Name, 'value');
}

Qva.Mgr.show.prototype.Paint = function(mode, node) {
    this.Touched = true;
    var show;
    if (mode == 'n') {
        show = (this.ModeIfNotEnabled == 'd');
    } else {
        var attr = node.getAttribute ('value');
        if (!attr) attr = node.getAttribute ('text');
        show = (attr == this.Condition);
    }
    this.Element.style.display = show ? '' : 'none';
}

Qva.Mgr.hide = function (owner, elem, name, prefix, condition) {
    if (!Qva.MgrSplit (this, name, prefix)) return;
    this.Condition = condition;
    this.Element = elem;
    owner.AddManager(this);
    owner.Append (this, this.Name, 'value');
}

Qva.Mgr.hide.prototype.Paint = function(mode, node) {
    this.Touched = true;
    var hide;
    if (mode == 'n') {
        if (this.Condition == null) {
            hide = (this.ModeIfNotEnabled != 'd');
        } else {
            hide = false;
        }
    } else {
        var attr = node.getAttribute ('value');
        if (!attr) attr = node.getAttribute ('text');
        hide = (attr == this.Condition);
    }
    this.Element.style.display = hide ? 'none' : '';
}

Qva.Mgr.disable = function (owner, elem, name, prefix) {
    this.Name = Qva.MgrMakeName (name, prefix);
    this.Element = elem;
    owner.AddManager(this);
}

Qva.Mgr.disable.prototype.Lock = Qva.LockDisabled;
Qva.Mgr.disable.prototype.Unlock = Qva.UnlockDisabled;
Qva.Mgr.disable.prototype.Paint = function(mode, node) {
    this.Touched = true;
    var element = this.Element;
    element.disabled = (mode != 'e');
    element.style.display = Qva.MgrGetDisplayFromMode (this, mode);
}

Qva.Mgr.background = function (owner, elem, name, prefix) {
    if (!Qva.MgrSplit (this, name, prefix)) return;
    owner.AddManager(this);
    this.Element = elem;
    elem.binderid = owner.binderid;
    this.AutoSize = elem.tagName == "DIV";
    owner.BackgroundMgr = this;
}

Qva.Mgr.background.prototype.PostPaint = function() {
    if (this.HasBkgImage) {
        var element = this.Element;
        var isBody = element === document.body;
        var offset = isBody ? Qva.GetAbsolutePageCoords(document.getElementById("PageContainer")).y : 0;
        var url = this.avq_url;
        if (this.AutoSize) {
            var width = getClientWidth(element);
            var height = isBody ? Qva.GetDocHeight() : getClientHeight(element);
            if (isNaN(width) || isNaN(height)) {
                if (isBody) {
                    width = parseInt(Qva.GetViewportWidth());
                    height = parseInt(Qva.GetViewportHeight());
                } else {
                    Assert(false);
                }
            }
            height -= offset;
            if (this.PageBinder.IsHosted && this.PageBinder.SetSize) {
                if (this.Height != height || this.Width != width) {
                    this.Height = height;
                    this.Width = width;
                    this.PageBinder.SetSize(this.PageBinder.Name || this.Name, this.Width + ":" + this.Height);
                }
            }
            url += '&width=' + escape(width);
            url += '&height=' + escape(height);
            url = Qva.AddProxyIfAny(url);
        }
        if (this.HasBkgImage) {
            this.appliedurl = url;
            element.style.backgroundImage = "url(" + url + ")";
        } else {
            this.appliedurl = "";
            element.style.backgroundImage = "";
        }
        if (isBody && this.backgroundPosition) {
            if (this.backgroundPosition.search("top") != -1) {
                if (IS_CHROME) {
                    element.style.backgroundPosition = "0px " + offset + "px";
                } else {
                    element.style.backgroundPosition = this.backgroundPosition.replace("top", offset + "px");
                }
            } else {
                element.style.backgroundPosition = this.backgroundPosition;
            }
        }
        if (element != document.body) element.style.backgroundRepeat = "no-repeat";
    }
}

Qva.Mgr.background.prototype.Paint = function(mode, node, name) {
    this.Touched = true;
    var element = this.Element,
        stamp = node.getAttribute('stamp'),
        gradientOK = IS_CHROME || (IS_GECKO && GECKO_VERSION >= 3.6) || IS_SAFARI || IE_DOCMODE > 8,
        bkgApplied = false;

    this.HasBkgImage = node.getAttribute('image') != null;
    this.avq_url = null;

    if (IS_IE && IE_DOCMODE >= 8) {
        element.style.filter = '';
    }

    var isBody = element === document.body;
    if (isBody) {
        if (this.HasBkgImage) {
            gradientOK = false;
        } else {
            element.style.backgroundImage = ''
            // Gradients on sheetbackground doesn't seem to break any functionality.
            gradientOK = IE_DOCMODE > 7;
        }
    }

    if (!this.HasBkgImage || isBody) {

        if (!gradientOK) { element.style.backgroundImage = ''; }
        // If there is no image available or image and gradient are combined fall back to solid color
        var useFallbackForGradient = (!gradientOK && stamp === null) || isBody;

        bkgApplied = Qva.ApplyColorDataToElement(node, element, gradientOK, this.HasBkgImage ? false : useFallbackForGradient);
        if (!bkgApplied) this.HasBkgImage = this.HasBkgImage || (stamp && stamp != "0");
    }
    // Used for bkgImages as well as fallback for IE since gradients is not 100% in IE
    if (this.HasBkgImage) {
        if (!element == document.body) {
            element.style.backgroundColor = ''; 
        }
        this.avq_url = this.PageBinder.BuildBinaryUrl(node.getAttribute("path"), stamp, this.Name);
    }

    if (element == document.body) {

        Qva.ResizeBody();
        if (node.getAttribute('bkgcolor')) element.style.backgroundColor = HtmlifyARGB(node.getAttribute('bkgcolor'));
        this.backgroundPosition = node.getAttribute('bkgposition');
        if (node.getAttribute('bkgrepeat')) element.style.backgroundRepeat = node.getAttribute('bkgrepeat');
        if (node.getAttribute('opacity')) {
            if (IS_WEBKIT || IS_OPERA) {
                element.style.opacity = parseFloat(node.getAttribute('opacity'));
            } else if (IS_GECKO) {
                element.style.MozOpacity = parseFloat(node.getAttribute('opacity'));
            } else {
                element.style.filter = "alpha(opacity:" + parseFloat(node.getAttribute('opacity')) * 100 + ")";
            }
        }
        if (!node.getAttribute('bkgposition') && node.getAttribute('bkgrepeat') != "repeat") {
            this.AutoSize = true
        }
    }

    if (this.HasBkgImage) {
        this.PageBinder.QueuePostPaintMessage(this);
    }
}

Qva.Mgr.fullsize = function (owner, elem, name, prefix) {
    if (!Qva.MgrSplit (this, name, prefix)) return;
    owner.AddManager(this);
    this.Element = elem;
    this.AutoSize = elem.style.width == 'auto' && elem.style.height == 'auto' && this.PageBinder.Element;
    if (! this.AutoSize) {
        this.PostPaint = null;
    }
}

Qva.Mgr.fullsize.prototype.PostPaint = function () {
    var element = this.Element;
    if (element.style.display == "none") return;
    if (this.AutoSize) {
        this.PageBinder.SetObjectWidth(0);
        this.PageBinder.SetObjectHeight(0);
        if (element.tagName == "DIV") {
            var newheight = this.PageBinder.GetContentMaxHeight (element);
            if (! isNaN (newheight)) {
                element.style.height = newheight + "px";
            }
        }
    } else {
        Assert (false);
    }
}

Qva.Mgr.fullsize.prototype.Paint = function() {
    this.Touched = true;
}

Qva.Mgr.visibility = function (owner, elem, name, prefix) {
    if (!Qva.MgrSplit (this, name, prefix)) return;
    owner.AddManager(this);
    this.Element = elem;
}

Qva.Mgr.visibility.prototype.Paint = function(mode) {
    this.Touched = true;
    this.Element.style.display = Qva.MgrGetDisplayFromMode (this, mode);
}

Qva.Mgr.label = function (owner, elem, name, prefix) {
    if (!Qva.MgrSplit (this, name, prefix)) return;
    owner.AddManager(this);
    this.Element = elem;
    this.AutoSize = elem.style.width == 'auto' && elem.style.height == 'auto' && this.PageBinder.Element;
    if (! this.AutoSize) {
        this.PostPaint = null;
    }
}

Qva.Mgr.label.prototype.PostPaint = function () {
    var element = this.Element;
    if (element.style.display == "none") return;
    if (this.AutoSize) {
        this.PageBinder.SetObjectWidth(0);
        this.PageBinder.SetObjectHeight(0);
        if (element.tagName == "DIV") {
            var newheight = this.PageBinder.GetContentHeight ();
            if (! isNaN (newheight)) {
                element.style.height = newheight + "px";
            }
        }
    }
}

Qva.Mgr.label.prototype.Paint = function(mode, node) {
    this.Touched = true;
    var element = this.Element;
    element.style.display = Qva.MgrGetDisplayFromMode (this, mode);
    if (this.AutoSize && element.style.display != "none") this.PageBinder.QueuePostPaintMessage (this);
}

Qva.Mgr.inline = function(owner, elem, name, prefix) {
    if (!Qva.MgrSplit(this, name, prefix)) return;
    owner.AddManager(this);
    this.Element = elem;
}

Qva.Mgr.inline.prototype.Paint = function(mode, node) {
    this.Touched = true;
    var element = this.Element;
    element.style.display = Qva.MgrGetDisplayFromMode(this, mode);
    if (element.style.display == "none") return;
    element.innerHTML = '';
    var svg = node.removeChild(node.firstChild);
    element.appendChild(svg);
}

Qva.Mgr.edit = function (owner, elem, name, prefix) {
    if (!Qva.MgrSplit (this, name, prefix)) return;
    owner.AddManager(this);
    this.Element = elem;
    owner.Append (this, this.Name, 'choice');
}

Qva.Mgr.edit.prototype.Paint = function(mode, node, name) {
    this.Touched = true;
    var element = this.Element;
    if (name == this.ToolTip && node.getAttribute('text')) {
        element.title = node.getAttribute('text');
        return;
    }
    if (node.getAttribute('title') != null) {
        element.title = node.getAttribute('title');
    }
    if (this.MultiLine) {
        var text = '';
        var elems = node.getElementsByTagName("element");
        for (var ix = 0; ix < elems.length; ++ix) {
            if (ix > 0) text += '\n';
            text += elems[ix].getAttribute('text');
        }
        if (!IS_IE) {
            element.style.whiteSpace = 'pre-wrap'; //'pre-line'; this is how it's solved in v11 in default.css
        }
        element.innerText = text;
        return;
    }
    var attrValue = '';
    if (mode != 'n') {
        attrValue = node.getAttribute(this.Attr);
        switch (this.Attr) {
            case 'value':
                if (node.getAttribute('text') == '') attrValue = '';
                break;
            case 'color':
                if (node.getAttribute('color') && node.getAttribute('bkgcolor')) {
                    element.style.color = node.getAttribute('color');
                    element.style.backgroundColor = node.getAttribute('bkgcolor');
                    return;
                }
        }
    }
    if (attrValue == '' && this.TextIfNull) attrValue = this.TextIfNull;
    var text;
    if (this.Dec != null) {
        text = Qva.Trunc(attrValue, this.Dec);
    } else {
        text = attrValue;
    }
    if (element.tagName == 'INPUT' || element.tagName == 'TEXTAREA') {
        element.value = text;
    } else {
        element.innerText = text;
    }
    element.style.display = Qva.MgrGetDisplayFromMode(this, mode);
}

Qva.Mgr.dsi = function(owner, elem, name, prefix) {
    if (!Qva.MgrSplit(this, name, prefix)) return;
    owner.AddManager(this);
    this.Element = elem;
}

Qva.Mgr.dsi.prototype.Paint = function(mode, node, name) {
    this.Touched = true;
    var element = this.Element;
    var lines = node.getAttribute('text').split(';');
    element.value = lines.join('\n');
}

Qva.Mgr.inputcheckbox = function (owner, elem, name, prefix, condition) {
    if (!Qva.MgrSplit (this, name, prefix)) return;
    this.Element = elem;
    owner.AddManager(this);
    if (condition != null) {
        var sep = condition.substr(1,1);
        var parts = condition.split(sep);
        this.Conditional = (parts[0] == '-') ? -1 : 1;
        var alt1 = (parts.length > 1) ? parts[1] : '';
        var alt2 = (parts.length > 2) ? parts[2] : null;
        elem.True = (this.Conditional > 0) ? alt1 : alt2;
        elem.False = (this.Conditional < 0) ? alt1 : alt2;
    } else {
        this.Conditional = 0;
        elem.True = '1';
        elem.False = '0';
        this.Attr = 'value';
    }
    
    elem.Name = this.Name;
    elem.Attr = this.Attr;
    elem.binderid = owner.binderid;
    
    elem.onclick = Qva.Mgr.inputcheckbox.OnClick;
    owner.Append (this, this.Name, 'choice');
}

Qva.Mgr.inputcheckbox.prototype.Lock = Qva.LockDisabled;
Qva.Mgr.inputcheckbox.prototype.Unlock = Qva.UnlockDisabled;
Qva.Mgr.inputcheckbox.prototype.Paint = function(mode, node) {
    this.Touched = true;
    var element = this.Element;
    var val = node.getAttribute (this.Attr);
    if (this.Conditional < 0) {
        element.checked = (val != element.False);
    } else {
        element.checked = (val == element.True);
    }
    var choices = node.getElementsByTagName ("choice");
    if (choices.length == 0) {
        element.disabled = (mode != 'e');
    } else {
        choices = choices[0].getElementsByTagName ("element");
        var range = choices.length;
        if (range > 1) {
            element.disabled = (mode != 'e');
        } else {
            element.disabled = true;
        }
    }
    element.style.display = Qva.MgrGetDisplayFromMode (this, mode);
}

Qva.Mgr.inputcheckbox.OnClick = function() {
    var binder = Qva.GetBinder(this.binderid);
    if (!binder.Enabled) return;
    var newval = this.checked ? this.True : this.False;
    if (newval != null) {
        binder.Set (this.Name, this.Attr, newval, true);
    } else {
        newval = this.checked ? this.False : this.True;
        binder.Set (this.Name, 'count', '-' + newval, true);
    }
}

Qva.Mgr.inputradio = function (owner, elem, name, prefix) {
    if (!Qva.MgrSplit (this, name, prefix)) return;
    this.Element = elem;
    elem.Name = this.Name;
    elem.Attr = this.Attr;
    elem.binderid = owner.binderid;
    owner.AddManager(this);
    
    elem.onclick = function () {
        var binder = Qva.GetBinder(this.binderid);
        if (!binder.Enabled) return;
        binder.Set (this.Name, this.Attr, this.getAttribute("value"), true);
    };
    owner.Append (this, this.Name, 'choice');
}

Qva.Mgr.inputradio.prototype.PostScan = function(scanner) {
    var CheckIf = this.Element.getAttribute (scanner.Prefix + 'checkif');
    if (CheckIf) {
        this.CheckIf = new Function("value", "return " + CheckIf);
    }
}

Qva.Mgr.inputradio.prototype.Lock = Qva.LockDisabled;
Qva.Mgr.inputradio.prototype.Unlock = Qva.UnlockDisabled;
Qva.Mgr.inputradio.prototype.Paint = function(mode, node) {
    this.Touched = true;
    var element = this.Element;
    var value = node.getAttribute(this.Attr);
    if (value == null) value = node.getAttribute("value");
    if (this.CheckIf) {
        element.checked = this.CheckIf(value);
    } else {
        element.checked = (element.getAttribute("value") == value);
    }
    element.disabled = (mode != 'e');
    if (mode == 'e' && !element.checked) {
        // check if should be disabled because not in choice
        var choices = node.getElementsByTagName ("choice");
        if (choices.length >= 1) {
            element.disabled = true;
            choices = choices[0].getElementsByTagName ("element");
            var cholen = choices.length;
            for (var ix = 0; ix < cholen; ++ix) {
                if (choices [ix].getAttribute(this.Attr) == element.getAttribute("value")) {
                    element.disabled = false;
                    break;
                }
            }
        }
    }
    element.style.display = Qva.MgrGetDisplayFromMode (this, mode);
}

Qva.Mgr.inputtext = function (owner, elem, name, prefix, condition) {
    if (!Qva.MgrSplit (this, name, prefix)) return;
    this.Element = elem;
    this.Condition = condition;
    elem.Name = this.Name;
    elem.binderid = owner.binderid;
    elem.Attr = this.Attr;
    
    owner.AddManager(this);
    elem.onclick = function (event) {
        Qva.ActiveObjectsCloak = true;
        if (!event) event = window.event;
        event.cancelBubble = true;
    };
    elem.onchange = function () {
        var binder = Qva.GetBinder(this.binderid);
        if (!binder.Enabled) return;
        if (this.value == '') {
            binder.Set(this.Name, 'text', '', true);
        } else {
            binder.Set(this.Name, this.Attr, this.value, true);
        }
    };
}
Qva.Mgr.inputtext.prototype.Lock = Qva.LockReadOnly;
Qva.Mgr.inputtext.prototype.Unlock = Qva.UnlockReadOnly;
Qva.Mgr.inputtext.prototype.Paint = function(mode, node) {
    this.Touched = true;
    var element = this.Element;
    element.readOnly = (mode != 'e');
    element.style.color = element.readOnly ? 'graytext' : '';
    element.disabled = false;
    var attrValue = '';
    if (mode != 'n') {
        attrValue = node.getAttribute (this.Attr);
        if (this.Attr == 'value' && node.getAttribute ('text') == '') attrValue = '';
    }
    if (mode != 'n' && this.Condition != null) {
        var cval = node.getAttribute ('value');
        if (cval == this.Condition) {
            element.disabled = true;
            attrValue = '';
        }
    }
    if (attrValue == '' && this.TextIfNull) attrValue = this.TextIfNull;
    if (this.Dec != null) {
        element.value = Qva.Trunc (attrValue, this.Dec);
    } else {
        element.value = attrValue;
    }
    element.style.display = Qva.MgrGetDisplayFromMode (this, mode);
}

function Avq_Search_Focus (event) {
    var binder = this.SearchMgr.PageBinder;
    var name = this.SearchMgr.Name.replace (".Input", "");
    if (this.value == this.SearchMgr.DefaultText) {
        this.style.fontStyle = "";
        this.style.color = "";
        this.value = "";
        Qva.SetCursor (this);
        binder.Set (name, "click", "", true);
    }
    binder.GlobalSearchObject = name;
}

function WebkitClickFix (event) {
    if (this.value == "**") {
        Qva.SetCursor (this);
    }
}

function Avq_Search_KeyDown(event) {
    if (! event) { 
        event = window.event; 
    }
    var mgr = this.SearchMgr;
    if (this.value == mgr.DefaultText) {
        this.value = "**";
        Qva.SetCursor (this);
        this.style.fontStyle = "";
        this.style.color = "";
    }
    var key = event.keyCode;
    var accept = false;
    switch (key) {
    case 13:    // <Enter>
        accept = true;
    case 27:    // <Escape>'
        var binder = mgr.PageBinder || Qva.GetBinder(mgr.binderid);
        if (binder.GetEnabled ()) {
            if (accept) binder.Set(mgr.SearchName, 'search', this.value, false);
            var cmd = accept ? "accept" : "abort";
            binder.Set (mgr.SearchName, "closesearch", cmd, true);
        } else {
            Qva.PendingSearchKey = (accept ? "accept" : "abort");
            Qva.PendingSearchKeyName = mgr.SearchName;
        }
        this.value = mgr.DefaultText;
        this.style.fontStyle = mgr.DefaultFontStyle;
        this.style.color = mgr.DefaultColor;
        binder.GlobalSearchObject = "";
        break;
    }
}

Qva.Mgr.inputsearch = function (owner, elem, name, prefix, condition) {
    if (!Qva.MgrSplit (this, name, prefix)) return;
    this.Element = elem;
    this.Condition = condition;
    elem.Name = this.Name;
    elem.binderid = owner.binderid;
    elem.Attr = this.Attr;
    elem.onkeydown = Avq_Search_KeyDown;
    elem.onkeyup = AvqAction_Search_KeyUp;
    elem.onfocus = Avq_Search_Focus;
    if (IS_WEBKIT) {
        elem.onclick = WebkitClickFix;
    } else {
        elem.onclick = Qva.CancelAction;
    }
    elem.SearchMgr = this;
    this.SearchName = this.Name;
    owner.AddManager(this);
}

Qva.Mgr.inputsearch.prototype.Paint = function (mode, node) {
    this.Touched = true;
    var element = this.Element;
    element.readOnly = (mode != 'e');
    element.disabled = false;
    element.style.display = Qva.MgrGetDisplayFromMode (this, mode);
    if (element.style.display == "none") return;
    if (! this.DefaultText) {
        this.DefaultText = node.getAttribute ("defaulttext");
    }
    if (node.getAttribute ("searching") != "true") {
        element.value = this.DefaultText;
        this.DefaultFontStyle = element.style.fontStyle;
        this.DefaultColor = element.style.color;
        element.blur();
    } else {
        this.PageBinder.GlobalSearchObject = this.Name.replace (".Input", "");
    }
}
Qva.Mgr.inputsearch.prototype.GetCellRect = function (col, row) {
    Assert(col == -1 && row == -1);
    var pos = Qva.GetAbsolutePageCoords(this.Element);
    return { 'left': pos.x, 'width': this.Element.offsetWidth, 'top': pos.y, 'height': this.Element.offsetHeight };
}

Qva.Mgr.textarea = Qva.Mgr.inputtext;
Qva.Mgr.inputpassword = Qva.Mgr.inputtext;

Qva.Mgr.text = function (owner, elem, name, prefix, tooltip) {
    if (!Qva.MgrSplit (this, name, prefix)) return;
    if (tooltip != null) {
        this.ToolTip = Qva.MgrMakeName (tooltip, prefix);
        owner.Append (this, this.ToolTip);
    }
    if (elem.tagName == 'SELECT') { Assert(false); }
    owner.AddManager(this);
    this.Element = elem;
}

Qva.Mgr.text.prototype.Paint = Qva.Mgr.edit.prototype.Paint;

Qva.Mgr.step = function (owner, elem, name, prefix, step) {
    switch (step) {
    case "next":
        this.Next = true;
        break;
    case "prev":
        this.Next = false;
        break;
    default:
        elem.Step = parseFloat (step);
        if (isNaN (elem.Step)) return;
        break;
    }
    if (!Qva.MgrSplit (this, name, prefix)) return;
    if (elem.Step == null) elem.Choice = new Array();
    owner.AddManager(this);
    this.Element = elem;
    
    elem.Name = this.Name;
    elem.binderid = owner.binderid;
    elem.Attr = this.Attr;
    elem.onclick = Qva.Mgr.step.OnClick;
    owner.Append (this, this.Name, 'choice');
}

Qva.Mgr.step.prototype.Lock = Qva.LockDisabled;
Qva.Mgr.step.prototype.Unlock = Qva.UnlockDisabled;
Qva.Mgr.step.prototype.Paint = function(mode, node) {
    this.Touched = true;
    var element = this.Element;
    element.disabled = (mode != 'e');
    if (mode == 'n') {
        // no action
    } else if (element.Step != null) {
        element.Last = parseFloat (node.getAttribute (this.Attr));
    } else {
        var value = node.getAttribute (this.Attr);
        var choiceNodes = node.getElementsByTagName ("choice");
        element.Pending = -1;
        if (choiceNodes.length >= 1) {
            var choices = choiceNodes[0].getElementsByTagName ("element");
            var height = choices.length;
            for (var rix = 0; rix < height; ++rix) {
                element.Choice [rix] = choices[rix].getAttribute(this.Attr);
                if (choices[rix].getAttribute("selected") == "yes") {
                    if (this.Next) {
                        element.Pending = rix + 1;
                    } else {
                        element.Pending = rix - 1;
                    }
                }
            }
            element.Choice.length = height;
        }
        if (element.Pending < 0 || element.Pending >= element.Choice.length) element.disabled = true;
    }
    element.style.display = Qva.MgrGetDisplayFromMode (this, mode);
}

Qva.Mgr.step.OnClick = function() {
    var binder = Qva.GetBinder(this.binderid);
    if (!binder.Enabled) return;
    var newval;
    if (this.Step != null) {
        newval = this.Last + this.Step;
    } else {
        if (this.Pending < 0 || this.Pending >= this.Choice.length) return;
        newval = this.Choice[this.Pending];
    }
    binder.Set (this.Name, this.Attr, '' + newval, true);
}

Qva.Mgr.actions = function (owner, elem, name, prefix) {
    if (!Qva.MgrSplit (this, name, prefix)) return;
    this.Attr = "mode";
    owner.AddManager(this);
    this.Element = elem;
    elem.Action = this.Name;
    elem.binderid = owner.binderid;
}

Qva.Mgr.actions.prototype.Lock = Qva.LockDisabled;
Qva.Mgr.actions.prototype.Unlock = Qva.UnlockDisabled;
Qva.Mgr.actions.prototype.Paint = function (mode, node) {
    this.Touched = true;
    var element = this.Element;
    var disabled = (mode != 'e');
    element.style.display = Qva.MgrGetDisplayFromMode (this, mode);
    if (node.getAttribute ('actions') == "true" && ! disabled) {
        element.style.cursor = 'pointer';
        element.onclick = onclick_action;
    } else {
        if (element.style.cursor == 'pointer') element.style.cursor = 'auto';
        if (element.onclick == onclick_action) element.onclick = null;
    }
}

Qva.Mgr.scroll = function (owner, elem, name, prefix) {
    if (!Qva.MgrSplit (this, name, prefix)) return;
    owner.AddManager(this);
    this.Element = elem;
}

Qva.Mgr.scroll.prototype.Paint = function (mode, node) {
    this.Touched = true;
    var element = this.Element;
    if (node.getAttribute ('overflowx')) {
        element.style.overflowX = node.getAttribute ('overflowx');
    }
    if (node.getAttribute ('overflowy')) {
        element.style.overflowY = node.getAttribute ('overflowy');
    }
}

Qva.Mgr.action = function (owner, elem, name, prefix) {
    if (!Qva.MgrSplit (this, name, prefix)) return;
    this.Attr = "mode";
    owner.AddManager(this);
    this.Element = elem;
    elem.Action = this.Name;
    elem.binderid = owner.binderid;
    elem.onclick = onclick_action;
    owner.Append (this, this.Name, 'action');
}

Qva.Mgr.action.prototype.Lock = Qva.LockDisabled;
Qva.Mgr.action.prototype.Unlock = Qva.UnlockDisabled;
Qva.Mgr.action.prototype.Paint = function(mode, node) {
    this.Touched = true;
    var element = this.Element;
    element.disabled = (mode != 'e');

    var label = node.getAttribute('label') || element.alt;
    
    if (label) {
        element.title = label;
    }
    

    element.style.display = Qva.MgrGetDisplayFromMode(this, mode);
    if (node.getAttribute('clientaction')) {
        element.onclick = onclick_ContextClientAction;
        element.Name = this.Name;
        element.AvqMgr = this;
        element.clientaction = node.getAttribute('clientaction');
        element.param = node.getAttribute("param");
    }
    element.Position = node.getAttribute('value');
    var icon = element.getAttribute('icon');
    if (icon) {
        var binder = Qva.GetBinder(element.binderid);
        if (element.disabled) icon += ".DISABLED";
        var url = binder.BuildBinaryUrl(node.getAttribute('path'), null, icon);
        element.src = url;
        element.onmouseover = function() { this.className = this.disabled ? "" : "HighlightImage"; }
        element.onmouseout = function() { this.className = ""; }
    }
}

Qva.Mgr.hr = function(owner, elem, name, prefix) {
    if (!Qva.MgrSplit(this, name, prefix)) return;
    owner.AddManager(this);
    var actor = elem.nextSibling;
    while (actor && actor.tagName != 'BUTTON') actor = actor.nextSibling
    if (actor) {
        this.Element = actor;
        actor.binderid = owner.binderid;
        actor.Name = this.Name;
        actor.moveObj = '*';
        actor.xOnly = true;
        actor.xMin = 0;
        actor.xMax = 100;
        actor.disabled = true;
        actor.style.cursor = actor.disabled ? '' : 'pointer';
        actor.onmousedown = Qva.Move.mouseDown;
    }
}

Qva.Mgr.hr.prototype.Paint = function(mode, node) {
    this.Touched = true;
    var element = this.Element;
    element.disabled = mode != 'e';
    var pos = parseInt(node.getAttribute("text"));
    pos = isNaN(pos) ? 0 : (pos / 2.55);
    element.style.left = pos + 'px';
    element.style.cursor = element.disabled ? '' : 'pointer';
}

Qva.Mgr.select = function (owner, elem, name, prefix, condition) {
    if (elem.multiple) { Assert(false); return; }
    //if (elem.size > 1) { debugger; return; }
    if (!Qva.MgrSplit (this, name, prefix)) return;
    owner.AddManager(this);
    this.Element = elem;
    this.Condition = condition;
    this.Texts = new Array ();
    this.Values = new Array ();
    this.Disabled = new Array ();
    this.Locked = new Array ();

    elem.binderid = owner.binderid;
    elem.Name = this.Name;
    
    elem.onchange = Qva.Mgr.select.OnChange;
    elem.onclick = Qva.CancelBubble;
}

Qva.Mgr.select.prototype.Lock = Qva.LockDisabled;
Qva.Mgr.select.prototype.Unlock = Qva.UnlockDisabled;
Qva.Mgr.select.prototype.Paint = function(mode, node) {
    this.Touched = true;
    var element = this.Element;
    if (mode != 'n') {
        element.ByValue = (node.getAttribute('value') != null);
    }
    var currentValue = node.getAttribute("text");
    if (this.Dec != null) currentValue = Qva.Trunc (currentValue, this.Dec);
    if (currentValue == null) currentValue = "";
    if (this.TextIfNull && currentValue == "") currentValue = this.TextIfNull;
    var optlen = element.options.length;
    if (mode == 'e' && (this.Condition == null || currentValue != '')) {
        var choices = node.getElementsByTagName ("choice");
        if (choices.length >= 1) choices = choices[0].getElementsByTagName ("element");
        var first = (this.Condition == null) ? 0 : 1;
        var cholen = choices.length - first;
        element.options.length = cholen;
        if (cholen >= 1) {
            this.SelectedIndex = -1;
            for (var ix = 0; ix < cholen; ++ix) {
                var cho = choices [ix + first];
                var optval = cho.getAttribute("text");
                if (this.TextIfNull && optval == "") optval = this.TextIfNull;
                if (this.Dec != null) optval = Qva.Trunc (optval, this.Dec);
                var opt = element.options [ix];
                opt.text = optval.replace (/\t/g, ' ');
                opt.value = element.ByValue ? cho.getAttribute('value') : optval;
                var selected = false;
                if (element.ByValue) {
                    if (opt.value == node.getAttribute("value")) {
                        this.SelectedIndex = ix;
                        selected = true;
                    }
                } else {
                    if (optval == currentValue) {
                        this.SelectedIndex = ix;
                        selected = true;
                    }
                }
                if (selected) opt.selected = true;
            }
            if (this.SelectedIndex == -1) {
                element.options [cholen-1].selected = true;
            }
        }
        element.disabled = false;
    } else {
        element.disabled = true;
        if (optlen < 1 || this.Condition != null) {
            element.options.length = 1;
            var opt0 = element.options [0];
            opt0.text = (currentValue != null) ? currentValue.replace (/\t/g, ' ') : '';
            opt0.value = element.ByValue ? node.getAttribute('value') : currentValue;
            opt0.selected = true;
        }
    }
    element.style.display = Qva.MgrGetDisplayFromMode (this, mode);
}

Qva.Mgr.select.OnChange = function() {
    var binder = Qva.GetBinder(this.binderid);
    if (!binder.Enabled) return;
    if (this.selectedIndex < 0) return;
    var opt = this.options [this.selectedIndex];
    if (this.ByValue === true) {
        binder.Set (this.Name, 'value', opt.value, true);
    } else {
        binder.Set (this.Name, "text", opt.text, true);
    }
}

Qva.Mgr.graph = function(owner, elem, name, prefix) {
    elem.Name = this.Name = Qva.MgrMakeName(name, prefix);
    elem.binderid = owner.binderid;
    elem.ObjectId = owner.Element.ObjectId;
    elem.isgraph = true;
    this.AutoSize = elem.style.width == 'auto' && elem.style.height == 'auto';
    owner.AddManager(this);
    this.Element = elem;
    this.ScrollBars = [];
    this.HotSpots = [];

    elem.SelectStart = Qva.Mgr.graph.SelectStart;
    elem.Select = Qva.Mgr.graph.Select;
    elem.SelectEnd = Qva.Mgr.graph.SelectEnd;
    elem.onmousedown = function(event) { Qva.MouseDown(event, elem); }
    
    if (!IS_TOUCHENABLED) { 
        var hover = Qva.GetBinder(elem.binderid).GetHoverDiv();
        hover.style.display = "none";
        elem.style.display = "none";
        elem.LastHoverPos = {};

        
        elem.onmousemove = function(event) {
            Qva.MouseMove(event, elem);
            if (! Qva.MgrWithSelectStart && ! Qva.MgrWithMouseDown) {
                elem.onmouseover(event);
            }
        }
    
        elem.onmouseover = function(event) {
            if (!elem.hover) return;
            if (!event) event = window.event;
            var binder = Qva.GetBinder(elem.binderid);
            if (!binder) return;
            var name = elem.Name;

            var hoverPos = {
                x: event.clientX + Qva.GetScrollLeft(),
                y: event.clientY + Qva.GetScrollTop()
            };

            if (hoverPos.x === elem.LastHoverPos.x && hoverPos.y === elem.LastHoverPos.y) {
                return;
            }

            elem.LastHoverPos = hoverPos;

            var hover = binder.GetHoverDiv();
            hover.style.left = hoverPos.x + 10 + "px";
            hover.style.top = hoverPos.y + 5 + "px";
            hover.style.display = "none";

            var pos = Qva.GetPageCoords(elem);
            var click = (event.clientX - pos.x) + ':' + (event.clientY - pos.y);

            if (elem.hoverTimout) { elem.hoverTimout = clearTimeout(elem.hoverTimout); }
            elem.hoverTimout = setTimeout(function() {
                if (Qva.IsContextOpen()) return;
                if (!binder.GetEnabled()) return;
                binder.Hover(name, click);
            }, 1000);
        };
        
        elem.onmouseout = function(event) {
            if (elem.hoverTimout) { elem.hoverTimout = clearTimeout(elem.hoverTimout); }
        }
    }
    elem.style.cursor = 'crosshair';
}

Qva.Mgr.graph.prototype.Unlock = function () {
    this.Touched = true;
    if (this.Element.style.display != 'none') {
        this.PostPaint ();
    }
}

GraphScrollBar.prototype.Step = function(pos) {
    var binder = this.PageBinder;
    if (!this.PageBinder.IsValid()) {
        return;
    }
    var name = this.Name;
    var params = {};
    if (this.ScrollId != null) {
        params.id = this.ScrollId;
        params.scrollpos = pos;
    } else {
        var step = pos - this.Pos;
        params.scrollstep = step;
    }
    binder.Set(name, params);
}

GraphScrollBar.prototype.createScrollEnd = function (top, left, height, width, color, firstbutton) {
    var div = document.createElement("div");
    if (this.IsXScroll) {
        div.style.top = (top + 1) + "px";
        left++;
        if (! firstbutton) left += (width - height);
        div.style.left = left + "px";
        height -= 2;
        div.style.height = height + "px";
        div.style.width = height + "px";
    } else {
        div.style.left = (left + 1) + "px";
        top++;
        if (! firstbutton) top += (height - width);
        div.style.top = top + "px";
        width -= 2;
        div.style.height = width + "px";
        div.style.width = width + "px";
    }
    div.style.backgroundColor = color;
    div.style.cursor = "pointer";
    div.style.position = "absolute";
    div.firstbutton = firstbutton;

    var position, points;
    if (this.IsXScroll) {
        position = [parseInt (height / 2) - 1, parseInt (height / 2) - 3];
        points = firstbutton ? [[2,0], [0,3], [2,6]] : [[0,0], [2,3], [0,6]];
    } else {
        position = [parseInt(width / 2) - 3, parseInt (width / 2)];
        points = firstbutton ? [[0,2], [3,0], [6,2]] : [[0,0], [3,2], [6,0]];
    }
    var size = [9, 9];
    this.G.CreatePolygonObj(points, "#000000", div, position, size);
    var _this = this;
    div.onmousedown = function(event) {
        if (!event) event = window.event;
        event.cancelBubble = true;
        _this.IsSliding = false;
        var pos = this.firstbutton ? _this.Pos - 1 : _this.Pos + 1;
        if (pos >= 0 && pos <= _this.Max ) {
            _this.UpdateSliderPos(pos);
            _this.Step (pos);
        }
        return false;
    };
    return div;
}

function GraphScrollBar (mgr, parent, top, left, width, height, color, bkgcolor, max, page, pos, isxscroll, id) {
    var o_scroll = document.createElement("div");
    o_scroll.style.backgroundColor = bkgcolor;
    o_scroll.style.cursor = "pointer";
    o_scroll.style.position = "absolute";

//    this.binderid = mgr.binderid;
    this.binderid = mgr.Element.binderid;
    this.PageBinder = mgr.PageBinder;
    this.Name = mgr.Name;
    this.IsXScroll = isxscroll;
    this.ScrollId = id;
    var _this = this;
    _this.IsSliding = false;

    this.G = SelectInitGraphics();

    o_scroll.onmousedown = function(event) {
        if (!event) event = window.event;
        event.cancelBubble = true;
        _this.IsSliding = false;
        var relpos = Qva.GetPageCoords(this);
        var px;
        var scrollpos;
        if (_this.IsXScroll) {
            px = _this.ScrollStart + event.clientX - relpos.x;
            scrollpos = parseInt(_this.Pos * _this.ScrollLength / _this.Page) + _this.ScrollStart + parseInt(_this.firstbutton.style.width);
        } else {
            px = _this.ScrollStart + event.clientY - relpos.y;
            scrollpos = parseInt(_this.Pos * _this.ScrollLength / _this.Page) + _this.ScrollStart + parseInt(_this.firstbutton.style.height);
        }
        var pos = px < (scrollpos) ? (_this.Pos - _this.Page) > 0 ? _this.Pos - _this.Page : 0 : Math.min(_this.Pos + _this.Page, (_this.Max - _this.Page));
        
        if (pos >= 0) {
            _this.UpdateSliderPos(pos);
            _this.Step (pos);
        }
        return false;
    };

    var i_scroll = document.createElement("div");
    i_scroll.style.backgroundColor = color;
    i_scroll.style.border = "solid 1px white";
    i_scroll.style.cursor = "pointer";
    i_scroll.style.position = "absolute";

    i_scroll.onmousedown = function(event) {
        if (!event) event = window.event;
        event.cancelBubble = true;
        if (_this.IsXScroll) {
            i_scroll.mouseZero = event.clientX;
            i_scroll.scrollZero = i_scroll.offsetLeft;
        } else {
            i_scroll.mouseZero = event.clientY;
            i_scroll.scrollZero = i_scroll.offsetTop;
        }

        function Update (event, mouseup) {
            if (!event) event = window.event;
            var px, pos;
            if (_this.IsXScroll) {
                px = i_scroll.scrollZero + event.clientX - i_scroll.mouseZero;
            } else {
                px = i_scroll.scrollZero + event.clientY - i_scroll.mouseZero;
            }
            if (px < _this.ScrollStart) {
                px = _this.ScrollStart;
                pos = 0;
            } else if (px > _this.ScrollStop) {
                px = _this.ScrollStop;
                pos = _this.Max - _this.Page;
            } else {
                pos = Math.round ((px - _this.ScrollStart) * _this.Page / _this.ScrollLength);
            }
            if (_this.IsXScroll) {
                i_scroll.style.left = px + "px";
            } else {
                i_scroll.style.top = px + "px";
            }
            if (_this.Pos != pos) {
                _this.Step(pos);
            } else if (mouseup) {
                var synchronizeScroll = function(pos) {
                    var posWhenMouseUp = pos;
                    if (posWhenMouseUp !== _this.pos) {
                        if (_this.PageBinder.IsValid()) {
                            if (xpos !== _this.pos) {
                                _this.Step(posWhenMouseUp);
                            }
                        } else {
                            setTimeout(synchronizeScroll, 1000);
                        }
                    }
                }
                synchronizeScroll();
                _this.IsSliding = false;

            }
        }
        function SlideEnd(event) {
            Qva.removeEvent(document, "mousemove", SlideDrag);
            Qva.removeEvent(document, "mouseup", SlideEnd);
            if (!event) event = window.event;
            Update(event, true);
            _this.i_Scroll.mouseZero = null;
            Qva.Select.Active = true;
        }
        function SlideDrag(event) {
            event = event || window.event;
            _this.isSliding = true;
            Update(event, false);
            if (event.preventDefault)
                event.preventDefault();
            else
                event.returnValue = false;
            return false;
        }
        Qva.Select.Active = false;
        Qva.addEvent(document, "mousemove", SlideDrag);
        Qva.addEvent(document, "mouseup", SlideEnd);
        return false;
    }

    this.o_Scroll = o_scroll;
    this.i_Scroll = i_scroll;
    parent.appendChild (o_scroll);
    parent.appendChild (i_scroll);

    this.UpdateScrollSize (top, left, width, height, max, page, pos, color, bkgcolor);
}

GraphScrollBar.prototype.UpdateScrollPos = function(pos) {
    if (!isNaN(pos)) {
        this.Pos = pos;
    }
    if (!this.isSliding) {
        this.UpdateSliderPos(pos);
    }
}

    GraphScrollBar.prototype.UpdateSliderPos = function(pos) {
        var newstart = parseInt(pos * this.ScrollLength / this.Page) + this.ScrollStart;
        if (this.IsXScroll) {
            this.i_Scroll.style.left = newstart + "px";
        } else {
            this.i_Scroll.style.top = newstart + "px";
        }
    }

GraphScrollBar.prototype.UpdateScrollSize = function (top, left, width, height, max, page, pos, color, bgColor) {
    if (this.Top != top || this.Left != left || this.Height != height || this.Width != width) {
        this.Height = height;
        this.Top = top;
        this.Left = left;
        this.Width = width;
        this.o_Scroll.style.height = height + "px";
        this.o_Scroll.style.top = top + "px";
        this.o_Scroll.style.left = left + "px";
        this.o_Scroll.style.width = Math.max(0, width) + "px";
        if (this.IsXScroll) {
            this.i_Scroll.style.height = Math.max (1, height - 4) + "px";
            this.i_Scroll.style.top = top + 1 + "px";
            this.i_Scroll.style.left = (height + left) + "px";
        } else {
            this.i_Scroll.style.top = (width + top) + "px";
            this.i_Scroll.style.left = (left + 1) + "px";
            this.i_Scroll.style.width = Math.max (1, width - 4) + "px";
        }
        var parent = this.o_Scroll.parentNode;
        if (this.firstbutton) {
            parent.removeChild (this.firstbutton);
            parent.removeChild (this.lastbutton);
        }
        var firstbutton = this.createScrollEnd (top, left, height, width, color , true);
        var lastbutton = this.createScrollEnd (top, left, height, width, color, false);
        this.firstbutton = firstbutton;
        this.lastbutton = lastbutton;
        parent.appendChild (firstbutton);
        parent.appendChild (lastbutton);
    }

    this.Page = page;
    this.Max = max;
    var maxpos = this.Max - this.Page;
    if (this.IsXScroll) {
        this.ScrollStart = this.o_Scroll.offsetLeft + parseInt (this.Height);
        this.ScrollLength = parseFloat ((this.Width - 2 * (this.Height)) * this.Page / this.Max);
        //this.ScrollLength = parseFloat(((this.Width - parseInt(this.firstbutton.style.width) - parseInt(this.lastbutton.style.width)) * this.Page) / this.Max) - 2;
        this.ScrollLengthVisible = (this.ScrollLength > 5) ? parseInt(this.ScrollLength) : 5
        this.ScrollStop = parseInt (maxpos * this.ScrollLength / this.Page) + this.ScrollStart - (this.ScrollLengthVisible - parseInt(this.ScrollLength));
        this.i_Scroll.style.width = this.ScrollLengthVisible + "px";
    } else {
        this.ScrollStart = this.o_Scroll.offsetTop + parseInt (this.Width);
        this.ScrollLength = parseFloat ((this.Height - 2 * (this.Width)) * this.Page / this.Max);
        //this.ScrollLength = parseFloat(((this.Height - parseInt(this.firstbutton.style.height) - parseInt(this.lastbutton.style.height)) * this.Page) / this.Max) - 2;
        this.ScrollLengthVisible = (this.ScrollLength > 5) ? parseInt(this.ScrollLength) : 5
        this.ScrollStop = parseInt (maxpos * this.ScrollLength / this.Page) + this.ScrollStart - (this.ScrollLengthVisible - parseInt(this.ScrollLength));
        this.i_Scroll.style.height = this.ScrollLengthVisible + "px";
    }
    if (bgColor) this.o_Scroll.style.backgroundColor = bgColor;
    this.i_Scroll.style.backgroundColor = color;
    this.firstbutton.style.backgroundColor = color;
    this.lastbutton.style.backgroundColor = color;
    this.UpdateScrollPos (pos);
}

GraphScrollBar.prototype.GetOffset = function (px) {
    var totlength;
    if (this.IsXScroll) {
        totlength = this.o_Scroll.offsetWidth - 2 * this.Height;
    } else {
        totlength = this.o_Scroll.offsetHeight - 2 * this.Width;
    }
    return Math.round(this.Page * px / totlength);
}

Qva.Mgr.graph.prototype.RemoveScrollBars = function() {
    for (var iScroll = 0; iScroll < this.ScrollBars.length; iScroll++) {
        this.Element.parentNode.removeChild(this.ScrollBars [iScroll].o_Scroll);
        this.Element.parentNode.removeChild(this.ScrollBars [iScroll].i_Scroll);
        this.Element.parentNode.removeChild(this.ScrollBars [iScroll].firstbutton);
        this.Element.parentNode.removeChild(this.ScrollBars [iScroll].lastbutton);
    }
    this.ScrollBars.length = 0;
}

Qva.Mgr.graph.prototype.Paint = function(mode, node) {
    this.IsSelectable = node.getAttribute("selectable") !== "false";
    this.Touched = true;
    var element = this.Element;
    element.style.display = Qva.MgrGetDisplayFromMode(this, mode);
    if (element.style.display == 'none') {
        return;
    }
    var disabled = (mode != 'e');
    element.disabled = disabled;
    if (!this.IsSelectable) {
        element.style.cursor = '';
        element.SelectStart = null;
        element.Select = null;
        element.SelectEnd = null;
        element.onmousedown = null;
        element.onmousemove = function(event) {
            this.onmouseover(event);
        }
    }

    element.hover = node.getAttribute('hover') === "true";
    var scrollnodes = node.getElementsByTagName("scroll");
    if (scrollnodes.length > 0) {
        var update = false;
        for (var iScroll = 0; iScroll < scrollnodes.length; iScroll++) {
            var scrollnode = scrollnodes [iScroll];
            var id = scrollnode.getAttribute("id");
            var isxscroll = scrollnode.getAttribute("horizontal") == "true";
            if (this.ScrollBars[iScroll]) {
                if (this.ScrollBars [iScroll].ScrollId != id || this.ScrollBars[iScroll].IsXScroll != isxscroll) {
                    update = false;
                    break;
                } else {
                    update = true;
                }
            } else {
                update = false;
                break;
            }
        }
        if (! update && this.ScrollBars.length > 0) {
            this.RemoveScrollBars ();
        }
        for (var iScroll = 0; iScroll < scrollnodes.length; iScroll++) {
            var scrollnode = scrollnodes [iScroll];
            var max = parseInt(scrollnode.getAttribute("max"));
            var page = parseInt(scrollnode.getAttribute("page"));
            var pos = parseInt(scrollnode.getAttribute("pos"));
            var top = parseInt(scrollnode.getAttribute("top"));
            var left = parseInt(scrollnode.getAttribute("left"));
            var height = parseInt(scrollnode.getAttribute("height"));
            var width = parseInt(scrollnode.getAttribute("width"));
            var color = HtmlifyARGB(scrollnode.getAttribute("color"));
            var bkgcolor = HtmlifyARGB(scrollnode.getAttribute("bkgcolor"));
            if (this.ScrollBars[iScroll]) {
                if (!this.ScrollBars[iScroll].i_Scroll.mouseZero) {
                    this.ScrollBars[iScroll].UpdateScrollSize(top, left, width, height, max, page, pos, color, bkgcolor)
                } else {
                    this.ScrollBars[iScroll].UpdateScrollPos(pos);
                }
            } else {
                if (width > 2 && height > 2) {
                    var id = scrollnode.getAttribute("id");
                    var isxscroll = scrollnode.getAttribute("horizontal") == "true";
                    this.ScrollBars [iScroll] = new GraphScrollBar(this, element.parentNode, top, left, width, height, color, bkgcolor, max, page, pos, isxscroll, scrollnode.getAttribute("id"));
                }
            }
        }
    } else {
        this.RemoveScrollBars ();
    }
    for (var iHS = 0; iHS < this.HotSpots.length; iHS++) {
        this.Element.parentNode.removeChild(this.HotSpots [iHS]);
    }
    this.HotSpots.length = 0;
    var hotspotnodes = node.getElementsByTagName("hotspot");
    if (hotspotnodes.length > 0) {
        for (var iHS = 0; iHS < hotspotnodes.length; iHS++) {
            var hotspotnode = hotspotnodes [iHS];
            var top = parseInt(hotspotnode.getAttribute("top"));
            var left = parseInt(hotspotnode.getAttribute("left"));
            var height = parseInt(hotspotnode.getAttribute("height"));
            var width = parseInt(hotspotnode.getAttribute("width"));
            var hotspot = document.createElement("div");
            hotspot.className = "Qv_Hotspot";
            hotspot.style.cursor = "pointer";
            hotspot.style.position = "absolute";
            hotspot.style.top = top + "px";
            hotspot.style.left = left + "px";
            hotspot.style.height = height + "px";
            hotspot.style.width = width + "px";
            hotspot.index = hotspotnode.getAttribute("index");
            hotspot.Name = element.Name;
            hotspot.binderid = element.binderid;
            hotspot.onclick = function(event) { Qva.GetBinder(this.binderid).SetClick(event, this.Name, this); }
            element.parentNode.appendChild (hotspot);
            this.HotSpots [iHS] = hotspot;
        }
    }
    var stamp = node.getAttribute('stamp');
    if (stamp != "0") {
        var name = stamp ? this.Name : node.getAttribute('name');
        if (element.disabled) name += ".DISABLED";
        var url = this.PageBinder.BuildBinaryUrl(node.getAttribute('path'), stamp, name);
        this.avq_url = url;
    }
    this.PageBinder.QueuePostPaintMessage(this);
}

Qva.Mgr.graph.prototype.PostPaint = function () {
    var elem = this.Element;
    if (elem.style.display == 'none') return;
    if (this.AutoSize) {
        this.PageBinder.SetObjectWidth(0);
        this.PageBinder.SetObjectHeight(0);
    }
    var url = this.avq_url;
    var sizeupdated = false;
    if (elem.style.width != '' && elem.style.height != '') {
        var objectbinder = Qva.GetObjectBinder(elem);
        var graphsize = objectbinder.GetImageSize (elem);
        if (this.GraphWidth != graphsize.width || this.GraphHeight != graphsize.height) {
            this.GraphWidth = graphsize.width;
            this.GraphHeight = graphsize.height;
            sizeupdated = true;
        }
        url += '&width=' + escape (graphsize.width) + '&height=' + escape (graphsize.height);
    }
    if (this.avq_url) {
        elem.src = Qva.AddProxyIfAny(url);
    }
    if (sizeupdated) {
        this.PageBinder.SetSize(this.Name, this.GraphWidth + ":" + this.GraphHeight);
    }
    elem.style.display = "block";
}

Qva.Mgr.graph.SelectStart = function (X, Y) { Qva.DragRect.Open (X, Y, this); }
Qva.Mgr.graph.Select      = function (X, Y) { Qva.DragRect.Size (X, Y); }
Qva.Mgr.graph.SelectEnd   = function (X, Y, ctrlpressed) { Qva.DragRect.End (X, Y, this, ctrlpressed); }

Qva.Mgr.binary = function (owner, elem, name, prefix) {
    elem.Name = this.Name = Qva.MgrMakeName (name, prefix);
    elem.binderid = owner.binderid;
    elem.ObjectId = owner.Element.ObjectId;
    owner.AddManager(this);
    this.Element = elem;
}

Qva.Mgr.binary.prototype.Paint = function (mode, node) {
    this.Touched = true;
    var element = this.Element;
    element.style.display = Qva.MgrGetDisplayFromMode (this, mode);
    if (element.style.display == 'none') {
        return;
    }
    var disabled = (mode != 'e');
    var stamp = node.getAttribute ('stamp');
    if (stamp != "0") {
        var name = stamp ? this.Name : node.getAttribute ('name');
        if (disabled ) name += ".DISABLED";
        var url = this.PageBinder.BuildBinaryUrl (node.getAttribute ('path'), stamp, name);
        this.avq_url = url;
    }
    this.PageBinder.QueuePostPaintMessage (this);
}

Qva.Mgr.binary.prototype.PostPaint = function () {
    var elem = this.Element;
    if (elem.style.display == 'none') return;
    var url = this.avq_url;
    if (this.avq_url) {
        elem.src = url;
    }
    elem.style.display = "block";
}

Qva.Mgr.button = function (owner, elem, name, prefix) {
    elem.Name = this.Name = Qva.MgrMakeName (name, prefix);
    elem.binderid = owner.binderid;
    elem.ObjectId = owner.Element.ObjectId;
    owner.AddManager(this);
    this.Element = elem;
}

Qva.Mgr.button.prototype.Paint = Qva.Mgr.binary.prototype.Paint;

Qva.Mgr.button.prototype.PostPaint = function () {
    var elem = this.Element;
    if (elem.style.display == 'none') return;
    var imageelem = null;
    if (elem.tagName != "BUTTON") { Assert(false); }
    for (var i = 0; i < elem.childNodes.length; i++) {
        if (elem.childNodes [i].tagName == "IMG") {
            imageelem = elem.childNodes [i];
            break;
        }
    }
    if (imageelem) {
        var url = this.avq_url;
        var sizeupdated = false;
        var sendsizetoserver = this.PageBinder.IsHosted;
        if (elem.style.width != '' && elem.style.height != '') {
            var objectbinder = Qva.GetObjectBinder(elem);
            var graphsize = objectbinder.GetImageSize (elem);
            if ((IS_GECKO || IS_CHROME)) {
                graphsize.width -= 3;
                graphsize.height -= 1;
            }
            if (sendsizetoserver && (this.GraphWidth != graphsize.width || this.GraphHeight != graphsize.height)) {
                this.GraphWidth = graphsize.width;
                this.GraphHeight = graphsize.height;
                sizeupdated = true;
            }
            url += '&width=' + escape (graphsize.width);
            url += '&height=' + escape (graphsize.height);
        }
        if (this.avq_url) {
            imageelem.src = Qva.AddProxyIfAny(url);
        }
        if (sizeupdated) {
            this.PageBinder.SetSize(this.Name, this.GraphWidth + ":" + this.GraphHeight);
        }
        imageelem.style.display = "block";
    } else {
        Assert(false);
    }
}

Qva.Mgr.dropdown = function(owner, elem, name, prefix) {
    this.Name = Qva.MgrMakeName((name != null) ? name : '', prefix);
    owner.AddManager(this);
    this.Element = elem;
    owner.Append(this, this.Name, 'menu');
    elem.disabled = true;
    elem.style.backgroundImage = 'url(/qvajaxzfc/qvsviewclient.aspx?datamode=binary&public=only&name=menu.DISABLED)';
    elem.onclick = function(event) {
        if (this.disabled) return;
        var binder = Qva.GetBinder(this.BinderId);
        Qva.HideContextMenu(true);
        var previousSibling = this.previousSibling;
        while (previousSibling && typeof previousSibling.tagName == "undefined") {
            previousSibling = previousSibling.previousSibling;
        }
        if (! previousSibling) {
            Assert (false);
            return;
        }  
        var pos = Qva.GetAbsolutePageCoords(previousSibling);
        pos.x += 2;
        pos.y += 19;
        binder.OnCreateContextDropDown(binder, event, this.Name, null, pos);
    }
    elem.Name = this.Name;
    elem.BinderId = this.BinderId;
}

Qva.Mgr.dropdown.prototype.Paint = function(mode, node) {
    this.Touched = true;
    this.Element.style.display = (mode != 'n') ? '' : 'none';
    this.Element.disabled = mode != 'e';
    var binder = Qva.GetBinder(this.BinderId);
    this.Element.style.backgroundImage = 'url(' + binder.BuildBinaryUrl(null, null, this.Element.disabled ? 'menu.DISABLED' : 'menu') + ')';
}

Qva.Mgr.menu = function(owner, elem, name, prefix) {
    this.Name = Qva.MgrMakeName((name != null) ? name : '', prefix);
    owner.AddManager(this);
    this.Element = elem;
    this.binderid = owner.binderid;
    owner.Append(this, this.Name, 'menu');
    Qva.SetContextMenu (elem, function (event) {
        if (! event) event = window.event;
        event.cancelBubble = true;
        return false;
    });
}

function ShowSubMenu (elem) {
    var row = elem.parentNode;
    if (row.AvqMgr.SubMenuRow != null && row.AvqMgr.SubMenuRow != row && row.AvqMgr.SubMenuRow.SubMenu) {
        row.AvqMgr.SubMenuRow.SubMenu.style.display = 'none';
    }
    row.AvqMgr.SubMenuRow = row;
    if (!row.SubMenu) {
        row.SubMenu = document.createElement ('table');
        row.SubMenu.className = "contextsubmenu";
        var at = Qva.GetPageCoords(row);
        row.SubMenu.style.top = at.y + Qva.GetScrollTop() + "px"; //1 + "px";
        row.SubMenu.style.left = at.x + row.offsetWidth + Qva.GetScrollLeft() + "px";
        document.body.appendChild(row.SubMenu);
        row.AvqMgr.AppendMenuItems (row.submenunode.getElementsByTagName ("action"), row.SubMenu, 1);
    }
    row.AvqMgr.SubMenuRow.SubMenu.style.display = '';
}

function HighLightRow (elem) {
    var row = elem.parentNode;
    row.className = "ContextHighlightedRow"; 
}

function LeaveRow (elem) {
    var row = elem.parentNode;
    row.className = "ContextRow"; 
}

function HideSubMenu (elem) {
    var row = elem.parentNode;
    if (row.AvqMgr.SubMenuRow != null && row.AvqMgr.SubMenuRow.SubMenu) {
        row.AvqMgr.SubMenuRow.SubMenu.style.display = 'none';
    }
}

Qva.Mgr.menu.prototype.AppendMenuItems = function(itemnodes, element, level) {
    var currentrow = 0;
    var icononly = true;
    var numberofactions = 0;
    for (var i = 0; i < itemnodes.length; i++) {
        var text = itemnodes[i].getAttribute("text");
        var isdivider = itemnodes[i].getAttribute("type") == "divider";
        var iconnodes = itemnodes[i].getElementsByTagName("icon");
        var nodelevel = parseInt(itemnodes[i].getAttribute("level"));
        if (nodelevel != level) continue;
        if (!text && !isdivider && iconnodes.length == 0) {
            Assert(false);
            continue;
        }
        if (isdivider && i == (itemnodes.length - 1)) break;
        numberofactions++;
        var row;
        var cell1;
        var cell2;
        var cell3;
        if (icononly) {
            icononly = (!text && !isdivider)
        }
        if (element.rows[currentrow] && element.rows[currentrow].cells[0]) {
            row = element.rows[currentrow];
            cell1 = row.cells[0];
            if (!icononly) cell2 = row.cells[1];
        } else {
            row = element.insertRow(-1);
            cell1 = row.insertCell(-1);
            if (!icononly) cell2 = row.insertCell(-1);
        }
        row.className = "ContextRow";
        currentrow++;
        cell1.style.width = "16px";
        if (!icononly) {
            if (row.cells[1]) {
                cell2 = row.cells[1];
            } else {
                cell2 = row.insertCell(-1);
            }
            cell2.className = "ContextTextCell";
            if (row.cells[2]) {
                cell3 = row.cells[2];
            } else {
                cell3 = row.insertCell(-1);
            }
            element.style.width = "";
        } else {
            element.style.width = "20pt";
        }

        if (itemnodes[i].getAttribute("type") == "divider") {
            var div = document.createElement('div');
            div.className = "ContextDivider";
            div.style.width = "100%";
            div.style.height = "1px";
            cell3.appendChild(div);
            cell3.style.padding = "0px 0px 0px 0px";
            cell3.style.height = "1px";
            var div = document.createElement('div');
            div.className = "ContextDivider";
            div.style.width = "100%";
            div.style.height = "1px";
            cell2.appendChild(div);
            cell2.style.padding = "0px 0px 0px 10px";
            cell2.style.height = "1px";
            cell1.style.padding = "0px";
            cell1.style.height = "1px";
        } else {
            for (var iIcon = 0; iIcon < iconnodes.length; iIcon++) {
                if (level != parseInt(iconnodes[iIcon].getAttribute("level"))) continue;
                var url = this.PageBinder.BuildBinaryUrl(iconnodes[iIcon].getAttribute("path"), iconnodes[iIcon].getAttribute("stamp"), iconnodes[iIcon].getAttribute("name"));
                if (iconnodes[iIcon].getAttribute("url")) url = iconnodes[iIcon].getAttribute("url");
                cell1.innerHTML = '<img alt="" src="' + url + '" style="' + iconnodes[iIcon].getAttribute("style") + ((level == 0 && !icononly) ? '" class="Qv_menu" />' : '" />');
            }
            if (!isdivider) {
                var action = itemnodes[i].getAttribute("name");
                if (!icononly) {
                    cell2.innerText = text ? text : " ";
                    var tt = itemnodes[i].getAttribute("tooltip");
                    if (tt) cell2.title = tt;
                    var shortcuttext = Qva.SpecialActions.GetShortcutText(action);
                    if (shortcuttext != "") {
                        cell3.innerText = shortcuttext;
                        cell3.className = "Qv_SubMenuContent";
                    }
                }
                var disabled = itemnodes[i].getAttribute("mode") == "disabled";
                row.disabled = disabled;
                row.binderid = this.binderid;
                if (!disabled) {
                    var submenu = itemnodes[i].getAttribute("submenu") == "true";
                    row.AvqMgr = this;
                    if (submenu) {
                        var img = document.createElement('img');
                        img.src = this.PageBinder.BuildBinaryUrl(null, null, "web:submenu");
                        cell3.className = "Qv_SubMenu";
                        cell3.appendChild(img);

                        row.submenunode = itemnodes[i];
                        cell1.onmouseover = function() {
                            ShowSubMenu(this);
                            HighLightRow(this);
                        }
                        cell2.onmouseover = function() {
                            ShowSubMenu(this);
                            HighLightRow(this);
                        }
                        cell3.onmouseover = function() {
                            ShowSubMenu(this);
                            HighLightRow(this);
                        }
                    } else {
                        var clientaction = itemnodes[i].getAttribute("clientaction");
                        if (action) {
                            row.NeedCursor = itemnodes[i].getAttribute("cursor") == "needed";
                            row.onclick = onclick_action;
                            row.Action = this.Context;
                            if (action != "*") row.Action += "." + action;
                            var index = itemnodes[i].getAttribute("index");
                            row.ObjectId = this.Context;
                            if (index) {
                                row.Position = index;
                            } else if (this.Position) {
                                row.Position = this.Position;
                            }
                        } else if (clientaction) {
                            row.onclick = onclick_ContextClientAction;
                            row.Name = this.Context;
                            row.ObjectId = this.Context.replace(this.PageBinder.DefaultScope + ".", "");
                            row.clientaction = clientaction;
                            row.param = itemnodes[i].getAttribute("param");
                        }
                        if (nodelevel == 0) {
                            cell1.onmouseover = function() {
                                HideSubMenu(this);
                                HighLightRow(this);
                            }
                            if (!icononly) {
                                cell2.onmouseover = function() {
                                    HideSubMenu(this);
                                    HighLightRow(this);
                                }
                                cell3.onmouseover = function() {
                                    HideSubMenu(this);
                                    HighLightRow(this);
                                }
                            }
                        } else {
                            cell1.onmouseover = function() {
                                HighLightRow(this);
                            }
                            if (!icononly) {
                                cell2.onmouseover = function() {
                                    HighLightRow(this);
                                }
                                cell3.onmouseover = function() {
                                    HighLightRow(this);
                                }
                            }
                        }
                    }
                    cell1.onmouseout = function() { LeaveRow(this); }
                    if (!icononly) {
                        cell2.onmouseout = function() { LeaveRow(this); }
                        cell3.onmouseout = function() { LeaveRow(this); }
                    }
                } else {
                    cell1.style.color = "InactiveCaptionText";
                    if (!icononly) cell2.style.color = "InactiveCaptionText";
                }
            } else {
                Assert(false);
            }
        }
    }
    return numberofactions;
}

Qva.Mgr.menu.prototype.Paint = function(mode, node) {
    var itemnodes = node.getElementsByTagName("action");
    if (itemnodes.length == 0) {
        Qva.HideContextMenu(true);
        return;
    }
    this.Touched = true;
    this.Context = this.PageBinder.DefaultScope + "." + node.getAttribute("context");
    this.Position = node.getAttribute("position");
    if (this.IsDropDown) {
        var maxheight = parseInt(node.getAttribute("maxheight"));
        if (isNaN(maxheight)) {
            this.Element.parentNode.style.width = "";
            this.Element.parentNode.style.height = "";
            this.Element.parentNode.style.overflowY = "";
            this.Element.parentNode.style.overflowX = "";
        } else {
            this.Element.parentNode.style.width = "160px";
            this.Element.parentNode.style.height = maxheight + "px";
            this.Element.parentNode.style.overflowY = "scroll";
            this.Element.parentNode.style.overflowX = "visible";
        }
    }
    var numberofactions = this.AppendMenuItems(itemnodes, this.Element, 0);
    if (numberofactions == 0) {
        Qva.HideContextMenu(true);
    } else {
        if (this.Element.offsetHeight + this.Element.offsetTop - Qva.GetScrollTop() > Qva.GetViewportHeight()) {
            var top = Math.max(0, this.Element.offsetTop - this.Element.offsetHeight);
            this.Element.style.top = top + "px";
        }
        if (this.Element.offsetWidth + this.Element.offsetLeft - Qva.GetScrollLeft() > Qva.GetViewportWidth()) {
            var left = Math.max(0, this.Element.offsetLeft - this.Element.offsetWidth);
            this.Element.style.left = left + "px";
        }
    }
}

Qva.Mgr.toolbar = function (owner, elem, name, prefix) {
    this.Name = Qva.MgrMakeName ((name != null) ? name : '', prefix);
    owner.AddManager(this);
    this.Element = elem;
    this.binderid = owner.binderid;
    owner.Append (this, this.Name, 'toolbar');
    Qva.StandardActions = this;
}


Qva.SpecialActions = {};
Qva.SpecialActions.list = [];
Qva.SpecialActions.AddAction = function (id, keycode, document, ctrl, shift) {
    this [id] = { "keycode": keycode, "document": document, "ctrl": ctrl, "shift": shift };
    this.list [this.list.length] = { "id": id, "keycode": keycode, "document": document, "ctrl": ctrl, "shift": shift }
}
Qva.SpecialActions.AddAction ("REDO", 89, true, true, false);
Qva.SpecialActions.AddAction ("UNDO", 90, true, true, false);
Qva.SpecialActions.AddAction ("LS", 76, true, true, true);
Qva.SpecialActions.AddAction ("US", 85, true, true, true);
Qva.SpecialActions.AddAction ("CLEARSTATE", 68, true, true, true);
Qva.SpecialActions.AddAction ("BCK", 37, true, false, true);
Qva.SpecialActions.AddAction ("FWD", 39, true, false, true);

Qva.SpecialActions.GetAction = function (event) {
    for (var i = 0; i < this.list.length; i++) {
        var item = this.list [i];
        if (item.keycode == event.keyCode) {
            if (item.ctrl == ctrlKeyPressed (event) && item.shift == event.shiftKey) {
                return item;
            }
            return null;
        }
    }
    return null;
}

Qva.SpecialActions.GetShortcutText = function (id) {
    var action = this [id];
    if (action) {
        var text = "\t";
        if (action.ctrl) text += "Ctrl+ ";
        if (action.shift) text += "Shift+ ";
        if (action.keycode == 37) {
            text += "Left Arrow";
        } else if (action.keycode == 39) {
            text += "Right Arrow";
        } else {
            text += String.fromCharCode (action.keycode);
        }
        return text;
    }
    return "";
}

Qva.Mgr.toolbar.prototype.HandleKeyUp = function (event) {
    var action = Qva.SpecialActions.GetAction (event);
    if (action && action.document) {
        this.PageBinder.Set (this.Name + "." + action.id, "action", "", true);
        return true;
    }
    return false;
}

Qva.Mgr.toolbar.prototype.Paint = function(mode, node) {
    this.Touched = true;
    var element = this.Element;

    element.style.display = Qva.MgrGetDisplayFromMode(this, mode);
    if (element.style.display == 'none') return;

    if (IS_TOUCHENABLED) { element.style.height = "36px"; }
    var closebtn = document.getElementById("Close");
    var transnodes = node.getElementsByTagName("translate");
    for (var t = 0; t < transnodes.length; t++) {
        var name = transnodes[t].getAttribute("name");
        var text = transnodes[t].getAttribute("text");
        this.PageBinder.Translations[name] = text;
        if (closebtn) {
            switch (name) {
                case "Close":
                    closebtn.innerText = text;
                    break;
                case "Restore":
                    closebtn.RestoreText = text;
                    break;
            }
        }
    }

    var itemnodes = node.getElementsByTagName("action");

    var table = element.tagName == "TABLE" ? element : element.getElementsByTagName("table")[0];
    if (!table) {
        table = document.createElement("table");
        element.appendChild(table);
    }
    var row;
    if (table.rows[0]) {
        row = table.rows[0];
    } else {
        row = table.insertRow(-1);
    }
    var selects = node.getElementsByTagName("value");
    var helps = node.getElementsByTagName("help");
    var NumberOfTR = itemnodes.length + 1 + selects.length + (helps.length == 1 ? 1 : 0);
    if (row.cells.length > 0 && (NumberOfTR != row.cells.length)) {
        table.deleteRow(0);
        row = table.insertRow(-1);
    }

    var cellcount = 0;
    for (var i = 0; i < itemnodes.length; i++) {
        var action = itemnodes[i].getAttribute("name");
        var clientaction = itemnodes[i].getAttribute("clientaction");
        var iconnodes = itemnodes[i].getElementsByTagName("icon");

        /*
        TEMPORARY HACK
        */
        if (IS_TOUCHENABLED && (action === 'CS' || action === 'REPOSITORY' || action === 'NEWSHEETOBJ' || action === 'SHOWFIELDS' || iconnodes[0].getAttribute('name') === 'NEWCHART')) { continue; }

        var cell;
        if (row.cells[cellcount]) {
            cell = row.cells[cellcount];
        } else {
            cell = row.insertCell(-1);

        }
        cellcount++;

        cell.style.width = (IS_TOUCHENABLED ? "36px" : "20px");
        cell.align = "center";
        cell.className = "";
        cell.binderid = this.binderid;

        var text = itemnodes[i].getAttribute("text");
        cell.title = text;

        if (iconnodes.length == 0) {
            cell.innerText = text;
            cell.style.width = itemnodes[i].getAttribute("width") || "";
        }

        for (var iIcon = 0; iIcon < iconnodes.length; iIcon++) {
            if (iconnodes[iIcon].getAttribute("usage") != null && iconnodes[iIcon].getAttribute("usage") != "caption") continue;
            var url = this.PageBinder.BuildBinaryUrl(iconnodes[iIcon].getAttribute("path"), iconnodes[iIcon].getAttribute("stamp"), iconnodes[iIcon].getAttribute("name"));
            if (IS_TOUCHENABLED) {
                url += '&width=32&height=32';
                cell.innerHTML = '<img title="' + text + '" style="height:32px; width:32px;" alt="&nbsp;" src="' + url + '" />';
            } else {
                cell.innerHTML = '<img title="' + text + '" style="height:16px; width:16px;" alt="&nbsp;" src="' + url + '" />';
            }
        }
        if (action || clientaction) {
            if (action) {
                cell.onclick = onclick_action;
                cell.Action = this.Name + "." + action;
            }
            if (clientaction) {
                cell.onclick = onclick_ContextClientAction;
                cell.Name = this.Name;
                cell.AvqMgr = this;
                cell.clientaction = clientaction;
                cell.param = itemnodes[i].getAttribute("param");
            }
            cell.disabled = itemnodes[i].getAttribute("mode") == "disabled";
            cell.onmouseover = function() { this.className = "ToolBarHighlightedButton"; }
            cell.onmouseout = function() { this.className = "ToolBarButton"; }
            cell.className = "ToolBarButton";
        } else {
            cell.style.padding = "0px";
            cell.style.backgroundColor = "#cccccc";
            cell.style.width = "1px";
        }
    }
    var name = this.PageBinder.DefaultScope + ".Bookmarks";
    var cell;
    if (row.cells[cellcount]) {
        cell = row.cells[cellcount];
    } else {
        cell = row.insertCell(-1);
    }
    cellcount++;
    if (cell.Name != name) {
        cell.Name = name;
        cell.className = "QvToolbarDropdownCell";
        var select = window.document.createElement("SELECT");
        cell.appendChild(select);
        new Qva.Mgr.bookmarkselect(this.PageBinder, select, name);
    }
    for (var iSelect = 0; iSelect < selects.length; iSelect++) {
        var name = this.Name + "." + selects[iSelect].getAttribute("name");
        var cell;
        if (row.cells[cellcount]) {
            cell = row.cells[cellcount];
        } else {
            cell = row.insertCell(-1);
        }
        cellcount++;
        if (cell.Name == name) continue;
        cell.Name = name;
        cell.className = "QvToolbarDropdownCell";
        var select = window.document.createElement("SELECT");
        cell.appendChild(select);
        var xmgr = new Qva.Mgr.select(this.PageBinder, select, name);
        xmgr.Lock = null;
        xmgr.Unlock = null;
    }
    if (helps.length == 1) {
        var cell;
        if (row.cells[cellcount]) {
            cell = row.cells[cellcount];
        } else {
            cell = row.insertCell(-1);
        }
        cellcount++;
        var url = this.PageBinder.BuildBinaryUrl(null, null, "WEBHELP");
        if (IS_TOUCHENABLED) {
            url += '&width=32&height=32';
            cell.innerHTML = '<img title="' + helps[0].getAttribute("text") + '" style="height:32px; width:32px;" alt="&nbsp;" src="' + url + '" />';
        } else {
            cell.innerHTML = '<img title="' + helps[0].getAttribute("text") + '" style="height:16px; width:16px;" alt="&nbsp;" src="' + url + '" />';
        }
        cell.HelpUrl = helps[0].getAttribute("url");
        cell.onmouseover = function() { this.className = "ToolBarHighlightedButton"; }
        cell.onmouseout = function() { this.className = "ToolBarButton"; }
        cell.style.paddingLeft = "6px";
        cell.style.paddingRight = "6px";
        cell.onclick = function() { Qva.Help.Show(this.HelpUrl); }
    }
}


Qva.MgrLinkScan = function (mgr, owner, lnkname, namePrefix) {
    mgr.LinkParts = lnkname.split ('$');
    var plen = mgr.LinkParts.length;
    mgr.LinkArgs = lnkname.split ('$');
    if (plen >= 2) {
        for (var lix = 1; lix < plen; lix += 2) {
            var name = namePrefix + mgr.LinkParts [lix];
            owner.Append (mgr, name);
            mgr.LinkParts [lix] = name;
        }
    }
};

function onclick_link () { 
    window.open (this.link);
}

Qva.Mgr.link = function (owner, elem, name, prefix, path) {
    if (!Qva.MgrSplit (this, name, prefix)) return;
    Qva.MgrLinkScan (this, owner, path, prefix);
    owner.AddManager(this);
    this.Element = elem;
    elem.Name = this.Name;
    elem.binderid = owner.binderid;
    elem.onclick = onclick_link;
}

Qva.Mgr.link.prototype.Paint = function (mode, node, name) {
    this.Touched = true;
    var element = this.Element;
    if (element.Name == name) {
        element.style.display = Qva.MgrGetDisplayFromMode (this, mode);
    }
    for (var lix = 1; lix < this.LinkParts.length; lix += 2) {
        if (this.LinkParts [lix] == name) {
            this.LinkArgs [lix] = node.getAttribute ("text");
        }
    }
    element.link = "";
    for (var lix = 0; lix < this.LinkArgs.length; lix ++) {
        element.link += this.LinkArgs [lix];
    }
}

Qva.Mgr.hover = function (owner, elem, name, prefix) {
    if (!Qva.MgrSplit (this, name, prefix)) return;
    owner.AddManager(this);
    this.Element = elem;
}

Qva.Mgr.hover.prototype.Paint = function(mode, node, name) {
    var element = this.Element;
    element.style.display = Qva.MgrGetDisplayFromMode (this, mode);
    element.innerHTML = "";
    var lines = node.getElementsByTagName ("line");
    for (var i = 0; i < lines.length; i++) {
        var p = document.createElement ("span");
        p.innerText = lines [i].getAttribute ("text");
        element.appendChild (p);
        var br = document.createElement ("br");
        element.appendChild (br);
    }
}

function SetRelatedElementsZindex (objectname, zindex) {
    var frame = window.document.getElementById (objectname);
    if (frame) {
        var oldzindex = frame.style.zIndex;
        if (zindex == oldzindex) return;
        frame.style.zIndex = zindex;
    }
    var ids = [objectname + "_minimized", objectname + "_minimized_resize", objectname + "_minimized_move", objectname + "_frame_resize", objectname + "_frame_move"];
    for (var i = 0; i < ids.length; i++) {
        var elem = window.document.getElementById (ids[i]);
        if (elem) elem.style.zIndex = zindex;
    }
}

Qva.Mgr.dlg = function(owner, elem, name, prefix, dlgname) {
    if (!Qva.MgrSplit(this, name, prefix)) return;
    owner.AddManager(this);
    this.Element = elem;
    if (dlgname.toUpperCase() == "EXPRESSION.QVPP") this.IsExpression = true;

    elem.binderid = owner.binderid;
    elem.Name = this.Name;
    elem.AvqDialog = dlgname;

    elem.onclick = Qva.Mgr.dlg.OnClick;
    elem.style.borderColor = 'gainsboro';
    elem.onmouseover = function() { if (!this.disabled) this.style.borderColor = 'darkgray'; }
    elem.onmouseout = function() { this.style.borderColor = 'gainsboro'; }
}

Qva.Mgr.dlg.prototype.Paint = function(mode, node) {
    this.Touched = true;
    var element = this.Element;
    element.disabled = (mode != 'e');
    if (this.IsExpression) {
        element.ExpressionType = node.getAttribute('expression');
        element.Value = node.getAttribute('text');
    }
    element.title = node.getAttribute('label');
    if (element.tagName == 'IMG') {
        var icon = element.getAttribute("icon");
        if (icon) {
            if (element.disabled) icon += ".DISABLED";
            var binder = Qva.GetBinder(element.binderid);
            var url = binder.BuildBinaryUrl(null, null, icon);
            element.src = url;
        }
    }

    element.style.display = Qva.MgrGetDisplayFromMode(this, mode);
}

Qva.Mgr.dlg.OnClick = function() {
    if (this.disabled) return;
    var pageBinder = Qva.GetBinder(this.binderid);
    if (this.ExpressionType) {
        Qva.ExpressionDialog(this, this.Value);
    } else if (Qva.ContextDialogBtn == this) {
        Qva.HideContextDialog();
    } else {
        var elem = this;
        pageBinder.LoadHtml(this.AvqDialog, function(body) {
            Qva.ShowContextDialog(elem, body);
            pageBinder.Refresh();
        });
    }
}

Qva.Mgr.panel = function(owner, elem, name, prefix, dlgname) {
    if (!Qva.MgrSplit(this, name, prefix)) return;
    owner.AddManager(this);
    this.Element = elem;
    elem.binderid = owner.binderid;
    elem.Name = this.Name;
    elem.cells[0].onclick = function() { Qva.SelectToolPane(this, false); }
    elem.cells[1].onclick = function() { Qva.SelectToolPane(this, true); }
    var pageBinder = Qva.GetBinder(elem.binderid);
    var icon = document.createElement("img");
    icon.src = pageBinder.BuildBinaryUrl('', null, "web:unpinned");
    elem.cells[1].appendChild(icon);

    var tcell = elem.parentNode.parentNode.rows[elem.rowIndex + 1].cells[0];
    pageBinder.LoadHtml(dlgname, function(body) {
        tcell.innerHTML = body;
        owner.Scan(elem.Name, tcell);
    });
}

Qva.Mgr.style = function (owner, elem, name, prefix) {
    if (!Qva.MgrSplit (this, name, prefix)) return;
    owner.AddManager(this);
    this.Element = elem;
    this.InlineStyle = owner.InlineStyle;
}

Qva.Mgr.style.prototype.Paint = function(mode, node) {
    this.Touched = true;
    if (this.InlineStyle) {
        SetElementStyle (this.Element, "fontFamily", node.getAttribute ('fontfamily'));
        SetElementStyle (this.Element, "fontSize", node.getAttribute ('fontsize'), "pt");
        SetElementStyle (this.Element, "fontStyle", node.getAttribute ('fontstyle'));
        SetElementStyle (this.Element, "fontWeight", node.getAttribute ('fontweight'));
        SetElementStyle (this.Element, "textAlign", node.getAttribute ('textalign'));
        SetElementStyle (this.Element, "verticalAlign", node.getAttribute ('verticalalign'));
        SetElementStyle (this.Element, "textDecoration", node.getAttribute ('textdecoration'));
        SetElementStyle (this.Element, "paddingTop", node.getAttribute ('paddingTop'), "px");
        SetElementStyle (this.Element, "paddingLeft", node.getAttribute ('paddingLeft'), "px");
        SetElementStyle (this.Element, "paddingRight", node.getAttribute ('paddingRight'), "px");
        SetElementStyle (this.Element, "paddingBottom", node.getAttribute ('paddingBottom'), "px");
        if (node.getAttribute ('color')) this.Element.style.color = HtmlifyARGB (node.getAttribute ('color'));
    }
}

Qva.Mgr.bookmarkselect = function(owner, elem, name) {
    if (owner.BookmarksSubscribers) owner.BookmarksSubscribers[owner.BookmarksSubscribers.length] = this;
    this.Element = elem;
    elem.binderid = owner.binderid;
    elem.Name = name;
    elem.onchange = Qva.Mgr.bookmarkselect.OnChange;
    elem.onclick = Qva.CancelBubble;
    if (owner.BookmarksMgr) this.Paint(owner.BookmarksMgr);
}

Qva.Mgr.bookmarkselect.prototype.Paint = function(BookMarkObject) {
    if (!BookMarkObject) return;
    var element = this.Element;
    var optlen = element.options.length;
    var cholen = BookMarkObject.BookMarks.length;
    element.options.length = cholen;
    if (cholen >= 1) {
        this.SelectedIndex = -1;
        for (var ix = 0; ix < cholen; ++ix) {
            var cho = BookMarkObject.BookMarks[ix];
            var optval = "" + cho.text;
            var opt = element.options[ix];
            opt.text = optval.replace(/\t/g, ' ');
            opt.value = cho.value;
            var selected = false;
            if (opt.text == BookMarkObject.CurrentText) {
                this.SelectedIndex = ix;
                selected = true;
            }
            if (selected) opt.selected = true;
        }
        if (this.SelectedIndex == -1) {
            element.options[cholen - 1].selected = true;
        }
    }
    element.disabled = false;
}

Qva.Mgr.bookmarkselect.OnChange = function() {
    var binder = Qva.GetBinder(this.binderid);
    if (!binder.Enabled) return;
    if (this.selectedIndex < 0) return;
    var opt = this.options[this.selectedIndex];
    binder.Set(this.Name, 'value', opt.value, true);
}

Qva.Mgr.bookmarks = function(owner, name) {
    if (!Qva.MgrSplit(this, name, "Document")) return;
    owner.AddManager(this);
    this.BookMarks = [];
}

Qva.Mgr.bookmarks.prototype.Paint = function(mode, node, name) {
    this.CurrentText = node.getAttribute("text");
    this.Mode = node.getAttribute("mode")
    var choice = node.firstChild;
    this.BookMarks.length = 0;
    if (choice.nodeName == "choice") {
        for (var chgrp = choice.firstChild; chgrp; chgrp = chgrp.nextSibling) {
            var bookmark = {};
            ObjectifyXmlAttributes(bookmark, chgrp);
            this.BookMarks[this.BookMarks.length] = bookmark;
        }
    } else {
        Assert(false);
    }
    this.PageBinder.UpdateBookMarks();
}

Qva.Mgr.bookmarks.prototype.NewBookmark = function(name, additive, share, excludeselections, layoutstate, hide, showpopupinfo, infomsg, inputfieldvaluesflag) {
    var params = {};
    params.bmname = name;
    if (additive) params.additive = "true"
    if (share) params.share = "true"
    if (excludeselections) params.excludeselections = "true"
    if (layoutstate) params.layoutstate = "true"
    if (hide) params.hide = "true"
    if (showpopupinfo) params.showpopupinfo = "true"
    if (infomsg) params.infomsg = infomsg;
    if (inputfieldvaluesflag) params.inputfieldvaluesflag = "true"
    this.PageBinder.Set(this.Name, params);
}

Qva.Mgr.bookmarks.prototype.SelectBookmark = function(id) {
    this.PageBinder.Set(this.Name, "value", id, true);
}

Qva.Mgr.bookmarks.prototype.DeleteBookmark = function(id) {
    this.PageBinder.Set(this.Name, "delete", id, true);
}


Qva.Mgr.selectionsstyle = function(owner, name) {
    if (!Qva.MgrSplit(this, name, "Document")) return;
    owner.AddManager(this);
    this.Styles = [];
}

Qva.Mgr.selectionsstyle.prototype.GetSelectionStyle = function(state) {
    for (var iStyle = 0; iStyle < this.Styles.length; iStyle++) {
        if (this.Styles[iStyle].name == state) return this.Styles[iStyle];
    }
    Assert (false);
    return null;
}

Qva.Mgr.selectionsstyle.prototype.Paint = function(mode, node, name) {
    var styleNode = node.firstChild;
    this.Styles.length = 0;
    if (styleNode.nodeName == "state") {
        for (; styleNode; styleNode = styleNode.nextSibling) {
            var style = {};
            style.name = styleNode.getAttribute ("name");
            ObjectifyXmlAttributes(style, styleNode);
            this.Styles[this.Styles.length] = style;
        }
    } else {
        Assert(false);
    }
    var stylesheetfound = false;
    for (var i = 0; i < document.styleSheets.length; i++) {
        if (stylesheetfound) break;
        var rules;
        try {
            rules = document.styleSheets[i].rules ? document.styleSheets[i].rules : document.styleSheets[i].cssRules;
        } catch (Error) { continue; }
        if (! rules) continue;
        for (var iStyle = 0; iStyle < this.Styles.length; iStyle++) {
            for (var is = 0; is < rules.length; is++) {
                var style = rules[is];
                if (style.selectorText == (".Qv" + this.Styles[iStyle].name)) {
                    stylesheetfound = true;
                    style.style.color = HtmlifyARGB(this.Styles[iStyle].color);
                    var backgroundcolor = Qva.Hex2CSSExpression(this.Styles[iStyle].backgroundcolor);
                    if (! backgroundcolor) backgroundcolor = HtmlifyARGB(this.Styles[iStyle].backgroundcolor);
                    style.style.backgroundColor = backgroundcolor;
                }
            }
        }
    }
    Assert(stylesheetfound);
}

