
// Utility javascript functions for WinansTech Website

var nHideCnt=0;
var nShowCnt=0;
var oOpenSubMenu=null;
var sMonitorText = '';

function checkBrowser() {
   var fAppVersion;
   if (navigator.appName == "Microsoft Internet Explorer") {
      fAppVersion = parseFloat(navigator.appVersion.substr(navigator.appVersion.indexOf("MSIE") + 5), 3);
   }
   else {
      fAppVersion = parseFloat(navigator.appVersion);
   }

   if (fAppVersion < 5.0) {
      alert("Your browser does not support oCurrent web technologies. Some features will not work as intended however, most features will work. Click OK to continue.");
   }
}

// Selects existing value in form text field
function selectItem() {
   var theEventCallerId;

   // Figure out who called us
   if(navigator.appName == "Netscape") {
      theEventCallerId = arguments.callee.caller.arguments[0].target.id;
   }
   else {
      theEventCallerId=window.event.srcElement.id;
   }

   var textArea = document.getElementById(theEventCallerId);
   textArea.select();
}

// Set the focus the the calling control
function setFocus(sId) {
   var textArea = document.getElementById(sId);
   textArea.focus();
}
  
function showSubMenu(event, sId) {
   var nX, nY;
   oOpenSubMenu = document.getElementById(sId); // Remember which sub menu is open

   if (window.event) {
      var oRefObj = oOpenSubMenu.parentElement; // IE
      nY = oRefObj.offsetHeight - 1;
   }
   else {
      var oRefObj = event.currentTarget;
      nY = oRefObj.offsetHeight + 1;
   }
   
   // Set positon of submenu
   nX = oRefObj.offsetLeft + (oRefObj.offsetWidth / 2) - (getElementWidth(sId) / 2) ;
   
   setElementProperty(sId, 'left', nX + "px");
   setElementProperty(sId, 'top', nY + "px");
   
   if (oRefObj.id == activePageMenuItem) { // Menu item for active page stays color of selection
      oOpenSubMenu.className = 'activePageSubMenu';
   }
   else {
      oRefObj.className = 'openMenuItem';
   }
}

function hideSubMenu(event, sId) {   
   var oCurrent, oRelated;

   // Get nodes of objects that caught and triggered the event
   if (window.event) { // Browser is IE
      oCurrent = window.document.getElementById(sId).parentElement; // Top of bubble chain
      oRelated = window.event.toElement; // Triggered
   }
   else { // Browser is DOM spec compliant
      oCurrent = event.currentTarget; // Caught
      oRelated = event.relatedTarget; // Triggered
   }
   // If catching and triggering ojects are the same OR triggering object is 
   // child (or subchild) of catcher THEN don't do anything.
   if ((oCurrent != oRelated) && !contains(oCurrent, oRelated)) {
      setElementProperty(sId, 'left', "-400px");
      setElementProperty(sId, 'top', "0px");
      oOpenSubMenu = null;
      if (oCurrent.id != activePageMenuItem) { // Active menu item stays color of selection
         oCurrent.className = 'menuItem';
      }
   }
}

function contains(oNodeA, oNodeB) { // Return true if oNodeA contains oNodeB.
   while (oNodeB.parentNode && (oNodeB != null)) 
      if ((oNodeB = oNodeB.parentNode) == oNodeA)
         return true;
   return false;
}


// Returns reference to the object if passed argument is its ID (string) otherwise returns the passed object argument
function getObj(uObj) {
   if(typeof(uObj) == "object") {
      return uObj;
   } else {
      return document.getElementById(uObj);
   }
}
   
function getElementLeft(uElement) {
   var nX = 0;
   var oElement = getObj(uElement);

   while (oElement != null) { // Loop thru parent elements till we get to the top node
      nX += oElement.offsetLeft;
      oElement = oElement.offsetParent;
   }
   return parseInt(nX);
}

// Return the px width of the element 
function getElementWidth(uElement) {
   var oElement = getObj(uElement);

   return parseInt(oElement.offsetWidth);
}

// Return the px distance from right border of the element to left border of window
function getElementRight(uElement){
   return getElementLeft(uElement) + getElementWidth(uElement);
}

// Return the px distance from top border of the element to top border of the window
function getElementTop(uElement) {
   var nY = 0;
   var oElement = getObj(uElement);

   while (oElement != null) { // Loop thru parent elements till we get to the top node
      nY+= oElement.offsetTop;
      oElement = oElement.offsetParent;
   }
   return parseInt(nY);
}

// Return the px height of the element
function getElementHeight(uElement) {
   var oElement = getObj(uElement);

   return parseInt(oElement.offsetHeight);
}

// Return the px distance from bottom border of the element to border top of the window
function getElementBottom(uElement) {
   return getElementTop(uElement) + getElementHeight(uElement);
}

// Return a style property of the element , return null if does not exist
function getElementProperty(uElement, p_property) {
   var oElement = getObj(uElement);

   if (oElement != null) {
      if(oElement.style) {
         oElement = oElement.style;
         if(oElement[p_property]) {
            return oElement[p_property];
         } 
         else {
            return null;
         }
      } 
      else {
         return null;
      }
   }
}

// Set a property of style type of the element
function setElementProperty(uElement, p_property, p_value){
   var oElement = null;
   if(typeof(uElement) == "object"){
      oElement = uElement;
   } else {
      oElement = document.getElementById(uElement);
   }
   if((oElement != null) && (oElement.style != null)){
      oElement = oElement.style;
      oElement[ p_property ] = p_value;
   }
}

// Checks a input text field for basic structure of an Email address.
// Does not verify the address exists
function checkEmailAddr(sFieldId) {
   var oAddr = document.getElementById(sFieldId);
   var regExp = /^\w+@\w+\.\w+$/;
   
   if (!regExp.test(oAddr.value)) {
      alert("The Email address "+oAddr.value+" does not appear to be a correctly formatted address. Please try again.");
      return false;
   }
   return true;
}

// For SuperUser updates to topics
function submitTopicEdit(sEditMode) {
   var oUpdate = document.updateTopic;
   oUpdate.entryMode.value = sEditMode;
   oUpdate.submit();
}

// For SuperUser updates to the lowest level items
function submitEdit(sInsertPoint, sEditMode) {
   var oUpdate = document.update;
   oUpdate.insertPointName.value = sInsertPoint;
   oUpdate.entryMode.value = sEditMode;
   oUpdate.submit();
}

// Makes the edit object for the submenu visible
function editSubMenu() {
   var oMenuEditBox = document.getElementById('menuEditBox');
   oMenuEditBox.style.top = "150px";
   oMenuEditBox.style.left = ((document.body.offsetWidth - oMenuEditBox.offsetWidth)/2) + "px";
}

// Highlite active menu items
function hiliteActiveMenus(sActivePageMenuItem, sActiveSubMenuItem) {
   if (sActivePageMenuItem) {
      var oActiveItem = document.getElementById(sActivePageMenuItem);
      oActiveItem.className = 'activePageMenuItem';
   }
   if (sActiveSubMenuItem) {
      var oActiveSubItem = document.getElementById(sActiveSubMenuItem);
      oActiveSubItem.className = 'activeSubMenuItem';
   } 
}

