function addHandler(object, event, handler, useCapture)
  {
  if(object.addEventListener)
    {
    object.addEventListener(event, handler, useCapture ? useCapture : false);
    }
  else if(object.attachEvent)
    {
    object.attachEvent('on' + event, handler);
    }
  else alert("Add handler is not supported");
  }

function removeHandler(object, event, handler)
  {
  if(object.removeEventListener)
    {
    object.removeEventListener(event, handler, false);
    }
  else if(object.detachEvent)
    {
    object.detachEvent('on' + event, handler);
    }
  else alert("Remove handler is not supported");
  }




function confirm_delete()
  {
  return confirm("Вы подтверждаете удаление?\nОперацию нельзя будет отменить!");
  }


function strip_tags( str ){    // Strip HTML and PHP tags from a string
    // 
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)

    return str.replace(/<\/?[^>]+>/gi, '');
}





function put_ajax_data(url, divid, object)
  {
  var callback = function(textdata)
    {
    var div = document.getElementById(divid);
    
    if(div == undefined)
      {
      if(object == undefined)
        {
        object = document.body;
        }

      var dinadiv = document.createElement("div");
      dinadiv.setAttribute("id", divid);
      object.appendChild(dinadiv);
      div = document.getElementById(divid);
      }
    
    //if( ! div.className.match(/ajax_loaded/) )
    //  {
    //  div.className += " ajax_loaded";
    //  }

    div.innerHTML = textdata;
    }
  
  get_ajax_data(url, callback, 10000);
  
  return false;
  }










function ajax() {
  this.xmlhttp = false; // объект xmlhttp
  this.xmlhttp_multiple = false; // можно ли повторно использовать объект xmlhttp
  this.method = "get";
  //this.url = "/";
  //this.success = function() {};
  this.x_agent = "ajax";
}; // endof ajax


ajax.prototype.request = function(options) { //ajax.prototype.
  
  if( typeof(options) == "undefined" ) return false;
  
  var xmlhttp = this.make_xmlhttp();
  if( !xmlhttp ) return false;
  
  // определяем дефолтные значения
  if( typeof(options.method) == "undefined" ) options.method = "get";
  if( typeof(options.timer) == "undefined" ) options.timer = 10000;
  if( typeof(options.url) == "undefined" ) options.url = "/";

  if( typeof(options.success) == "undefined" ) options.success = function() { alert("Request complete!"); };
  if( typeof(options.error) == "undefined" ) options.error = function() { alert("Request error!"); };
  if( typeof(options.timeout) == "undefined" ) options.error = function() { alert("Timeout!"); };

  options.method = options.method.toUpperCase();

  var ajaxthis = this;
  var timeout = function() {
    xmlhttp.abort();
    //if( ! confirm("Сервер не отвечает...\nПовторить запрос?") ) return false;
    //ajaxthis.call(options);
  };
  
  try {
    var data = null;

    if( typeof(options.data) == "undefined" ) options.method = "GET";
   

    if( options.method == "GET" )
      {
      xmlhttp.open("GET", options.url, true); // +"?"+sVars //sVars = "";
      }
    else if ( options.method == "POST" )
      {
      xmlhttp.open("POST", options.url, true);
      xmlhttp.setRequestHeader("Method", "POST " + options.url + " HTTP/1.1");
      xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      
      data = "";

      for(var para in options.data) { // если свойство унаследовано - continue //if ( ! options.data.hasOwnProperty(para) ) continue;
        if( data != "" ) data += "&";
        data += para + "=" + encodeURIComponent( options.data[para] );
        }
      }

    var request_timeout = setTimeout( function() { xmlhttp.abort(); options.timeout(); }, options.timer);

    xmlhttp.onreadystatechange = function() {
      if( xmlhttp.readyState != 4 ) return;
      
      clearTimeout(request_timeout); // очистить таймаут при наступлении readyState 4

      if(xmlhttp.status == 200 || xmlhttp.status == 0) options.success( xmlhttp.responseText, xmlhttp.statusText, xmlhttp );
      else options.error(xmlhttp, xmlhttp.statusText);
      };

    xmlhttp.setRequestHeader("x-agent", this.x_agent);
    xmlhttp.send( data ); //sVars
    }
  catch(z)
    {
    alert("ajax.call error: see documentation");
    return false;
    }
    
  return true;
};


ajax.prototype.call = ajax.prototype.request;


ajax.prototype.make_xmlhttp = function() { //ajax.prototype
  // используем существующий xmlhttp объект дважды, если это разрешено
  if( this.xmlhttp && this.xmlhttp_multiple ) return this.xmlhttp;

  try { this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { this.xmlhttp = new XMLHttpRequest(); }
  catch (e) { this.xmlhttp = false; }}}

  return this.xmlhttp;
};
