var ajaxOptions = {
    type: "POST",
    dataType: "json"
};

$(document).ready(function() {
    $("div.toolbar a:not(.disabled), form a:not(.disabled)")
    .mousedown(function() {
        $(this).addClass('clicked');
    })
    .mouseover(function() {
        $(this).addClass('hover');
    })
    .mouseout(function() {
        $(this).removeClass('hover');
    })
    .mouseup(function() {
        $(this).removeClass('clicked');
    });

    $("#toolbox-taille-texte").fontScaler({
        target: "#content-main",
        increment: "fixed",
        fixed: {
            character: "A  ",
            size1: 100,
            size2: 125,
            size3: 150,
            id1: "fontsize1",
            id2: "fontsize2",
            id3: "fontsize3"
        },
        store: true,
        storetime: 525600
    });

    $.ajaxSetup(ajaxOptions);

    $("#fontsize1").css("font-size", "14px");
    $("#fontsize2").css("font-size", "16px");
    $("#fontsize3").css("font-size", "18px");
	
    $("#modal").jqm({
        overlay: 50,
        modal: true
    });

	$($('.articles .article').get(3)).css('border', 0);
	
	if ($('#content-container .definition').length > 0) {
		initDefinitions();
	}
});

initDefinitions = function() {
	var definitions = {};

	$('#content-container .definition')
		.live("mouseover", function(e) {
			link = $(this);
			word = $(this).html();
			pos = $(this).position();
			$(this).css('cursor', 'help');
			$('#definition').css('left', pos.left - 40);
			$('#definition').css('top', pos.top + 18);

			if (definitions[word] == undefined) {
				$.get('/dictionnaire/definition?word=' + word, {}, function(data) {
					$('#definition').html("<b>" + word + "</b> : " + data);
					$('#definition').css('display', 'block');
					definitions[word] = data;
				}, 'text');			
			} else {
				$('#definition').html("<b>" + word + "</b> : " + definitions[word]);
				$('#definition').css('display', 'block');			
			}
		})
		.live("mouseout", function(event) {
			$('#definition').css('display', 'none');
		})
		.live('click', function(event) {
			return false;
		});
}

showAuthentificationForm = function() {
    $("#modal").jqm({
        ajax: "/authentification/login"
    });
    $("#modal").jqmShow();
}

doLogin = function(form) {
	form = $(form);

	var submit = $("input[type=submit]", form);
	var parent = submit.parent();
	submit.remove();
	parent.append('<img width="20px" id="' + submit.attr('id') + '" class="throbber" src="/images/throbber.gif" />');
	
	$('.error_list', form).remove();

   ajaxOptions.url = form.attr('action');
    // ajaxOptions.url = "/frontend_dev.php/authentification/login";
    ajaxOptions.data = "__ajaxcall=true&" + form.serialize();
    ajaxOptions.success = function(response) {
		if (response.html)
		{
			$(form).html('<div id="confirmation">' + response.html + '</div>');
		}
		else if (response.redirect)
		{
			window.location = response.redirect;
		}
		else if (response.errors)
		{
			for (i in response.errors)
			{
				$('#frontloginclassic_' + i, form).before('<ul class="error_list"><li>' + response.errors[i] + '</li></ul>');
			}

			$('.throbber').remove();
			parent.append(submit);
		}
    };

    $.ajax(ajaxOptions);
	
	return false;
}

showMsg = function(tpl) {
    $("#modal").jqm({
        ajax: "/common/showmsg?tpl=" + tpl
    });
    $("#modal").jqmShow();
}

showForgottenPassword = function() {
    $("#modal").jqmHide();
    $("#modal").jqm({ajax: "/authentification/forgottenpassword"});
    $("#modal").jqmShow();
	return false;
}


doForgottenPassword = function(form) {
	form = $(form);

	var submit = $("input[type=submit]", form);
	var parent = submit.parent();
	submit.remove();
	parent.append('<img width="20px" id="' + submit.attr('id') + '" class="throbber" src="/images/throbber.gif" />');
	
	$('.error_list', form).remove();

	ajaxOptions.url = form.attr('action');
    ajaxOptions.data = "__ajaxcall=true&" + form.serialize();
    ajaxOptions.success = function(response) {
		
		if (response.html)
		{
			$(form).html('<div id="confirmation">' + response.html + '</div>');
		}
		else if (response.redirect)
		{
			window.location = response.redirect;
		}
		else if (response.errors)
		{
			for (i in response.errors)
			{
				$('#forgottenpassword_' + i, form).before('<ul class="error_list"><li>' + response.errors[i] + '</li></ul>');
			}

			$('.throbber').remove();
			parent.append(submit);
		}
    };

    $.ajax(ajaxOptions);
	
	return false;
}

showSubscriptionForm = function() {
    $("#modal").jqm({
        ajax: "/common/subscription"
    });
    $("#modal").jqmShow();
}

doSubscription = function(form) {
	form = $(form);

	var submit = $("input[type=submit]", form);
	var parent = submit.parent();
	submit.remove();
	parent.append('<img width="20px" id="' + submit.attr('id') + '" class="throbber" src="/images/throbber.gif" />');
	
	$('.error_list', form).remove();

	ajaxOptions.url = form.attr('action');
    ajaxOptions.data = "__ajaxcall=true&" + form.serialize();
    ajaxOptions.success = function(response) {
		
		if (response.html)
		{
			$(form).html('<div id="confirmation">' + response.html + '</div>');
		}
		else if (response.redirect)
		{
			window.location = response.redirect;
		}
		else if (response.errors)
		{
			for (i in response.errors)
			{
				$('#frontsubscription_' + i, form).before('<ul class="error_list"><li>' + response.errors[i] + '</li></ul>');
			}

			$('.throbber').remove();
			parent.append(submit);
		}
    };

    $.ajax(ajaxOptions);
	
	return false;
}


showContactForm = function(subject, title) {
    $("#modal").jqm({
        ajax: "/common/contact",
		onLoad: function() {
			if (subject) {
				$('#contact_idtype').children().each(function(i, child) 
				{
					if (subject == $(child).html())
					{
						$('#contact_idtype').val($(child).val());
					} 
				});				
			}
			
			if (title) {
				$('#contact_title').val(title);
			}
		}
    });
    $("#modal").jqmShow();
}

saveContact = function(form) {
	if ($('#contact_content', form).val() == "" ||
		$('#contact_content', form).val() == "Cliquez ici pour saisir votre question" ||
		$('#contact_content', form).val() == "Veuillez d'abord saisir une question, s'il vous plait")
	{
		$('#contact_content', form).val("Veuillez d'abord saisir une question, s'il vous plait");
		return false;
	}

    ajaxOptions.url = "/common/contact";
	ajaxOptions.data = $(form).serialize();

	var submit = $("input[type=submit]", form);
	var parent = submit.parent();
	submit.remove();
	parent.append('<img width="20px" id="' + submit.attr('id') + '" class="throbber" src="/images/throbber.gif" />');
	
	$('.error_list', form).remove();
	
	if ($("input[id=contact_title]", form).length == 0)
	{
		ajaxOptions.data = ajaxOptions.data + '&contact[title]=Un nouveau message sur wicare.fr';;
	} 
	
    ajaxOptions.success = function(response) {
		if (response.html)
		{
			$(form).html('<div id="confirmation">' + response.html + '</div>');
		}
		else if (response.errors)
		{
			for (i in response.errors)
			{
				$('#contact_' + i, form).before('<ul class="error_list"><li>' + response.errors[i] + '</li></ul>');
			}

			$('.throbber').remove();
			parent.append(submit);
		}
    }

    $.ajax(ajaxOptions);
	return false;
}

showTagFilterForm = function() {
    $("#modal").jqm({
        ajax: "/motscles/filtrer",
        onHide: function() {
            window.location.reload(true);
        }
    });
    $("#modal").jqmShow();
}

showTagFilterList = function() {
    ajaxOptions.url = "/motscles/userlist";
    ajaxOptions.async = false;
    ajaxOptions.data = "__ajaxcall=true";
    ajaxOptions.success = function(response) {
        if(response.tags)
        {
            tags = response.tags;
            for(i in tags)
            {
                var tag = tags[i];

                var link = '<span>' + tag.name + '&nbsp;<a href="javascript: void(0);" onClick="toggleTagFilter(' + tag.id + ');">' + (tag.active == '1' ? 'Des.' : 'Act.') + '</a>&nbsp;<a href="javascript: void(0);" onClick="if(confirm(\'Etes vous sur de supprimer ce mot clé ?\')) deleteTagFilter(' + tag.id + ');">Supprimer</a></span>&nbsp;';
                $("#tagslist").append(link);
            }
        }
    };
	
    $("#tagslist").html('');
	
    $.ajax(ajaxOptions);
}

toggleTagFilter = function(tagid) {
    ajaxOptions.url = "/motscles/toggle";
    ajaxOptions.async = false;
    ajaxOptions.data = "__ajaxcall=true&tagid=" + tagid;
    ajaxOptions.success = function(response) {
        if(response.error)
        {
            $("#tagerror").html(response.error);
            $("#tagmessage").html("&nbsp;");
        }
        else
        {
            $("#tagerror").html("&nbsp;");
            $("#tagmessage").html(response.result);
            showTagFilterList();
        }
    }

    $.ajax(ajaxOptions);
}

deleteTagFilter = function(tagid) {
    ajaxOptions.url = "/motscles/delete";
    ajaxOptions.async = false;
    ajaxOptions.data = "__ajaxcall=true&tagid=" + tagid;
    ajaxOptions.success = function(response) {
        if(response.error)
        {
            $("#tagerror").html(response.error);
            $("#tagmessage").html("&nbsp;");
        }
        else
        {
            $("#tagerror").html("&nbsp;");
            $("#tagmessage").html(response.result);
            showTagFilterList();
        }
    }

    $.ajax(ajaxOptions);
}

showForgottenPasswordForm = function() {
    $("#modal").jqm({
        ajax: "/authentification/forgottenpassword"
    });
    $("#modal").jqmShow();
}

showPrivateMessageForm = function(id) {
    $("#modal").jqm({
        ajax: "/message/create?dest=" + id
    });
    $("#modal").jqmShow();
}

savePrivateMessage = function() {
    ajaxOptions.url = "/message/save";
    ajaxOptions.async = false;
    ajaxOptions.data = "__ajaxcall=true&privatemessage[dest]=" + $("#privatemessage_dest").val() + "&privatemessage[message]=" + $("#privatemessage_message").val() + "&privatemessage[_csrf_token]=" + $("#privatemessage__csrf_token").val();
    ajaxOptions.success = function(response) {
        if(response.error)
        {
            $("#privatemessage_error").html(response.error);
            $("#privatemessage_msg").html("&nbsp;");
        }
        else if(response.id == 0)
        {
            $("#privatemessage_error").html("Le message n'a pu être crée.");
            $("#privatemessage_msg").html("&nbsp;");
        }
        else
        {
            $("#privatemessage_error").html("&nbsp;");
            $("#privatemessage_msg").html("Le message a été envoyé.");
            $("#privatemessage_form").slideUp();
        }
    }

    $.ajax(ajaxOptions);
}

removePrivateMessage = function(id) {
    ajaxOptions.url = "/message/delete";
    ajaxOptions.async = false;
    ajaxOptions.data = "__ajaxcall=true&id=" + id;
    ajaxOptions.success = function(response) {
        if(response.error)
        {
            alert(response.error);
        }
        else
        {
            alert(response.result);
            window.location.reload(true);
        }
    }

    $.ajax(ajaxOptions);
}

removeDiscution = function(id) {
    ajaxOptions.url = "/discuter/delete";
    ajaxOptions.async = false;
    ajaxOptions.data = "__ajaxcall=true&id=" + id;
    ajaxOptions.success = function(response) {
        if(response.error)
        {
            alert(response.error);
        }
        else
        {
            alert(response.result);
            window.location.reload(true);
        }
    }

    $.ajax(ajaxOptions);
}

removeTemoignage = function(id) {
    ajaxOptions.url = "/temoigner/delete";
    ajaxOptions.async = false;
    ajaxOptions.data = "__ajaxcall=true&id=" + id;
    ajaxOptions.success = function(response) {
        if(response.error)
        {
            alert(response.error);
        }
        else
        {
            alert(response.result);
            window.location.reload(true);
        }
    }

    $.ajax(ajaxOptions);
}

removeUrl = function(id) {
    ajaxOptions.url = "/favoris/delete";
    ajaxOptions.async = false;
    ajaxOptions.data = "__ajaxcall=true&id=" + id;
    ajaxOptions.success = function(response) {
        if(response.error)
        {
            alert(response.error);
        }
        else
        {
            alert(response.result);
            window.location.reload(true);
        }
    }

    $.ajax(ajaxOptions);
}

removeComment = function(id) {
    ajaxOptions.url = "/commentaire/delete";
    ajaxOptions.async = false;
    ajaxOptions.data = "__ajaxcall=true&id=" + id;
    ajaxOptions.success = function(response) {
        if(response.error)
        {
            alert(response.error);
        }
        else
        {
            alert(response.result);
            window.location.reload(true);
        }
    }

    $.ajax(ajaxOptions);
}

saveComment = function() {
    ajaxOptions.url = "/commentaire/save";
    ajaxOptions.async = false;
    ajaxOptions.data = "__ajaxcall=true&comment[iddocument]=" + $("#comment_iddocument").val() + "&comment[idparent]=" + $("#comment_idparent").val() + "&comment[anonymous]=" + $("#comment_anonymous").val() + "&comment[paragraph]=" +$("#comment_paragraph").val() + "&comment[comment]=" + $("#comment_comment").val() + "&comment[_csrf_token]=" + $("#comment__csrf_token").val();
    ajaxOptions.success = function(response) {
        if(response.error)
        {
            $("#comment_error").html(response.error);
            $("#comment_message").html("&nbsp;");
        }
        else if(response.id == 0)
        {
            $("#comment_error").html("Le commentaire n'a pu être crée.");
            $("#comment_message").html("&nbsp;");
        }
        else
        {
            $("#comment_error").html("&nbsp;");
            $("#comment_message").html("Le commentaire a été posté.");
            $("#comment_comment").val("");
            $("#comment_form").slideUp();
        }
    }

    $.ajax(ajaxOptions);
}

showDiscutionForm = function(id) {
    $("#modal").jqm({
        ajax: "/discuter/create?idtheme=" + id,
        onHide: function() {
            window.location.reload(true);
        }
    });
    $("#modal").jqmShow();
}


saveDiscution = function() {
    ajaxOptions.url = "/discuter/save";
    ajaxOptions.async = false;
    ajaxOptions.data = "__ajaxcall=true&discution[idtheme]=" + $("#discution_idtheme").val() + "&discution[title]=" + $("#discution_title").val() + "&discution[content]=" + $("#discution_content").val() + "&discution[_csrf_token]=" + $("#discution__csrf_token").val();
    ajaxOptions.success = function(response) {
        if(response.error)
        {
            $("#discution_error").html(response.error);
            $("#discution_message").html("&nbsp;");
        }
        else if(response.id == 0)
        {
            $("#discution_error").html("La discussion n'a pu être crée.");
            $("#discution_message").html("&nbsp;");
        }
        else
        {
            $("#discution_error").html("&nbsp;");
            $("#discution_message").html("La discussion a été postée.");
            $("#discution_form").slideUp();
        }
    }

    $.ajax(ajaxOptions);
}

showTemoignageForm = function(id) {
    $("#modal").jqm({
        ajax: "/temoigner/create"
    });
    $("#modal").jqmShow();
}

saveTemoignage = function() {
    ajaxOptions.url = "/temoigner/save";
    ajaxOptions.async = false;
    ajaxOptions.data = "__ajaxcall=true&temoignage[anonymous]=" + $("#temoignage_anonymous").val() + "&temoignage[title]=" + $("#temoignage_title").val() + "&temoignage[content]=" + $("#temoignage_content").val() + "&temoignage[_csrf_token]=" + $("#temoignage__csrf_token").val();
    ajaxOptions.success = function(response) {
        if(response.error)
        {
            $("#temoignage_error").html(response.error);
            $("#temoignage_message").html("&nbsp;");
        }
        else if(response.id == 0)
        {
            $("#temoignage_error").html("Le temoignage n'a pu être crée.");
            $("#temoignage_message").html("&nbsp;");
        }
        else
        {
            $("#temoignage_error").html("&nbsp;");
            $("#temoignage_message").html("Le temoignage a été posté et est en attente de validation.");
            $("#temoignage_form").slideUp();
        }
    }

    $.ajax(ajaxOptions);
}

showUrlForm = function(id) {
    $("#modal").jqm({
        ajax: "/favoris/create?iddossier=" + id
    });
    $("#modal").jqmShow();
}

fetchUrlInfos = function(url) {
    ajaxOptions.url = "/favoris/fetchUrlInfos?url=" + url;
    ajaxOptions.async = true;
    ajaxOptions.success = function(response) {
		$("#url_label").val('');
		$("#url_content").val('');
		$("#url_img").attr('src', '');
	
		if(response.title != null) {
			$("#url_label").val(response.title);
		}

		if (response.description != null) {
			$("#url_content").val(response.description);
		}

		if (response.img != null) {
			$("#url_img").attr('src', response.img);
		}
    }

    $.ajax(ajaxOptions);
	return false;
}

saveUrl = function() {
    ajaxOptions.url = "/favoris/save";
    ajaxOptions.async = false;
	ajaxOptions.data = "__ajaxcall=true&" + $('#url_post').serialize();
    ajaxOptions.success = function(response) {
        if(response.error)
        {
            $("#url_error").html(response.error);
            $("#url_message").html("&nbsp;");
        }
        else if(response.id == 0)
        {
            $("#url_error").html("L'url n'a pu être enregistré.");
            $("#url_message").html("&nbsp;");
        }
        else
        {
            $("#url_error").html("&nbsp;");
            $("#url_message").html("L'url a été postée et est en attente de validation.");
            $("#url_form").slideUp();
        }
    }

    $.ajax(ajaxOptions);
}

showAbuseForm = function(id) {
    $("#modal").jqm({
        ajax: "/abuse/create?iddocument=" + id
    });
    $("#modal").jqmShow();
}

saveAbuse = function() {
    ajaxOptions.url = "/abuse/save";
    ajaxOptions.async = false;
    ajaxOptions.data = "__ajaxcall=true&abuse[iddocument]=" + $("#abuse_iddocument").val() + "&abuse[comment]=" + $("#abuse_comment").val() + "&abuse[_csrf_token]=" + $("#abuse__csrf_token").val();
    ajaxOptions.success = function(response) {
        if(response.error)
        {
            $("#abuse_error").html(response.error);
            $("#abuse_message").html("&nbsp;");
        }
        else if(response.id == 0)
        {
            $("#abuse_error").html("L'abus n'a pu être enregistré.");
            $("#abuse_message").html("&nbsp;");
        }
        else
        {
            $("#abuse_error").html("&nbsp;");
            $("#abuse_message").html("Merci pour votre participation à l'amélioration de WiCare.");
            $("#abuse_form").slideUp();
        }
    }

    $.ajax(ajaxOptions);
}

saveDocument = function(id) {
    ajaxOptions.url = "/common/documentsave";
    ajaxOptions.async = false;
    ajaxOptions.data = "__ajaxcall=true&id=" + id;
    ajaxOptions.success = function(response) {
        if(response.error)
        {
            alert(response.error);
        }
        else
        {
            alert("Document archive");
            window.location.reload(true);
        }
    }

    $.ajax(ajaxOptions);
}

removeDocument = function(id) {
    ajaxOptions.url = "/common/documentdelete";
    ajaxOptions.async = false;
    ajaxOptions.data = "__ajaxcall=true&id=" + id;
    ajaxOptions.success = function(response) {
        if(response.error)
        {
            alert(response.error);
        }
        else
        {
            alert(response.result);
            window.location.reload(true);
        }
    }

    $.ajax(ajaxOptions);
}

shareDocument = function(module, link) {
    $("#modal").jqm({
        ajax: "/" + module +  "/documentshare?module=" + module + "&link=" + link
    });
    $("#modal").jqmShow();
}

saveMark = function(id, value) {
    ajaxOptions.url = "/note/save";
    ajaxOptions.async = false;
    ajaxOptions.data = "__ajaxcall=true&mark[iddocument]=" + id + "&mark[value]=" + value;
}

shareDocument = function(module, link) {
    $("#modal").jqm({
        ajax: "/" + module +  "/documentshare?module=" + module + "&link=" + link
    });
    $("#modal").jqmShow();
}

saveMark = function(id, value) {
    ajaxOptions.url = "/note/save";
    ajaxOptions.async = false;
    ajaxOptions.data = "__ajaxcall=true&mark[iddocument]=" + id + "&mark[value]=" + value;
    ajaxOptions.success = function(response) {
        if(response.error)
        {
            $("#mark_error" + id).html(response.error);
            $("#mark_message" + id).html("&nbsp;");
        }
        else if(response.id == 0)
        {
            $("#mark_error" + id).html("La note n'a pu être enregistré.");
            $("#mark_message" + id).html("&nbsp;");
        }
        else
        {
            $("#mark_error" + id).html("&nbsp;");
            $("#mark_message" + id).html("Vous avez approuvé ce document.");
        }
    }

    $.ajax(ajaxOptions);
}
askComment = function(id, idparent) {
    $("#modal").jqm({
        ajax: "/commentaire/create?iddocument=" + id + "&idparent=" + idparent,
        onHide: function() {
            window.location.reload(true);
        }
    });
    $("#modal").jqmShow();
}

followUser = function(id, notify, save) {
    ajaxOptions.url = "/profile/follow";
    ajaxOptions.async = false;
    ajaxOptions.data = "__ajaxcall=true&m_user_tracked[idusertracked]=" + id + "&m_user_tracked[notifybymail]=" + notify + "&m_user_tracked[_csrf_token]=" + $("#m_user_tracked__csrf_token").val() + "&save=" + save;
    ajaxOptions.success = function(response) {
        if(response.error)
        {
            $("#user_error").html(response.error);
            $("#user_message").html("&nbsp;");
        }
        else
        {
            $("#user_error").html("&nbsp;");
            $("#user_message").html("Utilisateur " + (save === 1 ? "enregistré" : "supprimé") + ".");
        }
    }

    $.ajax(ajaxOptions);
}

searchHome = function() {
    var word = $("#frontsearch_word").val();
    var err = false;

    if(!word)
    {
        $("#word_error").html("Veuillez renseigner ce champ.");
        err = true;
    }
    else if(word.length < 3)
    {
        $("#word_error").html("Un mot de 3 caracteres minimum.");
        err = true;
    }
    else
    {
        $("#word_error").html("&nbsp;");
    }

    if(err === true)
    {
        return false;
    }

    baseurl = "/";
    ajaxOptions.url = "/common/txttourl";
    ajaxOptions.async = false;
    ajaxOptions.data = "__ajaxcall=true&q=" + word;
    ajaxOptions.success = function(response) {
        if(response.txttourl)
        {
            baseurl += response.txttourl + "/recherche";
        }
        else
        {
            return false;
        }
    }

    $.ajax(ajaxOptions);

    window.location.href = baseurl;
}

searchDirectory = function() {
    var what = $("#what").val();
    var nextto = $("#nextto").val();
    var err = false;

    if(!what)
    {
        $("#what_error").html("Veuillez renseigner ce champ.");
        err = true;
    }
    else
    {
        $("#what_error").html("&nbsp;");
    }

    if(!nextto)
    {
        $("#nextto_error").html("Veuillez renseigner ce champ.");
        err = true;
    }
    else
    {
        $("#nextto_error").html("&nbsp;");
    }

    if(err === true)
    {
        return false;
    }

    baseurl = "/annuaire-perte-autonomie/resultat";

    ajaxOptions.url = "/common/txttourl";
    ajaxOptions.async = false;
    ajaxOptions.data = "__ajaxcall=true&q=" + what;
    ajaxOptions.success = function(response) {
        if(response.txttourl)
        {
            baseurl += "/" + response.txttourl;
        }
        else
        {
            return false;
        }
    }

    $.ajax(ajaxOptions);

    ajaxOptions.data = "__ajaxcall=true&q=" + nextto;
	
    $.ajax(ajaxOptions);

    window.location.href = baseurl;
    return false;
}

saveTagFilter = function()
{
    var tagid = $("#tagid").val();
    ajaxOptions.url = "/motscles/save";
    ajaxOptions.async = false;
    ajaxOptions.data = "__ajaxcall=true&tagid=" + tagid;
    ajaxOptions.success = function(response) {
        if(response.error)
        {
            $("#tagerror").html(response.error);
            $("#tagmessage").html("&nbsp;");
        }
        else
        {
            $("#tagerror").html("&nbsp;");
            $("#tagmessage").html("Le mot cle a été ajouté");
            $("#tagstr").val('');
            showTagFilterList();
        }
    }

    $.ajax(ajaxOptions);
}

initAutocompleteTag = function()
{
    var elt = $("#tagstr");
    elt.autocomplete("/motscles/list",
    {
        delay: 3,
        minChars: 3,
        selectFirst: true,
        hightlight: false,
        autoFill: true,
        dataType: "text"
    }).bind("result", function (event, data, formatted) {
        $("#tagid").val(data[1]);
    });
}

rmTagInForm = function(id)
{
    if(confirm("Etes vous sur ?"))
    {
        $("#url_tags_list :selected").each(function(k, v) {

            if(id == $(v).val())
            {
                this.selected = false;
            }
        });
        $("#tag_" + id).remove();
    }
}

initAutocompleteTagInForm = function(id)
{
    var elt = $("#" + id);
    elt.autocomplete("/motscles/list",
    {
        delay: 3,
        minChars: 3,
        selectFirst: true,
        hightlight: false,
        autoFill: false,
        dataType: "text"
    }).bind("result", function (event, data, formatted) {
		console.log(data);
        var link = '<a id="tag_' + data[1] + '" href="javascript: void(0);" onClick="rmTagInForm(' + data[1] + ');">' + data[0]  + '</a>&nbsp;';
        $("#tdtagslist").append(link);
		$("#url_tags_list").selectOptions(data[1]);
		$("#" + id).val('');
    });
}

initAutocomplete = function(module, id)
{
    var elt = $("#" + id);
    elt.autocomplete("/" + module + "/list",
    {
        delay: 3,
        minChars: 3,
        scrollHeight: 100,
        selectFirst: true,
        hightlight: false,
        autoFill: true,
        dataType: "text"
    });
}

pagination_toggle = function(elm, elementName)
{
    var pagerRest = $('.pager-rest', $(elm).parent().parent());
    var lastElement = $(pagerRest).prev()[0];
	
    if (pagerRest.css('display') == 'none')
    {
        pagerRest.show('slide');
        lastElement.addClass(elementName);
        lastElement.removeClass(elementName + '-last');
    }
    else
    {
        pagerRest.hide('slide');
        lastElement.removeClass(elementName);
        lastElement.addClass(elementName + '-last');
    }
}

printDocument = function(id)
{
    window.open("/common/documentprint?id=" + id);
}

// Permet de mettre a jour le formulaire de contact si sujet choisi ou non

updateContactTitle = function()
{
    var vals = $("#contact_idtype").selectedValues();
    var texts = $("#contact_idtype").selectedTexts();

    idtype = vals[0];
    label = texts[0];

    if(idtype > 0)
    {
        $("#contact_title").val(label);
        $("#contact_title").attr("readonly", "true");
    }
    else
    {
        $("#contact_title").val("");
        $("#contact_title").removeAttr("readonly");
    }
}
