window.Ads = { lang: { '1night': null, 'we': null, 'midweek': null, '1week': null, '2weeks': null, '3weeks': null, '1month': null, 'open': null, 'closed': null, }, getLangs: function() { var that = this; $.each(that.lang, function(lang, value) { $.get('/en/json/lang', {string: 'ad.period.duration.'+lang}, function(response) { var response = JSON.parse(response); if(response && response.success == true && response.result != false) that.lang[lang] = response.result; if(response.result == false) { $.get('/en/json/lang', {string: 'ad.period.'+lang}, function(response) { var response = JSON.parse(response); if(response && response.success == true && response.result != false) that.lang[lang] = response.result; }); } else { } }); }); }, refreshPeriods: function() { var that = this; alert(that.periods.data); $.each(that.periods.data, function(index, period) { if(period.duration == 'open') { var tr = $('').addClass('owners_adform_periodsummary_vacant').addClass('period-'+period.id).addClass('period-duration'); } else { var tr = $('').addClass('owners_adform_periodsummary_booked').addClass('period-'+period.id).addClass('period-duration'); } var actions = $('').attr('width', '10%'); var edit_link = $('').attr('href', '#').attr('style', 'margin-left:15px;').append('nomdufichier.ext').data('period', period); var delete_link = $('').attr('href', '#').append('nomdufichier.ext'); actions.append(edit_link); actions.append(delete_link); var from = $('').attr('width', '15%'); from.append('

' + period.from + '

'); var to = $('').attr('width', '15%'); to.append('

' + period.to + '

'); var price = $('').attr('width', '40%'); price.append('

' + period.price + '€/semaine

'); var minimum_duration = $('').attr('width', '20%'); minimum_duration.append('

' + window.Ads.lang[period.duration] + '

'); tr.append(actions); tr.append(from); tr.append(to); tr.append(price); tr.append(minimum_duration); window.Ads.periods.table.find('tbody').append(tr); edit_link.click(function(e) { var period = $(this).data('period'); window.Ads.periods.$el.find('input.from').val(period.from); window.Ads.periods.$el.find('input.to').val(period.to); window.Ads.periods.$el.find('input.price').val(period.price); window.Ads.periods.$el.find('input.price_promo').val(period.price_promo); window.Ads.periods.$el.find('select.duration').val(period.duration); window.Ads.periods.$el.find('select.duration').parent().find('.customStyleSelectBoxInner').text(window.Ads.lang[period.duration]); window.Ads.periods.$el.find('select.minimum_duration').val(period.minimum_duration); window.Ads.periods.$el.find('select.minimum_duration').parent().find('.customStyleSelectBoxInner').text(window.Ads.lang[period.minimum_duration]); window.Ads.periods.$el.data('editing', true); window.Ads.periods.$el.data('periodId', period.id); e.preventDefault(); return false; }); delete_link.click(function(e){ $('.input-period-id-'+period.id).remove(); $('.input-period-from-'+period.id).remove(); $('.input-period-to-'+period.id).remove(); $('.input-period-price-'+period.id).remove(); $('.input-period-price_promo-'+period.id).remove(); $('.input-period-duration-'+period.id).remove(); $('.input-period-minimum_duration-'+period.id).remove(); var input = $('').attr('type', 'hidden').attr('name', 'deleted_periods[]').val(period.id); $('form#owners_adform').append(input); window.Ads.periods.table.find('tbody tr.period-'+period.id).remove(); e.preventDefault(); return false; }); }); }, init: function(){ this.periods.data = {}; this.getLangs(); setTimeout('window.Ads.refreshPeriods()', 3500); var that = this; this.periods.selector = '#owners_adform fieldset#periods'; this.periods.$el = $(this.periods.selector); this.periods.table = this.periods.$el.find('#owners_adform_periodsummary'); var periodsData = $('.periodData'); this.periods.count = 0; $.each(periodsData, function(index, el) { var $el = $(el); var periodId = $el.data('periodId'); if($el.hasClass('input-period-id')) { if(!that.periods.data[periodId]) that.periods.data[periodId] = {}; that.periods.data[periodId]['id'] = $el.val(); } else if ($el.hasClass('input-period-from')) { if(!that.periods.data[periodId]) that.periods.data[periodId] = {}; that.periods.data[periodId]['from'] = $el.val(); } else if ($el.hasClass('input-period-to')) { if(!that.periods.data[periodId]) that.periods.data[periodId] = {}; that.periods.data[periodId]['to'] = $el.val(); } else if ($el.hasClass('input-period-price')) { if(!that.periods.data[periodId]) that.periods.data[periodId] = {}; that.periods.data[periodId]['price'] = $el.val(); } else if ($el.hasClass('input-period-price_promo')) { if(!that.periods.data[periodId]) that.periods.data[periodId] = {}; that.periods.data[periodId]['price_promo'] = $el.val(); } else if ($el.hasClass('input-period-duration')) { if(!that.periods.data[periodId]) that.periods.data[periodId] = {}; that.periods.data[periodId]['duration'] = $el.val(); } else if ($el.hasClass('input-period-minimum_duration')) { if(!that.periods.data[periodId]) that.periods.data[periodId] = {}; that.periods.data[periodId]['minimum_duration'] = $el.val(); } }); var that = this; this.periods.$el.find('.owners_add').click(that.onAddPeriod); $('.ez-radio input.package').click(function(){ that.packages.selection[0] = $(this).val(); that.packages.renderTable(); $('input.order-package').val($(this).val()); }); $('#defaultPack').trigger('click'); $('.ez-checkbox input.promo_du_moment').change(function(){ that.packages.selection[1] = $(this).is(':checked'); that.packages.renderTable(); var val = ($(this).is(':checked')) ? 1 : 0; $('input.order-promo_du_moment').val(val); }); $('.ez-checkbox input.promo_du_moment').trigger('change'); $('.ez-checkbox input.a_la_une').change(function(){ that.packages.selection[2] = $(this).is(':checked'); if(!that.packages.selection[3]) { that.packages.selection[3] = 1; } var val = ($(this).is(':checked')) ? 1 : 0; $('input.order-a_la_une').val(val); that.packages.renderTable(); }); $('.ez-checkbox input.a_la_une').trigger('change'); $('.ez-radio input.a_la_une_duration').change(function(){ that.packages.selection[3] = $(this).val(); that.packages.renderTable(); $('input.order-a_la_une_duration').val($(this).val()); }); $('.ez-radio input.a_la_une_duration').trigger('change'); $('form#owners_adform').find('#addCode').click(function(e) { that.packages.promo = $('form#owners_adform').find('#formPromo').val(); that.packages.renderTable(); e.preventDefault(); return false; }); this.packages.renderTable(); }, packages: { 'decouverte': { price: 39, lang: { fr: 'Forfait DECOUVERTE (3 mois)' } }, 'confort': { price: 69, lang: { fr: 'Forfait CONFORT (6 mois)' } }, 'serenite': { price: 89, lang: { fr: 'Forfait SERENITE (12 mois)' } }, 'promo_du_moment': { price: 19, lang: { fr: 'Pack "Promo du moment" (1 mois)' } }, 'a_la_une': { prices: { 1: 25, 3: 45, 6: 60, }, selection: 0, lang: { fr: { 1: 'Pack "Annonces à la une" (1 mois)', 3: 'Pack "Annonces à la une" (3 mois)', 6: 'Pack "Annonces à la une" (6 mois)', } } }, promo: false, defaultPack: 'decouverte', fare: function() { if(this.selection.length > 0){ var that = this, fare = 0; if(that[that.selection[0]]) { var price = that[that.selection[0]].price; fare += price; } if (that.selection[1] == true) { var price = that.promo_du_moment.price; fare += price; } if (that.selection[2] == true) { var p = that.a_la_une; var price = p.prices[that.selection[3]]; fare += price; } return (this.promo != false) ? this.calculatePromo(fare) : fare; } else { return (this.promo != false) ? this.calculatePromo(this[this.defaultPack].price) : this[this.defaultPack].price; } }, promo_designation: '...', calculatePromo: function(fare){ var that = this; $.get('/ads/calculatePromo', { code: that.promo, fare: fare, subscription: function() { var val = window.Ads.packages.selection[0]; return val; } }, function(response){ response = JSON.parse(response); if(response) { if(response.error == 0) { that.promo_designation = response.result.designation; $('form#owners_adform').find('#promoDesignation').html(response.result.designation+ ' €'); $('form#owners_adform').find('#orderTotal').html(''+response.result.price + ' €'); return response.result.price; } else { that.promo = false; that.renderTable(); alert('Code promotionnel invalide.'); return false; } } }).error(function() { that.promo = false; that.renderTable(); alert('Une erreur est survenue lors de la vérification du code promotionnel.'); return false; }); return '...'; }, renderTable: function(){ if(this.selection.length > 0) { this.clearTable(); var that = this; var table = $('#owners_adform_cartsummary'); if(that[that.selection[0]]) { var p = that[that.selection[0]]; var rowData = { designation: p.lang.fr, price: p.price, } table.append(that.renderRow(rowData)); } if (that.selection[1] == true) { var p = that.promo_du_moment; var rowData = { designation: p.lang.fr, price: p.price, } table.append(that.renderRow(rowData)); } if (that.selection[2] == true) { var p = that.a_la_une; p.price = p.prices[that.selection[3]]; var rowData = { designation: p.lang.fr[that.selection[3]], price: p.price, } if (p) table.append(that.renderRow(rowData)); } var total = that.renderTotal(); if(that.promo != false) table.append(that.renderPromo()); table.append(total); } else { this.clearTable(); var p = this[this.defaultPack]; var rowData = { designation: p.lang.fr, price: p.price, } var table = $('#owners_adform_cartsummary'); table.append(this.renderRow(rowData)); var total = this.renderTotal(); if(this.promo != false) this.renderPromo; table.append(total); } }, renderRow: function(rowData){ var tr = $('').addClass('owners_adform_cartsummary_row'); var designation = $('').attr('width', '80%').css('textAlign', 'left').append('

' + rowData.designation + '

'); var price = $('').attr('witdh', '20%').append('

' + rowData.price + ' €

'); tr.append(designation); tr.append(price); return tr; }, renderPromo: function(){ var that = this; var tr = $('').addClass('owners_adform_cartsummary_row'); var designation = $('').attr('width', '80%').css('textAlign', 'left').append('

Coupon promotionnel valide (' +that.promo + ')

'); var price = $('').attr('witdh', '20%').append('

' + that.promo_designation + ' €

'); tr.append(designation); tr.append(price); return tr; }, renderTotal: function(){ var tr = $('').attr('id', 'owners_adform_cartsummary_footer'); var designation = $('').attr('width', '80%').css('textAlign', 'right').append('

TOTAL

'); var price = $('').attr('witdh', '20%').append('

' + this.fare() + ' €

'); tr.append(designation); tr.append(price); return tr; }, clearTable: function(){ var table = $('#owners_adform_cartsummary'); var rows = table.find('tbody .owners_adform_cartsummary_row'); $.each(rows, function(index, row){ $(row).remove(); }); table.find('tbody #owners_adform_cartsummary_footer').remove(); }, selection: [], }, validatePeriodForm: function(){ var ret = true; if(!$("#owners_adform").validate().element('#period-form-from')) { ret = false; } if(!$("#owners_adform").validate().element('#period-form-to')) { ret = false; } if($('#period-form-price').val() == '') { ret = false; } //if($('#period-form-price_promo').val() == '') { ret = false; } //console.log(typeof($('#period-form-price').val())); //if($('#period-form-price_promo').val()) { // if(typeof($('#period-form-price_promo').val()) != 'number') { ret = false; } //} if($('#period-price-duration').val() == 'off' || !$('#period-price-duration').val()) { ret = false; } if($('#period-form-minimum_duration').val() == 'off' || !$('#period-form-minimum_duration').val()) { ret = false; } return ret; }, onAddPeriod: function(event){ if(!window.Ads.validatePeriodForm()) return false; var period = { from: window.Ads.periods.$el.find('input.from').val(), to: window.Ads.periods.$el.find('input.to').val(), price: window.Ads.periods.$el.find('input.price').val(), price_promo: window.Ads.periods.$el.find('input.price_promo').val(), duration: window.Ads.periods.$el.find('select.duration').val(), minimum_duration: window.Ads.periods.$el.find('select.minimum_duration').val(), }; if(!period.price_promo) period.price_promo = 0; if(window.Ads.periods.$el.data('editing')) { var tmpId = window.Ads.periods.$el.data('periodId'); if($('tr.period-'+tmpId+'.period-duration').hasClass('owners_adform_periodsummary_vacant')) { if(period.duration != 'open') { $('tr.period-'+tmpId+'.period-duration').removeClass('owners_adform_periodsummary_vacant'); $('tr.period-'+tmpId+'.period-duration').addClass('owners_adform_periodsummary_booked'); } } else { if(period.duration != 'closed') { $('tr.period-'+tmpId+'.period-duration').removeClass('owners_adform_periodsummary_booked'); $('tr.period-'+tmpId+'.period-duration').addClass('owners_adform_periodsummary_vacant'); } } $('.period-'+tmpId+'-from').text(period.from); $('.period-'+tmpId+'-to').text(period.to); $('.period-'+tmpId+'-price').text(period.price + '€/semaine'); $('.period-'+tmpId+'-minimum_duration').text(window.Ads.lang[period.duration]); $('.input-period-from-'+tmpId).val(period.from); $('.input-period-to-'+tmpId).val(period.to); $('.input-period-price-'+tmpId).val(period.price); $('.input-period-price_promo-'+tmpId).val(period.price_promo);// $('.input-period-duration-'+tmpId).val(period.duration); $('.input-period-minimum_duration-'+tmpId).val(period.minimum_duration); window.Ads.resetPeriodForm(); window.Ads.periods.$el.data('editing', false); window.Ads.periods.$el.data('periodId', null); } else { if(period.price && period.from && period.to /* && period.duration && period.minimum_duration */) { window.Ads.periods.count++; period.tmpId = window.Ads.periods.count; if(period.duration == 'open') { var tr = $('').addClass('owners_adform_periodsummary_vacant').addClass('period-'+period.tmpId).addClass('period-duration'); } else { var tr = $('').addClass('owners_adform_periodsummary_booked').addClass('period-'+period.tmpId).addClass('period-duration'); } var actions = $('').attr('width', '10%'); var edit_link = $('
').attr('href', '#').attr('style', 'margin-left:15px;').append('nomdufichier.ext').data('period', period); var delete_link = $('').attr('href', '#').append('nomdufichier.ext'); actions.append(edit_link); actions.append(delete_link); var from = $('').attr('width', '15%'); from.append('

' + period.from + '

'); var to = $('').attr('width', '15%'); to.append('

' + period.to + '

'); var price = $('').attr('width', '40%'); price.append('

' + period.price + '€/semaine

'); var minimum_duration = $('').attr('width', '20%'); minimum_duration.append('

' + window.Ads.lang[period.duration] + '

'); tr.append(actions); tr.append(from); tr.append(to); tr.append(price); tr.append(minimum_duration); window.Ads.periods.table.find('tbody').append(tr); window.Ads.periods.data[period.tmpId] = period; var input_from = $('').attr('value', period.from).attr('type','hidden').attr('name','period['+period.tmpId+'][from]').addClass('input-period-from-'+period.tmpId).addClass('inputPeriodFrom'); var input_to = $('').attr('value', period.to).attr('type','hidden').attr('name','period['+period.tmpId+'][to]').addClass('input-period-to-'+period.tmpId).addClass('inputPeriodTo'); var input_price = $('').attr('value', period.price).attr('type','hidden').attr('name','period['+period.tmpId+'][price]').addClass('input-period-price-'+period.tmpId); var input_price_promo = $('').attr('value', period.price_promo).attr('type','hidden').attr('name','period['+period.tmpId+'][price_promo]').addClass('input-period-price_promo-'+period.tmpId); var input_duration = $('').attr('value', period.duration).attr('type','hidden').attr('name','period['+period.tmpId+'][duration]').addClass('input-period-duration-'+period.tmpId); var input_minimum_duration = $('').attr('value', period.minimum_duration).attr('type','hidden').attr('name','period['+period.tmpId+'][minimum_duration]').addClass('input-period-minimum_duration-'+period.tmpId); $('#owners_adform').append(input_from); $('#owners_adform').append(input_to); $('#owners_adform').append(input_price); $('#owners_adform').append(input_price_promo); $('#owners_adform').append(input_duration); $('#owners_adform').append(input_minimum_duration); edit_link.click(function(e) { var period = $(this).data('period'); window.Ads.periods.$el.find('input.from').val(period.from); window.Ads.periods.$el.find('input.to').val(period.to); window.Ads.periods.$el.find('input.price').val(period.price); window.Ads.periods.$el.find('input.price_promo').val(period.price_promo); window.Ads.periods.$el.find('select.duration').val(period.duration); window.Ads.periods.$el.find('select.duration').parent().find('.customStyleSelectBoxInner').text(window.Ads.lang[period.duration]); window.Ads.periods.$el.find('select.minimum_duration').val(period.minimum_duration); window.Ads.periods.$el.find('select.minimum_duration').parent().find('.customStyleSelectBoxInner').text(window.Ads.lang[period.minimum_duration]); window.Ads.periods.$el.data('editing', true); window.Ads.periods.$el.data('periodId', period.tmpId); e.preventDefault(); return false; }); delete_link.click(function(e){ window.Ads.periods.data[period.tmpId] = null; $('.input-period-from-'+period.tmpId).remove(); $('.input-period-to-'+period.tmpId).remove(); $('.input-period-price-'+period.tmpId).remove(); $('.input-period-price_promo-'+period.tmpId).remove(); $('.input-period-duration-'+period.tmpId).remove(); $('.input-period-minimum_duration-'+period.tmpId).remove(); window.Ads.periods.table.find('tbody tr.period-'+period.tmpId).remove(); e.preventDefault(); return false; }); window.Ads.resetPeriodForm(); } else { } } event.preventDefault(); return false; }, resetPeriodForm: function(){ window.Ads.periods.$el.find('input.from').val(''); window.Ads.periods.$el.find('input.to').val(''); window.Ads.periods.$el.find('input.price').val(''); window.Ads.periods.$el.find('input.price_promo').val(''); window.Ads.periods.$el.find('select.duration').val("off"); window.Ads.periods.$el.find('select.duration').parent().find('.customStyleSelectBoxInner').text(""); window.Ads.periods.$el.find('select.minimum_duration').val("off"); window.Ads.periods.$el.find('select.minimum_duration').parent().find('.customStyleSelectBoxInner').text(""); }, periods: {}, }; //function ajouter par mohamed function DesactiverEntrer(idFormulaire){ $('#'+idFormulaire).bind("keypress", function(e){ if(e.keyCode == 13){ // //window.Ads.calculatePromo(); //window.Ads.init(); click_me('addCode');return false; } } ); } function click_me(element) { try { // ie document.getElementById(element).click(); } catch(e) { var evt = document.createEvent("MouseEvents"); // creates mouse event evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); // inits mouse event var cb = document.getElementById(element); // gets element cb.dispatchEvent(evt); // sends event to element } } $(function(){ DesactiverEntrer('formPromo'); window.Ads.init(); });