var innovaphone = innovaphone || {};
innovaphone.pbx = innovaphone.pbx || {};
innovaphone.pbx.appclient = innovaphone.pbx.appclient || {};
var icons = {
    trace: { viewbox: '0 0 24 24', code: '<path d="M16.32,14.17a9.19,9.19,0,0,0,1.53-5.09A9,9,0,0,0,8.92,0,9,9,0,0,0,0,9.08a9,9,0,0,0,8.92,9.09,8.78,8.78,0,0,0,5.52-2L18.06,20,20,18Zm-7.4,1.2A6.24,6.24,0,0,1,2.75,9.08,6.24,6.24,0,0,1,8.92,2.8,6.24,6.24,0,0,1,15.1,9.08,6.24,6.24,0,0,1,8.92,15.37ZM9.8,9.8H8.17V4.9H9.8Zm0,3.27H8.17V11.44H9.8Z"></path>' },
    externalapps: { viewbox: '0 0 24 24', code: '<path d="M17.51,6.66l-3.08,3.89a4.7,4.7,0,0,0-.62-2.44l2.13-2.69a1.83,1.83,0,0,0-.3-2.56,1.84,1.84,0,0,0-2.57.3L8.93,8.4a1.88,1.88,0,0,0-.29.54,2,2,0,0,0-.09.8A1.79,1.79,0,0,0,9.24,11a1.67,1.67,0,0,0,.78.35L8.71,13A3.49,3.49,0,0,1,8,12.53a3.8,3.8,0,0,1-1.31-2A3.5,3.5,0,0,1,6.57,10a3.75,3.75,0,0,1,0-.67c0-.09,0-.18,0-.28a2.25,2.25,0,0,1,.06-.36c0-.11.06-.23.1-.34l.09-.24a4,4,0,0,1,.52-.92L11.5,1.92a3.83,3.83,0,0,1,6,4.74ZM13.43,10.2a1.53,1.53,0,0,0,0-.3c0-.13-.05-.26-.08-.38s-.05-.2-.08-.3L13.14,9h0l-.06-.12A2.06,2.06,0,0,0,13,8.59,3.12,3.12,0,0,0,12.57,8h0A2.75,2.75,0,0,0,12,7.47,3.49,3.49,0,0,0,11.29,7L10,8.68a1.8,1.8,0,0,1,.78.36,1.71,1.71,0,0,1,.51.63,2,2,0,0,1,.15.47v0a2.68,2.68,0,0,1,0,.49v0a1.83,1.83,0,0,1-.09.39h0a1.42,1.42,0,0,1-.08.18,1.82,1.82,0,0,1-.2.36h0L6.93,16.84a1.84,1.84,0,0,1-1.22.68H5.39l-.31-.05h0a1.82,1.82,0,0,1-1-2.91l2.11-2.66a5,5,0,0,1-.6-1.82c0-.21,0-.43,0-.64L2.49,13.33a3.85,3.85,0,0,0,.63,5.38h0a3.94,3.94,0,0,0,1.41.69l.14,0a3.75,3.75,0,0,0,.82.1h0a3.39,3.39,0,0,0,.45,0A3.83,3.83,0,0,0,8.5,18.08l4.14-5.24a3.94,3.94,0,0,0,.39-.61l.09-.22c.06-.15.12-.29.17-.44a2.61,2.61,0,0,0,.06-.27,2.93,2.93,0,0,0,.07-.41,2.29,2.29,0,0,0,0-.25s0,0,0-.06A2.38,2.38,0,0,0,13.43,10.2Z"></path>' },
    add: { viewbox: '0 0 24 24', code: '<path d="M8.24,8.24V0h3.52V8.24H20v3.52H11.76V20H8.24V11.76H0V8.24Z"></path>' },
    settings: { viewbox: '0 0 24 24', code: '<path d="M20.5,11.5c0-1.3,0.9-2.5,2.1-2.9C22.9,8.5,23,8.2,22.9,8c-0.2-0.7-0.5-1.3-0.8-2c-0.2-0.3-0.6-0.4-0.8-0.1 c-0.7,0.6-1.7,0.8-2.7,0.5c-0.9-0.3-1.7-1-2-2c-0.3-1-0.1-2,0.5-2.7C17.3,1.4,17.3,1,17,0.8c-0.6-0.3-1.3-0.6-2-0.8 c-0.3-0.1-0.6,0.1-0.7,0.3c-0.4,1.2-1.5,2.1-2.9,2.1S9,1.6,8.6,0.4C8.5,0.1,8.2-0.1,8,0C7.3,0.2,6.6,0.5,6,0.8 C5.7,1,5.6,1.4,5.8,1.7c0.6,0.7,0.8,1.7,0.5,2.7c-0.3,0.9-1,1.7-2,2c-1,0.3-2,0.1-2.7-0.5C1.4,5.6,1,5.7,0.8,6C0.5,6.6,0.2,7.3,0,8 c-0.1,0.3,0.1,0.6,0.3,0.7c1.2,0.4,2.1,1.5,2.1,2.9S1.6,14,0.4,14.3C0.1,14.4-0.1,14.7,0,15c0.2,0.7,0.5,1.3,0.8,2 c0.2,0.3,0.6,0.4,0.8,0.1c0.7-0.6,1.7-0.8,2.7-0.5c0.9,0.3,1.7,1,2,2c0.3,1,0.1,2-0.5,2.7C5.6,21.6,5.7,22,6,22.1 c0.6,0.3,1.3,0.6,2,0.8c0.3,0.1,0.6-0.1,0.7-0.3c0.4-1.2,1.5-2.1,2.9-2.1s2.5,0.9,2.9,2.1c0.1,0.3,0.4,0.4,0.7,0.3 c0.7-0.2,1.3-0.5,2-0.8c0.3-0.2,0.4-0.6,0.1-0.8c-0.6-0.7-0.8-1.7-0.5-2.7c0.3-0.9,1-1.7,2-2c1-0.3,2-0.1,2.7,0.5 c0.3,0.2,0.7,0.2,0.8-0.1c0.3-0.6,0.6-1.3,0.8-2c0.1-0.3-0.1-0.6-0.3-0.7C21.4,14,20.5,12.8,20.5,11.5z M11.4,16.7 c-2.9,0-5.2-2.3-5.2-5.2s2.3-5.2,5.2-5.2s5.2,2.3,5.2,5.2S14.3,16.7,11.4,16.7z"></path>' },
    checkmark: { viewbox: '-2 0 16 10', code: '<path d=\"M4.2,9.6L0,5.4L1.4,4l2.8,2.8L11,0l1.4,1.4L4.2,9.6z\"></path>' },
    download: { viewbox: '0 0 20 20', code: '<path d="M14.33,6.21l2.17,2.2L10,15,3.5,8.41l2.17-2.2L8.56,9.14V0h2.88V9.14ZM16.5,18H3.5v2h13Z"></path>' },
    info: { viewbox: '0 0 16 16', code: '<path d="M12,5H10V3h2ZM10,15.62,11,9V8H9.31L7,8.69v1.39l2.25-.7L8,16v1h2.12L13,14.92v-.69Z"></path>' },
    help: { viewbox: '0 0 32 32', code: '<path d="M8.62.38h2.07A3.3,3.3,0,0,1,14,3.67V5.92A3.78,3.78,0,0,1,12.38,9l-1.16.82a.52.52,0,0,0-.23.43v3.2a.52.52,0,0,1-.52.52H8.53A.52.52,0,0,1,8,13.46v-4a1.93,1.93,0,0,1,.84-1.6l1.39-1A1.81,1.81,0,0,0,11,5.44v-1A1.09,1.09,0,0,0,9.9,3.38H9.09A1.09,1.09,0,0,0,8,4.47v.78a.53.53,0,0,1-.53.53H5.53A.52.52,0,0,1,5,5.26V4A3.61,3.61,0,0,1,8.62.38ZM8.39,20h2.22a.4.4,0,0,0,.39-.39V17.39a.4.4,0,0,0-.39-.39H8.39a.4.4,0,0,0-.39.39v2.22A.4.4,0,0,0,8.39,20Z"></path>' },
    close: { viewbox: '0 0 32 32', code: '<path d="M7.5,10,1.68,4.18l2.5-2.5L10,7.5l5.82-5.82,2.5,2.5L12.5,10l5.82,5.82-2.5,2.5L10,12.5,4.18,18.32l-2.5-2.5Z"></path>' },
    offline: { viewbox: '-3 -3 16 16', code: '<path d="M10,5A5,5,0,1,1,5,0,5,5,0,0,1,10,5Z"></path>' },
    edit: { viewbox: '0 0 20 20', code: '<path d="M19.67,4.82,16.38,8.19,11.1,3.75,14.4.38A1,1,0,0,1,15.66.15l4.13,3.48A.81.81,0,0,1,19.67,4.82ZM.86,14.25,0,19.3A.54.54,0,0,0,.8,20l5.34-1.26,8.64-8.86L9.51,5.39Z"></path>' },
    del: { viewbox: '0 0 20 20', code: '<path d="M8.17,18h-2V8h2Zm6,0h-2V8h2ZM3.17,6V19c0,.89,1,1,1,1h12c1,0,1-.37,1-1V6Zm0-1h14a1,1,0,0,0,1-1,1,1,0,0,0-1-1h-5V1c.05-.22-.59-1-.89-1H9.06c-.28,0-.89.72-.89,1V3h-5a.9.9,0,0,0-1,1A1,1,0,0,0,3.17,5Z"></path>' },
    check: { viewbox: '0 0 20 20', code: '<path d=\"M6.67,17.26.81,11.38a1.14,1.14,0,0,1,0-1.62h0a1.15,1.15,0,0,1,1.63,0L6.67,14,17.56,3.08a1.15,1.15,0,0,1,1.63,0h0a1.14,1.14,0,0,1,0,1.62Z\"></path>' },
    mail: { viewbox: '0 0 20 20', code: '<path d=\"M20,5.34a.43.43,0,0,0,0,0V16.86a.68.68,0,0,1-.69.64H.69A.71.71,0,0,1,0,16.75V5.39s0,0,0-.07L9.6,14.05a.72.72,0,0,0,.4.24c.2,0,.3,0,.4-.12ZM20,2.5H0l9.52,9a.75.75,0,0,0,1,0Z\"></path>' },
    speaker: { viewbox: '0 0 20 20', code: '<path d="M1.8,12.25H.9a.9.9,0,0,1-.9-.9V8.64a.9.9,0,0,1,.9-.9H5.21A24.54,24.54,0,0,0,10,3.59a.45.45,0,0,1,.64,0,.52.52,0,0,1,.14.32V16.09a.46.46,0,0,1-.45.46.46.46,0,0,1-.32-.13l-.35-.36a25.58,25.58,0,0,0-4.5-3.81H4.51M15,12.91a12.19,12.19,0,0,0,0-5.82,1,1,0,1,0-1.94.49,10.16,10.16,0,0,1,0,4.84,1,1,0,0,0,.72,1.22l.25,0A1,1,0,0,0,15,12.91Zm3.83,1.33.33-1.33a11.94,11.94,0,0,0,0-5.82l-.33-1.33A1,1,0,0,0,17.65,5a1,1,0,0,0-.73,1.21l.33,1.33a10,10,0,0,1,0,4.85l-.33,1.34A1,1,0,0,0,17.65,15l.24,0A1,1,0,0,0,18.86,14.24Z"></path>' },
    hotkey: { viewbox: '0 0 20 20', code: '<path d="m6,2c-2.21,0-4,1.79-4,4v8c0,2.21,1.79,4,4,4h8c2.21,0,4-1.79,4-4V6c0-2.21-1.79-4-4-4H6m0-2h8c3.31,0,6,2.69,6,6v8c0,3.31-2.69,6-6,6H6c-3.31,0-6-2.69-6-6V6C0,2.69,2.69,0,6,0Zm7.5,15h-7c-.55,0-1-.45-1-1s.45-1,1-1h7c.55,0,1,.45,1,1s-.45,1-1,1Z"></path>' },
    handset: { viewbox: '0 0 20 20', code: '<path d="M8.68,1.07a.36.36,0,0,1,.18.38c-.17.85-.33,4.08-.44,4.18-.34.28-2.44.81-2.88,1.31-.31.35,1,5.69,1,5.69a5.67,5.67,0,0,0,2.59,1,.44.44,0,0,1,.35.42c.05,1,.22,4,.18,4.87a.57.57,0,0,1-.44.5c-.64.16-1.84.46-2.33.56a.4.4,0,0,1-.47-.26C5.7,17.58,3.6,6.94,3.62,6.5A28.41,28.41,0,0,1,5.42.2.36.36,0,0,1,5.83,0,14.35,14.35,0,0,1,8.68,1.07ZM11.19,8.7,11,8.59a1,1,0,0,1-.28-1.39,10,10,0,0,0,1.64-4.57,1,1,0,0,1,2,.2,12.12,12.12,0,0,1-2,5.48A1,1,0,0,1,11.19,8.7Zm3.16,2.54a.65.65,0,0,1-.22-.11,1,1,0,0,1-.28-1.38l.76-1.15A10,10,0,0,0,16.25,4l.13-1.37a1,1,0,0,1,1.1-.9,1,1,0,0,1,.89,1.1l-.13,1.36a12.07,12.07,0,0,1-2,5.48l-.76,1.14A1,1,0,0,1,14.35,11.24Z"></path>' },
    headphone: { viewbox: '0 0 20 20', code: '<path d="M8.25,15.25c0,1.48-1.57,1.68-3,1.71l-1-9.26.41-.26c1-.42,2.59.33,2.59,1.43ZM9.51,0A9.52,9.52,0,0,0,.21,9.49l.32,1.06c0,.21,0,.42,0,.64L2,13.52a5.55,5.55,0,0,0,1.19,2.21,5.11,5.11,0,0,0,1.35,1l-1-8.66A2.13,2.13,0,0,0,2.79,8,2.45,2.45,0,0,0,2.4,8,7.46,7.46,0,0,1,9.55,1.94,7.47,7.47,0,0,1,16.71,8a2.58,2.58,0,0,0-.39,0c-.11,0-.2,0-.31,0L15,16.4l-2.91,2.32a1,1,0,0,0-.79-.42H9a1,1,0,0,0-1,1H8a1.17,1.17,0,0,0,1.17,1.17h2a1.17,1.17,0,0,0,.76-.3l0,0,5-4L18,12.65l.55-1.46c0-.34,0-.67,0-1h0l.3-.73A9.59,9.59,0,0,0,9.51,0Zm5,7.42A1.79,1.79,0,0,0,12.16,9l-1,6.37c0,1.57,1.44,1.52,3,1.52l.14,0,1-9-.07,0h0Z"></path>' },
    ring: { viewbox: '0 0 20 20', code: '<path d="M17,15.25v.5A1.22,1.22,0,0,1,15.83,17H11.34v1.61A1.35,1.35,0,0,1,10,20H9.81A1.35,1.35,0,0,1,8.5,18.61V17H4a1.22,1.22,0,0,1-1.19-1.25v-.5A1.22,1.22,0,0,1,4,14h.9a.77.77,0,0,0,.75-.79V6.87A1.82,1.82,0,0,1,7.44,5H8.5V3.5A1.46,1.46,0,0,1,9.92,2h0a1.46,1.46,0,0,1,1.42,1.5V5H12.4a1.82,1.82,0,0,1,1.77,1.88v6.33a.77.77,0,0,0,.75.79h.91A1.22,1.22,0,0,1,17,15.25ZM20,6c0-4.75-3.19-6-4.88-6a1,1,0,0,0-.95,1,1,1,0,0,0,.95,1c.12,0,3,0,3,4A.95.95,0,1,0,20,6ZM1.89,6c0-3.83,2.69-4,3-4a1,1,0,0,0,.94-1,1,1,0,0,0-1-1C3.19,0,0,1.25,0,6A1,1,0,0,0,.94,7,1,1,0,0,0,1.89,6Z"></path>' },
    backlight: {
        viewbox: '0 0 32 32',
        code: '<path d="m 15.5,23.5 c 3.875,0 7,-3.15 7,-7 0,-3.85 -3.15,-7.0000003 -7,-7.0000003 -3.85,0 -7,3.1500003 -7,7.0000003 0,3.85 3.125,7 7,7 z"/><path d="m 16.525,6.2499997 v -3.225 c 0,-0.575 -0.45,-1.025 -1.025,-1.025 -0.575,0 -1.025,0.45 -1.025,1.025 v 3.225 c 0,0.575 0.45,1.025 1.025,1.025 0.575,0 1.025,-0.45 1.025,-1.025 z"/><path d="m 14.475,26.75 v 3.225 c 0,0.575 0.45,1.025 1.025,1.025 0.575,0 1.025,-0.45 1.025,-1.025 V 26.75 c 0,-0.575 -0.45,-1.025 -1.025,-1.025 -0.575,0 -1.025,0.45 -1.025,1.025 z"/><path d="m 23.475,9.9749997 2.275,-2.275 c 0.4,-0.4 0.4,-1.05 0,-1.45 -0.4,-0.4 -1.05,-0.4 -1.45,0 l -2.275,2.275 c -0.4,0.4 -0.4,1.05 0,1.45 0.2,0.2000003 0.45,0.3000003 0.725,0.3000003 0.275,0 0.525,-0.1 0.725,-0.3000003 z"/><path d="m 5.25,26.75 c 0.2,0.2 0.45,0.3 0.725,0.3 0.25,0 0.525,-0.1 0.725,-0.3 l 2.275,-2.275 c 0.4,-0.4 0.4,-1.05 0,-1.45 -0.4,-0.4 -1.05,-0.4 -1.45,0 L 5.25,25.3 c -0.4,0.4 -0.4,1.05 0,1.45 z"/><path d="m 30,16.5 c 0,-0.575 -0.45,-1.025 -1.025,-1.025 H 25.75 c -0.575,0 -1.025,0.45 -1.025,1.025 0,0.575 0.45,1.025 1.025,1.025 h 3.225 c 0.55,0 1.025,-0.45 1.025,-1.025 z"/><path d="M 2.025,17.525 H 5.25 c 0.575,0 1.025,-0.45 1.025,-1.025 0,-0.575 -0.45,-1.025 -1.025,-1.025 H 2.025 C 1.45,15.475 1,15.925 1,16.5 c 0,0.575 0.475,1.025 1.025,1.025 z"/><path d="m 25.025,27.05 c 0.25,0 0.525,-0.1 0.725,-0.3 0.4,-0.4 0.4,-1.05 0,-1.45 l -2.275,-2.275 c -0.4,-0.4 -1.05,-0.4 -1.45,0 -0.4,0.4 -0.4,1.05 0,1.45 L 24.3,26.75 c 0.2,0.2 0.475,0.3 0.725,0.3 z"/><path d="m 8.975,9.9749997 c 0.4,-0.4 0.4,-1.05 0,-1.45 l -2.275,-2.275 c -0.4,-0.4 -1.05,-0.4 -1.45,0 -0.4,0.4 -0.4,1.05 0,1.45 l 2.275,2.275 C 7.725,10.175 7.975,10.275 8.25,10.275 c 0.275,0 0.525,-0.1 0.725,-0.3000003 z"/>'
    }
};


function BtButton(owner, lang, title, send, property) {
    var that = this,
        checkbox = new innovaphone.ui1.SvgInline("flex: 0 0 auto; width: 16px; height: 16px; margin: 7px; background-color: var(--uncheck-bg); fill: var(--uncheck-bg); cursor: pointer;", icons.checkmark.viewbox, icons.checkmark.code),
        title = new innovaphone.ui1.Node("div", "flex: 1 1 0; color: var(--c1); font-size: 14px; margin: 5px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;").addTranslation(lang, title),
        active = false,
        force = false;

    this.createNode("div", "flex: 1 1 150px; margin: 5px 5px 5px 10px; display: flex; flex-direction: row; flex-wrap: nowrap; overflow: hidden; order: 1;");
    this.add(title);
    this.addEvent("click", function (e) {
        if (force == false) {
            active = !active;
            checkbox.container.style.backgroundColor = active ? "var(--check-bg)" : "var(--uncheck-bg)";
            checkbox.container.style.fill = active ? "var(--check-c)" : "var(--uncheck-bg)";
            send({ mt: "io", cmd: property, dst: "bt" })
        }
    });
};
BtButton.prototype = innovaphone.ui1.nodePrototype;

/* BtRemoteDevice object */
BtRemoteDevice = function (devAddress, devName, devState) {
    this._address = (devAddress == null) ? null : devAddress;
    this._name = (devName == null) ? null : devName;
    if ((devState == null) || ((devState != BtRemoteDevice.DISCONNECTED) && (devState != BtRemoteDevice.SCANNED) && (devState != BtRemoteDevice.CONNECTED))) {
        this._state = BtRemoteDevice.NOT_SET;
    }
    else {
        this._state = devState;
    }
    this._isPaired = false;
    this._owner = null;
}

BtRemoteDevice.prototype.getAddress = function () {
    return this._address;
}

BtRemoteDevice.prototype.getName = function () {
    return this._name;
}

BtRemoteDevice.prototype.setName = function (devName) {
    this._name = (devName == null) ? null : devName;
}

BtRemoteDevice.prototype.getState = function () {
    return this._state;
}

BtRemoteDevice.prototype.setState = function (devState) {
    if ((devState == null) || ((devState != BtRemoteDevice.DISCONNECTED) && (devState != BtRemoteDevice.SCANNED) && (devState != BtRemoteDevice.CONNECTED))) {
        this._state = BtRemoteDevice.NOT_SET;
    }
    else {
        this._state = devState;
    }
}

BtRemoteDevice.prototype.setIsPaired = function (paired) {
    if ((paired == null) || (typeof (paired) != 'boolean')) {
        return;
    }
    this._isPaired = paired;
}

BtRemoteDevice.prototype.getIsPaired = function () {
    return this._isPaired;
}

BtRemoteDevice.prototype.getOwner = function () {
    return this._owner;
}

BtRemoteDevice.prototype.setOwner = function (owner) {
    this._owner = (owner == null) ? null : owner;
}

BtRemoteDevice.prototype.render = function (parentNode) {
    if (parentNode == null) {
        return;
    }

    var btnConn, btnDisc, btnUnpair, btnDetails;

    var lblDeviceName = new innovaphone.ui1.Node("p");
    lblDeviceName.addText(this.getName() + " [" + this.getAddress() + "]");
    parentNode.add(lblDeviceName);

    //var lblDeviceName = document.createElement("p");
    //lblDeviceName.innerHTML = this.getName() + " [" + this.getAddress() + "]";
    //parentNode.appendChild(lblDeviceName);

    // option 1
    if (this.getState() === BtRemoteDevice.CONNECTED) {
        btnDisc = new innovaphone.ui1.Node("button", "", "Disconnect");
        btnDisc.addEvent("click", function (e, device) {
            if (device && device.getOwner() && device.getOwner().itemCommand) {
                device.getOwner().itemCommand("disconnect", device);
            }
        }, this);
        parentNode.add(btnDisc);
    }
    if ((this.getState() === BtRemoteDevice.SCANNED) || (this.getState() === BtRemoteDevice.DISCONNECTED)) {
        btnConn = new innovaphone.ui1.Node("button", "", "Connect");
        btnConn.addEvent("click", function (e, device) {
            if (device && device.getOwner() && device.getOwner().itemCommand) {
                device.getOwner().itemCommand("connect", device);
            }
        }, this);
        parentNode.add(btnConn);
        /*
        btnConn = document.createElement("button");
        btnConn.innerHTML = "Connect";
        registerEvent(btnConn, "click", this, function (e, device) {
            if (device && device.getOwner() && device.getOwner().itemCommand) {
                device.getOwner().itemCommand("connect", device);
            }
        });
        parentNode.appendChild(btnConn);
        */
    }
    else {
    }

    if (this.getIsPaired()) {
        btnUnpair = new innovaphone.ui1.Node("button", "", "Unpair");
        btnUnpair.addEvent("click", function(e, device) {
            if (device && device.getOwner() && device.getOwner().itemCommand) {
                device.getOwner().itemCommand("unpair", device);
            }
        }, this);
        parentNode.add(btnUnpair);
        /*
        btnUnpair = document.createElement("button");
        btnUnpair.innerHTML = "Unpair";
        registerEvent(btnUnpair, "click", this, function (e, device) {
            if (device && device.getOwner() && device.getOwner().itemCommand) {
                device.getOwner().itemCommand("unpair", device);
            }
        });
        parentNode.appendChild(btnUnpair);
        */
    }
}

BtRemoteDevice.prototype.toString = function () {
    return "{_address: " + this._address + ", _name: " + this._name + ", _state: " + this._state + "}";
}

BtRemoteDevice.NOT_SET = 0;
BtRemoteDevice.DISCONNECTED = 1;
BtRemoteDevice.SCANNED = 2;
BtRemoteDevice.CONNECTED = 3;


/* BtDevice object */
BtDevice = function () {
    this.RemoteDevices = new Object();
    this._btEnabled = null;
    this._localDeviceName = null;
    this._HFPDevice = null;
    this._A2DPDevice = null;
    this._pairedList = new Array();
    this._scannedList = new Array();
}

BtDevice.prototype.addDevice = function (device) {
    if ((device == null) || (device.constructor !== BtRemoteDevice) || (device.getAddress() == null)) {
        return;
    }
    device.setOwner(this);
    var devAddr = device.getAddress();
    if (this.RemoteDevices[devAddr] == null) {
        this.RemoteDevices[devAddr] = device;
    }
    else {
        var dev = this.RemoteDevices[devAddr];
        if (device.getName() != null) {
            dev.setName(device.getName());
        }
        if (device.getState() != BtRemoteDevice.NOT_SET) {
            dev.setState(device.getState());
        }
    }
}


BtDevice.prototype.getDeviceByAddress = function (devAddress) {
    if (devAddress == null) {
        return null;
    }
    return this.RemoteDevices[devAddress];
}

BtDevice.prototype.getAllDevices = function () {
    var arr_result = new Array();
    for (var key in this.RemoteDevices) {
        arr_result.push(this.RemoteDevices[key]);
    }
    return arr_result;
}

BtDevice.prototype.getScannedDevices = function () {
    return this._scannedList;
}

BtDevice.prototype.getConnectedDevices = function () {
    var arr_result = new Array();
    for (var key in this.RemoteDevices) {
        if (this.RemoteDevices[key].getState() == BtRemoteDevice.CONNECTED) {
            arr_result.push(this.RemoteDevices[key]);
        }
    }
    return arr_result;
}

BtDevice.prototype.getPairedDevices = function () {
    return this._pairedList;
}


BtDevice.prototype.deleteDevice = function (device) {
    if ((device == null) || (device.constructor !== BtRemoteDevice) || (device.Address == null)) {
        return;
    }
    delete this.RemoteDevices[device.Address];
}

BtDevice.prototype.deleteDeviceByAddress = function (devAddress) {
    if (devAddress == null) {
        return null;
    }
    delete this.RemoteDevices[devAddress];
}

BtDevice.prototype.deleteAllDevices = function () {
    for (var key in this.RemoteDevices) {
        delete this.RemoteDevices[key];
    }
}

BtDevice.prototype.deleteDevicesByState = function (state) {
    if ((state == null) || ((devState != BtRemoteDevice.NOT_SET) && (devState != BtRemoteDevice.DISCONNECTED) && (devState != BtRemoteDevice.SCANNED) && (devState != BtRemoteDevice.CONNECTED))) {
        return null;
    }
    for (var key in this.RemoteDevices) {
        if (this.RemoteDevices[key].State == state) {
            delete this.RemoteDevices[key];
        }
    }
}

BtDevice.prototype.getBtEnabled = function () {
    return this._btEnabled;
}

BtDevice.prototype.setBtEnabled = function (btEnabled) {
    if ((btEnabled === null) || (typeof (btEnabled) == 'boolean')) {
        this._btEnabled = btEnabled;
    }
}

BtDevice.prototype.getDeviceName = function () {
    return this._localDeviceName;
}

BtDevice.prototype.setDeviceName = function (devName) {
    if ((devName === null) || (typeof (devName) == 'string')) {
        this._localDeviceName = devName;
    }
}

BtDevice.prototype.clearPairingStatusInAllDevices = function () {
    for (var key in this.RemoteDevices) {
        this.RemoteDevices[key].setIsPaired(false);
    }
}


BtDevice.prototype.getHFPDevice = function() {
    return this._HFPDevice;
}

BtDevice.prototype.setHFPDevice = function(device) {
    if (device === null) {
        if (this._HFPDevice != null) {
            this._HFPDevice.setState(BtRemoteDevice.DISCONNECTED);
            this._HFPDevice = null;
        }
    }
    else if ((device === undefined) || (device.constructor != BtRemoteDevice) || (device.getAddress() == null)) {
        return;
    }
    else {
        var devices = this.getAllDevices();
        var i;
        if (this._HFPDevice != null) {
            this._HFPDevice.setState(BtRemoteDevice.DISCONNECTED);
        }
        for (i = 0; i < devices.length; ++i) {
            if (device.getAddress() == devices[i].getAddress()) {
                this._HFPDevice = devices[i];
                this._HFPDevice.setState(BtRemoteDevice.CONNECTED);
                break;
            }
        }
        if (i == devices.length) {
            this.addDevice(device);
            this._HFPDevice = device;
            this._HFPDevice.setState(BtRemoteDevice.CONNECTED);
        }
    }
}


BtDevice.prototype.getA2DPDevice = function() {
    return this._A2DPDevice;
}

BtDevice.prototype.setA2DPDevice = function(device) {
    if (device === null) {
        if (this._A2DPDevice != null) {
            this._A2DPDevice.setState(BtRemoteDevice.DISCONNECTED);
            this._A2DPDevice = null;
        }
    }
    else if ((device === undefined) || (device.constructor != BtRemoteDevice) || (device.getAddress() == null)) {
        return;
    }
    else {
        var devices = this.getAllDevices();
        var i;
        if (this._A2DPDevice != null) {
            this._A2DPDevice.setState(BtRemoteDevice.DISCONNECTED);
        }
        for (i = 0; i < devices.length; ++i) {
            if (device.getAddress() == devices[i].getAddress()) {
                this._A2DPDevice = devices[i];
                this._A2DPDevice.setState(BtRemoteDevice.CONNECTED);
                break;
            }
        }
        if (i == devices.length) {
            this.addDevice(device);
            this._A2DPDevice = device;
            this._A2DPDevice.setState(BtRemoteDevice.CONNECTED);
        }
    }
}


BtDevice.prototype.processGetDevInfo = function(jsonObj) {
    var i, j;
    var data = jsonObj.data;
    var data_elem;
    var reply, result;
    var isConnected, devAddr, devName;
    for (i = 0; i < data.length; ++i) {
        data_elem = data[i];
        if ((data_elem.cmd.indexOf("AT+BTEN") >= 0) && (data_elem.result == "OK") && data_elem.replies && (data_elem.replies.constructor == Array)) {
            for (j = 0; j < data_elem.replies.length; ++j) {
                reply = data_elem.replies[j];
                result = reply.match(/^\+BTEN=([0-9]+)/);
                if (result) {
                    this._btEnabled = null;
                    if (result[1] == "1") {
                        this._btEnabled = true;
                    }
                    else if (result[1] == "0") {
                        this._btEnabled = false;
                    }
                    break;
                }
            }
        }
        if ((data_elem.cmd.indexOf("AT+NAME") >= 0) && (data_elem.result == "OK") && data_elem.replies && (data_elem.replies.constructor == Array)) {
            for (j = 0; j < data_elem.replies.length; ++j) {
                reply = data_elem.replies[j];
                result = reply.match(/^\+NAME=(.*)/);
                if (result) {
                    this._localDeviceName = result[1];
                    break;
                }
            }
        }
        if ((data_elem.cmd.indexOf("AT+HFPSTAT") >= 0) && (data_elem.result == "OK") && data_elem.replies && (data_elem.replies.constructor == Array)) {
            isConnected = false;
            devAddr = null;
            devName = null;
            for (j = 0; j < data_elem.replies.length; ++j) {
                reply = data_elem.replies[j];
                result = reply.match(/^\+HFPSTAT=([0-9]+)/);
                if (result) {
                    if (result[1] >= 3) {
                        isConnected = true;
                    }
                    break;
                }
            }
            for (j = 0; j < data_elem.replies.length; ++j) {
                reply = data_elem.replies[j];
                result = reply.match(/^\+HFPDEV=(.*),(.*)/);                // =<dev_addr>,<dev_name>
                if (result) {
                    devAddr = result[1];
                    devName = result[2];
                    break;
                }
            }
            if (isConnected) {
                this.setHFPDevice(new BtRemoteDevice(devAddr, devName, BtRemoteDevice.CONNECTED));
            }
            else {
                this.setHFPDevice(null);
            }
        }
        if ((data_elem.cmd.indexOf("AT+A2DPSTAT") >= 0) && (data_elem.result == "OK") && data_elem.replies && (data_elem.replies.constructor == Array)) {
            isConnected = false;
            devAddr = null;
            devName = null;
            for (j = 0; j < data_elem.replies.length; ++j) {
                reply = data_elem.replies[j];
                result = reply.match(/^\+A2DPSTAT=([0-9]+)/);
                if (result) {
                    if (result[1] >= 3) {
                        isConnected = true;
                    }
                    break;
                }
            }
            for (j = 0; j < data_elem.replies.length; ++j) {
                reply = data_elem.replies[j];
                result = reply.match(/^\+A2DPDEV=(.*),(.*)/);               // =<dev_addr>,<dev_name>
                if (result) {
                    devAddr = result[1];
                    devName = result[2];
                    break;
                }
            }
            if (isConnected) {
                this.setA2DPDevice(new BtRemoteDevice(devAddr, devName, BtRemoteDevice.CONNECTED));
            }
            else {
                this.setA2DPDevice(null);
            }
        }
    }
}

BtDevice.prototype.processGetConnectedDevs = function(jsonObj) {
    var i, j;
    var data = jsonObj.data;
    var data_elem;
    var reply, result;
    var isConnected, devAddr, devName;

    for (i = 0; i < data.length; ++i) {
        data_elem = data[i];
        if ((data_elem.cmd.indexOf("AT+HFPSTAT") >= 0) && (data_elem.result == "OK") && data_elem.replies && (data_elem.replies.constructor == Array)) {
            isConnected = false;
            devAddr = null;
            devName = null;
            for (j = 0; j < data_elem.replies.length; ++j) {
                reply = data_elem.replies[j];
                result = reply.match(/^\+HFPSTAT=([0-9]+)/);
                if (result && (result[1] >= 3)) {
                    isConnected = true;
                    break;
                }
            }
            for (j = 0; j < data_elem.replies.length; ++j) {
                reply = data_elem.replies[j];
                result = reply.match(/^\+HFPDEV=(.*),(.*)/);                // =<dev_addr>,<dev_name>
                if (result) {
                    devAddr = result[1];
                    devName = result[2];
                    break;
                }
            }
            if (isConnected) {
                this.setHFPDevice(new BtRemoteDevice(devAddr, devName, BtRemoteDevice.CONNECTED));
            }
            else {
                this.setHFPDevice(null);
            }
        }
        if ((data_elem.cmd.indexOf("AT+A2DPSTAT") >= 0) && (data_elem.result == "OK") && data_elem.replies && (data_elem.replies.constructor == Array)) {
            isConnected = false;
            devAddr = null;
            devName = null;
            for (j = 0; j < data_elem.replies.length; ++j) {
                reply = data_elem.replies[j];
                result = reply.match(/^\+A2DPSTAT=([0-9]+)/);
                if (result && (result[1] >= 3)) {
                    isConnected = true;
                    break;
                }
            }
            for (j = 0; j < data_elem.replies.length; ++j) {
                reply = data_elem.replies[j];
                result = reply.match(/^\+A2DPDEV=(.*),(.*)/);               // =<dev_addr>,<dev_name>
                if (result) {
                    devAddr = result[1];
                    devName = result[2];
                    break;
                }
            }
            if (isConnected) {
                this.setA2DPDevice(new BtRemoteDevice(devAddr, devName, BtRemoteDevice.CONNECTED));
            }
            else {
                this.setA2DPDevice(null);
            }
        }
    }
}

BtDevice.prototype.processGetPairedDevs = function(jsonObj) {
    var i, j, k;
    var data = jsonObj.data;
    var data_elem;
    var reply, result;
    var newDevice, devices;
    for (i = 0; i < data.length; ++i) {
        data_elem = data[i];
        if ((data_elem.cmd.indexOf("AT+PLIST") >= 0) && (data_elem.result == "OK") && data_elem.replies && (data_elem.replies.constructor == Array)) {
            devices = this.getAllDevices();
            for (j = 0; j < this.devices; ++j) {
                this.devices[j].setIsPaired(false);
            }
            this._pairedList.length = 0;
            for (j = 0; j < data_elem.replies.length; ++j) {
                reply = data_elem.replies[j];
                result = reply.match(/^\+PLIST=(.*),(.*),(.*),(.*)/);       // =<index>,<profile>,<dev_addr>,<dev_name>
                if (result) {
                    for (k = 0; k < devices.length; ++k) {
                        if (devices[k].getAddress() == result[3]) {
                            devices[k].setIsPaired(true);
                            this._pairedList.push(devices[k]);
                            break;
                        }
                    }
                    if (k == devices.length) {         // device not found in list, add it
                        newDevice = new BtRemoteDevice(result[3], result[4], BtRemoteDevice.DISCONNECTED);
                        newDevice.setIsPaired(true);
                        this.addDevice(newDevice);
                        this._pairedList.push(newDevice);
                    }
                }
            }
        }
    }
}

BtDevice.prototype.processScanDevs = function(jsonObj) {
    var i, j, k;
    var data = jsonObj.data;
    var data_elem;
    var reply, result;
    var newDevice, devices;
    for (i = 0; i < data.length; ++i) {
        data_elem = data[i];
        if ((data_elem.cmd.indexOf("AT+SCAN") >= 0) && (data_elem.result == "OK") && data_elem.replies && (data_elem.replies.constructor == Array)) {
            for (j = 0; j < this._scannedList; ++j) {
                this._scannedList[j].setState(BtRemoteDevice.DISCONNECTED);
            }
            this._scannedList.length = 0;
            devices = this.getAllDevices();
            for (j = 0; j < data_elem.replies.length; ++j) {
                reply = data_elem.replies[j];
                result = reply.match(/^\+SCAN=(.*),(.*),(.*),(.*),(.*),(.*)/);      // =<index>,<RSSI>,<addr_type>,<dev_addr>,<dev_name>,<dev_class>
                if (result) {
                    for (k = 0; k < devices.length; ++k) {
                        if (devices[k].getAddress() == result[4]) {
                            devices[k].setState(BtRemoteDevice.SCANNED);
                            this._scannedList.push(devices[k]);
                            break;
                        }
                    }
                    if (k == devices.length) {
                        newDevice = new BtRemoteDevice(result[4], result[5], BtRemoteDevice.SCANNED);
                        this.addDevice(newDevice);
                        this._scannedList.push(newDevice);
                    }
                }
            }
        }
    }
}

BtDevice.prototype.processDisconnectDev = function(jsonObj) {
    var i, j;
    var data = jsonObj.data;
    var data_elem;
    var reply, resultHFP, resultA2DP;
    for (i = 0; i < data.length; ++i) {
        data_elem = data[i];
        if ((data_elem.cmd.indexOf("AT+A2DPDISC") >= 0) && (data_elem.result == "OK") && data_elem.replies && (data_elem.replies.constructor == Array)) {
            for (j = 0; j < data_elem.replies.length; ++j) {
                reply = data_elem.replies[j];
                resultA2DP = reply.match(/^\+A2DPSTAT=[1]+$/);
                if (resultA2DP) {
                    this.setA2DPDevice(null);
                    break;
                }
            }
        }
        if ((data_elem.cmd.indexOf("AT+HFPDISC") >= 0) && (data_elem.result == "OK") && data_elem.replies && (data_elem.replies.constructor == Array)) {
            for (j = 0; j < data_elem.replies.length; ++j) {
                reply = data_elem.replies[j];
                resultHFP = reply.match(/^\+HfPSTAT=[1]+$/);
                if (resultHFP) {
                    break;
                }
            }
        }
    }
}

BtDevice.prototype.processUnpairDev = function(jsonObj) {
    var i, j;
    var data = jsonObj.data;
    var data_elem;
    var args = jsonObj.args;
    var sepIdx, devAddr;
    var reply, result;
    for (i = 0; i < data.length; ++i) {
        data_elem = data[i];
        if ((data_elem.cmd.indexOf("AT+PLIST") >= 0) && (data_elem.result == "OK") && data_elem.replies && (data_elem.replies.constructor == Array)) {
            for (j = 0; j < data_elem.replies.length; ++j) {
                reply = data_elem.replies[j];
                result = reply.match(/^OK$/);
                if (result) {
                    sepIdx = args.indexOf(",");
                    if (sepIdx == -1) {
                        devAddr = args;
                    }
                    else {
                        devAddr = args.substring(0, sepIdx);
                    }
                    this.RemoteDevices[devAddr].setIsPaired(false);
                    break;
                }
            }
            // TODO: check for error
        }
    }
    var str = "";
    str.substring(0, )
}


BtDevice.prototype.processOperation = function(jsonObj) {
    var op = jsonObj.op;

    if (op == "disconnect_dev") {
        this.processDisconnectDev(jsonObj);
    }
    else if (op == "get_dev_info") {
        this.processGetDevInfo(jsonObj);
    }
    else if (op == "get_connected_devs") {
        this.processGetConnectedDevs(jsonObj);
    }
    else if (op == "get_paired_devs") {
        this.processGetPairedDevs(jsonObj);
    }
    else if (op == "scan_devs") {
        this.processScanDevs(jsonObj);
    }
    else if (op == "unpair_dev") {
        this.processUnpairDev(jsonObj);
    }
    else if (op == "bt_event") {
        // do nothing
    }
    else {
        console.log("processOperation: Unknown operation.");
    }
}

BtDevice.prototype.toString = function () {
    var result = "";
    result = "<div class='bt_settings'>";
    for (var key in this.RemoteDevices) {
        result = result + this.RemoteDevices[key].toString();
    }
    result = result + "<div>";
    return result;
}

BtDevice.prototype.itemCommand = null;


/* PageIP270BTSettings object */
function PageIP270BTSettings(owner, lang, send) {
    this.createNode("div", "position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px; padding: 5px; overflow: auto;");

    var that = this,
        btScan = new BtButton(that, lang, "Scan", send, "scan"),
        btPair = new BtButton(that, lang, "Pair", send, "pair"),
        btUnpair = new BtButton(that, lang, "Unpair", send, "unpair"),
        resp = new innovaphone.ui1.Node("div", "font-size: 16px; color: var(--c1); margin: 10px; padding: 5px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;"),
        title = this.add(new innovaphone.ui1.Node("div", "font-size: 16px; color: var(--c1); margin: 10px; padding: 5px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;")).addTranslation(lang, "Manage Devices");
    this.add(resp);

    this.btHeader = new innovaphone.ui1.Node("div", "font-size: 16px; color: var(--c1); margin: 10px; padding: 5px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;", "", "jar_header");
    this.btArea = new innovaphone.ui1.Node("div", "font-size: 16px; color: var(--c1); margin: 10px; padding: 5px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;", "", "jar_body");
    this.add(this.btHeader);
    this.add(this.btArea);

    this.myBtDevice = new BtDevice();

    this.myBtDevice.itemCommand = function (command, device) {
        if (device) {
            var jsonObj;
            if (command == "connect") {
                jsonObj = { mt: "io", op: "connect_dev", args: device.getAddress(), dst: "bt" };
            }
            else if (command == "disconnect") {
                jsonObj = { mt: "io", op: "disconnect_dev", args: device.getAddress(), dst: "bt" };
            }
            else if (command == "unpair") {
                jsonObj = { mt: "io", op: "unpair_dev", args: device.getAddress(), dst: "bt" };
            }
            send(jsonObj);
        }
    }

    var pairBtn0 = new innovaphone.ui1.Node("input", "");
    pairBtn0.setAttribute("type", "button").setAttribute("value", "Scan");
    pairBtn0.addEvent("click", function (e, obj) {
        send({ mt: "io", op: "scan_devs", args: "1", dst: "bt" });
    });

    var pairBtn9 = new innovaphone.ui1.Node("input", "");
    pairBtn9.setAttribute("type", "button").setAttribute("value", "Get BT Dev Info");
    pairBtn9.addEvent("click", function (e, obj) {
        send({ mt: "io", op: "get_dev_info", args: "abcde", dst: "bt" });
    });

    var pairBtn10 = new innovaphone.ui1.Node("input", "");
    pairBtn10.setAttribute("type", "button").setAttribute("value", "Get Paired");
    pairBtn10.addEvent("click", function (e, obj) {
        send({ mt: "io", op: "get_paired_devs", args: "", dst: "bt" });
    });

    this.add(pairBtn9);
    this.add(pairBtn0);
    this.add(pairBtn10);

    owner.onmessage.attach(function (sender, obj) {
        if (obj == null) {
            console.log("JSON is null.");
            return;
        }
        if ((typeof(obj.mt) == "undefined") || 
            (typeof(obj.op) == "undefined") || 
            (typeof(obj.args) == "undefined") || 
            (typeof(obj.data) == "undefined") ||
            (obj.data.constructor != Array)) {
            console.log("Invalid JSON format: " + JSON.stringify(obj));
            return;
        }
        console.log(obj);
        if (obj.mt !== "resp") {
            console.log("Invalid JSON content.");
            return;
        }

        that.myBtDevice.processOperation(obj);

        if (obj.op == "connect_dev") {
            window.setTimeout(function() {
                send({ mt: "io", op: "get_dev_info", args: null, dst: "bt" });
            }, 3000);
        }
        else if (obj.op == "disconnect_dev") {
            window.setTimeout(function() {
                send({ mt: "io", op: "get_dev_info", args: null, dst: "bt" });
            }, 2000);
        }
        else if (obj.op == "get_connected_devs") {
            that.renderBtHeader(that.btHeader);
            that.renderBtBody(that.btArea);
        }
        else if (obj.op == "get_dev_info") {
            that.renderBtHeader(that.btHeader);
        }
        else if (obj.op == "get_paired_devs") {
            that.renderBtBody(that.btArea);
        }
        else if (obj.op == "scan_devs") {
            that.renderBtBody(that.btArea);
        }
        else if (obj.op == "unpair_dev") {
            send({ mt: "io", op: "get_paired_devs", args: "", dst: "bt" });
            window.setTimeout(function() {
                send({ mt: "io", op: "get_dev_info", args: null, dst: "bt" });
            }, 2000);
        }
        else if (obj.op == "bt_event") {
            window.setTimeout(function() {
                send({ mt: "io", op: "get_dev_info", args: null, dst: "bt" });
            }, 2000);
        }
    });
}

PageIP270BTSettings.prototype = innovaphone.ui1.nodePrototype;

PageIP270BTSettings.prototype.renderBtHeader = function (parentNode) {
    parentNode.addText("");

    var lblBtEnabled = new innovaphone.ui1.Div("", "Bluetooth Enabled:");
    var lblBtEnabledValue = new innovaphone.ui1.Div("margin-bottom: 10px");
    if (this.myBtDevice.getBtEnabled() != null) {
        lblBtEnabledValue.addText(this.myBtDevice.getBtEnabled() ? "Yes" : "No");
    }
    else {
        lblBtEnabledValue.addText("Unknown");
    }
    parentNode.add(lblBtEnabled);
    parentNode.add(lblBtEnabledValue);

    var lblDevName = new innovaphone.ui1.Div("", "Device Name:");
    var lblDevNameValue = new innovaphone.ui1.Div("margin-bottom: 10px", this.myBtDevice.getDeviceName());
    parentNode.add(lblDevName);
    parentNode.add(lblDevNameValue);

    var lblHFP = new innovaphone.ui1.Div("", "Hands Free:");
    var lblHFPValue = new innovaphone.ui1.Div("margin-bottom: 10px");
    var hfpDevice = this.myBtDevice.getHFPDevice();
    if (hfpDevice != null) {
        lblHFPValue.addText("Connected to '" + hfpDevice.getName() + "' [" + hfpDevice.getAddress() + "]");
    }
    else {
        lblHFPValue.addText("Not connected");
    }
    parentNode.add(lblHFP);
    parentNode.add(lblHFPValue);

    var lblA2DP = new innovaphone.ui1.Div("", "Audio:");
    var lblA2DPValue = new innovaphone.ui1.Div("margin-bottom: 10px");
    var a2dpDevice = this.myBtDevice.getA2DPDevice();
    if (a2dpDevice != null) {
        lblA2DPValue.addText("Connected to '" + a2dpDevice.getName() + "' [" + a2dpDevice.getAddress() + "]");
    }
    else {
        lblA2DPValue.addText("Not connected");
    }
    parentNode.add(lblA2DP);
    parentNode.add(lblA2DPValue);

    if ((a2dpDevice != null) || (hfpDevice != null)) {
        btnDisc = new innovaphone.ui1.Node("button", "", "Disconnect");
        btnDisc.addEvent("click", function (e, device) {
            if (device && device.itemCommand) {
                device.itemCommand("disconnect", ((a2dpDevice != null) ? a2dpDevice : hfpDevice));
            }
        }, this.myBtDevice);
        parentNode.add(btnDisc);
    }
}

PageIP270BTSettings.prototype.renderBtBody = function (parentNode) {
    parentNode.addText("");

    var divItem = null;
    var devs = null;

    var pnlPaired = new innovaphone.ui1.Node("fieldset");
    pnlPaired.add(new innovaphone.ui1.Node("legend", "", "Paired Devices"));
    var devs = this.myBtDevice.getPairedDevices();
    if (devs.length == 0) {
        divItem = new innovaphone.ui1.Div("margin: 10px");
        divItem.addText("No devices.");
        pnlPaired.add(divItem);
    }
    else {
        for (var i = 0; i < devs.length; ++i) {
            divItem = new innovaphone.ui1.Div("margin: 10px");
            devs[i].render(divItem);
            pnlPaired.add(divItem);
        }
    }
    parentNode.add(pnlPaired);

    var pnlScanned = new innovaphone.ui1.Node("fieldset");
    pnlScanned.add(new innovaphone.ui1.Node("legend", "", "Scanned Devices"));
    var devs = this.myBtDevice.getScannedDevices();
    if (devs.length == 0) {
        divItem = new innovaphone.ui1.Div("margin: 10px");
        divItem.addText("No devices.");
        pnlScanned.add(divItem);
    }
    else {
        for (var i = 0; i < devs.length; ++i) {
            divItem = new innovaphone.ui1.Div("margin: 10px");
            devs[i].render(divItem);
            pnlScanned.add(divItem);
        }
    }
    parentNode.add(pnlScanned);
}

