var fasoon = fasoon || {}; (function (ns, $) { "use strict"; ns.QuotePriceStep4 = function () { this.construct.apply(this, arguments); }; var proto = ns.QuotePriceStep4.prototype; proto.construct = function (options) { this.options = $.extend(true, { form_id: undefined, plugins_url: undefined, save_action: undefined, ajax_url: undefined, gtm_data: {}, persons_class: undefined, add_person_class: undefined, add_company_class: undefined, overlay_class: 'delivery_scope_overlay', new_person_form_class: undefined, new_company_form_class: undefined, nationality_id: undefined, nationality_switzerland_val: undefined, hometown_id: undefined, persons_data: {}, person_zip_id: undefined, company_zip_id: undefined, company_person_zip_id: undefined, load_contact_data_id: undefined, contact_data: {}, contact_data_confirmation: undefined, errorsContainerClass: 'form-error', zip_numeric_options: { vMin: 0, vMax: 999999999, aSep: '', mDec: 0 } }, options); this.form = $('#' + this.options.form_id); if (!this.form.length) { alert('Form #' + this.options.form_id + ' is not found.'); return; } this.persons = $('.' + this.options.persons_class, this.form); if (!this.persons.length) { alert('Container .' + this.options.persons_class + ' is not found.'); return; } this.new_person_form = $('.' + this.options.new_person_form_class + ' form'); if (this.new_person_form.length) { this.new_person_form.on('submit', $.proxy(this.new_person_form_submit, this)); if (this.options.nationality_id && this.options.hometown_id) { this.hometown = $('#' + this.options.hometown_id, this.new_person_form); this.nationality = $('#' + this.options.nationality_id, this.new_person_form); if (this.nationality.length && this.hometown.length) { this.nationality.on('change', $.proxy(this.on_nationality_change, this)); this.on_nationality_change(); } } $('[type=submit]', this.new_person_form).on('click', $.proxy(this.required_show, this)); // ff fix this.load_contact_data_btn = $('#' + this.options.load_contact_data_id, this.new_person_form); if (this.load_contact_data_btn.length) { this.load_contact_data_btn.on('click', $.proxy(this.load_contact_data, this)); } this.person_zip = $('#' + this.options.person_zip_id, this.new_person_form); if (this.person_zip.length) { this.person_zip.autoNumeric('init', this.options.zip_numeric_options); } } this.new_company_form = $('.' + this.options.new_company_form_class + ' form'); if (this.new_company_form.length) { this.new_company_form.on('submit', $.proxy(this.new_company_form_submit, this)); $('[type=submit]', this.new_company_form).on('click', $.proxy(this.required_show, this)); // ff fix this.company_zip = $('#' + this.options.company_zip_id, this.new_company_form); if (this.company_zip.length) { this.company_zip.autoNumeric('init', this.options.zip_numeric_options); } this.company_pserson_zip = $('#' + this.options.company_person_zip_id, this.new_company_form); if (this.company_pserson_zip.length) { this.company_pserson_zip.autoNumeric('init', this.options.zip_numeric_options); } } $('.' + this.options.add_person_class, this.form).on('click', $.proxy(this.add_person_form, this)); $('.' + this.options.add_company_class, this.form).on('click', $.proxy(this.add_company_form, this)); this.overlay_close_btn = $('.' + this.options.overlay_class + ' .btn-close'); this.overlay_close_btn.on('click', $.proxy(this.on_overlay_close_btn, this)); this.init_persons_actions(); fasoon_gtm.company_foundation(4, "Initiated", this.options.gtm_data); this.form.find('#back').on('click', $.proxy(this.onBack, this)); this.form.on('submit', $.proxy(this.onSubmit, this)); }; proto.onBack = function () { this.options.gtm_data.click_name = "Back"; fasoon_gtm.company_foundation(4, "Complete", this.options.gtm_data); return true; }; proto.onSubmit = function () { this.options.gtm_data.click_name = "Continue"; fasoon_gtm.company_foundation(4, "Complete", this.options.gtm_data); return true; }; proto.personGtmEvent = function (action, category) { fasoon_gtm.push({ "event": "company_foundation", "step_number": "4", "step_progress": "In Progress", "item_category": category, "item_action": action, }); }; proto.load_contact_data = function () { var is_empty = true; for (var i in this.options.contact_data) { if ('prefix' === i) { continue; } if ($('[name="' + i + '"]', this.new_person_form).val()) { is_empty = false; break; } } if (!is_empty && !confirm(this.options.contact_data_confirmation)) { return; } for (var i in this.options.contact_data) { if ('prefix' === i) { $('[name="' + i + '"][value="' + this.options.contact_data[i] + '"]').prop('checked', true); } else { $('[name="' + i + '"]', this.new_person_form).val(this.options.contact_data[i]).trigger('blur');; } } }; proto.on_overlay_close_btn = function () { this.personGtmEvent('Close', ($('.' + this.options.new_person_form_class).is(':visible') ? 'person' : 'company')); this.hide_overlay(); }; proto.hide_overlay = function () { $('.' + this.options.overlay_class).hide(); this.clear_person_forms(); }; proto.on_nationality_change = function () { if (this.nationality.val() == this.options.nationality_switzerland_val) { this.hometown.removeClass('hidden'); this.hometown.prop('required', true); } else { this.hometown.addClass('hidden'); this.hometown.removeAttr('required'); } }; proto.add_person_form = function() { this.on_nationality_change(); $('.' + this.options.new_person_form_class).show(); $(document).scrollTop(0); }; proto.add_company_form = function() { $('.' + this.options.new_company_form_class).show(); $(document).scrollTop(0); }; proto.new_person_form_submit = function() { var data = this.new_person_form.serializeObject(); data.action = this.options.save_action; $.post(this.options.ajax_url, data, $.proxy(this.new_form_submit_success, this), 'json'); return false; }; proto.new_company_form_submit = function() { var data = this.new_company_form.serializeObject(); data.action = this.options.save_action; $.post(this.options.ajax_url, data, $.proxy(this.new_form_submit_success, this), 'json'); return false; }; proto.new_form_submit_success = function (response) { if (response.is_success) { this.options.persons_data = response.persons_data; this.persons.html(response.persons_html); this.init_persons_actions(); this.removeErrors(); this.hide_overlay(); this.personGtmEvent(this.capitalizeFirstLetter(response.action), response.person.person_type); } else { if (response.hasOwnProperty('wrong_nonce') && !response.wrong_nonce) { document.location.reload(); } else { alert(response.error); } } }; proto.capitalizeFirstLetter = function (string) { return string.charAt(0).toUpperCase() + string.slice(1); }; proto.removeErrors = function () { $('.' + this.options.errorsContainerClass).remove(); }; proto.clear_person_forms = function () { if (this.new_person_form.length) { this.new_person_form[0].reset(); $('.filled', this.new_person_form).removeClass('filled'); $('[name=id]', this.new_person_form).val(''); } if (this.new_company_form.length) { this.new_company_form[0].reset(); $('.filled', this.new_company_form).removeClass('filled'); $('[name=id]', this.new_company_form).val(''); } this.required_hide(); // ff fix }; proto.required_hide = function () { // ff fix if (this.new_person_form.length) { this.new_person_form.addClass('required-hide'); } if (this.new_company_form.length) { this.new_company_form.addClass('required-hide'); } }; proto.required_show = function () { // ff fix if (this.new_person_form.length) { this.new_person_form.removeClass('required-hide'); } if (this.new_company_form.length) { this.new_company_form.removeClass('required-hide'); } }; proto.init_persons_actions = function () { $('.person_action', this.persons).on('click', $.proxy(this.person_action, this)); }; proto.person_action = function (ev) { var a = $(ev.target); var id = a.data('id'); var type = a.data('type'); var data = this.options.persons_data[type][id]; data.action = this.options.save_action; data.id = id; if ('person' === type) { data._wpnonce = $('[name=_wpnonce]', this.new_person_form).val(); } else if ('company' === type) { data._wpnonce = $('[name=_wpnonce]', this.new_company_form).val(); } if ('delete' === a.data('action')) { data.sub_action = 'delete'; $.post(this.options.ajax_url, data, $.proxy(this.new_form_submit_success, this), 'json'); } else if ('edit' === a.data('action')) { var form; if ('person' === type) { form = this.new_person_form; } else if ('company' === type) { form = this.new_company_form; } $.each(data, function(name, val) { var $el = $('[name="'+name+'"]', form), type = $el.attr('type'); switch(type) { case 'checkbox': $el.attr('checked', 'checked'); break; case 'radio': $el.filter('[value="'+val+'"]').attr('checked', 'checked'); break; default: $el.val(val); } $el.trigger('blur'); }); if ('person' === type) { this.add_person_form(); } else if ('company' === type) { this.add_company_form(); } } return false; }; })(fasoon, jQuery);