/*
utils.js - provides various utility functions

Groups Included:
	Cookie Management (get, set, remove)
	Location URL parsing

*/
/* 
	---------------------------------
		Cookie Management Functions
	---------------------------------
*/


function removeCookie(name){
	cStr = name.toLowerCase()+'='+'removing;expires=Fri, 02-Jan-1970 00:00:00 GMT';
	document.cookie=cStr;
}


function setCookie(name,value,expires){
	cStr = name.toLowerCase()+'='+value;
	if (expires!=null){
		cStr += ';expires=' + expires;
	}
	document.cookie=cStr;
}

function getCookie(name){
	var curCookies = loadCookies();
	var argname = name.toLowerCase();
	var rtnStr = curCookies[argname];
	if (!(rtnStr)){
		rtnStr=null;
	}
	return rtnStr;

}

function loadCookies(){
	var cookies = new Array();
	var cookieStr = document.cookie;
	var splitCookies = cookieStr.split(';');
	
	for (var i=0;i<splitCookies.length;i++){
		var iCookie = splitCookies[i].split('=');
		var argname = ltrim(iCookie[0]);
		var value   = iCookie[1];
		cookies[argname]=value;
	}
	return cookies;
}

/* 
	---------------------------------
		Location URL managment
	---------------------------------
*/
/*
 * getArgs()::Array of argument values;
 *
 * This function parses the comma separated name=value argument
 * pairs from the query string of the URL and stores the name=value
 * pair in an object returned to the calling method
 */
 
function getArgs(){
	var args = new Object();
	var query = location.search.substring(1);
	var pairs = query.split(",");
	for (var i=0;i<pairs.length;i++){
		var pos = pairs[i].indexOf('=');
		if (pos==-1) continue;
		var argname = pairs[i].substring(0,pos);
		var value   = pairs[i].substring(pos+1);
		args[argname] = unescape(value);
		//debug("getArgs::name="+argname+"::value="+value);
	}
	return args;
}


/* ----------------------------------
	Misc String Functions
*/
function ltrim(text){
	// left trim whitespace
	var pos = 0;
	for (i=0;i<text.length;i++){
		if (text.charAt(i) != " ")
			break;
		pos = i+1;
	}
	return text.substring(pos);
}

function rtrim(text){
	// left trim whitespace
	//postDebugMessage("Text in ::" + text + "::len = " + text.length);
	if (text==null || text == '')
		return null;
		
	var len = text.length;
	for (i=text.length;i>=0;i--){
		if (text.charAt(i) != " ")
			break;
		len = len - 1;
	}
	//postDebugMsg("len out = " + len);
	return text.substring(0,len);
}

	
