
/*---------------------------------------------------------------------------
	Copyright 2004-2005 Thomson Course Technology, a division of
	Thomson Learning, Inc. All rights reserved. 
---------------------------------------------------------------------------*/

/***********************************************************************
SAM_utilities_client.js

Contains utility functions that are executed on the CLIENT side.

***********************************************************************/

/********************************************************************
Trims spaces and &nbsp;s from the right end of the specified string.
*********************************************************************/

function trim(pString) {
	var NBSP = String.fromCharCode(160);
	if (pString != null && pString != "") {
		while (pString.substr(pString.length - 1) == " " ||
			   pString.substr(pString.length - 1) == NBSP) {
			pString = pString.substr(0, pString.length - 1);
		}
	}
	return pString;
}

/************************************************************************
Trims spaces, &nbsp;s, and CRLFs from BOTH ends of the specified string.
*************************************************************************/

function superTrim(pString) {
	var NBSP = String.fromCharCode(160);
	var CRLF = String.fromCharCode(13) + String.fromCharCode(10);
	if (pString != null && pString != "") {
		while (pString.substr(pString.length - 1) == " " ||
			   pString.substr(pString.length - 1) == NBSP ||
			   pString.substr(pString.length - 2) == CRLF) {			   
			pString = pString.substr(0, pString.length - 1);
		}
		while (pString.substr(0, 1) == " " ||
			   pString.substr(0, 1) == NBSP ||
			   pString.substr(0, 2) == CRLF) {
			pString = pString.substr(1, pString.length);
		}
	}			
	return pString;
}

/***********************************************************************
Used to parse apart a string value that is made up of multiple pieces of 
data separated by a delimiter. This function returns the datum from the
specified position. The following values must be provided:

	pString - the string containing multiple pieces of data
	pPosition - the position of the datum to be retrieved
					(2 = the second datum)
	pDelimiter - the delimiter used to separated the pieces of data 
					in pString
************************************************************************/

function getToken(pString, pPosition, pDelimiter) {

	// Strip off all tokens BEFORE the target token.
	for (var i=1; i < pPosition; i++) {
		vDelimiterPos = pString.indexOf(pDelimiter);
		if (vDelimiterPos == -1) return -1;
		pString = pString.substr(vDelimiterPos + 1);
	}
	
	// If there are additional tokens AFTER the target token, 
	// strip them off too.
	if (pString.indexOf(pDelimiter) != -1) {
		pString = pString.slice(0, pString.indexOf(pDelimiter));
	}
	
	return pString;
}

/***********************************************************************
Displays a prompt dialog when a user clicks the Logout selection on the
navigation bar, asking the user when they really want to log out.
************************************************************************/

function confirmLogout() {
	retval = confirm ("Do you really want to log out of SAM Networking Academy?");	
	if (retval == true) {
		parent.document.location = "../logout.sam";
	}		
		
}

/***********************************************************************
Displays a prompt dialog when a user clicks the Cancel button while 
adding or editing data. The dialog warns users that all data will be
lost and asks them to confirm that they really want to proceed.
************************************************************************/

CANCEL_PROMPT = "All new information will be lost. Proceed?";

function confirmCancel(pFuseaction) {

	retval = confirm(CANCEL_PROMPT);	
	if (retval == true) {
		parent.document.forms[0].fuseaction.value = pFuseaction;
		parent.document.forms[0].submit();
		return true;
	}
	else {
		return false;
	}		
		
}	

// --------------------------------------------------------------------------
// Removes an item from a specified position in an array. (The length of the
// adjusted array is reduced by one.)
// --------------------------------------------------------------------------

function removeArrayItem(pArray, pItem) {
	var tempArray = new Array();
	var o = 0;
	for (var i=0; i < pArray.length; i++) {
		if (i != pItem) {
			tempArray[o] = pArray[i];
			o++;
		}
	}
	return tempArray;
}

function doWait() {
	document.body.style.cursor = "wait";
}