if ( HP == null ) var HP={};
if ( HP.Forms == null ) HP.Forms = {};

//
// Konstanten
//
const C_FRMVAL_EMPTY = "C_EMPTY";
const C_FRMVAL_EMAIL = "C_EMAIL";
const C_FRMVAL_NUMBER = "C_NUMBER";


HP.Forms.Formvalidator = new Class({
    // Implementierungen
    Implements: [Events,Options],

    // Optionen
    options: {
        "Form"          : null,
        "Fields"        : [],
        "ErrorClass"    : "FormError"
    },

    //
    // Constructor
    //              
    initialize: function(options) {
        this.setOptions(options);
        this.ErrorList = [];
        
        window.addEvent("load", this.setup.bindWithEvent(this));
    },


    //
    // Prüft ein Feld
    //
    checkField: function( Field ) {
        if ( Field == null ) return false;
        
        return this.fireEvent("onCheck", Field);
    },
    
    //
    // setzt eine Fehler
    //
    setError: function(Field,ParseAktion,ErrorMsg) {
        this.ErrorList.push({
            Field       : Field,
            ParseAktion : ParseAktion,
            ErrorMsg    : ErrorMsg,
            Type        : Field.nodeName
        });

        Field.addEvent("focus", this.onElementEnter.bindWithEvent(this));        
        if ( !Field.hasClass( this.options.ErrorClass ) ) {
            Field.addClass( this.options.ErrorClass );
        }
    },
    
    //
    // entfernt den Fehler vom Feld
    //
    unsetError: function(Field) {
        if ( Field.hasClass( this.options.ErrorClass ) ) {
            Field.removeClass( this.options.ErrorClass );
        }    
    },
    
    //
    // wird beim Eintretten des Focus in ein Formularfeld ausgelöst
    //
    onElementEnter: function(evt) {
        evt.stop();
        this.unsetError(evt.target);
        evt.target.removeEvents("focus");
    },
    
    //
    // submitted das Formular
    //
    formSubmit: function( ignoreCheck ) {
        if ( ignoreCheck == true || this.submit() ) {
            this.Form.submit();
        } 
    },
    
    //
    // Setupfunktion wird nach dem laden der Seite aufgerufen
    //
    submit: function() {
        var Field = null;
        var Value = null;
        var List = null;         
        var Element = null;
        var Error = false;
        
        for (i=0; i < this.ErrorList.length; i++ ) {
            this.unsetError(this.ErrorList[i].Field);
        }
        
        this.ErrorList = [];
        
        for (i=0; i < this.options.Fields.length; i++) {
            Element = this.options.Fields[i];
            Field = $(Element.Id);
            if ( !Field ) alert("Das Feld '"+Element.Id+"' konnte nicht gefunden werden.");
            Value = null;
            
            if ( typeof(Element.Method) == "string" ) {
            
                //
                // Nodebezeichnung
                //
                switch ( Field.nodeName ) {
                    case "INPUT":
                        if ( Field.type.toUpperCase() == "TEXT" ) Value = Field.value;
                            else if ( Field.type.toUpperCase() == "CHECKBOX" ) Value = Field.checked;
                                else if ( Field.type.toUpperCase() == "RADIO" ) {
                                    for (z=0; z < this.Form[ Field.name ].length; z++) {
                                        if ( this.Form[ Field.name ][z].checked == true ) {
                                            Value = this.Form[ Field.name ][z].value;
                                            break;
                                        }
                                    }                                
                                } 
                    break;
                    
                    case "TEXTAREA":
                        Value = Field.value;
                    break;
                    
                    case "SELECT":
                        Value = this.Form[ Field.Name ].options[ this.Form[ Field.Name ].options.selectedIndex ].value;
                    break;
                }
            
                //
                // Methode zum verfizieren
                //
                switch ( Element.Method ) {
                    // auf Leer prüfen
                    case C_FRMVAL_EMPTY:
                        Error = ( Value == null || Value == "" || Value == false);
                    break;
                    
                    // auf Email prüfen
                    case C_FRMVAL_EMAIL:
                        Error = ( !Value.match(/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}/i) );                    
                    break;
                    
                    // auf Nummer prüfen
                    case C_FRMVAL_NUMBER:
                        Error = ( !Value.match(/^[0-9,.]+$/)  );                    
                    break;
                }
                
                
                var Event = { Field:Field, Handled: false };
                if ( Error ) {
                    this.fireEvent("onError", Event);
                    if ( !Event.Handled ) {
                        this.setError(Field,Element.Method,Element.ErrorMsg);
                    }
                }
                
            } else {
                // Bei funktion diese ausführen
                alert(typeof(Element.Method));            
            }
        }
        
        var Str = "";
        var firstElement = null;
        for (i=0; i < this.ErrorList.length; i++ ) { 
            Str += "- " + this.ErrorList[i].ErrorMsg+"\n";
            if ( firstElement == null && (this.ErrorList[i].Type == "TEXTAREA" || (this.ErrorList[i].Type == "INPUT" && this.ErrorList[i].Field.type.toUpperCase() == "TEXT") || this.ErrorList[i].Type == "SELECT")) {
                firstElement = this.ErrorList[i].Field;
            }  
        }
        if ( Str != "" ) {
            alert("Folgende Fehler sind beim bearbeiten der Formulardaten aufgetretten:\n\n"+Str);
            if ( firstElement != null ) firstElement.focus();
        }
        
        return (this.ErrorList.length == 0);
    },

    //
    // Setupfunktion wird nach dem laden der Seite aufgerufen
    //
    setup : function() {
        this.Form = $(this.options.Form);
        
        if ( this.Form != null ) {
            this.Form.addEvent("submit", this.submit.bindWithEvent(this));
        } else {
            alert("No formular found");
        }     
    }    
});
