// YBP Stuff

//coupon submit

var ajax_process_coupon = function(url) {
  $.ajax({
    type: "POST",
    url: url,                                
    beforeSend : function (xhr) {
      xhr.setRequestHeader('Accept-Encoding', 'identity');        
      $('div#coupon-error').removeClass('error').html("");     
      $('img#coupon_busy_indicator').show();
    },      
    dataType: "json",   
    data: $('#checkout-coupon').serialize(),    
    complete: function() { $('img#coupon_busy_indicator').hide(); },
    success: function(json) {  
      // TODO - create new div for coupon messages and provide feedback that it was accepted       
      update_coupon_confirmation(json);
			//alert('success');
    },
    error: function (XMLHttpRequest, textStatus, errorThrown) {
      $('div#coupon-error').addClass('error').html("Server Error: Unable to Process Coupon");
    }
  });  	
};


var update_coupon_confirmation = function(order) {
  var textToInsert = '';
  for (var key in order.credits) {
    textToInsert  += '<p class="subtotal">' + key + ':(' + order.credits[key] + ')</p>';
  }
  $('div#order_credits').html(textToInsert); 
};      



// public/javascripts/application.js
jQuery.ajaxSetup({ 
  'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
})

jQuery.fn.submitWithAjax = function() {
  this.submit(function() {
	$(this).validate();
	if($(this).valid()){
			$(this).children('.loading').show();
    	$.post(this.action, $(this).serialize(), null, "script");
	}
    return false;
  })
  return this;
};



$(document).ready(function() {
$('a.remote-delete').click(function() {
// we just need to add the key/value pair for the DELETE method
// as the second argument to the JQuery $.post() call
if(confirm('Are you sure?')){
$.post(this.href, { _method: 'delete' }, null, "script");
return false;
} else {
return false;
};
});
});

$(document).ready(function() {
  $("#newsletter_form").submitWithAjax();
})
$(document).ready(function() {
  $("#user_feedback").submitWithAjax();
})

$(document).ready(function() {
  $("#checkout_coupon").submitWithAjax();
})

// to be used with checkout.js
var update_addresses = function(result) {
	$('#checkout_bill_address_attributes_firstname').val(result[0].address.firstname);
	$('#checkout_bill_address_attributes_lastname').val(result[0].address.lastname);
	$('#checkout_bill_address_attributes_address1').val(result[0].address.address1);
	$('#checkout_bill_address_attributes_address2').val(result[0].address.address2);
	$('#checkout_bill_address_attributes_city').val(result[0].address.city);
	$('#checkout_bill_address_attributes_phone').val(result[0].address.phone);
	$('#checkout_bill_address_attributes_zipcode').val(result[0].address.zipcode);
	$('#checkout_bill_address_attributes_country_id').val(result[0].address.country_id);
	update_state('b');
	$('#checkout_bill_address_attributes_state_id').val(result[0].address.state_id);
	$('#checkout_bill_address_attributes_state_name').val(result[0].address.state_name);
	
	$('#checkout_ship_address_attributes_firstname').val(result[1].address.firstname);
	$('#checkout_ship_address_attributes_lastname').val(result[1].address.lastname);
	$('#checkout_ship_address_attributes_address1').val(result[1].address.address1);
	$('#checkout_ship_address_attributes_address2').val(result[1].address.address2);
	$('#checkout_ship_address_attributes_city').val(result[1].address.city);
	$('#checkout_ship_address_attributes_phone').val(result[1].address.phone);
	$('#checkout_ship_address_attributes_zipcode').val(result[1].address.zipcode);
	$('#checkout_ship_address_attributes_country_id').val(result[1].address.country_id);
	update_state('s');
	$('#checkout_ship_address_attributes_state_id').val(result[1].address.state_id);
	$('#checkout_ship_address_attributes_state_name').val(result[1].address.state_name);
	
};


