
var systranBox = {

    _srcBox : null,
    _tgtBox : null,
    _activeRequest : null,
    _srcChangeTimer : null,
    _lastText : null,
    _lastLp : null,
    onLoadResult : null,
    _maxSize : null,

    init : function() {
        var srcBox = document.getElementById("edit_src");
        var tgtBox = document.getElementById("edit_tgt");
        if(srcBox.addEventListener) {
            srcBox.addEventListener("keyup", systranBox.onSrcChange, true);
            srcBox.addEventListener("keyup", systranBox.maxSize, true);
        }
        else if(srcBox.attachEvent) {
            srcBox.attachEvent("onkeyup", systranBox.onSrcChange);
            srcBox.attachEvent("onkeyup", systranBox.maxSize);
        }
        systranBox._srcBox = srcBox;
        systranBox._tgtBox = tgtBox;
    },

    doEmail : function() {
        if (!_hasEmail)
            window.open("translateTextEmailPopup.jsp", "emailText", "scrollbars=yes,resizable=yes,toolbar=no,width=200,height=40");
        else
            this.email(null);
    },

    callEmail : function() {
        var email = document.getElementById("email").value;
        if (email != '') {
            opener.systranBox.email(email);
            window.close();
        }
    },

    email : function(email) {
        var options = {};
        if (email)
            options["email"] = email;
        options["template"] = "template/box/translate.html";
        options["variables"] = "srcWindow,tgtWindow";
        var sent = "srcWindow=" + this._srcBox.value + "tgtWindow=" + this._tgtBox.value;
        this._activeEmail = sai.sendGenericRequest("sai", "email_text", options, sent);
        setTimeout(this.checkEmail, 200);
    },

    checkEmail : function() {
        if(systranBox._activeEmail == null) {
            return;
        }
        if(systranBox._activeEmail.isComplete()) {
            if(systranBox._activeEmail.isSuccess()) {
                alert(localization.getString ("app.text.email.sent"));
            }
            else {
                alert(systranBox._activeEmail.getErrorMessage());
            }
        }
        else {
            setTimeout(systranBox.checkEmail, 200);
        }
    },

    printWindow : function(url,w,h) {
        if (window.myPWin && ! myPWin.closed) { myPWin.close(); }
        myPWin = window.open(url, "print", "scrollbars=yes,resizable=yes,toolbar=no,width="+w+",height="+h);
        myPWin.focus();
    },

    translate : function() {
        systranBox.translateText(systranBox._srcBox.value);
    },

    translateText : function(text) {
        text = trim(text);
        if(text == "") {
            return;
        }
        systranBox._lastText = text;
        systranBox._lastLp = languageSelection.getSelectedLp();
        systranBox._activeRequest = sai.translateText(text, languageSelection.getSelectedLp());
        setTimeout(systranBox.checkRequest, 200);
    },

    checkRequest : function() {
        if(systranBox._activeRequest == null) {
            return;
        }
        if(systranBox._activeRequest.isComplete()) {
            if(systranBox._activeRequest.isSuccess()) {
                var output = systranBox._activeRequest.getOutput();
                systranBox.loadResult(output);
            }
            else {
                alert("Error:" + systranBox._activeRequest.getErrorMessage());
            }
        }
        else {
            setTimeout(systranBox.checkRequest, 200);
        }
    },

    loadResult : function(text) {
        systranBox._tgtBox.value = text;
        if(systranBox.onLoadResult) {
            systranBox.onLoadResult();
        }
    },

    onSrcChange : function() {
        if(systranBox._srcChangeTimer != null) {
            clearTimeout(systranBox._srcChangeTimer);
        }
        systranBox._srcChangeTimer = setTimeout(systranBox.handleSrcChange, 1000);
    },

    handleSrcChange : function() {
        var text = trim(systranBox._srcBox.value);
        if(text != systranBox._lastText || languageSelection.getSelectedLp() != systranBox._lastLp) {
            systranBox.translateText(text);
        }
    },

    maxSize : function() {
        if(systranBox._maxSize != null) {
            var value = systranBox._maxSize - systranBox._srcBox.value.length;
            if(value < 0) {
                systranBox._srcBox.value = systranBox._srcBox.value.substring(0, systranBox._maxSize);
            }
        }
    },

    generateCharMAP : function() {
        var charMAP = "", lstCHAR = new Array("&#0161;","&#0191;","&#0162;","&#0163;","&#0165;","&#8364;","&#0164;","&#0166;","&#0167;","&#0168;","&#0169;","&#0174;","&#8482;","&#0170;","&#0172;","&#0175;","&#0176;","&#0180;","&#0181;","&#0182;","&#0183;","&#0184;","&#0186;","<br />","&#8220;","&#8221;","&#8222;","&#0171;","&#0187;","&#0185;","&#0178;","&#0179;","&#0188;","&#0189;","&#0190;","&#0247;","&#0215;","&#0177;","&#8240;","&#0402;","&#8211;","&#8212;","<br />","&#0198;","&#0230;","&#0199;","&#0231;","&#0208;","&#0240;","&#0209;","&#0241;","&#0216;","&#0248;","&#0254;","&#0222;","&#0223;","&#0140;","&#0156;","&#0352;","&#0353;","<br />","&#0192;","&#0193;","&#0194;","&#0195;","&#0196;","&#0197;","&#0200;","&#0201;","&#0202;","&#0203;","&#0204;","&#0205;","&#0206;","&#0207;","&#0210;","&#0211;","&#0212;","&#0213;","&#0214;","&#0217;","&#0218;","&#0219;","&#0220;","&#0221;","&#0376;","<br />","&#0224;","&#0225;","&#0226;","&#0227;","&#0228;","&#0229;","&#0232;","&#0233;","&#0234;","&#0235;","&#0236;","&#0237;","&#0238;","&#0239;","&#0242;","&#0243;","&#0244;","&#0245;","&#0246;","&#0249;","&#0250;","&#0251;","&#0252;","&#0253;","&#0255;");
        for(var i=0; i < lstCHAR.length; i++)
            { ( lstCHAR[i] != "<br />" ? charMAP += '<a href="javascript:void 0;" onclick="systranBox.specCHAR(innerHTML);" title="'+ lstCHAR[i] +'">'+ lstCHAR[i] +'</a>' : charMAP += lstCHAR[i] ); }
        document.write(charMAP);
    },

    specCHAR : function(x) {
        if(document.selection) {
            systranBox._srcBox.focus();
            var sel = document.selection.createRange();
            sel.text = x;
        }
        else if (systranBox._srcBox.selectionStart || systranBox._srcBox.selectionStart == '0') {
            var startPos = systranBox._srcBox.selectionStart;
            var endPos = systranBox._srcBox.selectionEnd;
            systranBox._srcBox.value = systranBox._srcBox.value.substring(0, startPos)+ x+ systranBox._srcBox.value.substring(endPos, systranBox._srcBox.value.length);
        }
        else {
            systranBox._srcBox.value += x;
        }
        systranBox.onSrcChange();
    }
}


function insertAtCursor(myField, myValue) {
//IE support

}


