$(document).ready(function () { FRMPTIAppForm.OnInitForm(); }); var FRMPTIAppForm = FRMPTIAppForm || {}; (function () { var onInitForm = function () { $("#mcm_hasaccessedservicebefore").change(this.OnChangeAccessedServiceBefore); setPractitionerLookupValues(); var dobControl = $('div.control')[23]; //note this is hard coded $(dobControl).on("dp.change", function (e) { var age = getAge(e.date); if (age >= 0) { $("#mcm_applicantage").val(getAge(e.date)); } else { $("#mcm_applicantage").val(null); } }); }; var onChangeAccessedServiceBefore = function () { let hasaccessedservicebefore = $("#mcm_hasaccessedservicebefore_1").is(":checked"); if (hasaccessedservicebefore) { PortalFormCommon.AddValidator("mcm_lastaccessedservicedate", "Last accessed service date is required."); } else { PortalFormCommon.RemoveValidator("mcm_lastaccessedservicedate"); } }; var getAge = function (dateString) { var today = new Date(); var birthDate = new Date(dateString); var age = today.getFullYear() - birthDate.getFullYear(); var m = today.getMonth() - birthDate.getMonth(); if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) { age--; } return age; }; var setPractitionerLookupValues = function () { //determine if the dropdown should be cleared let currentValue = $("#mcm_keyworkerid").val(); // request to our custom page with id as parameter $.getJSON("/frmppractitioners-json", function (data) { if (data.results.length > 0) { $("#mcm_keyworkerid").empty(); //create option for each returned entity data.results.forEach(element => { let option = document.createElement("option"); option.value = element.Id; option.innerText = element.Name; $("#mcm_keyworkerid").append(option); }); $("#mcm_keyworkerid").val(currentValue); } } ); }; /** * Public Members */ this.OnInitForm = onInitForm; this.OnChangeAccessedServiceBefore = onChangeAccessedServiceBefore; }).apply(FRMPTIAppForm);