/*
WM_setCookie(), WM_readCookie(), WM_killCookie(), WM_acceptsCookies()
A set of functions that eases the pain of using cookies.

Source: Webmonkey Code Library
(http://www.hotwired.com/webmonkey/javascript/code_library/)

Author: Nadav Savio
Author Email: nadav@wired.com
Исправления: Сергей Молоков, 21.10.99

Usage: 

WM_setCookie('name', 'value'[, hours, 'path', 'domain', secure]);
  where name, value, and path are strings, and secure is either true or null. Only name and value are required.

WM_readCookie('name');
  Returns the value associated with name.

WM_getCookieValue('name');
  This is a helper function used by WM_readCookie() and WM_killCookie(). It extracts a single value based on name, given a cookie of the form 'name=value'.

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

WM_acceptsCookies();
  Returns true or false.
*/

function cookieDateH( H) // Выдаёт дату в часах относительно текущей
{
  var d = new Date();

  d.setTime( d.getTime() + H*60*60*1000);
  return d.toGMTString();
}

function WM_acceptsCookies() {
  // This function tests whether the user accepts cookies.
  // Declare variable.
  var answer;
  // Try to set a cookie.
  document.cookie = 'WM_acceptsCookies=yes';
  // If it fails, return false; if it succeeds, return true.
  if(document.cookie == '') answer = false; else answer = true;
  // Then clean up by expiring the cookie.
  document.cookie = 'WM_acceptsCookies=yes; expires=Fri, 13-Apr-1970 00:00:00 GMT';
  return answer;
}

function WM_setCookie (name, value, hours, path, domain, secure) 
{
  var expirationDate = hours;
  var cookieString = '';

  // Don't waste your time if the browser doesn't accept cookies.
  if (WM_acceptsCookies()) {
    if (hours) {
      if ( typeof( hours) != 'string') expirationDate = cookieDateH( hours);
    }
    // Set the cookie, adding any parameters that were specified.
    cookieString = name + '=' + escape(value);
    if ( hours ) {
      cookieString = cookieString + ';EXPIRES=' + expirationDate;
      if ( path  ) { 
        cookieString = cookieString + ';PATH=' + path;
        if ( domain) { 
          cookieString = cookieString + ';DOMAIN=' + domain;
          if ( secure && (secure == true)) cookieString = cookieString + '; SECURE';
        }
      }
    }
    cookieString = cookieString + ';';
     
    document.cookie = cookieString;
//    cookie = cookieString;
//    alert( 'Установка cooke \n\"' + cookieString + '\"');
  }
}

function WM_readCookie(name) {
  // if there's no cookie, return false else get the value and return it
  if(document.cookie == '') return false; 
  else return unescape(WM_getCookieValue(name));
}

function WM_getCookieValue(name) {  
  var firstChar, lastChar, endOfName;// Declare variables.

  // Get the entire cookie string. (This may have other name=value pairs in it.)
  var theBigCookie = document.cookie;

  // Grab just this cookie from theBigCookie string.
  // Find the start of 'name'.
  firstChar = theBigCookie.indexOf(name);

  // If you found it,
  if ( (firstChar != -1) && (theBigCookie.charAt(firstChar + name.length) == '=')) 
  { 
    firstChar += name.length + 1;// skip 'name' and '='.

    // Find the end of the value string (i.e. the next ';').
    lastChar = theBigCookie.indexOf(';', firstChar);

    if (lastChar == -1) lastChar = theBigCookie.length;
    
    return theBigCookie.substring( firstChar, lastChar); // Return the value.
  } 
    
  return false; // If there was no cookie, return false.
}

function WM_killCookie(name, path, domain) {
  // We'll need the name and the value to kill the cookie, so get the value.
  var theValue = WM_getCookieValue(name);
  // Assuming there actually is such a cookie.
  if( theValue ) {
    // Set an expired cookie, adding 'path' and 'domain' 
    // if they were given.
    document.cookie = name + '=' + theValue + '; expires=Fri, 13-Apr-1970 00:00:00 GMT' + ((path)?';path=' + path:'') + ((domain)?';domain=' + domain:'');
  }
}
//eof
