function addChamp() {
    if (document.getElementById("file10") != null) alert("Impossible d'envoyer plus de 10 images simultanément");
    var imgMore = document.getElementById("moreFields");
    var fileField = document.createElement("input");
    fileField.type = "file";fileField.accept = "png,jpeg,jpg,gif";
    for (i = 1; i < 11 ; i++) {
        if (document.getElementById("file"+i.toString()) == null) {
            fileField.name = "file"+i.toString();fileField.id = "file"+i.toString();
            var backButton = document.getElementById("retour");
            var sendButton = document.getElementById("send");
            document.getElementById("uploadPics").removeChild(sendButton);document.getElementById("uploadPics").removeChild(backButton);document.getElementById("uploadPics").removeChild(document.getElementById("moreFields"));document.getElementById("uploadPics").appendChild(fileField);document.getElementById("uploadPics").appendChild(imgMore); document.getElementById("uploadPics").appendChild(document.createElement("br"));document.getElementById("uploadPics").appendChild(sendButton);document.getElementById("uploadPics").appendChild(backButton);
            break;
        }
    }
}

function dispPicTools(sender) {
    if(!document.getElementById("deletebutton")) {
        var delButton = document.createElement("img");
        delButton.src = "../images/editdelete.png";delButton.alt = "Delete";delButton.id = "deletebutton";delButton.style.padding = '0';delButton.style.border = '0';
        delButton.onclick = function() {
            if(confirm("Attention : voulez-vous vraiment supprimer cette image ? Cette action est irréversible.")) {
                var xhr = getXMLHTTP();
                xhr.onreadystatechange = function(){ if(xhr.readyState == 4 && xhr.status == 200){ var rep = xhr.responseText; alert(rep); if (rep=='Image supprimée avec succès') { sender.parentNode.removeChild(sender); } } }
                xhr.open("POST","adm/galery/delphoto.php",true);
                xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                xhr.send("id="+sender.id);
            }
        }
        var editButton = document.createElement("img");
        editButton.src = "../images/edit.png";editButton.alt = "Edit";editButton.id = "editbutton";editButton.style.padding = '0';editButton.style.border = '0';
        editButton.onclick = function() {
            var fullcomm = document.getElementById("fullcomm"+sender.id).innerHTML;
            var comm = prompt("Description associée à l'image ?",fullcomm);
            if(comm !=null && comm!="" && comm!=fullcomm) {
                var xhr = getXMLHTTP();
                xhr.onreadystatechange = function(){ if(xhr.readyState == 4 && xhr.status == 200){ var rep = xhr.responseText; alert(rep); if (rep=='Commentaire mis à jour avec succès') { window.location.reload(); } } }
                xhr.open("POST","adm/galery/editcomm.php",true);
                xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
                xhr.send("id="+sender.id+"&comm="+comm);
            }
        }
        var br = document.createElement("br");
        br.id = "breakline";
        sender.appendChild(br);
        sender.appendChild(delButton);
        sender.appendChild(editButton);
    }
}

function hidePicTools(sender,event) {
    if (!e) var e = window.event || event;
    var relTarg = e.relatedTarget || e.toElement;
    if (relTarg.parentNode != sender) {
        var br = document.getElementById("breakline");
        var del = document.getElementById("deletebutton");
        var edit = document.getElementById("editbutton");
        sender.removeChild(br);
        sender.removeChild(del);
        sender.removeChild(edit);
    }
}

function diaporama(id) {
    var fond = document.createElement('div');
    fond.id = 'fondimage';
    fond.style.backgroundImage = 'url("../../images/atbkg.png")';
    fond.style.width = '100%';
    fond.style.height = '100%';
    fond.style.minHeight = (GetDomOffset(document.getElementById('pied'), 'offsetTop')+30)+'px';
    fond.style.overflow='auto';
    fond.style.display='block';
    fond.style.position='absolute';
    fond.style.left = '0';
    fond.style.top = '0';
    fond.style.backgroundColor = 'transparent';
    fond.style.zIndex = '10';
    var xhr = getXMLHTTP();
    xhr.onreadystatechange = function(){
        if(xhr.readyState == 4 && xhr.status == 200){
            var rep = xhr.responseText;
            if (rep.charAt(rep.length - 4) != '.') { alert(rep); }
            var xhrq = getXMLHTTP();
            xhrq.onreadystatechange = function(){
                if(xhrq.readyState == 4 && xhr.status == 200){
                    var comm = xhrq.responseText;
                    var theimg = document.createElement('img');
                    theimg.src = "galery/"+rep;
                    fond.innerHTML = '<div id="divpic" style="width:814px;margin:auto;">\n\
                      <img src="../images/1leftarrow.png" alt="<" onclick="previouspicture('+id+');" />\n\
                      <img src="../images/application_exit.png" alt="X" onclick="document.body.removeChild(document.getElementById(\'fondimage\'));" />\n\
                      <img src="../images/1rightarrow.png" alt=">" onclick="nextpicture('+id+');" /><br/>\n\
                      <img id="flszpic" src="galery/'+rep+'" alt="photo" style="width:'+theimg.width+'px; padding:5px; border: 2px solid #909090; margin:auto; display:block;" onclick="document.body.removeChild(document.getElementById(\'fondimage\'));" />\n\
                      <p style="background-color:white; padding: 5px; border : 2px solid black;">'+comm+'</p>\n\
                      </div>';
                    if (document.getElementById('flszpic').style.width == '0px') {
                        document.body.removeChild(document.getElementById('fondimage'));
                        diaporama(id);
                    }
                }
            }
            xhrq.open("POST","adm/galery/getcomm.php",true);
            xhrq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            xhrq.send("id="+id);
        }
    }
    xhr.open("POST","adm/galery/getpicture.php",true);
    xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
    xhr.send("id="+id);
    document.body.appendChild(fond);
}

function previouspicture(id) {
    var xhr = getXMLHTTP();
    xhr.onreadystatechange =  function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var rep = xhr.responseText;
            if (rep != '') {
                document.body.removeChild(document.getElementById('fondimage'));
                diaporama(rep);
            }
        }
    }
    xhr.open("POST",'adm/galery/previouspicture.php',true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.send("id="+id);
}

function nextpicture(id) {
    var xhr = getXMLHTTP();
    xhr.onreadystatechange =  function() {
        if (xhr.readyState == 4 && xhr.status == 200) {
            var rep = xhr.responseText;
            if (rep != '') {
                document.body.removeChild(document.getElementById('fondimage'));
                diaporama(rep);
            }
        }
    }
    xhr.open("POST",'adm/galery/nextpicture.php',true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xhr.send("id="+id);
}
