function QButton_update() {
    with (this) {
        image.src = ((!enabled && res.imgD) || (value ? res.imgP : res.imgN)).src;
    }
}

function QButton_doEvent() {
    with (this) {
        if (enabled) {
            if (res.style == 1) {
                this.value = value ? 0 : 1;
                update();
            }
            onClick(value, tag);
        }
    }
    return false;
}

function QButton_enable(state) {
    this.enabled = state;
    this.update();
}

function QButton_set(value) {
    if (this.enabled) {
        this.value = value ? 1 : 0;
        this.update();
    }
    return true;
}

function QButton(parent, name, res, tooltip) {
    this.init(parent, name);
    if (res) {
        this.res = res;
        this.tip = tooltip || "";
        this.enabled = true;
        this.value = 0;
        this.set = QButton_set;
        this.enable = QButton_enable;
        this.update = QButton_update;
        this.doEvent = QButton_doEvent;
        this.onClick = QControl.event;
        with (this) {
            document.write('<a href="#" hidefocus="true" unselectable="on"' +
                (tip ? ' title="' + tip + '"' : '') + ' onClick="return ' + name +
                '.doEvent()" onMouseOver="' + (res.style == 2 ? name + '.set(1);' : '') +
                'window.top.status=' + name + '.tip;return true" onMouseOut="' +
                (!res.style || (res.style == 2) ? name + '.set();' : '') + 'window.top.status=\'\'"' +
                (!res.style ? ' onMouseDown="return ' + name + '.set(1)" onMouseUp="return ' + name + '.set()"' : '') +
                '><img class="qbutton" name="' + id + '" src="' + res.imgN.src + '" border="0" width="' +
                res.width + '" height="' + res.height + '"></a>');
            this.image = document.images[id] || new Image(1, 1);
        }
    } else {
        this.document.write("invalid resource");
    }
}
QButton.prototype = new QControl();
QButton.NORMAL    = 0;
QButton.CHECKBOX  = 1;
QButton.WEB       = 2;
QButton.SIGNAL    = 3;

function QControl_init(parent, name) {
    this.parent = parent || self;
    this.window = (parent && parent.window) || self;
    this.document = (parent && parent.document) || self.document;
    this.name = (parent && parent.name) ? (parent.name + "." + name) : ("self." + name);
    this.id = "Q";
    var h = this.hash(this.name);
    for (var j=0; j<8; j++) {
        this.id += QControl.HEXTABLE.charAt(h & 15);
        h >>>= 4;
    }
}

function QControl_hash(str) {
    var h = 0;
    if (str) {
        for (var j=str.length-1; j>=0; j--) {
            h ^= QControl.ANTABLE.indexOf(str.charAt(j)) + 1;
            for (var i=0; i<3; i++) {
                var m = (h = h<<7 | h>>>25) & 150994944;
                h ^= m ? (m == 150994944 ? 1 : 0) : 1;
            }
        }
    }
    return h;
}

function QControl_nop() {
}

function QControl() {
    this.init = QControl_init;
    this.hash = QControl_hash;
    this.window = self;
    this.document = self.document;
    this.tag = null;
}
QControl.ANTABLE  = "w5Q2KkFts3deLIPg8Nynu_JAUBZ9YxmH1XW47oDpa6lcjMRfi0CrhbGSOTvqzEV";
QControl.HEXTABLE = "0123456789ABCDEF";
QControl.nop = QControl_nop;
QControl.event = QControl_nop;

function QButtonRes(style, width, height, normal, pressed, disabled) {
    this.style = style;
    this.width = width;
    this.height = height;
    this.imgN = new Image(width, height);
    this.imgN.src = normal;
    this.imgP = new Image(width, height);
    this.imgP.src = pressed;
    if (disabled) {
        this.imgD = new Image(width, height);
        this.imgD.src = disabled;
    }
}
