var Contact = new function () {

    this.init = function() {
    };

    this.inputFocus = function(e) {
        var val = $(this).val();
        var rel = $(this).attr("title");

        if (e.type == "focus") {
            $(this).removeClass("error").addClass("active");

            if (val == rel)
                $(this).val("");
        }
        else {
            if (val === "")
                $(this).val(rel).removeClass("active");
        }
    };
    
	this.submitForm = function() {
        var form = $(this);
		var department = $("select#cDepartment");
		var message = $("textarea#cMessage");
		var name = $("input#cName");
		
        form.find("input[type=\"submit\"]").attr("disabled", true);

		var emailEl = $("input#cEmail");
		var phoneEl = $("input#cPhone");
		var emailVal = (emailEl.val() == emailEl.attr("title")) ? "" : emailEl.val();
		var phoneVal = (phoneEl.val() == phoneEl.attr("title")) ? "" : phoneEl.val();

		if (Main.validateEmpty(form)) {
            if ($("select#cDepartment").val() > 0) {
    			if (emailVal !== "" || phoneVal !== "") {
                    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(emailVal)) {
        				$.ajax({
        					type: "POST",
        					url: "/ajax.php?mode=contact",
        					data: "email=" + encodeURIComponent(emailVal) + "&phone=" + encodeURIComponent(phoneVal) + "&department=" + encodeURIComponent(department.val()) + "&message=" + encodeURIComponent(message.val()) + "&name=" + encodeURIComponent(name.val()),
        					beforeSend: function() {
        					},
        					success: function(response) {
        						$("div.contactResponse").show().html(response);
    
        						$.each(form.find(":text, textarea"), function(index, value) {
                                    $(this).val($(this).attr("title")).removeClass("active");
        						});
        					},
        					failure: function() {
        					}
        				});
    				}
    				else {
    				    $("input#cEmail").addClass("error");
    				    $("div.contactResponse").show().html("Ange en korrekt e-postadress");
    				}
    			}
    			else {
    				$("div.contactResponse").show().html("Ange e-postadress eller telefonnummer");
    			}
            }
            else {
                $("div.contactResponse").show().html("Välj en avdelning");
            }
		}
		
		form.find("input[type=\"submit\"]").removeAttr("disabled");
		
		return false;
	};
}