/*
Class: FormValidation [Built on MooTools framework: www.mootools.net]
Author: Martin Jezek 2009
Version: 1.2
*/
var FormValidation = new Class({
    Implements: Options,
    options: {
        default_border: "1px solid #444444",
        error_border: "1px solid #ff0000",
        error_background: "#ff0000",
        error_message: "Musíte vyplnit všechny povinné údaje označené hvězdičkou!!",
        error_mail_message: "Zadejte E-mail ve správném tvaru!! [např.: jmeno@domena.cz]",
        error_phone_message: "Zadejte Telefoní číslo ve správném tvaru!! [např.: +420 789 123 456]",
        error_zipcode_message: "Zadejte PSČ ve správném tvaru!! [např.: 512 03]",
        error_mail_or_phone_message: "Zadejte E-mail nebo Telefoní číslo!!"
    },
    initialize: function (options) {
        this.setOptions(options);
        this.validateForms();
    },
    validate_error: false,
    validate_mail_error: false,
    validate_phone_error: false,
    validate_zipcode_error: false,
    validate_mail_or_error: false,
    validate_phone_or_error: false,
    regular_mail: /\S+@\S+\.\w{2,4}$/,
    regular_phone: /^(\+42[0-9]\s*)?[1-9](\s*\d\s*){8}$/,
    regular_zipcode: /^[0-9]{3} ?[0-9]{2}$/,
    result_message: "",
    validateForms: function () {
        $$("form.form-validation").each(function (form) {
		
			// problem s pridavanim eventu ...
            //form.addEvent("submit", this.validateInputs.bind(this, form))
			
			var that = this;
			
			form.onsubmit = function() {
				return that.validateInputs(form);
			}
			
        }, this);
    },
    validateInputs: function (form) {
        this.result_message = "";
        this.validate_error = false;
        this.validate_mail_error = false;
        this.validate_phone_error = false;
        this.validate_zipcode_error = false;
        this.validate_mail_or_error = false;
        this.validate_phone_or_error = false;
        form.getElements("input").each(function (input) {
            if (input.hasClass("check-is-blank")) {
                input.setStyle("border", this.options.default_border);
                if (input.value.trim() == "") {
                    input.setStyle("border", this.options.error_border);
                    this.validate_error = true;
                }
            }
            if (input.hasClass("check-is-checked")) {
                input.setStyle("border", this.options.default_border);
                if (input.checked != true) {
                    input.setStyle("border", this.options.error_border);
                    this.validate_error = true;
                }
            }
            if (input.hasClass("check-is-mail")) {
                input.setStyle("border", this.options.default_border);
                input.value = input.value.replace(/ /g, "");
                if (!(this.regular_mail.test(input.value))) {
                    input.setStyle("border", this.options.error_border);
                    this.validate_mail_error = true;
                }
            }
            if (input.hasClass("check-is-phone")) {
                input.setStyle("border", this.options.default_border);
                input.value = input.value.replace(/ /g, "");
                if (!(this.regular_phone.test(input.value))) {
                    input.setStyle("border", this.options.error_border);
                    this.validate_phone_error = true;
                }
            }
            if (input.hasClass("check-is-zipcode")) {
                input.setStyle("border", this.options.default_border);
                input.value = input.value.replace(/ /g, "");
                if (!(this.regular_zipcode.test(input.value))) {
                    input.setStyle("border", this.options.error_border);
                    this.validate_zipcode_error = true;
                }
            }
            if (input.hasClass("check-is-mail-or")) {
                input.setStyle("border", this.options.default_border);
                input.value = input.value.replace(/ /g, "");
                if (!(this.regular_mail.test(input.value))) {
                    input.setStyle("border", this.options.error_border);
                    this.validate_mail_or_error = true;
                }
                form.getElements("input").each(function (inputz) {
                    if (inputz.hasClass("check-is-phone-or")) {
                        inputz.setStyle("border", this.options.default_border);
                        inputz.value = inputz.value.replace(/ /g, "");
                        if (!((this.validate_mail_or_error == true) && (inputz.value == ""))) {
                            if (inputz.value != "") {
                                if (!(this.regular_phone.test(inputz.value))) {
                                    this.validate_phone_or_error = true;
                                    inputz.setStyle("border", this.options.error_border);
                                }
                            }
                        }
                        if ((this.validate_mail_or_error == true) && (input.value == "")) {
                            if (!(this.regular_phone.test(inputz.value))) {
                                if (input.value == "" && inputz.value != "") {
                                    this.validate_mail_or_error = false;
                                    input.setStyle("border", this.options.default_border);
                                }
                                this.validate_phone_or_error = true;
                                inputz.setStyle("border", this.options.error_border);
                            } else {
                                input.setStyle("border", this.options.default_border);
                                this.validate_mail_or_error = false;
                            }
                        }
                    }
                }, this);
            }
        }, this);
        form.getElements("select").each(function (select) {
            if (select.hasClass("check-is-selected-first")) {
                select.setStyle("background", "#ffffff");
                if (select.options[0].selected == true) {
                    select.setStyle("background", this.options.error_background);
                    this.validate_error = true;
                }
            }
        }, this);
        form.getElements("textarea").each(function (textarea) {
            if (textarea.hasClass("check-is-blank")) {
                textarea.setStyle("border", this.options.default_border);
                if (textarea.value == "") {
                    textarea.setStyle("border", this.options.error_border);
                    this.validate_error = true;
                }
            }
        }, this);
        if (this.validate_mail_or_error == true && this.validate_phone_or_error == true) {
            this.result_message += this.options.error_mail_or_phone_message + " \n";
        } else if (this.validate_mail_or_error == true && this.validate_phone_or_error == false) {
            this.result_message += this.options.error_mail_message + " \n";
        } else if (this.validate_mail_or_error == false && this.validate_phone_or_error == true) {
            this.result_message += this.options.error_phone_message + " \n";
        }
        if (this.validate_zipcode_error) this.result_message += this.options.error_zipcode_message + " \n";
        if (this.validate_phone_error) this.result_message += this.options.error_phone_message + " \n";
        if (this.validate_mail_error) this.result_message += this.options.error_mail_message + " \n";
        if (this.validate_error) this.result_message += this.options.error_message + " \n";
        if (this.result_message == "") {
            return true;
        } else {
            alert(this.result_message);
            return false;
        }
    }
});
