$(document).ready(function()
{
  var deliveryCheckbox = $('input[id*="delivery-address-same"]');
  deliveryCheckbox.change(function()
  {
    var checked = deliveryCheckbox.is(':checked');
    
    if (checked)
    {
      //copy details across and disable
      $('div.address-details input, div.address-details select').each(function()
      {
        var id = $(this).attr('id');
        $('div.delivery-address-details input[id="delivery-'+id+'"], div.delivery-address-details select[id="delivery-'+id+'"]').val($(this).val()).attr('disabled', true);
      });
    }
    else
    {
      //don't touch details, but re-enable
      $('div.address-details input, div.address-details select').each(function()
      {
        var id = $(this).attr('id');
        $('div.delivery-address-details input[id="delivery-'+id+'"], div.delivery-address-details select[id="delivery-'+id+'"]').attr('disabled', false);
      });
    }
  });
  
  deliveryCheckbox.trigger('change');
  
  handleSubscribe();
});

function handleSubscribe()
{
  $('#subscribe-enews').live('click', function()
  {
    $(this).trigger('change');
  });
  $('#subscribe-enews').live('change', function()
  {
    //alert($(this).is(':checked'));
    var checked = $(this).is(':checked');
    var subscribeForm = $(this).parents('form').find('#subscribe-form');
    
    if (checked)
    {
      $(subscribeForm).show();
    }
    else
    {
      $(subscribeForm).hide();
    }
  });
  $('#subscribe-enews').trigger('change');
  
  $('#signup-username, #signup-firstname, #signup-lastname, #signup-phone, #signup-company, #signup-position, #signup-address1, #signup-postcode, #signup-suburb').attr('readonly', true);
  
  textInputCopy('#email-address', '#signup-username');
  textInputCopy('#first-name', '#signup-firstname');
  textInputCopy('#last-name', '#signup-lastname');
  textInputCopy('#company', '#signup-company');
  textInputCopy('#position', '#signup-position');
  textInputCopy('#phone-number', '#signup-phone');
  textInputCopy('#address1', '#signup-address1');
  textInputCopy('#postcode', '#signup-postcode');
  textInputCopy('#suburb', '#signup-suburb');
}

function textInputCopy(from, to)
{
  $(from).live('keyup', function()
  {
    $(this).parents('form').find(to).val($(this).val());
  });
}