var jsIBEUtils =
{
  addEvent: function(el, evname, func)
  {
    if(el.attachEvent) // IE
      el.attachEvent("on" + evname, func);
    else if(el.addEventListener) // Gecko / W3C
      el.addEventListener(evname, func, false);
    else
      el["on" + evname] = func;
  },

  removeEvent: function(el, evname, func)
  {
    if(el.detachEvent) // IE
      el.detachEvent("on" + evname, func);
    else if(el.removeEventListener) // Gecko / W3C
      el.removeEventListener(evname, func, false);
    else
      el["on" + evname] = null;
  },

  GetRealPos: function(el)
  {
    if(!el || !el.offsetParent)
      return false;
    var res=Array();
    res["left"] = el.offsetLeft;
    res["top"] = el.offsetTop;
    var objParent = el.offsetParent;
    while(objParent.tagName.toUpperCase() != "BODY" && objParent.tagName.toUpperCase() != "HTML")
    {
      res["left"] += objParent.offsetLeft;
      res["top"] += objParent.offsetTop;
      objParent = objParent.offsetParent;
    }
    res["right"]=res["left"] + el.offsetWidth;
    res["bottom"]=res["top"] + el.offsetHeight;

    return res;
  },

  FindChildObject: function(obj, tag_name, class_name)
  {
    if(!obj)
      return null;
    var tag = tag_name.toUpperCase();
    var cl = (class_name? class_name.toLowerCase() : null);
    var n = obj.childNodes.length;
    for(var j=0; j<n; j++)
    {
      var child = obj.childNodes[j];
      if(child.tagName && child.tagName.toUpperCase() == tag)
        if(!class_name || child.className.toLowerCase() == cl)
          return child;
    }
    return null;
  },

  FindNextSibling: function(obj, tag_name)
  {
    var o = obj;
    var tag = tag_name.toUpperCase();
    while(o.nextSibling)
    {
      var sibling = o.nextSibling;
      if(sibling.tagName && sibling.tagName.toUpperCase() == tag)
        return sibling;
      o = sibling;
    }
    return null;
  },

  FindPreviousSibling: function(obj, tag_name)
  {
    var o = obj;
    var tag = tag_name.toUpperCase();
    while(o.previousSibling)
    {
      var sibling = o.previousSibling;
      if(sibling.tagName && sibling.tagName.toUpperCase() == tag)
        return sibling;
      o = sibling;
    }
    return null;
  },

  FindParentObject: function(obj, tag_name)
  {
    var o = obj;
    var tag = tag_name.toUpperCase();
    while(o.parentNode)
    {
      var parent = o.parentNode;
      if(parent.tagName && parent.tagName.toUpperCase() == tag)
        return parent;
      o = parent;
    }
    return null;
  },

  FindParentObjectId: function(obj, tag_name, id)
  {
    if(!obj)
      return null;
    var o = obj;
    var tag = tag_name.toUpperCase();
    var divId = (id ? id.toLowerCase() : null);
    while(o.parentNode)
    {
      var parent = o.parentNode;
      if(parent.tagName && parent.tagName.toUpperCase() == tag && parent.getAttribute('id') && parent.getAttribute('id').toLowerCase() == divId)
        return parent;
      o = parent;
    }
    return null;
  },

  IsIE: function()
  {
    return (document.attachEvent && !this.IsOpera());
  },

  IsOpera: function()
  {
    return (navigator.userAgent.toLowerCase().indexOf('opera') != -1);
  },

  ToggleDiv: function(div)
  {
    div = jsIBEUtils.GetById(div);
    var style = div.style;
    if(style.display!="none")
      style.display = "none";
    else
      style.display = "block";
    return (style.display != "none");
  },

  urlencode: function(s)
  {
    return escape(s).replace(new RegExp('\\+','g'), '%2B');
  },

  OpenWindow: function(url, width, height)
  {
    var w = screen.width, h = screen.height;
    if(this.IsOpera())
    {
      w = document.body.offsetWidth;
      h = document.body.offsetHeight;
    }
    info = window.open(url, 'info', 'status=no,scrollbars=yes,resizable=yes,width='+width+',height='+height+',top='+Math.floor((h - height)/2-14)+',left='+Math.floor((w - width)/2-5));
    try{info.focus()}
    catch(e){};
  },

  SetPageTitle: function(s)
  {
    document.title = phpVars.titlePrefix+s;
    var h1 = document.getElementsByTagName("H1");
    if(h1)
      h1[0].innerHTML = s;
  },

  LoadPageToDiv: function(url, div_id)
  {
    var div = jsIBEUtils.GetById(div_id);
    if(!div)
      return;
    CHttpRequest_old.Action = function(result)
    {
      CloseWaitWindow();
      div.innerHTML = result;
    }
    ShowWaitWindow();
    CHttpRequest_old.Send(url);
  },

  LoadHTMLToDiv: function(url, div_id, postData,function_to_call)
  {
    var div = jsIBEUtils.GetById(div_id);
    var ftc = function_to_call;
    if(!div)
      return;
    CHttpRequest_old.Action = function(result)
    {
      CloseWaitWindow();
      div.style.cursor = 'default';
      if ( result == null )
      {
        div.innerHTML = '<div class="errortext">' + phpVars.messNoData + '</div>';
        return;
      }
      div.innerHTML = result;
      if ( ftc != null ) {
        ftc();
      }
    }
    ShowWaitWindow();
    div.style.cursor = 'wait';
    if(postData != null && postData.length > 0)
    {
      CHttpRequest_old.SendXML("POST", url, true, postData);
    }
    else
    {
      CHttpRequest_old.SendXML("GET", url);
    }
  },

  trim: function(s)
  {
    var r, re;
    re = /^[ \r\n]+/g;
    r = s.replace(re, "");
    re = /[ \r\n]+$/g;
    r = r.replace(re, "");
    return r;
  },

  Redirect: function(args, url)
  {
    var e = null, bShift = false;
    if(args.length > 0)
      e = args[0];
    if(!e)
      e = window.event;
    if(e)
      bShift = e.shiftKey;

    if(bShift)
      window.open(url);
    else
    {
      ShowWaitWindow();
      window.location=url;
    }
  },

  False: function(){return false;},

  AlignToPos: function(pos, w, h)
  {
    var x = pos["left"], y = pos["bottom"];

    var body = document.body;
    if((body.clientWidth + body.scrollLeft) - (pos["left"] + w) < 0)
    {
      if(pos["right"] - w >= 0 )
        x = pos["right"] - w;
      else
        x = body.scrollLeft;
    }

    if((body.clientHeight + body.scrollTop) - (pos["bottom"] + h) < 0)
    {
      if(pos["top"] - h >= 0)
        y = pos["top"] - h;
      else
        y = body.scrollTop;
    }

    return {'left':x, 'top':y};
  },

  GetById: function(id)
  {
    itm = null;
    if (document.getElementById) {
      itm = document.getElementById(id);
    }
    else if (document.all)  {
      itm = document.all[id];
    }
    else if (document.layers) {
      itm = document.layers[id];
    }
    return itm;
  },

  Submit: function(obj)
  {
    if(!obj)
      return;
    obj.disabled = true;
    try{ShowWaitWindow()} catch(e){};
    obj.form.submit();
  }

}

/************************************************/

function JCFloatDiv()
{
  var _this = this;
  this.floatDiv = null;
  this.x = this.y = 0;

  this.Show = function(div, left, top, dxShadow)
  {
    var zIndex = parseInt(div.style.zIndex);
    if(zIndex <= 0 || isNaN(zIndex))
      zIndex = 100;
    div.style.zIndex = zIndex;
    div.style.left = left + "px";
    div.style.top = top + "px";

    if(jsIBEUtils.IsIE())
    {
      var frame = document.createElement("IFRAME");
      frame.src = "javascript:void(0)";
      frame.id = div.id+"_frame";
      frame.style.position = 'absolute';
      frame.style.zIndex = zIndex-1;
      frame.style.width = div.offsetWidth + "px";
      frame.style.height = div.offsetHeight + "px";
      frame.style.left = div.style.left;
      frame.style.top = div.style.top;
      frame.frameBorder = 0;
      document.body.appendChild(frame);
    }

    /*shadow*/
    if(isNaN(dxShadow))
      dxShadow = 5;
    if(dxShadow > 0)
    {
      var img;
      if(jsIBEUtils.IsIE())
      {
        img = document.createElement("DIV");
        img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='/bitrix/images/ibe/shadow.png',sizingMethod='scale')";
      }
      else
      {
        img = document.createElement("IMG");
        img.src = '/bitrix/images/ibe/shadow.png';
      }
      img.id = div.id+'_shadow';
      img.style.position = 'absolute';
      img.style.zIndex = zIndex-2;
      img.style.width = div.offsetWidth+'px';
      img.style.height = div.offsetHeight+'px';
      img.style.left = parseInt(div.style.left)+dxShadow+'px';
      img.style.top = parseInt(div.style.top)+dxShadow+'px';
      document.body.appendChild(img);
    }
  }

  this.Close = function(div)
  {
    if(!div)
      return;
    var sh = jsIBEUtils.GetById(div.id+"_shadow");
    if(sh)
      sh.parentNode.removeChild(sh);

    var frame = jsIBEUtils.GetById(div.id+"_frame");
    if(frame)
      frame.parentNode.removeChild(frame);
  }

  this.Move = function(div, x, y, dxShadow)
  {
    if(!div)
      return;

    var left = parseInt(div.style.left)+x;
    var top = parseInt(div.style.top)+y;
    div.style.left = left+'px';
    div.style.top = top+'px';

    this.AdjustShadow(div, dxShadow);
  }

  this.AdjustShadow = function(div, dxShadow)
  {
    var sh = jsIBEUtils.GetById(div.id+"_shadow");
    if(sh)
    {
      if(isNaN(dxShadow))
        dxShadow = 5;

      sh.style.width = div.offsetWidth+'px';
      sh.style.height = div.offsetHeight+'px';
      sh.style.left = parseInt(div.style.left)+dxShadow+'px';
      sh.style.top = parseInt(div.style.top)+dxShadow+'px';
    }

    var frame = jsIBEUtils.GetById(div.id+"_frame");
    if(frame)
    {
      frame.style.width = div.offsetWidth + "px";
      frame.style.height = div.offsetHeight + "px";
      frame.style.left = div.style.left;
      frame.style.top = div.style.top;
    }
  }

  this.StartDrag = function(e, div)
  {
    if(!e)
      e = window.event;
    this.x = e.clientX + document.body.scrollLeft;
    this.y = e.clientY + document.body.scrollTop;
    this.floatDiv = div;

    jsIBEUtils.addEvent(document, "mousemove", _this.MoveDrag);
    document.onmouseup = this.StopDrag;

    var b = document.body;
      b.ondrag = jsIBEUtils.False;
      b.onselectstart = jsIBEUtils.False;
    b.style.MozUserSelect = _this.floatDiv.style.MozUserSelect = 'none';
      b.style.cursor = 'move';
    }

  this.StopDrag = function(e)
  {
    jsIBEUtils.removeEvent(document, "mousemove", _this.MoveDrag);
    document.onmouseup = null;
    this.floatDiv = null;

    var b = document.body;
    b.ondrag = null;
    b.onselectstart = null;
    b.style.MozUserSelect = _this.floatDiv.style.MozUserSelect = '';
      b.style.cursor = '';
  }

  this.MoveDrag = function(e)
  {
    var x = e.clientX + document.body.scrollLeft;
    var y = e.clientY + document.body.scrollTop;
    if(_this.x == x && _this.y == y)
      return;

    _this.Move(_this.floatDiv, (x - _this.x), (y - _this.y));
    _this.x = x;
    _this.y = y;
  }
}
var jsFloatDiv = new JCFloatDiv();

/************************************************/

function WaitOnKeyPress(e)
{
  if(!e) e = window.event
  if(!e) return;
  if(e.keyCode == 27)
    CloseWaitWindow();
}

function ShowWaitWindow()
{
  CloseWaitWindow();

  var div = document.body.appendChild(document.createElement("DIV"));
  div.id = "wait_window_div";
  div.innerHTML = phpVars.messLoading;
  div.className = "waitwindow";
  div.style.left = document.body.scrollLeft + (document.body.clientWidth - div.offsetWidth) - 5 + "px";
  div.style.top = document.body.scrollTop + 5 + "px";

  if(jsIBEUtils.IsIE())
  {
    var frame = document.createElement("IFRAME");
    frame.src = "javascript:void(0)";
    frame.id = "wait_window_frame";
    frame.className = "waitwindow";
    frame.style.width = div.offsetWidth + "px";
    frame.style.height = div.offsetHeight + "px";
    frame.style.left = div.style.left;
    frame.style.top = div.style.top;
    document.body.appendChild(frame);
  }
  jsIBEUtils.addEvent(document, "keypress", WaitOnKeyPress);
}

function CloseWaitWindow()
{
  jsIBEUtils.removeEvent(document, "keypress", WaitOnKeyPress);

  var frame =jsIBEUtils.GetById("wait_window_frame");
  if(frame)
    frame.parentNode.removeChild(frame);

  var div = jsIBEUtils.GetById("wait_window_div");
  if(div)
    div.parentNode.removeChild(div);
}

/************************************************/

function JCHttpRequest_old()
{
  this.Action = null; //function(result){}

  this.httpRequest = null;
  this._span = null;

  this.OnDataReady = function(result)
  {
    if(this.Action)
      this.Action(result);
    this._RemoveScript();
    this.httpRequest = null;
  }

  this._CreateHttpObject = function()
  {
    var obj = null;
    if(window.XMLHttpRequest)
    {
      try {obj = new XMLHttpRequest();} catch(e){}
    }
        else if(window.ActiveXObject)
        {
            try {obj = new ActiveXObject("Microsoft.XMLHTTP");} catch(e){}
            if(!obj)
              try {obj = new ActiveXObject("Msxml2.XMLHTTP");} catch (e){}
        }
        return obj;
  }

  this._CreateScript = function(href)
  {
    var span = null;
    span = document.body.appendChild(document.createElement("SPAN"));
    span.style.display = 'none';
    span.innerHTML = '...<s'+'cript></' + 'script>';
    this._span = span;
    setTimeout(
      function()
      {
          var s = span.getElementsByTagName("script")[0];
          s.language = "JavaScript";
          if(s.setAttribute)
            s.setAttribute('src', href);
          else
            s.src = href;
      }, 10
    );
  }

  this._RemoveScript = function()
  {
    if(this._span)
    {
      this._span.parentNode.removeChild(this._span);
      this._span = null;
    }
  }

  this.Send = function(url, acync)
  {
    if(acync != true && acync != false)
      acync = true;

    var httpRequest = this._CreateHttpObject();
    if(httpRequest)
    {
      this.httpRequest = httpRequest;
      var _this = this;
      httpRequest.onreadystatechange = function()
      {
        if(httpRequest.readyState == 4 && httpRequest.status == 200)
        {
          try
          {
            var s = httpRequest.responseText;
            var code = '';
            var start = s.indexOf('<script>');
            if(start != -1)
            {
              var end = s.indexOf('</script>', start);
              if(end != -1)
              {
                code = s.substr(start+8, end-start-8);
                s = s.substr(0, start) + s.substr(end+9);
              }
            }
            _this.OnDataReady(s);
            if(code != '')
              eval(code);
          }
          catch (e)
          {
            var w = window.open("about:blank");
            w.document.write(httpRequest.responseText);
            w.document.close();
          }
        }
      }
      httpRequest.open("GET", url, acync);
      return httpRequest.send("");
    }
    else
    {
      this._CreateScript(url);
    }
  }

  this.SendXML = function(reqType, url, acync, postData)
  {
    if(acync != true && acync != false)
      acync = true;

    var httpRequest = this._CreateHttpObject();
    if(httpRequest)
    {
      this.httpRequest = httpRequest;
      var _this = this;
      httpRequest.onreadystatechange = function()
      {
        if(httpRequest.readyState == 4 && httpRequest.status == 200)
        {

          try
          {
            _this.OnDataReady(httpRequest.responseText);
          }
          catch (e){
            alert(e.message);
            _this.OnDataReady(null);
          }
        }
      }
      try{
        if(reqType.toLowerCase() == "post")
        {
          httpRequest.open("POST", url, acync);
          httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");
          return httpRequest.send(postData);
        }
        else
        {
          httpRequest.open("GET", url, acync);
          return httpRequest.send(null);
        }
      }
      catch (e)
      {
        alert(
        "The application cannot contact "+
        "the server at the moment. "+
        "Please try again in a few seconds.\n"+
        "Error detail: "+e.message);
      }
    }
  }
}
var CHttpRequest_old = new JCHttpRequest_old();