/**
* Copy the delivery information to the payment information on the checkout
* screen if corresponding fields exist.
*/
function uc_vat_number_cf_cart_copy_address(checked, source, target, pane) {
  if (!checked) {
    $('#' + target + '-pane div.address-pane-table').slideDown();
    copy_box_checked = false;
    return false;
  }
                             // this is new too...
  if (target == 'billing' || target == 'pbilling') {
    var x = 28;
  }
  else {
    var x = 26;
  }

  // Hide the target information fields.
  //$('#' + target + '-pane div.address-pane-table').slideUp();
  copy_box_checked = true;

  // Copy over the zone options manually.
  if ($('#edit-panes-' + target + '-' + pane + '-zone').html() != $('#edit-panes-' + source + '-' + source + '-zone').html()) {
    $('#edit-panes-' + target + '-' + pane + '-zone').empty().append($('#edit-panes-' + source + '-' + source + '-zone').children().clone());
    $('#edit-panes-' + target + '-' + pane + '-zone').attr('disabled', $('#edit-panes-' + source + '-' + source + '-zone').attr('disabled'));
  }

  // Copy over the information and set it to update if delivery info changes.
  $('#' + source + '-pane input, select, textarea').each(
    function() {
      if (this.id.substring(0, x) == 'edit-panes-' + source + '-' + source) {
     
        $('#edit-panes-' + target + '-' + pane + this.id.substring(x)).val($(this).val());
        if (target == 'billing' || target == 'pbilling') {
          $(this).change(function () { uc_vat_number_cf_update_billing_field(this, pane); });
        }
        else {
          $(this).change(function () { uc_vat_number_cf_update_delivery_field(this); });
        }
      }
    }
  );

  return false;
}

function uc_vat_number_cf_update_billing_field(field, pane) {
  if (copy_box_checked) {
    $('#edit-panes-' + pane + '-billing' + field.id.substring(28)).val($(field).val());
  }
}

function uc_vat_number_cf_update_delivery_field(field) {
  if (copy_box_checked) {
    $('#edit-panes-delivery-delivery' + field.id.substring(26)).val($(field).val());
  }
}
