var strQuoteControlKey = "SHIFT-Enter";

var textid = 'phorum_textarea';

var navstr = navigator.userAgent.toLowerCase();
var isMoz = 0;
var isIE = 0;

//alert( 'navstr = ' + navstr );

isIE = (navstr.indexOf("ie") > -1);
isChrome = (navstr.indexOf("chrome") > -1);
if (!isIE && !isChrome)
  isMoz = (navstr.indexOf("mozilla") > -1);

isOpera = (navstr.indexOf("opera") > -1);

//alert( navstr + ' ' + isOpera );

function KeyPressedQuote(event)
{
  var code = 0;
  var ctrl = 0;
  var shift = 0;

  if (isMoz) {
    code = event.which;
    ctrl = event.ctrlKey;
    shift = event.shiftKey;
  }
  else
  {
    code = window.event.keyCode;
    ctrl = window.event.ctrlKey;
    shift = window.event.shiftKey;

  }

  if (ctrl && shift)
  {
    return DoCode( code );
  }
  else
  {

  if ((code == 13 )&&( shift ))
  {

    var userSelection;
    var n = 0;

    if (window.getSelection  && !window.opera)
    {
    	userSelection = window.getSelection();
    	n = 1;
    }
    else if (document.getSelection) 
    {
	    userSelection = document.getSelection();
	    n = 2;
    }
    else if (document.selection)
    {
      // should come last; Opera!
    	userSelection = document.selection.createRange().text;
    	n = 3;
    }

    if (userSelection != '') 
    {
      //alert( n + ': ' + userSelection );

      var quote = "\n[quote]" + userSelection + "[/quote]\n";
      var txt = document.getElementById(textid);
      txt.value += quote;

      //alert('Цитата добавлена к сообщению');

      return false;
    }
  }

  }
}

function DoCode( code )
{
    //if (( code != 16 ) && ( code != 17 ))
    //alert( "code = " + code );

    if (isIE) {
      var code_i = 9; var code_b = 2; var code_u = 21; var code_s = 19; var code_a = 1; var code_p = 16; var code_c = 3; var code_d = 4;
    } else {
      var code_i = 73; var code_b = 66; var code_u = 85; var code_s = 83; var code_a = 65; var code_p = 80; var code_up = 38; var code_dn = 40; var code_c = 67; var code_d = 68;
    }

    if (!isIE) //(isMoz) 
    {
      var textfield = document.getElementById(textid);

      if (textfield.selectionStart || textfield.selectionStart == '0') {
        //textfield.focus();
        var startPos = textfield.selectionStart;
        var endPos = textfield.selectionEnd;
        txt1 = textfield.value.substring(startPos, endPos);
      }

    } else {
      cursor_pos_selection = document.selection.createRange(); 
      txt1 = cursor_pos_selection.text;                         
    }

    txt2 = "";

    if (txt1.length == 0) 
    { 
      return true; 
    }
    else 

    if ((code == "bold")||(code == code_b)) { // b
      txt2 = "<b>" + txt1 + "</b>";
    } 
    else if ((code == "italic")||(code == code_i)) { // i
      txt2 = "<i>" + txt1 + "</i>";
    } 
    else if ((code == "uline")||(code == code_u)) { // u
      txt2 = "<u>" + txt1 + "</u>";
    } 
    else if ((code == "symbol")||(code == code_s)) { // s
      txt2 = "<font face=\"Symbol\">" + txt1 + "</font>";
    } 
    else if ((code == "sup")||(code == code_up)) { 
      txt2 = "<sup>" + txt1 + "</sup>";
    } 
    else if ((code == "sub")||(code == code_dn)) { 
      txt2 = "<sub>" + txt1 + "</sub>";
    } 
    else if ((code == "link")||(code == code_a)) { // a
      txt2 = "<a href=\"" + txt1 + "\">" + txt1 + "</a>";
    } 
    else if ((code == "image")||(code == code_p)) { // p
      txt2 = "<img src=\"" + txt1 + "\">";
    } 

    else if ((code == "cut")||(code == code_c)) { // p
      txt2 = "[cut=Скрытый текст]" + txt1 + "[/cut]";
    } 

    else if ((code == "strike")||(code == code_d)) { // p
      txt2 = "<s>" + txt1 + "</s>";
    } 
    else if ((code == "tab-table")) { 
      txt2 = "[tab-table]" + txt1 + "[/tab-table]";
    } 
    else
    {
      return true;
    }

    if ( txt2 != "" ) {
      if (!isIE) //(isMoz) 
      {
        textfield.value = textfield.value.substring(0, startPos) + 
                          txt2 + 
                          textfield.value.substring(endPos, textfield.value.length);
      } else {
        cursor_pos_selection = document.selection.createRange(); 
        cursor_pos_selection.text = txt2;
      }
    }

    return false;
}

function ExtractText(node)
{
  var node_str = ( node.nodeValue != null) ? node.nodeValue : "";

  if ( node.childNodes != null )
    for (var i = 0; i < node.childNodes.length; i++)
      node_str += ExtractText( node.childNodes[i] );

  return node_str;
}


function QuoteAllMessage(textid)
{
    var msg = document.getElementById("message_text");

    if (isIE){
      msg = msg.innerText;    //alert('ie: '+ msg);
    } else if (isMoz) {
      msg = ExtractText(msg); //alert('moz: '+ msg);
    } else {
      msg = msg.text;         //alert('opera: '+ msg);
    }

    //msg = msg.replace( "\n", '' );

    var quote = "\n>>\n" + msg + "\n>>\n";

    var txt = document.getElementById(textid);
    txt.value += quote;

    //alert('Цитата добавлена к сообщению');
}
