var ajax = null;

function filter(text,count,page)
{

  document.getElementById('loading-indicator').style.display = '';
  if(ajax != null)
  {
    ajax.doAbort();
  }
  ajax = new Ajax();
  ajax.url="inc/ajaxscripts/articles.php";
  if(text != null)
    ajax.params = "f="+text;
    ajax.params += "&c="+count;
    ajax.params += "&p="+page;
  ajax.onSuccess=successHandlerFS;
  ajax.onError=errorHandlerFS;
 // ajax.onAbort=abortHandler;
 doActionFS();
}

function doActionFS()
{
  if(!ajax.aborted)
    ajax.doRequest();
}

function so_clearInnerHTML(obj) {
	while(obj.firstChild) obj.removeChild(obj.firstChild);
}

function so_getText(obj) {
	if(obj.textContent) return obj.textContent;
	if (obj.nodeType == 3) return obj.data;
	var txt = new Array(), i=0;
	while(obj.childNodes[i]) {
		txt[txt.length] = so_getText(obj.childNodes[i]);
		i++;
	}
    return txt.join("");
}

function copyAttributes(source, destination)
{
  var attributes = source.attributes;
  if(attributes != null)
  {
    for (var i = 0; i < attributes.length; i++)
    {
      var attributeName = attributes[i].nodeName;
      destination.setAttribute(attributeName, source.getAttribute(attributeName));
    }
  }
}

function addNodes(source, destination)
{

    for (var i = 0; i < source.childNodes.length; i++)
    {
      var oldnode = source.childNodes[i];
      var oldnodeName = oldnode.nodeName;
      var newnode =  null;
      if(oldnodeName == "#text")
      {
        newnode = document.createTextNode(oldnode.nodeValue);
      }
      else
      {
        newnode = document.createElement(oldnodeName);
	hasScript = true;
	if(oldnode.attributes.getNamedItem('id') != null && oldnode.attributes.getNamedItem('id').nodeValue == "noscript") hasScript = false;
        
        if(oldnodeName == "tr" && hasScript)
        {
          
        addEvent(newnode, "mouseover", function(){addClass(this, "over")});
        addEvent(newnode, "mouseout", function(){removeClass(this, "over")});
        addEvent(newnode, 'click', function(){window.location.href = "?page=articledetail&id=" + this.id});
        }
      }
      copyAttributes(oldnode, newnode);
      destination.appendChild(newnode);
      addNodes(oldnode, newnode);
    }
}

function TableSkin(el, i)
{
  if(i % 2){
          removeClass(el, 'altRow');
  }else{
          addClass(el, 'altRow');
  }
}

function successHandlerFS(txt, xml)
{
  var d = document;
  var mytable = d.getElementById('myTableBody');
  so_clearInnerHTML(mytable);
  var xmlelement = xml.getElementsByTagName('xmlresponse')[0];
  addNodes(xmlelement, mytable);
  for(var i = 0; i<mytable.getElementsByTagName('tr').length; i++ )
  {
    TableSkin(mytable.getElementsByTagName('tr')[i], i);
  }
  
  document.getElementById('loading-indicator').style.display = 'none';
}
  
  
  
function errorHandlerFS(msg)
{
  alert("Error: " + msg);
  document.getElementById('loading-indicator').style.display = 'none';
}


