function BrowserType() {
  var MSIE=navigator.userAgent.indexOf('MSIE');
  var NETS=navigator.userAgent.indexOf('Netscape');
  var OPER=navigator.userAgent.indexOf('Opera');
  var MZ=navigator.userAgent.indexOf('Mozilla');

  if (MZ != -1 && MSIE == -1 && NETS == -1 && OPER == -1) {
        return 'MZ';
  }
  else if (NETS != -1) {
      return 'NETS';
  }
  else if (MSIE != -1 && OPER == -1) {
    return 'MSIE';
  }
  else {
        return 'OPER';
  }
}
function TagInsert(tag, elm) {
    window.document.getElementById(elm).focus();
    if(tag == '[a]') {
      var userInput = prompt("Please enter the site you'd like to link", "http://");
      tag = '<a href=\'' + userInput + '\'></a>';
    }
    var rng = document.selection.createRange();
    rng.text = tag;
}
function CreatePanel(elm) {
  if(BrowserType() == 'MSIE') {
    document.write('<input class=reg_input3 type="button" value="Жирн." OnClick="TagInsert(\'<b></b>\', \'' + elm + '\')">');
    document.write('&nbsp;');
    document.write('<input class=reg_input3 type="button" value="Курсив" OnClick="TagInsert(\'<i></i>\', \'' + elm + '\')">');
    document.write('&nbsp;');
    document.write('<input class=reg_input3 type="button" value="Нов.стр." OnClick="TagInsert(\'<br>\', \'' + elm + '\')">');
    document.write('&nbsp;');
    document.write('<input class=reg_input3 type="button" value="Подч." OnClick="TagInsert(\'<u></u>\', \'' + elm + '\')">');
    document.write('&nbsp;');
    document.write('<input class=reg_input3 type="button" value="Ссылка" OnClick="TagInsert(\'[a]\', \'' + elm + '\')">');
    document.write('&nbsp;');
  }
  else {
    document.write('<br>');
    document.write('<div class="CtrlButton" onclick="return pnhEditTextarea(\'' + elm + '\', \'br\')">Нов.стр.</div>');
    //document.write('[<a href="#" onclick="return pnhEditTextarea(\'' + elm + '\', \'strong\')">Strong</a>]');
    document.write('<div class="CtrlButton" onclick="return pnhEditTextarea(\'' + elm + '\', \'bold\')">Жирн.</div>');
    document.write('<div class="CtrlButton" onclick="return pnhEditTextarea(\'' + elm + '\', \'cursive\')">Курсив</div>');
    document.write('<div class="CtrlButton" onclick="return pnhEditTextarea(\'' + elm + '\', \'u\')">Подч.</div>');
//    document.write('[<a href="#" onclick="return pnhEditTextarea(\'' + elm + '\', \'emphasis\')">Em</a>]');
    document.write('<div class="CtrlButton" onclick="return pnhEditTextarea(\'' + elm + '\', \'a_href\')">Ссылка</div>');
    document.write('<br>');
  }
}


// for Mozilla
function pnhGetSelectionStart(element) {
    var startpos = 0;
    startpos = element.selectionStart; // moz
    return startpos;
}

function pnhGetSelectionEnd(element) {
    var endpos = 0;
    endpos = element.selectionEnd; // moz
    return endpos;

}

function pnhSetSelectionStart(element,newposition) {
    element.selectionStart = newposition; // moz
}


function pnhSetSelectionEnd(element,newposition) {
    element.selectionEnd = newposition; // moz
}

function pnhSetSelection(element,newstart,newend) {
    pnhSetSelectionStart(element,newstart);
    pnhSetSelectionEnd(element,newend);
}


// Inserts a string at a given position
function pnhStringInsert(DOMEle,newtext,newpos) {
    DOMEle.value=DOMEle.value.slice(0,newpos)+newtext+DOMEle.value.slice(newpos);
}


function pnhEditTextarea(textarea_id, action) {
    // grab the textarea element off the dom tree
    var ta = $(textarea_id); //document.getElementById(textarea_id);

    // decide what you're addding
    var startTag = "";
    var endTag = "";

    switch (action) {
        case "bold":
            startTag = "<b>";
            endTag   = "</b>";
            break;
        case "cursive":
            startTag = "<i>";
            endTag   = "</i>";
            break;
        case "br":
            startTag = "";
            endTag   = "<br>";
            break;
        case "u":
            startTag = "<u>";
            endTag   = "</u>";
            break;
        case "strong":
            startTag = "<strong>";
            endTag   = "</strong>";
            break;
        case "emphasis":
            startTag = "<em>";
            endTag   = "</em>";
            break;
        case "a_href":
            var userInput = prompt("Please enter the site you'd like to link", "http://");
            startTag = "<a href=\""+userInput+"\">";
            endTag = "<\/a>";
            break;


    }


    // decide where to add it and then add it

//    alert("selectionEnd,length: "+ta.selectionEnd+","+ta.value.length);

    var firstPos = pnhGetSelectionStart(ta);
    var secondPos = pnhGetSelectionEnd(ta)+startTag.length; // we're inserting one at a time
    pnhStringInsert(ta,startTag,firstPos);
    pnhStringInsert(ta,endTag,secondPos);


    // reset focus... after the first tag and before the second
    pnhSetSelectionStart(ta,firstPos+startTag.length);
    pnhSetSelectionEnd(ta,secondPos);
    ta.focus();

    return false;
}

// !for Mozilla

function ImgInsert(img, elm, child, w, h, alt, spawEd) {
//    window.document.getElementById(elm).focus();
    if (child == '') {
      tag = '<img src="' + img+ '" alt="' + alt + '" ' + (w ? " width=" + w : "") + (h ? " height=" + h : "") +  '>';
    }else {
//      tag = '<a href="show_im.php?img_id=' + child + '" target="_blank"><img src="' + img+ '" border="0"></a>';
      tag = "<a href=\"#\" OnClick=\"window.open('/show_im.php?img_id=" + child + "','win_1','resizable,width=310,height=340'); return false;\">" + '<img src="' + img+ '" alt="' + alt + '" ' + (w ? " width=" + w : "") + (h ? " height=" + h : "") +  '>' + "</a>";

    }

    if (spawEd && spawEd.getActivePage().editing_mode == 'design') { 
      spawEd.insertHtmlAtSelection(tag);
      return false;
    }

    var textEl = get_by_id(elm);
//    var text = tag;
    if (document.selection) {
      textEl.focus();
      var rng = document.selection.createRange();
      rng.text = tag;
    } else { // Mozilla    
      if (textEl.selectionEnd) {
        mozWrap(textEl, tag);
      } else {
	textEl.value = textEl.value + tag;
      }
    }
    return false;
}

function mozWrap(txtarea, txt){
  var selLength = txtarea.textLength;
  var selStart = txtarea.selectionStart;
  var s1 = (txtarea.value).substring(0,selStart);
  var s2 = (txtarea.value).substring(selStart, selLength);
  txtarea.value = s1 + txt + s2;
}
		       

function DeleteConfirm() {
  if (confirm('Вы действительно хотите удалить данную запись?')) {
    return true;
  } else {
    return false;
  }
}

function get_by_id(id) {
  return document.getElementById ? document.getElementById(id) :
     document.all ? document.all[id] : 0;
};
	    
function $(id) {
  return get_by_id(id);
}

function newsAsyncReq() { 
  var ajaxReq = new CXMLHttp();
  ajaxReq.onDone = function(xmlhttp) { 
    window.location.reload();
  }
  var req = collectFormItems('', 'newsDelivery');
  ajaxReq.postQuery('?action=news_delivery', req);
}
	      
function collectFormItems(url, formName) { 
  var form = document.forms[formName];
  if (form == null) 
    return '';
  
  for (var i = 0; i < form.elements.length; i++) {
    el = form.elements[i];
    if (el.type == 'checkbox')
      v = (el.checked) ? 1 : 0;
    else
      v = el.value;

    if (el.type == 'radio' && el.checked)
      v = el.value;
    else if (el.type == 'radio' && !el.checked)
      continue;

    if (el.name != '') {
      if (url == '')
        url = el.name + "=" + encodeURIComponent(v);
      else
        url += "&" + el.name + "=" + encodeURIComponent(v);
    }
  }
  return url;
}
