var DownloadManagerDocumentForm = function () {};

/***
 * Initialise le formulaire
 */
DownloadManagerDocumentForm.Init = function (callBack) {
    Event.AddById("btnSaveDocument", "click", DownloadManagerDocumentForm.SaveDocument);
    DownloadManagerDocumentForm.callBack = callBack;
};

/***
 * Retourne l'id du formulaire
 */
DownloadManagerDocumentForm.GetId = function () {
    return Form.GetId("DownloadManagerDocumentForm");
};

/***
 * Sauvegarde le document
 */
DownloadManagerDocumentForm.SaveDocument = function (e) {

    if (Form.IsValid("DownloadManagerDocumentForm")) {

        var data = "Class=DownloadManager&Methode=SaveDocument&App=DownloadManager";
        data += Form.Serialize("DownloadManagerDocumentForm");

        Request.Post("Ajax.php", data).then(data => {

            if (data.statut == "Error") {
                Form.RenderError("DocumentForm", data.message);
            } else {
                Dialog.Close();
                Animation.Notify(Language.GetCode("DownloadManager.DocumentAdded"));

                if (DocumentForm.callBack) {
                    DocumentForm.callBack(data);
                }
            }
        });
    }
};
