        var opened = false, vkb = null, text = null;
   
        function keyb_change() {
            //document.getElementById("switchA").innerHTML = (opened ? "Show" : "Hide");
            opened = !opened;

            if(opened && !vkb) {
                // Note: all parameters, starting with 3rd, in the following
                // expression are equal to the default parameters for the
                // VKeyboard object. The only exception is 15th parameter
                // (flash switch), which is false by default.
                vkb = new VKeyboard("keyboard",    // container's id
                                    keyb_callback, // reference to the callback function
                                    false,         // create the arrow keys or not? (this and the following params are optional)
                                    false,         // create up and down arrow keys? 
                                    false,         // reserved
                                    false,         // create the numpad or not?
                                    "Arial",          // font name ("" == system default)
                                    "12px",           // font size in px
                                    "#848589",        // font color
                                    "#cccccc",        // font color for the dead keys
                                    "#d2d6d9",        // keyboard base background color
                                    "#f0f1f5",        // keys' background color
                                    "#DDD",        // background color of switched/selected item
                                    "#ffffff",        // border color
                                    "#cccccc",        // border/font color of "inactive" key (key with no value/disabled)
                                    "#ffffff",        // background color of "inactive" key (key with no value/disabled)
                                    "#f77",        // border color of the language selector's cell
                                    true,          // show key flash on click? (false by default)
                                    "#cc3300",     // font color during flash
                                    "#ff9966",     // key background color during flash
                                    "#cc3300",     // key border color during flash
                                    false,         // embed VKeyboard into the page?
                                    true,          // use 1-pixel gap between the keys?
                                    0);            // index(0-based) of the initial layout
            }
            else
                vkb.Show(opened);
            text = document.getElementById("search_text");
            text.focus();

            if(document.attachEvent)
                text.attachEvent("onblur", backFocus);
        }

        function backFocus() {
            if(opened) {
                var l = text.value.length;
                setRange(text, l, l);
                text.focus();
            }
        }

        // Callback function:
        function keyb_callback(ch) {
            var val = text.value;
            switch(ch) {
                case "Backspace":
                    var min = (val.charCodeAt(val.length - 1) == 10) ? 2 : 1;
                    text.value = val.substr(0, val.length - min);
                    break;
                case "Enter":
                    //text.value += "\n";
                    keyb_change()
                    break;
                default:
                    text.value += ch;
            }
        }

        function setRange(ctrl, start, end) {
            if(ctrl.setSelectionRange) { // Standard way (Mozilla, Opera, ...)
                ctrl.setSelectionRange(start, end);
            }
            else { // MS IE
                var range;
                try {
                    range = ctrl.createTextRange();
                }
                catch(e) {
                    try {
                        range = document.body.createTextRange();
                        range.moveToElementText(ctrl);
                    }
                    catch(e) {
                        range = null;
                    }
                }
                if(!range) return;
                range.collapse(true);
                range.moveStart("character", start);
                range.moveEnd("character", end - start);
                range.select();
            }
        }
