var invalidValidations = [];

var MyValidation = Class.create(LiveValidation, {
    initialize: function($super, element, optionsObj){
        $super(element, Object.extend(optionsObj, {
            validMessage: "",
            onlyOnBlur: true,
            onInvalid: function(){
                invalidValidations.push(this);
                
                this.addFieldClass();
                
                if(!Object.isUndefined(this.msgBox.forElement) && this.msgBox.forElement != this.element){
                    return;
                }
                this.msgBox.appear({
                    duration: 0.3
                });

                this.msgBoxText.innerHTML = this.message;
                this.msgBox.forElement = this.element;
                
                this.positionMsgBox();
            },
            positionMsgBox: function(){
                this.boxHeight = this.msgBox.getDimensions().height;
                /*this.msgBox.clonePosition(this.element, {
                    offsetLeft: -15,
                    offsetTop: -this.boxHeight-1
                });*/
                var parentViewportOffset = this.element.getOffsetParent().viewportOffset();
                new Effect.Move(this.msgBox, {
                    x: this.element.positionedOffset().left - 15,
                    y: this.element.positionedOffset().top - this.boxHeight + 2,
                    mode: "absolute",
                    duration: 0.3,
                    afterFinish: this.checkForRepositioning.bind(this)
                });
            },
            checkForRepositioning: function(){
                if(this.boxHeight != this.msgBox.getDimensions().height){
                    //fix position
                    this.positionMsgBox();
                }
            },
            onValid: function(){
                invalidValidations = invalidValidations.without(this);
                
                if(this.msgBox.forElement == this.element){
                    this.msgBox.forElement = undefined;
                }
                this.addFieldClass();

                /*
                 * check for other validation errors in other fields (but not this one).
                 */
                invalidValidations.each(function(v){
                    if(!v.validate()){
                        throw $break;
                    }
                });

                if(invalidValidations.length == 0){
                    this.hideMsgBox();
                }
            },
            doOnFocus: function(){
                this.focused = true;
                this.wasEverFocused = true;
            }
        }));
        
        this.msgBox = $("validation_box");
        Event.observe(this.msgBox, "click", this.hideMsgBox.bind(this));
        this.msgBoxText = $("validation_box_text");
        this.form.onsubmit = this.onSubmit.bindAsEventListener(this);
    },

    hideMsgBox: function(){
        this.msgBox.fade({
            duration: 0.3
        });
    },

    onSubmit: function(event){
        if(!this.alreadyTriedToSubmit && !LiveValidation.massValidate(this.formObj.fields)){
            this.alreadyTriedToSubmit = true;
            window.alert("Wir benötigen mehr Informationen um Ihr Angebot erstellen können. \nBitte überprüfen Sie Ihre Eingaben, bevor Sie Ihre Anfrage senden.");
            //don't submit form
            Event.stop(event);
            //scroll to first error
            new Effect.ScrollTo(this.msgBox);
        }
    }
});

function makeRequired(fieldName, msg, applyDefaultMessage){
    var field = $(fieldName);
    Element.addClassName(field, "required");
    Element.addClassName($("label_"+fieldName), "required");

    var v = new MyValidation(fieldName, {});
    v.add(Validate.Presence, {
        failureMessage: applyDefaultMessage ? "<b>Bitte geben Sie "+msg+" ein.</b>" : msg
    });
    return v;
}

Validate.Date = function (value, paramsObj){
    var params = Object.extend({
        failureMessage: "<b>Bitte geben Sie ein gültiges Datum ein.</b>"
    }, paramsObj || {});
    if(this.element.prompt == value || "" == value){
        return true;
    }
    var date = Date.parse(value);
    if(date == null){
        Validate.fail(params.failureMessage);
    }

    this.element.value = date.toString("dd.MM.yyyy");
    
    if(params.notBefore != null && date.compareTo(params.notBefore) < 0){
        Validate.fail(params.notBeforeMsg);
    }
    
    return true;
}
