//////////////////////////////////////////////////////////////////////////////
// Jemsek Specialty Clinic                                                  //
// JavaScript File                                                          //
// Support: General Browser Use (getElementById)                            //
// Design & Copyright: John Jemsek - HOPT Studios                           //
//////////////////////////////////////////////////////////////////////////////

// Menu Constants
var MENU_HEAD_CLASS = "menuhead";
var SUBMENU_CLASS   = "submenu";
var MENU_HEAD_ON    = "menuheadon";
var MENU_HEAD_OFF   = "menuheadoff";
var DEFAULT_TIMEOUT = 500;

// Global Variables
var menuHeadArray  = new Array();
var subMenuArray   = new Array();
var menuMapHash    = new Array();
var hideTimeoutPtr = hideTimeout('all',DEFAULT_TIMEOUT);

//////////////////////////////////////////////////////////////////////////////
// loadListeners : Function puts listners on elements for mouse events.     //
// Usage         : Call on page load to set up a javascript/CSS based menu  //
//////////////////////////////////////////////////////////////////////////////
function loadListeners(){
   if(document.getElementById){
      var tmp_array;
      var parent_elem;
      var namecount = 0;

      tmp_array = document.getElementsByTagName("A"); // Get All Anchors

      for(var i=0;i<tmp_array.length;i++){
         // Weed Out To Get All Of The Menu Heads
         tmp_elem = tmp_array[i];
         if(tmp_elem.className.indexOf(MENU_HEAD_CLASS) != -1){
            // Set An ID For The Menu Head
            tmp_elem               = tmp_array[i];
            tmp_elem.id            = MENU_HEAD_CLASS+namecount;
            tmp_elem.onmouseover   = function(){hideMenu('all');window.clearTimeout(hideTimeoutPtr);showSubMenu(this.id);highlightMenu(this.id,'on');};
            tmp_elem.onmouseout    = function(){window.clearTimeout(hideTimeoutPtr);if (menuMapHash[this.id]){ hideTimeoutPtr=hideTimeout('all',DEFAULT_TIMEOUT); } else { hideTimeoutPtr=hideTimeout('all',0); }};
            tmp_elem.onfocus       = function(){this.onmouseover();};
            tmp_elem.onblur        = function(){this.onmouseout();};
            menuHeadArray[menuHeadArray.length] = tmp_elem;

            // Name The Parent Container If Possible
            if (tmp_elem.parentNode){
               // Using A Self-Defined Field "note" Since Using "id" Breaks IE (Way To Go Guys!)
		       tmp_elem.parentNode.note = MENU_HEAD_CLASS+namecount;
            }
            namecount++;
         }
      }

      tmp_array = document.getElementsByTagName("DIV");  // Get All DIV Tags
      namecount = 0;

      for(var i=0;i<tmp_array.length;i++){
         // Weed Out To Get All Of The Sub-Menus
         if(tmp_array[i].className.indexOf(SUBMENU_CLASS) != -1){
            tmp_elem = tmp_array[i];
            if(tmp_elem){
               tmp_elem.id = SUBMENU_CLASS+namecount;
               subMenuArray[subMenuArray.length] = tmp_elem;
               tmp_elem.onmouseover=function(){window.clearTimeout(hideTimeoutPtr);};
               tmp_elem.onmouseout=function(){window.clearTimeout(hideTimeoutPtr);hideTimeoutPtr=hideTimeout('all',DEFAULT_TIMEOUT);};
               tmp_elem.onfocus=function(){this.onmouseover();};
               tmp_elem.onblur=function(){this.onmouseout();};

               // Link The Parent Menu Node - If Possible
               if (tmp_elem.parentNode){
                  if (tmp_elem.parentNode.note){
                     menuMapHash[tmp_elem.parentNode.note] = SUBMENU_CLASS+namecount;
                  }
               }
               namecount++;
            }
         }
      }
   }
}

//////////////////////////////////////////////////////////////////////////////
// highlightMenu : Highlights a javascript/CSS based menu header            //
// Usage  : Call with parameters                                            //
//               menuID    : Name Of Menu Header To Highlight               //
//               onOff     : Turn On/Off                                    //
//////////////////////////////////////////////////////////////////////////////
function highlightMenu(menuID,onOff){
   var menuHead = document.getElementById(menuID);
   if(menuHead){
      if (onOff == "on")
         menuHead.className = MENU_HEAD_ON;
      else if (onOff == "off")
         menuHead.className = MENU_HEAD_OFF;
   }
}

//////////////////////////////////////////////////////////////////////////////
// showMenu : Shows a javascript/CSS based dropdown sub-menu                //
// Usage  : Call with parameters                                            //
//               menuID    : Name Of Menu To Display                        //
//////////////////////////////////////////////////////////////////////////////
function showSubMenu(menuID){
   hideMenu('all'); // First Hide All Of The Menus
   if (menuMapHash[menuID]){
      var subMenu = document.getElementById(menuMapHash[menuID]);
      if (subMenu)
         subMenu.style.display = 'block'; // Change Menu Display To 'block'
   }
}


//////////////////////////////////////////////////////////////////////////////
// hideMenu : Hides a javascript/CSS based dropdown sub-menu                //
// Usage  : Call with parameters                                            //
//               menuID    : Name Of Menu To Hide (keyword 'all' hides all) //
//////////////////////////////////////////////////////////////////////////////
function hideMenu(menuID){
   if(menuID == 'all'){
      for (var i=menuHeadArray.length-1; i>=0; i--) {
          menuHead = menuHeadArray[i];
          if(menuHead)
             highlightMenu(menuHead.id,'off');
      }

      for (var i=subMenuArray.length-1; i>=0; i--){
         subMenu = subMenuArray[i];
         if(subMenu)
            subMenu.style.display = 'none'; // Change Menu Display To 'none'
      }
   }
   else {
      subMenu = document.getElementById(menuID);
      if(subMenu)
         subMenu.style.display = 'none'; // Change Menu Display To 'none'
   }
}


//////////////////////////////////////////////////////////////////////////////
// hideTimeout : Initiates a menu-hide setTimout for sub-menu               //
// Usage  : Call with parameters                                            //
//               menuID    : Name Of Menu To Hide (keyword 'all' hides all) //
//               mSecs     : Time (in milliseconds) to set timout           //
//////////////////////////////////////////////////////////////////////////////
function hideTimeout(menuID,mSecs){
   var ptr;
   eval('ptr=setTimeout ("hideMenu(\'' + menuID + '\')",' + mSecs + ')');
   return ptr;
}


//////////////////////////////////////////////////////////////////////////////
// checkEmail : Function checks email subscribe/un-subscribe forms.         //
// Usage      : Call on email subscribe/us-subscribe form submit.           //
//////////////////////////////////////////////////////////////////////////////
function checkEmail(form){
   if ((form.email1.value == form.email2.value) && (form.email1.value != '')){
      return true;
   }
   else if (form.email1.value == ''){
      alert('You must fill in a valid email address.');
      return false;
   }
   else if (form.email2.value == ''){
      alert('You must fill in a valid confirmation email address.');
      return false;
   }
   else {
      alert('Email addresses do not match.');
      return false;
   }
}


//////////////////////////////////////////////////////////////////////////////
// popWin : Pops a new window with given size and specifications            //
// Usage  : Call with parameters                                            //
//               winname    : Name Of New Window                            //
//               width      : Width Of New Window                           //
//               height     : Height Of New Window                          //
//               usetoolbar : Have Toolbar On Top?                          //
//////////////////////////////////////////////////////////////////////////////
function popWin(winname,width,height,usetoolbar){

   var x   = 800; // Default Width
   var y   = 600; // Default Height
   var agt = navigator.userAgent.toLowerCase();

   if ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1)){
      // IE
      x = screen.width;
      y = screen.height;
   }
   else {
      // Netscape Or Others
      x = window.outerWidth;
      y = window.outerHeight;
   }

   var X = (x-200)/2;
   var Y = (y-200)/2;
   poppedWin = (usetoolbar)
             ? window.open ("",winname,'toolbar=yes,menubar=yes,resizable=yes,alwaysRaised=yes,status=no,scrollbars=yes,screenX='+X+',screenY='+Y+',top='+Y+',left='+X+',width='+width+',height='+height)
             : window.open ("",winname,'resizable=yes,alwaysRaised=yes,status=no,scrollbars=yes,screenX='+X+',screenY='+Y+',top='+Y+',left='+X+',width='+width+',height='+height);
   poppedWin.focus();
}
