function Ajax()
{
  this.requestObj = null;
  this.getRequestObj = function ()
  {
    if (window.XMLHttpRequest)
    {
         return new XMLHttpRequest;
    }
    else if (window.ActiveXObject)
    {
         return new ActiveXObject("Microsoft.XMLHTTP");
    }
    else return null;
  }

  this.getReadyStateHandler = function (req, handler, p)
  {
    return function ()
    {
       handler(req,p);
    }
  }

  this.sendRequest = function (url, params, method, myFunc, p)
  {
    this.requestObj = this.getRequestObj();
    if (!this.requestObj) return false;

    if (!method || !method.length || method.length < 4) method = "POST";

    this.requestObj.onreadystatechange = this.getReadyStateHandler(this.requestObj,myFunc, p);

    this.requestObj.open(method,url,true);
    this.requestObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    this.requestObj.send(params);
  }
}

function ViewBlock(divId, rssLink, itemsLimit, textLimit, dirname, informerTitle , randomize, stripTags){
  this.textLimit      = itemsLimit;
  this.itemsLimit     = textLimit;
  this.randomize      = randomize;
  this.stripTags      = stripTags;
  this.imageSTR       = 'style="padding: 0px 5px 0px 0px;" width="75" border="0" alt="Цифровик: каталог цифровой техники, новости, обзоры: [TITLE]" title="Цифровик: каталог цифровой техники, новости, обзоры: [TITLE]" align="left"';
            //alert(arr[1]);

  this.headerTemplate = '';
  this.itemTemplate   = '';
  this.footerTemplate = '';
  this.itemsText      = '';
  this.divId          = '';
  this.ajax           = new Ajax;
  this.dirname        = 'scripts/';
  this.serverSide     = 'get.php';
  var obj             = this;

  this.getTemplate = function(tempName){
    this.ajax.sendRequest(this.dirname+this.serverSide, 'template='+tempName,'post', this.parseResponse, tempName);
  }


  this.getRss = function(link){
    this.ajax.sendRequest(this.dirname+this.serverSide, 'link='+link, 'post', this.rssOut, 'aha');
  }

  this.parseResponse = function(resp, tempName){
    if (resp.readyState == 4)
    {
      if(tempName == 'header')
      {
        
        obj.headerTemplate = resp.responseText;
      }
      if(tempName == 'footer')
      {
        obj.footerTemplate = resp.responseText;
      }
      if(tempName == 'item'){
        obj.itemTemplate   = resp.responseText;
      }
    }
  }


  this.parseRSS = function(divId, rssLink, itemsLimit, textLimit, dirname, informerTitle, stripTags){
    if(!divId || !rssLink || !dirname) return false;
    this.informerTitle = informerTitle;
    this.divId = divId;
    this.dirname = dirname;
    if(stripTags == false)  this.stripTags = false;
    if(itemsLimit >= 0) this.itemsLimit = itemsLimit;
    if(textLimit >= 0)  this.textLimit  = textLimit;
    this.getTemplate('header');
    this.getTemplate('footer');
    this.getTemplate('item');
    this.getRss(rssLink);
  }


  this.rssOut = function(resp)
  {
    if (resp.readyState == 4)
    {
      
      data = obj.parseXML(resp.responseXML);
      for (var i in data.items)
      {
        var str = new String(obj.itemTemplate);

        str = str.replace(/\[DESCRIPTION\]/g, data.items[i].description);
        str = str.replace(/\[LINK\]/g,        data.items[i].link);
        str = str.replace(/\[TITLE\]/g,       data.items[i].title);
        
        obj.itemsText += str;
      }
      obj.printOutput();
    }

  }

  this.printOutput = function(){
    if(this.headerTemplate != '' && this.itemsText != '' && this.footerTemplate != ''){
      var text = this.headerTemplate + this.itemsText + this.footerTemplate;
      text = text.replace(/\[INFORMERTITLE\]/g, this.informerTitle);
      document.getElementById(this.divId).innerHTML = text;
    }else{
      setTimeout("this.printOutput", 50);
    }
  }

  // Returns a random integer between min and max
  // Using Math.round() will give you a non-uniform distribution!
  this.getRandomInt = function(min, max)
  {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }
  
  this.getItems = function(ret, items, i){
    
    ret.items[i] = new Object();
    for (var j = 0; j < items.item(i).childNodes.length; j++)
    {
      var node = items.item(i).childNodes.item(j);
      if (node.firstChild)
      {
        var text = node.firstChild.nodeValue; 
        var picOld  = false;
        var picNew  = false;
        
        if(text.match(/<img.*?>/) != null)
        {
          var picOld = new String(text.match(/<img.*?>/i));
          //alert(picOld.match(/src=\"(.*?)\"/i))
          //var arr = picOld.match(/src=\"(.*?)\"/i);
          //ret.items[i]['image'] = arr[1];
          //alert(arr[1]);
          var picNew = new String(picOld.replace(/align=[\"|\'|].*?[\"|\'|]/i, ''));
          picNew = new String(picNew.replace(/width=[\"|\'|].*?[\"|\'|]/i,     ''));
          picNew = new String(picNew.replace(/border=[\"|\'].*?[\"|\'|]/i,    ''));
          picNew = new String(picNew.replace(/height=[\"|\'|].*?[\"|\'|]/i,    ''));
          picNew = new String(picNew.replace(/class=[\"|\'|].*?[\"|\'|]/i,     ''));
          picNew = new String(picNew.replace(/id=[\"|\'|].*?[\"|\'|]/i,        ''));
          picNew = new String(picNew.replace(/alt=[\"|\'|].*?[\"|\'|]/i,       ''));
          picNew = new String(picNew.replace(/title=[\"|\'|].*?[\"|\'|]/i,     ''));
          picNew = new String(picNew.replace(/hspace=[\"|\'|].*?[\"|\'|]/i,    ''));
          picNew = new String(picNew.replace(/vspace=[\"|\'|].*?[\"|\'|]/i,    ''));
          picNew = new String(picNew.replace(/style=[\"|\'|].*?[\"|\'|]/i,     ''));
          picNew = new String(picNew.replace(/\/>/i,               '>'));
          picNew = new String(picNew.replace(/>/i, ' '+this.imageSTR+'/>'));
          //alert([picOld,picNew])
          //alert(text.match(/<img.*?>/));
          text = text.replace(picOld,' ');
          //alert(text)
                    
        }
        if(this.stripTags)
        {
          text = text.replace(new RegExp("&lt;", "g"),  "<");
          text = text.replace(new RegExp("&gt;", "g"), ">");
          text = text.replace(new RegExp("<[^>]+>", "g"),   "");
        }

        if(obj.textLimit > 0 && text.length > obj.textLimit)
        {
          text = text.substr(0, obj.textLimit);
          text = text.substr(0, text.lastIndexOf(' '));
        }
        if(picNew && this.stripTags == true) text = picNew+text;
        ret.items[i][node.nodeName] = text;

      }
    }
    return ret;
  }
 this.parseXML = function(doc) {
    var ret = new Object();
    ret.items = new Array();

    try
    {
      if (!doc) {
          return ret;
      }

      ret.title       = doc.getElementsByTagName("title").item(0).firstChild.nodeValue;
      ret.description = doc.getElementsByTagName("description").item(0).firstChild.nodeValue;
      ret.link        = doc.getElementsByTagName("link").item(0).firstChild.nodeValue;

      ret.pubDate     = doc.getElementsByTagName("pubDate").item(0).firstChild.nodeValue;

      var items       = doc.getElementsByTagName("item");
      var LIMIT       = obj.itemsLimit;

      if(items.length < obj.itemsLimit || obj.itemsLimit == 0)  LIMIT = items.length;
      
      if(obj.randomize == true)
      {
        //alert('aha');
        var rands = new Array;
        for (var i = 0; i < LIMIT; i++)
        {
          var num = obj.getRandomInt(0,items.length);
          if(!obj.in_array(num, rands) && typeof(items[num]) != 'undefined'  && items[num] != null)
          {
            if(rands.length < LIMIT)
            {
              rands[rands.length] = num;
            }
          }else{
           --i;
          }
        }
        for (var i = 0; i < rands.length; i++)
        {
          ret = obj.getItems(ret, items, rands[i]);
        }
        
      }else{
        for (var i = 0; i < LIMIT; i++)
        {
          ret = obj.getItems(ret, items, i);
        }
      }

    }
    catch(e)
    {
  
    }
    return ret;
}

  this.in_array = function(needle, hash){
    for(var i = 0; i < hash.length; ++i){
      if(hash[i] == needle) return true;
    }
    return false;
  }
  this.parseRSS(divId, rssLink, itemsLimit, textLimit, dirname, informerTitle, stripTags);

}
