

String.prototype.trim = function() {
	return this.replace(/^\s+|\n+$/g,"");
}
var g_d = window.document;


// supposed fix for css dropdown menus for IE 6 and under (it's unlikely the
// navbar will even show; inline lists are likely unrenderable)
onnavhover = function() {
	var navelms = document.getElementById("nav").getElementsByTagName("LI");
	for (var i = 0; i < navelms.length; ++i)
	{
		navelms[i].onmouseover = function() {
			this.className += " hover";
		}
		navelms[i].onmouseout = function() {
			this.className = this.className.replace(new RegExp(" hover\\b"), "");
		}
	}
}
if (window.attachEvent) { window.attachEvent("onload", onnavhover); }


// ---------------------------- AJAX LOGIN ----------------------------------
var g_loginurl = "http://shinyshell.net/login";
var g_loginboxid = "loginbox";

var g_usernameid = "usernamefield";
var g_passwordid = "passwordfield";
var g_submitid = "login_submit";

// ---------------------- AJAX ARTICLE COMMENTS -----------------------------
var g_articleurl = "http://shinyshell.net/article";

// --------------------------------------------------------------------------
var g_xmlhttp = null;
var g_submitbutton = null;

/**
 * Initiates the AJAX login process
 */
function ajax_login()
{
	var username = g_d.getElementById(g_usernameid).value;
	var password = g_d.getElementById(g_passwordid).value;
	g_submitbutton = g_d.getElementById(g_submitid);
	
	if (username == "" && password == "")
	{
		alert('You are required to enter both a username and password in order to log in.');
		return false;
	}
	
	// edit the submit button
	g_submitbutton.disabled = true;
	g_submitbutton.value = 'Logging in...';
	
	createajaxobject();
	
	g_xmlhttp.onreadystatechange = login_processrequestchange;
	g_xmlhttp.open("post", g_loginurl, true);
	g_xmlhttp.setRequestHeader( 
		'Content-Type', 
		'application/x-www-form-urlencoded; charset=UTF-8' 
	);
	g_xmlhttp.send("mode=ajax&username=" + username + "&password=" + password);
}


/**
 * Creates the AJAX request object used for the behind-the-scenes
 * communcation with the server.
 *
 * @return object - The request object
 */
function createajaxobject()
{
	try
	{
		g_xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try
		{
			g_xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");	
		}
		catch (e) {}
	}
	if (g_xmlhttp == null)
	{
		g_xmlhttp = new XMLHttpRequest();
	}
}

/**
 * Callback function for the AJAX login request object, handles the
 * various request states, and passes the received data on for further processing.
 */
function login_processrequestchange()
{
	if (g_xmlhttp.readyState == 4)
	{
		if (g_xmlhttp.status == 200)
		{
			var rt = g_xmlhttp.responseText;
			if (rt.replace("\n", "") == "fail")
			{
				g_submitbutton.disabled = false;
				g_submitbutton.value = 'Try again'
				alert("The login failed. Please use a valid username and password.");
			}
			else
			{
				login_updates('<fieldset><em>Thanks for logging in!</em></fieldset><br />' + rt);
			}
		}
		else
		{
			alert("Problem retrieving server data. Please inform Electron.");
		}
	}
}



/**
 * Replaces the loginbox content with a new version
 *
 * @param string - The new div content.
 */
function login_updates(content)
{
	g_d.getElementById(g_loginboxid).innerHTML = content;
}



// -------------------------------------------------------------------------------------------------------------------------
/**
 * Function to initiate AJAX commenting on the articles page.
 * 
 * @param int - ID number of the article to which the comment should be posted.
 * @return bool - Returns false to stop the form from submitting.
 */
function ajax_submit_article_comment(articleid)
{
	var textarea = document.getElementById('articlecomment');
	var imgstatusbox = document.getElementById('articleimgstatusbox');
	var txtstatusbox = document.getElementById('articletxtstatusbox');
	var csubmit = document.getElementById('articlecommentsubmit');

	csubmit.disabled = true;
	csubmit.style.cursor = 'wait';
	textarea.readOnly = true;
	textarea.style.cursor = 'default';
	imgstatusbox.innerHTML = '<img src="http://shinyshell.net/library/img/ajax-loader.gif" alt="..." />';
	txtstatusbox.innerHTML = '<br /><br />Please wait...';
	
	createajaxobject();
	
	thisurl = g_articleurl + "?id=" + articleid;
	
	g_xmlhttp.onreadystatechange = article_processrequestchange;
	g_xmlhttp.open("post", thisurl, true);
	g_xmlhttp.setRequestHeader( 
		'Content-Type', 
		'application/x-www-form-urlencoded; charset=UTF-8' 
	);
	g_xmlhttp.send("mode=ajax&comment=" + escape(textarea.value));
	
	return false;
}

function ajax_article_completed()
{
	var textarea = document.getElementById('articlecomment');
	var imgstatusbox = document.getElementById('articleimgstatusbox');
	var txtstatusbox = document.getElementById('articletxtstatusbox');
	var csubmit = document.getElementById('articlecommentsubmit');
	
	csubmit.value = 'Posted';
	
	csubmit.disabled = false;
	csubmit.style.cursor = 'pointer';
	textarea.readOnly = false;
	textarea.value = ''
	
	imgstatusbox.innerHTML = '';
	txtstatusbox.innerHTML = '<br /><br />Completed';
	
}

function ajax_article_fail()
{
	var textarea = document.getElementById('articlecomment');
	var imgstatusbox = document.getElementById('articleimgstatusbox');
	var txtstatusbox = document.getElementById('articletxtstatusbox');
	var csubmit = document.getElementById('articlecommentsubmit');
	
	csubmit.value = 'Failed!';
	
	imgstatusbox.innerHTML = '';
	txtstatusbox.innerHTML = '<br /><br />Posting failed';
	
}

/**
 * Callback function for the AJAX comment request object, handles the
 * various request states, and passes the received data on for further processing.
 */
function article_processrequestchange()
{
	if (g_xmlhttp.readyState == 4)
	{
		if (g_xmlhttp.status == 200)
		{
			var rt = g_xmlhttp.responseText;
			if (rt.replace("\n", "") == "fail")
			{
				ajax_article_fail();
				alert("An error occured while trying to post your comment. You may have insufficient privileges to do this.");
			}
			else
			{
				ajax_article_completed();
			}
		}
		else
		{
			alert("Problem retrieving server data. Please inform Electron.");
		}
	}
}


function spotlight_rate(siteid)
{
	var url = "http://shinyshell.net/library/ratespotlight.py?siteid=" + siteid;
	desktop = window.open(url, "name", "toolbar=no,location=no,status=no,menubar=no,scrollbars=no,width=500,height=200,resizable=no");
}

