// var ttsCookie = new TTSCookieMgr();
// setTimeout("ttsCookie.setCookie('ltaPrice2Version', 'A');",1000);
// var ltaCookie = ttsCookie.getCookie('ltaPrice2Version');

/**
 * This class is used to edit the tts_ab cookie.
 * The tts_ab cookie can contain as many name/value pairs as needed.
 */
function TTSCookieMgr()
{
	this.defaultRootName = "tts_session";
	this.rootName = this.defaultRootName;
	this.rootValue = "";
	this.delimiter = "~";
	this.nameValueDelimiter = "::";
	this.expires = "";
	this.path = "/";
}

/**
 * This will allow you to utilize or create another cookie with 
 * 		multiple name:value pairs
 * @param {String} name - Name of the actual cookie
 */
TTSCookieMgr.prototype.setRootCookieName = function(name)
{
	if (name == "") this.rootName = this.defaultRootName;
	else this.rootName = name;
	this.rootValue = "";
}

/**
 * This will set the cookie to expire in the amount of days specified
 * @param {String} days - Number of days to expire cookie
 */
TTSCookieMgr.prototype.setExpirationDate = function(days)
{
	if (Number(days) == 0) {
		this.expires = "";
	} else {
		this.expires = new Date();
		this.expires.setDate(this.expires.getDate() + Number(days));
	}
}

/**
 * This method is used to grab the value of the TTSCookie cookie.
 */
TTSCookieMgr.prototype.getRootCookie = function()
{
	if (this.rootValue != "") return this.rootValue;
	
	var ret = document.cookie;
	var tmpRet = ret;
	var pat = new RegExp("\(.*)(" + this.rootName + "=)([^;]*)(.*)");
	if (ret.indexOf(this.rootName + "=") > -1) {
		ret = ret.replace(pat, '$3');
	}

	if (tmpRet == ret) ret = "";
	this.rootValue = ret;
	return this.rootValue;
}

TTSCookieMgr.prototype.getCookiesByPrefix = function(prefix)
{
	var tmpRoot = this.getRootCookie();
	var cookieValue = "";

	if (tmpRoot != "") {
		var cookies = tmpRoot.split(this.delimiter);
		var cookie;
		for (var i=0; i<cookies.length; i++) {
			cookie = cookies[i].split(this.nameValueDelimiter);
			if (cookie[0].indexOf(prefix) == 0) {
				if (cookieValue != "") cookieValue += this.delimiter;
				cookieValue += cookie[0] + this.nameValueDelimiter + cookie[1];
			}
		}
	}
	return cookieValue;
}

/**
 * This method will iterate through all the name/value pairs 
 * 		to get the value of the name provided.
 * @param {String} cookieName - Name of the cookie to get
 */
TTSCookieMgr.prototype.getCookie = function(cookieName)
{
	var tmpRoot = this.getRootCookie();
	var cookieValue = "";

	if (tmpRoot != "") {
		var cookies = tmpRoot.split(this.delimiter);
		var cookie;
		for (var i=0; i<cookies.length; i++) {
			cookie = cookies[i].split(this.nameValueDelimiter);
			if (cookie[0] == cookieName) {
				cookieValue = cookie[1];
				break;
			}
		}
	}
	return cookieValue;
}

/**
 * This method will iterate through all the name/value pairs
 * 		to see if the cookie is already set. If so, this will simply 
 * 		update its value and if not, this will create a new cookie.
 * @param {String} cookieName - Name of the cookie to create
 * @param {String} cookieValue - The value of the cookie to create
 */
TTSCookieMgr.prototype.setCookie = function(cookieName, cookieValue)
{
	var tmpRoot = this.getRootCookie();
	var root = "";
	
	if (tmpRoot != "") {
		var cookies = tmpRoot.split(this.delimiter);
		var cookie;
		var matchFound = false;

		for (var i=0; i<cookies.length; i++) {
			cookie = cookies[i].split(this.nameValueDelimiter);

			if (cookie[0] == cookieName){
				cookie[1] = cookieValue;
				matchFound = true;
			}
			// Reconstruct the cooie's name:value pair
			if (root == "") root = cookie[0] + this.nameValueDelimiter + cookie[1];
			else root += this.delimiter + cookie[0] + this.nameValueDelimiter + cookie[1];
		}
		if (!matchFound) {
			if (root == "") root = cookieName + this.nameValueDelimiter + cookieValue;
			else root += this.delimiter + cookieName + this.nameValueDelimiter + cookieValue;
		}
	} else {
		root = cookieName + this.nameValueDelimiter + cookieValue;
	}
	// Set the root cookie value and create the cookie
	this.rootValue = root;
	this.createCookie();
}

/**
 * This method will iterate through all the name/value pairs 
 * 		to find the cookie to delete. Once found, it will be removed.
 * @param {String} cookieName - Name of the cookie to remove
 */
TTSCookieMgr.prototype.removeCookie = function(cookieName)
{
	var tmpRoot = this.getRootCookie();
	var root = "";
	
	if (tmpRoot != "") {
		var cookies = tmpRoot.split(this.delimiter);
		var cookie;
		
		for (var i=0; i<cookies.length; i++) {
			cookie = cookies[i].split(this.nameValueDelimiter);
			if (cookie[0] != cookieName){
				if (root == "") root = cookie[0] + this.nameValueDelimiter + cookie[1];
				else root += this.delimiter + cookie[0] + this.nameValueDelimiter + cookie[1];
			}
		}
	} else {
		root = tmpRoot;
	}
	// Set the root cookie value and create the cookie
	this.rootValue = root;
	this.createCookie();
}

/**
 * This method will delete the root cookie and all its key value pairs
 */
TTSCookieMgr.prototype.removeRootCookie = function()
{
	var cookie_date = new Date ( );  // current date & time
	cookie_date.setTime ( cookie_date.getTime() - 1 );
    this.expires = cookie_date.toGMTString();
	document.cookie = this.rootName + "=" + this.rootValue + ";expires=" + this.expires + ";path=/";
}

/**
 * This will create the cookie on the client.
 * If an expiration date is specified, it will add the expires prop.
 */
TTSCookieMgr.prototype.createCookie = function()
{
	// If no value exists, expire the cookie
	if (this.rootValue == "") this.setExpirationDate(-1);
	
	if (this.expires != "") {
		document.cookie = this.rootName + "=" + this.rootValue + ";expires=" + this.expires.toGMTString() + ";path=/";
	} else {
		document.cookie = this.rootName + "=" + this.rootValue + ";path=/";
	}
	
	// Reset the expiration date
	if (this.rootValue == "") this.setExpirationDate("");
}
