
$(function() {
	$("#linkComplaint[rel]").overlay({
		onBeforeLoad: function() {
			this.expose();
		},
		onClose: function(content) {
			$.unexpose();
		},
        closeOnClick: false
	});


	$("#frmComplaint").validate({

	    rules: {
	        denuncia_id_tipus_denuncia:  { required: true},
	        denuncia_nom:                { required: true},
	        denuncia_email:              { required: true}
	    },

	    messages: {
	        denuncia_id_tipus_denuncia:  { required: 	"Introduzca Error detectado"},
	        denuncia_nom:                { required: 	"Introduzca Nombre"},
	        denuncia_email:              { required: 	"Introduzca Email"}
	    },

	    errorLabelContainer: $("#divWarning ul"),

	    errorElement:"li",
	    debug:false
	});

});



function toogleHidden() {
    var div = $('#divComplaint');
    //div.toogleClass("hidden");
    if(div.hasClass("hidden"))
        div.removeClass("hidden");
    else
        div.addClass("hidden");
    }

function checkEmail(event) {
    $.post("/checkEmail_xd.html", {
        email: event.target.value,
        ajax: 'true'
    }, function(json_data){
        var obj 	= eval("(" + json_data + ")");
        if($("#denuncia_id_usuari")[0].value=="")
            $("#denuncia_id_usuari").attr("value",obj.id_usuari);
        }, "JSON");
    }

function sendComplaint(event) {
    $.post("/new-complaint_pn.html", {
            id_anunci: $("#denuncia_id_anunci")[0].value,
            id_usuari: $("#denuncia_id_usuari")[0].value,
            id_tipus_denuncia: $("input[@name=denuncia_id_tipus_denuncia]:checked")[0].value,
            nom: $("#denuncia_nom")[0].value,
            email: $("#denuncia_email")[0].value,
            comentaris: $("#denuncia_comentaris")[0].value
            },
        function(json_data) {
            var obj 	= eval("(" + json_data + ")");
            $("#frmComplaint").hide();
            $("#divComplaintSent").show();
            },
        "JSON");
    }
