// calculate product price from area selected.
// product priced per square or linear metre

// V1.03

var vatfactor = 1;		// Set to 1.175 if we want VAT inc pricing displayed. Use 1 for vat exclusive prices

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, ProductPriceRaw, MinWidth, MaxWidth, MinLength, MaxLength, SellByExactArea){		// called when product loads - saves some useful info		
  prodinfo[pid] = new Object();
  prodinfo[pid].pricepermetre = ProductPriceRaw;
  prodinfo[pid].minwidth = MinWidth;
  prodinfo[pid].maxwidth = MaxWidth;
  prodinfo[pid].minlength = MinLength;
  prodinfo[pid].maxlength = MaxLength;
  prodinfo[pid].sellbyexactarea = SellByExactArea;
}

// 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('wid_' + pid).value;	// customer entered width
  var length = document.getElementById('len_' + pid).value;	// customer entered length
  var validcount = 0;
  if ( (width != '') &&  ! isNaN(width) )
    {
    if ( width <  prodinfo[pid].minwidth )
      {
      alert('Width must be more than ' + CommaFormatted(prodinfo[pid].minwidth + 'cm'));
      }
    else
      {
      validcount++;
      }
    if ( width > prodinfo[pid].maxwidth )
      {
      alert('Width must be less than ' + CommaFormatted(prodinfo[pid].maxwidth + 'cm'));
      }
    else
      {
      validcount++;
      }
    }
  if ( (length != '') && ! isNaN(length) )
    {
    if ( length <  prodinfo[pid].minlength )
      {
      alert('Length must be more than ' + CommaFormatted(prodinfo[pid].minlength + 'cm'));
      }
    else
      {
      validcount++;
      }
    if ( length >  prodinfo[pid].maxlength )
      {
      alert('Length must be less than ' + CommaFormatted(prodinfo[pid].maxlength + 'cm'));
      }
    else
      {
      validcount++;
      }
    }
  if ( validcount == 4 )
    {
    // see if selling by area or length
    if ( prodinfo[pid].sellbyexactarea )		// by exact area - product priced per square metre
    	{
    	var price = prodinfo[pid].pricepermetre * (length / 100) * (width / 100);
    	}
    else						// by length required - product priced per linear metre
     	{
    	var price = prodinfo[pid].pricepermetre * length / 100;
    	}
    
    price = price.toFixed(2);
    // price override info for cart. Format price: then any number of informative name;value;suffix separated by :
    // e.g.
    // 12.34:Width;1,200;mm:Length;3,456;mm
    document.getElementById('ovr_' + pid).value = price + ':' + 'Width;' + CommaFormatted(width) + ';cm:' + 'Length;' + CommaFormatted(length) + ';cm';
    // display VAT inclusive if required - NB Actinic bug in TaxTreament prevents us using this
    if ( vatfactor != 1 ) price = (price * vatfactor).toFixed(2);   
    document.getElementById('prc_' + pid).innerHTML = price;  
    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('wid_' + pid).value = prodinfo[pid].minwidth;
    document.getElementById('len_' + pid).value = prodinfo[pid].minlength;
    calcprice(pid);
    }
}

// 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);
