var windowHeight = 0; var windowWidth = 0; var postMade = false; $(document).ready(function () { $('[data-toggle="tooltip"]').tooltip(); progressbar.set('tc_select_product'); /*Events*/ $("[data-role=filter]").each(function(){ try { if ($(this).prop('name') == 'web_groups[]') { $(this).change(function () { trainingCard.filter.setLinkedContracts(); trainingCard.filter.viewHandler(); trainingCard.filter.execute(true); }); } else { $(this).change(function () { if ($(this).prop('name') == 'personal_trainers[]') { if($(this).val()) { if($('#personal_trainers_show_all').length){ $('#personal_trainers_show_all').prop('checked', false); $('#personal_trainers_show_all').closest('label').removeClass('active'); } } else { //show all selected $('input[name="personal_trainers[]"]').each(function(){ if($(this).val()){ $(this).prop('checked', false); $(this).closest('label').removeClass('active'); } }); } } trainingCard.filter.viewHandler(); trainingCard.filter.execute(true); /*trainingCard.filter.scrollHandler($(this)); JKU poistettu*/ }) } } catch (e){ console.log('Error in document.ready filter events: '+ e.message); } }); /*Read input values*/ $("#tc_customer_details input").each(function () { try { if ($(this).attr('type') != 'radio') { $(this).val(trainingCard.input.get($(this).prop('name'))); } /*Validate input values*/ if($(this).val()) { trainingCard.input.set($(this)); } $(this).blur(function () { trainingCard.input.set($(this)); }); } catch (e){ console.log('Error in customerdetails read input values: '+ e.message); } }); $("#tc_customer_details select").each(function (index) { try { if($(this).prop('name') == 'birthday[]'){ $(this).val(trainingCard.input.get($(this).prop('name'), index)); } /*Validate input values*/ $(this).change(function () { trainingCard.input.set($(this)); }); } catch (e){ console.log('Error in customerdetails validate input values: '+ e.message); } }); //Tabs trainingCard.setOnClick("a[href=#tab_select_product]", function () { if (trainingCard.navigation.getTab() == '#tab_customer_details') { var prodCode = trainingCard.product.get(); trainingCard.product.tabInit(); if ($('#auto_select').length > 0) { trainingCard.product.set(prodCode); trainingCard.product.show(); } progressbar.set('tc_select_product'); } else { trainingCard.product.tabInit(); } }); trainingCard.setOnClick("a[href=#tab_membership]", function () { trainingCard.membership.tabInit(); //trainingCard.membership.show(); }); trainingCard.setOnClick("a[href=#tab_customer_details]", function () { //Set product if (trainingCard.product.get()) { trainingCard.product.view(trainingCard.product.get(), false); } //Set membership product if (trainingCard.membership.get()) { trainingCard.membership.view(trainingCard.membership.get(), false); } trainingCard.customerDetails.show(); }); //Click current tab $("a[href="+trainingCard.navigation.getTab()+"]").click(); trainingCard.show(); //Execute filter if($('input[name="web_groups[]"],select[name="web_groups[]"]').length) { trainingCard.filter.setLinkedContracts(); } trainingCard.filter.viewHandler(); trainingCard.filter.execute(true); if($('#slider_exists').length > 0){ trainingCard.slider.init(); } //remove (if any) filter background if site is in mobile if(site.isMobile()) { //$("#tc_filter").css("background-image", "none"); } progressbar.setOnClick('tc_select_product', function(){ $("a[href=#tab_select_product]").click(); }); if($('#btn_tc_campaign_code').length){ $('#btn_tc_campaign_code').on('click', function(){ if($('body.training_card #campaign_code').val()) { var url = 'index.php?func=training_card&campaign_code=' + $('body.training_card #campaign_code').val(); window.location = url; } }); $('body.training_card #campaign_code').on('keydown', function(e){ if (e.keyCode == 13) { $('#btn_tc_campaign_code').click(); } }); if($('#campaign_code_in_use').length){ $('body.training_card #campaign_code').closest('.row').hide(); } } $(window).resize(function () { ResizeHandler(); }); ResizeHandler(); }); function ResizeHandler() { /*If width has changed*/ if (windowWidth != $(window).width()) { if(site.isMobile()) { if($('#slider_exists').length > 0) { if ($(window).width() < 530) { trainingCard.slider.resize('.slider-horizontal', true); } } } } if($('#slider_exists').length > 0) { /* //resize personal trainer filter even if window size has not changed.. trainingCard.slider.resize('.slider-personal_trainers', true); if($('.slider-tick-label:visible').first().width() == '0'){ //slider lables are overlapping trainingCard.slider.resize('.slider-horizontal', false); }*/ trainingCard.slider.checkOverlappingLabels(); } SaveWindowResolution(); } function SaveWindowResolution () { windowHeight = $(window).height(); windowWidth = $(window).width(); } var trainingCard = new function () { /*trainingCard.customerDetails*/ this.customerDetails = new function () { /*trainingCard.customerDetails.previous*/ this.previous = function() { /* go back from customer details*/ var productType = $("div[data-product='" + trainingCard.product.get() + "']").data('product-type'); if ($("#tab_membership").length && productType == 'contract_product') { $("a[href=#tab_membership]").click(); } else { $("a[href=#tab_select_product]").click(); } if($('#auto_select').length > 0){ trainingCard.filter.viewHandler(); trainingCard.filter.execute(true); } }; /*trainingCard.customerDetails.disable*/ this.disable = function () { trainingCard.setDisable("a[href=#tab_customer_details]"); }; /*trainingCard.customerDetails.enable*/ this.enable = function () { executeFunction = function () { trainingCard.customerDetails.show(); } trainingCard.setEnable("a[href=#tab_customer_details]", executeFunction); }; /*trainingCard.customerDetails.show*/ this.show = function () { if(DEBUG) { console.log('trainingCard.customerDetails.show'); } try { progressbar.set('tc_customer_details'); progressbar.setOnClick('tc_select_product', function (){ trainingCard.customerDetails.previous(); }); $("#mobile_back_to_p_list").addClass('hidden_important'); //Set navigation buttons trainingCard.navigation.setBack(function () { trainingCard.customerDetails.previous(); }); trainingCard.navigation.setContinue(function () { trainingCard.post(); }); trainingCard.navigation.setTab("#tab_customer_details"); this.viewHandler(); trainingCard.show(); } catch (e){ console.log('Error in trainingCard.customerDetails.show: '+ e.message); } }; /*trainingCard.customerDetails.viewHandler*/ this.viewHandler = function () { if(DEBUG) { console.log('trainingCard.customerDetails.viewHandler'); } //Navigation $("#tc_navigation").show(); trainingCard.navigation.showNext(); //show back button trainingCard.navigation.showBack(); }; /*trainingCard.customerDetails.reset*/ this.reset = function () { $("#tc_customer_details input, #tc_customer_details select").each(function () { try { //reset input and cookie value if ($(this).attr('type') != 'radio') { $(this).val(''); //reset cookie value Cookie.set('tc_input_'+ $(this).prop('name'), ''); } } catch (e){ console.log('Error in customerdetails reset: '+ e.message); } }); }; }; /*trainingCard.membership*/ this.membership = new function () { var previewProductCode; /*trainingCard.membership.confirm (modal lightbox VIEW callback) */ this.confirm = function (status) { if(DEBUG) { console.log('trainingCard.membership.confirm'); } if (status) { var productCode = trainingCard.membership.get(); trainingCard.membership.set(productCode); //if tabs then show, else post. if ($("#tab_customer_details").length) { trainingCard.membership.show(); } else { trainingCard.post(); } }else{ //Reset product code trainingCard.membership.tabInit(); } return false; }; /*trainingCard.membership.disable*/ this.disable = function () { trainingCard.setDisable("a[href=#tab_membership]"); }; /*trainingCard.membership.enable*/ this.enable = function () { executeFunction = function () { trainingCard.membership.tabInit(); }; trainingCard.setEnable("a[href=#tab_membership]", executeFunction); }; /*trainingCard.membership.show*/ this.show = function () { if(DEBUG) { console.log('trainingCard.membership.show'); } this.view(this.get(), false); trainingCard.membership.previewProductCode=null; $("#mobile_back_to_p_list").addClass('hidden_important'); if (this.get()) { trainingCard.customerDetails.enable(); $("a[href=#tab_customer_details]").click(); }else { //Set navigation buttons trainingCard.navigation.setBack(function () { $("a[href=#tab_select_product]").click(); }); trainingCard.customerDetails.disable(); trainingCard.navigation.setTab("#tab_membership"); this.viewHandler(); trainingCard.show(); } }; /*trainingCard.membership.beforeSelect*/ this.beforeSelect = function (productCode) { if (DEBUG) { console.log('trainingCard.membership.beforeSelect'); } trainingCard.membership.previewProductCode = productCode; this.checkExpireDate(productCode); } /*trainingCard.membership.select*/ this.select = function (productCode) { if(DEBUG) { console.log('trainingCard.membership.select'); } trainingCard.membership.set(productCode); trainingCard.membership.previewProductCode = null; if (site.isMobile()) { this.view(productCode, true); }else { this.next(productCode); } }; /*trainingCard.membership.doExpireDateCheck*/ this.checkExpireDate = function (productCode) { if (DEBUG) { console.log('trainingCard.membership.checkExpireDate'); } //if membership end_date < contract_product end_date, show confirm. //But only if contract product is not continuous var is_continuous; var contract_product_expire_date; var ms_expire_date; if (trainingCard.product.get()) { contract_product_expire_date = trainingCard.product.getData('expire_date'); } else { //buy only membership if($('#id_exp_date')){ //customer current contract expire date contract_product_expire_date = $('#id_exp_date').val(); } } if (contract_product_expire_date) { //get membership product expire date ms_expire_date = this.getData('expire_date', productCode); if (ms_expire_date) { //parse dates var d1 = parseDate(contract_product_expire_date); //tämä antaa jostain syystä 31.01.2101 jos continuous. (chromessa) var d2 = parseDate(ms_expire_date); //is continuous (2100-12-31) is_continuous = contract_product_expire_date.indexOf('2100') !== -1; if (!is_continuous) { //compare end dates if (d1 && d2 && d2 < d1) { // modal confirm lightbox var html = { title: 'Huom!', body: 'Jäsenyytesi ei kata kortin voimassaoloaikaa', buttons: {accept: 'Jatka', reject: 'Takaisin'} }; showConfirmation(trainingCard.confirm, html); return false; } } } } var selectedProductType = trainingCard.product.selectedProductType(); if(selectedProductType == 'membership_product'){ //buy only membership trainingCard.product.set(productCode); } this.select(productCode); }; /*trainingCard.membership.continue*/ //modal confirm Too Short callback this.continue = function (status) { if(DEBUG) { console.log('trainingCard.membership.continue'); } if(!trainingCard.product.get()){ //buy only membership if(status){ var tmpCode = trainingCard.membership.previewProductCode; trainingCard.membership.previewProductCode = null; trainingCard.product.select(tmpCode); } return false; } if(status){ trainingCard.membership.select(trainingCard.membership.previewProductCode); } else { //Reset product code trainingCard.membership.tabInit(); return false; } } /*trainingCard.membership.get*/ this.get = function () { //Get product code from cookie. return Cookie.get('tc_selected_membership'); }; /*trainingCard.membership.set*/ this.set = function (productCode) { if (productCode) { //Set to form $("#id_membership_product").val(productCode); //Save product code to cookie. return Cookie.set('tc_selected_membership', productCode); }else{ //Remove from form $("#id_membership_product").val(''); //Disable possible other tabs trainingCard.customerDetails.disable(); //Remove saved product code from cookie return Cookie.remove('tc_selected_membership'); } }; /*trainingCard.membership.next*/ this.next = function (productCode) { this.set(productCode); //if tabs then show, else post. if ($("#tab_customer_details").length) { this.show(); }else{ trainingCard.post(); } }; /*trainingCard.membership.tabInit*/ this.tabInit = function () { if(DEBUG) { console.log('trainingCard.membership.tabInit'); } hideUserNotification(); trainingCard.navigation.setTab("#tab_membership"); //set selected product code to form var productCode = trainingCard.product.get(); if(productCode) { trainingCard.product.set(productCode); } //Remove selected product trainingCard.membership.set(); trainingCard.membership.previewProductCode=null; //Execute product select with empty product code //trainingCard.membership.view('', false); trainingCard.membership.show(); }; /*trainingCard.membership.view*/ this.view = function (productCode, modalSummary) { if(DEBUG) { console.log('trainingCard.membership.view'); } this.set(productCode); trainingCard.summary.show(trainingCard.membership, modalSummary); }; /*trainingCard.membership.viewHandler*/ this.viewHandler = function () { if(DEBUG) { console.log('trainingCard.membership.viewHandler'); } //Navigation $("#tc_navigation").show(); trainingCard.navigation.hideNext(); }; /*trainingCard.membership.getData*/ this.getData = function(attribute, productCode) { if(!productCode) { productCode = trainingCard.membership.get(); } var product_row; if(productCode) { //get contract product end date product_row = $('.tc_product_list [data-product="' + productCode + '"]'); if ($(product_row)) { return $(product_row).data(attribute); } } return false; }; }; /*trainingCard.option*/ this.option = new function (readOptionInputs) { this.values = { 'personalTrainers': {'dataName': 'personal_trainer', 'values': []}, }; } /*trainingCard.filter*/ this.filter = new function (readFilterInputs) { this.values = { 'productTypes': {'dataName': 'product-type', 'values': []}, 'commitMonths': {'dataName': 'commit-month', 'values': []}, 'frequencies': {'dataName': 'frequency', 'values': []}, 'contracts': {'dataName': 'contract', 'values': []}, 'sc_types': {'dataName': 'sc_type', 'values': []}, 'products': {'dataName': 'product', 'values': []}, 'personalTrainers': {'dataName': 'personal_trainer', 'values': []}, 'webGroups': {'dataName': 'web-group', 'values': []}, }; this.linkedContractCount = 0; this.selectedContractIndex = 0; this.previousLinkedContractCount = 0; this.previousSelectedWebGroupIndex = null; this.previousLinkedContracts = ''; /*trainingCard.filter.execute*/ this.execute = function (readFilterInputs) { if(DEBUG) { console.log('trainingCard.filter.execute'); } try { if (readFilterInputs) { this.readInputs(); } $("#no_results").hide(); //reset price preview $('.pricePreview').html(''); $('.pricePreview').hide(); //Show all rows $(".product_row").show(); var autoSelectProductCode; //Loop products $(".product_row").each(function () { $(this).show(); for (var filter in trainingCard.filter.values) { var value = $(this).data(trainingCard.filter.values[filter]['dataName']); //Ex data-commit-month if (trainingCard.filter.hideRow(value, trainingCard.filter.values[filter]['values'])) { $(this).hide(); break; } } if ($(this).is(":visible")) { if (!autoSelectProductCode) { autoSelectProductCode = $(this).data("product"); } } }); var numOfVisibleRows = $('.product_row').filter(function () { return $(this).css('display') !== 'none'; }).length; if (numOfVisibleRows == 0) { $("#no_results").show(); } var selectedProductType = trainingCard.product.selectedProductType(); if ($('#auto_select').length > 0) { //auto select first product if (numOfVisibleRows > 0 && selectedProductType == 'contract_product') { if(autoSelectProductCode){ trainingCard.product.preview(autoSelectProductCode); } if (numOfVisibleRows == 1) { //hide product list $('.product_row').hide(); } else { //show product list $('.product_row').show(); } } else { if(numOfVisibleRows > 0){ trainingCard.navigation.hideNext(); } //hide summary summary.hide(); if(DEBUG) { //console.log('summary hidden: numOfVisibleRows: ' + numOfVisibleRows + ' selectedProductType: ' + selectedProductType); } } } } catch (e){ console.log('Error in trainingCard.filter.execute: '+ e.message); } }; /*trainingCard.filter.readInputs*/ this.readInputs = function () { if(DEBUG) { console.log('trainingCard.filter.readInputs'); } try { trainingCard.filter.values.productTypes.values = []; trainingCard.filter.values.commitMonths.values = []; trainingCard.filter.values.frequencies.values = []; trainingCard.filter.values.contracts.values = []; trainingCard.filter.values.sc_types.values = []; trainingCard.filter.values.products.values = []; trainingCard.filter.values.webGroups.values = []; trainingCard.option.values.personalTrainers.values = []; //Read filter values $('input[name="product_types[]"]').each(function () { if ($(this).prop('checked')) { trainingCard.filter.values.productTypes.values.push($(this).val()); } }); var selectedProductType = trainingCard.product.selectedProductType(); if (selectedProductType == 'contract_product') { $('input[name="commit_months[]"]').each(function () { if ($(this).prop('checked')) { trainingCard.filter.values.commitMonths.values.push($(this).val()); } }); $('input[name="frequencies[]"]').each(function () { if ($(this).prop('checked')) { trainingCard.filter.values.frequencies.values.push($(this).val()); } }); $('input[name="contracts[]"]').each(function () { if ($(this).prop('checked')) { trainingCard.filter.values.contracts.values.push($(this).val()); } }); if ($('input[name="web_groups[]"]').length) { $('input[name="web_groups[]"]').each(function () { if ($(this).prop('checked')) { trainingCard.filter.values.webGroups.values.push($(this).val()); } }); } if ($('select[name="web_groups[]"]').length) { if($('select[name="web_groups[]"]').val()){ trainingCard.filter.values.webGroups.values.push($('select[name="web_groups[]"]').val()); } } //read options if ($('input[name="personal_trainers[]"]').length) { $('input[name="personal_trainers[]"]').each(function () { if ($(this).prop('checked')) { if($(this).val()) { trainingCard.option.values.personalTrainers.values.push($(this).val()); } } }); } } if (selectedProductType == 'single_card') { $('input[name="sc_types[]"]').each(function () { if ($(this).prop('checked')) { trainingCard.filter.values.sc_types.values.push($(this).val()); } }); } $('input[name="products[]"]').each(function () { if ($(this).prop('checked')) { trainingCard.filter.values.products.values.push($(this).val()); } }); } catch (e){ console.log('Error in trainingCard.filter.readInputs: '+ e.message); } }; /*trainingCard.filter.setLinkedContracts*/ this.setLinkedContracts = function () { if(DEBUG) { console.log('trainingCard.filter.setLinkedContracts'); } try { //show contract options according to web group //hide all if (($('input[name="web_groups[]"]').length > 0 || $('select[name="web_groups[]"]').length > 0) && $('input[name="contracts[]"]').length > 0) { $('.contracts_area').show(); var linkedContracts = new Array(); var webGroup; var selectedWebGroupIndex = 0; var webGroups = $('input[name="web_groups[]"]').length; if($('select[name="web_groups[]"]').length > 0){ webGroups = $('select[name="web_groups[]"] option').length; } var newSelectedIndex = 0; //get selected web group $('input[name="web_groups[]"]').each(function (index) { if ($(this).is(':checked')) { webGroup = $(this).val(); selectedWebGroupIndex = index; } }); if($('select[name="web_groups[]"]').length > 0){ webGroup = $('select[name="web_groups[]"]').val(); selectedWebGroupIndex = $('select[name="web_groups[]"] option:selected').index(); } if(trainingCard.filter.previousSelectedWebGroupIndex == null){ trainingCard.filter.previousSelectedWebGroupIndex = selectedWebGroupIndex; } if(trainingCard.filter.previousSelectedWebGroupIndex == null){ trainingCard.filter.previousSelectedWebGroupIndex = 0; } $('input[name="contracts[]"]').each(function (index) { if($(this).prop('checked')){ trainingCard.filter.selectedContractIndex = index; } //hide all contracts $(this).hide(); //radio $(this).prop('checked', false); $(this).prop('disabled', true); //for slider $(this).parent().hide(); //label $(this).parent().removeClass('active'); //Loop products $(".product_row").each(function () { if ($(this).data('web-group') == webGroup) { if (linkedContracts.indexOf($(this).data('contract')) == -1) { linkedContracts.push($(this).data('contract')); } } }); }); if(DEBUG) { /* console.log('previously selected index:: ' + trainingCard.filter.selectedContractIndex); console.log('number of previous linkedContracts:: ' + trainingCard.filter.previousLinkedContractCount); console.log('number of linkedContracts:: ' + linkedContracts.length); */ } if((linkedContracts.length <= trainingCard.filter.previousLinkedContractCount || trainingCard.filter.previousLinkedContractCount == 0)){ //try to maintain same selected option index even if contract options/indexes have changed //get new selected index var idX = linkedContracts.length; if(selectedWebGroupIndex > trainingCard.filter.previousSelectedWebGroupIndex) { //changed level up, add //how many levels? if(trainingCard.filter.previousLinkedContractCount == linkedContracts.length) { var levels = selectedWebGroupIndex - trainingCard.filter.previousSelectedWebGroupIndex; newSelectedIndex = trainingCard.filter.selectedContractIndex + (idX * levels); } } if(selectedWebGroupIndex < trainingCard.filter.previousSelectedWebGroupIndex){ //changed level down, subtract //how many levels? if(trainingCard.filter.previousLinkedContractCount == linkedContracts.length) { var levels = trainingCard.filter.previousSelectedWebGroupIndex - selectedWebGroupIndex; newSelectedIndex = trainingCard.filter.selectedContractIndex - (idX * levels); if (newSelectedIndex < 0 && trainingCard.filter.selectedContractIndex < idX) { //keep index newSelectedIndex = trainingCard.filter.selectedContractIndex; } } } if(selectedWebGroupIndex == trainingCard.filter.previousSelectedWebGroupIndex) { newSelectedIndex = trainingCard.filter.selectedContractIndex; } if(trainingCard.filter.previousLinkedContractCount == linkedContracts.length){ //keep index newSelectedIndex = trainingCard.filter.selectedContractIndex; } if(DEBUG) { //console.log('new index:: ' + newSelectedIndex); } } $('input[name="contracts[]"]').each(function (i) { var contract = $(this).val().toString(); var contractElement = $(this); var itemSelected = false; $.each(linkedContracts, function (index, value) { if (value.toString() == contract) { $(contractElement).show(); $(contractElement).prop('disabled', false); //for slider if(!$(contractElement).prop('checked')){ $(contractElement).parent().removeClass('active'); } $(contractElement).parent().show(); if((linkedContracts.length <= trainingCard.filter.previousLinkedContractCount || trainingCard.filter.previousLinkedContractCount == 0)){ if(i == newSelectedIndex){ if(DEBUG) { //console.log('selecting index:: ' + (index+i)); } $(contractElement).prop('checked', true); $(contractElement).parent().addClass('active'); } } else { if(newSelectedIndex == 0 && !$('#contract_show_all').length) { //select first visible option if (index == 0) { if (DEBUG) { //console.log('selecting index:: ' + index); } $(contractElement).prop('checked', true); $(contractElement).parent().addClass('active'); } } } } }); }); trainingCard.filter.selectedContractIndex = newSelectedIndex; trainingCard.filter.previousLinkedContractCount = linkedContracts.length; trainingCard.filter.previousSelectedWebGroupIndex = selectedWebGroupIndex; trainingCard.filter.previousLinkedContracts = linkedContracts; if ($('#slider_exists').length > 0 && $('.slider').length > 0) { //re-create contracts slider $('.slider-contracts').remove(); trainingCard.slider.create('contracts'); if(site.isMobile()) { if ($(window).width() < 530) { trainingCard.slider.resize('.slider-contracts', false); } } } else { if ($('input[name="contracts[]"]:visible').length == 0){ //no contracs are visible, hide heading if(DEBUG) { //console.log('no contracts visible'); } $('.sp_sub_header_contracts').hide(); } else { $('.sp_sub_header_contracts').show(); } } if ($('input[name="contracts[]"]:visible').length <= 1) { //25.3.2019 if contracts 1 or less, contract filter not visible #19570 $('.contracts_area').hide(); } } } catch (e){ console.log('Error in trainingCard.filter.setLinkedContracts: '+ e.message); } } /*trainingCard.filter.viewHandler*/ this.viewHandler = function() { if(DEBUG) { console.log('trainingCard.filter.viewHandler'); } try { //show filters according to product type if ($('[name="product_types[]"]').length > 0) { $(".product_type_filter").hide(); var selectedProductType = trainingCard.product.selectedProductType(); if (selectedProductType) { if ($("#" + selectedProductType + "_filter").length) { $("#" + selectedProductType + "_filter").show(); } } if (selectedProductType == 'membership_product') { $("#tc_sp_membership_info").show(); } else { $("#tc_sp_membership_info").hide(); } } } catch (e){ console.log('Error in trainingCard.filter.viewHandler: '+ e.message); } }; /*trainingCard.filter.hideRow*/ this.hideRow = function (value, filterValues) { if (filterValues.length > 0) { if (filterValues[0] == '') { return false; } if (!InArray(value, filterValues)) { return true; } } return false; }; /*trainingCard.filter.scrollHandler*/ this.scrollHandler = function (caller) { if(DEBUG) { console.log('trainingCard.filter.scrollHandler'); } site.scrollTo('#tc_filter'); }; }; /*trainingCard.product*/ this.product = new function () { /*trainingCard.product.confirm (modal lightbox callback) */ this.confirm = function (status) { if(DEBUG) { console.log('trainingCard.product.confirm'); } if (status) { var productCode = trainingCard.product.get(); trainingCard.product.set(productCode); //if tabs then show, else post. if ($("#id_frm_training_card .nav-tabs").length) { trainingCard.product.show(); } else { trainingCard.post(); } }else{ //Reset product code trainingCard.product.set(); trainingCard.membership.set(); /*Set to product selected view*/ trainingCard.product.viewHandler(); } return false; }; /*trainingCard.product.next*/ this.next = function (productCode) { this.set(productCode); //if tabs then show, else post. if ($("#id_frm_training_card .nav-tabs").length) { this.show(); }else{ trainingCard.post(); } }; /*trainingCard.product.get*/ this.get = function () { //Get product code from cookie. return Cookie.get('tc_selected_product'); }; /*trainingCard.product.set*/ this.set = function (productCode) { if (productCode) { //Set to form $("#id_rcr_product").val(productCode); //Save product code to cookie. return Cookie.set('tc_selected_product', productCode); }else{ //Remove from form $("#id_rcr_product").val(''); //Disable possible other tabs trainingCard.membership.disable(); trainingCard.customerDetails.disable(); //Remove saved product code from cookie return Cookie.remove('tc_selected_product'); } }; /*trainingCard.product.select*/ this.select = function (productCode) { if(DEBUG) { console.log('trainingCard.product.select'); } try { if($('#user_input_required').length){ //reset flags trainingCard.userConfirmation.shown = false; trainingCard.userConfirmation.handled = false; //set product code this.set(productCode); //show user confirmations (modal lightbox) trainingCard.userConfirmation.show(); } else { var productType = $("div[data-product='" + productCode + "']").data('product-type'); if (site.isMobile() && (productType != 'contract_product' || $("#tab_membership").length == 0)) { this.view(productCode, true); } else { this.next(productCode); } } } catch (e){ console.log('Error in trainingCard.product.select: '+ e.message); } }; /*trainingCard.product.show*/ this.show = function () { if(DEBUG) { console.log('trainingCard.product.show'); } try { if (this.get()) { $("#no_results").hide(); //Set navigation buttons trainingCard.navigation.setBack(function () { trainingCard.product.productList(); }); //Where to go var productType = $("div[data-product='" + this.get() + "']").data('product-type'); if ($("#tab_membership").length && productType == 'contract_product') { //Free customer details trainingCard.membership.enable(); $("a[href=#tab_membership]").click(); } else { if ($("#tab_customer_details").length) { this.view(this.get(), false); //Free customer details trainingCard.customerDetails.enable(); $("a[href=#tab_customer_details]").click(); if (site.isMobile) { site.scrollTo('body'); } } else { trainingCard.post(); } } } else { this.productList(); trainingCard.show(); trainingCard.customerDetails.disable(); trainingCard.navigation.setTab("#tab_select_product"); } if (site.isMobile()) { summary.hide(); } } catch (e){ console.log('Error in trainingCard.product.show: '+ e.message); } }; /*trainingCard.product.preview*/ this.preview = function (productCode) { if(DEBUG) { console.log('trainingCard.product.preview'); } trainingCard.product.set(productCode); trainingCard.summary.show(trainingCard.product, false); if($('#auto_select').length > 0) { //Navigation $("#tc_navigation").show(); trainingCard.navigation.showNext(); trainingCard.navigation.hideBack(); trainingCard.navigation.setContinue(function () { trainingCard.product.select(productCode); }); } } /*trainingCard.product.tabInit*/ this.tabInit = function () { if(DEBUG) { console.log('trainingCard.product.tabInit'); } hideUserNotification(); trainingCard.navigation.setTab("#tab_select_product"); //Remove selected product trainingCard.product.set(''); trainingCard.membership.set(''); //Execute product select with empty product code trainingCard.product.view('', false); trainingCard.product.viewHandler(); }; /*trainingCard.product.view*/ this.view = function (productCode, modalSummary) { if(DEBUG) { console.log('trainingCard.product.view'); } try { this.set(productCode); var productType = $("div[data-product='" + trainingCard.product.get() + "']").data('product-type'); if (trainingCard.navigation.getTab() == '#tab_customer_details' && $("#tab_membership").length && productType == 'contract_product') { //no need to create summary if we are on the customerdetails tab, and have selected a contract product, membership.view will do that } else { trainingCard.summary.show(trainingCard.product, modalSummary); } progressbar.set('tc_select_product'); } catch (e){ console.log('Error in trainingCard.product.view: '+ e.message); } }; /*trainingCard.product.productList*/ this.productList = function () { if(DEBUG) { console.log('trainingCard.product.productList'); } //Change header $("#tc_header_select_product").html(""); //Remove saved product code from cookie this.set(''); trainingCard.membership.set(''); //Run filter with input values //trainingCard.filter.execute(true); //Change visibilities this.viewHandler(); if (site.isMobile()) { summary.hide(); } trainingCard.customerDetails.disable(); }; /*trainingCard.product.viewHandler*/ this.viewHandler = function () { if(DEBUG) { console.log('trainingCard.product.viewHandler'); } if (this.get()) { //Navigation $("#tc_navigation").show(); //Mobile back to product list $("#mobile_back_to_p_list").removeClass('hidden_important'); }else{ if($('#auto_select').length > 0){ trainingCard.filter.viewHandler(); trainingCard.filter.execute(true); } else { //Navigation $("#tc_navigation").hide(); //Mobile back to product list $("#mobile_back_to_p_list").addClass('hidden_important'); } } }; /*trainingCard.product.selectedProductType*/ this.selectedProductType = function() { try { if ($('input[name="product_types[]"]').length > 0) { //there are several product types, check which is selected return $('input[name="product_types[]"]:checked').val(); } else { if ($('#contract_product_filter input').length > 0) { return 'contract_product'; } else if ($('#single_card_filter input').length > 0) { return 'single_card'; } else { //no filter if(trainingCard.product.get()){ return trainingCard.product.getData('product-type'); } return 'membership_product'; } } } catch (e){ console.log('Error in trainingCard.product.selectedProductType: '+ e.message); } }; /*trainingCard.product.getData*/ this.getData = function(attribute, productCode) { if(!productCode) { productCode = trainingCard.product.get(); } var product_row; if(productCode) { //get contract product end date product_row = $('.tc_product_list .product_row[data-product="' + productCode + '"]'); if ($(product_row)) { return $(product_row).data(attribute); } } return false; }; }; /*trainingCard.summary*/ this.summary = new function () { /*trainingCard.summary.show*/ this.show = function (caller, modalSummary) { if(DEBUG) { console.log('trainingCard.summary.show'); } try { var productCode = trainingCard.product.get(); var productType = $("div[data-product='" + productCode + "']").data('product-type'); var membershipProduct = trainingCard.membership.get(); var url = 'index.php?func=tc_summary&products[' + productType + ']=' + productCode; if (membershipProduct) { url = url + '&products[membership_product]=' + membershipProduct; } //options if(productCode) { for (var option in trainingCard.option.values) { var dataName = trainingCard.option.values[option]['dataName']; var values = trainingCard.option.values[option]['values']; if (values.length > 0) { for (var val in values) { url += '&options[' + dataName + '][]=' + values[val]; } } } } //start date url += '&start_date=' + $('#id_date_saved').val(); //save card number url += '&save_card_no=' + $('#id_save_card_no').val(); //get product details $.getJSON(url, function (html) { if (productCode && !site.isMobile()) { //Show summary summary.maximize(); } else { //Hide summary summary.hide(); if(DEBUG) { //console.log('summary hidden: productCode: ' + productCode + ' url: ' + url); } } $("#base_summary_content").html(html); //Change header $("#tc_header_select_product").html('Olet valinnut'); //Set to product selected view //caller.viewHandler(); //Show training card content trainingCard.show(); if (site.isMobile) { //site.scrollTo('body'); if (modalSummary) { // modal lightbox var html = { title: 'Olet valitsemassa', body: $("#base_summary_content").html(), buttons: {accept: 'Jatka', reject: 'Takaisin'} }; showConfirmation(trainingCard.confirm, html); } } //reset price preview $('.pricePreview').html(''); $('.pricePreview').hide(); if (site.isMobile && productCode) { //show price preview var priceElement; //get price from summary priceElement = $('#base_summary').find('#tc_summary').find('.su_price').find('td'); if (!$(priceElement).length) { //get total price instead priceElement = $('#base_summary').find('#tc_summary').find('.total'); } if ($(priceElement).length) { var price = $(priceElement).html(); if (price != '') { $('.pricePreview').html('' + price + ''); $('.pricePreview').show(); } } } }); } catch (e){ console.log('Error in trainingCard.summary.show: '+ e.message); } } }; /*trainingCard.navigation*/ this.navigation = new function () { this.tab = '#tab_select_product'; /*trainingCard.navigation.setBack*/ this.setBack = function (setFunction) { trainingCard.setOnClick("#btn_tc_navigation_back", setFunction); }; this.setContinue = function (setFunction) { trainingCard.setOnClick("#btn_tc_navigation_continue", setFunction); }; this.setTab = function (tab) { this.tab = tab; Cookie.set ('tc_current_tab', tab); }; this.getTab = function () { if (Cookie.get ('tc_current_tab')) { this.tab = Cookie.get('tc_current_tab'); } return this.tab; }; /*trainingCard.navigation.hideNext*/ this.hideNext = function () { $("#btn_tc_navigation_continue").hide(); }; /*trainingCard.navigation.showNext*/ this.showNext = function () { $("#btn_tc_navigation_continue").show(); }; /*trainingCard.navigation.showBack*/ this.showBack = function () { $("#btn_tc_navigation_back").show(); }; /*trainingCard.navigation.hideBack*/ this.hideBack = function () { $("#btn_tc_navigation_back").hide(); }; }; /*trainingCard.input*/ this.input = new function () { /*trainingCard.input.get*/ this.get = function (name, index) { var val = Cookie.get('tc_input_'+name); if(name == 'birthday[]'){ if(index != undefined && val != '') { if(DATEFMT == 'd.m.Y') { var tmp = val.split('.'); } else { var tmp = val.split('-'); } val = tmp[index]; } } return val; }; /*trainingCard.input.getErrorMessage*/ this.getErrorMessage = function (jQInput) { return 'Virhe kentässä: ' + jQInput.attr('placeholder') + "
"; }; /*trainingCard.input.integer*/ this.getInteger = function (str) { str = str.replace(/[^0-9.]/g, ''); str = str.replace(/(\..*)\./g, '$1'); return str; }; /*trainingCard.input.set*/ this.set = function (jQInput) { try { var str = jQInput.val(); //Set to uppercase if (jQInput.attr('type') == 'text') { str = str.substring(0, 1).toLocaleUpperCase() + str.substring(1); } //Trim str = str.trim(); /*Data fetch*/ switch (jQInput.prop('name')) { case 'zip': str = this.getInteger(str); $.getJSON("index.php?func=tc_search&case=city&zip=" + str, function (data) { if (data) { $('#id_city').val(data); } }); break; } /*Validation*/ if (!jQInput.attr('required') && str == '') { jQInput.get(0).setCustomValidity(''); } else { switch (jQInput.prop('name')) { case 'social_sec_no': //set last character to uppercase var lastChar = str.substring(str.length-1).toLocaleUpperCase(); str = str.substring(0, str.length-1)+lastChar; if (validator.isValid.socialSecNo($("input[name=country_code]").val(), str)) { jQInput.get(0).setCustomValidity(''); } else { jQInput.get(0).setCustomValidity('Virheellinen henkilötunnus'); } break; case 'birthday[]': var obj = $('[name="birthday[]"]'); var tmp = ''; var d = DATEFMT == 'd.m.Y' ? $(obj[0]).val() : $(obj[2]).val(); var m = $(obj[1]).val(); var y = DATEFMT == 'd.m.Y' ? $(obj[2]).val() : $(obj[0]).val(); //check if all values are selected if (d != '' && $.isNumeric(d) && m != '' && $.isNumeric(m) && y != '' && $.isNumeric(y)) { if(DATEFMT == 'd.m.Y'){ var strVal = d + '.' + m + '.' + y; } else { var strVal = y + '-' + m + '-' + d; } //pad day and month if (d.length == 1) { d = '0' + d; } if (m.length == 1) { m = '0' + m; } tmp = y + '-' + m + '-' + d; } if(DEBUG) { console.log(tmp); } if (tmp != '') { if (validator.isValid.date(tmp)) { jQInput.get(0).setCustomValidity(''); } else { jQInput.get(0).setCustomValidity('Virheellinen syntymäaika'); } } else { jQInput.get(0).setCustomValidity(''); } break; case 'email_confirm': if($('input[name="email"]').length){ if($('input[name="email"]').val() != $('input[name="email_confirm"]').val()){ jQInput.get(0).setCustomValidity('Sähköpostiosoitteet eivät täsmää. Ole hyvä ja tarkasta antamasi osoitteet.'); } else { jQInput.get(0).setCustomValidity(''); } } break; } } if (jQInput[0].checkValidity()) { jQInput.removeClass('invalid'); if(jQInput.prop('name') == 'birthday[]'){ if(strVal){ Cookie.set('tc_input_' + jQInput.prop('name'), strVal); } } else { Cookie.set('tc_input_' + jQInput.prop('name'), jQInput.val()); } } else { jQInput.addClass('invalid'); } jQInput.val(str); } catch (e){ console.log('Error in trainingCard.input.set: '+ e.message); } }; this.showErrors = function () { var errorMsg = ''; $("input").each(function (){ $(this).removeClass('invalid'); }); $("select").each(function (){ $(this).removeClass('invalid'); }); $("input:invalid").each(function (){ errorMsg = errorMsg + trainingCard.input.getErrorMessage($(this)); $(this).addClass('invalid'); }); $("select:invalid").each(function (){ errorMsg = errorMsg + trainingCard.input.getErrorMessage($(this)); $(this).addClass('invalid'); }); if (errorMsg) { showUserNotification ('danger', errorMsg); return true; }else{ hideUserNotification(); return false; } }; }; /*trainingCard.slider*/ this.slider = new function () { this.getMultiplyValue = function () { return 1; //100 JKU HACK to get smooth scrolling } /*trainingCard.slider.init*/ this.init = function () { //hide all filter buttons :) $('#tc_filter .btn-group').hide(); var name = ''; //loop through filters $('#tc_filter .btn-group').each(function(){ //get radio element name name = $(this).find('.filter_input').first().attr('name'); name = name.replace('[]', ''); if($('#slider_' + name).length > 0){ //slider element exists, create slider attributes trainingCard.slider.create(name); } }); } /*trainingCard.slider.create*/ this.create = function (name) { if(DEBUG) { console.log('trainingCard.slider.create'); } try { //create the slider var slider = new Slider('#slider_' + name, { min: 0, max: trainingCard.slider.getMultiplyValue() * ($('input[name="' + name + '[]"]').length - 1), ticks: this.getSteps(name), ticks_labels: this.getTexts(name), //ticks_snap_bounds: 30, //not possible? value: this.getInitValue(name), tooltip: trainingCard.slider.getTooltipVisibility(name), tooltip_position: 'bottom', //formatter: function (value) { //return trainingCard.slider.getTooltip(name, value); //} }); $('#slider_' + name).prev().addClass('slider-' + name); /*$('#slider_' + name).on("change", function (slideEvt) { console.log(slideEvt); var newValue = slideEvt.value.newValue; console.log('slider clicked! oldvalue:: ' + $('#slider_' + name).val() + ' newvalue:: ' + newValue); trainingCard.slider.onChangeEvent(name, newValue); });*/ if(site.isMobile()){ $('.slider-' + name).on("mouseup touchend", function(slideEvt){ // mobile devices release event. On slider area //get current slider (input) value var newValue = $('#slider_' + name).val(); if(DEBUG) { console.log('slider drag stopped! new value:: ' + newValue); } trainingCard.slider.onChangeEvent(name, newValue); }); } else { $('.' + name + '_area').on("mouseup touchend", function(slideEvt){ // desktop release event. On whole surrounding area. //get current slider (input) value var newValue = $('#slider_' + name).val(); if(DEBUG) { console.log('slider drag stopped! new value:: ' + newValue); } trainingCard.slider.onChangeEvent(name, newValue); }); } } catch (e){ console.log('Error in trainingCard.slider.create: '+ e.message); } } /*trainingCard.slider.getTexts*/ this.getTexts = function (name) { var filterTexts = new Array(); $('input[name="'+ name +'[]"]').each(function () { if(!$(this).prop('disabled')) { filterTexts.push($(this).next().html()); } }); //console.log(filterTexts); return filterTexts; } /*trainingCard.slider.getSteps*/ this.getSteps = function (name) { var filterSteps = new Array(); var counter = 0; $('input[name="'+ name +'[]"]').each(function () { if(!$(this).prop('disabled')) { filterSteps.push(trainingCard.slider.getMultiplyValue() * counter); counter++; } }); //console.log(filterSteps); return filterSteps; } /*trainingCard.slider.getTooltip*/ this.getTooltip = function (name, value) { var counter = 0; var text = ''; if (value > 0) { value = value / trainingCard.slider.getMultiplyValue(); } $('input[name="'+ name +'[]"]').each(function () { if(!$(this).prop('disabled')) { if (counter == value) { if($(this).parent().data('original-title')){ text = $(this).parent().data('original-title'); } else { text = $(this).next().html(); } } counter++; } }); return text; } /*trainingCard.slider.getTooltipVisibility*/ this.getTooltipVisibility = function (name) { //if(name=='personal_trainers') //return ''; //show tooltip return 'hide'; } /*trainingCard.slider.getInitValue*/ this.getInitValue = function (name) { var retVal=0; var index=0; var bRetVal = false; $('input[name="'+ name +'[]"]').each(function () { if(!$(this).prop('disabled')) { if ($(this).prop('checked') == true) { if(index > 0) { retVal = trainingCard.slider.getMultiplyValue() * index; } bRetVal = true; return retVal; //exit loop } index++; } }); if(bRetVal) { return retVal; } else { return 0; } } /*trainingCard.slider.onChangeEvent*/ this.onChangeEvent = function (name, value) { //click the appropriate filter button try { if (value > 0) { value = value / trainingCard.slider.getMultiplyValue(); } if(DEBUG) { console.log('slider onchange event:: ' + value); } var counter = 0; $('input[name="' + name + '[]"]').each(function () { if (!$(this).prop('disabled')) { if (counter == value) { $(this).click(); //for some reason, slider gets stuck onto mouse sometimes and is dragged whenever mouse is moved, set focus to some element instead //$('#btn_tc_navigation_continue').focus(); } counter++; } }); trainingCard.slider.checkOverlappingLabels(); if(site.isMobile()) { if ($(window).width() < 530) { trainingCard.slider.resize('.slider-'+name, true); } } return; } catch (e){ console.log('Error in trainingCard.slider.onChangeEvent: '+ e.message); } } /*trainingCard.slider.checkOverlappingLabels*/ this.checkOverlappingLabels = function () { $('.slider-horizontal').each(function(){ var parent = $(this); var firstLabel = $(this).find('.slider-tick-label:visible').first(); if($(firstLabel).length){ if($(firstLabel).width() == '0') { //re-create slider var input = $(this).next(':input'); if($(input).length){ var name = $(input).attr('id').substring($(input).attr('id').indexOf('_')+1); if(name){ $(this).remove(); trainingCard.slider.create(name); if(site.isMobile()) { if ($(window).width() < 530) { trainingCard.slider.resize('.slider-'+name, false); } } } } } } }); } /*trainingCard.slider.resize*/ this.resize = function (element, resize_all) { try { if(DEBUG) { console.log('trainingCard.slider.resize:: ' + element); } var maxSize = $('#tc_filter').css('width'); var sliderElement, sliderLabel, sliderTickPosition, labelWidth, labelFontSize; if(site.isMobile()){ maxSize = maxSize.replace('px', '') - 70; //remove padding labelWidth = '87px'; // '100px'; //50 labelFontSize = '12px'; } else { maxSize = maxSize.replace('px', ''); //remove padding labelWidth = '100px'; labelFontSize = '14px'; } $(element).find('.slider-tick-label-container').each(function(){ $(this).css('margin-left', '41.25px'); }); $(element).each(function (index) { sliderElement = $(this); //change slider size $(sliderElement).css('max-width', maxSize + 'px'); //find labels $(sliderElement).find('.slider-tick-label').each(function (indexLabel) { sliderLabel = $(this); if (indexLabel > 0 || resize_all || site.isMobile()) { //find related tick position $(sliderElement).find('.round').each(function (indexPos) { if (indexLabel == indexPos) { //re-position tick label according to tick position sliderTickPosition = $(this); var left = $(sliderTickPosition).css('left'); left = left.replace('%', ''); var prevLeft; if(site.isMobile()) { left = left.replace('px', '') - 45; } else { left = left.replace('px', '') - 50; } if(left != prevLeft && (left > prevLeft || !prevLeft) && left && left.toString() != '0%') { $(sliderLabel).css('left', left + 'px'); $(sliderLabel).css('position', 'absolute'); $(sliderLabel).css('white-space', 'normal'); $(sliderLabel).css('height', '50px'); $(sliderLabel).css('max-width', labelWidth); $(sliderLabel).css('min-width', labelWidth); $(sliderLabel).css('word-break', 'break-all'); $(sliderLabel).css('word-wrap', 'break-word'); $(sliderLabel).css('font-size', labelFontSize); } prevLeft = left; } }); } }) }); } catch (e){ console.log('Error in trainingCard.slider.resize: '+ e.message); } } } /*trainingCard.hide*/ this.hide = function () { $("#training_card").hide(); }; /*trainingCard.setOnClick*/ this.setOnClick = function (selector, setFunction) { //Reset first, otherwise there are multiple calls $(selector).unbind("click"); $(selector).click(setFunction); }; /*trainingCard.setDisable*/ this.setDisable = function (selector) { $(selector).addClass('disabled'); $(selector).parent().addClass('disabled'); trainingCard.setOnClick(selector, function (){return false;}); }; /*trainingCard.setEnable*/ this.setEnable = function (selector, executeFunction) { $(selector).removeClass('disabled'); $(selector).parent().removeClass('disabled'); if (executeFunction) { trainingCard.setOnClick(selector, function () { executeFunction(); }); } }; /*trainingCard.show*/ this.show = function () { $("#training_card").show(); }; /*trainingCard.post*/ this.post = function () { try { if (this.input.showErrors()) { //Errors in form return false; } //JK 06.07.2016 Prevent double submits if (postMade) { return false; } postMade = true; ShowPageLoading(); hideUserNotification(); ScrollTo("#img_loading"); //$('#id_frm_training_card')[0].checkValidity(); var url = 'index.php?func=tc_post'; var currentLocation = document.location; if (currentLocation.toString().indexOf('inframe=1') > -1) { url += '&inframe=1'; } if (currentLocation.toString().indexOf('iframe=0') > -1) { url += '&iframe=0'; } $.post(url, $("#id_frm_training_card").serialize(), function (data) { }) .done(function (redirectUrl) { window.location = redirectUrl; }) .fail(function (data) { postMade = false; //Allow to submit again var errorMsg = ''; if (data && data.responseJSON && data.responseJSON.errors) { for (errorCode in data.responseJSON.errors) { errorMsg = errorMsg + "

" + data.responseJSON.errors[errorCode] + "

"; } } else { //something went really wrong errorMsg = errorMsg + "

" + 'Toiminto epäonnistui. Ole hyvä yritä hetken kuluttua uudelleen' + "

"; } showUserNotification('danger', errorMsg); HidePageLoading(); }); } catch (e){ console.log('Error in trainingCard.post: '+ e.message); } }; /*trainingCard.confirm (common modal lightbox callback)*/ this.confirm = function(status){ if(DEBUG) { console.log('trainingCard.confirm'); } try { //determine selected product type, then use that callback function var selectedProductType = trainingCard.product.selectedProductType(); if($('#user_input_required').length){ if(trainingCard.userConfirmation.shown){ if(!trainingCard.userConfirmation.handled){ //user input filled in trainingCard.userConfirmation.confirm(status); return; } } else { if (site.isMobile() && (selectedProductType != 'contract_product' || $("#tab_membership").length == 0)) { } else { //show user confirmations (modal lightbox) trainingCard.userConfirmation.show(); return; } } } if (selectedProductType == 'contract_product' && $("#tab_membership").length) { if(site.isMobile()) { if(!trainingCard.membership.previewProductCode){ trainingCard.membership.confirm(status); //callback from modal 2 (view) } else { trainingCard.membership.continue(status); //callback from modal 1 (too short) } } else{ trainingCard.membership.continue(status); } } else { if(selectedProductType == 'membership_product'){ //buy only membership if(trainingCard.membership.previewProductCode){ trainingCard.membership.continue(status); //callback from modal 1 (too short) } else { trainingCard.product.confirm(status); //callback from modal 2 (view) } } else { trainingCard.product.confirm(status); } } return false; } catch (e){ console.log('Error in trainingCard.confirm: '+ e.message); } }; /*trainingCard.userConfirmation*/ this.userConfirmation = new function () { this.shown = false; this.handled = false; /*trainingCard.userConfirmation.confirm*/ this.confirm = function(status) { if(DEBUG) { console.log('trainingCard.userConfirmation.confirm'); } try { if (status) { //ok pressed //check required fields var err=''; if($('#start_date').val()){ //validate date var inp_date = $('#start_date').val(); if(DATEFMT == 'd.m.Y'){ var tmp = inp_date.split('.'); var day = tmp[0]; var month = tmp[1]; var year = tmp[2]; d = new Date(year, month, day, 23, 59, 59); } else { var tmp = inp_date.split('-'); var day = tmp[2]; var month = tmp[1]; var year = tmp[0]; d = new Date(year, month, day, 23, 59, 59); } if(d < Date.now() || isNaN(d)){ err += 'Virhe kentässä: Valitse aloituspvm'; } else { $('#id_date_saved').val($('#start_date').val()); } } //favorite location if($('#id_select_location').length) { if ($('#id_select_location').val()) { $('#id_frm_training_card #id_favorite_location').val($('#id_select_location').val()); } else { if(err != ''){ err += '
'; } err += 'Virhe kentässä: Valitse sijainti'; } } //confirm save card no if($('#save_card_no').length) { if($('#save_card_no').val()){ $('#id_frm_training_card #id_save_card_no').val($('#save_card_no').val()); } else{ if (err != '') { err += '
'; } err += 'Ole hyvä valitse jos sinulla on tallessa vanha jäsenkorttisi.'; } } if(err){ this.show(err); } else { //continue this.continue(); } } else { //cancel pressed //reset flag trainingCard.userConfirmation.shown = false; } } catch (e){ console.log('Error in trainingCard.userConfirmation.confirm: '+ e.message); } } /*trainingCard.userConfirmation.show*/ this.show = function (errorMsg) { if(DEBUG) { console.log('trainingCard.userConfirmation.show'); } try { //check if there is any user input required. var userInputRequired = false; var selectedProductType = trainingCard.product.selectedProductType(); if ($("#userConfirmation ." + selectedProductType).length && $("#userConfirmation ." + selectedProductType).html().trim() != '') { userInputRequired = true; } if ($("#userConfirmation .general").length && $("#userConfirmation .general").html().trim() != '') { userInputRequired = true; } if(!userInputRequired){ //exit function this.continue(); return; } //reset flag trainingCard.userConfirmation.shown = true; /* modal lightbox */ var html = { title: 'Valitse seuraavat tiedot', body: $("#userConfirmation").html(), buttons: {accept: 'Jatka', reject: 'Takaisin'} }; showConfirmation(trainingCard.confirm, html); //set start date max date if($('#start_date_max_days_ahead').val() && $('#start_date').length){ $('.start_date.datepicker') .datepicker('setEndDate', '+'+$('#start_date_max_days_ahead').val()+'d') .datepicker('update'); } if(errorMsg){ var errorElement = $('#dialog-confirmation #userConfirmationErr'); //cloned element $(errorElement).html(errorMsg); $(errorElement).show(); } } catch (e){ console.log('Error in trainingCard.userConfirmation.show: '+ e.message); } } /*trainingCard.userConfirmation.continue*/ this.continue = function (errorMsg) { if(DEBUG) { console.log('trainingCard.userConfirmation.continue'); } this.handled = true; var productCode = trainingCard.product.get(); var productType = $("div[data-product='" + productCode + "']").data('product-type'); if (site.isMobile() && (productType != 'contract_product' || $("#tab_membership").length == 0)) { trainingCard.product.view(productCode, true); } else { trainingCard.product.next(productCode); } return; } } }; function inArray(obj, a) { var i = a.length; while (i--) { if (a[i] === obj) { return true; } } return false; }