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:', 'pleaseCorrectTheseFields': '' } }), options: { errorDisplayMethod: '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(); } $(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; } } $('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]; }, setErrorDisplayMethod: function (idForm, value) { Qs_Form.setFormOption(idForm, 'errorDisplayMethod', value); }, getErrorDisplayMethod: function (idForm) { return Qs_Form.getFormOption(idForm, 'errorDisplayMethod'); }, 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]; }, callExternal: function (spec) { if (typeof spec == 'function') { spec.call(); } else if (typeof spec == 'string') { $.globalEval(spec + '.call(this)'); } else if (typeof spec == 'object' && spec != null) { var scriptHtml = ''; for (var callback in spec) { var params = new Array(); if (is_numeric(callback)) { if (typeof spec[callback] == 'string') { callback = spec[callback] } else if (typeof spec[callback] == 'object' && typeof spec[callback] != null ) { var key = array_key(spec[callback]); params = spec[callback][key] callback = key; } else { continue; } } else { params = spec[callback]; } scriptHtml += callback; if (params.length) { scriptHtml += '.apply(this, ' + json_encode(params) + ');\n'; } else { scriptHtml += '.call(this);\n'; } } if (scriptHtml.length) { $.globalEval(scriptHtml); } } }, onSubmitSuccess: function (response, form) { if (qs.processAjaxResponse(response)) { return true; } var id = $(form).attr('id'); Qs_Form.setFormOption(id, 'response', response); if (response.isValid) { if (form) { var onSuccessCallback = Qs_Form.getFormOption($(form).attr('id'), 'onSuccessCallback'); if (typeof onSuccessCallback == 'undefined') { form.submit(); } else { Qs_Form.callExternal(onSuccessCallback); } } } else { var onErrorCallback = Qs_Form.getFormOption($(form).attr('id'), 'onErrorCallback'); if (typeof onErrorCallback != 'undefined') { Qs_Form.callExternal(onErrorCallback); return true; } Qs_Form.displayErrors(id); } return response; }, displayErrors: function (id) { var response = Qs_Form.getFormOption(id, 'response'); var form = document.getElementById(id); var displayMethod = Qs_Form.getErrorDisplayMethod(id); switch (displayMethod) { case 'ALERT': var message; if (typeof response.customErrorMessage == 'string') { message = response.customErrorMessage; } else { message = Qs_Form.message.get('alertInvalidInformationEntered') + "\n"; message += Qs_Form.prepareErrorsAlert(response.errors, response.elements); message += '\n\n' + Qs_Form.message.get('pleaseCorrectTheseFields'); } var name = array_key(response.errors); if (form[name] && form[name].focus) { form[name].focus(); } else if (form[name + '[input]'] && form[name + '[input]'].focus) { // встановлення фокуса для каптчі form[name + '[input]'].focus(); } if ($('#' + name + '-label').size()) { $.scrollTo('#' + name + '-label'); } alert(message); break; case 'HTML': Qs_Form.displayErrorsHtml(form, response.errors); var name = array_key(response.errors); var focusKey = null; if (form[name] && form[name].focus) { focusKey = name; } else if (form[name + '[input]'] && form[name + '[input]'].focus) { // встановлення фокуса для каптчі focusKey = name + '[input]'; } if (focusKey != null) { if ($('[name="' + focusKey + '"]:visible', form).size()) { form[focusKey].focus(); } } var formPrependId = Qs_Form.getFormOption(id, 'prependId'); if (formPrependId) { name = id + '-' + name; } if ($('#' + name + '-label').size()) { $.scrollTo('#' + name + '-label'); } Qs_Form.displayErrorsAlert(); break; default: alert('Qs_Form. Unknown errorDisplayMethod "' + displayMethod + '"'); } if (typeof response.captcha == 'object' && response.captcha != null) { for (var name in response.captcha) { var captcha = $('#' + name + '-element'); if ($(captcha).size() != 0) { $('img:first', captcha).attr('src', response.captcha[name].src); $('#' + name + '-id', captcha).attr('value', response.captcha[name].id); $('#' + name + '-input', captcha).attr('value', ''); if (typeof response.captcha[name].href != 'undefined') { $('a.captcha_refresh', captcha).attr('href', response.captcha[name].href); } } } } }, displayErrorsAlert: function () { var message = Qs_Form.message.get('invalidInformationEntered') + '\n' + Qs_Form.message.get('pleaseCorrectTheseFields'); alert(trim(message)); }, prepareErrorsAlert: function (errors, titles) { var messages = ''; for (var element in errors) { var message = ''; var isTitle = true; for (var errorIndex in errors[element]) { if (typeof errors[element][errorIndex] == 'string') { message += '\n - ' + errors[element][errorIndex]; } else { messages += Qs_Form.prepareErrorsAlert(errors[element], titles[element]); isTitle = false; break; } } if (isTitle && message != '') { messages += '\n' + titles[element] + ':' + message; } } return messages; }, displayErrorsHtml: function (form, errors, belongsTo) { for (var element in errors) { var html = ''; var elementName; if (typeof belongsTo != 'undefined') { elementName = belongsTo + '[' + element + ']'; } else { elementName = element; } for (var errorIndex in errors[element]) { if (typeof errors[element][errorIndex] == 'string') { html += '