var Qs_Form = { message: new qs.Message({ 'eng': { 'invalidInformationEntered': 'Please review the comments marked in red and make appropriate corrections.', 'alertInvalidInformationEntered': 'Please make the following corrections:', 'alertFromErrors': '', 'pleaseCorrectTheseFields': '' } }), options: { elementsErrorsDisplayMethod: 'HTML', formErrorsDisplayMethod: 'HTML', errorAlertEnabled: true }, formOptions: {}, init: function (idForm, options) { if (true == options.disabled) { Qs_Form.disable(idForm); return true; } var form = document.getElementById(idForm); if (!form) { alert('Form (id = ' + idForm + ') is not available'); return false; } if (!form.tagName || form.tagName != 'FORM') { alert('Element (id = ' + idForm + ') is not a form - tagName = ' + form.tagName); return false; } if (options) { Qs_Form.formOptions[idForm] = options; } if (true == Qs_Form.getFormOption(idForm, 'errorAlertEnabled') && true == Qs_Form.getFormOption(idForm, 'isErrors') ) { Qs_Form.displayErrorsAlert(); Qs_Form.setErrorFocus(form); } $(form).unbind('submit.form-plugin').bind('submit.form-plugin', Qs_Form.onSubmit); $(':submit,input:image', form).unbind('click.form-plugin').bind('click.form-plugin', Qs_Form.buttonOnClick); return true; }, disable: function (idForm) { var form = document.getElementById(idForm); var limit = form.elements.length; for (i = 0; i < limit; i++) { if ('TEXTAREA' == form.elements[i].tagName) { var editor = false; if (typeof Qs_CkEditor != 'undefined') { editor = Qs_CkEditor.getElementInstance(form.elements[i].id); } if (false != editor) { editor.disable(); } else { form.elements[i].disabled = true; } } form.elements[i].disabled = true; } }, buttonOnClick: function (e) { var form = this.form; form.clk = this; if (this.type == 'image') { if (e.offsetX != undefined) { form.clk_x = e.offsetX; form.clk_y = e.offsetY; } else if (typeof $.fn.offset == 'function') { /* try to use dimensions plugin */ var offset = $(this).offset(); form.clk_x = e.pageX - offset.left; form.clk_y = e.pageY - offset.top; } else { form.clk_x = e.pageX - this.offsetLeft; form.clk_y = e.pageY - this.offsetTop; } } $('input.hidden-btn-value', form).remove(); $(e.target).after($('').attr('name', e.target.name).val(e.target.value)); setTimeout(function() { form.clk = form.clk_x = form.clk_y = null; }, 10); }, getOption: function (name) { return Qs_Form.options[name]; }, setElementsErrorsDisplayMethod: function (idForm, value) { Qs_Form.setFormOption(idForm, 'elementsErrorsDisplayMethod', value); }, getElementsErrorsDisplayMethod: function (idForm) { return Qs_Form.getFormOption(idForm, 'elementsErrorsDisplayMethod'); }, setFormErrorsDisplayMethod: function (idForm, value) { Qs_Form.setFormOption(idForm, 'formErrorsDisplayMethod', value); }, getFormErrorsDisplayMethod: function (idForm) { return Qs_Form.getFormOption(idForm, 'formErrorsDisplayMethod'); }, setFormOption: function (idForm, name, value) { if (typeof Qs_Form.formOptions[idForm] == 'undefined') { Qs_Form.formOptions[idForm] = {}; } Qs_Form.formOptions[idForm][name] = value; }, getFormOption: function (idForm, name) { if (typeof Qs_Form.formOptions[idForm] == 'undefined' || typeof Qs_Form.formOptions[idForm][name] == 'undefined' ) { return Qs_Form.getOption(name); } return Qs_Form.formOptions[idForm][name]; }, onSubmitSuccess: function (response, form) { if (response.sessionExpired) { if (response.loginUrl) { qs.openPopupByLocation(response.loginUrl, 'qsPopup', 900, 400); } return true; } var $form = $(form); var id = $form.attr('id'); if (!_.isArray(response.formErrors)) { response.formErrors = []; } if (!_.isArray(response.elementsErrors)) { response.elementsErrors = []; } Qs_Form.setFormOption(id, 'response', response); $form.trigger({ type: 'qsFormSubmitSuccess', response: response }); //Qs_Form.setFormOption(id, 'requiredFiles', response.requiredFiles); -- reverted rev 4201 (task 21122) if (response.isLocked) { alert(response.error); } else if (response.isValid && Qs_Form.isValidFiles(id)) { if (form) { var onSuccessCallback = Qs_Form.getFormOption($(form).attr('id'), 'onSuccessCallback'); if (typeof onSuccessCallback == 'undefined') { form.submit(); } else { qs.call(onSuccessCallback); } } } else { var onErrorCallback = Qs_Form.getFormOption($(form).attr('id'), 'onErrorCallback'); if (typeof onErrorCallback != 'undefined') { qs.call(onErrorCallback); return true; } Qs_Form.displayErrors(id); } return response; }, displayErrors: function (id) { var response = Qs_Form.getFormOption(id, 'response'); if (!response.formErrors.length || response.elementsErrors.length) { response = Qs_Form.validateFiles(id, response); } var displayErrorsAlert = false; var form = document.getElementById(id); var elementsDisplayMethod = Qs_Form.getElementsErrorsDisplayMethod(id); var name, message, focusKey; if (response.formErrors.length) { var formDisplayMethod = Qs_Form.getFormErrorsDisplayMethod(id); switch (formDisplayMethod) { case 'ALERT': Qs_Form.displayFromErrorsAlert(form, response.formErrors); break; case 'HTML': Qs_Form.displayErrorsHtml(form, response.formErrors); displayErrorsAlert = true; break; default: alert('Qs_Form. Unknown formErrorsDisplayMethod "' + formDisplayMethod + '"'); } } if (response.elementsErrors.length) { switch (elementsDisplayMethod) { case 'ALERT': message = Qs_Form.message.get('alertInvalidInformationEntered') + "\n"; message += Qs_Form.prepareErrorsAlert(response.elementsErrors); message += '\n\n' + Qs_Form.message.get('pleaseCorrectTheseFields'); alert(message); break; case 'HTML': Qs_Form.displayErrorsHtml(form, response.elementsErrors); displayErrorsAlert = true; break; default: alert('Qs_Form. Unknown elementsErrorsDisplayMethod "' + elementsDisplayMethod + '"'); } } if (displayErrorsAlert) { Qs_Form.displayErrorsAlert(); Qs_Form.setErrorFocus(form); } if (response.elementsErrors.length) { for (var i in response.elementsErrors) { if (response.elementsErrors[i].type == 'captcha') { var captchaElement = $('#' + response.elementsErrors[i].id + '-element'); if ($(captchaElement).size() != 0) { var captchaData = response.elementsErrors[i].captcha; var captchaId = response.elementsErrors[i].id; $('img:first', captchaElement).attr('src', captchaData.src); $('#' + captchaId + '-hidden', captchaElement).attr('value', captchaData.id); $('#' + captchaId + '-input', captchaElement).attr('value', ''); $('#' + captchaId + '-input', captchaElement).blur(); if (typeof captchaData.href != 'undefined') { $('a.captcha_refresh', captchaElement).attr('href', captchaData.href); } } } } } if (response.elementsErrors.length) { $(form).trigger({ type: 'qsFormAfterSubmitSuccess', response: response }); } }, setErrorFocus: function (form) { var id = $(form).attr('id'); var response = Qs_Form.getFormOption(id, 'response'); var focusKey = null; var scrollToKey = null; if (response.formErrors.length) { scrollToKey = id; } else if (response.elementsErrors.length) { var element = response.elementsErrors[0]; if (0 == $('#' + element.id, form).size()) { // check if it is radio button var radio = $('#' + element.id + '-element input[id^=' + element.id + '][type=radio]', form).first(); if ($(radio).size()) { scrollToKey = element.id + '-label'; } } else { if ($('#' + element.id, form).prop('focus')) { focusKey = element.id; scrollToKey = element.id + '-label'; } } } if (focusKey && $('#' + focusKey + ':visible', form).size()) { $('#' + focusKey, form).focus(); } if (scrollToKey && $('#' + scrollToKey + ':visible').size()) { $.scrollTo('#' + scrollToKey); } }, displayFromErrorsAlert: function (form, errors) { var message = Qs_Form.message.get('alertFromErrors') + "\n"; for (var i in errors) { message += ' - ' + errors[i] + '\n'; } alert(message); }, getFirstName: function (errors, subitem) { var name = _.keys(errors)[0]; var result = name; if (typeof subitem != 'undefined' && subitem == true) { result = '[' + result + ']'; } if (typeof errors[name] == 'object') { result += Qs_Form.getFirstName(errors[name], true); } if (typeof subitem == 'undefined') { var pos = result.lastIndexOf('['); if (pos > 0) { result = result.substring(0, pos); } } return result; }, displayErrorsAlert: function () { var message = Qs_Form.message.get('invalidInformationEntered') + '\n' + Qs_Form.message.get('pleaseCorrectTheseFields'); alert(_.str.trim(message)); }, prepareErrorsAlert: function (elementsErrors) { var messages = ''; for (var elementIndex in elementsErrors) { var message = ''; for (var errorIndex in elementsErrors[elementIndex].errors) { message += '\n - ' + elementsErrors[elementIndex].errors[errorIndex]; } if (message != '') { messages += '\n' + elementsErrors[elementIndex].label + ':' + message; } } return messages; }, displayErrorsHtml: function (form, elementsErrors) { for (var elementIndex in elementsErrors) { this.displayElementErrors(elementsErrors[elementIndex]); } }, displayElementErrors: function (element) { var html = ''; for (var errorIndex in element.errors) { html += '