// some code to calculate dates
// var today = new Date();
// var zero_date = new Date(0,0,0);
// today.setTime(today.getTime() - zero_date.getTime());

// var todays_date = new Date(today.getYear(),today.getMonth(),today.getDate(),0,0,0);
// var expires_date = new Date(todays_date.getTime() + (8 * 7 * 86400000));            

function getCookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

function setCookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
      ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
	  ( (domain) ? ";domain=" + domain : "") +
	    ( (secure) ? ";secure" : "");
}

function deleteCookie(name,path,domain) {
    if (getCookie(name)) document.cookie = name + "=" +
      ( (path) ? ";path=" + path : "") +
        ( (domain) ? ";domain=" + domain : "") +
	  ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

// Sets a cookie on the client if one doesn't already exist
function setNewCookie(name,value,expire_date) {

    var cookie = getCookie(name);
    if(!cookie) {
	setCookie(name,value,expire_date);
    }
}

function updateCookieCount( name, expire_date ) {
    var count = getCookie( name );
    if(count == null) {
        setCookie( name, '1' );
        return 1;
    }  
    else {
        var newcount = parseInt( count ) + 1;
        deleteCookie('count');
        setCookie('count', newcount, expire_date );
        return count;
    }
}

function noExpiration() {

  var today = new Date();                                                             
  var zero_date = new Date(0,0,0);                                                    
  today.setTime(today.getTime() - zero_date.getTime());                               
  var todays_date = new Date(today.getYear(),today.getMonth(),today.getDate(),0,0,0); 
  var expire_date = new Date(todays_date.getTime() + (8 * 7 * 86400000));            

  return expire_date;
}
