
function nothing() { };

function strip_px(s) {
  if (!s) { alert('STRIP_PX !s'); return 0; }
  if (s.substring(s.length-2)!='px') return s;
  return Number(s.substring(0,s.length-2));
  }

String.prototype.trim=function() {
  return this.replace(/^\s+|\s+$/g,"");
  }

function array_in(a,s) {
  for (var i in a) { if (a[i]==s) return true };
  return false;
  }
function array_find(a,s) {
  for (var i in a) { if (a[i]==s) return i };
  return -1;
  }
function array_del(a,n) {
  if (n===undefined) return false;
  var x=a.splice(n,1);
  return x;
  };

function event_x(e) {
  try { if (e.clientX) return e.clientX; } catch (a) {};
  try { if (e.screenX) return e.screenX; } catch (a) {};
  }

function event_y(e) {
  try { if (e.clientY) return e.clientY; } catch (a) {};
  try { if (e.screenY) return e.screenY; } catch (a) {};
  }

function event_ok(e,msg) {
  if (!e) {
    if (!window.event) { alert(msg); return false; }
    return window.event;
    }
  return e;
  }

var inie=navigator.appName=='Microsoft Internet Explorer'?true:false;

function event_but(e) {
  if (inie) { return(e.button); }
  else return(e.which);
  }

function add_class(obj,c) {
  if (obj.className.indexOf(' '+c)<0) {
    obj.className=obj.className+' '+c;
    return true;
    }
  return false;
  }

function del_class(obj,c) {
  var result=false;
  while (obj.className.indexOf(' '+c)>=0) {
    obj.className=obj.className.replace(' '+c,'');
    result=true;
    }
  return result;
  }

function view_get_dx() {
  if (typeof window.innerWidth!='undefined')
    var vx=window.innerWidth;
  else {
    if (typeof document.documentElement!='undefined' &&
        typeof document.documentElement.clientWidth!='undefined' &&
        document.documentElement.clientWidth!=0)
      var vx=document.documentElement.clientWidth;
    else vx=document.getElementsByTagName('body')[0].clientWidth;
    }
  return vx;
  }
function view_get_dy() {
  if (typeof window.innerWidth!='undefined')
    var vy=window.innerHeight;
  else {
    if (typeof document.documentElement!='undefined' &&
        typeof document.documentElement.clientWidth!='undefined' &&
        document.documentElement.clientWidth!=0)
      var vy=document.documentElement.clientHeight-6;
    else
      vy=document.getElementsByTagName('body')[0].clientHeight;
    }
  return vy;
  }

var timers=new Array();

timer=function(ms) {
  this.ms=ms?ms:100;
  this.count=0;
  this.active=false;
  this.loop=function() { };
  }
timer.prototype.start=function() {
  this.active=true;
  this.num=timers.length;
  timers.push(this);
  this.process();
  }
timer.prototype.stop=function() {
  this.active=false;
  for (var i=this.num; i<timers.count-1; i++) timers[i]=timers[i+1];
  timers.pop();
  }
timer.prototype.process=function() {
  this.loop();
  this.count+=this.ms;
  setTimeout('timers['+this.num+'].process()',this.ms);
  }

ajax=function() {
  this.clean();
  }

ajax.prototype.clean=function() {
  this.error=false;
  this.txt=false;
  this.xml=false;
  this.state=0;
  this.loading=false;
  this.done=nothing;
  this.onerror=nothing;
  this.async=true;
  }

ajax.prototype.progress=function(i) {
  };

ajax.prototype.process=function() {
  if (!this.loading) return;
  this.state=this.req.readyState;
  this.progress(this.state);
  if (this.state == 4) {
    if (this.req.status == 200) {
      this.txt=this.req.responseText;
      this.xml=this.req.responseXML;
      this.progress(0);
      this.done();
      }
    else {
//      alert('HTTP error: '+this.req.status+' '+this.req.statusText);
      this.error=this.req.status;
      this.progress(0);
      this.onerror();
      }
    this.loading=false;
    }
  }

ajax.prototype.sendget=function(url) {
  if (!url) { alert('AJAX.SENDGET: No url!'); return; }
  this.url=url;
  if (this.async) this.progress(0);
  if (window.XMLHttpRequest) {
    this.req = new XMLHttpRequest();
    }
  else if (window.ActiveXObject) {
    this.req = new ActiveXObject("Microsoft.XMLHTTP");
    if (!this.req)
      this.req = new ActiveXObject("Msxml2.XMLHTTP");
    if (!this.req) {
      alert('AJAX.SENDGET: Cannot send XMLHttpRequest!');
      this.error=true; return false;
      }
    }
  var here=this; this.req.onreadystatechange=function () { here.process(); }
  this.req.open('get',url,this.async);
  this.req.send(null);
  this.loading=true;
  if (!this.async) this.progress(0);
  return true;
  }

ajax.prototype.sendpost=function(id,url) {
  var f=document.getElementById(id);
  var a=new Array();
  for (var i=0; i<f.elements.length; i++) if (f.elements[i].name) {
    var s=encodeURIComponent(f.elements[i].name);
    s+="=";
    s+=encodeURIComponent(f.elements[i].value);
    a.push(s);
    }
  this.post=a.join("&");
  if (!url) { alert('AJAX.REQUEST: No url!'); return; }
  this.url=url;
  if (this.async) this.progress(0);
  if (window.XMLHttpRequest) {
    this.req = new XMLHttpRequest();
    }
  else if (window.ActiveXObject) {
    this.req = new ActiveXObject("Microsoft.XMLHTTP");
    if (!this.req)
      this.req = new ActiveXObject("Msxml2.XMLHTTP");
    if (!this.req) {
      alert('AJAX.SENDPOST: Cannot send XMLHttpRequest!');
      this.error=true; return false;
      }
    }
  var here=this; this.req.onreadystatechange=function () { here.process(); }
  this.req.open('post',url,this.async);
  this.req.setRequestHeader("Content-type","application/x-www-form-urlencoded, charset=utf-8");
  this.req.setRequestHeader("Content-length",this.post.length);
  this.req.setRequestHeader("Connection","close");
  this.req.send(this.post);
  this.loading=true;
  if (!this.async) this.progress(0);
  return true;
  }

ajax.prototype.request=function(url) {
  return this.sendget(url);
  }

function make_attr(a) {
  if (!a) a=new Array();
  var s='';
  for (var i=0; i<a.length; i++) {
    if (s!='') s=s+' ';
    if (!a[i][1]) s=s+a[i][0];
    else s=s+a[i][0]+'="'+a[i][1]+'"';
    }
  return s;
  }

function obj_setattr(o,a) {
  if (!o) { alert('OBJ_SETATTR: !o'); return; }
  if (!a) a=new Array();
  for (var i=0; i<a.length; i++) {
    if (!a[i][1]) o.setAttribute(a[i][0],'');
    else o.setAttribute(a[i][0],a[i][1]);
    }
  }

function create_obj(tag,a) {
  var o;
  if (!a) a=new Array();
  o=document.createElement(tag);
  obj_setattr(o,a);
  return o;
  }

function kill(el) { el.parentNode.removeChild(el); }

function flush(el) {
  if (el.hasChildNodes()) {
    while (el.childNodes.length>=1) el.removeChild(el.firstChild);
    }
  }

function hide(box) {
  if (document.getElementById(box).style.display!='none') {
    document.getElementById(box).style.display='none';
    return(true);
    }
  return(false);
  }
function show(box) {
  if (document.getElementById(box).style.display!='') {
    document.getElementById(box).style.display='';
    return(true);
    }
  return(false);
  }
function show_hide(sh,hd) {
  if (hd) document.getElementById(hd).style.display='none';
  if (sh) document.getElementById(sh).style.display='';
  }
function toggle(box) {
  var b=document.getElementById(box);
  b.style.display=b.style.display=='none'?'':'none';
  }

function ajax_feed(feed,sh,url) {
  var a=new ajax();
  a.feed=feed; a.show=sh;
  a.done=function () {
    if (this.feed) {
      var feed=document.getElementById(this.feed);
      feed.innerHTML=this.txt;
      }
    if (this.show) {
      var show=document.getElementById(this.show);
      show.style.display='';
      }
    }
  a.request(url+'&_CLEAN');
  }

function ajax_trigger(feed,sh,url) {
  if (!hide(sh)) ajax_feed(feed,sh,url);
  }

var clipboard='';

function get_selection() {
  var t='';
  if (clipboard!='') {
    t=clipboard;
    clipboard='';
    }
  else if (window.getSelection) {
    var s=window.getSelection();
    if (!s.isCollapsed) for (i=0; i<s.rangeCount; i++) {
      var r=s.getRangeAt(i);
      t=t+r.toString();
      }
    }
  else if (document.selection) {
    t=t+document.selection.createRange().text;
    }
  return t;
  }
function selection_capture() {
  clipboard='';
  clipboard=get_selection();
  }

function ta_position(ta,last) {
  ta.focus();
  if (ta.selectionEnd) {
    if (last) return ta.selectionEnd;
    else return ta.selectionStart;
    }
  else if (window.getSelection) {
    s=window.getSelection();
    r=s.getRangeAt(0); r.startOffset;
    if (last) return r.endOffset;
    else return r.startOffset-1;
    }
  else if (document.selection) {
    var r=document.selection.createRange();
    var c=r.duplicate();
    c.moveToElementText(ta);
    r.collapse(!last);
    c.setEndPoint('EndToEnd',r);
    return c.text.length;
    }
  return ta.value.length;
  }
function ta_selection(ta) {
  ta.focus();
  var n1=ta_position(ta);
  var n2=ta_position(ta,true);
  return(ta.value.substr(n1,n2-n1));
  }
function ta_sel(ta,n,m) {
  if (!m) var m=n;
  if (ta.selectionStart) {
    ta.setSelectionRange(n,m);
    ta.focus();
    }
  else if (ta.createTextRange) {
    var r=ta.createTextRange();
    r.moveStart('character',n);
    if (n!=m) r.moveEnd('character',m);
    else r.collapse(true);
    r.select();
    }
  }
function ta_insert(ta,s) {
  ta.focus();
  var pos=ta_position(ta);
  ta.value=ta.value.substr(0,pos)+s+ta.value.substr(pos);
  ta_sel(ta,pos,pos+s.length);
  }
function ta_replace(ta,s,p) {
  ta.focus();
  var n1=ta_position(ta);
  var n2=ta_position(ta,true);
  if (document.selection) {
    var r=document.selection.createRange();
    r.text=s;
    }
  else {
    ta.value=ta.value.substr(0,n1)+s+ta.value.substr(n2);
    }
  if (!p) p=s.length;
  ta_sel(ta,n1+p);
  ta.focus();
  }
function ta_surround(ta,s1,s2) {
  if (!s1) s1='';
  if (!s2) s2='';
  ta.focus();
  var n1=ta_position(ta);
  var n2=ta_position(ta,true);
  ta.value=ta.value.substr(0,n1)+s1+ta.value.substr(n1,n2-n1)+s2+ta.value.substr(n2);
  ta_sel(ta,n1+s1.length);
  }

function bb_button(fe,title,el,func,s) {
  var feed=document.getElementById(fe);
  var b=create_obj('a');
  b.className='bb';
  b.href='javascript:void(0)';
  b.title=title;
  b.el=el; b.func=func;
  b.onclick=function() { bb_func(this.el,this.func); }
  b.onmouseover=function() { selection_capture(); }
  b.onmousedown=function() { this.className='bb bb_active'; }
  b.onmouseup=function() { this.className='bb'; }
  b.innerHTML='<nobr>'+s+'</nobr>';
  feed.appendChild(b);
  }

function bb_func(el,func) {
  var ta=document.getElementById(el);
  var c=get_selection();
  if (c=='') c=ta_selection(ta);
  if (c!='') c=c.trim();
  switch (func) {
    case 'b':
    case 'i':
    case 'u':
    case 's':
    case 'p':
      var s='['+func+']'+c+'[/'+func+']';
      ta_replace(ta,s,s.length);
      break;
    case 'title':
    case 'header':
    case 'code':
    case 'q':
      var s='['+func+']'+c+'[/'+func+"]\n";
      ta_replace(ta,s,s.length);
      break;
    case 'url':
      if (c=='' || w=='') { c='http://ссылка'; var w='название'; }
      else { var w=c.replace('https://',''); w=w.replace('http://',''); w=w.replace('ftp://',''); }
      var s='[url='+c+']'+w;
      ta_replace(ta,s+'[/url]',s.length-1);
      break;
    case 'hr':
      var s="[line]\n";
      ta_replace(ta,s,s.length);
      break;
    case 'br':
      var s="[br]";
      ta_replace(ta,s,s.length);
      break;
    }
  }

function bb_panel(el,ta) {
  bb_button(el,'Сделать выделенный текст жирным шрифтом',ta,'b','<b>Ж</b>');
  bb_button(el,'Сделать выделенный текст наклонным шрифтом',ta,'i','<i>К</i>');
  bb_button(el,'Подчеркнуть выделенный текст',ta,'u','<u>П</u>');
  bb_button(el,'Зачеркнуть выделенный текст',ta,'s','<s>X</s>');
  bb_button(el,'Сделать выделенный текст подзаголовком',ta,'header','<b>H</b>');
  bb_button(el,'Сделать выделенный текст большим заголовком',ta,'title','<b>T</b>');
  bb_button(el,'Сделать выделенный текст отдельным абзацем',ta,'p','<b>&para;</b>');
  bb_button(el,'Вставить принудительный переход на новую строку',ta,'br','<b>BR</b>');
  bb_button(el,'Вставить горизонтальную линию',ta,'hr','&mdash;');
  bb_button(el,'Выделить или вставить кусок кода',ta,'code','<span style="font-family:courier,monotype; font-weight:bold">Код</span>');
  bb_button(el,'Сделать выделенный текст цитатой',ta,'q','<span style="font-weight:bold">"..."</span>');
  bb_button(el,'Сделать выделенный текст ссылкой',ta,'url','http://');
  }

