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) { 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); }, 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; } } 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 id = $(form).attr('id'); if (!_.isArray(response.formErrors)) { response.formErrors = []; } if (!_.isArray(response.elementsErrors)) { response.elementsErrors = []; } Qs_Form.setFormOption(id, 'response', response); 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.displayFromErrorsHtml(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); } } } } } }, 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); }, displayFromErrorsHtml: function (form, errors) { var html = ''; for (var i in errors) { html += '