function select_country(country_field,city_prefix,state_prefix,province_prefix,zip_id)
{
   var country = country_field.options[country_field.selectedIndex].value;
   var city_prompt = document.getElementById(city_prefix+'prompt');
   var state_prompt = document.getElementById(state_prefix+'prompt');
   var state_field = document.getElementById(state_prefix+'field');
   var province_prompt = document.getElementById(province_prefix+'prompt');
   var province_field = document.getElementById(province_prefix+'field');
   var zip_cell = document.getElementById(zip_id);
   if (country == 1) {
      state_prompt.style.display = '';   province_prompt.style.display = 'none';
      state_field.style.display = '';   province_field.style.display = 'none';
      city_prompt.innerHTML = 'City';   zip_cell.innerHTML = 'Zip Code';
   }
   else if (country == 29) {
      state_prompt.style.display = 'none';   province_prompt.style.display = 'none';
      state_field.style.display = 'none';   province_field.style.display = 'none';
      city_prompt.innerHTML = 'Parish';   zip_cell.innerHTML = 'Postal Code';
   }
   else {
      state_prompt.style.display = 'none';   province_prompt.style.display = '';
      state_field.style.display = 'none';   province_field.style.display = '';
      city_prompt.innerHTML = 'City';   zip_cell.innerHTML = 'Postal Code';
   }
   if (document.Join && document.Join.Same) {
      var copy_flag = document.Join.Same.checked;
      if (copy_flag && (state_prefix == 'state_')) {
         document.Join.ship_country.selectedIndex = document.Join.country.selectedIndex;
         select_country(document.Join.ship_country,'ship_city_','ship_state_',
                        'ship_province_','ship_zip_cell');
      }
   }
}

function copy_address()
{
   var copy_flag = document.Join.Same.checked;
   if (copy_flag) {
      document.Join.ship_address1.value = document.Join.bill_address1.value;
      document.Join.ship_address2.value = document.Join.bill_address2.value;
      document.Join.ship_city.value = document.Join.bill_city.value;
      document.Join.ship_state.selectedIndex = document.Join.bill_state.selectedIndex;
      document.Join.ship_province.value = document.Join.bill_province.value;
      document.Join.ship_zipcode.value = document.Join.bill_zipcode.value;
      document.Join.ship_country.selectedIndex = document.Join.bill_country.selectedIndex;
   }
   else {
      document.Join.ship_address1.value = '';
      document.Join.ship_address2.value = '';
      document.Join.ship_city.value = '';
      document.Join.ship_state.selectedIndex = 0;
      document.Join.ship_province.value = '';
      document.Join.ship_zipcode.value = '';
      document.Join.ship_country.selectedIndex = 0;
   }
   select_country(document.Join.ship_country,'ship_city_','ship_state_',
                  'ship_province_','ship_zip_cell');
}

function display_cvv()
{
   var cvv_image = document.getElementById("cvvImage");
   cvv_image.style.display = '';
}

function remove_cvv()
{
   var cvv_image = document.getElementById("cvvImage");
   cvv_image.style.display = 'none';
}

function add_onload(funct)
{
   if (window.addEventListener) window.addEventListener('load',funct,false);
   else if (window.attachEvent) window.attachEvent('onload',funct);
}

function format_amount(num)
{
   num = num.toString().replace(/\$|\,/g,'');
   if (num == '') return '';
   if (isNaN(num)) num = "0";
   sign = (num == (num = Math.abs(num)));
   num = Math.floor(num*100+0.50000000001);
   cents = num%100;
   num = Math.floor(num/100).toString();
   if (cents < 10) cents = "0" + cents;
   for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
      num = num.substring(0,num.length-(4*i+3))+','+
            num.substring(num.length-(4*i+3));
   return (((sign)?'':'-') + num + '.' + cents);
}

function select_shipping(shipping_field)
{
   var shipping = shipping_field.options[shipping_field.selectedIndex].value;
   if (shipping == '') return;
   var shipping_info = shipping.split('|');
   var shipping = shipping_info[1];
   if (shipping == '') shipping = 0;
   else shipping = parseFloat(shipping,10);
   var subtotal = document.Cart.subtotal.value;
   if (subtotal == '') subtotal = 0;
   else subtotal = parseFloat(subtotal,10);
   var tax = document.Cart.tax.value;
   if (tax == '') tax = 0;
   else tax = parseFloat(tax,10);
   var coupon_amount = document.Cart.coupon_amount.value
   if (coupon_amount == '') coupon_amount = 0;
   else coupon_amount = parseFloat(coupon_amount,10);
   var total = subtotal + tax + shipping - coupon_amount;
   var total_cell = document.getElementById('total_cell');
   var currency = document.Cart.currency.value;
   if (currency == "EUR") total_cell.innerHTML = "€"+format_amount(total)+" EUR";
   else total_cell.innerHTML = "$" + format_amount(total);
}

