function copy(to,str){
        document.getElementById(to).innerHTML = str;
}

function init(){
        loadParams();
        getDeliveryPrice();
}

function loadParams(){
        copy('name_label',document.getElementById('Name').value);
        copy('address_label',document.getElementById('Address').value);
        copy('postalcode_label',document.getElementById('PostalCode').value)
        copy('deliverymessage_label',document.getElementById('DeliveryMessage').value);
        copy('instructionsforcourier_label',document.getElementById('InstructionsForCourier').value);
}

function getDeliveryPrice(){
        var params = "PostalCode=" + document.getElementById('PostalCode').value;
        new Ajax.Request('/cgi-bin/ajax/getdeliveryprice.pl', {
        parameters: params,
        onSuccess: function(transport) {
              copy('delivery',transport.responseText);
              calculate();
        }
        });
}

function checkAll(field)
{
for (i = 0; i < field.length; i++)
        field[i].checked = true ;
}

function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
        field[i].checked = false ;
}

function clickextra(id){
        if (document.getElementById('extrachecked').value == id){
                uncheckAll(document.afpForm.extra);
                document.getElementById('extrachecked').value = 0;
        }else{
                uncheckAll(document.afpForm.extra);
                document.getElementById(id).checked=true;
                document.getElementById('extrachecked').value = id;
        }
        calculate();
}

function calculate(){
        var productprice = document.getElementById('Price'+document.getElementById('productprice').value).value;
        var deliveryprice = document.getElementById('delivery').innerHTML;
        var extraprice = document.getElementById('price'+document.getElementById('extrachecked').value).value;
        var alltotal = productprice*1 + deliveryprice*1 + extraprice*1;
        copy('alltotal',sprintf('%.2f',alltotal));
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}


function sprintf () {
  var i = 0, a, f = arguments[i++], o = [], m, p, c, x;
  while (f) {
    if (m = /^[^\x25]+/.exec(f)) o.push(m[0]);
    else if (m = /^\x25{2}/.exec(f)) o.push('%');
    else if (m = /^\x25(?:(\d+)\$)?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(f)) {
      if (((a = arguments[m[1] || i++]) == null) || (a == undefined)) throw("Too few arguments.");
      if (/[^s]/.test(m[7]) && (typeof(a) != 'number'))
        throw("Expecting number but found " + typeof(a));
      switch (m[7]) {
        case 'b': a = a.toString(2); break;
        case 'c': a = String.fromCharCode(a); break;
        case 'd': a = parseInt(a); break;
        case 'e': a = m[6] ? a.toExponential(m[6]) : a.toExponential(); break;
        case 'f': a = m[6] ? parseFloat(a).toFixed(m[6]) : parseFloat(a); break;
        case 'o': a = a.toString(8); break;
        case 's': a = ((a = String(a)) && m[6] ? a.substring(0, m[6]) : a); break;
        case 'u': a = Math.abs(a); break;
        case 'x': a = a.toString(16); break;
        case 'X': a = a.toString(16).toUpperCase(); break;
      }
      a = (/[def]/.test(m[7]) && m[2] && a > 0 ? '+' + a : a);
      c = m[3] ? m[3] == '0' ? '0' : m[3].charAt(1) : ' ';
      x = m[5] - String(a).length;
      p = m[5] ? str_repeat(c, x) : '';
      o.push(m[4] ? a + p : p + a);
    }
    else throw ("Huh ?!");
    f = f.substring(m[0].length);
  }
  return o.join('');
}