var AjaxBase = location.protocol + "//" + location.hostname + (location.port ? ':' + location.port : ''), phone = Modernizr.inputtypes.date && (navigator.userAgent.match(/iPad/i) || navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/BlackBerry/i) || navigator.userAgent.match(/webOS/i)) ? true : false; //Pollyfills if (typeof Object.assign != 'function') { // Used on formvalidation.io framework, fixes an issue with ie11 Object.assign = function (target) { 'use strict'; if (target == null) { throw new TypeError('Cannot convert undefined or null to object'); } target = Object(target); for (var index = 1; index < arguments.length; index++) { var source = arguments[index]; if (source != null) { for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } } return target; }; } // JQuery extensions (function ($) { $.fn.attrBegins = function (s) { var matched = ""; this.each(function (index) { $.each(this.attributes, function (index, attr) { if (attr.name.indexOf(s) === 0) { matched = attr.name; } }); }); return matched; }; $.extend(true, $.trumbowyg, { langs: { // jshint camelcase:false en: { fontFamily: 'Font' } } }); // jshint camelcase:true var defaultOptions = { fontList: [ { name: 'Arial', family: 'Arial, Helvetica, sans-serif' }, { name: 'Courier New', family: '\'Courier New\', Courier, monospace' }, { name: 'Georgia', family: 'Georgia, serif' }, { name: 'Impact', family: 'Impact, Charcoal, sans-serif' }, { name: 'Lucida Console', family: '\'Lucida Console\', Monaco, monospace' }, { name: 'Lucida Sans', family: '\'Lucida Sans Uncide\', \'Lucida Grande\', sans-serif' }, { name: 'Palatino', family: '\'Palatino Linotype\', \'Book Antiqua\', Palatino, serif' }, { name: 'Tahoma', family: 'Tahoma, Geneva, sans-serif' }, { name: 'Times New Roman', family: '\'Times New Roman\', Times, serif' }, { name: 'Trebuchet', family: '\'Trebuchet MS\', Helvetica, sans-serif' }, { name: 'Verdana', family: 'Verdana, Geneva, sans-serif' } ] }; // Add dropdown with web safe fonts $.extend(true, $.trumbowyg, { plugins: { fontfamily: { init: function (trumbowyg) { trumbowyg.o.plugins.fontfamily = trumbowyg.o.plugins.fontfamily || defaultOptions; trumbowyg.addBtnDef('fontfamily', { dropdown: buildDropdown(trumbowyg), hasIcon: false, text: trumbowyg.lang.fontFamily }); } } } }); function buildDropdown(trumbowyg) { var dropdown = []; $.each(trumbowyg.o.plugins.fontfamily.fontList, function (index, font) { trumbowyg.addBtnDef('fontfamily_' + index, { title: '' + font.name + '', hasIcon: false, fn: function () { trumbowyg.execCmd('fontName', font.family, true); } }); dropdown.push('fontfamily_' + index); }); return dropdown; } //datetimepicker icons $.fn.datetimepicker.Constructor.Default = $.extend({}, $.fn.datetimepicker.Constructor.Default, { icons: { time: 'far fa-clock', date: 'far fa-calendar', up: 'far fa-arrow-up', down: 'far fa-arrow-down', previous: 'far fa-chevron-left', next: 'far fa-chevron-right', today: 'far fa-calendar-check-o', clear: 'far fa-trash', close: 'far fa-times' } }); })(jQuery); // General fucntions function showLoader() { var title = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : "Loading, please wait."; var text = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : "We are processing your request"; var button = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false; $('#title-loading').text(title); $('#text-loading').text(text); if (button === true) { $('#button-loading').show(); $('#scene-loading').hide(); } else { $('#button-loading').hide(); $('#scene-loading').show(); } $('html').css({ "overflow-y": "hidden" }); $('#loading-wrapper').fadeIn(); } function hideLoader() { $('html').css({ "overflow-y": "auto" }); $('#loading-wrapper').fadeOut(); $('#scene-loading').show(); $('#button-loading').hide(); } // Global functions + listeners $(function() { $('body').on('click', '[data-sh-container]', function(e) { e.preventDefault(); ShowHideDetails(this); }); }); function ShowHideDetails(element) { var ele = $(element), container = $(ele.attr('data-sh-container')), showText = typeof (ele.attr('data-sh-show')) === 'undefined' ? 'Show' : ele.attr('data-sh-show'), hideText = typeof (ele.attr('data-sh-hide')) === 'undefined' ? 'Hide' : ele.attr('data-sh-hide'); if (ele.hasClass('sh-active')) { container.slideUp(); ele.removeClass('sh-active').html(showText); } else { container.slideDown(); ele.addClass('sh-active').html(hideText); } } function hasAttributeStartingWith(node, attr) { var ele = typeof (node.attributes) === 'undefined' ? node[0] : node; return Array.from(ele.attributes).filter(function (attributeNode) { return attributeNode.nodeName.indexOf(attr) === 0; }).length > 0; } // datepickers setting function //TODO transform that for datetimepicker var format = phone ? "YYYY-MM-DD" : "DD/MM/YYYY"; function setDatepicker(dateInfo) { var ele = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; var date = dateInfo, min = phone ? date.minPhone : date.min, max = phone ? date.maxPhone : date.max, errorMessage = "Please select a date between " + date.min + " and " + date.max; if (ele === null) { ele = $("[name='" + date.name + "']"); } else { ele = ele; } if (ele.length) { ele.attr("data-fv-date", "true").attr("data-fv-date___format", format).attr("data-fv-date___message", errorMessage); if (phone) { // set calendars for mobile phones with HTML5 ele.attr("type", "date") .attr("min", min).attr("data-fv-date___min", min) .attr("max", max).attr("data-fv-date___max", max); ele.change(function () { validator.revalidateField(ele.attr('name')); }); } else { // set datpickers for pc with jqeury-ui ele.attr("type", "text") .prop("readonly", true) .attr("data-fv-date___min", min) .attr("data-fv-date___max", max); ele.removeClass('hasDatepicker').datepicker({ dateFormat: 'dd/mm/yy', changeMonth: true, constrainInput: true, numberOfMonths: 2, minDate: min, maxDate: max, onClose: function () { if (typeof (date.revalidate) === 'undefined') { fv[date.fv].revalidateField(ele.attr('name')); } } }); } } } var timeFormat = "DD/MM/YYYY h:m"; function setDateTimepicker(dateInfo) { var ele = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; var date = dateInfo, min = date.min, max = date.max, errorMessage = "Please select a date between " + date.min + " and " + date.max, datePickerMinDate = date.minPhone.split(" ")[0], datePickerDefaultDate = typeof date.defaultDate === 'string' ? date.defaultDate.split(" ")[0] : false; if (ele === null) { ele = $("[name='" + date.name + "']"); } else { ele = ele; } if (ele.length) { ele.attr("data-fv-date", "true").attr("data-fv-date___format", timeFormat).attr("data-fv-date___message", errorMessage); ele.attr("type", "text") //.prop("readonly", true) .attr("data-fv-date___min", min) .attr("data-fv-date___max", max); ele.datetimepicker({ minDate: datePickerMinDate, maxDate: moment().add(1, "minutes").valueOf(), useCurrent: false, defaultDate: datePickerDefaultDate, format: 'DD/MM/YYYY HH:mm' }); ele.on("change.datetimepicker", function (e) { if (typeof (date.revalidate) === 'undefined') { fv[date.fv].revalidateField(ele.attr('name')); } }); } } function postCodeLookupSearch(self) { return function (ele, modelName) { var obj = modelName.split(/[.\[\]]/).filter(function (v) { return v !== '' }), o = null, searchList = null; if (obj.length === 3) { o = self[obj[0]]; o = ko.isObservable(o) ? o() : o; o = ko.isObservable(o[obj[1]]) ? o[obj[1]]() : o[obj[1]]; o = ko.isObservable(o[obj[2]]) ? o[obj[2]]() : o[obj[2]]; searchList = $("#" + obj[0] + obj[1] + obj[2] + "SearchList"); } else if (obj.length === 2) { o = self[obj[0]]; o = ko.isObservable(o) ? o() : o; o = ko.isObservable(o[obj[1]]) ? o[obj[1]]() : o[obj[1]]; searchList = $("#" + obj[0] + obj[1] + "SearchList"); } else { o = self[modelName]; o = ko.isObservable(o) ? o() : o; searchList = $("#" + modelName + "SearchList"); } o.Addresses.removeAll(); ele.closest(".form-group").find("svg").removeClass("fa-search").addClass("fa-circle-notch fa-spin").prop("disabled", true); $.post('/Customers/LookupPostcode', { Postcode: ele.val() }).done(function (response) { searchList.show(); $.each(response.results, function (index, result) { o.Addresses.push({ value: result.id, text: result.description }); }); }).always(function () { ele.closest('.form-group').find("svg").removeClass("fa-circle-notch fa-spin").addClass("fa-search").prop("disabled", false); }); }; } function selectAddress(self) { return function (modelName, data, event) { var id, obj = modelName.split(/[.\[\]]/).filter(function (v) { return v !== '' }), countrySelect = $(event.target).closest('.addressWrapper').find("[name$='.Country']"), o = null, searchList = null; if (obj.length === 3) { o = self[obj[0]]; o = ko.isObservable(o) ? o() : o; o = ko.isObservable(o[obj[1]]) ? o[obj[1]]() : o[obj[1]]; o = ko.isObservable(o[obj[2]]) ? o[obj[2]]() : o[obj[2]]; searchList = $("#" + obj[0] + obj[1] + obj[2] + "SearchList"); } else if (obj.length === 2) { o = self[obj[0]]; o = ko.isObservable(o) ? o() : o; o = ko.isObservable(o[obj[1]]) ? o[obj[1]]() : o[obj[1]]; searchList = $("#" + obj[0] + obj[1] + "SearchList"); } else { o = self[modelName]; o = ko.isObservable(o) ? o() : o; searchList = $("#" + modelName + "SearchList"); } id = o.address(); if (event.originalEvent) { $.post('/Customers/LookupAddress', { id: id }).done(function (response) { var address = response; var houseNameOrNumberParts = [address.organization, address.subBuilding, address.buildingName, address.buildingNumber]; var houseNameOrNumber = houseNameOrNumberParts .filter(function (x) { return x }) .join(", "); o.HouseNameOrNumber(houseNameOrNumber); o.Street(address.street); o.City(address.locality); o.County(address.region); var countryValue; switch (address.countryCode.toUpperCase()) { case 'GB': switch (address.postcode.slice(0, 2)) { case 'GY': countryValue = countrySelect.find('option:contains("Channel Islands")').val(); if (!countryValue) { countryValue = countrySelect.find('option:contains("Guernsey")').val(); } break; case 'IM': countryValue = countrySelect.find('option:contains("Isle of Man")').val(); break; case 'JE': countryValue = countrySelect.find('option:contains("Jersey")').val(); break; case 'BT': countryValue = countrySelect.find('option:contains("Northern Ireland")').val(); break; default: countryValue = countrySelect.find('option:contains("England and Wales")').val(); if (!countryValue) { countryValue = countrySelect.find('option:contains("United Kingdom")').val(); } break; } break; case 'IE': countryValue = countrySelect.find('option:contains("Republic of Ireland")').val(); break; default: break; } if (countryValue) { countrySelect.val(countryValue); o.Country(countryValue); } if (address.hasOwnProperty("geometry") && o.hasOwnProperty("Longitude") && o.hasOwnProperty("Latitude")) { o.Latitude(address.geometry.coordinate.latitude); o.Longitude(address.geometry.coordinate.longitude); } }).always(function () { searchList.hide(); o.Addresses.removeAll(); }); } } }; Number.prototype.formatMoney = function (c, d, t) { var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "." : d, t = t == undefined ? "," : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0; return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ""); }; String.prototype.unformatMoney = function (s) { // s is the simbol for the thousand separator var n = this, s = s == undefined ? "," : s, r = new RegExp(s, 'g'), c = n.replace(r, ""); return parseFloat(c); }; String.prototype.toCapitalise = function () { return this.charAt(0).toUpperCase() + this.slice(1).toLowerCase(); }; String.prototype.isNumber = function () { return /^\d+$/.test(this); } //Polifyll for IE11 if (!Object.getOwnPropertyDescriptor(NodeList.prototype, 'forEach')) { Object.defineProperty(NodeList.prototype, 'forEach', Object.getOwnPropertyDescriptor(Array.prototype, 'forEach')); } $(function () { function SetWinTimeout(time) { var t = typeof(time) !== 'undefined' ? time : 1140000; // after 19 min i.e. 19 * 60 * 1000 wintimeout = window.setTimeout(function () { $("#TimeOut").fadeIn(); var cont = 59, seg = 6000; wininterval = window.setInterval(function () { if (cont != 0) { if (typeof _draw !== 'undefined') _draw(0.000166 * seg); seg--; if (seg % 100 == 0) { cont--; $("#Timer").html(cont); } } else { window.clearInterval(wininterval); window.location.href = AjaxBase + "/Account/Logout"; } }, 10); }, t); }; /*=============================TIME OUT=========================*/ if (typeof(timeout) === 'undefined') { var wintimeout; var wininterval; SetWinTimeout(sessionTime); $('body').on('click', '#TimeOut', function () { $.ajax({ url: AjaxBase + '/account/refreshsession', // Redirects to action method for every 30 minutes. dataType: "json", type: "GET", fail: function () { toastr.error(" An error occurred has ocurred refreshing your session."); window.location.href = AjaxBase + "/Account/Logout"; }, error: function () { toastr.error(" An error occurred has ocurred refreshing your session."); window.location.href = AjaxBase + "/Account/Logout"; }, success: function (data) { window.clearInterval(wininterval); $("#TimeOut").remove(); window.clearTimeout(wintimeout); //when user clicks remove timeout and reset it SetWinTimeout(); } }); }); } if ($('#timeoutCanvas').length > 0) { // Prepare canvas var canvas = $('#timeoutCanvas')[0]; var startColor = $('#timeoutCanvas').attr('startColor'); var endColor = $('#timeoutCanvas').attr('endColor'); if (typeof startColor == 'undefined') startColor = "#17a2b8"; if (typeof endColor == 'undefined') endColor = startColor; var options = { value: 1, size: 100, startAngle: -Math.PI, startColor: startColor, endColor: endColor, animation: false }; $.easing.circleProgressEase = function (x, t, b, c, d) { if ((t /= d / 2) < 1) return c / 2 * t * t * t + b; return c / 2 * ((t -= 2) * t * t + 2) + b; }; var s = options.size, // square size v = options.value, // current value: from 0.0 to 1.0 r = s / 2, // radius t = s / 14; // thickness canvas.width = s; canvas.height = s; var ctx = canvas.getContext('2d'); var lg = ctx.createLinearGradient(0, 0, s, 0); lg.addColorStop(0, options.startColor); lg.addColorStop(1, options.endColor); ctx.fillStyle = "rgba(0, 0, 0, .1)"; // Draw circle if (options.animation) _drawAnimated(v); else _draw(v); $('.number').click(function () { if (options.animation) _drawAnimated(v); else _draw(v); }); function _draw(p) { // Clear frame ctx.clearRect(0, 0, s, s); // Draw background circle ctx.beginPath(); ctx.arc(r, r, r, -Math.PI, Math.PI); ctx.arc(r, r, r - t, Math.PI, -Math.PI, true); ctx.closePath(); ctx.fill(); // gray fill // Draw progress arc ctx.beginPath(); ctx.arc(r, r, r, -Math.PI, -Math.PI + Math.PI * 2 * p); ctx.arc(r, r, r - t, -Math.PI + Math.PI * 2 * p, -Math.PI, true); ctx.closePath(); ctx.save(); ctx.clip(); ctx.fillStyle = lg; ctx.fillRect(0, 0, s, s); // gradient fill ctx.restore(); } function _drawAnimated(v) { $(canvas).stop(true, true).css({ value: 0 }).animate({ value: v }, $.extend({}, options.animation, { step: function (p) { _draw(p); $(canvas).trigger('circle-animation-progress', [p / v, p]); }, complete: function () { $(canvas).trigger('circle-animation-end'); } })); } } });