
/******************************************************************************

Lib Name:	LIB_JS_rememberMe.js
Author:		Graham South
Created:	30/4/2001

History:	Date		Name	Change
			12/06/01	GS		Change to have pathName and domainName as parameters


This check to make sure that cookies are switched on within the browser.
If not, no saving to cookies will be performed.

The format of the cookie is:-

	username::password-->username::password-->username::password


The default cookie path is set to "/"
The default domain is set to "www.btopenworld.com"

There are 4 published functions:
	
addRememberMePair ( username, password, pathName, domainName )

Description:	Adds a username and password pair to the remember-me cookie. 

Returns:		1 if updated correctly.
				0 otherwise

Note:			If the password is null, it just stores the username
	
deleteRememberMePair ( username, pathName, domainName )

Description:	Removes username & password pair from the remember-me cookie using username as the unique key

Returns:		1 if deleted correctly.
				0 otherwise

deleteAllRememberMePairs ( pathName, domainName )

Description:	Remove all username & password pairs from the remember-me cookie.

Returns:		1 if removed correctly.
				0 otherwise

Note:			This function maybe useful if the cookie becomes corrupted and needs to be reset.

listRememberMePairs()

Description:	Returns a list of username & password pairs currently held in the remember-me cookie

Returns:		2 populated javascript arrays
					usernameList - list of stored usernames
					passwordList - list of stored passwords
				Both arrays are synchronised such that the indexes relate to each other, even if null passwords exist.
				If no username & password pairs exist, then the arrays still exist but are empty.

There are 4 unpublished functions:

	setRMCookie (name, value, hours, path, domain, secure)
		Sets the browser cookie with the params. 
		Returns 1 on success, 0 on failure

	readRMCookie(name)
		Reads the contents of the named cookie and returns the cookie data

	killRMCookie(name, path, domain)
		Removes the named cookie by time expiring it
		Returns 1 on success, 0 on failure

	removeUser(in_username)
		Removes the named user from the cookie


********************************************************************************/

var usernameList;
var passwordList;
var RMcookieName = "pop_users";
var defaultPath = "/";
var defaultDomain = "www.btopenworld.com";

<!-- Begin

function addRememberMePair ( in_username, in_password, pathName, domainName )
{
	var old_cookie = removeUser(in_username);

	if( in_password == '' ) {
		//just store the username
		if(old_cookie!='') {
			var new_cookie = old_cookie+'-->'+in_username+'::'
		} else {
			var new_cookie = in_username+'::'
		}
    } else {
		//store both username password
		if(old_cookie!='') {
			var new_cookie = old_cookie+'-->'+in_username+'::'+in_password
		} else {
			var new_cookie = in_username+'::'+in_password
		}
	}
	ret=setRMCookie(RMcookieName,new_cookie,43200,pathName,domainName,'');
	return ret;
}

function deleteRememberMePair ( in_username, pathName, domainName )
{
	var old_cookie = removeUser(in_username);
	ret=setRMCookie(RMcookieName,old_cookie,79935000,pathName,domainName,'');
	return ret;
}

function deleteAllRememberMePairs (pathName, domainName)
{
	ret=killRMCookie(RMcookieName, pathName, domainName);
	return ret;
}

function listRememberMePairs() {
	
	var the_cookie = readRMCookie(RMcookieName);

	if(the_cookie!='') {
	
		//Now process the cookies we found
		var the_values = unescape(the_cookie);

		var separated_values = the_values.split("-->");
		var property_value = "";
		var username;
		var password;
		usernameList = new Array(separated_values.length);
		passwordList = new Array(separated_values.length);
	
		for (loop = 0; loop < separated_values.length; loop++) {
			property_value = separated_values[loop];
			var broken_info = property_value.split("::");

    		usernameList[loop] = broken_info[0];
    		passwordList[loop] = broken_info[1];
			
		}	
	} else {

		usernameList = new Array(0);
		passwordList = new Array(0);
	}
}

function setRMCookie (name, value, hours, path, domain, secure) {
    ret = 0;	
	if(hours) {
	    if ( (typeof(hours) == 'string') && Date.parse(hours) ) { // already a Date string
		var numHours = hours;
	    } else if (typeof(hours) == 'number') { // calculate Date from number of hours
		var numHours = (new Date((new Date()).getTime() + hours*3600000)).toGMTString();
    	}
	}
	document.cookie = name + '=' + escape(value) + ((numHours)?(';expires=' + numHours):'') + ((path)?';path=' + path:';path=' + defaultPath) + ((domain)?';domain=' + domain:';domain=' + defaultDomain) + ((secure && (secure == true))?'; secure':'');
	if (document.cookie != '') {
		ret = 1;
	}
	return ret;
} // setRMCookie


function readRMCookie(name) {
    if(document.cookie == '') { // there's no cookie, so go no further
		return ''; 
    } else { // there is a cookie
		var firstChar, lastChar;
		var theBigCookie = document.cookie;
		firstChar = theBigCookie.indexOf(name);	// find the start of 'name'
		var NN2Hack = firstChar + name.length;
		if((firstChar != -1) && (theBigCookie.charAt(NN2Hack) == '=')) { // if you found the cookie
	    	firstChar += name.length + 1; // skip 'name' and '='
	    	lastChar = theBigCookie.indexOf(';', firstChar); // Find the end of the value string (i.e. the next ';').
	    	if(lastChar == -1) lastChar = theBigCookie.length;
	    	return unescape(theBigCookie.substring(firstChar, lastChar));
		} else { // If there was no cookie of that name, return false.
	   		return '';
		}
    }	
} // readRMCookie


function killRMCookie(name, path, domain) {
	var theValue = readRMCookie(name); // We need the value to kill the cookie
	if(theValue) {
		document.cookie = name + '=' + theValue + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:';path=' + defaultPath) + ((domain)?';domain=' + domain:';domain=' + defaultDomain); // set an already-expired cookie
		return 1
	} else {
		return 0
	}
} // killRMCookie


function removeUser(in_username) {
	var old_cookie = unescape(readRMCookie(RMcookieName))
	var new_cookie = '';

	//If the cookie is empty don't worry about deleting user
	if(old_cookie=='') return '';
	
	//Cycle through cookie and find the username
	var separated_values = old_cookie.split("-->");
	var property_value = "";
	var first_entry = true;	

	for (loop = 0; loop < separated_values.length; loop++) {
		property_value = separated_values[loop];
		var broken_info = property_value.split("::");
		var username = broken_info[0];
		var password = broken_info[1];

		if(password==null) 
			password='';

		if(in_username!=username) {
			if(first_entry) {
				new_cookie += username+'::'+password;
				first_entry=false;
			} else {
				new_cookie += "-->"+username+'::'+password;
			}
		}//end if
	
	}//end for	


	return(new_cookie);

}

// End -->
