$.fn.serializeObject = function()
{
    var o = {};
    var a = this.serializeArray();
    $.each(a, function() {
        if (o[this.name] !== undefined) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else {
            o[this.name] = this.value || '';
        }
    });
    return o;
};

var $mailmodel = {
	get_modal: function(id){$.modal(document.getElementById('mail_model_' + id));return false;},
	mail_model: function(id, url){
		$("#simplemodal-container").height("auto"); 
		var form = $('#mail_model_form_' + id);
		var oElements = form.serializeObject();
		form.find(".chargement").show();
		form.find(".success").hide();
		form.find(".error").hide();
		$.post(url,oElements,function(res){
			form.find(".chargement").hide();
			if (res.ok == false){
				form.find(".error").show();
				var errors = "";
				for(var i in res.errors){
				    errors += "<li>" + i + ": " + res.errors[i][0] + "</li>";
				}
				form.find(".error ul").html(errors);
			}else if(res.ok == true){
				form.find(".success").show();
				form.find(".success").html(res.message);
				form.find(".form_question").hide();
				setTimeout('$.modal.close()', 5000 );
				
			} else{	
				alert("une erreur est survenue, réessayer plus tard.")}
		}, "json")
	}
}

	
