// JavaScript Document
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);
			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('bgColor',globHeadBackColor);
		loadingTableData.setAttribute('id','moreover_headlines');
		loadingTableData.appendChild(createTextNode("Calling news feed",globRegText));
		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");
	tableData.setAttribute("bgColor",globHeadBackColor);
	
	if (globFeedNameShow)
	{
		tableData.appendChild(createTextNode(currentSearch.searchName.toUpperCase(),globSearchTitle));
	}
	if (article.length > 0)
	{    
		if (globFeedNameShow)
		{
			//tableData.appendChild(createBreakReturn()); 
			//tableData.appendChild(createBreakReturn()); 
		}
	}
	else 
	{
		tableData.appendChild(createBreakReturn());  
		tableData.appendChild(createAnchor("Please click here if the news feed did not generate first time","javascript: checkLoad();",globHeadlineStyle));
	}
	
	for (var article_counter = 0; article_counter < article.length; article_counter++)
	{
		tableData.appendChild(createAnchor(article[article_counter].headline_text,article[article_counter].url,globHeadlineStyle,"1234"));  
		tableData.appendChild(createBreakReturn());
		tableData.appendChild(createAnchor(article[article_counter].source,article[article_counter].document_url,globSourceStyle,"1234"));
		if (article[article_counter].access_status == "sub" || article[article_counter].access_status == "reg" || article[article_counter].access_status == "prem")
			{
			tableData.appendChild(createAnchor("  " + article[article_counter].access_status, article[article_counter].access_registration,globSourceStyle,"1234"));
			}
		if (globTimeDisplay.toUpperCase() == "YES") tableData.appendChild(createTextNode(" " + article[article_counter].harvest_time,globTimeStyle));   
		tableData.appendChild(createBreakReturn()); 
		if (globExtractDisplay.toUpperCase() == "YES")
			{
			tableData.appendChild(createTextNode(article[article_counter].extract,globTimeStyle));
			tableData.appendChild(createBreakReturn());
			}
		tableData.appendChild(createTextNode("\u00A0",globTimeStyle));   
		tableData.appendChild(createBreakReturn());
	}
	tableData.appendChild(createBreakReturn()); 
	if (currentSearch.fromResult > 0)
	{ 
		var prevLink = "javascript:previous()";
		tableData.appendChild(createAnchor("prev",prevLink,globHeadlineStyle));
		tableData.appendChild(createTextNode(" "));
	}
	if (article.length > 0)
	{
		tableData.appendChild(createTextNode((currentSearch.fromResult +1)+ " - " + (currentSearch.fromResult + article.length),globSourceStyle));
	}

	if (globNumberOfResults == article.length)
	{
		var nextLink = "javascript:next()";
		tableData.appendChild(createTextNode(" "));
		tableData.appendChild(createAnchor("next",nextLink,globHeadlineStyle));
	}    
	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.fromResult = arguments[2];
}

function detectBrowser()
{
	if (navigator.appVersion.indexOf("MSIE 5") != -1 || navigator.appVersion.indexOf("MSIE 4") != -1 )
	{
		browser = 4;
	}
	else if (navigator.appVersion.indexOf("MSIE ") != -1)
	{
		browser = 6; 
	}
	else 
	{
		browser = 1;
	}
	return browser;
}

function initialise()
{ 
	if (browser == 1)
	{
		classNameParam = "class";
	}
	loadFeed(globFeedName,globFeedQuery,0);
} 

function loadFeed(searchName,searchTerm,fromResult)
{ 
	article = 0;
	currentSearch = new CurrentSearch(searchName,searchTerm,fromResult);

	var outFormat = (globExtractDisplay.toUpperCase() == "YES") ? "js001" : "js";
	var feed= globPageServer + searchTerm + "&o=" + outFormat + "&fr=" + fromResult+ "&client_id=" + globClientId + "&n=" + globNumberOfResults;  
	script = document.createElement('script');
	if(/mac/i.test(navigator.platform)&&/msie/i.test(navigator.userAgent))
	{

		obj = document.createElement('div');
		obj.innerHTML ="<s" + "cript type='text/javascript' src='"+feed+"'></s" + "cript>";

		document.body.appendChild(obj);
	}

	else
	{
		script.setAttribute('src',feed);
		script.setAttribute('language','Javascript');
		script.setAttribute('type','text/javascript');
		document.getElementsByTagName('head').item(0).appendChild(script);
	}

	checkLoad();
}

function next()
{
	loadFeed(currentSearch.searchName,currentSearch.searchTerm,currentSearch.fromResult + globNumberOfResults);
}

function previous()
{
	loadFeed(currentSearch.searchName,currentSearch.searchTerm,currentSearch.fromResult - globNumberOfResults);
}

