var phones = []; var plans = []; var durations = []; var phonePrices = []; var isMobile = { Android: function() { return navigator.userAgent.match(/Android/i); }, BlackBerry: function() { return navigator.userAgent.match(/BlackBerry/i); }, iOS: function() { return navigator.userAgent.match(/iPhone|iPad|iPod/i); }, Opera: function() { return navigator.userAgent.match(/Opera Mini/i); }, Windows: function() { return navigator.userAgent.match(/IEMobile/i); }, any: function() { return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows()); } }; $(document).ready(function () { initPlansAndPhones(); var journeyCookie = getJourneyCookie(); if (journeyCookie != "" && journeyCookie.split("_").length>0) { var journeyCookieArray = journeyCookie.split("_"); if($("#loginLink").length<1){ if ($("#renewalBox").length > 0) { if (journeyCookieArray[1] == "modal-selection-box") { showModalRenewal('modal-renewal-box', journeyCookieArray[2]); } else if (journeyCookieArray[1] == "modal-selection-plan-box") { showModalRenewal('modal-renewal-box', journeyCookieArray[3]); } } else { if (journeyCookieArray[1] == "modal-selection-box") { showModalSelection('modal-selection-box',journeyCookieArray[2]); } else if (journeyCookieArray[1] == "modal-selection-plan-box") { showModalSelectionPlan('modal-selection-plan-box',journeyCookieArray[2],journeyCookieArray[3]); } } } delete_cookie("journeyId"); } }); $('#listingRange').on('pageChange', function(event, param1, param2) { initPlansAndPhones(); }); $('.planCategory').on('click', function() { $(this).removeClass( ); $('.breadcrumb').children("li.active").remove(); var $parent =$(this).parent(); $parent.addClass('active'); $parent.children('span').remove(); var linkText = $(this).text(); $parent.append(linkText); $(this).remove( ); }); $(document).on('change', 'input:radio[class=duration]', function() { initPlansAndPhones(); var durationStr = $(this).closest('ul.item-price').find('input:radio[class=duration]:checked').val(); $(this).closest('li.item').find('a.plan-details-url').each(function () { var $anchor = $(this); var oldHref = $anchor.attr('href'); var newHref = ""; var param = "defaultDuration=" + durationStr; oldHref = oldHref.replace(/defaultDuration=[^&]*/, ""); var lastChar = oldHref.charAt(oldHref.length - 1); if (lastChar === '?' || lastChar === '&') { newHref = oldHref + param; } else if (oldHref.indexOf('?') > 0) { newHref = oldHref + '&' + param; } else { newHref = oldHref + '?' + param; } $anchor.attr('href', newHref); }); }); function initPlansAndPhones(){ $('.int-single-product, .product-item').each(function () { var phone = $.parseJSON($(this).find('.phone-json').html()); var plan = $.parseJSON($(this).find('.plan-json').html()); var laptop = $.parseJSON($(this).find('.laptop-json').html()); var tablet = $.parseJSON($(this).find('.tablet-json').html()); if(phone != null){ phones[phone.id] = new ModalWindow(phone, plan); } if (plan != null) { console.log("plan " + plan); plans[plan.id]=new ModalWindow(phone, plan, laptop, tablet); var durationStr = $(this).find('input:radio[class=duration]:checked').val(); console.log("durationStr " + durationStr); if (durationStr == undefined){ $('.familyOfferPriceDetails').each(function () { durationStr = $(this).find('input:radio[class=duration]:checked').val()+"m"; console.log("durationStr1 " + durationStr); }); } //Added for renewal if (durationStr == undefined) durationStr = plan.duration; durations[plan.id] = durationStr; console.log("durations " + durations); } }); $('.planDuration-json').each(function () { var phoneDurationPrice = $.parseJSON($(this).html()); phoneDurationPrice.price = $(this).next().html(); phonePrices[phoneDurationPrice.planId + '-' + phoneDurationPrice.duration + '-' + phoneDurationPrice.phoneId] = phoneDurationPrice; }); } function setPlanDuration(planId, duration, phoneId) { console.log("setPlanDurationL" + duration); durations[planId]=duration; if ($('#' + planId + '_phonePrice').length>0){ $('#' + planId + '_phonePrice').html(phonePrices[planId + '-' + duration + '-' + phoneId].price); } return true; } function ModalWindow(phone, plan, laptop, tablet) { this.phone = phone; this.plan = plan; this.laptop = laptop; this.tablet = tablet; } function showModalSelection(linkId, phoneId) { var currentSelection = phones[phoneId]; $("#modalLoginLink").attr('data-product', phoneId); showRenewal('Selection', currentSelection, linkId); } function showModalSelectionPlan(linkId, planId, hasApplicableDevices, simoAvailable, exclusive) { var currentSelection = plans[planId]; var duration = durations[planId]; $("#modalPlanLoginLink").attr('data-product', planId); $("#modalPlanLoginLink").attr('data-applicable-devices', hasApplicableDevices); showRenewalPlan('SelectionPlan', currentSelection, linkId, duration, hasApplicableDevices, simoAvailable, exclusive); } function showModalSelectionPlanWithDuration(linkId, planId, hasApplicableDevices, duration, simoAvailable, exclusive) { var currentSelection = plans[planId]; $("#modalPlanLoginLink").attr('data-product', planId); $("#modalPlanLoginLink").attr('data-applicable-devices', hasApplicableDevices); showRenewalPlan('SelectionPlan', currentSelection, linkId, duration, hasApplicableDevices, simoAvailable, exclusive); } function showModalSelectionPrepaidPlan(linkId, planId) { var currentSelection = plans[planId]; showRenewalPrepaidPlan('Selection', currentSelection, linkId); } function showModalRenewal(linkId, phoneId) { var currentSelection = phones[phoneId]; showRenewal('Renewal', currentSelection, linkId); $("#modalRenewalPhoneAction").html(currentSelection.phone.form); $("#modalRenewalPayMonthlyAction").html(currentSelection.plan.form); } function showModalRenewalPlan(linkId, phoneId) { var currentSelection = phones[phoneId]; showRenewalPlan('Renewal', currentSelection, linkId); $("#modalRenewalPhoneAction").html(currentSelection.phone.form); $("#modalSelectionPlanAction").html(currentSelection.plan.form); } function isEmpty(value){ return (value == null || value.length === 0); } function accessoryToBasket(accessoryId) { var currentSelection = phones[accessoryId]; var accessoryForm = $('#accessoryToBasketForm'); accessoryForm.find('.product-id').val(currentSelection.phone.id); accessoryForm.attr('action', $('#originatingRequestURI').val()); accessoryForm.find('.sku-id').val(currentSelection.phone.skuId); clickEvent("accessoryToBasketButton"); } function addToRibbon(accessoryId){ var currentSelection = phones[accessoryId]; // Update the displayed elements of the journey ribbon $(".ribbon-device-price").html(currentSelection.phone.price); $(".ribbon-device-price").parent().removeClass("hidden") $(".ribbon-plan-price").html(currentSelection.plan.price); $(".btn-primary").removeClass("disabled"); // Update the journey form with the device ID $('#planFormNou').find('#deviceId').val(currentSelection.phone.id); } function allToBasket(accessoryId) { var planId=$("#placeForSelectedPlanId").html(); var currentProductSelection = phones[accessoryId]; //var currentPlanSelection = plans[planId]; var accessoryForm = $('#allToBaskeForm'); accessoryForm.find('.phoneId').val(currentProductSelection.phone.id); accessoryForm.find('.sku-id').val(currentProductSelection.phone.skuId); accessoryForm.find('.planId').val(planId); accessoryForm.attr('action', $('#originatingRequestURI').val()); // accessoryForm.find('.sku-id').val(currentSelection.phone.skuId); console.log('planId=' + planId + ', phoneId='+ currentProductSelection.phone.id); clickEvent("submitAddToBasket"); // $('form#slotMachineForm input#phoneId').val(phone); // $('form#slotMachineForm input#planId').val(plan); // $('form#slotMachineForm input#serviceId').val(service); // $('form#slotMachineForm input#accessoryId').val(accessory); // $('form#slotMachineForm input#duration').val(duration); // $('form#slotMachineForm input#submitAddToBasket').click(); } function showRenewal(modalType, selection, linkId) { var payMonthlyWithPlan = $('#modal' + modalType + 'PayMonthlyWithPlan'); //var payMonthlyImg = $('#modal' + modalType + 'PayMonthlyImg'); var phoneToPostpaidPlanForm = $('#phoneToPostpaidPlanForm' + modalType); var phoneType = selection.phone.type; if (!isEmpty(selection.plan.id) && phoneType!=('accessory')) { //payMonthlyImg.attr('src', selection.plan.imageUrl); //payMonthlyImg.attr('title', selection.plan.name); //payMonthlyImg.attr('alt', selection.plan.name); $('#modal' + modalType + 'PayMonthlyPrice').html(formatPrice(selection.plan.price)); $('#modal' + modalType + 'PayMonthlyPrice').next().show(); payMonthlyWithPlan.html(selection.plan.withPlan + " " + selection.plan.planDuration); payMonthlyWithPlan.show(); //payMonthlyImg.show(); phoneToPostpaidPlanForm.find('.btn-primary').removeClass('disabled'); if(document.getElementById("renewal.message.phone.price.starting.with")){ document.getElementById("renewal.message.phone.price.starting.with").className = "visible"; } if(document.getElementById("selection.message.phone.price.starting.with")){ document.getElementById("selection.message.phone.price.starting.with").className = "visible"; } } else { $('#modal' + modalType + 'PayMonthlyPrice').html(''); $('#modal' + modalType + 'PayMonthlyPrice').next().hide(); payMonthlyWithPlan.hide(); //payMonthlyImg.hide(); phoneToPostpaidPlanForm.find('.btn-primary').addClass('disabled'); if(document.getElementById("renewal.message.phone.price.starting.with")){ document.getElementById("renewal.message.phone.price.starting.with").className = "hidden"; } if(document.getElementById("selection.message.phone.price.starting.with")){ document.getElementById("selection.message.phone.price.starting.with").className = "hidden"; } } $('#modal' + modalType + 'PhonePrice').html(formatPrice(selection.phone.price)); $('#modal' + modalType + 'PhonePricePrepaid').html(formatPrice(selection.phone.price)); //var phoneImg = $('#modal' + modalType + 'PhoneImg'); //phoneImg.attr('src', selection.phone.imageUrl); //phoneImg.attr('title', selection.phone.name); //phoneImg.attr('alt', selection.phone.name); var withPhone = $('#modal' + modalType + 'WithPhone'); withPhone.attr('href', selection.phone.url); withPhone.html(selection.phone.withPhone); var withPhone = $('#modal' + modalType + 'WithPhonePrepaid'); withPhone.attr('href', selection.phone.url); withPhone.html(selection.phone.withPhone); phoneToPostpaidPlanForm.find('.product-id').val(selection.phone.id); phoneToPostpaidPlanForm.attr('action', $('#originatingRequestURI').val()); var phoneToPrepaidPlanForm = $('#phoneToPrepaidPlanForm' + modalType); phoneToPrepaidPlanForm.find('.product-id').val(selection.phone.id); phoneToPrepaidPlanForm.attr('action', $('#originatingRequestURI').val()); var phoneToRenewalForm = $('#phoneToRenewalForm'); phoneToRenewalForm.find('.product-id').val(selection.phone.id); phoneToRenewalForm.attr('action', $('#originatingRequestURI').val()); var onlyPhoneForm = $('#onlyPhoneForm' + modalType); onlyPhoneForm.find('.product-id').val(selection.phone.id); onlyPhoneForm.attr('action', $('#originatingRequestURI').val()); onlyPhoneForm.find('.sku-id').val(selection.phone.skuId); var planForm = $('#modal' + modalType + 'PayMonthlyAction').find('form'); planForm.find('.product-id').val(selection.phone.id); planForm.attr('action', $('#originatingRequestURI').val()); setJourneyParameters(selection); clickEvent(linkId); } function setJourneyParameters(selection){ var deviceType = selection.phone.type; var journeyStartStep = "postpaidPhoneToPlan"; var journeyCurrentStep = "choosePhone"; var prepaidJourneyStartStep = "prepaidPhoneToPlan"; if(deviceType == 'laptop') { journeyStartStep = "postpaidLaptopToPlan"; prepaidJourneyStartStep = "prepaidLaptopToPlan"; journeyCurrentStep = "chooseLaptop"; } else if(deviceType == 'tablet') { journeyStartStep = "postpaidTabletToPlan"; prepaidJourneyStartStep = "prepaidTabletToPlan"; journeyCurrentStep = "chooseTablet"; } else if(deviceType == 'accessory') { journeyStartStep = "postpaidModemToPlan"; prepaidJourneyStartStep = "prepaidModemToPlan"; journeyCurrentStep = "chooseModem"; } $('.postpaidJourneyStartStep').each(function(){ $(this).val(journeyStartStep); }); $('.prepaidJourneyStartStep').each(function(){ $(this).val(prepaidJourneyStartStep); }); $('.postpaidJourneyCurrentStep').each(function(){ $(this).val(journeyCurrentStep); }); $('.prepaidJourneyCurrentStep').each(function(){ $(this).val(journeyCurrentStep); }); } function clickEvent (linkId) { if( document.createEvent ) { var clickEvent = document.createEvent('MouseEvents'); clickEvent.initEvent( "click", true, true ); if (document.getElementById(linkId) != null) { if(isMobile.any()) { $('#'+linkId).click(); }else{ document.getElementById(linkId).dispatchEvent(clickEvent); } } } else if( document.createEventObject ) { // for IE var clickEvent = document.createEventObject(); document.getElementById(linkId).fireEvent( 'onclick', clickEvent ); } } function showRenewalPlan(modalType, selection, linkId, duration, hasApplicableDevices, simoAvailable, exclusive) { $('#modal' + modalType + 'PlanPrice').html(formatPrice(selection.plan.price)); var withPlan = $('#modal' + modalType + 'WithPlan'); withPlan.attr('href', selection.plan.url); withPlan.html(selection.plan.withPlan); var planForm = $('#modal' + modalType + 'PlanAction').find('form'); planForm.find('.product-id').val(selection.plan.id); planForm.find('.planDuration').val(duration); planForm.find('.complexOfferId').val(selection.plan.complexOfferId); planForm.attr('action', $('#originatingRequestURI').val()); var phoneForm = $('#modal' + modalType + 'PhoneAction').find('form'); phoneForm.find('.product-id').val(selection.plan.id); phoneForm.find('.planDuration').val(duration); phoneForm.find('.complexOfferId').val(selection.plan.complexOfferId); phoneForm.attr('action', $('#originatingRequestURI').val()); if (false) { phoneForm.find('.btn-continue').addClass('disabled'); } else { phoneForm.find('.btn-continue').removeClass('disabled'); } if ((typeof simoAvailable !== 'undefined') && (simoAvailable == 'false')) { $(".simolny").hide(); } else { $(".simolny").show(); } if ((typeof exclusive !== 'undefined') && (exclusive == 'true')) { $(".select-phone-block").hide(); } else { $(".select-phone-block").show(); } clickEvent(linkId); } function showRenewalPrepaidPlan(modalType, selection, linkId) { var planImg = $('#modal' + modalType + 'PlanImg'); //planImg.attr('src', selection.plan.imageUrl); //planImg.attr('title', selection.plan.name); //planImg.attr('alt', selection.plan.name); $('#modal' + modalType + 'PlanPrice').html(formatPrice(selection.plan.price)); $('#modal' + modalType + 'PhonePrice').html(formatPrice(selection.plan.price)); var withPlan = $('#modal' + modalType + 'WithPlan'); withPlan.attr('href', selection.plan.url); withPlan.html(selection.plan.withPlan); var withPhone = $('#modal' + modalType + 'WithPhone'); withPhone.attr('href', selection.plan.url); withPhone.html(selection.plan.withPlan); var planForm = $('#modal' + modalType + 'PlanAction').find('form'); planForm.find('.product-id').val(selection.plan.id); planForm.attr('action', $('#originatingRequestURI').val()); var phoneForm = $('#modal' + modalType + 'PhoneAction').find('form'); phoneForm.find('.product-id').val(selection.plan.id); phoneForm.attr('action', $('#originatingRequestURI').val()); clickEvent(linkId); } function showModalSelectionPostpaidDataPlan(linkId, planId, hasApplicablesDevices) { var selection = plans[planId]; var duration = durations[planId]; // alert("linkId:"+linkId+",planId:"+planId+",hasApplicablesDevices:"+hasApplicablesDevices+",duration:"+duration+",selection.laptop:"+selection.laptop+",selection.tablet:"+selection.tablet+",selection.plan"+selection.plan); setModalSegmentData('modalLaptop', "laptop"); setModalSegmentData('modalTablet', "tablet"); setModalSegmentData('modalSimOnly', selection.plan); setPriceWithLinkId(linkId,'modalSimOnly', selection.plan); setModalSegmentData('modalModem', selection.plan); setPlanFormDataWithLinkId(linkId,'modalLaptop', selection.plan, duration, hasApplicablesDevices); setPlanFormDataWithLinkId(linkId,'modalTablet', selection.plan, duration, hasApplicablesDevices); setPlanFormDataWithLinkId(linkId,'modalSimOnly', selection.plan, duration); setPlanFormDataWithLinkId(linkId,'modalModem', selection.plan, duration, hasApplicablesDevices); clickEvent(linkId); } function showModalSelectionPostpaidDataPlanWithDuration(linkId, planId, hasApplicablesDevices, duration) { var selection = plans[planId]; // alert("linkId:"+linkId); setModalSegmentData('modalLaptop', "laptop"); setModalSegmentData('modalTablet', "tablet"); setModalSegmentData('modalSimOnly', selection.plan); setPriceWithLinkId(linkId,'modalSimOnly', selection.plan); setModalSegmentData('modalModem', selection.plan); setPlanFormDataWithLinkId(linkId,'modalLaptop', selection.plan, duration, hasApplicablesDevices); setPlanFormDataWithLinkId(linkId,'modalTablet', selection.plan, duration, hasApplicablesDevices); setPlanFormDataWithLinkId(linkId,'modalSimOnly', selection.plan, duration); setPlanFormDataWithLinkId(linkId,'modalModem', selection.plan, duration, hasApplicablesDevices); clickEvent(linkId); } function showModalSelectionPrepaidDataPlan(linkId, planId) { var selection = plans[planId]; var duration = ""; setPlanFormData('modalLaptop', selection.plan, duration); setPlanFormData('modalTablet', selection.plan, duration); setPlanFormData('modalSimOnly', selection.plan, duration); setPlanFormData('modalModem', selection.plan, duration); setPrice('modalSimOnly', selection.plan); clickEvent(linkId); } function setModalSegmentData(modalSegmentName, data){ if(data != null){ $('#'+modalSegmentName+'Box').show(); //setImage(modalSegmentName, data); //setPrice(modalSegmentName, data); //setLinkPriceWith(modalSegmentName, data); }else{ // alert("hide:"+'#'+modalSegmentName+'Box'); $('#'+modalSegmentName+'Box').hide(); } } function setImage(modalSegmentName, data){ var img = $('#'+modalSegmentName+'Image'); img.attr('src', data.imageUrl); img.attr('title', data.name); img.attr('alt', data.name); } function setPrice(modalSegmentName, data){ // alert("setPrice:"+data.price+"in "+'#'+modalSegmentName+'Price'); var price = formatPrice(data.price); $('#'+modalSegmentName+'Price').html(price); } function setPriceWithLinkId(linkId, modalSegmentName, data){ // alert("setPrice:"+data.price+"in "+'#'+modalSegmentName+'Price'); var price = formatPrice(data.price); var priceId='#'+modalSegmentName+'Price'; if(linkId!='' && linkId!=undefined){ if(linkId.indexOf("-box")>0){ linkId=linkId.substring(0,linkId.indexOf("-box")); } priceId ="#"+linkId+" #"+modalSegmentName+'Price'; } $(priceId).html(price); } function formatPrice(price){ var price = Number(price); var priceFormatted = price.toFixed(2).toLocaleString(); var priceParts = priceFormatted.split('.'); var priceInt = ''+priceParts[0]+''; var priceDec = ''+priceParts[1]+''; return priceInt+priceDec; } function setLinkPriceWith(modalSegmentName, data){ var withLink = $('#'+modalSegmentName+'WithPlan'); withLink.attr('href', data.url); withLink.html(data.name); } function setPlanFormData(modalSegmentName, data, duration, hasApplicableDevices){ // console.log("'#'+modalSegmentName+'Action'"); // alert(".product-id : " + data.id); // alert(".duration : " + duration); // alert(".complexOfferId : " + data.complexOfferId); var actionForm = $('#'+modalSegmentName+'Action').find('form'); actionForm.find('.product-id').val(data.id); actionForm.find('.planDuration').val(duration); console.log(".planDuration : " + duration); actionForm.find('.complexOfferId').val(data.complexOfferId); actionForm.attr('action', $('#originatingRequestURI').val()); if (false) { actionForm.find('.btn-continue').addClass('disabled'); } else { actionForm.find('.btn-continue').removeClass('disabled'); } } //the following function created for fixing an issue in search page where the pop up+form was not filled with data. This is because all modals are imported in this page and not the proper one is filled function setPlanFormDataWithLinkId(linkId,modalSegmentName, data, duration, hasApplicableDevices){ // console.log("'#'+modalSegmentName+'Action'"); // alert(".product-id : " + data.id); // alert(".duration : " + duration); // alert(".complexOfferId : " + data.complexOfferId); var actionForm = null; if(linkId!='' && linkId!=undefined){ if(linkId.indexOf("-box")>0){ linkId=linkId.substring(0,linkId.indexOf("-box")); } actionForm = $("#"+linkId+" #"+modalSegmentName+'Action').find("form"); // alert("#"+linkId+" #"+modalSegmentName+'Action'); }else{ actionForm = $('#'+modalSegmentName+'Action').find('form'); } // alert(actionForm.find('.product-id')); actionForm.find('.product-id').val(data.id); actionForm.find('.planDuration').val(duration); console.log(".planDuration : " + duration); actionForm.find('.complexOfferId').val(data.complexOfferId); actionForm.attr('action', $('#originatingRequestURI').val()); if (false) { actionForm.find('.btn-continue').addClass('disabled'); } else { actionForm.find('.btn-continue').removeClass('disabled'); } } window.onload = function() { initPlansAndPhones(); }