/******************************************************************************

Lib Name:	LIB_JS_usercredentials.js
Author:		Graham South
Created:	09/05/2001

History:	Date		Name	Change
			12/06/01	GS		Change setAUCCookie to add domain


===================================================================

There is 1 published functions:
	
getActiveUserCredential (parameterName)

Inputs:			parameterName will be one of:
					(U)SERNAME
					(F)RIENDLYNAME
					(POR)TALTYPE
					(PR)ODUCTID
					(C)ONTACTCONTROL
					(I)DACONTROL
					(D)OB
					(POS)TCODE

	Description:	Returns the value of the chosen parameter for the active user.

	Returns:		Parameter value if exists.
					Empty string otherwise.

	Note:			This is the javascript (client side) equivalent to the server-side getUserCredentials API except PASSWORD is not a vaild parameterName.

===================================================================

There are 3 unpublished functions:


unescapeChars (inString)

	Description:	Filters "=" "!" " " chars. Used because only specific escape chars are required to be escaped

	Returns:		Unescaped string


getAUCCookie (cookieName)

	Description:	Gets the contents of the named cookie

	Returns:		Named cookie contents


setAUCCookie (cookiename, contents, pathName, domainName)

	Description:	Sets the contents of the named cookie

	Returns:		None


The format of the cookie data is:-

	username=XXX!password=XXX!whoami=XXX!type=XXX!productid=XXX!contactcontrol=XXX!idacontrol=XXX!dob=XXX!postcode=XXX

********************************************************************************/

var AUCcookieName = "pop_active_user"

<!-- Begin

function getActiveUserCredential ( parameterName )
{
	
	//(U)SERNAME
	//(F)RIENDLYNAME
	//(POR)TALTYPE
	//(PR)ODUCTID
	//(C)ONTACTCONTROL
	//(I)DACONTROL
	//(D)OB
	//(POS)TCODE

	// Note that password isn't a valid parameter although it still exists in the cookie.
	// Can't be de-crypted client side, hence not available

	if ( /^[Uu]/.test(parameterName) )
	{
		parameterIndex=0;
	}
	else if ( /^[Ff]/.test(parameterName) )
	{
		parameterIndex=2;		
	}
	else if ( /^[Pp][Oo][Rr]/.test(parameterName) )
	{
		parameterIndex=3;		
	}
	else if ( /^[Pp][Rr]/.test(parameterName) )
	{
		parameterIndex=4;		
	}
	else if ( /^[Cc]/.test(parameterName) )
	{
		parameterIndex=5;		
	}
	else if ( /^[Ii]/.test(parameterName) )
	{
		parameterIndex=6;		
	}
	else if ( /^[Dd]/.test(parameterName) )
	{
		parameterIndex=7;		
	}
	else if ( /^[Pp][Oo][Ss]/.test(parameterName) )
	{
		parameterIndex=8;		
	}
	else
	{
		return "";
	}

	var cookie_content = getAUCCookie(AUCcookieName);

	//alert ('Cookie content: '+cookie_content);


	if (cookie_content != "nothing" ) { 

		arrayOfParams = (unescapeChars(cookie_content)).split("!");   
		thisParam = arrayOfParams[parameterIndex];
		thisValue = thisParam.split("=");
		return unescapeChars(thisValue[1]);
			
	} else {
		return "";
	}
	
}


function unescapeChars (inString) {
	// replaces any escaped characters with their correct interpretation
	newstr1=inString.replace(/%3D/g, "=");
	newstr2=newstr1.replace(/%21/g, "!");
	newstr3=newstr2.replace(/%20/g, " ");
	return newstr3;
}


function setAUCCookie(cookiename, contents, pathName, domainName) {
	var expDate = new Date();
	expDate.setTime(expDate.getTime() + (25 * 24 * 60 * 60 * 1000 * 365))
	expDate = expDate.toGMTString();
	document.cookie = cookiename + '=' + contents + ";expires=" + expDate + ";path=" + pathName +";domain= " + domainName + ";"
}


function getAUCCookie(cookieName) {
	var allcookies = document.cookie;
	var pos = allcookies.indexOf(cookieName + "=");
	if(pos != -1) {
		var start = pos+ (cookieName.length+1);
		var end = allcookies.indexOf(";", start);
		if(end == -1) end= allcookies.length;
		var value = allcookies.substring(start,end);
		return value;
		}
	else {
		return "nothing";
	}
}

// End -->

