$(function(){html
var checkoutShipping = {jquery
/*ajax
** Choose Shipping Address app
*/dom
chooseAddress: function(){ide
$(".jq_chooseShippingAddress").on("change", function(){this
var $form = $(this).closest("form");spa
if($(this).val() != -1) {orm
var shippingAddressID = $(this).val(),htm
data = $.parseJSON($form.find("#shippingAddressJSON").text());
var shippingAddress = data[shippingAddressID];
$form.fillForm(shippingAddress);
$form.find("#sgAddressBookId").val($(this).val());
/*$form.find(".jq_singleShippingAddressForm").disabledForm(true);*/
}else {
/*$form.find(".jq_singleShippingAddressForm").disabledForm(false);*/
$form.find("#sgAddressBookId").val("");
$form.clearForm();
$(this).val("-1");
}
defaultAddresss();
});
}
};
checkoutShipping.chooseAddress();
/*
** Show add new address popup in muliple shipping address
*/
var MulipleShippingAdress = {
settings: {
$currentSelect: "",
$currentSelectVal: ""
},
init: function(){
this.bindEvent();
this.submitShippingAddressForm();
this.submitMultipleShippingAddressForm();
},
bindEvent: function(){
var settings = this.settings;
// show shipping address popup
$(".jq_addNewAddress").on("change", function(event){
event.preventDefault();
settings.$currentSelect = $(this);
settings.$currentSelectVal = $(this).closest(".form-group").find("var[name=selectVal]");
if($(this).val() == -1){
$("#modalShippingAddress").modal("show");
}else {
settings.$currentSelectVal.text($(this).val());
}
});
// bind model hidden event
$("#modalShippingAddress").on("hidden.bs.modal", function(event){
event.preventDefault();
$(this).find("form").clearForm();
settings.$currentSelect.val(settings.$currentSelectVal.text());
});
// bind modal cancel event
$("#modalShippingAddress .jq_cancel").on("click", function(event){
event.preventDefault();
$(this).closest(".modal").modal("hide");
});
},
submitShippingAddressForm: function(){
var settings = this.settings;
//ajax submit
$("#saveNewAddress").on("click", function(event){
event.preventDefault();
$(this).addClass("disabled");
$(this).attr("disabled", "disabled");
var address = $("#addNewShippingAddressForm #shippingFirstName").val() + ", " +
$("#addNewShippingAddressForm #shippingLastName").val()+ ", " +
$("#addNewShippingAddressForm #shippingAddress1").val();
var $this = $(this);
//clear error message
$('#addNewShippingAddressForm').find(".has-error").removeClass("has-error");
$('#addNewShippingAddressForm').find(".form-group label.error").detach();
$('#addNewShippingAddressForm').ajaxSubmit({
success: function(data){
data = $.parseJSON(data);
if(typeof(data.status) !== undefined && data.status) {
// add shipping address option
$(".jq_addNewAddress").find("option:last").before('<option value="'+data.addressId+'" >'+ address + '</option>');
settings.$currentSelectVal.text(data.addressId);
$("#modalShippingAddress").modal("hide");
} else {
//set error message on popup page
for ( var key in data.error ){
var $formGroup = $("#"+key).closest(".form-group");
var contentKey = data.error[key];
var contentValue = contentKeyParser(contentKey);
$formGroup.addClass("has-error");
$formGroup.append('<label class="error">'+contentValue+'</label>');
}
}
$this.removeClass("disabled");
$this.removeAttr("disabled");
}
});
});
},
submitMultipleShippingAddressForm: function(){
/*
* Bind Event
* submit multiple shipping address form
*/
$(".jq_multipleShippingAddressSubmit").on("click", function(event){
event.preventDefault();
var isSubmit = true,
errorMap = $.parseJSON($("#multipleShippingAddressForm").find("var[name=errorMessage]").text());
$("#multipleShippingAddressForm .jq_addNewAddress").each(function(){
var value = $(this).val();
if( $.trim(value).length === 0 || value == -1 ) {
var $formGroup = $(this).closest(".form-group");
$formGroup.addClass("has-error");
if($formGroup.find("label.error").size() > 0){
$formGroup.find("label.error").html(errorMap.empty);
}else {
$formGroup.append('<label class="error">'+errorMap.empty+'</label>');
}
isSubmit = false;
} else {
$(this).closest(".form-group").removeClass("has-error");
$(this).closest(".form-group").find("label.error").detach();
}
});
if(isSubmit){
$("#multipleShippingAddressForm").submit();
}
});
}
};
var defaultAddresss = function(){
var saveDefaultShippingAdressCheck = $("#saveDefaultShippingAdressCheck");
$('.form-group').on('click',saveDefaultShippingAdressCheck.parent().next(),function(){
if(saveDefaultShippingAdressCheck.attr('checked')){
saveDefaultShippingAdressCheck.val('true');
}else{
saveDefaultShippingAdressCheck.val('false');
}
});
if($('#defaultShippingGroupId').val() != undefined && $('#defaultShippingGroupId').val() != "" && $('#defaultShippingGroupId').val() == $("#sgAddressBookId").val()){
saveDefaultShippingAdressCheck.attr('checked',true).val('true').parent().next().addClass('checked');
}else{
saveDefaultShippingAdressCheck.attr('checked',false).val('false').parent().next().removeClass('checked');
}
}
/*
* Bind Event
* submit single shipping address form
*/
$(".jq_shippingAddressSubmit").on("click", function(event){
event.preventDefault();
$("#createShippingAddressForm").submit();
});
/*
* Bind Event
* submit shipping method form
*/
$(".jq_shippingMethodSubmit").on("click", function(event){
event.preventDefault();
$("#applyShippingMethodForm").submit();
});
/*
* Bind Event
* submit back to shipping address form
*/
$(".jq_backToShippingAddressFromMethod").on("click", function(event){
event.preventDefault();
$("#backToShippingAddressForm").submit();
});
/*
* Bind Event
* go to multiple shipping address form
*/
$(".jq_goToMultipleShippingAddressFrom").on("click", function(event){
event.preventDefault();
$("#shipToMultipleAddressForm").submit();
});
/*
* Bind Event
* go to single shipping address form
*/
$(".jq_goToSingleShippingAddressFrom").on("click", function(event){
event.preventDefault();
$("#shipToSingleAddressForm").submit();
});
/*
* Bind Event
* submit back to shipping address form
*/
$(".jq_backToCart").on("click", function(event){
event.preventDefault();
$("#backToCartFrom").submit();
});
MulipleShippingAdress.init();
function contentKeyParser(key) {
// because key is like xx.xx.xx so not use jquery
var domKey = document.getElementById(key);
var value = $(domKey).text() || "";
return value;
}
/**
* for highlight navigation bar
*/
(function highlightNavigationBar() {
var $cartridgeType = $(".jq_checkoutCartridgeType");
var cartridgeType = $(".jq_checkoutCartridgeType").text();
var navTab = $("." + "jq_" + cartridgeType);
if (!navTab.hasClass("active")) {
navTab.addClass("active");
}
})();
});