/************************************************************/
/* viterbo_site.js - Viterbo Main Site JavaScript Library   */
/*   requires: cc_lib.js, alms.lib.js                       */
/************************************************************/


//GLOBAL: constants (as properties of window)
var MIN_MCONTENT_OFFSET = 270;    //default x-offset for placement of the main content pane
var MIN_IPANE_OFFSET = 161;       //default y-offset for placement of the interactive content pane
var MEDIA_MARGIN_X = 57;          //the x-margin of space required around the media window
var MEDIA_MARGIN_Y = 171;         //the y-margin of space required around the media window

var CORRECT = 1; var INCORRECT = 2; //drill question answer values


//var INTERACTIVE_PANE_OFFSET = 30;//default top offset for placement of the interactive content pane
//var ibutt_pane_offset = 28;       //the (negative) vertical offset so ibutt's stick to the top of ipane's
//var ibutt_offset = 0;             //calculates the offset so ibutt's can squish together
//var BOTTOM_MARGIN = 20;           //the size of the margin at the bottom of each page
//var CONTENT_MARGIN_HEIGHT = 4;    //the height of the margins of the content areas


//GLOBAL: variables
//  Master guides for dynamic resizing
var bodyWidth = 0;                //width of the document body
var bodyHeight = 0;               //height of the document body
var mediaWidth = 0;               //width of the media window
var mediaHeight = 0;              //height of the media window
var xMedian = MIN_MCONTENT_OFFSET;//x-position of the vertical line between left column and main content ( = mediaWidth + constant > minimum left column width )
var yMedian = MIN_IPANE_OFFSET;   //y-position of the horizontal line between media window and interactive panes ( = mediaHeight + constant > minimum ipane y-offset )

//  Reserved pointers to each dynamically positioned element
var mcdiv = null;					//main content outer DIV
var icdiv = null;					//main content inner container DIV or IFRAME
var bcdiv = null;					//breadcrumb DIV
var pndiv = null;					//page_nav DIV
var mpdiv = null;					//media_pane DIV
var modiv = null;         //the media object itself, could be any of the following: img, object,
var drdiv = null;					//drill DIV (not used)
var active_ipane = null;	//currently active switchable ipane DIV
var tldiv = null;         //top-left background image
var tcdiv = null;         //top-center background image
var trdiv = null;         //top-right background image
var mldiv = null;         //mid-left background image
var mbdiv = null;         //mid-center background image
var mrdiv = null;         //mid-right background image
var skdiv = null;         //background sketch image
var xmdiv = null;         //xMedian Guide Line
var ymdiv = null;         //yMedian Guide Line

var emlinp = null;        //The login email input
var codinp = null;        //The activation code input
var fnainp = null;        //The first name input
var mnainp = null;        //The middle name input
var lnainp = null;        //The last name input
var pw1inp = null;        //The password 1 input
var pw2inp = null;        //The password 2 input
var subinp = null;        //The submit button





//FUNCTIONS: absolute_position_left - returns the absolute x coordinate (px) of an element
//           absolute_position_top  - returns the absolute y coordinate (px) of an element
function absolute_position_left(obj)
{
	return (obj.offsetParent == null
					? obj.offsetLeft
					: obj.offsetLeft + absolute_position_left(obj.offsetParent));
}
function absolute_position_top(obj)
{
	return (obj.offsetParent == null
					? obj.offsetTop
					: obj.offsetTop + absolute_position_top(obj.offsetParent));
}




//the following three functions are helper infrastructure to
//craete a XMLHTTPRequest and register a listner callback function
var UNINITIALIZED = 0; var LOADING = 1; var LOADED = 2; var INTERACTIVE = 3; var COMPLETE = 4; //XMLHttpRequest.readyState values
var OK = 200; var BAD_REQUEST = 400; var UNAUTHORIZED = 401; var FORBIDDEN = 403; var NOT_FOUND = 404; var INTERNAL_SERVER_ERROR = 500; //some HTTP return status codes

//FUNCTION: newXMLHttpRequest - returns a valid XMLHttpRequest object, whether using IE or others
function newXMLHttpRequest()
{
	if (window.XMLHttpRequest) //We are compatible with Mozilla
		return new XMLHttpRequest();
	else if (window.ActiveXObject)
	{//We have ActiveX, try a couple known IE instantiation methods
		try
		{
			return new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e1)
		{//first method failed
			try
			{
				return new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e2)
			{// both methods failed
				return false;
			}
		}
 	}
	return false;
}

//FUNCTION: getReadyStateHandler - returns a function that handles the readyState change event
function getReadyStateHandler(req, responseXmlHandler)
{
	//alert("getReadyStateHandler:"+req.readyState);
	return function()
	{
		if(req.readyState == COMPLETE)
     	responseXmlHandler(req);
 	}
}

//FUNCTION: getIndexedReadyStateHandler - returns a function that handles the readyState change event for an indexed xml request
function getIndexedReadyStateHandler(req, reqIndex, responseXmlHandler)
{
	//alert("getIndexedReadyStateHandler:"+req.readyState);//ASYNC_TEST
	return function()
	{
		if(req.readyState == COMPLETE)
     	responseXmlHandler(req, reqIndex);
 	}
}




//FUNCTION: activation_mode - switches the login form to course activation mode
function activation_mode()
{
	var elm = document.getElementById('login_top');
	if(elm) elm.style.height = '5px';

	elm = document.getElementById('login_title');
	if(elm) elm.innerHTML = 'Course Activation';

	elm = document.getElementById('enter_code');
	if(elm) elm.style.display = 'block';

	elm = document.getElementById('enter_name');
	if(elm) elm.style.display = 'block';

	elm = document.getElementById('enter_password');
	if(elm) elm.style.display = 'block';

	elm = document.getElementById('enter_password_verify');
	if(elm) elm.style.display = 'block';

	elm = document.getElementById('activation_prompt');
	if(elm) elm.style.display = 'none';

	elm = document.getElementById('forgotten_prompt');
	if(elm) elm.style.display = 'block';

	elm = document.getElementById('login_bottom');
	if(elm) elm.style.height = '5px';

	document.forms[0].proc.value = 'activate';

	validate_code(document.forms[0].code);
	validate_name(document.forms[0]);
	validate_email(document.forms[0].email,false);
	validate_passwords(document.forms[0],false);

	return false; //cancel href
}




//FUNCTION: forgotten_mode - switches the login form to forgotten password mode
function forgotten_mode()
{
	var elm = document.getElementById('login_top');
	if(elm) elm.style.height = '100px';

	elm = document.getElementById('login_title');
	if(elm) elm.innerHTML = 'Forgotten Password';

	elm = document.getElementById('enter_code');
	if(elm) elm.style.display = 'none';

	elm = document.getElementById('enter_name');
	if(elm) elm.style.display = 'none';

	elm = document.getElementById('enter_password');
	if(elm) elm.style.display = 'none';

	elm = document.getElementById('enter_password_verify');
	if(elm) elm.style.display = 'none';

	elm = document.getElementById('activation_prompt');
	if(elm) elm.style.display = 'block';

	elm = document.getElementById('forgotten_prompt');
	if(elm) elm.style.display = 'none';

	elm = document.getElementById('login_bottom');
	if(elm) elm.style.height = '160px';

	document.forms[0].proc.value = 'forgot';

	return false; //cancel href
}




//FUNCTION: login_mode - switches the login form to class login mode
function login_mode()
{
	document.forms[0].proc.value = 'login';

	var elm = document.getElementById('login_top');
	if(elm) elm.style.height = '30px';

	elm = document.getElementById('login_title');
	if(elm) elm.innerHTML = 'Class Login';

	elm = document.getElementById('enter_code');
	if(elm) elm.style.display = 'none';

	elm = document.getElementById('enter_name');
	if(elm) elm.style.display = 'none';

	elm = document.getElementById('enter_password');
	if(elm) elm.style.display = 'block';

	elm = document.getElementById('enter_password_verify');
	if(elm) elm.style.display = 'none';

	elm = document.getElementById('activation_prompt');
	if(elm) elm.style.display = 'block';

	elm = document.getElementById('forgotten_prompt');
	if(elm) elm.style.display = 'block';

	elm = document.getElementById('login_bottom');
	if(elm) elm.style.height = '30px';

	return false; //cancel href
}




//FUNCTION: restricted_mode - toggles whether the activation form will prevent modification of name and 2nd password.
//                          - true: name and 2nd password are disabled
function restricted_mode(mode)
{

}




//FUNCTION: validate_submit - validates the form before submitting, chooses which validation function to use
function validate_submit(formElm)
{
	//turn on the wait icon
	var elm = document.getElementById('wait_icon_holder');
	if(elm) elm.style.display = 'block';
	//determine which validate function to use
	//alert("validate_submit:"+document.forms[0].proc.value);
	if(document.forms[0].proc.value == 'login')
		return validate_login(formElm);
	else if(document.forms[0].proc.value == 'activate')
		return validate_activate(formElm);
	else
		return validate_forgot(formElm);
}




//FUNCTION: validate_login - validates the login forms and logs into the course
function validate_login(formElm)
{//alert("validate_login('"+formElm.email.value+"','"+formElm.password.value+"')");

	if(formElm.email.value && formElm.password.value)
	{//the email and password input boxes have values

  	//Initiate an Ajax request to validate the login
		//setup the XMLHttpRequest obj
		var req = newXMLHttpRequest();

		//register the callback handler function
		var callbackHandler = getReadyStateHandler(req, validate_login_handler);
		req.onreadystatechange = callbackHandler;

		//alert("sending request");
		//initiate the request
		req.open("POST", '/svc/valid_login.svc.php', true);
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.send("e="+escape(formElm.email.value)+"&p="+escape(formElm.password.value));
		return false;//we can't submit the form just yet, need to wait for Ajax return
	}
	else
	{
		//turn off the wait icon
		var elm = document.getElementById('wait_icon_holder');
		if(elm) elm.style.display = 'none';
		alert('Please enter your e-mail address and password to log in.');
		return false;
	}
}
//CALLBACK: validate_login_handler - handles Ajax return for validating logins
function validate_login_handler(req)
{//alert("validate_login_handler: "+req+"\nstatus change: "+req.status);

	if(req.status == OK && req.responseText)
	{//The XMLHttpRequest returned OK
		if(!req.responseXML)
		{//the response returned text, but not XML, it's probably an error
			alert("Error:\n"+req.responseText);
		}
		else
		{//the response is in XML format (not an error message)
			//alert("Success\n"+req.responseText);
			var authorized = req.responseXML.getElementsByTagName('Error');
			if(authorized.length)
			{//an error was returned
				alert('An error occurred while attempting to validate your login.  Please try again later.');
			}
			authorized = req.responseXML.getElementsByTagName('Login');
			if(authorized.length)
			{//a login status record was returned
				var user_id = authorized[0].getAttribute('user_id');
				authorized = authorized[0].getAttribute('authorized');
				if(authorized.toUpperCase() == 'TRUE')
				{//User is authorized
					//alert('submit form');
					document.forms[0].submit();
				}
				else
				{//User is not authorized
					var elm = document.getElementById('wait_icon_holder');
					if(elm) elm.style.display = 'none';
					alert('Your e-mail and password are not correct.');
				}
			}
		}
	}
}




//FUNCTION: validate_code - validates the course activiation code
function validate_code(codeElm)
{//alert('validate_code, length='+codeElm.value.replace(/[- ]/g,'').length);

	if(codeElm.value.replace(/[- ]/g,'').length == 16)
	{//All codes must be 16 chars

		//turn on the wait icon
		var elm = document.getElementById('wait_icon_holder');
		if(elm) elm.style.display = 'block';

  	//Initiate an Ajax request to validate the login
		//setup the XMLHttpRequest obj
		var req = newXMLHttpRequest();

		//register the callback handler function
		var callbackHandler = getReadyStateHandler(req, validate_code_handler);
		req.onreadystatechange = callbackHandler;

		//alert("sending request");
		//initiate the request
		req.open("POST", '/svc/valid_activation_code.svc.php', true);
		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.send("e="+escape(codeElm.form.email.value)+"&c="+escape(codeElm.value));
	}
	else
	{//code is not 16 chars, turn off the status_icon
		var elm = document.getElementById('code_status');
		if(elm) elm.src = '/common/viterbo/blank.gif';
	}
	//return false;
}
//CALLBACK: validate_code_handler - handles Ajax return for validating codes
function validate_code_handler(req)
{//alert("validate_code_handler: "+req+"\nstatus change: "+req.status);

	if(req.status == OK && req.responseText)
	{//The XMLHttpRequest returned OK
		if(!req.responseXML)
		{//the response returned text, but not XML, it's probably an error
			alert("Error:\n"+req.responseText);
		}
		else
		{//the response is in XML format (not an error message)
			//alert("Success\n"+req.responseText);

			//turn off the wait icon
			var elm = document.getElementById('wait_icon_holder');
			if(elm) elm.style.display = 'none';

			var validCode = req.responseXML.getElementsByTagName('ValidCode');
			if(validCode.length)
			{//a valid code status record was returned
				var validated = validCode[0].getAttribute('validated');
				var activated = validCode[0].getAttribute('activated');
				var issued = validCode[0].getAttribute('issued');
				var user_id = validCode[0].getAttribute('user_id');
				var first_name = validCode[0].getAttribute('first_name');
				var last_name = validCode[0].getAttribute('last_name');
				var middle_name = validCode[0].getAttribute('middle_name');
				var email = validCode[0].getAttribute('email');
				var pwd1 = validCode[0].getAttribute('password');
				//alert(validated.toUpperCase()+':'+user_id+':'+email);
				if(validated.toUpperCase() == 'TRUE' && user_id && activated && pwd1)
				{//this is a valid code that is already being used
					var elm = document.getElementById('code_status');
					if(elm) elm.src = '/common/viterbo/checkmark.gif';
					//populate the form with values that may have already been set during issue of code
					if(first_name) document.forms[0].first_name.value = first_name;
					if(last_name) document.forms[0].last_name.value = last_name;
					if(middle_name) document.forms[0].middle_name.value = middle_name;
					if(email) document.forms[0].email.value = email;
					validate_name(document.forms[0]);
					validate_email(document.forms[0].email,true);
					setTimeout("alert(\"This code has already been activated.\\n\\nTo log in to the course, enter your\\ne-mail address and your password in\\nthe appropriate boxes, then click\\nthe 'Go to class' button.\\n\\nIf you have forgotten your password, click\\nthe 'Forgotten password' link below.\");",0);
					return true;
				}
				else if(validated.toUpperCase() == 'TRUE')
				{//this is a valid and available activation code
					var elm = document.getElementById('code_status');
					if(elm) elm.src = '/common/viterbo/checkmark.gif';
					//populate the form with values that may have already been set during issue of code
					if(first_name) document.forms[0].first_name.value = first_name;
					if(last_name) document.forms[0].last_name.value = last_name;
					if(middle_name) document.forms[0].middle_name.value = middle_name;
					if(email) document.forms[0].email.value = email;
					validate_name(document.forms[0]);
					validate_email(document.forms[0].email,true);
					return true;
				}
				else
				{//this is NOT a valid and available activation code
					var elm = document.getElementById('code_status');
					if(elm) elm.src = '/common/viterbo/xmark.gif';
					return true;
				}
			}
			alert('The activation code validation service is out of order.  Please try again later.');
			return false;
		}
	}
}




//FUNCTION: validate_activate - validates the course activation form
function validate_activate(formElm)
{//alert("validate_activate()");

	var elm = document.getElementById('wait_icon_holder');
	if(formElm.code.value.replace(/[- ]/g,'').length != 16)
	{//code is too short
		if(elm) elm.style.display = 'none';
		setTimeout("alert(\"Please enter a valid 16-letter course activation code.\");",0);
		return false;//we can't submit the form just yet, need to wait for Ajax return
	}
	if(formElm.first_name.value.length < 1 || formElm.last_name.value.length < 2)
	{//name is too short
		if(elm) elm.style.display = 'none';
		setTimeout("alert(\"Please enter your full name.\");",0);
		return false;//we can't submit the form just yet, need to wait for Ajax return
	}
	if(formElm.email.value.match(/^[A-Z0-9._%-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$/i))
	{//email is correct format
	}
	else
	{//email is NOT correct format
		if(elm) elm.style.display = 'none';
		setTimeout("alert(\"Please enter a valid email address.\");",0);
		return false;//we can't submit the form just yet, need to wait for Ajax return
	}
	if(formElm.password.value.length < 6)
	{//password is too short
		if(elm) elm.style.display = 'none';
		setTimeout("alert(\"Your password must be at least 6\\ncharacters in length.\");",0);
		return false;//we can't submit the form just yet, need to wait for Ajax return
	}
	if(formElm.password_verify.value != formElm.password.value)
	{//passwords don't match
		if(elm) elm.style.display = 'none';
		setTimeout("alert(\"Your passwords do not match.\");",0);
		return false;//we can't submit the form just yet, need to wait for Ajax return
	}
	//var o = '';
	//for(var i=0;i<formElm.elements.length;i++)
	//{
		//o += formElm.elements[i].name+':'+formElm.elements[i].value+"\n";
	//}
	//alert(o);
	return true;
}




//FUNCTION: validate_name - validates the user's name
function validate_name(formElm)
{//alert("validate_name()");
	var elm = document.getElementById('name_status');
	if(elm && formElm.first_name.value.length > 0 && formElm.last_name.value.length > 1)
		elm.src = '/common/viterbo/checkmark.gif';
	else
		elm.src = '/common/viterbo/blank.gif';
}




//FUNCTION: validate_email - validates the user's email address
function validate_email(emailElm,fullCheck)
{//alert("validate_email()");
	if(document.forms[0].proc.value == 'activate')
	{//we're in the activation code form

		var elm = document.getElementById('email_status');
		if(elm && emailElm.value.match(/^[A-Z0-9._%-]+@(?:[A-Z0-9-]+\.)+[A-Z]{2,6}$/i))
		{//email is correct format

			if(fullCheck)
			{//perform check on full email address
				//turn on the wait icon
				var elm = document.getElementById('wait_icon_holder');
				if(elm) elm.style.display = 'block';

		  	//Initiate an Ajax request to validate the login
				//setup the XMLHttpRequest obj
				var req = newXMLHttpRequest();

				//register the callback handler function
				var callbackHandler = getReadyStateHandler(req, validate_email_handler);
				req.onreadystatechange = callbackHandler;

				//alert("sending request: valid_email.svc");
				//initiate the request
				req.open("POST", '/svc/valid_email.svc.php', true);
				req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				req.send("e="+escape(emailElm.value)+"&c="+escape(emailElm.form.code.value));
			}
			else
			{//partial email address only
				elm.src = '/common/viterbo/checkmark.gif';
			}
		}
		else
		{//email is not formatted correctly
			//alert('invalid email');
			elm.src = (fullCheck ? '/common/viterbo/xmark.gif' : '/common/viterbo/blank.gif');
		}
	}
}
//CALLBACK: validate_email_handler - handles Ajax return for validating email addresses
function validate_email_handler(req)
{//alert("validate_email_handler: "+req+"\nstatus change: "+req.status);

	if(req.status == OK && req.responseText)
	{//The XMLHttpRequest returned OK
		if(!req.responseXML)
		{//the response returned text, but not XML, it's probably an error
			alert("Error:\n"+req.responseText);
		}
		else
		{//the response is in XML format (not an error message)
			//alert("Success\n"+req.responseText);

			//turn off the wait icon
			var elm = document.getElementById('wait_icon_holder');
			if(elm) elm.style.display = 'none';

			var validEmail = req.responseXML.getElementsByTagName('ValidEmail');
			if(validEmail.length)
			{//a valid code status record was returned
				var validated = validEmail[0].getAttribute('validated');
				var user_id = validEmail[0].getAttribute('user_id');
				var email = validEmail[0].getAttribute('email');
				//alert(validated.toUpperCase()+':'+user_id+':'+email);
				var elm = document.getElementById('email_status');
				if(validated.toUpperCase() == 'TRUE')
				{//this is a valid and available email
					if(elm) elm.src = '/common/viterbo/checkmark.gif';
					return true;
				}
				else
				{//this is NOT a valid email
					//alert('INVALID EMAIL');
					if(elm) elm.src = '/common/viterbo/xmark.gif';
					if(user_id)
						setTimeout("alert(\"This email address is already in use by another user.\\n\\nPlease choose another email address.\\n\\nIf you have forgotten your password, click\\nthe 'Forgotten password' link below.\");",0);
					return true;
				}
			}
			alert('The email address validation service is out of order.  Please try again later.');
			return false;
		}
	}
}




//FUNCTION: validate_passwords - validates the user's passwords
function validate_passwords(formElm,fullCheck)
{//alert("validate_passwords()");
	if(document.forms[0].proc.value == 'activate')
	{//we're in the activation code form

		var pwd1 = document.getElementById('pwd1_status');
		var pwd2 = document.getElementById('pwd2_status');
		if(pwd1 && pwd2)
		{
			//check password 1
			if(formElm.password.value.length >= 6)
			{//password 1 is long enough
				pwd1.src = '/common/viterbo/checkmark.gif';
			}
			else if(fullCheck)
			{//password 1 is too short, full check
				pwd1.src = '/common/viterbo/xmark.gif';
				setTimeout("alert(\"Your password must be at least 6\\ncharacters in length.\");",0);
				return;
			}
			else
			{//password 1 is too short, delayed check
				pwd1.src = '/common/viterbo/blank.gif';
			}

			//check password 2
			if(formElm.password_verify.value.length >= 6 && formElm.password_verify.value == formElm.password.value)
			{//password 2 matches password 1
				pwd2.src = '/common/viterbo/checkmark.gif';
			}
			else if(fullCheck && formElm.password_verify.value.length > 0)
			{//password 2 doesn't match, full check
				pwd2.src = '/common/viterbo/xmark.gif';
				setTimeout("alert(\"Your passwords do not match.\");",0);
			}
			else if(formElm.password_verify.value.length >= formElm.password.value.length && formElm.password_verify.value != formElm.password.value)
			{//password 2 doesn't match, delayed auto-length check
				pwd2.src = '/common/viterbo/xmark.gif';
			}
			else
			{//password 2 doesn't match, delayed check
				pwd2.src = '/common/viterbo/blank.gif';
			}
		}
	}
}




//FUNCTION: init_welcome_page - Display welcome screen user elements
function init_welcome_page()
{
	//send an XMLHttpRequest for the user's session info
	if(get_data_from_auth_cookie() && user_id)
	{
		//alert(user_id+':'+session_id);
		postAjaxRequest('/svc/user_session_info.svc.php','user_id='+escape(user_id)+'&context=home',showUserCourseList);
		home_dir = 'home';
	}
	else
		showLiveHelpButton(document.getElementById('live_help_container'));
}

//HANDLER: showUserCourseList() - populates and displays the user course list
function showUserCourseList(req)
{//alert("HANDLER: showUserCourseList\n  status change: "+req.status);
	if(req.status == OK && req.responseText)
	{//The XMLHttpRequest returned OK
		if(!req.responseXML)
		{//the response returned text, but not XML, it's probably an error
			alert("Error:\n"+req.responseText);
		}
		else
		{//the response is in XML format (not an error message)
			//alert("Success\n"+req.responseText);
			var elmArr = null;

			//check for user info errors
			elmArr = req.responseXML.getElementsByTagName('Error');
			var i=0;//count the number of errors
			for(i=0;i < elmArr.length;i++)
			{//loop through each error message element
				if(Number(elmArr[i].getAttribute('errorNumber')) >= 3)
				{//errors in the 3+ range are authentication problems, we need to log out
					//TODO: Errors in the 4 range mean the user is OK, but the course is not, once we get more than one course, this part needs to be modified so that it goes to the user home page instead of logging out
					alert(getData(elmArr[i])+"\nLogging out...");
					logoutSession();
					document.location="/logout.php"; //redirect to logout page
				}
			}
			if(i>0) return false; //if there were any errors, stop here

			//get this user's email address from the xml response
			var nArr = req.responseXML.getElementsByTagName('email');
			for(var i=0;i < nArr.length;i++)
				user_email = getData(nArr[i]);

			//get this user's alias from the xml response
			nArr = req.responseXML.getElementsByTagName('alias');
			for(var i=0;i < nArr.length;i++)
				user_alias = getData(nArr[i]);

			//get this user's name from the xml response
			nArr = req.responseXML.getElementsByTagName('users_name');
			for(var i=0;i < nArr.length;i++)
				users_name = getData(nArr[i]);

			//populate this user's name in the header
			var containerElm = document.getElementById('users_name');
			if(containerElm)
				containerElm.innerHTML = users_name;

			//populate the list of courses this user has access to
			containerElm = document.getElementById('access_list');
			if(containerElm)
			{
				removeAllChildren(containerElm);

				var listTitle = 'Course List:';
				var groupArr = req.responseXML.getElementsByTagName('Group');
				for(var i=0;i < groupArr.length;i++)
					if(groupArr[i].getAttribute('group_name') && groupArr[i].getAttribute('group_name').ltrim().substr(0,3) == 'LMS')
						listTitle = 'Access List:';

				var courseLinkList = '';
				for(var i=0;i < groupArr.length;i++)
					courseLinkList += "<div class='course_list_item'>"
						+ "<a href='/doc.php/"+groupArr[i].getAttribute('context')+"/index.html'>"+groupArr[i].getAttribute('group_name')+"</a><br />"
						+ getData(groupArr[i])
						+ "</div>\n";

				containerElm.innerHTML = "<h3>"+listTitle+"</h3>\n"
					+courseLinkList;
			}


			//Display the live help button (now that we have retrieved user data)
			showLiveHelpButton(document.getElementById('live_help_container'));
		}
	}
}




//FUNCTION: check_logout_action - checks to see if we're at the logout screen explicitly, or because of a redirect from another URL
function check_logout_action()
{// action="/doc.php/home/index.html"
	var elm = null;
	if(document.location.pathname.indexOf("/doc.php/") == -1)
	{//this is not a doc.php URL, set the form action to the default login (user home) page
		//alert('check_logout_action(1)');
		document.forms[0].action = USER_HOME_PAGE;
		if(elm = document.getElementById('logout_title'))
			elm.innerHTML = 'Logged Out';
		if(elm = document.getElementById('logout_msg'))
			elm.innerHTML = 'You are logged out.  You may log back in again if you wish to return to class.';
	}
	else
	{//this URL is being redirected through doc.php, leave the form action blank so that it will re-post to itself
		//alert('check_logout_action(2)');
		if(elm = document.getElementById('logout_title'))
			elm.innerHTML = 'Login Required';
		if(elm = document.getElementById('logout_msg'))
			elm.innerHTML = 'You are logged out.  As a security measure, online sessions are closed after 30 minutes of inactivity.  You may log in again to return to class.';
	}
}




//FUNCTION: check_window_size - resets the size of elements when the window size has changed
function check_window_size()
{
	if( (!bodyHeight || !bodyWidth
		|| (bodyHeight != document.body.clientHeight)
		|| (bodyWidth != document.body.clientWidth) )
		&& tldiv && tcdiv && trdiv && mldiv && mbdiv && mrdiv)
	{//global references exist; we have lost our size marker, or the user has changed the height of the window

		//store the dimensions of body after resizing, so we can check if user changes it again
		bodyHeight = document.body.clientHeight;
		bodyWidth = document.body.clientWidth;
		if(mpdiv)
		{//there is a media pane object
			if( (modiv = get_child_node(mpdiv,'IMG'))
			 || (modiv = get_child_node(mpdiv,'OBJECT')) )
			{//found a media object
				//alert('body.clientWidth: '+document.body.clientWidth+'\n'+'body.clientHeight: '+document.body.clientHeight+'\n\nmpdiv.clientHeight: '+ mpdiv.clientHeight+'\nmpdiv.clientWidth: '+ mpdiv.clientWidth+'\nmpdiv.offsetHeight: '+ mpdiv.offsetHeight+'\nmpdiv.offsetWidth: '+ mpdiv.offsetWidth+'\nmpdiv.height: '+ mpdiv.height+'\nmpdiv.width: '+ mpdiv.width+'\n'+'\nmodiv.clientHeight: '+ modiv.clientHeight+'\nmodiv.clientWidth: '+ modiv.clientWidth+'\nmodiv.offsetHeight: '+ modiv.offsetHeight+'\nmodiv.offsetWidth: '+ modiv.offsetWidth+'\nmodiv.height: '+ modiv.height+'\nmodiv.width: '+ modiv.width+'\n');
				mediaWidth = mpdiv.offsetWidth;
				mediaHeight = mpdiv.offsetHeight;
				xMedian = ((xMedian = mediaWidth + MEDIA_MARGIN_X) < MIN_MCONTENT_OFFSET ? MIN_MCONTENT_OFFSET : xMedian);
				yMedian = ((yMedian = mediaHeight + MEDIA_MARGIN_Y) < MIN_IPANE_OFFSET ? MIN_IPANE_OFFSET : yMedian);
			}
		}
		//alert('bodyWidth='+bodyWidth+'\nbodyHeight='+bodyHeight+'\nmediaWidth='+mediaWidth+'\nmediaHeight='+mediaHeight+'\nxMedian='+xMedian+'\nyMedian='+yMedian);

		//Resize the composite background
		tcdiv.style.width = ((n = bodyWidth - tldiv.homeWidth - trdiv.homeWidth) < 0 ? 0 : n)+'px';
		trdiv.style.left = (tldiv.homeWidth + tcdiv.offsetWidth)+'px';
		mbdiv.style.width = ((n = bodyWidth - mldiv.homeWidth - mrdiv.homeWidth) < 0 ? 0 : n)+'px';
		mrdiv.style.left = (mldiv.homeWidth + mbdiv.offsetWidth)+'px';
		mldiv.style.height = ((n = bodyHeight - tldiv.homeHeight) < 0 ? 0 : n)+'px';
		mbdiv.style.height = ((n = bodyHeight - tcdiv.homeHeight) < 0 ? 0 : n)+'px';
		mrdiv.style.height = ((n = bodyHeight - trdiv.homeHeight) < 0 ? 0 : n)+'px';
		//skdiv.style.left = (bodyWidth - skdiv.homeWidth)+'px';
		//skdiv.style.top = (bodyHeight - skdiv.homeHeight)+'px';

	}
}




//FUNCTION: initialize_animated_objects - sets starting values for all objects to be animated on this page
//  This function needs to be hard-coded using the global pointers for each animated object
function init_page()
{
	//make sure we're on viterbo-online.com and not www.viterbo-online.com (TODO: this is a kluge)
	if(document.domain.indexOf("www.") != -1)
		document.location = (String(document.location).replace(/www./,''));


	ccDebug();//Start debugger tool

	//Set global references to positioned elements
	tldiv = document.getElementById('standard_tl');
	tcdiv = document.getElementById('standard_tc');
	trdiv = document.getElementById('standard_tr');
	mldiv = document.getElementById('standard_ml');
	mbdiv = document.getElementById('standard_mc');
	mrdiv = document.getElementById('standard_mr');
	skdiv = document.getElementById('bg_sketch');
	mcdiv = document.getElementById('container_block_id');
	bcdiv = document.getElementById('breadcrumb_bar_id');
	mpdiv = document.getElementById('media_pane_id');
	xmdiv = document.getElementById('xMedianGuide');
	ymdiv = document.getElementById('yMedianGuide');

	emlinp = document.getElementById('email');;        //The login email input
	codinp = document.getElementById('code');;        //The activation code input
	fnainp = document.getElementById('first_name_id');;        //The first name input
	mnainp = document.getElementById('middle_name_id');;        //The middle name input
	lnainp = document.getElementById('last_name_id');;        //The last name input
	pw1inp = document.getElementById('password');;        //The password 1 input
	pw2inp = document.getElementById('password_verify');;        //The password 2 input
	subinp = document.getElementById('go_to_class');;        //The submit button

	//Verify all references
	if(tldiv && tcdiv && trdiv && mldiv && mbdiv && mrdiv
		//&& emlinp && codinp && fnainp && mnainp && lnainp && pw1inp && pw2inp && subinp
		)
	{//references were all found

		//Find the rest of the form elements
		//emlinp.msg = document.getElementById('email_msg');
		//emlinp.ico = document.getElementById('email_status');

		//codinp.msg = document.getElementById('code_msg');
		//codinp.ico = document.getElementById('code_status');
		//codinp.onkeyup = validate_code

		//lnainp.msg = mnainp.msg = fnainp.msg = document.getElementById('name_msg');
		//lnainp.ico = mnainp.ico = fnainp.ico = document.getElementById('name_status');

		//pw1inp.msg = document.getElementById('pwd1_msg');
		//pw1inp.ico = document.getElementById('pwd1_status');

		//pw2inp.msg = document.getElementById('pwd2_msg');
		//pw2inp.ico = document.getElementById('pwd2_status');

		//Store original sizes
		tldiv.homeWidth = tldiv.offsetWidth; tldiv.homeHeight = tldiv.offsetHeight;
		tcdiv.homeWidth = tcdiv.offsetWidth; tcdiv.homeHeight = tcdiv.offsetHeight;
		trdiv.homeWidth = trdiv.offsetWidth; trdiv.homeHeight = trdiv.offsetHeight;
		mldiv.homeWidth = mldiv.offsetWidth; mldiv.homeHeight = mldiv.offsetHeight;
		mbdiv.homeWidth = mbdiv.offsetWidth; mbdiv.homeHeight = mbdiv.offsetHeight;
		mrdiv.homeWidth = mrdiv.offsetWidth; mrdiv.homeHeight = mrdiv.offsetHeight;

		//(Do this last) Adjust window size and Set up window size checker
		window.check_window_size();
		window.setInterval(check_window_size,2000);

	}
	else
	{//some of the elements are not available yet, re-try
		setTimeout(init_page,50);
	}
}

