function CheckValidationErrors(ValidatingForm) { var wValidationFormID = ""; if (ValidatingForm != "") { wValidationFormID = "#" +ValidatingForm + " "; } if ($(wValidationFormID + ".field-validation-error").length > 0) { var wErrorMessage = wThefollowingerrorsmustbecorrected + "\n\n";//"The following errors must be corrected:\n\n"; var wFirstErrorField = ""; var wScrollTarget = null; var wFirstErrorFields =[]; for (var i = 0; i < $(wValidationFormID + ".field-validation-error").length; i++) { var wParentNode = $(wValidationFormID + ".field-validation-error")[i].parentNode; wFirstErrorFields.push($(wValidationFormID + ".field-validation-error")[i].innerText); while (wParentNode != null) { if ($(wParentNode).hasClass('form-group')) { break; } wParentNode = wParentNode.parentNode; } if (wScrollTarget == null) { wScrollTarget = $(wParentNode).closest('.form-group'); $(wScrollTarget)[0].scrollIntoView(); } if (wParentNode != null) { $(wParentNode).addClass('has-feedback'); $(wParentNode).addClass('has-error'); } } if (wFirstErrorFields.length > 0) { var validation_summary_area = $('.validation-summary', $('#' +ValidatingForm)) //$(ValidatingForm)(".validation-summary"); if (validation_summary_area != null && validation_summary_area.length > 0) { var wValidationFieldArea = $(validation_summary_area[0]); if(!wValidationFieldArea.hasClass('alert')) { wValidationFieldArea.addClass("alert"); } if (!wValidationFieldArea.hasClass('alert-danger')) { wValidationFieldArea.addClass("alert-danger"); } $(validation_summary_area)[0].scrollIntoView(); } } if (wValidationFormID != "") { $(wValidationFormID).find(":submit").removeClass('disabled submit-disabled'); } else { $('.submit-disabled').removeClass('disabled submit-disabled'); } } } function RegisterFormValidation(JQueryFormCtrl) { $.validator.unobtrusive.parse(JQueryFormCtrl); JQueryFormCtrl.submit(function (e) { var wFormID = e.target.id; //if (wFormID != "") { $(this).find(".form-group.has-feedback").removeClass("has-feedback"); $(this).find(".form-group.has-error").removeClass("has-error"); var wForm = "#" +wFormID; //$(wForm).validate().form(); $(this).find(":submit:not(.no-disable)").addClass('disabled submit-disabled') $(this).validate().form(); CheckValidationErrors(wFormID); } //return false; }); } var LinkHasChangesSelecter = 'a[href]:not([href^="javascript"], [href^="#"], [btn-type^="redirect"])'; var tempTab; var wLostFocusClickedTabEvent; function ApplyHasChangesCheck() { var ClickedTab = null; var mContinuewithTabClickEvent = false; function CheckAppropriateAction(aButton) { var wResettingForm = null; var wClickEvent = aButton.attr("onclick"); var wHREFValue = aButton.attr("href"); var wBtnTypeValue = aButton.attr("btn-type"); if (wClickEvent == null ){ if (wHREFValue == "#" || wHREFValue == "javascript:void(0)") { wResettingForm = $(aButton).parents('form:first'); } else if (wBtnTypeValue == "redirect" && wHREFValue != "") { $(window).off('beforeunload'); return; } } return wResettingForm; } function ActiveTabHasChanges() { if (tempTab != null) { this.ActiveTab = TabInfo(tempTab); } else { this.ActiveTab = TabInfo($('a[data-toggle="tab"]').parents('.active')); } this.HasChanges = false; if (ActiveTab != null) { this.Tab = ActiveTab.Tab; this.Panel = ActiveTab.Panel; if (ActiveTab == null || Tab == null || Panel == null || mContinuewithTabClickEvent) { this.HasChanges = false; } else { this.HasChanges = CheckPanelHasChanges(Panel); } } return this; } function CheckPanelHasChanges(wPanel) { wHasChanges = false; $(wPanel).find('form').each(function () { if (wHasChanges) { return; } wHasChanges = CheckFormHasChanges($(this)) }); return wHasChanges; } function URLLinkClickEvent(e) { tempTab = $(this).parent().parent().find('.active'); var URLTabClicked = TabInfo(tempTab); if (URLTabClicked.Panel == null || URLTabClicked.Panel === undefined || URLTabClicked.Panel.length <= 0) { tempTab = $(this).parent().parent().parent().parent().find('li.active [data-toggle=tab]'); } if (ActiveTabHasChanges().HasChanges) { e.preventDefault(); ClickedTab = this; PromptForTabSave(ActiveTab.Tab, ActiveTab.Panel); } } function CheckFormHasChanges(wForm) { wHasChanges = false; var ForceChangesCheck = false; //[data-toggle="popover"] if ($(wForm).attr("haschangescheck") != null && $(wForm).attr("haschangescheck").toLowerCase() == "false") { return; } else if ($(wForm).attr("haschangescheck") != null && $(wForm).attr("haschangescheck").toLowerCase() == "true") { ForceChangesCheck = true; } $(wForm).find(':input').each(function () { if (wHasChanges || ($(this).attr("HasChangesCheck") !== undefined && $(this).attr("HasChangesCheck").toLowerCase() == "false")) { return; } if ($(this)[0].name != "multiselect") { var wOrignialValue = $(this).attr("OriginalValue"); if (wOrignialValue == null) { wOrignialValue = $(this).attr("originalvalue"); } if(wOrignialValue == null){ if (ForceChangesCheck){ wOrignialValue = ""; } } if (wOrignialValue != null) { switch ($(this)[0].nodeName) { case "TEXTAREA": case "INPUT": switch ($(this).attr("type")) { case "hidden": break; case "checkbox": //if(wOrignialValue.toLowerCase() != $(this)[0].checked.toString().toLowerCase()){ if (wOrignialValue.toLowerCase() != $(this)[0].checked.toString().toLowerCase()) { wHasChanges = true; return } break; case "radio": if (wOrignialValue.toLowerCase() == $(this).val().toString().toLowerCase()) { if (!$(this)[0].checked) { wHasChanges = true; return } } break; default: if (wOrignialValue != $(this).val()) { wHasChanges = true; return } break; } case "SELECT": wMultiSelect = ($(this).hasClass("multiselect") ? true : false); wOrignialSelected = wOrignialValue.toLowerCase().split("^"); if ($(this)[0].name.indexOf('FilterCriteria') == 0 && wOrignialValue.indexOf('^') < 0) { wOrignialSelected = wOrignialValue.toLowerCase().split(","); } for (s = 0; s < $(this)[0].length; ++s) { if ($(this)[0].options[s].selected) { if (wOrignialSelected.indexOf($(this)[0].options[s].value.toLowerCase()) < 0) { wHasChanges = true; return } } else { if (wOrignialSelected.indexOf($(this)[0].options[s].value.toLowerCase()) >= 0) { wHasChanges = true; return } } } break; } } else { switch ($(this)[0].nodeName) { case "INPUT": switch ($(this).attr("type")) { case "radio": if ($(this)[0].name.toLowerCase().indexOf("default") == 0 && $(this)[0].checked) { wHasChanges = true; return } break; case "password": if ($(this).val() != "") { wHasChanges = true; return } break } break; } } } }); return wHasChanges; } function FocusFirstFormControl(aFormCtrl) { var wFirstCtrl = null; if (aFormCtrl != null && aFormCtrl.length > 0) { $(aFormCtrl).find(':input').each(function () { switch ($(this)[0].nodeName) { case "SELECT": case "TEXTAREA": case "INPUT": switch ($(this).attr("type")) { case "hidden": break; default: if (wFirstCtrl == null) { wFirstCtrl = $(this); } break; } break; } }); } if (wFirstCtrl != null) { wFirstCtrl.focus(); } } $("a.btn.btn-link[btn-type=reset], button.btn.btn-link[btn-type=reset]").click(function (e) { var wResettingForm = CheckAppropriateAction($(this)); if (wResettingForm != null && wResettingForm.length > 0) { wResettingForm[0].reset(); FocusFirstFormControl(wResettingForm); } }); $("a.btn.btn-link[btn-type=clear], button.btn.btn-link[btn-type=clear]").click(function (e) { var wResettingForm = CheckAppropriateAction($(this)); if (wResettingForm != null && wResettingForm.length > 0) { $(wResettingForm).find(':input').each(function () { if ($(this)[0].name != "multiselect") { switch ($(this)[0].nodeName) { case "TEXTAREA": case "INPUT": switch ($(this).attr("type")) { case "hidden": break; case "radio": case "checkbox": $(this)[0].checked = false; break; default: $(this).val(""); break; } break; case "SELECT": wMultiSelect = ($(this).hasClass("multiselect") ? true : false); if (wMultiSelect) { $(this).multiselect('clearSelection'); } for (s = 0; s < $(this)[0].length; ++s) { $(this)[0].options[s].selected = false; } if (wMultiSelect) { $(this).multiselect('rebuild'); } $(this).trigger("change"); break; } } }); FocusFirstFormControl(wResettingForm) } }); $("a.btn.btn-link[btn-type=redirect], button.btn.btn-link[btn-type=redirect]").click(function (e) { var wResettingForm = CheckAppropriateAction($(this)); }); $("a.btn.btn-link[btn-type=restore], button.btn.btn-link[btn-type=restore]").click(function (e) { var wResettingForm = CheckAppropriateAction($(this)); RestoreFormOriginalValue(wResettingForm); }); function RestoreFormOriginalValue(aForm) { if (aForm != null && aForm.length > 0) { $(aForm).find(':input').each(function () { if ($(this)[0].name != "multiselect") { var wOrignialValue = $(this).attr("OriginalValue"); if (wOrignialValue == null) { wOrignialValue = $(this).attr("originalvalue"); } if (wOrignialValue == null) { wOrignialValue = ""; } if (wOrignialValue != null) { switch ($(this)[0].nodeName) { case "TEXTAREA": case "INPUT": switch ($(this).attr("type")) { case "hidden": break; case "radio": case "checkbox": if (wOrignialValue != null && (wOrignialValue.toLowerCase().trim() == $(this).val().toLowerCase().trim() || wOrignialValue.toLowerCase().trim() == "true")) { $(this)[0].checked = true; } else { $(this)[0].checked = false; } break; default: $(this).val(wOrignialValue) break; } break; case "SELECT": wMultiSelect = ($(this).hasClass("multiselect") ? true : false); if (wMultiSelect) { $(this).multiselect('clearSelection'); } wOrignialSelected = wOrignialValue.toLowerCase().split("^"); if ($(this)[0].name.indexOf('FilterCriteria') == 0 && wOrignialValue.indexOf('^') < 0) { wOrignialSelected = wOrignialValue.toLowerCase().split(","); } for (s = 0; s < $(this)[0].length; ++s) { if (wOrignialSelected.indexOf($(this)[0].options[s].value.toLowerCase()) >= 0) { $(this)[0].options[s].selected = true; } else { $(this)[0].options[s].selected = false; } } if (wMultiSelect) { $(this).multiselect('rebuild'); } $(this).trigger("change"); break; } } else { switch ($(this)[0].nodeName) { case "INPUT": switch ($(this).attr("type")) { case "radio": if ($(this)[0].name.toLowerCase().indexOf("default") == 0 && $(this)[0].checked) { $(this)[0].checked = false; } break; case "password": $(this).val(""); break; } break; } } } }); } } $('[data-toggle=tab]').each(function () { var TabItem = this; var TabCtrl = $(this).parents('li'); if (TabItem.addEventListener) { TabItem.addEventListener('click', function (e) { ClickedTab = this; tempTab = $(this).parent().parent().find('.active'); if (tempTab != null && tempTab.length == 0) { tempTab = $(this).parent().parent().find('[aria-expanded="true"]'); } //$(ClickedTab).find('') // var ActiveTab = $(this).parents('.active'); var ActiveTab = ActiveTabHasChanges() if (ActiveTab.HasChanges) { e.stopImmediatePropagation(); PromptForTabSave(ActiveTab.Tab, ActiveTab.Panel); } else if (ActiveTab.Tab.hasClass('badge')) { ActiveTab.Tab.removeClass('badge'); } }, true); } else { TabItem.attachEvent("onclick", function (e) { ClickedTab = this; tempTab = $(this).parent().parent().find('.active'); if (tempTab != null && tempTab.length == 0) { tempTab = $(this).parent().parent().find('[aria-expanded="true"]'); } // var ActiveTab = $(this).parents('.active'); var ActiveTab = ActiveTabHasChanges() if (ActiveTab.HasChanges) { e.stopImmediatePropagation(); PromptForTabSave(ActiveTab.Tab, ActiveTab.Panel); } else if (ActiveTab.Tab.hasClass('badge')) { ActiveTab.Tab.removeClass('badge'); } }); } }); if ($('form a.btn[type=submit], button.btn[type=submit]').length > 0) { $('form a.btn[type=submit], button.btn[type=submit]').parents('form').submit(function (event) { $(window).unbind("beforeunload"); }); $('form a.btn[btn-type=redirect], button.btn[btn-type=redirect]').parents('form').submit(function (event) { $(window).unbind("beforeunload"); }); //$(document).ajaxComplete(function (event, xhr, settings) { // $(window).off('beforeunload', BeforeUnloadCheck()); // $(window).on('beforeunload', BeforeUnloadCheck()); //}); $(window).on('beforeunload', function (e) { tempTab = $(this).parent().parent().find('.active'); var URLTabClicked = TabInfo(tempTab); if (URLTabClicked.Panel == null || URLTabClicked.Panel === undefined || URLTabClicked.Panel.length <= 0) { tempTab = $(this).parent().parent().parent().parent().find('li.active [data-toggle=tab]'); } if (ActiveTabHasChanges().HasChanges) { return wThereAreUnsavedChanges; } }); $(LinkHasChangesSelecter).on("click", URLLinkClickEvent); } PrompDialog = $('#eRadDialog').dialog({ autoOpen: false, width: 'auto', // overcomes width:'auto' and maxWidth bug maxWidth: 600, height: 'auto', modal: true, fluid: true, //new option resizable: false // buttons: { // "Save Changes": function() { // ActiveTab.addClass('badge'); // $(ActivePanel).find("a:contains('Save'), button:contains('Save')").each(function () { // $(this).click(); // }); // mContinuewithTabClickEvent = true; // ClickedTab.click(); // $( this ).dialog( "close" ); // //e.continueEventPropegationAsIfNothingHappened(); // }, // "Revert Changes": function() { // //$(ActivePanel).find('a[text=Cancel]').each(function () { // $(ActivePanel).find("a:contains('Cancel'), button:contains('Cancel')").each(function () { // $(this).click(); // }); // mContinuewithTabClickEvent = true; // ClickedTab.click(); // //ev.resume(); // //ActiveTab.find('> a')[0].parent.trigger(e); // $( this ).dialog( "close" ); // //e.continueEventPropegationAsIfNothingHappened(); // }, // // Cancel: function() { // e.stopImmediatePropagation(); // $( this ).dialog( "close" ); // } // }, // close: function(){ // mContinuewithTabClickEvent = false; // } }); $('.ui-dialog-titlebar-close').html('' + wClose + '') function PromptForTabSave(ActiveTab, ActivePanel) { var wSaveButtonText = wSaveChanges;//"Save Changes"; if ($(ActivePanel).find("a:contains(" + wSave.toLowerCase() + "),a:contains(" + wSave + "),a:contains(" + wSave.toUpperCase() + "),a:contains(" + wSubmit.toLowerCase() + "),a:contains(" + wSubmit + "),a:contains(" + wSubmit.toUpperCase() + "), a:contains(" + wSend.toLowerCase() + "),a:contains(" + wSend + "),a:contains(" + wSend.toUpperCase() + "), button[type=submit]:contains(" + wSave.toLowerCase() + "),button[type=submit]:contains(" + wSave + "),button[type=submit]:contains(" + wSave.toUpperCase() + "), button[type=submit]:contains(" + wSend.toLowerCase() + "), button[type=submit]:contains(" + wSend + "), button[type=submit]:contains(" + wSend.toUpperCase() + "), button[type=submit]:contains(" + wSubmit.toLowerCase() + "), button[type=submit]:contains(" + wSubmit + "), button[type=submit]:contains(" + wSubmit.toUpperCase() + ")").length) { wSaveButtonText = wSend;//"Send"; } PrompDialog.dialog({ title: wConfirmationRequired,//"Confirmation Required", message: "", draggable: false, open: function () { $(this).html(wThereareunsavedchanges);//"There are unsaved changes." }, buttons: [{ text:wSaveChanges, click: function () { //ActiveTab.addClass('badge'); if ($(ActivePanel).find('form').valid()) { $(ActivePanel).find("a:contains(" + wSave.toLowerCase() + "),a:contains(" + wSave + "),a:contains(" + wSave.toUpperCase() + "),a:contains(" + wSubmit.toLowerCase() + "),a:contains(" + wSubmit + "),a:contains(" + wSubmit.toUpperCase() + "), a:contains(" + wSend.toLowerCase() + "),a:contains(" + wSend + "),a:contains(" + wSend.toUpperCase() + "), button[type=submit]:contains(" + wSave.toLowerCase() + "),button[type=submit]:contains(" + wSave + "),button[type=submit]:contains(" + wSave.toUpperCase() + "), button[type=submit]:contains(" + wSend.toLowerCase() + "), button[type=submit]:contains(" + wSend + "), button[type=submit]:contains(" + wSend.toUpperCase() + "), button[type=submit]:contains(" + wSubmit.toLowerCase() + "), button[type=submit]:contains(" + wSubmit + "), button[type=submit]:contains(" + wSubmit.toUpperCase() + "), button[type=button]:contains(" + wSave.toLowerCase() + "),button[type=button]:contains(" + wSave + "),button[type=button]:contains(" + wSave.toUpperCase() + "), button[type=button]:contains(" + wSend.toLowerCase() + "), button[type=button]:contains(" + wSend + "), button[type=button]:contains(" + wSend.toUpperCase() + "), button[type=button]:contains(" + wSubmit.toLowerCase() + "), button[type=button]:contains(" + wSubmit + "), button[type=button]:contains(" + wSubmit.toUpperCase() + ")").each(function (key, value) { if (ClickedTab != null) { wLostFocusClickedTabEvent = ClickedTab; } if (CheckFormHasChanges(value.form)) { $(value).click(); //if (value.form != null && value.form && !value.form.hasAttribute("data-ajax") && value.form.hasAttribute("action")) { // $.ajax({ // type: (value.form.hasAttribute("method") ? $(value.form).attr("method").toUpperCase() : "POST"), // url: $(value.form).attr("action"), // data: $(value.form).serialize() // }); //} else { // $(value).click();//trigger("click"); //} } }); mContinuewithTabClickEvent = true; if (ClickedTab != null) { ClickedTab.click(); ClickedTab = null; } //e.continueEventPropegationAsIfNothingHappened(); } else { CheckValidationErrors($(ActivePanel).find('form').attr('id')); toastr.error(wTheformcouldnotbesavedPleasechecktheformfieldsforerrors);//"The form could not be saved. Please check the form fields for errors." } $(this).dialog("close"); }, },{ text:wDiscardChanges, click: function () { //$(ActivePanel).find('a[text=Cancel]').each(function () { var OriginalClickedTab = ClickedTab; try { $(LinkHasChangesSelecter).off("click", URLLinkClickEvent); $(ActivePanel).find("a:contains(" + wCancel.toLowerCase() + "), button:contains(" + wCancel.toLowerCase() + "),a:contains(" + wCancel + "), button:contains(" + wCancel + "),a:contains(" + wCancel.toUpperCase() + "), button:contains(" + wCancel.toUpperCase() + ")").each(function (key, value) { if ($(value).attr("btn-type") == null || $(value).attr("btn-type").toLowerCase() == "redirect") { RestoreFormOriginalValue($(value).closest('form')); } else { $(value).focus(); $(value).click();//trigger("click"); } }); mContinuewithTabClickEvent = true; if (OriginalClickedTab != null) { OriginalClickedTab.focus(); OriginalClickedTab.click(); OriginalClickedTab = null; } } finally { $(LinkHasChangesSelecter).on("click", URLLinkClickEvent); } //ev.resume(); //ActiveTab.find('> a')[0].parent.trigger(e); $(this).dialog("close"); //e.continueEventPropegationAsIfNothingHappened(); }, }, { text: wCancel, click: function () { //e.stopImmediatePropagation(); $(this).dialog("close"); } } ] , close: function () { mContinuewithTabClickEvent = false; } }); PrompDialog.dialog("open"); } } function UpdateOriginalValue(wSavingForm) { if (wSavingForm.indexOf("#") < 0) { wSavingForm = "#" + wSavingForm; } var wSavingFormJqueryID = wSavingForm; $(wSavingForm).find(':input').each(function () { if ($(this)[0].name != "multiselect") { var wOrignialValue = $(this).attr("OriginalValue"); if (wOrignialValue == null) { wOrignialValue = $(this).attr("originalvalue"); } var wNewUpdatedValue = ""; if (wOrignialValue != null) { switch ($(this)[0].nodeName) { case "TEXTAREA": case "INPUT": switch ($(this).attr("type")) { case "hidden": break; case "radio": wNewUpdatedValue = ($(this)[0].checked ? $(this).val() : GroupedControlCheckedValue($(wSavingFormJqueryID + " input[name='" + $(this)[0].name + "']"))); break; case "checkbox": wNewUpdatedValue = $(this)[0].checked; break; default: wNewUpdatedValue = $(this).val(); break; } break; case "SELECT": wMultiSelect = ($(this).hasClass("multiselect") ? true : false); wOrignialSelected = wOrignialValue.split("^"); if ($(this)[0].name.indexOf('FilterCriteria') == 0 && wOrignialValue.indexOf('^') < 0) { wOrignialSelected = wOrignialValue.split(","); } for (s = 0; s < $(this)[0].length; ++s) { if ($(this)[0].options[s].selected) { wNewUpdatedValue += $(this)[0].options[s].value if (wMultiSelect) { wNewUpdatedValue += "^"; } else { break; } } } break; } $(this).attr("originalvalue", wNewUpdatedValue) } else { switch ($(this)[0].nodeName) { case "TEXTAREA": case "INPUT": switch ($(this).attr("type")) { case "hidden": break; case "radio": case "checkbox": if (!$(this).eq(0).is(':checked')) { $(this).eq(0).attr('checked', false); } else { $(this).eq(0).attr('checked', true); } break; default: $(this).attr("originalvalue", $(this).val()) break; } break; case "SELECT": wMultiSelect = ($(this).hasClass("multiselect") ? true : false); for (s = 0; s < $(this)[0].length; ++s) { if ($(this)[0].options[s].selected) { wNewUpdatedValue += $(this)[0].options[s].value if (wMultiSelect) { wNewUpdatedValue += "^"; } else { break; } } } $(this).attr("originalvalue", wNewUpdatedValue) break; } } } }); } function GroupedControlCheckedValue(aGroupedControls) { var OriginalValue = ""; aGroupedControls.each(function () { if ($(this)[0].checked) { OriginalValue = $(this).val().toString(); return; } }); return OriginalValue; }