//
// 25 May 2005 Steve Pearce 
//    Rename to v3, uses '=' rather than ':' as delimiter for keys
//    so components can access cookie key values correctly
//

function setCookie(name,value,expires,path,domain,secure,key) {

	// ensure path is set to '/' if not otherwise specified
	if (path == null || path.length==0)
		{
		path = '/';
		}

	if (key) {
		cookieValue = getCookie(name);
		cookieKeys = getKeys(cookieValue);
		cookieKeys[key] = escape(value)
		value = ''
		for (var prop in cookieKeys) {
			if (value.length > 0) value += '&';
			value += prop + '=' + cookieKeys[prop]
		}
	}
	else {
		value = escape(value)
	}
	document.cookie = name + '=' + value +
	((expires) ? '; expires=' + expires.toGMTString() : '') +
	((path) ? ';path=' + path : '') +
	((domain) ? ';domain=' + domain : '') +
	((secure) ? '; secure' : '');
}
function getCookie(name, key) {
	var search = name + '='
  var returnvalue = '';
  if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search)
    // if cookie exists
    if (offset != -1) { 
			offset += search.length
      // set index of beginning of value
      end = document.cookie.indexOf(';', offset);
      // set index of end of cookie value
      if (end == -1) end = document.cookie.length;
      if (key) {
				returnvalue = unescape(getKeys(document.cookie.substring(offset, end))[key])
      }
      else {
				returnvalue=unescape(document.cookie.substring(offset, end))
			}
		}	
	}
	return returnvalue;
}

function getKeys(cookieValue) {
	var keyValuePair, keyValue = new Array();
	if (cookieValue.length > 0) {
		keyValuePair = cookieValue.split('&')	
		for (var i = 0, j = keyValuePair.length || 0; i < j; i++) {
			keyValuePair[i] = keyValuePair[i].split('=');
			keyValue[keyValuePair[i][0]] = keyValuePair[i][1];
		}
	}
	return keyValue;
}