/*
This script is provided on an "AS IS" freeware basis and ...
The examples and "Syntax: ..." show how to set and get a cookie for UserName
*/

//This function will set a cookie call "UserName" to the value in the ToolBook global variable:
function setUserName(){
	if (TB_JavaScript=="") findTB_JavaScript();
	var userName = getTB_JavaScript("UserName");
	if (userName==""){
		var errNum = 1;
		var errMsg = "No value pass for User Name??";
		getHandleError(errNum, errMsg);
		return;
	}
	setCookie("UserName",userName);
	setTB_JavaScript("ErrNum",0);
}

//This function will get a cookie call "UserName" and put it into the ToolBook global variable TB_JavaScript["UserName"]
function getUserName(){
	if (TB_JavaScript=="") findTB_JavaScript();
	var userName = 	getCookie("UserName");
	setTB_JavaScript("UserName",userName);
	setTB_JavaScript("ErrNum",0);
}

//This function will set a cookie using TB_JavaScript["CookieName"] and TB_JavaScript["CookieValue"]
function setTB_Cookie(){
	if (TB_JavaScript=="") findTB_JavaScript();
	var cookieName = getTB_JavaScript("CookieName");
	if (cookieName==""){
		var errNum = 2;
		var errMsg = "No Cookie Name passed?";
		getHandleError(errNum, errMsg);
		return;
	}
	var cookieValue =  getTB_JavaScript("CookieValue");
	if (cookieValue==""){
		var errNum = 1;
		var errMsg = "No value passed for Cookie Name: " + cookieName;
		getHandleError(errNum, errMsg);
		return;
	}
	setCookie(cookieName, cookieValue);
	setTB_JavaScript("ErrNum",0);
}

//This function will get a cookie using TB_JavaScript["CookieName"] and put it in TB_JavaScript["CookieValue"]
function getTB_Cookie(){
	if (TB_JavaScript=="") findTB_JavaScript();
	var cookieName = getTB_JavaScript("CookieName");
	if (cookieName==""){
		var errNum = 2;
		var errMsg = "No Cookie Name?";
		getHandleError(errNum, errMsg);
		return;
	}
	var cookieValue = getCookie(cookieName);
	setTB_JavaScript("CookieValue",cookieValue);
	setTB_JavaScript("ErrNum",0);
}

//This function will allow you to pass the error back to ToolBook or handle it here
function getHandleError(errNum, errMsg){
	if (TB_JavaScript=="") findTB_JavaScript();
	if (getTB_JavaScript("ReturnError")==true){
		setTB_JavaScript("ErrNum",errNum);
		setTB_JavaScript("ErrMsg",errMsg);
		return;
	}
	errMsg = "The following error occured:\rError: #" + errNum + "  " + errMsg
	alert(errMsg);
}

/*
This function will set the TB_JavaScript array to the new value
Syntax: setTB_JavaScript("POST_URL",POST_URL);
*/
function setTB_JavaScript(ElementName, newValue){
	eval(TB_JavaScript)[ElementName] = newValue;
}

/*
This function will get the value from TB_JavaScript array
Syntax: var POST_URL = getTB_JavaScript("POST_URL");
*/
function getTB_JavaScript(ElementName){
	return eval(TB_JavaScript)[ElementName];
}

/*
This function will find the global array TB_JavaScript[]
In TB we MUST assign an element of this array as follows:
	TB_JavaScript["TB_JavaScript"] = "TB_JavaScript"
*/
var TB_JavaScript = '';
function findTB_JavaScript(){
	var varName = '';
	for(i=100;i<120;i++){
		var varName = '$' + String.fromCharCode(i)
		if(eval(varName)['TB_JavaScript']=='TB_JavaScript'){
			TB_JavaScript=varName;
			return;
		}
	}
}

//------ Cookie scripts:
function getCookie(name){
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen){
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) return '';
	}
	return '';
}

function setCookie(name, value, numDays){
	var exp = new Date();
// If we don't pass numDays then default to 1 day
	if ((1*numDays)!=numDays) numDays=1;
// Cookies expire after numDays days
	exp.setHours (exp.getHours() + Math.floor(numDays*24));  
	document.cookie = name + "=" + escape (value) + "; expires=" + exp.toGMTString();
}

// This function will delete a cookie
// Syntax:   deleteCookie("UserName")
function deleteCookie(name){
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);  
// This cookie is history
	var cval = getCookie (name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

// Used in the function getCookie() above
function getCookieVal(offset){
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
