//
// ***
// * This javascript library contains the javascript functions used in relation
// * to user accounts
// ***
//

/***************** BEGIN - LiteLogon process functions **********************/

// function to switch between tabs on the litelogon display page
// there was a clever function for doing this by switching the src attribute
// on the tab images - IE7 didn't like it.
function showTab(elementId) {
	document.getElementById('litelogon_login').style.display = "none";
	document.getElementById('litelogon_registration').style.display = "none";
	document.getElementById(elementId).style.display = "inline";	
	if (elementId == 'litelogon_login') {
		document.getElementById('litelogon_login_tab').style.display = "none";
		document.getElementById('litelogon_login_tab_current').style.display = "inline";
		document.getElementById('litelogon_registration_tab').style.display = "inline";
		document.getElementById('litelogon_registration_tab_current').style.display = "none";
	} else {
		document.getElementById('litelogon_login_tab').style.display = "inline";
		document.getElementById('litelogon_login_tab_current').style.display = "none";
		document.getElementById('litelogon_registration_tab').style.display = "none";
		document.getElementById('litelogon_registration_tab_current').style.display = "inline";		
	}
}

/***************** END - LiteLogon process functions **********************/


/************ Single Sign On Cookie functions ***********/
var SINGLE_SIGN_ON_COOKIE_NAME = "SSOCOOK";

// function to write a single sign-on client to the single sign-on cookie
function writeToSingleSignOnCookie(clientName) {
	var singleSignOnCookieValue = obtainSingleSignOnCookieValue(SINGLE_SIGN_ON_COOKIE_NAME);
	if (singleSignOnCookieValue != null) {
		if (singleSignOnCookieValue.indexOf(clientName) == -1) {
			singleSignOnCookieValue = singleSignOnCookieValue + "," + clientName;
			createSingleSignOnCookie(SINGLE_SIGN_ON_COOKIE_NAME, singleSignOnCookieValue);	
		} else {
			return false;
		}
	} else {
		createSingleSignOnCookie(SINGLE_SIGN_ON_COOKIE_NAME, clientName);	
	}
}

// function to read the single sign-on cookie
function obtainSingleSignOnCookieValue(cookieName) {
	var searchName = cookieName + "=";
	var cookies = document.cookie.split(';');
	for(var i=0; i<cookies.length; i++) {
		var cookie = cookies[i];
		while (cookie.charAt(0)==' ') {
			cookie = cookie.substring(1,cookie.length);
			if (cookie.indexOf(searchName) == 0) { 
				return cookie.substring(searchName.length,cookie.length);
			}
		}
	}
	return null;
}

// function to delete the single sign-on cookie
function deleteSingleSignOnCookie() {
	document.cookie = escape(SINGLE_SIGN_ON_COOKIE_NAME) + "=" + SINGLE_SIGN_ON_COOKIE_NAME + "; path=/" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

// function to create cookie
function createSingleSignOnCookie(cookieName, cookieValue, expires) {
	document.cookie = escape(cookieName) + "=" + escape(cookieValue) + "; path=/";
}
