var App_ECommerce_Order_Admin = qs.createObject(); App_ECommerce_Order_Admin.defaultOptions = { addProductsElementId: 'add-products-link', dialogOptions: { id: 'dialog-container', title: 'Add Products', width: 960, height: 650, productListContainer: '#product-popup-list-container', quantityFieldNamePrefix: 'quantity-' }, filter: '' }; App_ECommerce_Order_Admin.prototype = { initialize: function (options) { this.options = $.extend({}, App_ECommerce_Order_Admin.defaultOptions, options); this.quantityElements = {}; this.initAddProductsElement(); return this; }, initAddProductsElement: function() { this.addProductsElement = $('#' + this.options.addProductsElementId); this.addProductsElement.bind('click.addProductsElement', _.bind(this.onClickAddProduct, this)); return this; }, onClickAddProduct: function(e) { this.initPopup(); return this; }, initPopup: function() { if (!this.options.url) { return false; } var url = this.options.url; var dialogOptions = this.options.dialogOptions; var obj = this; var dialog = $('
', {'id': this.options.dialogOptions.id}) .dialog({ resizable: false, draggable: false, width: dialogOptions.width, height: dialogOptions.height, modal: true, title: dialogOptions.title, buttons: { 'Add Product(s)': function() { obj.addProducts(obj); }, 'Close': function() { $(this).dialog('close'); window.location.reload(); } }, open: function() { obj.blockPopup(obj); $.ajax({ url: url, dataType: 'html', type: 'GET', success: function(data) { $('').html(data.toString()).appendTo(dialog); obj.initFilter(obj); obj.initList(obj); obj.initPagination(obj); obj.unblockPopup(obj); } }); }, close: function(ev, ui) { $(this).remove(); window.location.reload(); } }); $('.ui-dialog-buttonpane').css({'text-align': 'center'}).find('.ui-dialog-buttonset').css({'float': 'none'}); $(window).scroll(function () { dialog.dialog('option', 'position', 'center'); }); return true; }, initFilter: function(obj) { var filterForm = $('.ui-dialog .filter_form'); $(filterForm).bind('submit.filterFormSubmit', function() { obj.blockPopup(obj); obj.options.filter = $(this).serialize(); obj.loadList($(this).attr('action') + '?', obj); obj.unblockPopup(obj); }); $(filterForm).find('#btnCancel').bind( 'click.clearFilterClick', function() { if (obj.options.filter.length) { obj.blockPopup(obj); obj.options.filter = ''; $(filterForm).each(function() {this.reset();}); obj.loadList($(filterForm).attr('action'), obj); obj.unblockPopup(obj); } return false; } ); return true; }, initList: function(obj) { obj.quantityElements = $('.ui-dialog input[name^=' + obj.options.dialogOptions.quantityFieldNamePrefix + ']'); obj.quantityElements.autoNumeric({aSep: '', aDec: ''}); obj.quantityElements.each(function() { $(this).keyup(function() { if ($(this).val().length) { $(this).val(parseInt($(this).val())); } if ($(this).css('background-color') == 'rgb(255, 0, 0)') { $(this).css({'background-color':'rgb(255, 255, 255)'}); } return false; }); }); return true; }, initPagination: function(obj) { if ($('.ui-dialog').size()) { var handler = function() { obj.blockPopup(obj); obj.loadList($(this).attr('href') + '&', obj); obj.unblockPopup(obj); return false; }; $(document).on('click.paginatorPageClick', '.ui-dialog .pagination a[href^=http]', handler); } return true; }, loadList: function(url, obj) { var defaultHtml = $(obj.options.dialogOptions.productListContainer).html(); $.ajax({ url: url + obj.options.filter, dataType: 'json', type: 'GET' }).done(function (data) { $(obj.options.dialogOptions.productListContainer).empty(); if (!data) { data = defaultHtml; } $(obj.options.dialogOptions.productListContainer).html(data); obj.initList(obj); }); return false; }, addProducts: function(obj) { $('.dialog_add_product_result').remove(); if (obj.quantityElements.length) { var requiredError = true; var validElements = {}; obj.quantityElements.each(function(i) { var target = $(this); if (parseInt(target.val()) > 0) { validElements[target.attr('name').split('-')[1]] = target.val(); requiredError = false; } }); if (requiredError) { obj.quantityElements.css({'background-color':'rgb(255, 0, 0)'}); } else { obj.blockPopup(obj); $.ajax({ url: obj.options.addToCartUrl, data: {action: 'addItem', 'cartId': obj.options.cartId, 'product': validElements}, dataType: 'json', async: false, type: 'POST' }).fail(function (status) { alert(status.responseText); }).done( function (data) { if (data.success && typeof data.success == 'object') { for (var productId in data.success) { $('#item-' + productId).after( $('', {'class':'dialog_add_product_result ' + data.success[productId].type }).html(data.success[productId].message) ); } } obj.unblockPopup(obj); } ); } } return false; }, blockPopup: function(obj) { $('#' + obj.options.dialogOptions.id).block({ css: {border: 0, color: '#fff', background: 'transparent', opacity: 0.5} }); }, unblockPopup: function(obj) { $('#' + obj.options.dialogOptions.id).unblock(); } }; var App_ECommerce_Order_Admin_CustomerTab = qs.createObject(); App_ECommerce_Order_Admin_CustomerTab.prototype = { _userDisplayElement: null, _userElementId: null, _form: null, initialize: function (formId, userElement) { this._form = $('#' + formId); if (this._form) { this._userElementId = userElement; this._userDisplayElement = $('#_' + this._userElementId); var obj = this; this._userDisplayElement.blur(function() { obj.onBlur(); }); } }, onBlur: function() { var userId = parseInt($('#' + this._userElementId).val()); var obj = this; if (userId) { qs.ajax(qs.constant('BASE_URL') + '/' + qs.constant('CURRENT_PAGE'), { action: 'getUserData', userId: userId }).fail(function(status) {alert(status.responseText);}).done(function(result) { if (result.success) { var asBillingElement = $('#shipping-asBilling'); if (!result.data.shipping.asBilling) { asBillingElement.prop('checked', false); appUserForm.onAsBillingClick(asBillingElement, 'shipping'); } else if (result.data.shipping.asBilling == 'y') { asBillingElement.prop('checked', true); appUserForm.onAsBillingClick(asBillingElement, 'shipping'); } for (var sKey in result.data.shipping) { var shippingElement = $('#shipping-' + sKey); if (shippingElement.attr('id') != asBillingElement.attr('id')) { if (shippingElement.attr('type') == 'checkbox') { if (result.data.shipping[sKey] == 'y') { shippingElement.prop('checked', true); } else { shippingElement.prop('checked', false); } } else { shippingElement.val(result.data.shipping[sKey]); } } } for (var bKey in result.data.billing) { var billingElement = $('#billing-' + bKey); if (billingElement.attr('type') == 'checkbox') { if (result.data.billing[key] == 'y') { billingElement.prop('checked', false); } else { billingElement.prop('checked', true); } } else { billingElement.val(result.data.billing[bKey]); billingElement.blur(); } } } else { obj._userDisplayElement.val(''); $('#' + obj._userElementId).val(''); } }); } } };