/* */ /** * jednoducha validace vsech formularu * */ var FormValidator = function() { this.validateClass = "jfv"; this.validators = [ {"elemClass": "jfv_str", "validator": this.validateNotEmpty}, {"elemClass": "jfv_int", "validator": this.validateInteger}, {"elemClass": "jfv_num", "validator": this.validateNumber}, {"elemClass": "jfv_email", "validator": this.validateEmail}, {"elemClass": "jfv_phone", "validator": this.validatePhone}, ]; this.classMandatory = "jfv_man"; var self = this; fLib.addEvent("load", function() { self.findInputs() }, window); } FormValidator.prototype.findInputs = function() { var elems = fLib.getElementsByClassNamePreg(/jfv_[\w]+/i); var self = this; for(var i = 0; i < elems.length; i++) { fLib.addEvent("blur", function() { self.checkElement(this); }, elems[i]); } } /** * validace jednotliveho elementu * */ FormValidator.prototype.checkElement = function(input) { var ret = true; var inputChecked = false; for(i in this.validators) { if(input.className.indexOf(this.validators[i].elemClass) != -1) { ret = this.validators[i].validator(input.value) && ret; inputChecked = true; } } var valret = true; if(inputChecked && (input.className.indexOf(this.classMandatory) != -1 || input.value != "")) { if(ret) { input.style.backgroundImage = "url(/images/done.png)"; } else { input.style.backgroundImage = "url(/images/incomplete.png)"; valret = false; } input.style.backgroundPosition = "right center"; input.style.backgroundRepeat = "no-repeat"; } else { //input.style.backgroundImage = "none"; } return valret; } FormValidator.prototype.validateNotEmpty = function(val) { return val != ""; } FormValidator.prototype.validateInteger = function(val) { return val == val.match(/[0-9]+/i); } FormValidator.prototype.validateEmail = function(val) { return val == val.match(/[a-z0-9._-]+@[a-z0-9.]+\.[a-z]+/i); } FormValidator.prototype.validatePhone = function(val) { return val == val.match(/[+0-9 ]+/i); } FormValidator.prototype.validateNumber = function(val) { return val == val.match(/[+0-9\.]+/i); } /** * kompletni validace celeho formulare * */ FormValidator.prototype.validate = function(form) { var elems = form.getElementsByTagName("*"); var i; var ret = true; for(i in elems) { if(elems[i].name != undefined && elems[i].className != undefined) { ret = this.checkElement(elems[i]) && ret; } } return ret; } //------------------------------------------------------------------------------ /** * formularovy pomocnik * */ var FormsHelper = function () { this.form = null; var self = this; fLib.addEvent("load", function() { self.findForms() }, window); } /** * po odeslani formulare zablokovat submit button * */ FormsHelper.prototype.findForms = function() { var elems = document.getElementsByTagName("form"); for(var i = 0; i < elems.length; i++) { if(elems[i].onsubmit == undefined) { //kvuli vraceni t/f musi byt jen jeden handler var self = this; elems[i].onsubmit = function() { return self.doSubmit(this); } } } } FormsHelper.prototype.doSubmit = function(form) { if(form.className.indexOf(formValidator.validateClass) != -1) { if(!formValidator.validate(form)) { dialogs.alert("Pro odeslání je nutné vyplnit všechny povinné údaje."); return false; } } //this.setElemDisabledState(form, "disabled"); return true; } FormsHelper.prototype.setElemDisabledState = function(frm, val) { var elems = frm.getElementsByTagName("input"); for(var i = 0; i < elems.length; i++) { if(elems[i].type == "submit" || elems[i].type == "button") { elems[i].disabled = val; } } } //------------------------------------------------------------------------------ FormsHelper.prototype.confirm = function(aForm) { this.form = aForm; var self = this; dialogs.confirmForm("Potvrzujete akci?", function() { self.submitForm(); }); } FormsHelper.prototype.submitForm = function() { if(this.form) { this.form.submit(); } } //------------------------------------------------------------------------------ /** * prehodi vsechny checkboxy v ramci daneho elementu * */ FormsHelper.prototype.invertCheckboxes = function(parentID) { var elem = fLib.getElementById(parentID); if(elem != null) { var elems = elem.getElementsByTagName("input"); for(var i = 0; i < elems.length; i++) { if(elems[i].type == "checkbox") { elems[i].checked = !elems[i].checked; } } } } //------------------------------------------------------------------------------ /** * zvyrazneni spatne vyplnenych inputu * */ var FormShowError = function(formName) { this.formName = formName; this.inputs = new Array(); var self = this; fLib.addEvent("load", function() { self.alertInputs(); }, window); } FormShowError.prototype.addInput = function(inpName) { this.inputs.push(inpName); } FormShowError.prototype.alertInputs = function() { var form = document.forms[this.formName]; if(form) { var i; for(i in this.inputs) { if(form[this.inputs[i]]) { fLib.addClassName(form[this.inputs[i]], "error"); } } } } var formValidator = new FormValidator(); var formsHelper = new FormsHelper();