//
// Bug report script
// based on Basis JavaSript library
// ---------------------------------
// Homepage: basis.et-lab.ru
// Author:   Dvornov lahmatiy Roman
// copyright (c) 2006-2007, Embl at www.embl.ru <office@embl.ru>
//
Function.prototype.bind=function(z)
{
 var ac=this;
 return function()
        {
         ac.apply(z,arguments)
        }
};
if(!Function.apply)
{
 Function.prototype.apply=function(h,m)
                          {
                           var r=[];
                           if(!h)h=window;
                           if(!m)m=[];
                           for(var i=0;i<m.length;i++)
                             r.push('m['+i+']');
                           h._a=this;
                           var y=eval('h._a('+r.join(', ')+')');
                           h._a=null;
                           return y
                          }
};
if(!Array.push)
{
 Array.prototype.push=function()
                      {
                       for(var i=0,j=this.length;i<arguments.length;i++,j++)
                         this[j]=arguments[i];
                       return this.length
                      }
};
function ae(v)
{
 return parseInt(v).toString(16).toUpperCase()
};

function getSelectionText()
{
 var a;
                                 if(document.selection)
                                 {
                                  var s=document.selection.createRange();
                                  a=(s&&s.text)||''

                                  s.collapse(true);
                                  var el=s.parentElement();
                                  s=document.selection.createRange();
                                  s.collapse(false);
                                  el=s.parentElement(); el1=el.parentElement;
                                  elem = 'ptag: '+el1.tagName+', pclass: '+el1.className+', pid: '+el1.id+', tag: '+el.tagName+', class: '+el.className+', id: '+el.id;
                                 }
                                 else
                                 {
                                  a=(window.getSelection||this.window.document.getSelection||function()
                                                                                             {return''})()

                                  selection=window.getSelection();
                                  range = selection.getRangeAt(0);
                                  start = range.startContainer;
                                  if(start.nodeName.toLowerCase() == "body") return null;
                                  if(start.nodeName == "#text")
                                  {
                                   var el=start.parentNode; var el1=el.parentNode;
                                   elem = 'ptag: '+el1.tagName+', pclass: '+el1.className+', pid: '+el1.id+', tag: '+el.tagName+', class: '+el.className+', id: '+el.id;
                                  }
                                 }
 return {"selection":a, "el":el, "additional":elem};
}

var x=window.escape;
var f={};
f[1025]=168;
f[1105]=184;
for(var i=1040;i<=1103;i++)
  f[i]=i-848;
  window.escape=function(o)
                {
                 var t=[];
                 for(var i=0;i<o.length;i++)
                 {
                  var n=o.charCodeAt(i);
                  if(typeof f[n]!='undefined')n=f[n];
                  if(n<=255)t.push(n)
                 }
                 return x(String.fromCharCode.apply(null,t))
                };

reportSelection = function()
                               {
                                var a; var elem;
                                try
                                {
                                 sel = getSelectionText();
                                 a=sel.selection;
                                 elem = sel.additional;
                                 el = sel.el;
                                }
                                catch(e)
                                {
                                 alert(e.message);
                                 return
                                }
                                if(a)
                                {
                                 a=String(a).replace(/^\s+|\s+$/g,'').replace(/\s+/g,' ');
                                 if(!a)return;
                                 if(a.length>255){alert('Вы выделили слишком большой текст!');return
                                }
                                if(confirm('Вы выделили текст:\n\n>>>'+a+'<<<\n\nХотите послать сообщение об ошибке?'))
                                {
                                 var u;
                                 if(u=prompt('Пожалуйста, прокомментируйте ошибку (обязательно):',''))
                                 {
                                  var d=new Transport(location.protocol+'//'+location.host+ERU);
                                  d.handlers.onComplete=function(aa)
                                                        {
                                                         var g=aa.responseText.split("\n");
                                                         if(g[g.length-1].toLowerCase()=='ok')
                                                         {
                                                          g.length=g.length-1;
                                                          alert(g.join('\n'))
                                                         }
                                                        };
                                  d.setParam('url',location.href);
                                  d.setParam('text',a);
                                  d.setParam('comment',u);
                                  d.setParam('element',elem);d.get()
                                 }
                                }
                               }
                              }


Transport=function(b,method)
          {
           this.transport=this.getTransport();
           this.url=b;
           (method ? this.method=method : this.method='GET');
           this.params={};
           this.handlers={};
           return this
          };
Transport.prototype={_d:['Uninitialized','Loading','Loaded','Interactive','Complete'],
setParam:function(ad,w)
         {
          this.params[ad]=w
         },
         responseIsSuccess:function()
                           {
                            try
                            {
                             return(this.transport.status==undefined)||(this.transport.status==0)||(this.transport.status>=200&&this.transport.status<300)
                            }
                            catch(e)
                            {
                             alert('Get transport status error:\n\n    '+(e.message||e));
                             return
                            }
                           },
         responseIsFailure:function()
                           {
                            return!this.responseIsSuccess()
                           },
         request:function(b)
                 {
                  var l;
                  var q=[];
                  for(var p in this.params)
                    q.push(p+'='+escape(this.params[p]));
                    l=q.join('&');
                  try{if(l && this.method.toUpperCase() != 'POST')b+=(b.indexOf('?')==-1?'?':'&')+l;
                  this.transport.open(this.method,b,true);
                  this.transport.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                  this.transport.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
                  this.transport.onreadystatechange=this.respondToReadyState.bind(this);

                  this.transport.send(this.method.toUpperCase() == 'POST' ? l : null);

                 }
         catch(e)
         {
          alert('Transport request error:\n\n   '+(e.message||e.description||e))}},get:function(b)
                                                                                        {
                                                                                         this.request(b||this.url)},
                                                                                         respondToReadyState:function(ab)
                                                                                                             {
//                                                                                                              var c=this._d[(ab && ab.eventPhase ? ab.eventPhase : ab)||this.transport.readyState];
//                                                                                                              alert(ab.eventPhase);

                                                                                                              var c=this._d[this.transport.readyState];

                                                                                                              var j;
                                                                                                              if(c=='Complete')
                                                                                                              {
                                                                                                               if(j=this.handlers.onComplete)j(this.transport)
                                                                                                              }
                                                                                                              else
                                                                                                               if(j=this.handlers['on'+c])j(this.transport)
                                                                                                             }
                                                                                         };
          Transport.prototype.getTransport=function()
                                           {
                                            var k=new Array(function()
                                                            {
                                                             return XMLHttpRequest?new XMLHttpRequest():null
                                                            },
                                                            function()
                                                            {
                                                             return ActiveXObject?new ActiveXObject('Msxml2.XMLHTTP'):null
                                                            },
                                                            function()
                                                            {
                                                             return ActiveXObject?new ActiveXObject('Microsoft.XMLHTTP'):null
                                                            });
                                            for(var i=0;i<k.length;i++)
                                              try
                                              {
                                               if(k[i]())
                                               return k[i]
                                              }
                                              catch(e){}
                                              return function()
                                                     {
                                                      return null
                                                     }
                                           }();
          document.onkeypress=function(e)
                              {
                               var c=e||window.event;
                               if(c.ctrlKey&&(c.keyCode==10||c.keyCode==13))
                               reportSelection();
         }
