/*
 * Javascript for intellitest.com
 * @author Richard Jowsey
 * @version  1.3.2  13-Jun-00
 */

var gw = null;  // the glossary popup window
var rm = null;  // the request snail-mail window
var tt = null;  // the bug database logon window
var u = "http://www.jowsey.com/servlet/intellitest.TryTesting";

/* opens the glossary popup displaying a designated word */
function glossary(theWord) {
   var w = '/glossary/glossary.html#' + theWord;
   gw = window.openCenter(w, "glossary", 500, 500);
}

/* cross-browser function which closes the glossary popup */
function closeGlossary() {
   if (gw && gw.open && !gw.closed) gw.close();
}

/* opens the request-snail-mail popup window */
function reqSnailMail() {
   rm = window.openCenter("requestMail.html", "requestMail", 450, 350);
}

/* closes the request-snail-mail popup */
function closeSnailMailReq() {
   if (rm && rm.open && !rm.closed) rm.close();
}

/* validates an email address field when the subscribe form is submitted */
function submitMail(theForm) {
   if (isEmail(theForm.email.value) == false) {
      alert("Invalid email address!\nPlease enter correct information...");
      theForm.email.focus();
      return false;
   }
   return true;
}

/* Q&D validation of an email address */
function isEmail(email) {
   if (!email) return false;
   var iChars = '*|,\":<>[]{}`\';()&$#%';
   for (var i = 0; i < email.length; i++) {
      if (iChars.indexOf(email.charAt(i)) != -1) return false;
   }
   if (email.indexOf('@') == -1) return false;
   if (email.indexOf('@') == email.length - 1) return false;
   if (email.indexOf('.') == -1) return false;
   if (email.indexOf('.') == email.length - 1) return false;
   return true;
}

/* opens a specified popup window centered in the screen */
function openCenter(win, name, width, height) {
   var str = "height=" + height + ",innerHeight=" + height + ",width=" + width + ",innerWidth=" + width;
   if (window.screen) {
      var ah = screen.availHeight - 30;
      var aw = screen.availWidth - 10;
      var xc = (aw - width) / 2;
      var yc = (ah - height) / 2;
      str += ",left=" + xc + ",screenX=" + xc;
      str += ",top=" + yc + ",screenY=" + yc;
   }
   str += ",scrollbars=yes,resizable=yes";
   win = window.open(win, name, str);
   if (win && win.open) win.focus();
   return win;
}

/* validates a bug submission [bug-entry.html] */
function submitBug(theForm) {
   if (theForm.summary.value == '') {
      alert("Summary is a required field.\nPlease enter this information...");
      theForm.summary.focus();
      return false;
   } else if (theForm.steps.value == '') {
      alert("Steps is a required field.\nPlease enter this information...");
      theForm.steps.focus();
      return false;
   }
   return true;
}

/** sets a username cookie [bug-entry.html] */
function userCookie(name) {
   if (name == "") return;
   var expires = new Date();
   expires.setTime(expires.getTime() + (3 * 24 * 60 * 60 * 1000));  // 3 days
   document.cookie = "uid=" + escape(name) + "; expires=" + expires.toGMTString() + "; path=/";
}

/* internal function to return the value of a specified cookie */
function cookieVal(arg) {
   var c = document.cookie;
   if (c == "") return "";
   var p1 = c.indexOf(arg);
   if (p1 == -1) return "";
   var p2 = c.indexOf("=", p1);
   var p3 = c.indexOf(";", p1);
   if (p3 == -1) p3 = c.length;
   return unescape(c.substring(p2 + 1, p3));
}

/** assigns the username field from a cookie [TryTesting] */
function getUserName() {
   var user = cookieVal("uid");
   if (user != "" && document.forms[0].uid.value == "")
      document.forms[0].uid.value = user;
   document.forms[0].uid.focus();
}

/* opens the bug database logon window [TryTesting] */
function logonBugDatabase() {
   var user = cookieVal("uid");
   if (user) u += "?uid=" + user;
   tt = window.openCenter(u, "bugDB", 500, 450);
}

/* opens the bug database list window [TryTesting] */
function listBugDatabase() {
   /*
   var user = cookieVal("uid");
   if (!user) user = prompt("Your User Name?", "");
   if (user) {
      u += "?uid=" + user + "&command=list";
      tt = window.openCenter(u, "bugDB", 600, 450);
   }
   */
   u += "?command=list";
   tt = window.openCenter(u, "bugDB", 600, 450);
}
