// track stats on every view
trackStats();

function canBookmark()
{
	if (location.pathname.indexOf("page.cfm") != -1)
	{
		if (document.title == "Related Websites") return false;
		return true;
	}
	if (location.pathname.indexOf("article.cfm") != -1) return true;
	if (location.pathname == "") return true;
	if (location.pathname == "/") return true;
	return false;
}

function sendBookmark(theLink)
{
	var url = encodeURI(theLink.href);
	document.getElementById("url").value = url;
	document.getElementById("bookmarkForm").action = "?request=sendbookmark&app=agr";
	document.getElementById("bookmarkButton").click();
}

function updateBookmarks()
{
	var bookmarks = document.getElementsByName("bookmark");
	var title = document.title;
	title = encodeURIComponent(title);
	for (var index = 0; index < bookmarks.length; index++)
	{
		var href = bookmarks[index].href;
		if (bookmarks[index].href.lastIndexOf("=") == href.length - 1)  bookmarks[index].href += title;
	}
}

function toggleEmail()
{
	var emailTable = document.getElementById("EmailTable");
	if (emailTable.style.display == "none") emailTable.style.display = "";
	else emailTable.style.display = "none";
}

function sendEmail(theLink)
{
	if (document.getElementById("destAddr").value == "") 
	{
		alert("Please specify the destination email address.");
		return;
	}
	if (document.getElementById("srcAddr").value == "") 
	{
		alert("Please specify the sender's email address.");
		return;
	}
	document.getElementById("title").value = document.title;
	document.getElementById("emailForm").action = "?request=sendemail&app=agr";
	//document.getElementById("emailForm").action = "http://localhost:8050/wmst/http?request=sendemail&app=vp";
	document.getElementById("emailButton").click();
}

function canComment()
{
	if (location.pathname.indexOf("page.cfm") != -1)
	{
		if (document.title == "Related Websites") return false;
		return true;
	}
	if (location.pathname.indexOf("article.cfm") != -1) return true;
	if (location.pathname == "") return true;
	if (location.pathname == "/") return true;
	return false;
}

function keyPressed(event)
{
	if (event.ctrlKey == false) return;
	if (event.keyCode != 10 && event.keyCode != 13) return;
	sendComment(event);
}     

function sendComment(event)
{
	var comment = document.getElementById("commentArea").value;
	comment = encodeURI(comment);
	var author = document.getElementById("authorField").value;
	author = encodeURI(author);
	document.getElementById("author").value = author;
	document.getElementById("comment").value = comment;
	document.getElementById("commentForm").action = "?request=sendcomment&app=agr";
	document.getElementById("submitButton").click();
}

function getQuote()
{
	document.getElementById("quoteRefresh").src = "/images/aggregator/ajaxButton.gif";
	
	var url = "?request=getquote&app=agr";
	if (window.XMLHttpRequest) _xmlhttp = new XMLHttpRequest();
	else if (window.ActiveXObject) _xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	else return;
	if (!_xmlhttp) return;
	_xmlhttp.onreadystatechange = handleQuoteResponse;
	// Use POST because no time for finding how to disable IE caching of GET
	_xmlhttp.open("POST", url, true);
	_xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	_xmlhttp.send(null);
}

function handleQuoteResponse()
{
	if(_xmlhttp.readyState != 4) return
	if (_xmlhttp.status != 200) return;
	document.getElementById("famousQuote").innerHTML = _xmlhttp.responseText;
}

function trackStats()
{
	var url = "?request=trackstats&app=agr";
	var xmlhttp;
	if (window.XMLHttpRequest) xmlhttp = new XMLHttpRequest();
	else if (window.ActiveXObject) xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	else return;
	if (!xmlhttp) return;
	//_xmlhttp.onreadystatechange = handleStatsResponse;
	// Use POST because no time for finding how to disable IE caching of GET
	xmlhttp.open("POST", url, true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.send(null);
}


