/* Copyright (C) 2020 OhmConnect, Inc. - All Rights Reserved */ // $('body').on('click', '.js-facebook-button', function () { signinWithFacebook() }); $(function() { if (window.location.href === 'https://www.ohmconnect.com/') { var zip_code = $("input[name='zip_code']").val(); var first_name = $("input[name='first_name']").val(); var email = $("input[name='email']").val(); var checkPhone = ($("input[name='phone']").length > 0); // Check if phone field exists var phone = ''; if (checkPhone) { phone = $("input[name='phone']").val(); } if (!!zip_code && !!first_name && !!email && (!checkPhone || checkPhone && !!phone)) { $("input[name='zip_code']").click(); } } // All available fields on WWW Signup with their error var FIELDS = {"zip_code": '', "first_name": '', "phone": '', "email":'', "password": ''}; function inputFieldExists(name) { return $("input[name='" + name + "']").length > 0; } for (var field of Object.keys(FIELDS)) { if (inputFieldExists(field)) { $("input[name='" + field + "']").css('caret-color', 'white'); } } function updateError(field, error, empty=false) { var color = '#fff'; FIELDS[field] = error; if (error !== '' || (empty && field !== 'phone')) { color = '#ff555d'; } $("input[name='" + field + "']").css('border-color', color); } // Shows/hides an error function toggleError(target, show, error='') { target.setCustomValidity(error); if (show) { target.reportValidity(); } } // Validate the SMS checkbox only when the form is submitted NOT when the phone field changes for better UX var smsDisclaimerCheckbox = $('input#sms-disclaimer'); var phoneField = $('input[name="phone"]'); function validateSMSCheckbox() { // Skip checking if not present. if (phoneField.length >= 1 && smsDisclaimerCheckbox.length >= 1) { if (phoneField.val().trim() !== '' && !smsDisclaimerCheckbox.is(':checked')) { toggleError(phoneField[0], true, 'Please select the checkbox below to agree to the terms.'); phoneField.css('border-color', '#ff555d') return true; } else { toggleError(phoneField[0], true, ''); phoneField.css('border-color', '#fff') } } return false; } // Clear the customValidity error whenever the checkbox changes else we won't be able to submit! // We cannot clear the the error on submit as the submit function does not // execute if there is an error. // The validateSMSCheckbox function in the hasError function takes care of checking if the checkbox is actually checked. if (smsDisclaimerCheckbox) { smsDisclaimerCheckbox.on('change', function() { toggleError(phoneField, true, ''); }); } // Pops first field's error bubble and return true if any field has an error, otherwise false function hasErrors() { for (var field of Object.keys(FIELDS)) { if (inputFieldExists(field) && FIELDS[field] !== '') { $("input[name='" + field + "']")[0].setCustomValidity(FIELDS[field]); $("input[name='" + field + "']")[0].reportValidity(); return true; } } return validateSMSCheckbox(); } // Calls relevant validation endpoint and handles error bubbles function validateField(field, value, oldValue, event) { toggleError(event.target, false); // Handle required error if (value === '') { updateError(field, '', true); if (oldValue !== value && field !== 'phone') { event.target.reportValidity(); } } if (value !== '' && oldValue !== value) { $.get('https://login.ohmconnect.com/api/v2/verify_signup/' + field + '/' + value, function (data) { if (data.status < 0) { toggleError(event.target, true, data.error || data.msg); updateError(field, data.error || data.msg); } else { updateError(field, ''); } } ); } } // Verifies the field value and updates the field error function verifyFieldValue(event) { var field = $(this).prop('name'); var value = $(this).val().trim(); validateField(field, value, this.oldValue, event); // Update oldValue to stop input validation loop on blur if (this.oldValue !== value) { this.oldValue = value; } } function addFieldValidationEvents() { Object.keys(FIELDS).forEach(function (field) { if (inputFieldExists(field)) { $("form > input[name='" + field + "']").on('blur', verifyFieldValue); // This is the requirement of error bubble to clear on each input $("form > input[name='" + field + "']").on('input', function(event){ event.target.setCustomValidity(''); }); } }); } addFieldValidationEvents(); // Adds events on input fields to validate and update errors // Add event to not submit when there is an error $("form").on("submit", function(e) { if (hasErrors()) { e.preventDefault(); } }); }); // var current_user_id; $(function(){ function getField(name) { return document.querySelector('input[name="' + name + '"]'); } function setFieldValue(key, value){ const fieldObject = getField(key); // Don't set if field doesn't exist if (!fieldObject) return; fieldObject.value = !!value ? value : ''; } // Get values from URL params const fieldQueryParams = { 'zip_code': queryParam('zip_code'), 'first_name': queryParam('first_name'), 'phone': queryParam('phone'), 'email': queryParam('email'), }; // Expand fields const zipCodeField = getField('zip_code'); if (!!zipCodeField && !!fieldQueryParams['zip_code']) zipCodeField.click(); // Insert values from URL params for (const key in fieldQueryParams) { setFieldValue(key, fieldQueryParams[key]); } }); function queryParam(url_key) { var query = window.location.search.substring(1); var vars = query.split("&"); for (var i = 0; i < vars.length; i++) { var pair = vars[i].split("="); if (pair[0] == url_key) { return pair[1]; } } return (false); } var trackingArgs = ['track_key', 'track_source', 'campaign', 'sub_campaign', 'channel', 'publisher', 'promo', 'article_name', 'link_location', 'query_string', 'referrer', 'is_spa', 'lsid', 'aid', 'pcode'] var PageViewTracker = { trackCalls: [], widget_visibility: new Set(), duplicate_widget_ids: new Set(), generate_uuid: function () { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8); return v.toString(16); }); }, get_url_param: queryParam, set_cookie: function(cookie_name, cookie_value, cookie_timeout) { if (cookie_timeout === undefined) { cookie_timeout = Infinity; } var options = {expires: cookie_timeout}; if (/ohmconnect.com$/i.test(document.domain)) { var domain = 'ohmconnect.com'; document.domain = domain; options.domain = domain; } else if (/originenergy.com.au$/i.test(document.domain)) { var domain = 'originenergy.com.au'; document.domain = domain; options.domain = domain; } Cookies.set(cookie_name, cookie_value, options); }, set_cookies: function() { if (!Cookies.get('ohm_track_key')) { PageViewTracker.set_cookie('ohm_track_key', this.generate_uuid()); PageViewTracker.set_cookie('ohm_referrer', document.referrer) } if (!Cookies.get('ohm_browser_id')) { PageViewTracker.set_cookie('ohm_browser_id', this.generate_uuid()); } if (!Cookies.get('ohm_user_id') || (Cookies.get('ohm_user_id') == 'None')) { user_id = 'None'; if (user_id != 'None') { PageViewTracker.set_cookie('ohm_user_id', user_id); } } var paramVal; $.each(trackingArgs, function (i, name) { paramVal = PageViewTracker.get_url_param(name); if (paramVal) { PageViewTracker.set_cookie('ohm_' + name, paramVal) } }); if (!Cookies.get('ohm_ip')) { PageViewTracker.log_user_action('geo_ip_location', function (geo_location) { if (geo_location.ip) { PageViewTracker.set_cookie('ohm_ip', geo_location.ip); } }) } if (window.location.search.indexOf('?impersonate=True') != -1) { let timeout_dttm = new Date(); timeout_dttm.setTime(timeout_dttm.getTime() + (3600 * 1000)); // 1 hour PageViewTracker.set_cookie('ohm_skip_tracking', 'skip', timeout_dttm.toUTCString()); // Don't track if impersonating a user } }, log_user_action: function (page_name, callback) { if (Cookies.get('ohm_skip_tracking') == 'skip') { return true; } if (typeof page_name == 'undefined' || page_name.length == 0) { var data = {"page_name": location.pathname, 'track_type': 'page_view', 'http_referrer': document.referrer, 'url': window.location.href, 'query_string': window.location.href.split("?").slice(1).join("?")}; } else { var data = {"page_name": page_name, 'track_type': 'user_log', 'http_referrer': document.referrer, 'url': window.location.href, 'query_string': window.location.href.split("?").slice(1).join("?")}; } $.each(trackingArgs, function (i, name) { var paramVal = Cookies.get('ohm_' + name); if (paramVal) { data[name] = paramVal; } }); status_code = $('#status-code'); if(status_code && status_code.length === 1){ status_code = status_code[0].innerText data["status_code"] = status_code } $.post("https://login.ohmconnect.com/track/pageview" + window.location.search, data, function (geo_location) { if (typeof (callback) == "function") { callback(geo_location) } }); }, track_click: function($element) { if ($element.attr('disabled') || $element.hasClass('disabled')) { return true; // Don't track if the user clicked on a disabled link / button / etc } var data = PageViewTracker._default_tracking_args($element); data.href = $element.attr('href'); if ((!data.href || !data.href.length)) { // If this is a button on a form, get the form action var form = $element.closest('form'); data.href = form.attr('action') } if ((!data.href || !data.href.length) && $element.attr('data-ohm-selector')) { // Some elements don't have a href value, but we want to identify what they trigger, so grab the id / selector data.href = $element.attr('data-ohm-selector'); } if ((!data.text || !data.text.length) && $element.attr('value')) { // Some elements don't have text, but report a value instead (e.g., submit inputs) data.text = $element.attr('value'); } if ($element.data('ohm-click-track')) { data.custom_track_info = $element.data('ohm-click-track'); } data.track_name = 'site_click'; if ($element.data('ohm-on-click')) { var on_click = eval($element.data('ohm-on-click')); if (typeof(on_click) == 'function') { on_click(); } } PageViewTracker.trackCalls.push(data); return true; }, register_click_tracking: function () { window.addEventListener('click', function(event) { $(event.target).closest("a, button, .clickable, [data-ohm-track=click], [data-ohm-track], input[type=submit]").each(function (i, el) { return PageViewTracker.track_click($(el)); }); return true; }, {'capture': true}); // Capture fires on the way *into* the event; jQuery's default (bubble) fires on the way out. For tracking capture will fire on what the user clicked on, rather than on any changes resulting therefrom }, track_widget_view: function ($element) { var data = PageViewTracker._default_tracking_args($element); if ($element.data('ohm-widget-track')) { data.custom_track_info = $element.data('ohm-widget-track'); } if ($element.data('ohm-on-view')) { var on_view = eval($element.data('ohm-on-view')); // https://stackoverflow.com/questions/197769/when-is-javascripts-eval-not-evil if (typeof(on_view) == 'function') { on_view(); } } data.track_name = 'widget_view'; PageViewTracker.trackCalls.push(data); return true; }, track_duplicate_id: function(id) { if(PageViewTracker.duplicate_widget_ids.has(id)) { return; } var data = PageViewTracker._default_tracking_args(null); data.widget = id; data.track_name = 'duplicate_widget_id'; PageViewTracker.trackCalls.push(data); PageViewTracker.duplicate_widget_ids.add(id); }, is_in_viewport: function ($element) { // adapted from https://medium.com/talk-like/detecting-if-an-element-is-in-the-viewport-jquery-a6a4405a3ea2 var elementTop = $element.offset().top; var elementMiddle = elementTop + $element.outerHeight() / 2; // We care about the middle of the element being exposed var viewportTop = $(window).scrollTop(); var viewportBottom = viewportTop + $(window).height(); return elementMiddle > elementTop && elementMiddle > viewportTop && elementMiddle < viewportBottom; }, update_widget_visibility: function() { $('.ohm-well, [data-ohm-track=view]').each(function () { var $this = $(this); var id = $(this).attr('id'); if (!id || PageViewTracker.widget_visibility.has(id)) { return } if ($('[id="' + id + '"]').length > 1) { // If there is more than one widget on this page with the same ID, we can't get reliable tracking info -- log it and move on PageViewTracker.track_duplicate_id(id); return } if (PageViewTracker.is_in_viewport($this)) { PageViewTracker.track_widget_view($this); PageViewTracker.widget_visibility.add(id); } }); }, register_widget_view_tracking: function () { // https://medium.com/talk-like/detecting-if-an-element-is-in-the-viewport-jquery-a6a4405a3ea // Firing on window.load rather than $().ready() causes update_widget_visibility() to execute after Vue code, preventing erroneous tracking of hidden elements window.addEventListener("load", function(event) { PageViewTracker.update_widget_visibility() }); $(window).on('resize scroll', function (event) { PageViewTracker.update_widget_visibility(); return true; }); }, track_hover: function ($element) { var data = PageViewTracker._default_tracking_args($element); if ($element.attr('disabled') || $element.hasClass('disabled')) { return true; // Don't track if the user hovered on a disabled link / button / etc } if ($element.data('ohm-hover-track')) { data.custom_track_info = $element.data('ohm-hover-track'); } if (!data.text && $element.data('original-title')) { data.text = $element.data('original-title'); } if (!data.text && $element.attr('title')) { data.text = $element.attr('title'); } data.track_name = 'site_hover'; PageViewTracker.trackCalls.push(data) return true; }, register_hover_tracking: function () { $().ready(function () { $("[data-ohm-track=hover], [data-toggle=tooltip]").hover(function () { return PageViewTracker.track_hover($(this)); }); }); }, tracking_batch_send: function () { if (Cookies.get('ohm_skip_tracking') == 'skip') { PageViewTracker.trackCalls = [] return true; } PageViewTracker.register_set_timeout_batch_send() if (PageViewTracker.trackCalls.length === 0) { return } var data = JSON.stringify(PageViewTracker.trackCalls) PageViewTracker.trackCalls = [] $.ajax({ type: "POST", url: "https://login.ohmconnect.com/track/batch", data: data, contentType: "application/json" }, function (geo_location) { if (typeof geo_location != 'undefined' && geo_location.ip) { PageViewTracker.set_cookie('ohm_ip', geo_location.ip); } }) }, register_set_timeout_batch_send: function () { setTimeout(PageViewTracker.tracking_batch_send, 10000); }, register_unload_batch_send: function() { $(window).on("beforeunload", function () { PageViewTracker.tracking_batch_send() }); }, _default_tracking_args: function($element) { // $element can be undefined or null var data = { page: window.location.pathname }; if ($element) { data.text = $element.text().trim(); data.widget = $element.closest('.ohm-well, [data-ohm-track=view]').attr('id'); // May be undefined } $.each(trackingArgs, function (i, name) { data[name] = Cookies.get('ohm_' + name); }); if (!data.track_key) { var uuid = PageViewTracker.generate_uuid(); PageViewTracker.set_cookie('ohm_track_key', uuid); data.track_key = uuid; } return data; }, call_functions: function () { this.set_cookies(); this.log_user_action(); this.register_click_tracking(); this.register_widget_view_tracking(); this.register_hover_tracking(); this.register_unload_batch_send(); this.register_set_timeout_batch_send(); }, }; $(function() { PageViewTracker.call_functions(); }); function is_mobile() { var check = false; (function (a) { if (/(android|bb\d+|meego).+mobile|avantgo|bada\/|blackberry|blazer|compal|elaine|fennec|hiptop|iemobile|ip(hone|od)|iris|kindle|lge |maemo|midp|mmp|mobile.+firefox|netfront|opera m(ob|in)i|palm( os)?|phone|p(ixi|re)\/|plucker|pocket|psp|series(4|6)0|symbian|treo|up\.(browser|link)|vodafone|wap|windows ce|xda|xiino/i.test(a) || /1207|6310|6590|3gso|4thp|50[1-6]i|770s|802s|a wa|abac|ac(er|oo|s\-)|ai(ko|rn)|al(av|ca|co)|amoi|an(ex|ny|yw)|aptu|ar(ch|go)|as(te|us)|attw|au(di|\-m|r |s )|avan|be(ck|ll|nq)|bi(lb|rd)|bl(ac|az)|br(e|v)w|bumb|bw\-(n|u)|c55\/|capi|ccwa|cdm\-|cell|chtm|cldc|cmd\-|co(mp|nd)|craw|da(it|ll|ng)|dbte|dc\-s|devi|dica|dmob|do(c|p)o|ds(12|\-d)|el(49|ai)|em(l2|ul)|er(ic|k0)|esl8|ez([4-7]0|os|wa|ze)|fetc|fly(\-|_)|g1 u|g560|gene|gf\-5|g\-mo|go(\.w|od)|gr(ad|un)|haie|hcit|hd\-(m|p|t)|hei\-|hi(pt|ta)|hp( i|ip)|hs\-c|ht(c(\-| |_|a|g|p|s|t)|tp)|hu(aw|tc)|i\-(20|go|ma)|i230|iac( |\-|\/)|ibro|idea|ig01|ikom|im1k|inno|ipaq|iris|ja(t|v)a|jbro|jemu|jigs|kddi|keji|kgt( |\/)|klon|kpt |kwc\-|kyo(c|k)|le(no|xi)|lg( g|\/(k|l|u)|50|54|\-[a-w])|libw|lynx|m1\-w|m3ga|m50\/|ma(te|ui|xo)|mc(01|21|ca)|m\-cr|me(rc|ri)|mi(o8|oa|ts)|mmef|mo(01|02|bi|de|do|t(\-| |o|v)|zz)|mt(50|p1|v )|mwbp|mywa|n10[0-2]|n20[2-3]|n30(0|2)|n50(0|2|5)|n7(0(0|1)|10)|ne((c|m)\-|on|tf|wf|wg|wt)|nok(6|i)|nzph|o2im|op(ti|wv)|oran|owg1|p800|pan(a|d|t)|pdxg|pg(13|\-([1-8]|c))|phil|pire|pl(ay|uc)|pn\-2|po(ck|rt|se)|prox|psio|pt\-g|qa\-a|qc(07|12|21|32|60|\-[2-7]|i\-)|qtek|r380|r600|raks|rim9|ro(ve|zo)|s55\/|sa(ge|ma|mm|ms|ny|va)|sc(01|h\-|oo|p\-)|sdk\/|se(c(\-|0|1)|47|mc|nd|ri)|sgh\-|shar|sie(\-|m)|sk\-0|sl(45|id)|sm(al|ar|b3|it|t5)|so(ft|ny)|sp(01|h\-|v\-|v )|sy(01|mb)|t2(18|50)|t6(00|10|18)|ta(gt|lk)|tcl\-|tdg\-|tel(i|m)|tim\-|t\-mo|to(pl|sh)|ts(70|m\-|m3|m5)|tx\-9|up(\.b|g1|si)|utst|v400|v750|veri|vi(rg|te)|vk(40|5[0-3]|\-v)|vm40|voda|vulc|vx(52|53|60|61|70|80|81|83|85|98)|w3c(\-| )|webc|whit|wi(g |nc|nw)|wmlb|wonu|x700|yas\-|your|zeto|zte\-/i.test(a.substr(0, 4))) check = true })(navigator.userAgent || navigator.vendor || window.opera); return check; }