var xmlHttp;


function createXMLHttpRequest() 
{
	if (window.XMLHttpRequest) 
	{
		xmlHttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
}


function setup_articles()
{
	var all_links = document.body.getElementsByTagName("A");
	var atcl_regexp = /ViewArticle.dbml\?[\w=&]*ATCLID=(\d+).*#?/i;

	var results = [];

	for (var i = 0; i < all_links.length; i++)
	{
		var link = all_links[i];
		/* 
			Make sure node does not have an onclick handler or is a residual style element
		*/
		if (link.getAttribute("_moz-rs-heading") == null && !link.getAttribute("onclick"))
		{
			//var o = parse_url(link);
			//pathname = o.pathname;
			//search_string = o.search;
		}
		else
		{
			continue;
		}

		var r = link.href.match(atcl_regexp);
		log(atcl_regexp, link.href);
		if (r && r[1])
		{ // Link refers to a ViewArticle.dbml page
			var atclid = r[1]; 
			if (atclid)
			{
				var func = (function() { 
					var _atclid = atclid;
					return function(){ return article_click(_atclid);}
				});
				link.onclick=func();
				results.push(atclid);
			}
		}
	}
	log(results.length);
	// Create Ajax Request
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange;
	var url_prefix = window.location.protocol + "//" + window.location.hostname;
	var search_ids = '&ARTICLE_LIST='+ results.join(',');
	log(search_ids);
	url = url_prefix + "/check_article.dbml?DB_OEM_ID=14000" + search_ids;
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
}

function article_click (atclid)
{
	log(atclid)
	if (article_lookup && article_lookup[atclid])
	{
		var url = article_lookup[atclid];
		pop_win('_name', url)
		return false;
	}
	return true;
}

var article_lookup;
function handleStateChange()
{
	if (xmlHttp.readyState == 4)
	{
		if (xmlHttp.status == 200)
		{
			article_lookup = eval( '(' + xmlHttp.responseText + ')' );
		}
		if (xmlHttp.status == 404)
		{
		}
	}
}
