// See: http://www.elated.com/articles/javascript-and-cookies/

//////////////////////////////
// FUNCTION: SET COOKIE //////
// set_cookie ( "username", "John Smith" );
// set_cookie ( "username", "John Smith", 2003, 01, 15 );
// set_cookie ( "username", "John Smith", 2003, 01, 15, "", "wai.co.uk", "secure" );
//////////////////////////////
function set_cookie ( name, value, exp_y, exp_m, exp_d, path, domain, secure ) {
  var cookie_string = name + "=" + escape ( value );

  if ( exp_y )
  {
    var expires = new Date ( exp_y, exp_m, exp_d );
    cookie_string += "; expires=" + expires.toGMTString();
  }

  if ( path )
        cookie_string += "; path=" + escape ( path );

  if ( domain )
        cookie_string += "; domain=" + escape ( domain );
  
  if ( secure )
        cookie_string += "; secure";
  
  document.cookie = cookie_string;
}

///////////////////////////////////
// FUNCTION: DELETE COOKIE ////////
// delete_cookie ( "username" ); //
///////////////////////////////////
function delete_cookie ( cookie_name ) {
  var cookie_date = new Date ( );  // current date & time
  cookie_date.setTime ( cookie_date.getTime() - 1 );
  document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

////////////////////////////////////////
// FUNCTION: GET COOKIE ////////////////
// var x = get_cookie ( "username" ); //
////////////////////////////////////////
function get_cookie ( cookie_name ) {
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}
