var labelStyleNormal = { className: "normal"}; var labelStyleMarked = { className: "red"}; var formular = { obj: null, element: null, init: function(id){ this.element = new Array(); this.obj = d_obj(id); }, addElement: function(id, type, mandatory){ var elm = new formElement(id, type, mandatory); if (elm) this.element[this.element.length] = elm; }, validateForm: function(){ var valid = true; for(var i=0; i < this.element.length; i++){ if (this.element[i].checkElement() == false) valid = false; } //alert(valid); return valid; } } //formular function formElement(id, type, mandatory){ this.id = id; this.type = type; if (this.type != "radio") this.obj = d_obj(id); else { this.obj = true; } this.mandatory = mandatory; if ((this.type == "text") || (this.type == "string") || (this.type == "textarea") || (this.type == "email") || (this.type == "select")) this.mark_obj = this.obj; else this.mark_obj = d_obj("mark_" + this.id) ? d_obj("mark_" + this.id) : null; this.mark_class = this.mark_obj.className; this.checkElement = function(){ var valid = true; if (this.mandatory == false) return true; else { switch(this.type){ case "string" : case "text" : valid = this.checkTextElement(); break; case "radio": valid = this.checkRadioElement(); break; case "email": valid = this.checkEmailElement(); break; case "checkbox": valid = this.checkCheckboxElement(); break; case "textarea": valid = this.checkTextareaElement(); break; case "select": valid = this.checkSelectElement(); break; default: valid = false; break; } return valid; } }; this.checkTextElement = function(){ if (this.obj.value != "") { this.clearLabelElement(); return true; } else { this.markLabelElement(); return false; } }; this.checkRadioElement = function(){ var valid = false; for (var i=0; i< formular.obj.elements.length; i++){ if ( formular.obj.elements[i].name == this.id) { if (formular.obj.elements[i].checked == true) { this.clearLabelElement(); valid = true; } } } if (valid == false) this.markLabelElement(); return valid; }; this.checkEmailElement = function(){ if (!(this.obj.value.search(/[@.]/) >= 0) || !(this.obj.value.search(/.+@..+\...+/) >= 0) || !(this.obj.value.length >= 8)) { this.markLabelElement(); return false; } else { this.clearLabelElement(); return true; } }; this.checkCheckboxElement = function(){ if (this.obj.checked != false) { this.clearLabelElement(); return true; } else { this.markLabelElement(); return false; } }; this.checkTextareaElement = function(){ if (this.obj.value != "") { this.clearLabelElement(); return true; } else { this.markLabelElement(); return false; } }; this.checkSelectElement = function(){ if (this.obj.value != "") { this.clearLabelElement(); return true; } else { this.markLabelElement(); return false; } }; this.markLabelElement = function(){ if (this.mark_obj) { if (this.mark_class == "") this.mark_obj.className = labelStyleMarked.className; else this.mark_obj.className = this.mark_class + " " + labelStyleMarked.className; } }; this.clearLabelElement = function(){ if (this.mark_obj) { if (this.mark_class == "") this.mark_obj.className = labelStyleNormal.className; else this.mark_obj.className = this.mark_class + " " + labelStyleNormal.className; } }; if (this.obj) return this; else return false; } function validateForm(){ return formular.validateForm(); }