// Version 1.01
var prodinfo = new Object();

function CommaFormatted(num){				// Comma format a number.
  var sep = ',';
  num = num.toString().split('').reverse().join('');		// reverse number
  num = num.replace(/(\d\d\d)(?=\d)(?!\d*\.)/g,'$1' + sep);	// add commas
  return num.split('').reverse().join('');			// reverse number back;
}

function saveproductinfo(pid, group){		// called when product loads - saves some useful info		
  prodinfo[pid] = new Object();
  prodinfo[pid].group = group;
  prodinfo[pid].minwidth = srg[group].widths[0];
  prodinfo[pid].maxwidth = srg[group].widths[srg[group].widths.length-1];
  prodinfo[pid].mindrop = srg[group].drops[0][0];
  prodinfo[pid].maxdrop = srg[group].drops[srg[group].drops.length-1][0]
}

function generatepricematrix(pid){		// generatediagnostic price table
  var group = prodinfo[pid].group;
  var widths = srg[group].widths;
  var html = '';
  html += '<table cellpadding=1 cellspacing=1 style="border: solid 1px silver;">';
  html += '<tr bgcolor=#dddddd><td bgcolor=#eeeeee align=center>' + group + '</td><td align=center colspan=' + widths.length + '>Width (all dimensions in cm).</td></tr>'; 
  html += '<tr bgcolor=#dddddd align=right><td bgcolor=#cccccc align=center>Drop</td>';
  for (i=0; i<widths.length; i++) html += '<td>' + widths[i] + '</td>';
  html += '</tr>';
  var drops = srg[group].drops;
  for (i=0; i<drops.length; i++)
    {
    html += '<tr bgcolor=#eeeeee align=right><td  bgcolor=#cccccc>' + drops[i][0] + '</td>';
    for (j=1; j<drops[i].length; j++) html += '<td style="font-size: 8px;">' + drops[i][j].toFixed(2) + '</td>';
    html += '</tr>';
    }
  html += '</table>';
  return html;
}

function displaypricematrix(pid){		// display diagnostic price table
  document.write(generatepricematrix(pid));
}

function requiredwidth(pid, width){		// assumes valid width - returns size required
  var group = prodinfo[pid].group;
  var widths = srg[group].widths;
  var widthindex = 0;
  // scan widths for minimum needed
  for (i=0; i<widths.length; i++)
    {
    if ( widths[i] < width ) widthindex++;
    } 
  return widths[widthindex];
}
  
function requireddrop(pid, drop){		// assumes valid drop - returns size required
  var group = prodinfo[pid].group;
  // now scan drops for minimum needed - 1st entry is drop size  
  var drops = srg[group].drops;  
  var dropindex = 0;
  for (i=0; i<drops.length; i++)
    {
    if ( drops[i][0] < drop ) dropindex++;
    } 
  return drops[dropindex][0]; 
}

function lookupprice(pid, width, drop){		// assumes valid width and drop - returns price required
  var group = prodinfo[pid].group;
  var widths = srg[group].widths;
  var widthindex = 0;
  // scan widths for minimum needed
  for (i=0; i<widths.length; i++)
    {
    if ( widths[i] < width ) widthindex++;
    } 
  // now scan drops for minimum needed - 1st entry is drop size  
  var drops = srg[group].drops;  
  var dropindex = 0;
  for (i=0; i<drops.length; i++)
    {
    if ( drops[i][0] < drop ) dropindex++;
    } 
  return drops[dropindex][widthindex + 1]; 
}

// main calculation routine - called when sizes change and before add to cart
// checks values entered, looks up price, displays warnings if error, displays price, sets form data, returns true / false
function calcprice(pid){
  var width = document.getElementById('wdt_' + pid).value;	// customer entered width
  var drop = document.getElementById('drp_' + pid).value;	// customer entered drop
  var validcount = 0;
  if ( (width != '') &&  ! isNaN(width) )
    {
    if ( width <  prodinfo[pid].minwidth )
      {
      alert('Width must be more than ' + prodinfo[pid].minwidth + 'cm');
      }
    else
      {
      validcount++;
      }
    if ( width > prodinfo[pid].maxwidth )
      {
      alert('Width must be less than ' + prodinfo[pid].maxwidth + 'cm');
      }
    else
      {
      validcount++;
      }
    }
  if ( (drop != '') && ! isNaN(drop) )
    {
    if ( drop <  prodinfo[pid].mindrop )
      {
      alert('Drop must be more than ' + prodinfo[pid].mindrop + 'cm');
      }
    else
      {
      validcount++;
      }
    if ( drop >  prodinfo[pid].maxdrop )
      {
      alert('Drop must be less than ' + prodinfo[pid].maxdrop + 'cm');
      }
    else
      {
      validcount++;
      }
    }
  if ( validcount == 4 )
    {
    var price = lookupprice(pid, width, drop) ;
    if ( price == 0 )
      {
      alert('That combination is only available in a wider size');
      document.getElementById('prc_' + pid).innerHTML = '';  
      document.getElementById('ovr_' + pid).value = '';  
      return false;
      }
    price = price.toFixed(2);
    document.getElementById('prc_' + pid).innerHTML = price;  
    //  pass details to Price Override V1.03 scripts  
    document.getElementById('ovr_' + pid).value = price + ':Width;' + width + ';cm:Drop;' + drop + ';cm';   
    return true;
    }
  document.getElementById('prc_' + pid).innerHTML = '';  
  document.getElementById('ovr_' + pid).value = '';  
  return false;  
} 

function checksizes(pid){		// called when form submitted
  return calcprice(pid);
} 

function refreshprices(){		// called on page load.  Refresh any prices.
  for ( pid in prodinfo )
    {
    document.getElementById('wdt_' + pid).value = prodinfo[pid].minwidth;
    document.getElementById('drp_' + pid).value = prodinfo[pid].mindrop;
    calcprice(pid);
    }
}

function toggletable(button, pid){	// toggle display of pricing table
  if ( button.value.indexOf('Display') > -1 )
    {
    document.getElementById('tbl_' + pid).innerHTML = generatepricematrix(pid);
    button.value = 'Hide price table';
    }
  else
    {
    document.getElementById('tbl_' + pid).innerHTML = '&nbsp;';
    button.value = 'Display price table';
    }
    
}

// in case we cannot activate on DOM loaded
if (window.attachEvent) 						// IE 
	{ 
	window.attachEvent("onload", refreshprices); 
	} 
else 									// DOM
	{  
	window.addEventListener("load", refreshprices, 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);}})();}})(refreshprices);