// START norpricesupport.js V1.25 
var havealtprice = false;
var choicealtprice = false;
var allcookies = document.cookie;		// if favourite currency set then use it
var pos = allcookies.indexOf('destcurr=');
if ( pos != -1 )
    {
    var start = pos + 9;
    var end = allcookies.indexOf(';', start);
    if ( end == -1 ) end = allcookies.length;
    var value = unescape(allcookies.substring(start,end));
    var bits = value.split(':');
    var isoname = bits[0];
    var base = bits[1];
    var dest = bits[2];
    var exchange = base / dest;
    var cgibase = cgiurl.replace(/(.*:\/\/)?(.*\/).*/, "$2");
    if ( (isoname != hidemain) && (isoname != hidealt) && (dest > 0) 
      && (location.href.indexOf(cgibase + 'bb') == -1) 
      && (location.href.indexOf(cgibase + 'ca') == -1) ) havealtprice = true;
    if ( (isoname != hidemain) && (isoname != hidealt) && (dest > 0) ) choicealtprice = true;
    }

function CommaFormatted(num){
  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 topounds(nPounds)		// necessary as some browsers still don't support toFixed()
  {
  var nWholeCurrency, nFraction, sFraction;
  var sign = '';
  if ( nPounds < 0 )
    {
    nPounds = Math.abs(nPounds);
    sign = '-';
    } 
  nWholeCurrency = Math.floor(nPounds);                		// no of pounds
  nFraction = Math.round((nPounds - nWholeCurrency) * 100);  	// Number of Fraction (2 decimal places)
  if ( nFraction > 99 ) nFraction = 99;
  if (nFraction < 10)                                  		// Needs to be two digits
    {
    sFraction = "0" + nFraction.toString();            		// Pad with leading zero
    }
  else
    {                                                 		// Already has two digits
    sFraction = nFraction.toString();                  		// So just format it
    }
  return(sign + nWholeCurrency.toString() + '.' + sFraction);
  }

function showalt(price){
if ( havealtprice )
 {
 document.write(getalt(price, true)); // show a formatted price
 }  
}

function getalt(price, formatted){	// return converted price with optional HTML formatting
 var prefix = formatted ? '<span class="altprice">' : '';  // see if we want HTML formatting
 var suffix = formatted ? '</span>' : '';                  //    or just plain text
 var optplus = price.match(/\+/) ? '+' : '';	// remember if leading "+"
 price = price.replace(/&#44;/g,"");     	// remove all commas
 price = price.replace(/&#46;/g,".");    	// restore decimal point
 var pbits = price.match(/(-?\d+\.\d\d)/);	// now look for first [-]n.nn 
 if ( pbits != null )
   { 
   var numprice = pbits[1];
   var altprice = numprice * exchange;
// remove comment below to show non EU currencies ex-VAT
//   if ( 'EUR-DKK-SEK-GBP'.indexOf(isoname) == -1 ) altprice = altprice / 1.175;
   if ( exchange > hidefraction )
     {
     return prefix + ' (' + optplus + CommaFormatted(Math.round(altprice)) + ' ' + isoname + ')' + suffix;
     }
   else
     {
     return prefix + ' (' + optplus + CommaFormatted(topounds(altprice)) + ' ' + isoname + ')' + suffix;
     }
   }
 return ' --- ';  // should never get here but just in case...
}

// now code to deal with prices embedded in choice description
var selcode;
var pattern = new RegExp('(.*)(' + mainsymbol + ')(\\d+\\.\\d\\d)(.*)');	// match for e.g. £12.34

function cnvtext(text, formatted){
  if ( ! choicealtprice ) return text;
  //
  // this routine could be changed to allow for different ways of including the option price.
  //
  text = text.replace(/&#(\d+);/g, function(m,s){return String.fromCharCode(s - 0)});	// remove Actinic escaping
  var val = text.match(/(.*)\((.*)\)(.*)/);		// have we a "(" and ")" in the text?
  if ( val != null )
    {
    var thisval = val[2];				// extract the value
    thisval = thisval.replace(/[^-\d\.\+]/g,"");	// remove everything but digits, +, - and . 
    if ( isNaN(thisval) ) return text;			// ignore invalid numbers
    return val[1] + '(' + val[2] + ')' + getalt(thisval, formatted) + val[3];	// return potentially converted text
    }
  else
    {
    val = text.match(pattern);				// have we currency symbol then n.nn
    if ( val != null )
      {
      var sign = ''
      var spos = val[1].match(/([-\+])$/);		// see if there was a + or -
      if ( spos != null ) sign = spos[1];
      var thisval = sign + val[3];			// create the optionally signed value
      if ( isNaN(thisval) ) return text;		// ignore invalid numbers
      return val[1] + val[2] + val[3] + getalt(thisval, formatted) + val[4];	// return potentially converted text
      }
    return text;					// return unaltered text
    }
}

function xcheck(text){		// for CHECKBOX and RADIO items (V7)
  document.write(cnvtext(text, true));
}

function xselend(id){						// called from Act_VariantListFooter.html (V7)
  if ( ! havealtprice ) return;					// abandon if no alt price
  var opts = document.getElementById('np-' + id).options; 	// the <select> statement in question
  for ( i = 0; i < opts.length; i++ )				// for each option
    {
    opts[i].text = cnvtext(opts[i].text, false);		// add in the alt price if required
    }
}

// V8 code
function pricelabels(){
  if ( ! choicealtprice ) return;
  var choicelabels = document.getElementsByTagName('span');
  for ( var i=0; i < choicelabels.length; i++ )
    {
    if ( choicelabels[i].className == 'NorPrice' )
      {
      choicelabels[i].innerHTML = cnvtext(choicelabels[i].innerHTML, true);
      }
    }
  choicelabels = document.getElementsByTagName('option');
  for ( i=0; i < choicelabels.length; i++ )
    {
    if ( choicelabels[i].className == 'NorPrice' )
      {
      choicelabels[i].innerHTML = cnvtext(choicelabels[i].innerHTML, false);
      }
    }
}
// END norpricesupport.js
