// support code for tabbed product descriptions V2.07 - V1.02
// modified to work on Actinic V7
// replace NETQUOTEVAR:PRODUCTDESCRIPTION
// with <div id="tbpd_NETQUOTEVAR:PRODUCTANCHOR">NETQUOTEVAR:PRODUCTDESCRIPTION</div>

// optional field: <div id="tbtb_NETQUOTEVAR:PRODUCTANCHOR"></div>
// will be replaced by tabs (and ProductDescription area will contain any prefix text.

// optional field: <div id="tbaf_NETQUOTEVAR:PRODUCTANCHOR"></div>
// will be replaced by any text that follows tabs (after {}).

// Additional code to format price table

function  rebuildpagewithpricetable(){		// scan DOM for our product descriptions and build price table if required
  var divs = document.getElementsByTagName('div');
  for ( var i=0; i < divs.length; i++ )
    {
    thisdiv = divs[i];
    if ( thisdiv.id )
      {
      if ( thisdiv.id.indexOf('prce_') == 0 )	// our ProductPrice area
        {
        var qtys = new Array();
        var costs = new Array();
        var incvat = new Array();
        var listprice = thisdiv.getAttribute('listprice');
	var spans = thisdiv.getElementsByTagName('span');
	for ( var j=0; j < spans.length; j++ )
	  {
	  var thisspan = spans[j];
	  if ( thisspan.id.indexOf('dscqy') > -1 ) qtys.push(thisspan.innerHTML);
	  if ( thisspan.id.indexOf('dscco') > -1 ) costs.push(thisspan.innerHTML);
	  if ( thisspan.id.indexOf('dsctx') > -1 ) incvat.push(thisspan.innerHTML);
	  }
	if ( qtys.length > 1 )			// we have multiple prices
	  {
	  var tablestart = '<table class="pricetable">';
	  var tablerow1 = '<tr><td class="pricehead"> </td>';
	  var tablerow2 = '<tr><td class="priceinfo">Ex VAT</td>';
	  var tablerow3 = '<tr><td class="priceinfo">Inc VAT</td>';

	  for ( var k=0; k < qtys.length; k++ )
	    {
	    qtybits = qtys[k].match(/\d+/);
	    thisqty = ( k == 0) ? '1+' : qtybits[0] + '+';
	    tablerow1 += '<td class="pricehead">' + thisqty + '</td>';
	    tablerow2 += '<td>' + costs[k] + '</td>';
	    tablerow3 += '<td>' + incvat[k].replace(/[&nbsp;|\s+].*/,'') + '</td>';
	    }
          tablerow1 += '</tr>';
	  tablerow2 += '</tr>';
	  tablerow3 += '</tr>';
	  var tableend = '</table>';
	  thisdiv.innerHTML = tablestart + tablerow1 + tablerow2 + tablerow3 + tableend; 
	  }  
        }
      }
    }
}
    
var pids = new Array();
var panes = new Array();
var bTabberSet = false;

function  rebuildpagewithtabs(){		// scan DOM for our poroduct descriptions and build tab HTML if required
  var divs = document.getElementsByTagName('div');
  for ( var i=0; i < divs.length; i++ )
    {
    thisdiv = divs[i];
    if ( thisdiv.id )
      {
      if ( thisdiv.id.indexOf('tbpd_') == 0 )
        {
        var anc = thisdiv.id.substring(5);
        var tabsdiv = document.getElementById('tbtb_' + anc);
        var afterdiv = document.getElementById('tbaf_' + anc);
        proddesc = thisdiv.innerHTML;
        var firstopenbrace = proddesc.indexOf('{');
        var firstclosingbrace = proddesc.indexOf('}');
	if ( (firstopenbrace > -1) && (firstclosingbrace > firstopenbrace) ) // is there at least one tab?
	  {
	  var panecount = 0;
	  var havepanes = true;
	  var tabhtml = '<div class="tab-panes">';
	  var before = '';
	  if ( firstopenbrace > 0 ) before = proddesc.substring(0, firstopenbrace);
	  var after = '';
	  var tabcode = proddesc.substring(firstopenbrace);
	  var doublebraces = tabcode.indexOf('{}')
	  if ( doublebraces > -1)	// optional {} signifies end of tabbed text
	    {
	    after = tabcode.substring(doublebraces + 2);
	    tabcode = tabcode.substring(0, doublebraces);
	    }
          newcode = tabsdiv ? '' : before; 
	  newcode += '<br clear="all" /><div id="tab-header">';
          newcode += '<ul id="ul_' + anc + '">';
          while ( (tabcode.indexOf('{') > -1) && tabcode.indexOf('}') > tabcode.indexOf('{') )
		{
		var tabname = tabcode.substring(tabcode.indexOf('{') + 1, tabcode.indexOf('}'));
                var tabtext = tabcode.substring(tabcode.indexOf('}') + 1);
		tabcode = '';
		if ( (tabtext.indexOf('{') > -1) && tabtext.indexOf('}') > tabtext.indexOf('{') )	// more tabs?
			{
			tabcode = tabtext.substring(tabtext.indexOf('{'));		// next tab
			tabtext = tabtext.substring(0, tabtext.indexOf('{'));		// text up to next tab
			}
		// output current tab
		newcode += '<li id="';
		if ( panecount == 0) 	// mark first tab so we can set it active
			{
			newcode += 'tab-current';
			}
		newcode += '"><a href="javascript:void(0)" onclick="return swaptabs(\'' + anc + '\',' + panecount + ')">';
		newcode += tabname + '</a></li>';
		tabhtml += '<div id="pane_' + anc + '_' + panecount + '">' + tabtext + '</div>';			// save the contents of the tab for later
		panecount++;
		}
	  newcode += '</ul></div><br/><div class="tab-container" id="cont_' + anc + '">' + tabhtml + '</div></div>';	// output all the tab text
          newcode += afterdiv ? '' : after; 
	  pids.push(anc);		// save list of tabs to activate on page load
	  if ( tabsdiv ) 
	    {
	    tabsdiv.innerHTML = newcode;
	    thisdiv.innerHTML = before;
	    }
	  else
	    {
	    thisdiv.innerHTML = newcode;
	    } 
	  if ( afterdiv ) afterdiv.innerHTML = after;  
	  }
        }
      }
    }
}

function swaptabs(ulid, seq){
  // make tab active class
  var litems = document.getElementById('ul_' + ulid).getElementsByTagName('li');
  for (var i=0; i<litems.length; i++)
    {
    litems[i].id = (i == seq) ? 'tab-current' : '';
    }
  // hide other panes (siblings)
  // make pane visible
  paneId = 'pane_' + ulid + '_' + seq;
  for (var con in panes) {
  if (panes[con][paneId] != null) { // tab and pane are members of this container
      var pane = document.getElementById(paneId);
      pane.style.display = "block";
      var container = document.getElementById(con);
      for (var i in panes[con]) {
        var pane = panes[con][i];
        if (pane == undefined) continue;
        if (pane.id == paneId) continue;
        if ( pane.style && pane.style.display ) pane.style.display = "none";
      }
    }
  }
  return false;
}

function setupPanes(Id) {
  // go through the DOM, find each tab-container
  // set up the panes array with named panes
  // find the max height, set tab-panes to that height
  var containerId = 'cont_' + Id;
  panes[containerId] = new Array();
  var maxHeight = 0; var maxWidth = 0;
  var container = document.getElementById(containerId);
  var paneContainer = container.getElementsByTagName("div")[0];
  var paneList = paneContainer.childNodes;
  for (var i=0; i < paneList.length; i++ ) {
    var pane = paneList[i];
    if (pane.nodeType != 1) continue;
    if (pane.offsetHeight > maxHeight) maxHeight = pane.offsetHeight;
    if (pane.offsetWidth  > maxWidth ) maxWidth  = pane.offsetWidth;
    panes[containerId][pane.id] = pane;
    pane.style.display = "none";
  }
    paneContainer.style.height = maxHeight + "px";
    paneContainer.style.width  = maxWidth + "px";
    // activate first tab
    swaptabs(Id, 0);
}

function setallpanes(){
// called on page load - set up all tab containers
  if ( bTabberSet ) return;		// only do once
  bTabberSet = true;
  rebuildpagewithtabs();
  rebuildpagewithpricetable();
  for ( var i=0; i<pids.length; i++ )
    {
    setupPanes(pids[i]); 
    }
}

// in case we cannot activate on DOM loaded
if (window.attachEvent) 						// IE 
	{ 
	window.attachEvent("onload", setallpanes); 
	} 
else 									// DOM
	{  
	window.addEventListener("load", setallpanes, false); 
	}

// DOM Ready detect based on www.kryogenix.org/days/2007/09/26/shortloaded
(function(i) {var u =navigator.userAgent;var e=/*@cc_on!@*/false; var st =
setTimeout;if(/webkit/i.test(u)){st(function(){var dr=document.readyState;
if(dr=="loaded"||dr=="complete"){i()}else{st(arguments.callee,10);}},10);}
else if((/mozilla/i.test(u)&&!/(compati)/.test(u)) || (/opera/i.test(u))){
document.addEventListener("DOMContentLoaded",i,false); } else if(e){     (
function(){var t=document.createElement('doc:rdy');try{t.doScroll('left');
i();t=null;}catch(e){st(arguments.callee,0);}})();}})(setallpanes);

