// START norpricesupport.js jQuery V2.10
var havealtprice = false;
var choicealtprice = false;
var onlyscanonce = 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.2;
   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;

if ( mainsymbol == '$' ) mainsymbol = '\\' + mainsymbol;			// escape dollar
var pattern = new RegExp('(.*)(' + mainsymbol + ')([\\d,]+\\.\\d\\d)(.*)');	// match for e.g. £12.34 or £1,234.56
function cnvtext(text, formatted){
  if ( ! choicealtprice ) return text;			// no conversion needed
  //
  // 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
  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].replace(/,/g,'');	// 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
}

// V2 jQuery method
function pricelabels(){
  if ( onlyscanonce ) return;
  onlyscanonce = true;
  if ( ! choicealtprice ) return;
  $("span[class='NorPrice']").each(function(index) { $(this).html(cnvtext($(this).html(), true)) });	// normal inline prices
  $("option[class='NorPrice']").each(function(index) { this.text = cnvtext(this.text, false) });	// option tags
}

$(document).ready(function() { pricelabels(); });	// do convert on page reload
// END norpricesupport.js

