function addClass(newClass, target)
{
	document.getElementById(target).className += " " + newClass;
}

function trim(str)
{
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

//FUNCTION NAMES ARE ARBITRARY NEED RENAMING
/* this function receives a username and password from a login form.  The username and password are checked using login.php.  If login.php returns "" then login is successful, otherwise it will return a string to display in the loginStatus div.  If the login is successful the page's user elements and content is loaded This function ALWAYS returns false so that the page will not be relaoded(this function is meant to be called from a form submit). CHANGED, form submit returns false. */
function login(username, password)
{
var postString = "username=" + username + "&password=" + password;
sendLogin(postString);
}

function sendLogin(postString)
{
document.getElementById('loginStatus').innerHTML = "<b>Logging in . . .</b>";
xmlhttp=null;
if (window.XMLHttpRequest)
  {// code for Firefox, Opera, IE7, etc.
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null)
  {
  xmlhttp.onreadystatechange = loginReply;
  xmlhttp.open("POST","/login.php",true);
  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlhttp.setRequestHeader("Content-length", postString.length);
  xmlhttp.setRequestHeader("Connection", "close");
  xmlhttp.send(postString);
  }
else
  {
  document.getElementById('loginStatus').innerHTML = "Error retrieving data.";
  }
}

function loginReply()
{
	if (xmlhttp.readyState==4)
	{// 4 = "loaded"
	if (xmlhttp.status==200)
	{// 200 = "OK"
	var response = xmlhttp.responseText;
	if(response == "")//login successful
		window.location.reload();
	else
		document.getElementById('loginStatus').innerHTML = response;
	}
	}
}

//NOT USED, INSTEAD RELOAD PAGE
function setupLoginSuccess()
{
document.getElementById('username').value = "";
document.getElementById('password').value = "";
document.getElementById('login').style.visibility = "hidden";
}


/* This function logs the user out by destroying the session and then, if successful, the helper method(confirmLogout) removes any user-specific elements and content. */
function logout()
{
	destroySession();
}


function destroySession()
{
xmlhttp=null;
if (window.XMLHttpRequest)
  {// code for Firefox, Opera, IE7, etc.
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null)
  {
  xmlhttp.onreadystatechange=confirmLogout;
  xmlhttp.open("GET","/logout.php",true);
  xmlhttp.send(null);
  }
else
  {
  document.getElementById('loginStatus').innerHTML = "Error retrieving data.";
  }
}

function confirmLogout()
{
	if (xmlhttp.readyState==4)
	{// 4 = "loaded"
	if (xmlhttp.status==200)
	{// 200 = "OK"
	if(xmlhttp.responseText == "logged out")//login successful
	{
		removeLogin();
	}
	else
		alert("you did NOT successfully log out, please try again or close your browser.");
	}
	}
}

function removeLogin()
{
document.getElementById('login').style.visibility = "visible";
document.getElementById('loginStatus').innerHTML = "Successfully Logged out";
}

// Page must contain an element with id="runSqlStatus", caller is responsible for hiding any duplicates
function runSql(sql, status)
{
document.getElementById('runSqlStatus').innerHTML = status;
postString = "sql=" + sql;
xmlhttp=null;
if (window.XMLHttpRequest)
  {// code for Firefox, Opera, IE7, etc.
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
if (xmlhttp!=null)
  {
  xmlhttp.onreadystatechange = sqlComplete;
  xmlhttp.open("POST","/sql.php",true);
  xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xmlhttp.setRequestHeader("Content-length", postString.length);
  xmlhttp.setRequestHeader("Connection", "close");
  xmlhttp.send(postString);
  }
}

// This function calls a method runSqlSuccess on success, define as needed
function sqlComplete()
{
	if (xmlhttp.readyState==4)
	{// 4 = "loaded"
	if (xmlhttp.status==200)
	{// 200 = "OK"
	runSqlSuccess(xmlhttp.responseText);
	}
	}
}