// JAVASCRIPT BASE (C) 2006 HUNGRY GUY

// JAVASCRIPT SYNTAX IS REALLY BIZARRE!
// IN A SANE LANGUAGE, A FUNCTION CALL LOOKS LIKE THIS:
//
//     result = function (parm1, parm2, parm3, ...);
//
// BUT IN JAVASCRIPT, A FUNCTION CALL LOOKS LIKE THIS:
//
//     result = parm1.function (parm2, parm3, ...);
//
// THIS WHACKY SYNTAX HAS CAUSED ME ENDLESS BUGS AND RUNTIME ERRORS!
// AND DON'T GET ME STARTED ON THIS $%#@& CASE-SENSITIVITY!
// SO I'VE CREATED MY OWN VERSIONS OF SOME COMMON FUNCTIONS:

function mysubstr (s, i, j) {return s.substr (i--, j);}
function mylength (s) {return s.length;}
function myindex (s, c) {return s.indexOf (c);}
function myupper (s) {return s.toUpperCase ();}
function mylower (s) {return s.toLowerCase ();}
function myrandom (i) {return Math.floor(Math.random() * i);}
function myint (i) {return Math.floor(i);}
function myminute (i) {
  today = new Date();
  nowtime = today.getTime() - 0;
  s1 = Math.floor (nowtime / 100000);
  minutes = Math.floor (s1 / 0.6);
  return minutes;
  }

// WRITE SOME TEXT TO THE PAGE

function put (str) { document.write (str); }

// INCREMENT COUNT BY INC

function incr (count, inc) {
  return Number(count) + Number(inc);
  }

// STUPID JAVASCRIPT THINKS THAT 3 + 5 = 35
// SO WE HAVE TO DO SOME GYRATIONS TO MAKE IT DO
// BASIC ARITHMETIC!

// DECREMENT COUNT BY INC BUT DON'T LET COUNT GO NEGATIVE

function decr (count, inc) {
  i = 0;
  while ((i < inc) && (count > 0)) { count--; i++; }
  return count;
  }

// TEST IF A GIVEN FLAG IS PRESENT IN THE STRING OF FLAGS

function active (flags, flag) {
  if (flags.indexOf (flag) >= 0) { return true; }
  else { return false; }
  }

// APPEND A FLAG TO THE STRING OF FLAGS

function addflag (flags, newflag) {
  newflags = '';
  if (active (flags, newflag)) { newflags = flags; }
  else { newflags = flags + newflag; }
  return newflags;
  }

// DELETE A FLAG FROM THE STRING OF FLAGS
// AND DELETE ALL OCCURRENCES OF THAT FLAG JUST TO BE SAFE

function delflag (flags, oldflag) {
  newflags = flags;
  ix1 = newflags.indexOf (oldflag);
  while (ix1 >= 0) {
    temp1 = newflags.substring (0, ix1);
    temp2 = newflags.substring (ix1 + 1, newflags.length);
    newflags = temp1 + temp2;
    ix1 = newflags.indexOf (oldflag);
    }
  return newflags;
  }

// REMOVE ANY/ALL INVALID CHARACTERS BEFORE SAVING A VALUE TO A COOKIE

function validate (str) {
  while (str.substr (str.length - 1, 1) == ' ') {
    str = str.substr (0, str.length - 1);
    }
  while (str.substr(0,1) == ' ') {
    str = str.substr (1, str.length - 1);
    }
  i = 0;
  while (i < str.length - 1) {
    if (str.substr (i, 2) == '  ') {
      str = str.substring (0, i) + str.substring (i + 1, str.length);
      i = 0;
      }
    else i++;
    }
  i = 0;
  while (i < str.length) {
    if ((str.substr (i, 1) >= 'A') && (str.substr (i, 1) <= 'Z')) {
      ok = 1;
      }
    else if ((str.substr (i, 1) >= 'a') && (str.substr (i, 1) <= 'z')) {
      ok = 1;
      }
    else if (str.substr (i, 1) == '.') {
      ok = 1;
      }
    else if (str.substr (i, 1) == ' ') {
      str = str.substring (0, i) + '_' + str.substring (i + 1, str.length);
      ok = 1;
      }
    else if (str.substr (i, 1) == "'") {
      ok = 1;
      }
    else if (str.substr (i, 1) == '-') {
      ok = 1;
      }
    else if (str.substr (i, 1) == '_') {
      ok = 1;
      }
    else if (str.substr (i, 1) == '@') {
      ok = 1;
      }
    else if ((str.substr (i,1) >= '0') && (str.substr (i,1) <= '9')) {
      ok = 1;
      }
    else ok = 0;
    if (ok == 0) {
      str = str.substring (0, i) + str.substring (i + 1, str.length);
      i = 0;
      }
    else i++;
    }
  return str;
  }

// ENSURE THAT THE COOKIE NAME IS VALID

function cookname (str) {
  str = str.toUpperCase ();
  i = 0;
  while (i < str.length) {
    if ((str.substr (i, 1) >= 'A') && (str.substr (i, 1) <= 'Z')) {
      ok = 1;
      }
    else if ((str.substr (i,1) >= '0') && (str.substr (i,1) <= '9')) {
      ok = 1;
      }
    else ok = 0;
    if (ok == 0) {
      str = str.substring (0, i) + str.substring (i + 1, str.length);
      i = 0;
      }
    else i++;
    }
  if (str.length <= 0) str = 'ANONYMOUS';
  return str;
  }

// SAVE A VARIABLE TO THE COOKIE IN THE FORM 'COOKIENAME=(/TAG:VALUE/TAG:VALUE/.../);'

function save (cookiename, tag, value) {
  today = new Date();
  year = today.getYear() - 0;
  if (year < 100) { year = year + 2000; }
  year = year + 10;
  gmt = 'Monday, 1-Jan-' + year + ' 01:01:01 GMT';
  // alert ('GMT="' + gmt + '"');
  cookiename = validate (cookiename);
  cookiename = cookiename.toUpperCase ();
  tag = validate (tag);
  tag = tag.toUpperCase ();
  value = escape (validate (String (value)));
  mycookie = document.cookie;
  if (mycookie <= 0) {
    document.cookie = cookiename + '=(/' + tag + ':' + value + '/);expires=' + gmt;
    return;
    }
  i = mycookie.indexOf(cookiename + '=(');
  if (i < 0) {
    document.cookie = cookiename + '=(/' + tag + ':' + value + '/);expires=' + gmt;
    return;
    }
  mycookie = mycookie.substr (i, mycookie.length - i);
  i = mycookie.indexOf (')');
  if (i < 0) {
    document.cookie = cookiename + '=(/' + tag + ':' + value + '/);expires=' + gmt;
    return;
    }
  mycookie = mycookie.substr (0, i + 1);
  i = mycookie.indexOf ('/' + tag + ':');
  if (i < 0) {
    i = mycookie.indexOf (')');
    mycookie = mycookie.substr (0, i) + tag + ':' + value + '/)';
    document.cookie = mycookie + ';expires=' + gmt;
    return;
    }
  else {
    mycookie1 = mycookie.substr (0, i + 1);
    mycookie2 = mycookie.substr (i + 1, mycookie.length - i - 1);
    i = mycookie2.indexOf ('/');
    mycookie2 = mycookie2.substr (i, mycookie2.length - i);
    mycookie = mycookie1 + tag + ':' + value + mycookie2;
    document.cookie = mycookie + ';expires=' + gmt;
    return;
    }
  }

// UNSAVE A VARIABLE FROM A COOKIE

function unsave (cookiename, tag) {
  today = new Date();
  year = today.getYear() - 0;
  if (year < 100) { year = year + 2000; }
  year = year + 10;
  gmt = 'Monday, 1-Jan-' + year + ' 01:01:01 GMT';
  cookiename = validate (cookiename);
  cookiename = cookiename.toUpperCase ();
  tag = validate (tag);
  tag = tag.toUpperCase ();
  mycookie = document.cookie;
  if (mycookie <= 0) return;
  i = mycookie.indexOf(cookiename + '=(');
  if (i < 0) return;
  mycookie = mycookie.substr (i, mycookie.length - i);
  i = mycookie.indexOf (')');
  if (i < 0) return;
  mycookie = mycookie.substr (0, i + 1);
  i = mycookie.indexOf ('/' + tag + ':');
  if (i < 0) {
    }
  else {
    mycookie1 = mycookie.substr (0, i + 1);
    mycookie2 = mycookie.substr (i + 1, mycookie.length - i - 1);
    i = mycookie2.indexOf ('/');
    mycookie2 = mycookie2.substr (i + 1, mycookie2.length - i - 1);
    mycookie = mycookie1 + mycookie2;
    document.cookie = mycookie + ';expires=' + gmt;
    return;
    }
  }

// OBTAIN A VARIABLE FROM A COOKIE

function load (cookiename, tag) {
  cookiename = validate (cookiename);
  cookiename = cookiename.toUpperCase ();
  tag = validate (tag);
  tag = tag.toUpperCase ();
  mycookie = document.cookie;
  if (mycookie <= 0) return '';
  i = mycookie.indexOf(cookiename + '=(');
  if (i < 0) return '';
  mycookie = mycookie.substr (i, mycookie.length - i);
  i = mycookie.indexOf (')');
  if (i < 0) return '';
  mycookie = mycookie.substr (0, i + 1);
  i = mycookie.indexOf ('/' + tag + ':');
  if (i < 0) return '';
  mycookie = mycookie.substr(i + 1, mycookie.length - i - 1);
  i = mycookie.indexOf ('/');
  if (i < 0) return '';
  mycookie = mycookie.substr(0, i);
  i = mycookie.indexOf (':');
  if (i < 0) return '';
  mycookie = mycookie.substr(i + 1, mycookie.length - i - 1);
  value = unescape (mycookie);
  i = 0;
  while (i < value.length) {
    if (value.substr (i, 1) == '_') {
      value = value.substring (0, i) + ' ' + value.substring (i + 1, value.length);
      }
    else i++;
    }
  return value;
  }

// PURGE AN ENTIRE COOKIE FROM THE USER'S DISK

function purge (cookiename) {
  cookiename = validate (cookiename);
  cookiename = cookiename.toUpperCase ();
  document.cookie = cookiename + '=;expires=Monday, 1-Jan-2000 01:01:01 GMT';
  }

// LINK TO ANOTHER PAGE WHILE PASSING PARAMETERS AS PART OF THE URL

function link (label, page, parms) {
  if (parms.substr (0, 1) != '?') {
    parms = '?' + parms;
    }
  i = parms.length - 1;
  if (parms.substr (i, 1) != '?') {
    parms = parms + '?';
    }
  put ('<A HREF="' + page + '.htm' + parms + '">' + label + '</A>');
  }

// GET A PARM OFF THE URL

function getparm (e) {
  url = document.location + '';
  temp = unescape (url);
  parm = '';
  if (e == 0) {
    parm = temp;
    i = parm.indexOf ('?');
    if (i > 0) {
      parm = parm.substr (0, i);
      }
    }
  else while (e > 0) {
    i = temp.indexOf ('?');
    if (i < 0) return '';
    temp = temp.substr (i + 1, temp.length - 1);
    parm = temp;
    i = parm.indexOf ('?');
    if (i > 0) {
      parm = parm.substr (0, i);
      }
    else parm = '';
    e--;
    }
  parm = unescape (parm);
  i = 0;
  while (i < parm.length) {
    if (parm.substr (i, 1) == '+') {
      parm = parm.substring (0, i) + ' ' + parm.substring (i + 1, parm.length);
      }
    else i++;
    }
  return parm;
  }

// OBTAIN A CATALOG DIRECTORY OF ALL COOKIES

function catdir (e) {
  if (e <= 0) return '';
  mycookie = document.cookie;
  if (mycookie <= 0) return '';
  while (e > 0) {
    i = mycookie.indexOf('=(');
    if (i <= 0) return '';
    f = 0;
    while (f == 0) {
      if (mycookie.substr (i, 1) == ' ') f = 1;
      else if (mycookie.substr (i, 1) == ';') f = 1;
      else if (i < 0) f = 1;
      else i--;
      }
    mycookie = mycookie.substr (i + 1, mycookie.length - i);
    i = mycookie.indexOf('=(');
    catname = mycookie.substr (0, i);
    mycookie = mycookie.substr (i + 2, mycookie.length - 2);
    e--;
    }
  return catname;
  }

// PROPERLY CAPITALIZE A NAME

function capname (str) {
  str = validate (str);
  str1 = str.toUpperCase ();
  str2 = str.toLowerCase ();
  if ((str != str1) && (str != str2)) return str;
  i = 0;
  while (i < str.length) {
    if (i == 0) {
      str1 = str.substr (0, 1);
      str1 = str1.toUpperCase ();
      str2 = str.substr (1, str.length - 1);
      str = str1 + str2;
      }
    else if (str.substr (i - 1, 1) == ' ') {
      str1 = str.substr (0, i);
      str2 = str.substr (i, 1);
      str2 = str2.toUpperCase ();
      str3 = str.substr (i + 1, str.length - i - 1);
      str = str1 + str2 + str3;
      }
    else if (str.substr (i - 1, 1) == "'") {
      str1 = str.substr (0, i);
      str2 = str.substr (i, 1);
      str2 = str2.toUpperCase ();
      str3 = str.substr (i + 1, str.length - i - 1);
      str = str1 + str2 + str3;
      }
    else if (str.substr (i - 1, 1) == "-") {
      str1 = str.substr (0, i);
      str2 = str.substr (i, 1);
      str2 = str2.toUpperCase ();
      str3 = str.substr (i + 1, str.length - i - 1);
      str = str1 + str2 + str3;
      }
    else if (str.substr (i - 1, 1) == "_") {
      str1 = str.substr (0, i);
      str2 = str.substr (i, 1);
      str2 = str2.toUpperCase ();
      str3 = str.substr (i + 1, str.length - i - 1);
      str = str1 + str2 + str3;
      }
    else {
      str1 = str.substr (0, i);
      str2 = str.substr (i, 1);
      str2 = str2.toLowerCase ();
      str3 = str.substr (i + 1, str.length - i - 1);
      str = str1 + str2 + str3;
      }
    i++;
    }
  return str;
  }

// DOES A GIVEN COOKIE EXIST BY THIS NAME?

function exists (cookiename) {
  cookiename = validate (cookiename);
  cookiename = cookiename.toUpperCase ();
  mycookie = document.cookie;
  if (mycookie <= 0) return false;
  i = mycookie.indexOf(cookiename + '=(');
  if (i < 0) return false;
  mycookie = mycookie.substr (i, mycookie.length - i);
  i = mycookie.indexOf (')');
  if (i < 0) return false;
  return true;
  }

// GET A PARM OFF THE URL
// OR RETURN THE INITIAL VALUE IF THE PARM DOESN'T EXIST

function getparminit (e, initvalue) {
  value = getparm (e);
  if (value.length == 0) value = initvalue;
  return value;
  }

// OBTAIN A VARIABLE FROM A COOKIE
// OR SAVE AND RETURN THE INITIAL VALUE IF THE VARIABLE DOESN'T EXIST

function loadinit (cookiename, tag, initvalue) {
  value = load (cookiename, tag);
  if (value.length == 0) {
    value = initvalue;
    save (cookiename, tag, value);
    }
  return value;
  }

// ERASE A USER'S COOKIE AND CLOSE THE WINDOW

function snuff (cookiename) {
  purge (cookiename);
  window.close();
  }

// DISPLAY A STATUS MESSAGE ON THE STATUS AREA

function smsg (points) {
  if (points > 0) {
    window.status = 'You have ' + points + ' Play Points.';
    }
  cmd = 'smsg(points)';
  timerTwo=window.setTimeout(cmd,5000);
  }

// END OF JAVAWORLD