// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
$(document).ready(function() {
  
  if($("#customer_paper_brand_id option:selected").text() != 'Other'){
    $("#hide_paper_brand").hide();
  }
  
  $("#customer_paper_brand_id").click(function() {
    if($("#customer_paper_brand_id option:selected").text() == 'Other'){
      $("#hide_paper_brand").show();
    } else {
      $('#customer_paper_brand_other').attr('value', '');
      $("#hide_paper_brand").hide();
    }
  });
  
  if($("#customer_waste_company_id option:selected").text() != 'Other'){
    $("#hide_waste_company").hide();
  }
  
  $("#customer_waste_company_id").click(function() {
    if($("#customer_waste_company_id option:selected").text() == 'Other'){
      $("#hide_waste_company").show();
    } else {
      $('#customer_waste_company_other').attr('value', '');
      $("#hide_waste_company").hide();
    }
  });
  
  if($("#customer_referral_source_id option:selected").text() != 'Other'){
    $("#hide_referral_source").hide();
  }
  
  $("#customer_referral_source_id").click(function() {
    if($("#customer_referral_source_id option:selected").text() == 'Other'){
      $("#hide_referral_source").show();
    } else {
      $('#customer_referral_source_other').attr('value', '');
      $("#hide_referral_source").hide();
    }
  });
  
  if($("#customer_alternative_delivery option:selected").text() != 'Leave it with a neighbour- please give name'){
    $("#hide_alternative_delivery").hide();
  }
  
  $("#customer_alternative_delivery").click(function() {
    if($("#customer_alternative_delivery option:selected").text() == 'Leave it with a neighbour- please give name'){
      $("#hide_alternative_delivery").show();
    } else {
      $('#customer_alternative_delivery_other').attr('value', '');
      $("#hide_alternative_delivery").hide();
    }
  });
  
  $('#commit').click(function(){
    if(!$('#terms_accepted').attr('checked')) {
      alert('Please accept our terms and conditions before continuing.');
      return false;
    }
  });
  
  $('#same_as_champion').click(function(){
    if($('#same_as_champion').attr('checked')) {
      if($('#customer_champion_firstname').attr('value') != ''){
        $('#customer_accounts_firstname').attr('value', $('#customer_champion_firstname').attr('value'));
      }
      
      if($('#customer_champion_surname').attr('value') != ''){
        $('#customer_accounts_surname').attr('value', $('#customer_champion_surname').attr('value'));
      }
      
      if($('#customer_champion_phone').attr('value') != ''){
        $('#customer_accounts_phone').attr('value', $('#customer_champion_phone').attr('value'));
      }
      
      if($('#customer_champion_email').attr('value') != ''){
        $('#customer_accounts_email').attr('value', $('#customer_champion_email').attr('value'));
      }
      
      if($('#customer_business_floor').text() != ''){
        $('#customer_accounts_floor').attr('value', $('#customer_business_floor').attr('value'));
      }

      if($('#customer_business_address').attr('value') != ''){
        $('#customer_accounts_address').attr('value', $('#customer_business_address').attr('value'));
      }
      
      if($('#customer_business_address_2').attr('value') != ''){
        $('#customer_accounts_address_2').attr('value', $('#customer_business_address_2').attr('value'));
      }
      
      if($('#customer_business_postcode').attr('value') != ''){
        $('#customer_accounts_postcode').attr('value', $('#customer_business_postcode').attr('value'));
      }
      
      $('#accounts-contact input:text').not('#customer_accounts_city').attr('readonly', true);
    } else {
      $('#accounts-contact input:text').attr('value','');
      $('#accounts-contact input:text').attr('readonly', false);
    }
  });
  
  $('#customer_big_bin_id').click(function(){
    if ($('#customer_big_bin_id:checked').length > 0) {
      $('#champion_details input:text').attr('value', '');
      $('#champion_details input:text').attr('disabled', 'true');
    } else {
      $('#champion_details input:text').attr('disabled', '');
    }
  });
  
  if ($('#customer_big_bin_id:checked').length > 0) {
      $('#champion_details input:text').attr('value', '');
      $('#champion_details input:text').attr('disabled', 'true');
    }
  
});
