var isValid = true;

var Validator = {
    handlers: $A(new Array()),
    first: function() { },
    last: function() { },
    add: function(target, callback, ev) {
        if (target) target = $(target);
        if (callback) {
            var i = target
				? { cb: callback, e: target, m: null, h: function() {
				    var r = this.cb(this.e), t = this.e.findParent("p");
				    if (this.m) { this.m.remove(); this.m = null; t.removeClassName("hasError"); this.e.removeClassName("hasError"); }
				    if ((this.e.errorMessage = r)) {
				        isValid = false;
				        t.parentNode.insertBefore(this.m = Validator.createMessage(r), t.nextSibling);
				        t.addClassName("hasError"); this.e.addClassName("hasError");
				    } return r;
				}
				}
				: { h: callback };
            this.handlers.push(i); i.h(target);
        }
        if (target)
            Event.observe(target, ev || target.changeEvent(), this.runThis(i).bind(this), false);
    },
    runThis: function(i) {
        return (i ? function() {
            this.first();
            i.h(); this.handlers.each(function(h) { if (h != i) h.h() });
            this.last();
        } : this.run).bind(this);
    },
    run: function() {
        this.first();
        this.handlers.each(function(h) { h.h() });
        this.last();
    },
    createMessage: function(m) {
        var p = $(document.createElement("p")); p.className = "error";
        p.appendChild(document.createTextNode(m));
        return p;
    },
    runValidation: function() {
        isValid = true;
        this.first();
        this.handlers.each(function(h) { h.h() });
        this.last();
        if (isValid == false)
            alert('Bitte vervollstaendigen Sie erst alle markierten Felder.');
        return isValid;
    }
};

Object.extend(Number.prototype, {
    toCurrency: function(precision, curr, mul) {
        return (this / (mul || 100)).toFixed(precision || 2).replace(".", ",") + " " + (curr || "\u20ac");
    }
});

Element.addMethods({
    changeEvent: function(e) { return e.type == "checkbox" ? "click" : "change"; },
    setValue: function(e, p, index) {
        (e = $(e.getElementsByTagName("var")[index || 0])).show();
        e.firstChild.data = p;
    },
    setValue2: function(e, p, index) {
        (e = $(e.getElementsByTagName("var")[index || 0])).show();
        e.firstChild.data = p.toFixed(2);
    },
    setPrize: function(e, p, index, mul, precision, curr) {
        // p = parseInt(p);
        e.setValue(p.toCurrency(precision, curr, mul), index);
    },
    findParent: function(e, tagName) {
        tagName = tagName.toUpperCase();
        while (e.parentNode && (!e.tagName || (e.tagName.toUpperCase() != tagName))) e = e.parentNode;
        return $(e);
    },
    addValidator: function(e, callback, name) { Validator.add(e, callback, name); },
    disable: function(e) { e.disabled = true; if (!e.hasClassName("disabled")) e.addClassName("disabled"); },
    enable: function(e) { e.disabled = false; e.removeClassName("disabled"); }
});

function IsEmpty(str)
{
    return ((/^\s*$/).test(str));
}

