// Currency Changer v1.0
// Copyright (C) 2005 Digital Routes, Scotland
// http://digitalroutes.co.uk/library
// This code is licensed under the GPL, see www.gnu.org for details.

// Converts currencies on a webpage.

// Include somewhere within the <BODY> of your webpage:
// <SCRIPT LANGUAGE='JavaScript1.2' SRC='http://digitalroutes.co.uk/currency/currency.js'></SCRIPT>
// <SCRIPT LANGUAGE='JavaScript1.2' SRC='http://digitalroutes.co.uk/currency/currencychange.js'></SCRIPT>


// Locate all the document's currency spans.
function currencychange_init() {
  var unit, value;
  var spans = document.getElementsByTagName('SPAN');
  for (var x=0; x<spans.length; x++) {
    if (spans[x].className.indexOf("currency_") == 0) {
      unit = spans[x].className.substr(9);
      value = currencychange_dom2txt(spans[x]);
      value = value.replace(/[^0-9.]/, '');
      value = parseFloat(value);
      if (!isNaN(value))
        currencychange_spans[currencychange_spans.length] = [spans[x], unit, value];
    }
  }
  currencychange_convert(currencychange_getcurrency());
}


// Look up the user's currency in a cookie.
function currencychange_getcurrency() {
  var dc = document.cookie;
  var prefix = "currency=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  begin = begin + prefix.length;
  return unescape(dc.substring(begin, end));
}


// Store the user's currency in a cookie.
function currencychange_setcurrency(unit) {
  var date = new Date();
  date.setTime(date.getTime()+(30*24*60*60*1000));
  document.cookie = "currency="+escape(unit)+"; expires="+date.toGMTString()+"; path=/;"
}


// Write a dropdown selection list for currencies.
function currencychange_dropdown() {
  document.write("<SELECT onChange='currencychange_setcurrency(this.options[this.selectedIndex].value);currencychange_convert(this.options[this.selectedIndex].value)' NAME='currencychange_dropdown' ID='currencychange_dropdown'>");
  var code, text, div, selected;
  var currency = currencychange_getcurrency();
  for(var i=0; i<arguments.length; i++) {
    div = arguments[i].indexOf(' ')
    if (div == -1) {
      code = text = arguments[i];
    } else {
      code = arguments[i].substr(0, div);
      text = arguments[i].substr(div+1);
    }
    selected = (currency == code) ? "SELECTED" : ""
    document.write("<OPTION VALUE='"+code+"' "+selected+">"+text+"</OPTION>");
  }
  document.write("</SELECT>");
}


// Change all prices to the desired type.
function currencychange_convert(totype) {
  if (!totype)
    return;
  var span, unit, value;
  for (var x=0; x<currencychange_spans.length; x++) {
    span = currencychange_spans[x][0];
    unit = currencychange_spans[x][1];
    value = currencychange_spans[x][2];
    span.innerHTML = currency_string(value, unit, totype);
  }
}


// Recursively build a plain-text version of a DOM structure.
// Bug: whitespace isn't always correct, but shouldn't matter for currencychange.
function currencychange_dom2txt(obj) {
  var text = "";
  if (!obj) return "";
  if (obj.nodeType==3)
    text = obj.data;
  else
    for (var x=0; x<obj.childNodes.length; x++)
      text = text + currencychange_dom2txt(obj.childNodes[x]);
  return text;
}


// Add the initialisation call to the body's onload function.
function currencychange_onload() {
  var f = '';
  if (window.onload) {
    f = window.onload.toString();
    f = f.substring(f.indexOf('{')+1, f.lastIndexOf('}'));
  }
  window.onload = new Function('currencychange_init();'+f);
}


var currencychange_spans = []; // List of currency spans;

currencychange_onload()
