(function () {
    function MissingValueException(element, text) {
        this.element = element;
        this.text    = text;
    }

    var Contact = {
        checkField: function (id, text) {
                        var element = document.getElementById(id);
                        if (element && element.value === "") {
                            throw new MissingValueException(element, text);
                        }
                    },
		checkMail: function (id, text) {
						//var validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
						var validRegExp = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
						var element = document.getElementById(id);
						var strEmail = element.value;
						if (strEmail.search(validRegExp) == -1) {
							//alert('Du måste ange en riktig e-mail adress.');
							//return false;
                            throw new MissingValueException(element, text);
						} 
					},
        validate:   function (e) {
                        try {
                            Contact.checkField("contact-name", "ditt namn");
                            Contact.checkField("contact-email", "din E-mail adress");
                            Contact.checkMail("contact-email", "en riktig E-mail adress");
                            Contact.checkField("contact-subject", "ditt ärende");
                            Contact.checkField("msg-text", "det meddelande du vill skicka");
                        } catch (ex) {
                            alert("Var god ange " + ex.text + ".");
                            ex.element.focus();
                            return false;
                        }
                        return true;
                    }
    };

    if (document.getElementById) {
        var form = document.getElementById("contact-form");
        if (form) {
            form.onsubmit = Contact.validate;
        }
    }
})();
