//document.write('<script type="text/javascript" src="/js/common/trans.js"></script>');

var btn_kbimg = null;
var source = null;
var query = null;
var container = null;
function insertAtCursor(myField, myValue) {
    container.CheckAndResetMessage();
    if (document.selection) {
        var temp; myField.focus();
        sel = document.selection.createRange(); temp = sel.text.lenght; sel.text = myValue;
        if (myValue.length == 0) {
            sel.moveStart('character', myValue.length);
            sel.moveEnd('character', myValue.length);
        } else {
            sel.moveStart('character', -myValue.length + temp);
        } sel.select();
    } else if (myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length);
        myField.selectionStart = startPos + myValue.length;
        myField.selectionEnd = startPos + myValue.length;
        myField.focus();
    } else {
        myField.value += myValue;
    }
    container.SetCounter();
}

function backAtCursor(myField) {
    if (document.selection) {
        myField.focus();
        sel = document.selection.createRange();

        if (sel.text.length > 0) {
            sel.text = '';
        } else {
            sel.moveStart('character', -1);
            sel.text = '';
        }

        sel.select();

    } else if (myField.selectionStart || myField.selectionStart == '0') {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos - 1) + myField.value.substring(endPos, myField.value.length);
        myField.selectionStart = startPos - 1;
        myField.selectionEnd = startPos - 1;
        myField.focus();
    } else {
        myField.value = myField.value.substr(0, (myField.value.length - 1));
        myField.focus();
    }
    container.SetCounter();
}


function callback(ch) {
    if (ch == "BakSpc") {
        var min = (source.value.charCodeAt(source.value.length - 1) == 10) ? 2 : 1;
        backAtCursor(source);

    }
    else if (ch == "Enter") {
        insertAtCursor(source, "\n");
    }
    else if (ch == "X") {
        hideKeyboard(btn_kbimg, "keyboard");
    }
    else {
        if ((ch == "بَ") || (ch == "بّ") || (ch == "بً") || (ch == "بٌ") || (ch == "بُ") || (ch == "بِ") || (ch == "بٍ")) {
            insertAtCursor(source, ch.substring(1));
        }
        else {
            insertAtCursor(source, ch);
        }
    }
}

function focus_keyboard(e) {
    source = get_event_source(e);
}

function register_field(id) {
    setup_event(document.getElementById(id), "focus", focus_keyboard);
}

function keystart(txtQueryID, btnID, Container) {
    btn_kbimg = document.getElementById(btnID);
    query = txtQueryID;
    container = Container;
    source = document.getElementById(txtQueryID);
    register_field(source);
}

function showHideKeyboard(o) {
    toggleBox(o, 'keyboard');
    return false;
}

function showKeyboard(oImg, szDivID) {
    var oDiv = document.getElementById(szDivID);
    oDiv.style.visibility = "visible";
    ShowVKeyboard("keyboard", "callback");
    oImg.src = "images/keyclosed.gif";
    oImg.value = "hide";
}

function hideKeyboard(oImg, szDivID) {
    var oDiv = document.getElementById(szDivID);
    oDiv.style.visibility = "hidden";
    ShowVKeyboard("keyboard", "callback");
    oImg.src = "images/keyopen.gif";
    oImg.value = "show";
}

function toggleBox(oImg, szDivID) {
    var obj = document.getElementById(szDivID);

    if (obj.style.visibility == "visible") {
        obj.style.visibility = "hidden";
        hideKeyboard(oImg, szDivID);
    }
    else {
        obj.style.visibility = "visible";
        showKeyboard(oImg, szDivID);
    }

    document.getElementById(query).focus();
}


