//
// JavaScript for all web pages
//

// define global variables
var form   = null;
var formOK = false;
var errCode     = "";
var errField    = "";
var errFieldNdx = 0;
var errFieldDsc = "";
var validChars  = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

// verifies that the form has no errors
function checkForm() {
  return formOK;
}

// initializes a page
function initPage(hasForm) {
  setStatus("");
  if (hasForm) {
    form   = document.forms[0];
    formOK = false;
    if (form.ErrCode != null)  errCode  = form.ErrCode.value;
    else errCode = "";
    if (form.ErrField != null) errField = form.ErrField.value;
    else errField = "";
    return initForm();
  } else return true;
}

// determines if one string (src) contains another string (arg)
// (case-sensitive)
function contains(src, arg) {
  return (src.indexOf(arg) >= 0);
}

// determines if one string (src) contains another string (arg)
// (case-insensitive)
function containsIgnoreCase(src, arg) {
  return contains(src.toLowerCase(), arg.toLowerCase());
}

// determines if one string (src) ends with another string (arg)
// (case-sensitive)
function endsWith(src, arg) {
  return (src.lastIndexOf(arg) == (src.length - arg.length));
}

// determines if one string (src) starts with another string (arg)
// (case-sensitive)
function startsWith(src, arg) {
  return (src.indexOf(arg) == 0);
}

// returns the primary version of the user's browser
function getBrowserVersion() {
  return navigator.appVersion.substring(0, navigator.appVersion.indexOf("."));
}

// returns a cookie value by variable name
function getCookie(name) {
  var allcookies = document.cookie;
  if ((allcookies == null) || (allcookies == "")) return null;
  var varname = name + "=";
  var cookies = allcookies.split(";");
  for (var i = 0; i < cookies.length; i++) {
    if (contains(cookies[i], varname)) {
      return unescape(trim(cookies[i]).substring(varname.length));
    }
  }
  return null;
}

// sets a cookie variable name and value (expires with session)
function setCookie(name, value) {
  document.cookie = name + "=" + escape(value) + "; path=/";
}

// corrects for Mac javascript date bug
// IMPORTANT:  this function should only be called once
// for any given date object
function fixDateObject(date) {
  var base = new Date(0);
  var skew = base.getTime(); // dawn of Unix time
  if (skew > 0) {            // should be 0 except on the Mac
    date.setTime(date.getTime() - skew);
  }
}

// go back to a previous page
function goBack() {
  parent.mainframe.location.replace(getCookie("storeHttp") + "GoBack");
  return true;
}

// sets the error field index
function setErrFieldNdx() {
  for (var i = 0; i < form.elements.length; i++) {
    if (form.elements[i].name == errField) {
      errFieldNdx = i;
      break;
    }
  }
  return true;
}

// sets the error field focus
function setErrFieldFocus() {
  if ((errFieldNdx < 0)
  ||  (errFieldNdx > form.elements.length)) return;
  // if the error field is a text input field,
  // select the field's text
  if ((form.elements[errFieldNdx].type == "text")
  ||  (form.elements[errFieldNdx].type == "password")) {
    form.elements[errFieldNdx].select();
  }
  // set the focus to the error field
  if ((form.elements[errFieldNdx].type == "checkbox")
  ||  (form.elements[errFieldNdx].type == "file")
  ||  (form.elements[errFieldNdx].type == "password")
  ||  (form.elements[errFieldNdx].type == "radio")
  ||  (form.elements[errFieldNdx].type == "select-one")
  ||  (form.elements[errFieldNdx].type == "select-multiple")
  ||  (form.elements[errFieldNdx].type == "text")
  ||  (form.elements[errFieldNdx].type == "textarea")) {
    form.elements[errFieldNdx].focus();
  }
}

// sets the text for the status bar
function setStatus(statustext) {
  self.status = statustext;
  return true;
}

// shows the help window for the selected help topic
function showHelp(helpTopic) {

  var helpName, helpFrame, helpText, helpWindow;

  // default to the standard user help
  if ((helpTopic == null) || (helpTopic == "")) helpTopic = "";

  // initialize for the help that is to be displayed
  if (startsWith(helpTopic, "Adm")) {
    helpName  = "AdminHelp";
    helpFrame = "html/admin/admhelp.html?";
    helpText  = "html/admin/admhelptext.html#";
  } else if ((startsWith(helpTopic, "Csr"))
         ||  (startsWith(helpTopic, "Sfi"))) {
    helpName  = "SfiHelp";
    helpFrame = "html/sfi/sfihelp.html?";
    helpText  = "html/sfi/sfihelptext.html#";
  } else {
    helpName  = "UserHelp";
    helpFrame = "html/help.html?";
    helpText  = "html/helptext.html#";
  }

  // show the help window
  helpWindow = window.open("", helpName, "resizable=yes,scrollbars=no,width=520,height=480");
  if (helpWindow.frames.length < 3) {
    helpWindow.location = getCookie("storeHttp") + helpFrame + helpTopic;
  } else {
    helpWindow.helptextframe.location = getCookie("storeHttp") + helpText + helpTopic;
    helpWindow.focus();
  }

  return false; // cancel hyperlink
}

// determines if the user's browser is Netscape
function isNetscape() {
  return containsIgnoreCase(navigator.appName, "Netscape");
}

// determines if the site administration frameset is active
function isSiteAdmin() {
  return getCookie("storeMode") == "siteadmin";
}

// determines if the sfi account selection frameset is active
function isSfiSelect() {
  return getCookie("storeMode") == "sfiselect";
}

// determines if the sfi shopping frameset is active
function isSfiShopping() {
  return getCookie("storeMode") == "sfishopping";
}

// determines if the standard shopping frameset is active
function isShopping() {
  return getCookie("storeMode") == "shopping";
}

// reloads the banner frame...
// invoked directly by various template onunload() events or
// indirectly by the reloadFrames() function below
function reloadBanner() {
  if (isNetscape()) parent.bannerframe.location.reload();
  else {
    if (isSfiShopping()) parent.bannerframe.location=getCookie("storeHttp") + "sfi/SfiBanner";
    else parent.bannerframe.location = getCookie("storeHttp") + "Banner";
  }
}

// reloads the categories frame...
// invoked directly by various template onunload() events or
// indirectly by the reloadFrames() function below
function reloadCategories() {
  if (isShopping()) {
    if (isNetscape()) parent.contentsframe1.location.reload();
    else parent.contentsframe1.location = getCookie("storeHttp") + "Categories";
  }
}

// reloads the banner frame & conditionally reloads the categories frame...
// invoked directly by various template onunload() events
function reloadFrames() {
  reloadBanner();
  if (isShopping()) {
    var reload = getCookie("reloadcategories");
    setCookie("reloadcategories", "false");
    if ((reload != null) && (reload == "true")) reloadCategories();
  }
}

// returns the given string with leading and trailing spaces removed
function trim(s) {
  var len = 0;
  if (s != null) len = s.length;
  if (len == 0) return s;
  var i = 0;
  var j = len - 1;
  while ((s.charAt(i) == " ") && (i <= j)) i++;
  while ((s.charAt(j) == " ") && (j >= i)) j--;
  return s.substring(i, j + 1);
}
