// JavaScript Document
/*
Usage:
setCookie('name', 'value'[, days, 'path', 'domain', secure]); 
where name, value, and path are strings, and secure is either true or null. 
Only name and value are required. Note: days can be either a number of days until the cookie expires
or a GMT-formatted date string such as 'Fri, 13-Apr-1970 00:00:00 GMT'. 

getCookie('name'); 
Returns the value associated with name. If it does not exist, then it returns null.

delCookie('name'[, 'path', 'domain']); 
Remember that path and domain must be supplied if they were set with the cookie. 
*/

function setCookie(cookieName, value, days, path, domain, secure){
	if(days) {
		if ((typeof(days) == 'string') && Date.parse(days) ) { // already a Date string
			var expdate = days;
		} else if (typeof(days) == 'number') { // calculate Date from number of hours
			var expdate = (new Date((new Date()).getTime() + (days * 24 * 3600 * 1000))).toGMTString();
		}
	}
	document.cookie = cookieName + '=' + escape(value) + ((expdate)?(';expires=' + expdate):'') + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'') + ((secure && (secure == true))?'; secure':''); // Set the cookie, adding any parameters that were specified.
}

function getCookie(search_name) { 
  var tab_cookies = document.cookie.split( ';' ); 
  for ( i = 0; i < tab_cookies.length; i++ ) { 
    var cookie_tmp = tab_cookies[i].split('='); 
    var cookie_name = cookie_tmp[0].replace(/^\s+|\s+$/g, ''); 
    if (cookie_name==search_name) { 
      // we need to handle case where cookie has no value but exists (no = sign, that is): 
      if (cookie_tmp.length>1) { 
        return unescape( cookie_tmp[1].replace(/^\s+|\s+$/g, '') ); 
      } 
      // cookie is initialized but no value => result = null 
      return null; 
    } 
  } 
  return null; 
}

function delCookie(name, path, domain) {
  var theValue = getCookie(name); // We need the value to delete the cookie
  if(theValue) {
      document.cookie = name + '=' + '; expires=Thu, 01-Jan-1970 00:00:01 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:''); // set an already-expired cookie
  }
} // delCookie

