// Utility.js - Copyright (c) 2000, 2001 Gartner Inc.  All rights reserved.


function getBrowserHeight() {
   if (bw.ns4){
      return window.innerHeight
   } else {
      return document.body.clientHeight
   }
}

function getBrowserWidth() {
   if (bw.ns4){
      return window.innerWidth
   } else {
      return document.body.clientWidth
   }
}


var strzCode = new String("z").charCodeAt(0);
var strZCode = new String("Z").charCodeAt(0);
var straCode = new String("a").charCodeAt(0);
var strACode = new String("A").charCodeAt(0);


function isAlpha(str) {
   alpha = true
   for (a=0; a<str.length; a++) {
      charStr = str.charAt(a)
      charCode = charStr.charCodeAt(0)
      if (charStr == " "
            || (charCode >= straCode && charCode <= strzCode)
            || (charCode >= strACode && charCode <= strZCode)) {
         // do nothing
      } else {
         alpha = false;
      }
   }
   return alpha
}


function isAlphaNumeric(str) {
   for(i=0; i<str.length; i++){
      if (!isAlpha(str.charAt(i)) && !isNumeric(str.charAt(i))) {
         return false;
      }
   }
   return true;
}


function isBlank(s) {
   if ((s == ' ') || (s == '\n') || (s == '\t')) {
      return true;
   } else {
      return false;
   }
}


function isNumeric(str) {
   for (a=0; a<str.length; a++){
      charStr = str.charAt(a)
      if (isNaN(parseInt(charStr))) return false
   }
   return true
}


function isValidDate(strDate) {
   intYear = parseInt(strDate.substring(0,4))
   intMonth = parseInt(strDate.substring(4,6))
   intDay = parseInt(strDate.substring(6,8))
   month_length = new Array(31,29,31,30,31,30,31,31,30,31,30,31)
   if (intMonth == 2 && intDay > 28 && (intYear%4 > 0) ) {
      return false;
   }
   if (intDay > month_length[intMonth-1]) {
      return false;
   }
   return true;
}


function isValidWildEMail(str) {
   var indDot = str.lastIndexOf(".");
   var indAt = str.indexOf("@");
   var semi = str.indexOf(";");
   var space = str.indexOf(" ");
   var tick = str.indexOf("`");
   var quote = str.indexOf("\"");

  // Check for invalid characters and obvious errors
  if ((semi > 0) || (space > 0) || (tick > 0) || (quote > 0)
    || (indDot == (str.length - 1)) || (indAt == 0) || (indAt == (str.length - 1)))
  {
    return false;
  }

  // If there is an @ sign, validate 2 strings
  if (indAt != -1)
  {
    if (validateString(str.substring(0, indAt), 0, false) == false)
      return false;
    if (validateString(str.substring(indAt+1), 1, false) == false)
      return false;
  }
  // Validate entire string
  else if (validateString(str, 0, true) == false)
    return false;

  return true;
}

function validateString(str, dotsRequired, wildRequired)
{
  // Check length
  if (str.length == 0)
    return false;

  // Check for illegal characters
  var semi = str.indexOf(";");
  var space = str.indexOf(" ");
  var tick = str.indexOf("`");
  var quote = str.indexOf("\"");

  // Cannot have more than one in a string
  var at = str.indexOf("@");

  if ((semi > 0) || (space > 0) || (tick > 0) || (quote > 0) || (at > 0))
    return false;

  var dotCnt = 0;
  var indDot, indWild, prevIndDot=-1;

  indDot = str.indexOf(".");
  indWild = str.indexOf("*");

  // A dot cannot be the first character in the string
  if (indDot == 0)
    return false;

  while (indDot > 0)
  {
    dotCnt++;

    // A dot cannot be the last character in the string
    if (indDot == (str.length-1))
      return false;

    // We cannot have sequential dots
    if ((prevIndDot != -1) && (indDot == (prevIndDot + 1)))
      return false;

    // Get the next dot in the string - If no more stop looping
    prevIndDot = indDot;
    indDot = str.indexOf(".", indDot + 1);
    if (indDot == -1)
      break;
  }

  // Are we required to have at least one wildcard character?
  if ((wildRequired == true) && (indWild == -1))
    return false;

  // If there is no wildcard character, we need to meet the required number of dots
  if (indWild == -1)
    return (dotCnt >= dotsRequired);
  return true;
}

function isValidEMail(str) {
   email = str ;
   var indDot = email.lastIndexOf(".");
   var indAt = email.indexOf("@");
   var semi = email.indexOf(";");
   var space = email.indexOf(" ");
   var tick = email.indexOf("`");
   var quote = email.indexOf("\"");
   if ((indDot <= 0)
         || (semi > 0)
         || (space > 0)
         || (tick > 0)
         || (quote > 0)
         || (indAt <= 0)
         || ((indDot - indAt) <= 1)
         || (indDot == (email.length - 1))) {
      return false;
   } else {
      return true;
   }
}


function isValidPassword(str) {
   alpha_count = 0
   numeric_count = 0
   for(i=0; i<str.length; i++) {
      if (isAlpha(str.charAt(i))) {
         alpha_count++;
      }
      if (isNumeric(str.charAt(i))) {
         numeric_count++;
      }
   }
   if (alpha_count && numeric_count && isAlphaNumeric(str)) {
      return true;
   } else {
      return false;
   }
}


function openAdmin(href) {
    window.open(href,'_blank','height=550,width=563,scrollbars=yes');
}


function openAdminFooter(href) {
   newWin = window.open(href,'_blank','height=550,width=563,scrollbars=yes');
   storeNewWin(newWin);
   return false;
}

function openBio(href) {
   window.open(href,'_blank','width=565,height=450,scrollbars=yes,resizable=no');
   return false;
}


function openBlankWindow(href) {
   window.open(href,'','height=450,width=750,toolbar=yes,location=yes,scrollbars=yes,resizeable=yes');
   return false;
}


function openDocFromDoc(href) {
   window.open(href,'_blank','height=550,width=680,scrollbars=yes,menubar=yes,resizable=yes');
}


function openHelp(href) {
   openNewAdmin(href);
   return false
}


function openNewAdmin(href) {

/*
   if (document.URL.substr(0,5)!="https" && top && top.footer) {
      top.footer.storeNewWin(
         window.open(href,'_blank','height=520,width=632,scrollbars=yes'));

   } else if (document.URL.substr(0,5) != "https"
         && opener
         && opener.top
         && opener.top.footer) {
      opener.top.footer.storeNewWin(
         window.open(href,'_blank','height=520,width=632,scrollbars=yes'));

   } else if (document.URL.substr(0,5)!="https"
         && opener
         && opener.opener
         && opener.opener.top
         && opener.opener.top.footer) {
      opener.opener.top.footer.storeNewWin(
         window.open(href,'_blank','height=520,width=632,scrollbars=yes'));

   } else if (document.URL.substr(0,5)!="https"
         && opener
         && opener.opener
         && opener.opener.opener
         && opener.opener.opener.top
         && opener.opener.opener.top.footer) {
      opener.opener.opener.top.footer.storeNewWin(
         window.open(href,'_blank','height=520,width=632,scrollbars=yes'));

   } else {
      window.open(href,'_blank','height=520,width=632,scrollbars=yes');
   }
*/
   if (document.URL.substr(0,5)!="https" && storeNewWin) {
      storeNewWin(window.open(href,'_blank','height=520,width=632,scrollbars=yes'));
   } else {
      window.open(href,'_blank','height=520,width=632,scrollbars=yes');
   }
}


function openNewAdminFooter(href) {
   var iLeft;
   var iTop;
   var sWinStr;

   //gets top and left positions based on user's resolution so hint window is centered.
   iLeft = (window.screen.width/2) - (320 + 10); //half the screen width minus half the new window width (plus 5 pixel borders).
   iTop = (window.screen.height/2) - (275 + 27); //half the screen height minus half the new window height (plus title and status bars).
   sWinStr="left=" + iLeft + ",top=" + iTop + ",screenX=" + iLeft + ",screenY=" + iTop;
   
   newWin = window.open(href,'_blank',sWinStr + 'height=550,width=640,scrollbars=yes,status=yes,resizable=yes');
   storeNewWin(newWin);
   return false;
}

function openNewSubAdminFooter(href) {
   //var iLeft;
   //var iTop;
   //var sWinStr;

   //gets top and left positions based on user's resolution so hint window is centered.
   //iLeft = (window.screen.width/2) - (320 + 10); //half the screen width minus half the new window width (plus 5 pixel borders).
   //iTop = (window.screen.height/2) - (275 + 27); //half the screen height minus half the new window height (plus title and status bars).
   //sWinStr="left=" + iLeft + ",top=" + iTop + ",screenX=" + iLeft + ",screenY=" + iTop;
   
   newWin = window.open(href,'__blank','height=550,width=640,scrollbars=yes,status=yes,resizable=yes');
   storeNewWin(newWin);
   return false;
}

function openAdminWindow(href)
{
   var iLeft;
   var iTop;
   var sWinStr;

   // Center the window
//   iLeft = (window.screen.width/2) - (320 + 10); //half the screen width minus half the new window width (plus 5 pixel borders).
//   iTop = (window.screen.height/2) - (275 + 27); //half the screen height minus half the new window height (plus title and status bars).
//   sWinStr="left=" + iLeft + ",top=" + iTop + ",screenX=" + iLeft + ",screenY=" + iTop;
//   newWin = window.open(href,'AdminWindow',sWinStr + 'height=550,width=640,scrollbars=yes,status=yes,resizable=yes,location=yes,menubar=yes,toolbar=yes,personalbar=yes');
   sWinStr="height=" + (window.screen.availHeight-150) + ",width=" + (window.screen.availWidth-20);
   newWin = window.open(href,'AdminWindow',sWinStr + ',scrollbars=yes,status=yes,resizable=yes,location=yes,menubar=yes,toolbar=yes,personalbar=yes');
   storeNewWin(newWin);
   return false;
}

function openNewRegWinFromSearchWin(href) {
   window.open(href,'_blank','height=520,width=632,scrollbars=yes');
}

function openRegistration(href) {
   window.open(href,'','height=550,width=675,scrollbars=yes');
   return false;
}

function openResult(href) {
   window.open(href,'_blank','height=550,width=680,scrollbars=yes,menubar=yes,resizable=yes');
}


function openSearch(href) {
   self.name = nameForWindow('homepage');

   var newname= nameForWindow('search')
   newWin = window.open(href, newname,'width=750,height=500,scrollbars=yes,resizable=yes');
   newWin.focus();
   storeNewWin(newWin);
   return false;
}

function nameForWindow(suffix) {

   var newname = eval('document.domain') + '_' + eval('suffix');
   return newname.replace(/\./g, '_');
}


function openSigninPopup(href) {

   var iMyLeft;
   var iMyTop;
   var sWinStr;
   //gets top and left positions based on user's resolution so hint window is centered.
   iMyLeft = (window.screen.width/2) - (140 + 10); //half the screen width minus half the new window width (plus 5 pixel borders).
   iMyTop = (window.screen.height/2) - (135 + 27); //half the screen height minus half the new window height (plus title and status bars).
   sWinStr="left=" + iMyLeft + ",top=" + iMyTop + ",screenX=" + iMyLeft + ",screenY=" + iMyTop;

   self.name = nameForWindow('homepage');
   
   signinPopup = window.open(href+'&parentWindow='+self.name,'LoginPopup', sWinStr + ',height=275,width=280,scrollbars=no,menubar=no');

   var killPopup = setTimeout("signinPopup.close()", 180000);
}


function openTechSup(href) {
   window.open(href,'','height=550,width=625,scrollbars=yes');
   return false;
}


function openWindow(href) {
   window.open(href,'','height=550,width=563,scrollbars=yes');
   return false;
}


function rollover(layRef,imgRef,srcRef) {
   var srcObj = eval(srcRef)
   if (bw.ns4){
      if ((layRef==null)||(layRef=='')){
         document[imgRef].src = srcObj;
      } else {
         document.layers[layRef].document.images[imgRef].src = srcObj;
      }
   } else {
      document[imgRef].src = srcObj;
   }
}


function toAlphaNumber(checkString) {
   newString = "";
   count = 0;
   for (i = 0; i < checkString.length; i++) {
      ch = checkString.substring(i, i+1);
      if ((ch >= "a" && ch <= "z") || (ch >= "A" && ch <= "Z") ||
      (ch >= "0" && ch <= "9")) {
      newString += ch;
      }
   }
   if (checkString != newString) {
      if (confirm("The value you have entered\n"
            + "contains invalid characters,\n"
            + "please correct them and resubmit.")) {
         return false;
      } else {
         return true;
      }
   }
   return true;
}

function storeNewWin( newWin ) {
   winCTR = winCTR + 1 ;
   if (winCTR > 40) {
      alert ("No more windows allowed");
      return false;
   } else  {
    childWindow[winCTR] = newWin ;
   }
}

function signout(){
   for ( i = 1; i < winCTR + 1 ; i++) {
      if(childWindow[i] != null){
         childWindow[i].close();
      }
   }
   return true;
}

function closeLogin(){
   if (signinPopup != null)
   {
      signinPopup.close();
   }
}

// END OF FILE
