// COPYRIGHT
// ~~~~~~~~~
// Original idea of Orphus system belongs to Dmitry Koteroff.
// If you want to modify this script, please contact 
// the author first: http://forum.dklab.ru/other/orphus/
var orphus = {
  maxlen:   256,
	badbrowser: "Ваш браузер не поддерживает эту функцию",
	toobig:     "Длина выделенного текста слишком верика",
	thanks:     "Спасибо!",
	docmsg:     "Документ: ",
	intextmsg:  "Орфографическая ошибка: ",
	ifsendmsg:  "Отправить сообщение об ошибке автору?",

  init: function() { with (this) {
    var th = this;
    // Hook keyboard.
    parent.document.onkeypress = function(e) { return th.onkeypress(e) };
  }},

  onkeypress: function(e) { with (this) {
    var pressed=0;
    var we = null;
    if (window.event) we = window.event;
    else if (parent && parent.event) we = parent.event;
    if (we) {
      // IE & Opera
      pressed = we.keyCode==10 ||  // IE
        (we.keyCode == 13 && we.ctrlKey); // Opera 
    } else if (e) {
      // NN
      pressed = 
        (e.which==10 && e.modifiers==2) || // NN4
        (e.keyCode==0 && e.charCode==106 && e.ctrlKey) ||
        (e.keyCode==13 && e.ctrlKey) // Mozilla
    }
    if (pressed) this.doSend();
  }},

  doSend: function(recurrent) { with (this) {
    var text = null;
    if (navigator.appName.indexOf("Netscape")!=-1 && eval(navigator.appVersion.substring(0,1))<5) {
      alert(badbrowser);
      return;
    }
    var w = parent;
    var selection = null;
    if (w.getSelection) {
      text = w.getSelection();
    } else if (w.document.getSelection) {
      text = w.document.getSelection();
    } else {
      selection = w.document.selection;
    }
    var context = null;
    if (selection) {
      var r = selection.createRange(); if (!r) return;
      text = r.text;
      context  = [text, 0];
    } else {
      context = [text, -1];
    }
    if (text == null) { 
      alert(orphus.badbrowser); 
      return; 
    }
    if (context[0] == "") return;
    var visCont = stripSlashn(context[0]);
    if (visCont.length > maxlen) {
      alert(toobig);
      return;
    }
    var url = w.document.location.href;
    var ts = new Date().getTime();
    var result = confirm(docmsg+"\n   "+url+"\n"+intextmsg+'\n   "'+visCont+'"\n\n'+ifsendmsg);
    var dt = new Date().getTime() - ts;
    if (result) {
      this.send(url, context);
    } else {
      if (!recurrent && dt < 50) {
        // Stupid MyIE blocks confirm() while Ctrl is pressed.
        var th = this;
        var sv = parent.document.onkeyup;
        parent.document.onkeyup = function(e) { with (th) {
          if (!e) e = parent.event;
          if (e.keyCode == 17) { // Ctrl is up.
            parent.document.onkeyup = sv;
            doSend(true);
          }
        }}
      }
    }
  }},

  stripSlashn: function(text) {
    text = ""+text;
    return text.replace("\r", "").replace("\n", "").replace(new RegExp("^\\s+|\\s+$", "g"), "");
  },

  send: function(url, context) { with (this) {
    var form = document.forms['form'];
    if (!form) return;
    form.bagref.value = url;
    form.bagreport.value = context[0];
    form.submit();
  }}

}
orphus.init();
