const _states = {'NSW':894690000, 'VIC':894690001, 'NT':894690002, 'QLD':894690003, 'SA':894690004, 'WA':894690005, 'TAS':894690006, 'ACT':894690007 }; const VALIDATOR_TYPE = {_blank:"", _required:"RequiredFieldValidator", _email:"RequiredEmailValidator", _healthEmail:"RequiredHealthEmailValidator", _dateLessThan:"RequiredDateLessThanValidator", _dateGreaterThan:"RequiredDateGreaterThanValidator", _atleastOneSubgridRecord:"RequiredAtleastOneRecordInSubgrid", _atleastOneRecordBetweenSubgrids:"RequiredAtleastOneRecordBetweenSubgrids"}; const VALIDATOR_GROUP = {_blank:"", _profile:"Profile"} var NSWUtilities4MSPP = { form_addValidatorAtleastOneRecordBetweenSubgrids:function (arrGridNames, gridLabel, minRecords = 1, validationMsg = undefined, validationGroup=VALIDATOR_GROUP._blank) { if (typeof (Page_Validators) == 'undefined') { console.error("Could not attach custom validator : 'Page_Validators' is undefined."); return }; let combinedGridName = arrGridNames.join('_'); if(gridLabel == undefined){ gridLabel = arrGridNames.join(' or ') } if(this.field_isInFormValidator(combinedGridName, Page_Validators, VALIDATOR_TYPE._atleastOneRecordBetweenSubgrids) == true) return; //if validator already exists // Create new validator //$("#" + fieldName + "_label").parent().addClass("required"); //let minRecordsMsg = minRecords == 1 ? gridLabel + " should have atleast " + minRecords + " record." : gridLabel + " should have atleast " + minRecords + " records." let minRecordsMsg = "Please enter atleast " + minRecords + " record" + ( minRecords == 1 ? "" : "s") + " in either of the grids. (" + gridLabel + ")" ; validationMsg = ((validationMsg === undefined || validationMsg == "") ? minRecordsMsg : validationMsg); let newValidator = document.createElement('span'); newValidator.style.display = "none"; newValidator.id = VALIDATOR_TYPE._atleastOneSubgridRecord + combinedGridName; newValidator.controltovalidate = combinedGridName; // "casetypecode"; newValidator.errormessage = `` + validationMsg + ``; newValidator.validationGroup = validationGroup; newValidator.initialvalue = ""; newValidator.evaluationfunction = function () { let results = false; $.each(arrGridNames, function(key, gridName){ if($("#" + gridName + " tr[data-entity]").length >= minRecords){ results = true; } }); return results; }; Page_Validators.push(newValidator); }, //function to check if there is atleast one record in the subgrid form_addValidatorAtleastOneSubgridRecord:function (gridName, gridLabel, minRecords = 1, validationMsg = undefined, validationGroup=VALIDATOR_GROUP._blank) { if (typeof (Page_Validators) == 'undefined') { console.error("Could not attach custom validator : 'Page_Validators' is undefined."); return }; if(this.field_isInFormValidator(gridName, Page_Validators, VALIDATOR_TYPE._atleastOneSubgridRecord) == true) return; //if validator already exists if(gridLabel == undefined) gridLabel = gridName; // Create new validator //$("#" + fieldName + "_label").parent().addClass("required"); //let minRecordsMsg = minRecords == 1 ? gridLabel + " should have atleast " + minRecords + " record." : gridLabel + " should have atleast " + minRecords + " records." let minRecordsMsg = "Please enter atleast " + minRecords + " record" + ( minRecords == 1 ? "" : "s") + " in " + gridLabel + "."; validationMsg = ((validationMsg === undefined || validationMsg == "") ? minRecordsMsg : validationMsg); let newValidator = document.createElement('span'); newValidator.style.display = "none"; newValidator.id = VALIDATOR_TYPE._atleastOneSubgridRecord + gridName; newValidator.controltovalidate = gridName; // "casetypecode"; newValidator.errormessage = `` + validationMsg + ``; newValidator.validationGroup = validationGroup; newValidator.initialvalue = ""; newValidator.evaluationfunction = function () { let rows = $("#" + gridName + " tr[data-entity]").length; if(rows >= minRecords){ return true; } else { return false; } }; Page_Validators.push(newValidator); }, form_addValidatorDateGreaterThan: function (fieldName, controlDate = new Date(new Date().setHours(0, 0, 0, 0)), validationMsg = undefined, validationGroup=VALIDATOR_GROUP._blank) { if (typeof (Page_Validators) == 'undefined') { console.error("Could not attach custom validator : 'Page_Validators' is undefined."); return }; if(this.field_isInFormValidator(fieldName, Page_Validators, VALIDATOR_TYPE._dateGreaterThan) == true) return; //if validator already exists // Create new validator //$("#" + fieldName + "_label").parent().addClass("required"); validationMsg = ((validationMsg === undefined || validationMsg == "") ? $("#" + fieldName + "_label").text() + " cannnot be less than " + controlDate.format("dd/MM/yyyy"): validationMsg); let newValidator = document.createElement('span'); newValidator.style.display = "none"; newValidator.id = VALIDATOR_TYPE._dateGreaterThan + fieldName; newValidator.controltovalidate = fieldName; // "casetypecode"; newValidator.errormessage = `` + validationMsg + ``; newValidator.validationGroup = validationGroup; newValidator.initialvalue = ""; newValidator.evaluationfunction = function () { let value = $("#" + fieldName).val(); let dateValue = new Date(value); if (value == null && value == "") { return true; } else if(isNaN(dateValue)){ return true; } else if(dateValue > controlDate) { return true; } else { return false; } }; Page_Validators.push(newValidator); }, form_addValidatorDateLessThan: function (fieldName, controlDate = new Date(new Date().setHours(0, 0, 0, 0)), validationMsg = undefined, validationGroup=VALIDATOR_GROUP._blank) { if (typeof (Page_Validators) == 'undefined') { console.error("Could not attach custom validator : 'Page_Validators' is undefined."); return }; if(this.field_isInFormValidator(fieldName, Page_Validators, VALIDATOR_TYPE._dateLessThan) == true) return; //if validator already exists // Create new validator //$("#" + fieldName + "_label").parent().addClass("required"); validationMsg = ((validationMsg === undefined || validationMsg == "") ? $("#" + fieldName + "_label").text() + " cannnot be greater than " + controlDate.format("dd/MM/yyyy"): validationMsg); let newValidator = document.createElement('span'); newValidator.style.display = "none"; newValidator.id = VALIDATOR_TYPE._dateLessThan + fieldName; newValidator.controltovalidate = fieldName; // "casetypecode"; newValidator.errormessage = `` + validationMsg + ``; newValidator.validationGroup = validationGroup; newValidator.initialvalue = ""; newValidator.evaluationfunction = function () { let value = $("#" + fieldName).val(); let dateValue = new Date(value); if (value == null && value == "") { return true; } else if(isNaN(dateValue)){ return true; } //else if(dateValue >= controlDate) { else if(dateValue > controlDate) { return false; } else { return true; } }; Page_Validators.push(newValidator); }, form_addValidatorHealthEmail: function (fieldName, validationMsg = undefined, validationGroup=VALIDATOR_GROUP._blank) { if (typeof (Page_Validators) == 'undefined') { console.error("Could not attach custom validator : 'Page_Validators' is undefined."); return }; if(this.field_isInFormValidator(fieldName, Page_Validators, VALIDATOR_TYPE._healthEmail) == true) return; //if validator already exists // Create new validator //$("#" + fieldName + "_label").parent().addClass("required"); validationMsg = ((validationMsg === undefined || validationMsg == "") ? "Your email account does not match with one of the approved NSW Health accounts ending with health.nsw.gov.au for the role you have selected." : validationMsg); let newValidator = document.createElement('span'); newValidator.style.display = "none"; newValidator.id = VALIDATOR_TYPE._healthEmail + fieldName; newValidator.controltovalidate = fieldName; // "casetypecode"; //newValidator.errormessage = "Your email account does not match with one of the approved NSW Health accounts ending with health.nsw.gov.au for the role you have selected."; newValidator.errormessage = `` + validationMsg + ``; newValidator.validationGroup = validationGroup; newValidator.initialvalue = ""; newValidator.evaluationfunction = function () { let value = $("#" + fieldName).val(); if (value != null && value != "") { let emailRegex = /^[a-zA-Z0-9]+[a-zA-Z0-9.%\-\+]*@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,4}$/ ; if(!value.match(emailRegex)){ return false; } let emailDomain = value.split("@")[1].toLowerCase(); switch (emailDomain) { case "moh.health.nsw.gov.au": case "doh.health.nsw.gov.au": case "health.nsw.gov.au": case "justicehealth.nsw.gov.au": case "ncahs.health.nsw.gov.au": case "sswahs.health.nsw.gov.au": case "svha.org.au": case "gsahs.health.nsw.gov.au": case "hammond.com.au": return true; break; default: return false; } } else { return false; } }; // Add the new validator to the page validators array: Page_Validators.push(newValidator); // Wire-up the click event handler of the validation summary link //$("a[href='#" + fieldName + "_label']").on("click", function () { scrollToAndFocus(fieldName + '_label', fieldName); }); }, form_addValidator: function (fieldName, validationMsg = undefined, validationGroup=VALIDATOR_GROUP._blank) { if (typeof (Page_Validators) == 'undefined') { console.error("Could not attach custom validator : 'Page_Validators' is undefined."); return }; if(this.field_isInFormValidator(fieldName, Page_Validators, VALIDATOR_TYPE._required) == true) return; //if validator already exists // Create new validator $("#" + fieldName + "_label").parent().addClass("required"); validationMsg = ((validationMsg === undefined || validationMsg == "") ? $("#" + fieldName + "_label").text() + " is a mandatory field." : validationMsg); let newValidator = document.createElement('span'); newValidator.style.display = "none"; newValidator.id = VALIDATOR_TYPE._required + fieldName; newValidator.controltovalidate = fieldName; // "casetypecode"; newValidator.errormessage = `` + validationMsg + ``; newValidator.validationGroup = validationGroup; newValidator.initialvalue = ""; newValidator.evaluationfunction = function () { switch($("#" + fieldName).prop("type")){ case "checkbox": if($("#" + fieldName).prop("checked")){ return true; } break; default: let value = $("#" + fieldName).val(); if (value != null && value != "") { return true; } } return false; }; // Add the new validator to the page validators array: Page_Validators.push(newValidator); // Wire-up the click event handler of the validation summary link //$("a[href='#" + fieldName + "_label']").on("click", function () { scrollToAndFocus(fieldName + '_label', fieldName); }); }, //eg. removeValidator("customerid") form_removeValidator: function(fieldName) { $.each(Page_Validators, function (index, validator) { if (validator !== undefined && validator.controltovalidate == fieldName) { Page_Validators.splice(index, 1); } }); $("#" + fieldName + "_label").parent().removeClass("required"); $("#" + fieldName).attr("aria-invalid","false"); }, form_hideField : function (fieldId){ $("#"+fieldId).hide(); $("#"+fieldId+"_label").hide(); $("#"+fieldId+"_label").parent().parent().hide(); this.form_removeValidator(fieldId); }, form_clearAndHideField : function (fieldId, clearReadOnlyFields = false){ if(!$("#"+fieldId).prop('disabled') || clearReadOnlyFields ) {$("#"+fieldId).clearInputs()}; this.form_hideField(fieldId); VendorUtilities.sendHeight(); //PP }, form_showField : function (fieldId){ $("#"+fieldId).show(); $("#"+fieldId+"_label").show(); $("#"+fieldId+"_label").parent().parent().show(); }, form_showFieldAndAddValidation : function (fieldId, validationGroup=VALIDATOR_GROUP._blank , validatorPrefix=VALIDATOR_TYPE._required ){ this.form_showField(fieldId); VendorUtilities.sendHeight(); switch(validatorPrefix){ case VALIDATOR_TYPE._required: this.form_addValidator(fieldId,undefined,validationGroup); break; case VALIDATOR_TYPE._email: //this.form_addValidatorEmail(fieldId,undefined,validationGroup); break; case VALIDATOR_TYPE._healthEmail: this.form_addValidatorHealthEmail(fieldId,undefined,validationGroup); break; case VALIDATOR_TYPE._blank: default: console.error("Invalid validatorPrefix value passed : " + validatorPrefix); } //Add switch case based on validatorPrefix //this.form_addValidator(fieldId,undefined,validationGroup); }, field_isInFormValidator : function (fieldId, arrValidators, validatorPrefix=VALIDATOR_TYPE._required){ let isPresent = false; let validatorId = validatorPrefix + fieldId; for (var i = 0; i < arrValidators.length; i++) { if (arrValidators[i].id == validatorId) { isPresent = true; break; } } return isPresent; }, setDateTimeFieldValue : function (fieldId, dateValue, saveInUTC = false) { //Get the submit field var $submitField = $("#" + fieldId); //Get the display field var $displayField = $submitField.nextAll(".datetimepicker").children("input"); //Get the display date format var dateFormat = $displayField.attr("data-date-format"); if(saveInUTC){ //Set the submit field. Remember this needs to be in UTC and the format must be exact. $submitField.val(moment.utc(dateValue).format("YYYY-MM-DDTHH:mm:ss.SSSSSSS")); } else{ $submitField.val(moment(dateValue).format("YYYY-MM-DDTHH:mm:ss.SSSSSSS")); } //Set the display field using the page's date format $displayField.val(moment(dateValue).format(dateFormat)); } } /* var VendorUtilities = { sendHeight : function(){ debugger; //const height = document.body.scrollHeight; const height = $(".row.sectionBlockLayout.text-start").height(); // Sends the form height only. will vary by each form // Post message to the parent with height data console.log("Send " + height); window.parent.postMessage( { type: "resize", height: height }, "*" ); $('.modal-content:visible').innerWidth(); } } */ /* Pirasanth */ var VendorUtilities = (function () { function postHeightMessage(action) { const height = $(".row.sectionBlockLayout.text-start").height(); console.log("Send " + height + (action ? " with action: " + action : "")); window.parent.postMessage( { type: "resize", height: height, action: action || null }, "*" ); } function postHeightToParent(action) { var modalHeight = $('.modal-content:visible').innerHeight(); var modalWidth = $('.modal-content:visible').innerWidth(); if(action == "Insert"){ modalHeight = $('.form').height(); modalWidth = $('.form').width(); } console.log("Modal height: " + modalHeight); window.parent.postMessage( { type: "resize", height: modalHeight, width:modalWidth, action: action || null }, "*" ); } return { sendHeight: function () { postHeightMessage(); // Just height without action }, sendNext: function () { postHeightMessage("Next"); // Height + "Next" action }, sendPrevious: function () { postHeightMessage("Previous"); // Height + "Previous" action }, sendSubmit: function () { postHeightMessage("Submitted"); // Height + "Submitted" action }, sendModalOpen: function () { postHeightToParent("Open"); // Open }, sendModalOpenSudmit: function () { postHeightToParent("Insert"); // Insert } }; })();