/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
	var expiry = new Date();
	expiry.setMonth(expiry.getMonth()+6);		//set the cookie for 6mths
    document.cookie= name + "=" + escape(value) + "; expires=" + expiry.toGMTString() +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

/*
* Stores an array as a comma separated string in the cookie.
* This function checks to make sure the value doesnt already exist in the cookie
* This function basically splits and rebuilds the str each time we wish to add another value
*/
function addUniqueToCookieArray(name, val) {
	if (getCookie(name)) {
		var arrStr	= getCookie(name);		//eg "http://www.google.com,http://www.fred.com,etc"
		arr		= arrStr.split(",");	//eg [0] = "http://www.google.com", [1] = "http://www.fred.com,etc"
		var exists = false;
		
		if (arr.length>0) {
			for (var i=0; i<arr.length; ++i) {
				//check for uniqueness!
				
				if (arr[i]==val) {
					exists = true;
					return;
				}//if
		
			} //for
		} //arr.length

		//append new value
		if ( exists == false){
			arrStr += "," + val;
			setCookie(name, arrStr);
			return;			
		}
		
	} else {  //set new cookie 
		setCookie(name, val);
		return;	
	} //getCookie

}

function getCookieArray(name){
	var i = 0;
	while (getCookie(name + i) != null) {
		this[i + 1] = getCookie(name + i);
		i++;
		this.length = i; 
	}
}

function inCookie(name, value) {
	var cookieStr;
	if (notEmptyCookie(name)) {
		cookieStr = getCookie(name);
		if (cookieStr.search(value)>-1) {
			return true;
		}
	}
	return false;
}

function notEmptyCookie(name) {
	cookieStr = getCookie(name);
	if ((cookieStr!=null) && (cookieStr>"")) {
		return true;
	}
	return false;
}


//only the first arg (name of cookie) is reqd, remaining args are data to be stored
//var testArray = new setCookieArray("_jtotw", "my", "data", "is", "very", "important");
//var testArray = new getCookieArray("_jtotw");


	/** query string array **/	
	var qsParm = new Array();
	
	/** populate the array with the values of the query string **/
	function qs() {
		var query = window.location.search.substring(1);
		var parms = query.split('&');
	
		for (var i=0; i<parms.length; i++) {
			var pos = parms[i].indexOf('=');
			if (pos > 0) {
				var key = parms[i].substring(0,pos);
				var val = parms[i].substring(pos+1);
					if ( qsParm[key] != undefined ){
						qsParm[key] = qsParm[key] + '|'+ val;
					} else {
						qsParm[key] =  val;
					}
				}
			}
	 }