/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie (defaults to end of current session)
   [path] - path for which the cookie is valid (defaults to path of calling document)
   [domain] - domain for which the cookie is valid (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/
function setCookie( locale ) {

  // create an instance of the Date object
  var now = new Date();

  //   fix the bug in Navigator 2.0, Macintosh
  fixDate(now);

  //  cookie expires in 365 days
  now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
  now.setTime(now.getTime());

  var curCookie = COOKIE_NAME + "=";
  curCookie+= escape(locale);
  curCookie+= ";expires=";
  curCookie+= now.toGMTString() ;
  curCookie+= ";path=" ;
  document.cookie = curCookie;
}

//
//  name - name of the desired cookie
//  return string containing value of specified cookie or null
//  if cookie does not exist

function getCookieLocaleValue() {
  var dc = document.cookie;

  var prefix = COOKIE_NAME + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}


//  This function will return the locale we will use
function getLocale(){

  var locale = getCookieLocaleValue();
  if (!locale){
    locale = DEFAULT_LOCALE;
    setCookie( DEFAULT_LOCALE );
    }

    return locale;

  }

//  This function will return the locale we will use
function changeLocale( newLocale ){
  if (newLocale == null || newLocale.length < 1)
    newLocale = DEFAULT_LOCALE;
  setCookie( newLocale );
  }


