var defaultExpiration = 0;
var defaultPath = '/';
var defaultDomain = null;
var defaultSecure = null;

function getValueFrom(str,name,delimiter) {
  //  Usage: getValue(String to search,name[,delimiter]);
  //  Returns: String containing the value associated with the name 
  //              if name is in the search string
  //           False if name is not in the search string 
  //  Parameters: 
  //     str = String to be searched
  //     name = name of the name=value pair
  //     delimiter (optional) = the character that seperates name=value pairs - default is ";"
  var args = getValueFrom.arguments;
  if (args.length < 2) return false; 

  delimiter = (delimiter) ? delimiter : ';';  // if no delimiter passed, set to ";"
  var first, last;
  first = str.indexOf(name+'=');
  if (first != -1) {
	last = str.indexOf(delimiter,first);
    if (last == -1) last = str.length;
    return str.substring(first+name.length+1,last);
  }  
  else return false;
}

function buildCookie(name, value, expiration, path, domain, secure) {
	var args = buildCookie.arguments;
	if (args.length < 2) return false; 
	if (args.length > 2) {
		if (isNaN(expiration))  // if expiration is not a number, expect mm dd, yyyy format
			var time = new Date(expiration);
		else {                  // expiration represents the numbers of days from today
			var time = new Date();
			time.setTime(time.getTime() + (expiration*24*60*60*1000));
		}
	}
	var cookieStr = name + "=" + value + ((expiration && (expiration != 0)) ? "; expires=" + time.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? "; secure" : "");
	return cookieStr;
}

function setCookie(name, value, expiration, path, domain, secure) {
	var args = setCookie.arguments;
	if (args.length < 2) return false; 
	value = escape(value);
	var prevCookieLength = 0;
	if (doesCookieExist(name)) {
		prevCookieLength = name.length + escape(getCookie(name)).length + 1;
	}
	if (((document.cookie.length - prevCookieLength) + name.length + value.length + 1) >= 4046) // cookie exceeds maximum size
		return false;
		
	expiration = (args.length > 2) ? args[2] : defaultExpiration;
	path = (args.length > 3) ? args[3] : defaultPath;
	domain = (args.length > 4) ? args[4] : defaultDomain;
	secure = (args.length > 5) ? args[5] : defaultSecure;
	  
	document.cookie=buildCookie(name,value,expiration,path,domain,secure);
	return true;
}

function deleteCookie(name,path,domain) {
  var args = deleteCookie.arguments;
  if (args.length == 0) return false; 
  path = (args.length > 1) ? args[1] : defaultPath;
  domain = (args.length > 2) ? args[2] : defaultDomain;
  if (getCookie(name)) {
	document.cookie=buildCookie(name,null,-1,path,domain);
	return true;
  }
  else return false;
}

function getCookie(name) {
  var args = getCookie.arguments;
  if (args.length == 0) return null; 
  var cookieValue = getValueFrom(document.cookie,name,';');
  if (cookieValue) return unescape(cookieValue);
  else return null;
}

function cookiesSupported() {
  setCookie('testingCookieSupport','support');
  var doesSupport = (getCookie('testingCookieSupport')) ? true : false;
  if (doesSupport) deleteCookie('testingCookieSupport');
  return doesSupport;
}

function doesCookieExist(name) {
  if (!name) return false;
  return (getCookie(name)) ? true : false;
}

