var App_Promo_Admin_Form = qs.createObject(); App_Promo_Admin_Form.prototype = { options: { isRelatedId: 'isRelated', productId: 'productId', hasNoExpirationDateId: 'hasNoExpirationDate', endDateId: 'endDate', codeId: 'code', newCardCodeId: 'newCardCode' }, hintOptions: { useWrapper: true, elements: [{selector: 'input[name=_productId]', hint: 'Autocomplete field'}] }, initialize: function (options) { this.options = $.extend(this.options, options); this.form = $('#' + this.options.formId); if (!$(this.form).size()) { alert('Error: form "' + this.options.formId + '" was not found'); return; } $(this.form).defaultHint(this.hintOptions); $('#' + this.options.isRelatedId).bind('change', _.bind(this.isRelatedOnChange, this)); this.isRelatedOnChange(); $('#' + this.options.hasNoExpirationDateId).bind('change', _.bind(this.hasNoExpirationDateOnChange, this)); this.hasNoExpirationDateOnChange(); $('#' + this.options.newCardCodeId).bind('click', _.bind(this.generateCode, this)); }, isRelatedOnChange: function () { var display = 'none'; if ($('#' + this.options.isRelatedId).val() == 'y') { display = 'block'; } this.toggleView(this.options.productId, display); }, hasNoExpirationDateOnChange: function () { var display = 'block'; if ($('#' + this.options.hasNoExpirationDateId).is(':checked')) { display = 'none'; } this.toggleView(this.options.endDateId, display); }, toggleView: function (elementId, display) { $('#' + elementId + '-label, #' + elementId + '-element').css('display', display); }, generateCode: function() { var deferred = qs.ajax(qs.constant('BASE_URL') + '/' + qs.constant('CURRENT_PAGE'), {action: 'generateCode'}); deferred.done(_.bind(this.updateCode, this)); }, updateCode: function(data) { var code = ''; if (data.code) { code = data.code; } else { alert('Code is not created.'); } $('#' + this.options.codeId).val(code); } };