// code borrowed from moreover.com and modified by JWA
// This stuff does the RSS news feeds.

function checkFeedName(name)
{
  var ok = " 1234567890qwertyuiopasdfghjklzxcvbnm_QWERTYUIOPASDFGHJKLZXCVBNM";
 
  for(i=0; i < name.length ;i++)
  {
    if(ok.indexOf(name.charAt(i))<0)
    { 
      return (false);
    }   
  } 
  return true;
}

function checkLoad()
{  
  if (script.readyState == "complete" || script.readyState == "loaded" || article.length > 0) 
  {   
    if (article.length == 0 && retry < globMaxTries)
    {
      retry ++;
      loadFeed(currentSearch.searchName,currentSearch.searchTerm,currentSearch.fromResult,currentSearch.searchType,currentSearch.saved);
      return;
    }
    retry = 0;
    createHeadlines();
  }
  else if (browser == 1 && tries > globMaxTries)
  {
    tries = 0;
    createHeadlines();
  }
  else
  {
    var headlineCell = document.getElementById("moreover_headlines");
    var loadingTableData = document.createElement("TD");
    loadingTableData.setAttribute('id','moreover_headlines');
    loadingTableData.appendChild(createTextNode("Searching for "+ currentSearch.searchName +"...",""));
    headlineCell.parentNode.replaceChild(loadingTableData,headlineCell);
    tries += 1;
    setTimeout("checkLoad()",2000);
  }
}

function createAnchor(tag,link,className)
{
  var newAnchor = document.createElement("a");
  newAnchor.setAttribute("href",link);
  
  if (arguments.length > 2)
  {
    newAnchor.setAttribute(classNameParam,className);
  }
  if (arguments.length == 4)  
  {
    newAnchor.setAttribute("target",arguments[3]);
  }
  newAnchor.appendChild(document.createTextNode(tag));
  return newAnchor;
}

function createBreakReturn()
{
  return document.createElement("BR");
}


function createHeadlines()
{
  var headlineCell = document.getElementById("moreover_headlines");
  var tableData = document.createElement("TD");
  tableData.setAttribute("id","moreover_headlines");
  tableData.setAttribute("valign","top");
  
  if (globFeedNameShow)
  {
    tableData.appendChild(createTextNode(currentSearch.searchName,""));
  }
  if (article.length > 0)
  {    
    if (globFeedNameShow)
    {
      if (currentSearch.searchType != "c")
      {
         tableData.appendChild(createTextNode(" Results "+ (currentSearch.fromResult +1)+ " - " + (currentSearch.fromResult + article.length),"bodytext"));
      }
      tableData.appendChild(createBreakReturn()); 
    }
  }
  else 
  {
    tableData.appendChild(createBreakReturn());  
    tableData.appendChild(createTextNode("No Results Found",""));  
    tableData.appendChild(createBreakReturn());
    tableData.appendChild(createAnchor("Try Searching Again","javascript: doQuery();",""));
  }
  
  for (var article_counter = 0; article_counter < article.length; article_counter++)
  {
    tableData.appendChild(createAnchor(article[article_counter].headline_text,article[article_counter].url,"headline","1234"));
//  tableData.appendChild(createTextNode(article[article_counter].headline_text,"headline"));  
    tableData.appendChild(createBreakReturn());
    tableData.appendChild(createTextNode(article[article_counter].source,"smalltext"));
    tableData.appendChild(createTextNode(" - " + article[article_counter].harvest_time,"smalltext"));   
    tableData.appendChild(createBreakReturn()); 
  }
  headlineCell.parentNode.replaceChild(tableData,headlineCell);
}

function createTextNode(textString)
{
  var spanTag = document.createElement("SPAN");
  if (arguments.length > 1)
  {
    spanTag.setAttribute(classNameParam, arguments[1]);
  }
  spanTag.appendChild(document.createTextNode(textString));  
  return spanTag;  
}

function CurrentSearch()
{
  
  this.searchName = arguments[0];
  this.searchTerm = arguments[1];
  this.searchType = arguments[2];
  this.fromResult = arguments[3];
  this.saved      = false;
  if (arguments.length > 4)
  {
    this.saved = arguments[4];
  }
}


function detectBrowser()
{
  if (navigator.appVersion.indexOf("MSIE 6") != -1)
  {
    browser = 6; 
  }
  else if (navigator.appVersion.indexOf("MSIE 5") != -1 || navigator.appVersion.indexOf("MSIE 4") != -1 )
  {
    browser = 4;
  }
  else
  {
    browser = 1;
  }
  return browser;
}

function doQuery()
{
  var querySearch   = document.getElementById("moreover_query_term").value;
  var typeOfSearch  = "q";
  var searchName    = querySearch;
  var fromResult    = 0;
  
if(querySearch != "" && querySearch != branding)
 {
 loadFeed(searchName,querySearch,fromResult,typeOfSearch,false);
// createFeedList();  -jwa
 }
}

  
function initialise()
{
  if (browser == 1)
  {
    classNameParam = "class";
  }
  loadFeed("Top stories","Top stories",0,"c",true);
} 

function loadFeed(searchName,searchTerm,fromResult,typeOfSearch,saved)
{ 
  article = 0;
  currentSearch = new CurrentSearch(searchName,searchTerm,typeOfSearch,fromResult,saved);
  searchTerm = escape(searchTerm); 
  var query = "query=" + searchTerm;
  if (typeOfSearch == 'c')
  {
    query = "c=" + searchTerm; 
  }
  var feed= 'http://p.moreover.com/page?' + query + "&o=js&d=earliest&n=7";  //was 4 jwa
  script = document.createElement('script');
  script.setAttribute('src',feed);
  script.setAttribute('language','Javascript');
  script.setAttribute('type','text/javascript');
  document.getElementsByTagName('head').item(0).appendChild(script);
  checkLoad();
}

function showFeed(number)
{
var searchTerm = escape(searchList[number].searchTerm); 
var query = "query=" + searchTerm;
if (searchList[number].searchType == 'c') {query = "c=" + searchTerm;}
window.location='http://p.moreover.com/page?o=rss002&' + query + branding;
}


// wiz bit
var branding = "";
if(location.search.indexOf("wiz=")==1 && location.search.indexOf("&")==-1)
 {
  branding = '&' + location.search.substring(1);
 }
// end wiz bit

