// Copyright © 2002 by Thoughtslinger Corporation.
// All Rights Reserved. Unauthorized Duplication Prohibited.

function makeArray (n)
{
  this.length = n;
  return this;
}

dayNames = new makeArray(7);
dayNames[1] = "Sunday";
dayNames[2] = "Monday";
dayNames[3] = "Tuesday";
dayNames[4] = "Wednesday";
dayNames[5] = "Thursday";
dayNames[6] = "Friday";
dayNames[7] = "Saturday";

monthNames = new makeArray(12)
monthNames[1] = "January";
monthNames[2] = "February";
monthNames[3] = "March";
monthNames[4] = "April";
monthNames[5] = "May";
monthNames[6] = "June";
monthNames[7] = "July";
monthNames[8] = "August";
monthNames[9] = "September";
monthNames[10] = "October";
monthNames[11] = "November";
monthNames[12] = "December";

function dateString (theDate)
{
  var theDay   = dayNames[theDate.getDay()+1];
  var theMonth = monthNames[theDate.getMonth()+1];
  var dayNum   = theDate.getDate();
  var theYear  = theDate.getFullYear();
  return theDay + " " + theMonth + " " + theDate.getDate() + ", " + theYear;
}

// Getting, setting and deleting cookies

function SetCookie (name, value, days)
{
  // set the expiration date
  var today = new Date();
  var expiry = new Date (today.getTime() + (days * 24 * 60 * 60 * 1000));

  // set the cookie
  if (value != null && value != "")
    document.cookie = name + "=" + escape(value) +
                      "; expires=" + expiry.toGMTString()
                      + "; path=/";
}

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 DeleteCookie (name)
{
  // if the cookie exists, set its expiry date to Jan 01 1970
  if (GetCookie(name))
  {
    document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// User Name - at least 4 chars, uc, lc, and underscore only.

function ToggleMenuLevel1()
{
  var SrcElement = window.event.srcElement;
  var elemLevel2 = document.all(SrcElement.level2ID);

  if (elemLevel2.style.display == 'none')
  {
    elemLevel2.style.display = '';
    SetCookie (elemLevel2.id, false);

//    if (SrcElement.usesGlyph == '1')
//      SrcElement.innerHTML = '&#054;';
  }

  else
  {
    elemLevel2.style.display = 'none';
    SetCookie (elemLevel2.id, true);

//    if (SrcElement.usesGlyph == '1')
//      SrcElement.innerHTML = '&#052;';
  }
}


// User Name - at least 4 chars, uc, lc, and underscore only.

function checkUserName (strng)
{
  var msg = "";
  var illegalChars = /\W/; // allow letters, numbers, and underscores

  if (strng == "") {
    msg = "You didn't enter a user name.\n";
  }
  else if (strng.length < 4) {
    msg = "The user name is too short.\n";
  }
  else if (illegalChars.test(strng)) {
    msg = "The user name contains illegal characters.\n";
  }
  return msg;
}

// password - at least 6 chars, uppercase, lowercase, and numeral

function checkPassword (strng)
{
  var errorCode = 0;
  var illegalChars = /[\W_]/; // allow only letters and numbers

  if (strng == "") {
    errorCode = 1;
  }
  else if (strng.length < 6) {
    errorCode = 2;
  }
  else if (illegalChars.test(strng)) {
    errorCode = 3;
  }
  return errorCode;
}

// session ID - numeric

function checkSessionID (strng)
{
  var errorCode = 0;
  var digits = "0123456789";

  if (strng == "") {
    errorCode = 1;
  }
  else {
    for (var i = 0; i < strng.length; i++)
    {
      temp = strng.substring(i, i+1)

      if (digits.indexOf(temp) == -1 && strng != "")
      {
        errorCode = 2;
        break;
      }
    }
  }
  return errorCode;
}

function checkEmail (strng)
{
  var msg="";

  // from Apple
  var emailFilter = /^.+@.+\..{2,3}$/;

  // from netscape
  // var reEmail = /^.+\@.+\..+$/


  if (strng == "") {
    msg = "You didn't enter an email address.\n";
  }
  else if (!(emailFilter.test(strng))) {
    msg = "The email address is invalid.\n";
  }
  else {
    // test email for illegal characters
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
    if (strng.match(illegalChars)) {
      msg = "The email address contains illegal characters.\n";
    }
  }

  if (msg != "") {
    return msg;
  }

  var at  = "@"
  var dot = "."
  var len = strng.length

  var lat  = strng.indexOf(at)
  var ldot = strng.indexOf(dot)
  var error = false;

  if (strng.indexOf(at)==-1 || strng.indexOf(at)==0 || strng.indexOf(at)==len) {
    error = true;
  }

  if (strng.indexOf(dot)==-1 || strng.indexOf(dot)==0 || strng.indexOf(dot)==len) {
    error = true;
  }

  // check for duplicate @
  if (strng.indexOf (at,(lat+1)) != -1) {
    error = true;
  }

  // dot positioning
  if (strng.substring(lat-1,lat)==dot || strng.substring(lat+1,lat+2)==dot) {
    error = true;
  }

  // must be a dot after the @
  if (strng.indexOf(dot,(lat+2))==-1) {
    error = true;
  }

  // no spaces
  if (strng.indexOf(" ")!=-1) {
    error = true;
  }

  if (error == true) {
    msg = "The email address is invalid.\n";
  }
  return msg;
}


// non-empty textbox

function isEmpty (strng)
{
  var error = "";
  if (strng.length == 0) {
    error = "The mandatory text area has not been filled in.\n"
  }
  return error;
}

function popWindow (page)
{
  popup = window.open (page,'_blank','toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width=600,height=400');
  if (popup != null)
  {
    if (popup.opener == null)
    {
      popup.opener = self;
    }
    popup.location.href = page;
  }
}


