var gc_form_submitted = false;

function gc_address_save(data) {
  // alert(data.id);
  // gc_form_submitted = true;
  $(data).children(':submit').attr('disabled', true);
  action = $('#guest-checkout-address-form').attr('action');
  $.post(action, $(data).serialize(), gc_address_save_callback, 'json');
}

function gc_address_save_callback(data) {
  // gc_form_submitted = false;
  if (data.error && data.error.length > 0) {
    n = data.error.length;
    h = 400 + (n*12);
    $('#GB_window').animate({height: h +"px"}, 500);
    errors = '';
    for(i in data.error) {
     errors += '* '+ data.error[i] + "<br />";
    }
    $('#checkout-address-errors').html(errors).css({opacity: 0}).animate({opacity: 1}, 750);
  }
  else {
    type = data.data.address_type;
    $('#checkout-' + type + '-address-inner').html(data.data.address);
    if (type == 'billing') {
      $('#edit-billing-check').removeAttr('checked');
      $('#billing-address').removeClass('billing-is-shipping');
    }
    gc_general_update(data);
    GB_hide();
  }
}

function gc_general_update(data) {
  if (typeof data.data.transaction_details === 'string') {
    $('#checkout-review-section').html(data.data.transaction_details);
  }
}

function gc_billing_change(item) {
  if (item.checked) {
    $('#billing-address').addClass('billing-is-shipping');
  }
  else {
    item.checked = true;
    $('#billing-change').click();
    //$('#billing-address').removeClass('billing-is-shipping');
    // $('#edit-billing-check').attr('checked', true);
  }
}

function gc_form_submit() {
  ret = (gc_form_submitted ? false : true);
  gc_form_submitted = true;
  return ret;

}

function gc_set_message(message, error) {
  m = $('#payment-messages');
  m.css({opacity: 0, display: 'block'});
  m.html(message);
  if (error) {
    m.addClass('payment-message-error');
  }
  else {
    m.removeClass('payment-message-error');
  }
  m.animate({opacity: 1}, 500)
}

function gc_clear_message(message, error) {
  m.animate({opacity: 0}, 500);
  /*, function() {
  $('#payment-messages').animate({opacity: 1}, 5000, function() {
    // Don't hide nested messages - only the last one added should be hidden...
    if (!(--gc_message_count)) {
      $('#payment-messages').animate({opacity: 0, display: 'none'}, 1000);
    }
  })
});*/

}
var su_adjust_editing = false;

function su_adjust() {
  fn = null;
  lbl = '';
  nids = [];

  if (su_adjust_editing) {
   fn = function(index, item) {
     i = $(item);
     nid = i.attr("data-nid");
     inp = i.find('input');
     amount = inp.val();
     pos = nids.length;
     nids[pos] = {'nid': nid, 'amount': amount};
     i.html('$'+ amount);
   };
   lbl = 'Edit item prices'

  }
  else {
   fn = function(index, item) {
     i = $(item);
     nid = i.attr("data-nid");
     amount = i.attr("data-amount");
     i.html('<input type="text" size=8 value="' + amount + '" />');
   };
   lbl = 'Save item prices';
  }
  $('#btn-edit-update input').val(lbl);
  $('.unit-price-field').each(fn);

  if (nids.length) {
    data = { 'adjustments': nids };
    gc_set_message('Applying adjustments', false);
    $.post(callback_path + 'adjust-set', data, gc_adjust_callback, 'json');
  }


  su_adjust_editing = !su_adjust_editing;

  // adjust = $('#edit-su-adjamount').attr('value');
  // oper = $('#edit-su-adjoperator').attr('value');
  // if (typeof adjust == 'undefined') {
    // adjust = '0';
  // }
}

function gc_adjust_callback(data) {
  if (data.message) {
    gc_set_message(data.message, !data.status);
  }
  else {
    gc_set_message('Error applying adjustment', true);
  }
  gc_general_update(data);
}

function gc_taxexempt_callback(data) {
  gc_general_update(data);
  gc_clear_message();
}

function gc_taxexempt() {
  gc_set_message('Adjusting tax...', false);
  data = {'taxExempt': $('#edit-taxexempt').prop('checked')};
  $.post(callback_path + 'tax-exemption', data, gc_taxexempt_callback, 'json');
}
