function jqReady(){ //custom function to check validity of email address /*function isValidEmailAddress(emailAddress) { var pattern = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i); return pattern.test(emailAddress); };*/ $('.email-form-wrapper form').submit(function(event){ /* stop form from submitting normally */ event.preventDefault(); if(isValidEmailAddress($('#q51685_q2').val())){ //hide form and show loader $('.form-fields-wrapper').hide(); $('#form-response').html('
Emailing report, please wait...
'); /* get some values from elements on the page: */ var $form = $(this), email = $form.find('#q51685_q2').val(), url = $(this).attr("action"); /* Send the data using post */ var posting = $.post(url, $form.serialize()); /* Put the results in a div */ posting.done(function( data ) { $('.form-fields-wrapper').remove(); var content = $(data).find('#thank-you-content'); $('#form-response').empty().append(content); ga('send', 'event', 'IP', 'Email report'); }); }else{ alert('Email address must be valid before emailing the report.'); } }); //function for removing object from array jQuery.removeFromArray = function(value, arr) { return jQuery.grep(arr, function(elem, index) { return elem !== value; }); }; //global vars and objects for quick access var $_ipWrapper = $('#ip-knowledge-tool-wrapper'); var $_ipSteps = $('#ip-steps'); var $_ipContent = $('#ip-dynamic-content-area'); var $_ipTemp = $('#ip-temp-content'); var $_ipControls = $_ipWrapper.find('.nav-controls'); var $_ipSelections; var _ipCurrentStep = -1; var _ipChosenTopics = new Array(); var _ipChosenQuestions = new Array(); //first check if we have loaded the page from an email link var _ipHashValue = window.location.hash.substring(1); if(_ipHashValue.indexOf('showStep=results') > -1){ //extract values from hash var hashTopics = _ipHashValue.substring(_ipHashValue.indexOf('t=') + 2,_ipHashValue.indexOf('&q')); var hashQuestions = _ipHashValue.substring(_ipHashValue.indexOf('q=') + 2,_ipHashValue.indexOf('&end')); //console.log(hashTopics); //console.log(hashQuestions); //show last step, set step number in cookie $.cookie('ipStep', 3, { expires: 1, path: '/' }); $.cookie('ipTopics', hashTopics, { expires: 1, path: '/' }); $.cookie('ipQuestions', hashQuestions, { expires: 1, path: '/' }); //reset hash window.location.hash = ''; } //if there are cookies, update our arrays to these values if($.cookie('ipTopics')){ _ipChosenTopics = $.cookie('ipTopics').split(','); } //if there are cookies, update our arrays to these values if($.cookie('ipQuestions')){ _ipChosenQuestions = $.cookie('ipQuestions').split(','); } $(function(){ //add some HTML containers for content that we are injecting //$('#left_nav').before(''); $_ipSelections = $('#ip-selections-wrapper'); //load the first step of the tool (or specific step if cookie exists) if($.cookie('ipStep')){ ipLoadStep($.cookie('ipStep')); }else{ ipLoadStep(0); } iniIpSelections(); }); //apply click functions to the steps $_ipSteps.find('a').each(function(i){ $(this).click(function(){ ipLoadStep(i); return false }); }); //apply click functions to the nav controls $_ipControls.find('.next').click(function(){ ipLoadStep(parseInt(_ipCurrentStep)+1); }); $_ipControls.find('.prev').click(function(){ ipLoadStep(parseInt(_ipCurrentStep)-1); }); $_ipControls.find('.print').click(function(){ //track this event in GA ga('send', 'event', 'IP', 'Print report'); window.print(); return false; }); $_ipControls.find('.email').click(function(){ if($('#q51685_q3').length > 0){ //set URL value $('#q51685_q3').val('./?a=66534#showStep=results&t=' + _ipChosenTopics.toString() + '&q=' +_ipChosenQuestions.toString() + '&end'); } $('.email-form-wrapper').slideDown(); $('html, body').animate({scrollTop: $('.email-form-wrapper').offset().top - 100}, 500); if($('#q51685_q2').val() == ''){ $('#q51685_q2').val($.cookie('ipEmail')); } $('#q51685_q2').focus(); return false; }); //load side summary selection box function iniIpSelections(){ $_ipSelections.load('./?a=51796 #ip-summary-topics', function(){ //add click function for removing selections $('#remove-selections-button').click(function(){ ipRemoveSelections(); return false; }); //add click toggle function to show and hide children $_ipSelections.find('.show-questions-toggle').click(function(){ var thisToggle = this; $(this).closest('li').find('ul').slideToggle('fast', function(){ if($(thisToggle).text() == '+'){ $(thisToggle).html('-'); }else{ $(thisToggle).html('+'); } }); return false; }); //update the side col selection summary list ipUpdateSelections(); }); } //function for loading a particular step function ipLoadStep(step){ //validate if we are leaving first step and email is incorrect if(_ipCurrentStep == 0 && !ipCheckEmail()){ //error in email, don't proceed ipShowLoader('hide'); ipUpdateSelections(); //validate if we are going to step 3 or 4 and no topics have been selected }else if((step == 2 || step == 3) && !ipValidateTopics()){ //no topics selected, don't proceed ipShowLoader('hide'); ipUpdateSelections(); //validate if we are going to step 4 and no questions have been selected }else if((step == 3) && !ipValidateQuestions()){ //no questions selected, don't proceed ipShowLoader('hide'); ipUpdateSelections(); }else{ //all good, no errors, load the step //hide the email form $('.email-form-wrapper').hide(); ipShowLoader(); //hide all nav controls $_ipControls.find('input').hide(); _ipCurrentStep = step; //highlight current step $_ipSteps.find('li').removeClass('active'); $_ipSteps.find('li:eq('+ _ipCurrentStep +')').addClass('active'); $_ipTemp.load($_ipSteps.find('a:eq(' + step + ')').attr('data-url'), function(){ ipShowLoader('hide'); //effects for removing old content and showing new content $_ipContent.slideUp(function(){ $_ipContent.html(''); $_ipTemp.children().appendTo($_ipContent); $_ipContent.slideDown(); ipAfterStepLoad(); }); //set step number in cookie $.cookie('ipStep', step, { expires: 1, path: '/' }); //record this screen load as a page view in GA ga('send', 'pageview', '%globals_asset_url%?screen=' + step); ipUpdateSelections(); }); } } //function for validating that at least 1 topic has been selected function ipValidateTopics(){ if(_ipChosenTopics.length < 1){ alert('At least 1 topic must be selected before you can proceed'); return false; }else{ return true; } } //function for validating that at least 1 topic has been selected function ipValidateQuestions(){ if(_ipChosenQuestions.length < 1){ alert('At least 1 question must be selected before you can proceed'); return false; }else{ return true; } } //function that gets called after a step has been loaded function ipAfterStepLoad(){ //check nav control buttons ipCheckControls(); //run certain functions depending on which step we are on switch(_ipCurrentStep.toString()){ case '0': //get started //populate email field with value from cookie $('#ip-email').val($.cookie('ipEmail')); break; case '1': //choose topics ipIniChooseTopics(); break; case '2': //select questions ipIniSelectQuestions(); break; case '3': //view results ipIniViewResults(); break; default: } } //function for when choose topics step is loaded function ipIniChooseTopics(){ //first read the topics cookie and check the already selected topics $(_ipChosenTopics).each(function(){ $('#topic-' + this).prop('checked',true); }); //add change function for the checkboxes when they are ticked $_ipContent.find('.topic-checkbox input').change(function(){ if($(this).is(':checked')){ //check if not already in array if($.inArray($(this).val(), _ipChosenTopics) == -1){ _ipChosenTopics.push($(this).val()); //track this event in GA ga('send', 'event', 'IP', 'Select topic', $(this).parent().find('label').text()); } //add to side summary selection $('#side-topic-' + $(this).val()).parent().show(); }else{ //remove this from our array _ipChosenTopics = jQuery.removeFromArray($(this).val(), _ipChosenTopics); //remove from side summary selection $('#side-topic-' + $(this).val()).prop('checked',false).parent().hide(); } //update cookie for chosen topics $.cookie('ipTopics', _ipChosenTopics.toString(), { expires: 1, path: '/' }); ipUpdateSelections(); }); } //function for when select questions step is loaded function ipIniSelectQuestions(){ //only activate the sections that are in our topic array $(_ipChosenTopics).each(function(){ $('#question-wrapper-' + this).show().addClass('active'); }); //first read the questions cookie and check the already selected questions $(_ipChosenQuestions).each(function(){ $('#sub-topic-' + this).prop('checked',true); }); //add change function for the checkboxes when they are ticked $_ipContent.find('.sub-topic-checkbox input').change(function(){ if($(this).is(':checked')){ //check if not already in array if($.inArray($(this).val(), _ipChosenQuestions) == -1){ _ipChosenQuestions.push($(this).val()); //track this event in GA ga('send', 'event', 'IP', 'Select question', $(this).parent().find('label').text()); } //add to side summary selection $('#side-sub-topic-' + $(this).val()).parent().show().closest('.accordion-wrapper').find('.show-questions-toggle').show(); }else{ //remove this from our array _ipChosenQuestions = jQuery.removeFromArray($(this).val(), _ipChosenQuestions); //remove from side summary selection $('#side-sub-topic-' + $(this).val()).prop('checked',false).parent().hide(); //if all from this topic removed, then hide the questions toggle link if($('#side-sub-topic-' + $(this).val()).closest('ul').find('li:visible').length < 1){ $('#side-sub-topic-' + $(this).val()).closest('.accordion-wrapper').find('.show-questions-toggle').hide(); } } //update cookie for chosen topics $.cookie('ipQuestions', _ipChosenQuestions.toString(), { expires: 1, path: '/' }); ipUpdateSelections(); }); //for all the sections that are not active, find all checked questions and remove them from our array $_ipContent.find('.accordion-wrapper:not(.active)').find('input:checked').each(function(){ //remove this from our array _ipChosenQuestions = jQuery.removeFromArray($(this).val(), _ipChosenQuestions); }); ipIniAccordions(); ipIniToggleTips(); } //function for the toggle tips button functionality function ipIniToggleTips(){ //add click function to button link $('#ip-toggle-tips').click(function(){ $(this).toggleClass('off'); //update cookie and text of button if($(this).hasClass('off')){ $(this).text('Toggle tips on'); $_ipWrapper.find('.sub-topic-tip.active').hide(); $.cookie('ipTips', 'off', { expires: 1, path: '/' }); }else{ $(this).text('Toggle tips off'); $_ipWrapper.find('.sub-topic-tip.active').show(); $.cookie('ipTips', 'on', { expires: 1, path: '/' }); } return false; }); //check if cookie exists and is off, then trigger the button if($.cookie('ipTips') == 'off'){ $('#ip-toggle-tips').toggleClass('off').text('Toggle tips on'); $_ipWrapper.find('.sub-topic-tip.active').hide(); } } //function for when view results step is loaded function ipIniViewResults(){ //only show the topic sections that have been selected $(_ipChosenTopics).each(function(){ $('#id-' + this).show().addClass('active'); }); //only show the question sections that have been selected $(_ipChosenQuestions).each(function(){ $('#id-' + this).show().addClass('active'); }); //show message for topics that have no questions selected $_ipContent.find('.accordion-wrapper.active').each(function(){ if($(this).find('.sub-topic-result-content.active').length < 1){ $(this).find('.sub-topic-results').prepend('No questions selected.
'); } }); } //function for updating the selection summary list (in left col) based on what is in the cookies function ipUpdateSelections(){ //hide all to start with $_ipSelections.find('li input').parent().hide(); $_ipSelections.find('.show-questions-toggle').hide(); //based on our selection array, show the ones that are selected if(_ipChosenTopics.length > 0){ $('#no-selections-message').hide(); $('#remove-selections-button').show(); $(_ipChosenTopics).each(function(){ $('#side-topic-' + this).parent().show(); }); $(_ipChosenQuestions).each(function(){ $('#side-sub-topic-' + this).parent().show().closest('.accordion-wrapper').find('.show-questions-toggle').show(); }); }else{ $('#no-selections-message').show(); $('#remove-selections-button').hide(); } } //function for when remove selection button is clicked, removes all checked topics function ipRemoveSelections(){ //find all checked selections and remove them from arrays if($_ipSelections.find('.selections-list > li > input:checked').length > 0 || $_ipSelections.find('.selections-list > li > ul > li > input:checked').length > 0){ //topics $_ipSelections.find('.selections-list > li > input:checked').each(function(){ $(this).prop('checked',false).parent().hide().find('ul li input').prop('checked',true).parent().hide(); //remove this from our array _ipChosenTopics = jQuery.removeFromArray($(this).val(), _ipChosenTopics); //update cookie for chosen topics $.cookie('ipTopics', _ipChosenTopics.toString(), { expires: 1, path: '/' }); }); //sub topics (questions) $_ipSelections.find('.selections-list > li > ul > li > input:checked').each(function(){ $(this).prop('checked',false).parent().hide(); //remove this from our array _ipChosenQuestions = jQuery.removeFromArray($(this).val(), _ipChosenQuestions); //update cookie for chosen topics $.cookie('ipQuestions', _ipChosenQuestions.toString(), { expires: 1, path: '/' }); }); //refresh main interface $_ipSteps.find('li.active a').trigger('click'); } } //function for enabling accordions within the tool function ipIniAccordions(){ $_ipContent.find('.ui-accordion-header').click(function(){ //only do this if this is closed if(!$(this).hasClass('ui-accordion-header-active')){ //close all other accordions $_ipContent.find('.ui-accordion-header-active').toggleClass('ui-accordion-header-active ui-corner-top ui-corner-all').closest('.accordion-wrapper').children('.ui-accordion-content, p').each(function(){ //check if the users window is below the start of this content (only for the div) if($(this).is('div') && $(this).is(':visible')){ $(this).slideToggle(function(){ var thisElement = $_ipContent.find('.ui-accordion-content:visible'); if($(window).scrollTop() > $(thisElement).offset().top){ $('html, body').animate({scrollTop: $(thisElement).offset().top - 100}, 300); } }); }else{ $(this).slideToggle(); } }); //open this one $(this).toggleClass('ui-accordion-header-active ui-corner-top ui-corner-all').closest('.accordion-wrapper').children('.ui-accordion-content, p').slideToggle(); //add last viewed accordion to cookie $.cookie('ipLastQuestion', '#'+$(this).parent().attr('id'), { expires: 1, path: '/' }); } }); //remove last "next topic" down and first "prev topic" up button $_ipContent.find('.accordion-wrapper.active:last .next-topic-button-wrapper').remove(); $_ipContent.find('.accordion-wrapper.active:first .prev-topic-button-wrapper').remove(); //add click functions to these buttons $_ipContent.find('.next-topic-button-wrapper a').click(function(){ $(this).closest('.accordion-wrapper').nextAll('.accordion-wrapper:visible:first').find('.ui-accordion-header').trigger('click'); }); $_ipContent.find('.prev-topic-button-wrapper a').click(function(){ $(this).closest('.accordion-wrapper').prevAll('.accordion-wrapper:visible:first').find('.ui-accordion-header').trigger('click'); }); //open the last viewed accordion for questions, if none, open first one if($.cookie('ipLastQuestion') && $_ipContent.find($.cookie('ipLastQuestion')).hasClass('active')){ $_ipContent.find($.cookie('ipLastQuestion')).find('.ui-accordion-header').trigger('click'); }else{ $_ipContent.find('.accordion-wrapper.active:first .ui-accordion-header').trigger('click'); } } //function for showing and hiding the relative nav control buttons function ipCheckControls(){ $_ipControls.find('.print, .email').hide(); if(_ipCurrentStep == 3){ //we are on the last step $_ipControls.find('.next').hide(); $_ipControls.find('.prev, .print, .email').show(); }else if(_ipCurrentStep == 0){ //we are on the first step $_ipControls.find('.next').show(); $_ipControls.find('.prev').hide(); }else{ //middle step $_ipControls.find('.next').show(); $_ipControls.find('.prev').show(); } } //function for hiding and showing the loaders function ipShowLoader(showHide){ if(showHide == 'hide'){ $_ipWrapper.find('.content-loader').hide(); $_ipSelections.find('.loader-wrapper').hide(); $('#remove-selections-button').show(); }else{ $_ipSelections.find('.loader-wrapper').show(); $_ipWrapper.find('.content-loader').show(); $('#remove-selections-button').hide(); } } //custom function to check validity of email address function isValidEmailAddress(emailAddress) { var pattern = new RegExp(/^[+a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/i); return pattern.test(emailAddress); }; //check the email if we are leaving the first step function ipCheckEmail(){ var emailValue = $('#ip-email').val(); //if email is filled in, validate its format if(emailValue != '' && !isValidEmailAddress(emailValue)){ alert('Email address entered is incorrect format.'); return false; }else{ //set email cookie (even if empty) $.cookie('ipEmail', $('#ip-email').val(), { expires: 1, path: '/' }); return true; } } }