/**
 * @author John Schneider
 * @copyright 2008 Reservoir Creative Group, LLC
 */

var currentCandidacyID;

 /**
  * Shows the candidates for a particular election.
  * @param {Object} id The ID of the candidacy type.
  */
 function ShowCandidacy(id) {
 	var jx = new Ajax();
	currentCandidacyID = id;
	CallbackFunction = function(text) {
		document.getElementById('maincontent').innerHTML = text;
		GetPositionName(id);
	}
	jx.Get('listpositions.php?q=show&c=' + id);
 }
 
 /**
 * Submits a request to vote for a certain candidate.
 * @param {Object} candidateID
 */
function Vote(candidateID)
{
	var jax = new Ajax();
	CallbackFunction = function(text) {
		if (text == "Not logged in")
		{
			alert("You must be logged in to submit a vote.");
		}
		else if (text.substr(0, 9) == 'Vote Cast') {
			ShowCandidacy(text.substr(9));
		}
		else {
			alert(text);
		}
	}
	jax.Post('addvote.php?q=vote', 'candid=' + candidateID);
}

//gets the name of a position by ID.
function GetPositionName(c) {
	var jx = new Ajax();
	CallbackFunction = function(text) {
		document.getElementById('contentheadertext').innerHTML = text;
		GetPositionVsText(c);
	}
	jx.Get('listpositions.php?q=showpname&c=' + c);
}

//Gets the versus text for a candidacy.
function GetPositionVsText(c) {
	var jx = new Ajax();
	CallbackFunction = function(text) {
		document.getElementById('contentheadersubtext').innerHTML = text;
	}
	jx.Get('listpositions.php?q=showpsname&c=' + c);
}

/**
 * Sets the content of the content div to the default.
 */
function PhoneHome() {
	var jx = new Ajax();
	CallbackFunction = function(text) {
		document.getElementById('contentheadertext').innerHTML = 'STAT Officer Election Online Voting';
		document.getElementById('contentheadersubtext').innerHTML = '';
		//document.getElementById('maincontent').innerHTML = text;
	}
	jx.Get('defaultindexcontent.html');
}

/**
 * Sets the login view so that the account signup view is displayed.
 */
function SetCreateUserView() {
	document.getElementById('notloggedindefault').style.visibility = "hidden";
	document.getElementById('notloggedindefault').style.height = "0px";
}

/**
 * Resets the form to the default view, preserving any entered data.
 */
function Reset() {
	document.getElementById('notloggedindefault').style.visibility = "visible";
	document.getElementById('notloggedindefault').style.height = "auto";
	document.getElementById('createuser').style.visibility = "visible";
	document.getElementById('createuser').style.height = "auto";
}

/**
 * Logs in a user.
 */
function Login()
{
	var jx = new Ajax();
	CallbackFunction = Direct;
	jx.Post('login.php', 'uname=' + document.getElementById('loginUname').value + '&passw=' + document.getElementById('loginPassw').value);
}

/**
 * Logs a user out.
 */
function Logout()
{
	var jx = new Ajax();
	CallbackFunction = function(text) {
		window.location.href = "";
	}
	jx.Get('login.php?q=logout');
}

/**
 * Checks to see if a user is logged in and goes straight to the logged in view if so.
 */
function Check()
{
	var jx = new Ajax();
	CallbackFunction = Direct;
	jx.Get('login.php?q=check');
}

/**
 * Directs a user to the logged in view if he is logged in.
 * @param {Object} loggedIn
 */
function Direct(loggedIn)
{
	if (loggedIn)
	{
		document.getElementById('login_loggedIn').style.visibility = "visible";
		document.getElementById('login_loggedIn').style.height = "auto";
		document.getElementById('login_loggedIn').innerHTML = "Welcome, " + loggedIn + "<br /><a href='javascript:void(0);' onclick='javascript:Logout();'>Logout</a>";
		document.getElementById('login_notLoggedIn').style.visibility = "hidden";
		document.getElementById('login_notLoggedIn').style.height = "0px";
		document.getElementById('notloggedindefault').style.visibility = "hidden";
		document.getElementById('notloggedindefault').style.height = "0px";
		document.getElementById('createuser').style.visibility = "hidden";
		document.getElementById('createuser').style.height = "0px";
		document.getElementById('maincontent').innerHTML = '<h4>To view candidates, click on their candidacy positions on the lefthand menu.  You\'re logged in, so you can vote for candidates while viewing them.</h4>';
		if (currentCandidacyID) {
			ShowCandidacy(currentCandidacyID);
		}
	}
	else
	{
		document.getElementById('login_loggedIn').innerHTML = '';
		document.getElementById('login_loggedIn').style.visibility = "hidden";
		document.getElementById('login_loggedIn').style.height = "0px";
		document.getElementById('login_notLoggedIn').style.visibility = "visible";
		document.getElementById('login_notLoggedIn').style.height = "auto";
		document.getElementById('loginUname').value = "";
		document.getElementById('loginPassw').value = "";
		document.getElementById('notloggedindefault').style.visibility = "visible";
		document.getElementById('notloggedindefault').style.height = "auto";
		document.getElementById('createuser').style.visibility = "visible";
		document.getElementById('createuser').style.height = "auto";
		PhoneHome();
	}
}

/**
 * Activates a user account.
 */
function ActivateUserAccount() {
	var email = document.getElementById('emailaddr').value;
	if (email.length == 0) {
		alert('You must specify an email address.');
		return;
	}
	var pass1 = document.getElementById('password1').value;
	var pass2 = document.getElementById('password2').value;
	if (pass1.length == 0 || pass2.length == 0) {
		alert('You must specify a password.');
		return;
	}
	var jx = new Ajax();
	CallbackFunction = function(text) {
		document.getElementById('createuser').innerHTML = text;
	}
	jx.Post("activateuseraccount.php", "email=" + email + "&pass1=" + pass1 + "&pass2=" + pass2);
}
