var TopicIdPrev = 0;
var TopicIdCur = 0; // introduction
var TopicIdNext = 0;
var CatIdPrev = 0;
var CatIdCur = 0;  
var CarIdNext = 0;
var LibIdPrev = 0;
var LibIdCur = 0;
var LibIdNext = 0;
var TopicOffsetTopCont = 0;
var TopicOffsetTopIndex = 0;


// do not use it 
function redrawHelp(fNavigDisplayed) {
       
            var xheight = document.all ? document.body.clientHeight : 
                window.innerHeight;

            var xwidth = document.all ? document.body.clientWidth : 
                window.innerWidth;
            
           // alert("xheight:'" + xheight);
           // alert("xwidth:'" + xwidth);

            var helpindex = document.getElementById('helpindex');
            var helpcontents = document.getElementById('helpcontents');
            var main = document.getElementById('main');
            var bkgtbl = document.getElementById('bkgrtbl');
            
            var footerhight = 0;
            var scrolheadheight = 14;
            var sidemenuwidth = 0; 
            var leftpanewidth = 210;
            var secondmenuheight = 0;
            
            var bannerheight = 20;
            if (fNavigDisplayed == '1'){
              bannerheight = 90; // 110;
              footerhight = 54; // 60;
            }  
            
            var sidemenu = document.getElementById('sidemenu');
            if (sidemenu != null) sidemenuwidth = 110;
            
            var secondnav = document.getElementById('secondnav');
            // secondmenuheight = secondnav.style.height;
            if (secondnav != null) secondmenuheight = 20;
             
            if ((helpindex.className == 'navHide') && (helpcontents.className == 'navHide')) {

               bkgtbl.height = xheight; // - scrolheadheight;
               main.style.height = xheight; // - scrolheadheight;
               main.style.width = xwidth - 10;

               return;
            }
            
            xwidth = xwidth - 4;
            xheight = xheight - bannerheight;
        
            bkgtbl.height = xheight - scrolheadheight - footerhight - secondmenuheight;
            
            helpcontents.style.height = xheight - footerhight - secondmenuheight;
            document.getElementById('contents_inner').style.height = xheight - scrolheadheight - footerhight - secondmenuheight; // - 8;
            
            helpindex.style.height = xheight - footerhight - secondmenuheight;
            document.getElementById('index_inner').style.height = xheight - scrolheadheight - footerhight - secondmenuheight; // - 8;
            
            
            
            var header = document.getElementById('header');
            if (header != null){
              header.style.width = xwidth - leftpanewidth - sidemenuwidth;
            }
            else
            {scrolheadheight = -12;}
            
            main.style.height = xheight - scrolheadheight - footerhight - secondmenuheight;
            
            main.style.width = xwidth - leftpanewidth - sidemenuwidth;


			// display default topic or library category
             if (TopicIdCur > 0)  DisplayTopic(TopicIdCur);    
             if (CatIdCur >0)     DisplayCategory(CatIdCur);
             if (LibIdCur >0)     DisplayLib(LibIdCur);
        }
        
                
        
        function DisplayContents () {
        
          //rAddLibContentsTree();
        
          document.getElementById('helpindex').className = "navHide"; // "HelpTopicHidden";
          document.getElementById('helpcontents').className = "navShow";
          document.getElementById('lefttab').className = "selected";
          document.getElementById('righttab').className = "unselected";
        }
        
        function DisplayHelpIndex () {
          
          //alert("rAddHelpIndexNodes: " + rAddHelpIndexNodes);
          if (rAddHelpIndexNodes != null){rAddHelpIndexNodes();}
          
          document.getElementById('helpcontents').className = "navHide"; //"HelpTopicHidden";
          document.getElementById('helpindex').className = "navShow";
          
          document.getElementById('lefttab').className = "unselected";
          document.getElementById('righttab').className = "selected";
          
        }
        
        function DisplayLibIndex () {
          
          rAddLibIndexNodes();
          document.getElementById('helpcontents').className = "navHide"; //"HelpTopicHidden";
          document.getElementById('helpindex').className = "navShow";
          
          document.getElementById('lefttab').className = "unselected";
          document.getElementById('righttab').className = "selected";
          
        }
                
        function DisplayPrevious() {
          if (TopicIdPrev > 0) 
            DisplayTopic(TopicIdPrev);
          else
            DisplayPreviousNew();
          
          TopicIdPrev = 0;
        }
        
        
        
        function DisplayPreviousNew() {
        
          var TopicIdNext2; 
          // find topicID based on left pane tab selected Contents or Index 
          if (document.getElementById('helpcontents').className == "navHide")
          {
            var TopicIdNext2 = GetPrevDivWithId('helpindex',TopicIdCur);
          }
          else
          {
             var TopicIdNext2 = GetPrevDivWithId('helpcontents',TopicIdCur);
          }
        
          if (TopicIdNext2 > 0) {
            DisplayTopic(TopicIdNext2);
          }
        }
        
        function DisplayNext() {
        
          var TopicIdNext2; 
          // find topicID based on left pane tab selected Contents or Index 
          if (document.getElementById('helpcontents').className == "navHide")
          {
            var TopicIdNext2 = GetNextDivWithId('helpindex',TopicIdCur);
          }
          else
          {
             var TopicIdNext2 = GetNextDivWithId('helpcontents',TopicIdCur);
          }
        
          if (TopicIdNext2 > 0) {
            DisplayTopic(TopicIdNext2);
           }
        }

        function DisplayNextOld() {
        
         if (TopicIdNext > 0) {
            DisplayTopic(TopicIdNext);
            TopicIdNext = 0;
          }
        }
        
        function DisplayTopicWithExpand(TopicID) {
          DisplayTopic(TopicID);
          //expandCollapse(document.getElementById(TopicID));
          expandNode(document.getElementById(TopicID));
        }
        
        function DisplayTopic(TopicID) {
	      
	     //alert("DisplayTopic1 TopicID=" + TopicID);
	      if (TopicID < 1) return; 
	      
	      var HelpVer;
	      
	      var topicDivNode = document.getElementById('topicid_' + TopicID);
	      if (topicDivNode == null){
              var divParentNode = document.getElementById('main');
           //   alert("divParentNode= " + divParentNode);
              if (BeHelpVersion == null){
               HelpVer = document.getElementById('HelpVer').value;
              }
              else
                HelpVer = BeHelpVersion;
             //  alert("DisplayTopic2 HelpVer=" + HelpVer);
             
             //if(!AddTopicNodeAjaxSinc(TopicID,HelpVer,divParentNode)){ 
             if(!AddTopicNodeAjaxAsinc(TopicID,HelpVer,divParentNode)){
              
                 alert("Faled to load topic=" + TopicID);
                 return;
              }
	      }
	      
	    // alert("offsetTop=" + document.getElementById('topicid_' + TopicID).offsetTop);
    	            	      
          DisplayDivWithPatt(TopicID,"topicid_");
          
          
          TopicOffsetTopCont = SelectDivsWithId('treeRoot',TopicID);
          TopicOffsetTopIndex = SelectDivsWithId('indexRoot',TopicID);
          
          //SetScrollerToDiv(TopicID,'contents_inner');
          
          SetScrollerToTopic('LeftPane',TopicOffsetTopIndex);
          SetScrollerToTopic('LeftPane',TopicOffsetTopCont);
          
          //SetScrollerToTopic('index_inner',TopicOffsetTopIndex);
          //SetScrollerToTopic('contents_inner',TopicOffsetTopCont);
          
              
          TopicIdPrev = TopicIdCur;
	      TopicIdCur = TopicID;
	      
	      rWriteToCookie("TopicID",TopicID,"");
	      
	      // 
	      if (TopicIDs[CurIndex] != TopicID){
	        
	        if (TopicIDs.length > 1){
	           // trancate array at CurIndex
	           if (CurIndex < TopicIDs.length - 1){
	            TopicIDs = TopicIDs.splice(CurIndex);
	           }
	           
	           TopicIDs.push(TopicID);
	           var PrevBut = document.getElementById("butPreviousTopic");
               if (PrevBut != null){
                  PrevBut.style.display = "inline";
               }
            }
            else {
              TopicIDs.push(TopicID);
            }
            CurIndex = TopicIDs.length - 1;
    
          }
         
        }
        
        
        
        function DisplayCategory(CatID) {
	      
	   //   alert ("DisplayCategory id=" + CatID);
	      if (CatID < 1) return;


	      var catDivNode = document.getElementById('catid_' + CatID);
	      if (catDivNode == null){
              var divParentNode = document.getElementById('main');
              
             if(!AddCategoryNodeAjaxAsinc(CatID,divParentNode)){
             //if(!AddCategoryNodeAjaxSinc(CatID,divParentNode)){
              
                 alert("Faled to load category=" + CatID);
                 return;
              }
              
              catDivNode = document.getElementById('catid_' + CatID);
	      }
                   
       
       /*  
          
         alert ("catDivNode.className=" + catDivNode.className);
         alert ("catDivNode.id=" + catDivNode.id);
         alert ("catDivNode.innerHTML=" + catDivNode.innerHTML);
         alert ("divParentNode.id=" + divParentNode.id);
       */   
       
       
       
         HideDivWithPatt ("libid_");
         DisplayDivWithPatt(CatID,"catid_");         
         
         UnselectDivsOfType('indexRoot','lib'); 
         SelectDivsWithId('treeRoot',CatID);
         
         CatIdPrev = CatIdCur;
         CarIdCur = CatID;
         LibIdCur = 0;
         
        // rWriteToCookie("CatID", CatID,"");
         rWriteToCookie("LibOrCatID","CatID_" + CatID,"");
         
         // alert('3');
        }
        
        function DisplayLib(LibID) {
 
       //   alert ("DisplayLib id=" + LibID);
          if (LibID < 1) return;
         
          var libDivNode = document.getElementById('libid_' + LibID);
         
	      if (libDivNode == null){

             var divParentNode = document.getElementById('main');

             if(!AddLibraryNodeAjaxAsinc(LibID,divParentNode)){
             //if(!AddLibraryNodeAjaxSinc(LibID,divParentNode)){
              
                 alert("Faled to load library=" + LibID);
                 return;
              }
             // else alert("node added");
             libDivNode = document.getElementById('libid_' + LibID);
	      }
          /*
          alert ("libDivNode.className=" + libDivNode.className);
          alert ("libDivNode.id=" + libDivNode.id);
          alert ("libDivNode.innerHTML=" + libDivNode.innerHTML);     
          alert ("divParentNode.id=" + divParentNode.id);
   */
                  
          HideDivWithPatt("catid_");
       
          DisplayDivWithPatt(LibID,"libid_");
          
          // expand parent node

          
          SelectDivsWithId('treeRoot',LibID);
                    
          SelectDivsWithId('indexRoot',LibID);
          
          LibIdPrev = LibIdCur;
          LibIdCur = LibID;
          CatIdCur = 0;
          
          rWriteToCookie("LibOrCatID","LibID_" + LibID,"");
          
          return;         
        }
        
        function DisplayAllDivWithPatt(patt,maindiv) {
         
         // alert("DisplayAllDivWithPatt"); 
          if (maindiv == null){
           maindiv = document.getElementById('main');
          }
          var allells = maindiv.getElementsByTagName('DIV');
          var xell;
          var xid;
          var divtype;
          
          for(var i=0; i < allells.length; i++)
	      {
		    xell = allells[i]; //topicid_
		    xid = new String();
		    xid = xell.getAttribute('id');
		    if (xid)
 		     if (xid.substring(0, patt.length) == patt) xell.className = "HelpTopicShow";

	      }
	      return;
        }
        
        function DisplayDivWithPatt(pattID,patt,maindiv) {
         
         // alert("DisplayDivWithPatt"); 
          if (maindiv == null){
           maindiv = document.getElementById('main');
          }
          var allells = maindiv.getElementsByTagName('DIV');
          var xell;
          var xid;
          var divtype;
          
          for(var i=0; i < allells.length; i++)
	      {
		    xell = allells[i]; //topicid_
		    xid = new String();
		    xid = xell.getAttribute('id');
		    if (xid){
		     if (xid.substring(0, patt.length) == patt){
		      if (xid == patt + pattID){
			    xell.className = "HelpTopicShow";
			  //  alert("div found"); 
	      //      alert("show div: " + xid + " xell.className: " + xell.className);
		      }
		      else {
                // hide all other topics
		       xell.className = "HelpTopicHidden"; 
		 //      alert("hide div: " + xid);
		      }
		     }
		    }
	      }
	      return;
        }
        
        function HideDivWithPatt(patt) {
          
          var maindiv = document.getElementById('main');
          var allells = maindiv.getElementsByTagName('DIV');
          var xell;
          var xid;
          var divtype;
         // alert("HideDivWithPatt1 allells.length" + allells.length);
          
          for(var i=0; i < allells.length; i++)
	      {
		    xell = allells[i]; 
		    xid = new String();
		    xid = xell.getAttribute('id');
		 //   alert("HideDivWithPatt2=" + i);
		    if (xid){
		
		      if (xid.substring(0, patt.length) == patt){
		         xell.className = "HelpTopicHidden"; 
		      }
		    }
	      }
	      return;
        }
        
                
        function DisplayTopicNoSinc(TopicID, el) {
          //alert ("Displaytopic=" + TopicID);
         
          //selectNode(el); 
          if (el != null){
            el.className = "treeSelected";
            alert("el.className = " + el.className);
          }
          
          if (TopicID < 1) {return;}
          if (TopicIdCur == TopicID) {return;}
          
          TopicIdPrev = TopicIdCur;
          TopicIdCur = TopicID;
          
          DisplayDivWithPatt (TopicID,'topicid_');
          
        }
        
        function PrintThisPage() {
            

		    var titlebar =	document.getElementById('tbar1');
		    if (titlebar != null) titlebar.className = "navHide"; 
		 
		    var mainnav =	document.getElementById('mainnav');
		    if (mainnav != null) mainnav.className = "navHide"; 
		    
		    var secondnav =	document.getElementById('secondnav');
		    if (secondnav != null) secondnav.className = "navHide"; 
		    
			document.getElementById("printstrip").className = "navHide";

			var sidemenu = document.getElementById('sidemenu');
			if (sidemenu != null) {sidemenu.className = "navHide"};

			var secondnav = document.getElementById('secondnav');
			if (secondnav != null) {secondnav.className = "navHide"};
			
			var printstrip = document.getElementById('printstrip');
			if (printstrip != null) {printstrip.className = "navHide"};

			var dvRestoreNav = document.getElementById('dvRestoreNav');
			if (dvRestoreNav != null) {dvRestoreNav.className = "navShow"};
			


			var header = document.getElementById('header');
			if (header == null){
			
				window.print();
				return;
			}

			// help page elements
			header.className = "navHide";
			var DivToHide = "helpindex";
			if (document.getElementById('helpindex').className == "navShow"){
				DivToHide = "helpcontents"
			}
			
			
			document.getElementById('helpindex').className = "navHide"; // "HelpTopicHidden";
			document.getElementById('helpcontents').className = "navHide"; //"HelpTopicHidden";
    
    
			window.print();
    
			RestorePageNavigation();
            
			document.getElementById(DivToHide).className = "navHide";
			
			redrawHelp (true);
			
		}
		
		function PrintPopTopic() {
		    
		    var div =	document.getElementById('PrintPage');
		    if (div != null) div.className = "navHide"; 
		 
			window.print();
			
		}
  
		function RestorePageNavigation() {

			
     	    var titlebar =	document.getElementById('tbar1');
		    if (titlebar != null) titlebar.className = "navShow"; 
		 
		    var mainnav =	document.getElementById('mainnav');
		    if (mainnav != null) mainnav.className = "navShow"; 
		    
		    var secondnav =	document.getElementById('secondnav');
		    if (secondnav != null) secondnav.className = "navShow"; 


			document.getElementById("printstrip").className = "navShow";
    		
    
			var sidemenu = document.getElementById('sidemenu');
			if (sidemenu != null) {sidemenu.className = "navShow"};

            var dvRestoreNav = document.getElementById('dvRestoreNav');
			if (dvRestoreNav != null) {dvRestoreNav.className = "navHide"};
			
			var printstrip = document.getElementById('printstrip');
			if (printstrip != null) {printstrip.className = "printstrip"};
			
			
			// help page elements
			var header = document.getElementById('header');
			if (header == null) return;
    
			header.className = "navShow";
			document.getElementById('helpindex').className = "navShow"; 
			document.getElementById('helpcontents').className = "navShow";
			document.getElementById('header').className = "navShow";

		}
  
		function RecalculateRate(ProductID) {

			var termid = parseInt(document.getElementById(ProductID + '_termid').value); 
			var quant = document.getElementById(ProductID + '_quant').value; 
			var baseprice = parseFloat(document.getElementById(ProductID + '_unitprice').value); 

			if (quant == '') {
			  quant = 1;
			}
			else {
			   quant = parseInt(quant);
			
        	   if (quant < 1) {
		    	  quant = 1;
			   }
			}
			document.getElementById(ProductID + '_quant').value = quant;

            var offset = (termid - 1) * 0.1;

    		if (quant < 4){discrate = 1 - offset;}
			if (quant > 3 && quant <= 10){ discrate = 0.9 - offset;}
			if (quant > 10){discrate = 0.8 - offset;}
			
    alert("RecalculateRate 4");			
    
			var total = '$' + Math.round(termid * quant * baseprice * discrate*100)/100;  
			var unitprice =  '$' + Math.round(baseprice * discrate*100)/100 + ' per year';  
    alert("RecalculateRate 5");			
			var percentoff;
    
			if (discrate < 1) {
				percentoff =  ' (' + Math.round((1 - discrate) * 100) + '% off)';
				total = total + percentoff;
			}
			else {
				percentoff = 'none';
			}
			
			
			var divtotal = document.getElementById(ProductID + '_total');
        
			if (divtotal != null){

				var newNode=document.createTextNode(total);
				if (divtotal.childNodes[0] != null) 
			    	divtotal.replaceChild(newNode,divtotal.childNodes[0]);
				else {
				  divtotal.appendChild(newNode);
			   }
			}
			
			return false;
        }
		
	    
  //==========  scroller functions ==========================================
	
  function SetScrollerToTopic(ScrollerId,TopicOffset) {

        
         if (TopicOffset > 0) {
           var scroller = document.getElementById(ScrollerId);
           
           var scrollerHeight = scroller.style.height;
           
           scrollerHeight = scrollerHeight.replace("px","");
           scrollerHeight = scrollerHeight.replace("pc","");
           scrollerHeight = scrollerHeight.replace("pt","");
           scrollerHeight = scrollerHeight.replace("mm","");
           
           var topMargin = 25;
           
        //   alert("scrollTop:" + scroller.scrollTop + " scrollerHeight:" + scrollerHeight + " TopicOffset" + TopicOffset);
           if ((TopicOffset > (scroller.scrollTop + scrollerHeight)) || (TopicOffset < (scroller.scrollTop + topMargin)))
        
              scroller.scrollTop = TopicOffset - topMargin;
         }     
  }
        
        		
  function SetScrollerToDiv(divId,scrollerDivId){
	// modified from http://www.webdeveloper.com/forum/archive/index.php/t-39750.html
	// scroll to div code lines
	var speed=80; //time to loop in milliseconds
	var sy=50; //nr pixels srolled/loop


	//check if div position is above or below the body srollTop
	var scroller = document.getElementById(scrollerDivId);
   
	//alert("SetScrollerToDiv" + scroller.scrollTop);
   
	if(document.getElementById(divId).offsetTop<scroller.scrollTop){
      window.scrollBy(0,-sy);
     
     if(document.getElementById(divId).offsetTop>=scroller.scrollTop){
        scroller.scrollTop=document.getElementById(divId).offsetTop;
        clearTimeout(w);
     }
     else {
      var w=setTimeout('SetScrollerToDiv(\''+divId+'\',\''+scrollerDivId+'\')',speed);
     }
    }
    else{
     window.scrollBy(0,sy);
     if(document.getElementById(divId).offsetTop<=scroller.scrollTop){
       scroller.scrollTop=document.getElementById(divId).offsetTop;
       clearTimeout(w);
     }
     else{
      var w=setTimeout('SetScrollerToDiv(\''+divId+'\',\''+scrollerDivId+'\')',speed);
     }
    }
  }
