var ContensisFormsLastElementFocus; var $progressAlert; var ContensisFormsCaptchaVisible = false; var ContensisFormsCurrentFormID = null; var ContensisFormsUploadControlCount = 0; var ContensisFormsUploadIncorrectFileType = 0; var uploader; var correctCaptcha = function (response) { $(".captchaStatus").html(''); }; var hasOwn = Object.prototype.hasOwnProperty; (function ($) { $.contensisForm = { addRules: function (rules) { $.contensisForm.rules = $.contensisForm.rules || []; $.contensisForm.rules.push(rules); }, addGroups: function (groups) { $.contensisForm.groups = $.contensisForm.groups || []; $.contensisForm.groups.push(groups); }, addDefaults: function (defaults) { $.contensisForm.defaults = $.contensisForm.defaults || []; $.contensisForm.defaults.push(defaults); }, addMessages: function (messages) { $.contensisForm.messages = $.contensisForm.messages || []; $.contensisForm.messages.push(messages); }, init: function () { // default items var rules = $.contensisForm.rules; var groups = $.contensisForm.groups; var defaults = $.contensisForm.defaults; var messages = $.contensisForm.messages; var config = { onsubmit: false, focusInvalid: true, errorClass: 'sys_cms-form-error', showErrors: $.contensisForm.showErrors, errorPlacement: $.contensisForm.errorPlacement }; if (rules) { config.rules = $.contensisForm.formatRules(rules); } if (groups) { config.groups = $.contensisForm.formatRules(groups); } $('form').validate(config); if (defaults && registerClearDefaultItem) { for (var i = 0, ilen = defaults.length; i < ilen; i++) { for (var id in defaults[i]) { if (hasOwn.call(defaults[i], id)) { registerClearDefaultItem(id, defaults[i][id]); } } } } if (messages && $.validator) { $.validator.messages = $.validator.messages || {}; for (var i = 0, ilen = messages.length; i < ilen; i++) { for (var id in messages[i]) { if (hasOwn.call(messages[i], id)) { $.validator.messages[id] = messages[i][id]; } } } } }, showErrors: function (errorMap, errorList) { $.each(errorList, function (name, value) { var element = $(value.element); var msgLabel = ''; if ($(element).is(':checkbox') || $(element).is(':radio')) { msgLabel = $(element).parent().parent().prev().text(); } else if ($(element).hasClass('tiny') || $(element).hasClass('address')) { msgLabel = $(element).parent().children(0).children(0).text(); } else if ($(element).hasClass('timeval')) { msgLabel = $(element).parent().parent().prev().text(); } else { msgLabel = $(element).parent().prev().text(); } this.message = msgLabel.replace('*', '') + " " + this.message; this.message = this.message.replace('This field', ''); }); this.defaultShowErrors(); }, errorPlacement: function (error, element) { if (element.rules().mincheckbox > 0) { var $p = $(element).parent().parent(); if ($p.siblings().hasClass('sys_cms-form-error') == false) { if (($p).children('label').length == 0) { error.insertBefore(($p).children(':first')); } } } else if ($(element).hasClass('timeval')) { var $p = $(element).parent().parent(); $p.children('label').remove(); if (($p).children('label').length == 0) { error.insertBefore(($p).children(':first')); } } else if ($(element).is('input[type=radio]')) { var $p = $(element).parent().parent(); if (($p).children('label').length == 0) { error.insertBefore(($p).children(':first')); } } else { error.insertBefore(element); } }, formatRules: function (rules) { var result = {}; for (var i = 0, ilen = rules.length; i < ilen; i++) { for (var id in rules[i]) { if (hasOwn.call(rules[i], id)) { var el = $('#' + id); var name = null; if (el.length > 0) { name = el.attr('name'); } else { el = $('[name$=\"$' + id + '\"]'); if (el.length > 0 && $(el[0]).is(':radio')) { name = $(el[0]).attr('name'); } } result[name || id] = rules[i][id]; } } } return result; } }; $(document).ready(function () { // Override the default validation hander to build the validation summary $.validator.defaults.onfocusout = function (element) { if ($(element).closest('.sys_cms-form-control').length != 0) { // BJ: Looks to see if it's in our forms before running HandleInputChange. HandleInputChange(element); } }; function HandleInputChange(element) { var currentpageId = ReturnPageId($(element).parents("li").attr('id')); var formID = ReturnFormId($(element).parents("ul").attr('id')).split("-")[1]; ShowValidationSummary(currentpageId, formID, false); } // Override default error message for url $.validator.messages.url = $('#hdValUrl' + $('[id*=hdValUrl]').attr('id').replace('hdValUrl', '')).val(); // add custom val method for filetype value $.validator.addMethod("filetypeallowed", function (value, element, param) { var paramSplit = param.split(","); if (value != "") { for (i = 0; i < paramSplit.length; i++) { if (value.toLowerCase().indexOf("." + $.trim(paramSplit[i])) > -1) { return true; } } } else { // let the check pass because any required fields will be handled with the required rule return true; } return false; }, $.validator.format($('#hdValFileType' + $('[id*=hdValFileType]').attr('id').replace('hdValFileType', '')).val())); // add custom val method for duplicate value $.validator.addMethod("isdup", function (value, element, param) { if (!param) { return true; } return false; }, $.validator.format($('#hdValIsDuplicate' + $('[id*=hdValIsDuplicate]').attr('id').replace('hdValIsDuplicate', '')).val())); // add custom val method for integer less than $.validator.addMethod("intlessthan", function (value, element, param) { if (parseInt(value) < parseInt(param)) { return true; } return false; }, $.validator.format($('#hdValLessThan' + $('[id*=hdValLessThan]').attr('id').replace('hdValLessThan', '')).val())); // add custom val method for minword count $.validator.addMethod("minwords", function (value, element, param) { var wordArray = value.split(/[\s\.\?]+/); if (wordArray.length >= param) { return true; } return false; }, $.validator.format($('#hdValMinWords' + $('[id*=hdValMinWords]').attr('id').replace('hdValMinWords', '')).val())); // add custom val method for maxword count $.validator.addMethod("maxwords", function (value, element, param) { var wordArray = value.split(/[\s\.\?]+/); if (wordArray.length <= param) { return true; } return false; }, $.validator.format($('#hdValMaxWords' + $('[id*=hdValMaxWords]').attr('id').replace('hdValMaxWords', '')).val())); // add custom val method for min checkboxes checked $.validator.addMethod("mincheckbox", function (value, element, param) { var $p = $(element).parent().parent(); var selected = $p.find('input[type=checkbox]:checked').length; if (selected >= param) { $p.children().children().removeClass('sys_cms-form-error'); $p.siblings('.sys_cms-form-error').remove(); return true; } $p.children('input[type=checkbox]').addClass('sys_cms-form-error'); return false; }, $.validator.format($('#hdValMinCheckbox' + $('[id*=hdValMinCheckbox]').attr('id').replace('hdValMinCheckbox', '')).val())); // shows or hides Other controls for radio elements $('.sys_cms-form-control div ul li div span input').click(function () { if ($(this).is('input[type=radio]')) { var parentDiv = $(this).parent().parent(); if ($(this).val() == 'Other') { $(parentDiv).children('span.sys_choices_other').find('.sys_otherlabel').show(); $(parentDiv).children('span.sys_choices_other').find('.sys_otherlabel').siblings().last().show(); $(parentDiv).children('span.sys_choices_other').find('.sys_otherlabel').siblings().last().focus(); } else { $(parentDiv).children('span.sys_choices_other').find('.sys_otherlabel').hide(); $(parentDiv).children('span.sys_choices_other').find('.sys_otherlabel').siblings().last().hide(); $(parentDiv).children('span.sys_choices_other').find('.sys_otherlabel').siblings().last().blur(); } } }); $.fn.randomize = function (childElem) { return this.each(function () { var $this = $(this); var elems = $this.children(childElem); var parent = $this.parent(); var modCount = 0; if ($(parent).hasClass('sys_threecolumns')) { modCount = 3; } else if ($(parent).hasClass('sys_twocolumns')) { modCount = 2; } elems.sort(function () { return (Math.round(Math.random()) - 0.5); }); $this.remove(childElem); for (var i = 0; i < elems.length; i++) { $(elems[i]).removeAttr('class'); if ((i % modCount) == 0) { $(elems[i]).addClass('sys_boxclearer'); } $this.append(elems[i]); } }); }; function SetInstructionsClass() { //console.warn('set instructions'); if ($('.sys_cms-form-body').find('[id*=instructions]').length > 0) { $('.sys_cms-form-control').addClass('sys_showinstructions'); } else { $('.sys_cms-form-control').removeClass('sys_showinstructions'); } } SetInstructionsClass(); function CheckLoadSingleReCaptcha() { var hdCaptcha = $('[id*=hdCaptchaVersion]'); if (hdCaptcha.length) { var formID = ReturnFormIdFromCaptcha($(hdCaptcha).attr('id')); var checkCaptcha = hdCaptcha.val(); if (checkCaptcha) { displayFormCaptcha(formID); } } } CheckLoadSingleReCaptcha(); $('.sys_cms-form-page').bind('keydown', function (e) { var key = e.which || e.keyCode; if (key === 13 && !acceptsEnter(e.target)) { var nextBtn = $('[id*=btnNext]', this); if (nextBtn.length) { nextBtn.click(); return false; } var submitBtn = $('[id*=btnSubmitForm]', this); if (submitBtn.length) { submitBtn.click(); return false; } } }); function acceptsEnter(element) { if (isDisabled(element)) { return false; } if (!isVisible(element)) { return false; } if (isAnchorWithhref(element)) { return true; } if (element.hasAttribute('contenteditable')) { return true; } let nodeName = element.nodeName.toLowerCase(); if (nodeName === 'button' || nodeName === 'textarea') { return true; } if (nodeName === 'input') { let type = element.getAttribute('type'); if (type && ((type.toLowerCase() === 'submit') || (type.toLowerCase() === 'reset'))) { return true; } } return false; } function isDisabled(element) { return element.hasAttribute('disabled'); } function isVisible(element) { return hasGeometry(element) && getComputedStyle(element).visibility === 'visible'; } function hasGeometry(element) { return !!(element.offsetWidth || element.offsetHeight || (typeof element.getClientRects === 'function' && element.getClientRects().length)); } function isAnchorWithhref(element) { return isAnchorElement(element) && element.hasAttribute('href'); } function isAnchorElement(element) { return element.nodeName.toLowerCase() === 'a'; } $("input,select,textarea").focus(function () { var formID = ReturnFormIdFromControl($(this).attr('id')); var checkCaptcha = $('#hdCaptchaVersion' + formID).val(); if (checkCaptcha) { if (formID != ContensisFormsCurrentFormID && ContensisFormsCurrentFormID != null) { ContensisFormsCaptchaVisible = false; } if (!ContensisFormsCaptchaVisible) { displayFormCaptcha(formID); } } //$('[id*=instructions]').hide(); // [Added by iG 16/04/2012] - Hide all of the instructions first. Ticketid: 13259 if ($('.sys_cms-form-control').hasClass('sys_showinstructions')) { ContensisFormsLastElementFocus = null; ContensisFormsLastElementFocus = $(this); var formelementid = ReturnItemId($(this).attr('id')); $('#instructions_' + formelementid + '-' + formID).show(); //console.warn('focus : ' + '#instructions_' + formelementid + '-' + formID); //console.warn('formid : ' + formID); //console.warn('formelementid : ' + formelementid); } }); function displayFormCaptcha(formID) { ContensisFormsCaptchaVisible = true; ContensisFormsCurrentFormID = formID; } $("input,select,textarea").blur(function () { var formID = ReturnFormIdFromControl($(this).attr('id')); if ($('.sys_cms-form-control').hasClass('sys_showinstructions')) { ContensisFormsLastElementFocus = null; var formelementid = ReturnItemId($(this).attr('id')); $('#instructions_' + formelementid + '-' + formID).hide(); } }); $('.sys_cms-form-body li').hover( function () { //if ($(this).find('[id*=instructions]').length > 0 && ContensisFormsLastElementFocus == null) { if ($(this).find('[id*=instructions]').length > 0) { $(this).find('[id*=instructions]').show(); } }, function () { //if ($(this).find('[id*=instructions]').length > 0 && ContensisFormsLastElementFocus == null) { if ($(this).find('[id*=instructions]').length > 0) { $(this).find('[id*=instructions]').hide(); } }); $('.sys_cms-form-body li input.required, textarea.required, select.required').each(function () { if ($(this).hasClass('timeval') || $(this).hasClass('tiny') || $(this).hasClass('address')) { if ($(this).parent().parent().prev('.sys_legend').children().length == 0) { $(this).parent().parent().prev('.sys_legend').append('*'); } } else { $(this).parent().prev('label').append('*'); } }); $('.sys_cms-form-body li input.filerequired').each(function () { $(this).parent().prev('label').append('*'); }); $('.sys_cms-form-body li input.checkrequired, input.radiorequired').each(function () { if ($(this).parent().parent().prev('.sys_legend').children().length == 0) { $(this).parent().parent().prev('.sys_legend').append('*'); } }); $('div.random').randomize('span'); $('.nodups').blur(function () { return validateDups(this, true); }); function validateDups(el, async) { var formelementid = ReturnItemId($(el).attr('id')); var formID = ReturnFormIdFromControl($(el).attr('id')); var val = $(el).val(); ContensisFormsLastElementFocus = null; ContensisFormsLastElementFocus = $(el); CheckDuplicate(formID, formelementid, val, async); return false; } function ReturnPageId(objId) { var IdArray = objId.split('_'); return IdArray[1]; } function ReturnItemId(objId) { var IdArray = objId.split('_'); var notNumber = isNaN(IdArray[1]); if (!notNumber) { // so is a number return IdArray[1]; } else { var value = IdArray[1].split('-'); return value[0]; } } function ReturnFormId(objId) { var IdArray = objId.split('_'); return IdArray[1]; } function ReturnFormIdFromControl(objId) { var IdArray = objId.split('-'); return IdArray[1]; } function ReturnFormIdFromCaptcha(objId) { return objId.replace('hdCaptchaVersion', ''); } function FormHasUploadElement(formID) { var hasForm = false; $('#formwrapperJS' + formID).find('.formsuploader').each(function (index) { if ($(this).val() != "") { hasForm = true; } }); return hasForm; } function scrollToTopOfForm(formId) { try { var form = $('#formwrapperJS' + formId); var formTop = form.offset().top; var windowScrollTop = $(window).scrollTop(); var windowHeight = $(window).innerHeight(); if (windowScrollTop > formTop || ((windowScrollTop + windowHeight) < formTop)) { $(window).scrollTop(formTop); } } catch (e) { } } $('[id*=btnSubmitForm]').click(function () { var formID = ReturnFormId($(this).attr('id')); var pageID = $(this).parent().parent().attr('id'); var currentpageId = ReturnPageId(pageID).split("-")[0]; var formIsValid = true; var invalidCount = 0; var thisForm = $('form#form1, form#Form1, form#aspnetForm'); var formHasUploadControl = FormHasUploadElement(formID); $(this).parent().parent().find("input, select, textarea").each(function (index) { if (typeof (thisForm.validate().element($(this))) !== 'undefined') { if ($(this).hasClass('formsuploader')) { if ($(this).val() != "") { formHasUploadControl = true; } } formIsValid = thisForm.validate().element($(this)); if (!formIsValid) invalidCount++; //if (!formIsValid) // break the loop if we fail the validation // return false; } }); if (invalidCount > 0) { formIsValid = false; ShowValidationSummary(currentpageId, formID, true); } if (formIsValid) { $("#formvalidationsummary_" + currentpageId + "-" + formID).remove(); $(this).attr("disabled", "disabled"); SaveFormResponse(formID, pageID, formHasUploadControl); } return false; }); $('[id*=btnNext]').click(function () { var currentpageId = parseInt(ReturnPageId($(this).attr('id'))); var formID = ReturnFormIdFromControl($(this).attr('id')); var nextpageId = CheckPageRule(formID, currentpageId); if (ValidatePage(formID, currentpageId, nextpageId)) { $('#formpage_' + currentpageId + '-' + formID).toggle(); if (nextpageId == null) { nextpageId = currentpageId + 1; } if (currentpageId == 0) { $('#hdPageNav' + formID).val('0'); } else { var pageSequence = []; pageSequence = ReturnPageSequenceArray(formID); pageSequence.push(currentpageId); $('#hdPageNav' + formID).val(pageSequence.join(",")); } $('#formpage_' + nextpageId + '-' + formID).toggle(); $("#formvalidationsummary_" + nextpageId + "-" + formID).show(); scrollToTopOfForm(formID); } return false; }); $('[id*=btnBack]').click(function () { var currentpageId = parseInt(ReturnPageId($(this).attr('id'))); var formID = ReturnFormIdFromControl($(this).attr('id')); $("#formvalidationsummary_" + currentpageId + "-" + formID).hide(); var prevPageId; var pageSequence = []; pageSequence = ReturnPageSequenceArray(formID); pageSequence.reverse(); if (pageSequence[0] == currentpageId) { pageSequence.splice(0, 1); } prevPageId = pageSequence[0]; pageSequence.reverse(); $('#hdPageNav' + formID).val(pageSequence.join(",")); $("#formpage_" + currentpageId + '-' + formID).toggle(); $("#formpage_" + prevPageId + '-' + formID).toggle(); scrollToTopOfForm(formID); return false; }); function ReturnPageSequenceArray(formID) { return $('#hdPageNav' + formID).val().split(","); } function PageIsInSequence(formID, currentPageId) { var pageSequence = ReturnPageSequenceArray(formID); for (var i = 0; i < pageSequence.length; i++) { var pageId = pageSequence[i]; if (pageId == currentPageId) { return true; break; } } return false; } function ValidatePage(formID, currentpageId, nextpageId) { var objCurrentPage = $('#formpage_' + currentpageId + '-' + formID); var objNextPage = $('#formpage_' + nextpageId + '-' + formID); var invalidCount = 0; var pageIsValid = true; var thisForm = $('form#form1, form#Form1, form#aspnetForm'); $(objCurrentPage).find("input, select, textarea").each(function (index) { if ($(this).is('.nodups')) { validateDups(this, false); } if (typeof (thisForm.validate().element($(this))) !== 'undefined') { pageIsValid = thisForm.validate().element($(this)); if (!pageIsValid) invalidCount++; //if (!pageIsValid) // break the loop if we fail the validation // return false; } }); if (invalidCount > 0) { pageIsValid = false; ShowValidationSummary(currentpageId, formID, true); } if (pageIsValid) { $("#formvalidationsummary_" + currentpageId + "-" + formID).remove(); return true; } return false; } $.contensisForm.init(); }); var originalDocumentTitle = document.title; function ShowValidationSummary(pageId, formId, scrollToError) { var summaryId = 'formvalidationsummary_' + pageId + "-" + formId; var summaryTitleId = summaryId + '-title'; //aria-labelledby="error-summary-title" $('#' + summaryId).remove(); var validationSummary = $('