/*---------------------------------------------------------------------------------*
 * File: tabs.js
 * Author: Andreas Blixt <andreasblixt@msn.com>
 * Source: http://tutorials.mezane.org/tabbed-navigation-using-css/
 * Modified by: Nishikant Kapoor (nkapoor@webrachna.com)
 * Notes: The JavaScript works by assuming you'll have <div class="content"> elements in the page
 * with IDs that match those of the anchors in links on the page. When a link to an anchor is
 * clicked, the script will scan for the element with the same ID as the anchor and show it,
 * while hiding the rest. Note that due to rendering issues with browsers, the element IDs have
 * an underscore prepended to them, stopping the browser from scrolling to their location. If CSS
 * is disabled, but JavaScript is enabled, this means the anchors won't scroll the page as
 * expected.
 *---------------------------------------------------------------------------------*/
// CSS helper functions
CSS = {
    // Adds a class to an element.
    AddClass: function (e, c) {
        if (!e.className.match(new RegExp("\\b" + c + "\\b", "i")))
            e.className += (e.className ? " " : "") + c;
    },

    // Removes a class from an element.
    RemoveClass: function (e, c) {
        e.className = e.className.replace(new RegExp(" \\b" + c + "\\b|\\b" + c + "\\b ?", "gi"), "");
    }
};

// Functions for handling tabs.
Tabs = {
  //return the ID of the item that is currently selected, i.e., has class=current
  getCurrent: function(ch) {
    var curr="";
    for (var i = 0; i < ch.length; i++) {
      var el = ch[i];
      if (el.tagName == "LI") {
        if (i == 0) {curr = el;}
        if (el.className == "current") {
          var anchors = el.getElementsByTagName("a");
          var a = anchors[0];  //expecting 1 anchor per LI
          return a.hash.substring(1);  //strip off leading # and send the rest #Page_2
	}
      }
    }
    return curr;
  },

  //set appropriate default tab in Step 1 when a tab in Step 2 is clicked
  //clickedTab is #Page_tabSeq, which is hard-coded in merChannelInp.tmpl
  setDefaultTabInStep1: function(clickedTab) {

    var tabDefSelStr = 'tabDefSel'; //var str='';
    var tmpArr1 = clickedTab.split('_'); var clickedTabSeq = tmpArr1[1];
    if (clickedTabSeq) {
      //str += " clickedTab=" + clickedTab + " clickedTabSeq=" + clickedTabSeq;
      for (var i=0; i < document.forms.length; i++) {
        var f = document.forms[i];
        //str += " f=" + f.name;
        if (f.name.indexOf('merMaintForm') > -1) {
          for (var j = 0; j < f.elements.length; j++) {
            var el = f.elements[j];
	    if (el.name.indexOf(tabDefSelStr) > -1) {
              var tmpArr2 = el.name.split(tabDefSelStr); //split into tabDefSel & tabSeq
              if (tmpArr2[1] == clickedTabSeq) { //this tab in Step 1 is same as what user clicked on in Step 2
                el.checked=1;
	      }
	      else {el.checked=0;}
            }
          }
        }
      } //for (var i = 0; i < document.forms.length; i++)
    } //if (clickedTabSeq)
    //alert("OnClickHandler(tabs.js):" + str);
  },

    // Changes to the tab with the specified ID.
    GoTo: function (contentId, parentId, skipReplace) {
        // This variable will be true if a tab for the specified
        // content ID was found.
        var foundTab = false;

        if (!parentId) {parentId = contentId.parentNode;}

        if (parentId) {
          var ch = document.getElementById(parentId).getElementsByTagName('*');
          for (var i = 0; i < ch.length; i++) {
            var el = ch[i];
            if (el.tagName == "LI") {
	      var li = el;
              // Give the current tab link the class "current" and
              // remove the class from any other TOC links.
              var anchors = li.getElementsByTagName("a");
              for (var k = 0; k < anchors.length; k++) {
                if (anchors[k].hash == "#" + contentId) {
                    CSS.AddClass(li, "current");
                    foundTab = true;
                    break;
                } else {
                    CSS.RemoveClass(li, "current");
                }
              }
            }
          }
	}

        // Show the content with the specified ID.
        var divsToHide = [];
	//alert("GoTo(tabs.js):parentId=" + parentId);
        var divs = document.getElementById(parentId).getElementsByTagName("div");

        for (var i = 0; i < divs.length; i++) {
          var div = divs[i];
          if (div.className.match(/\bcontentMer\b/i)) {
	    if (div.id == "_" + contentId) {div.style.display = "block";}
	    else {divsToHide.push(div);}
          }
        }

        // Hide the other content boxes.
        for (var i = 0; i < divsToHide.length; i++) {
          var d = divsToHide[i]; d.style.display = "none";
	}

    },

    OnClickHandler: function (e) {
      // Stop the event (to stop it from scrolling or
      // making an entry in the history).
      if (!e) e = window.event;
      if (e.preventDefault) e.preventDefault(); else e.returnValue = false;

      var parentId = this.parentNode;  //LI - merChannel.tmpl
      parentId = parentId.parentNode;  //OL
      parentId = parentId.parentNode;  //noWrapDiv
      parentId = parentId.parentNode;  //td - tableCell
      parentId = parentId.parentNode;  //tr - tableRow
      parentId = parentId.parentNode;  //tableSection
      parentId = parentId.parentNode;  //tableElement
      parentId = parentId.parentNode;  //1005_P - contentMerP div

      // Get the name of the anchor of the link that was clicked.
      Tabs.GoTo(this.hash.substring(1), parentId.id, true);

      //alert ("this.hash.substring(1)=" + this.hash.substring(1) + " parentId.id=" + parentId.id);

      //set appropriate default tab in Step 1 when a tab in Step 2 is clicked
      Tabs.setDefaultTabInStep1(this.hash.substring(1));  //#Page_tabSeq is hard-coded in merChannelInp.tmpl
    },

    Init: function () {
      if (!document.getElementsByTagName) return;

      // Attach an onclick event to all the anchor links on the page.
      var anchors = document.getElementsByTagName("a");
      for (var i = 0; i < anchors.length; i++) {
        var a = anchors[i];
        if (a.hash) {a.onclick = Tabs.OnClickHandler;}
      }

      //get all parents of all tabbed navigations in all the modules on Home page
      var elemArr = getElementsByClass('contentMerP');

      for (var i = 0; i < elemArr.length; i++) {  //loop through the parents
        var parentNode = elemArr[i]; var parentId = parentNode.id;

        // get a list of all descendants for the given parent
        var ch = document.getElementById(parentId).getElementsByTagName('*');

        for (var j = 0; j < ch.length; j++) {
          var div = ch[j];
          if (div.className.match(/\bcontentMer\b/i)) {div.id = "_" + div.id;}
        }

        var contentId = Tabs.getCurrent(ch); //Page_1, Page_21
        if (contentId) {Tabs.GoTo(contentId, parentId, true);}
      } //for (var i = 0; i < elemArr.length; i++)
      //alert("Init(tabs.js)...");
    }
};


// Hook up the OnLoad event to the tab initialization function.
// window.onload = Tabs.Init;

/******************************************************
function initTabs () {
  // Hide the content while waiting for the onload event to trigger.
  //nkk var contentId = window.location.hash || "#Introduction";

  var contentId = window.location.hash;

  //alert("initTabs(tabs.js): contentId=" + contentId);

  if (document.createStyleSheet) {
    var style = document.createStyleSheet();
    style.addRule("div.contentMer", "display: none;");
    style.addRule("div" + contentId, "display: block;");
  }
  else {
    var head = document.getElementsByTagName("head")[0];
    if (head) {
      var style = document.createElement("style");
      style.setAttribute("type", "text/css");
      style.appendChild(document.createTextNode("div.contentMer { display: none; }"));
      style.appendChild(document.createTextNode("div" + contentId + " { display: block; }"));
      head.appendChild(style);
    }
  }
}
********************************************************/
