String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, ''); 
};

var intv = '';
function closeMe(e) {
    e.innerHTML='';
    e.className='invisible';
}
function getWindowHeight() {
    var h = 0;
    if (typeof(window.innerHeight) == 'number') {
        h = window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        h = document.documentElement.clientHeight;
    } else if (document.body && document.body.offsetHeight) {
        h = document.body.offsetHeight;
    }
    return h;
}
function getWindowWidth() {
    var w = 0;
    if (typeof(window.innerWidth) == 'number') {
        w = window.innerWidth;
    } else if (document.documentElement && document.documentElement.clientWidth) {
        w = document.documentElement.clientWidth;
    } else if (document.body && document.body.offsetWidth) {
        w = document.body.offsetWidth;
    }
    return w;
}
function tic(id) {
    ch = document.getElementById('c'+id);
    el = document.getElementById('t'+id);
    im = document.getElementById('b'+id);
    if(ch.checked) {
        el.className = 'toftoedit';
        im.src = 'gui/checkempty.png';
        ch.checked = false;
    } else {
        el.className = 'toftoedit t2esel';
        im.src = 'gui/checked.png';
        ch.checked = true;
    }
}
function selectAll() {
    try {
        f = document.getElementById('toflist');
        for (var i = 0; i < f.childNodes.length; i++) {
            var child = f.childNodes[i];
            if (child.className == "toftoedit") { tic(child.id.substring(1,child.id.length)); }
        }
    } catch(e) { }
}
function styleOrphan(id,v) {
    e = document.getElementById(id);
    if (v==0) {
        e.className = 'batchedit orphan';
    } else {
        e.className = 'batchedit';
    }
}
function replaceIt(a,n,r,id) {
// action / n = new value /id à remplacer
// r == 0 on remplace l'id.html, r == 1 on remplace id.parentNode.html
    function ajaxReq() {
        var xhr = createXHR();
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    if(r == 0) {
                        try { document.getElementById(id).innerHTML = (xhr.responseText); } catch (e) {}
                    } else if(r == 1) {
                        try { document.getElementById(id).parentNode.value = (xhr.responseText); } catch (e) {}
                    }
                } else if (xhr.status == 404) {
                    alert('Le contenu n\'est pas accessible (404).');
                } else {
                    alert('Un problème inconnu est survenu avec la requête. (Code : '+xhr.status+')');
                }
                document.getElementById('saving').innerHTML = '';
            }
            else {
                try { document.getElementById('saving').innerHTML = '<span>wait...</span>'; } catch (e) {}
            }
        };
        xhr.open("POST","./inc/ajax.inc.php",true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
        xhr.send("a="+a+"&n="+n);
    }
    ajaxReq();
}
function saveMe(a,f,v,r,id) {
// action / field / value / retour (si pas de retour sauv juste)
// r == 0 pas de retour, ==1 retour type code, ==2 retour type value
// r == 3 parentnode
    function ajaxReq() {
        var xhr = createXHR();
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    if(r == 1) {
                        if(xhr.responseText != 'NaN') {
                            try { document.getElementById(id).innerHTML = (xhr.responseText); } catch (e) {}
                        }
                    } else if(r == 2) {
                        try { document.getElementById(id).value = (xhr.responseText); } catch (e) {}
                    } else if(r == 3) {
                        try { document.getElementById(id.parentNode.id).innerHTML = (xhr.responseText); } catch (e) {}
                    }
                } else if (xhr.status == 404) {
                    alert('Le contenu n\'est pas accessible (404).');
                } else {
                    alert('Un problème inconnu est survenu avec la requête. (Code : '+xhr.status+')');
                }
                document.getElementById('saving').innerHTML = '';
            }
            else {
                try { document.getElementById('saving').innerHTML = '<span>saving...</span>'; } catch (e) {}
            }
        };
        xhr.open("POST","./inc/ajax.inc.php",true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
        if(r == 3) {
            xhr.send("a="+a+"&f="+f.parentNode.id+"&v="+v);
        } else {
            xhr.send("a="+a+"&f="+f+"&v="+v);
        }
    }
    ajaxReq();
}
function upMe() {
    var files = document.getElementById('imgs').files;
    var gid = document.getElementById('gid').value;
    document.getElementById('uploaded').className = 'uploaded';
    for (var i = 0, f; f = files[i]; i++) {
        if((f.type == 'image/jpeg') && (f.size <= 1048576))
        {
            if(upload(f,gid)) { 
                document.getElementById('uploaded').innerHTML = document.getElementById('uploaded').innerHTML + '<div class="upok">Fichier <em>'+f.name+'</em> est OK</div>'; 
            } else {
                document.getElementById('uploaded').innerHTML = document.getElementById('uploaded').innerHTML + '<div class="uperr">Fichier <em>'+f.name+'</em> a provoqué une erreur</div>';
            }
        } else {
            if(f.type != 'image/jpeg') { 
                document.getElementById('uploaded').innerHTML = document.getElementById('uploaded').innerHTML + '<div class="uperr">Fichier <em>'+f.name+'</em> de mauvais type ('+f.type+')</div>'; 
            } else if(f.size > 1048576) { 
                document.getElementById('uploaded').innerHTML = document.getElementById('uploaded').innerHTML + '<div class="uperr">Fichier <em>'+f.name+'</em> trop lourd</div>'; 
            }
        }
    }
    document.getElementById('uploaded').innerHTML = document.getElementById('uploaded').innerHTML + '<div class="upclose">cliquez sur la fenêtre pour la fermer</div>'; 
    replaceIt('getTofsEdit',gid,0,'toflist');
}
function upload(file,gid) {

    var formData = new FormData();
    formData.append("a", "upload");
    formData.append("g", gid);
    formData.append("file", file);

    var xhr = new XMLHttpRequest();

    xhr.open("POST","./inc/ajax.inc.php",true);
    xhr.send(formData);

    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            if ((xhr.status >= 200 && xhr.status <= 200) || xhr.status == 304) {
                if (xhr.responseText != "") {
                    return false;
                }
            }
        }
    }

    return true;
}

// Quick Ajax Function
function quicky(f,a) {
    function ajaxReq() {
        var xhr = createXHR();
        xhr.onreadystatechange = function() { 
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    try { location.reload(); } catch (e) { } //
                } else if (xhr.status == 404) {
                    alert('Le contenu n\'est pas accessible (404).');
                } else {
                    alert('Un problème inconnu est survenu avec la requête. (Code : '+xhr.status+')');
                }
            }
            else {
                try { document.getElementById('saving').innerHTML = '<span>saving...</span>'; } catch (e) {}
            }
        };
        xhr.open("POST","./inc/ajax.inc.php",true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
        xhr.send("a="+f+"&v="+a);
    }
    ajaxReq();  
}
function loadImg() {
    var w = getWindowWidth();
    var h = getWindowHeight();
    function ajaxReq() {
        var xhr = createXHR();
        xhr.onreadystatechange = function() { 
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    try { document.getElementById('tofcont').innerHTML = (xhr.responseText);} catch (e) { document.getElementById('waiter').className = 'wait hide'; }
                } else if (xhr.status == 404) {
                    alert('Le contenu n\'est pas accessible (404).');
                    document.getElementById('waiter').className = 'wait hide';
                } else {
                    alert('Un problème inconnu est survenu avec la requête. (Code : '+xhr.status+')');
                    document.getElementById('waiter').className = 'wait hide';
                }
            }
            else {
                try { 
                    document.getElementById('tofcont').className = 'opa20'; 
                    document.getElementById('waiter').className = 'wait show'; 
                } catch (e) { document.getElementById('waiter').className = 'wait hide'; }
            }
        };
        xhr.open("POST","./inc/ajax.inc.php",true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
        xhr.send("a=loadImg&w="+w+"&h="+h);
    }
    ajaxReq();  
}
function imgLoaded(n) {
    try { 
        document.getElementById('tofcont').className = 'dummy'; 
        document.getElementById('waiter').className = 'wait hide';
        t = setTimeout('loadImg()',n);
    } catch (e) {  }
}
function loadSc() {
    var w = getWindowWidth();
    var h = getWindowHeight();
    function ajaxReq() {
        var xhr = createXHR();
        xhr.onreadystatechange = function() { 
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    try { document.getElementById('showcase').innerHTML = (xhr.responseText);} catch (e) { }
                } else if (xhr.status == 404) {
                    alert('Le contenu n\'est pas accessible (404).');
                } else {
                    alert('Un problème inconnu est survenu avec la requête. (Code : '+xhr.status+')');
                }
            }
            else { }
        };
        xhr.open("POST","./inc/ajax.inc.php",true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
        xhr.send("a=loadSc&w="+w+"&h="+h);
    }
    ajaxReq(); 
}
function bodybg(col) {
    document.getElementById('body').style.backgroundColor = '#'+col;
}
function scrollit(id,dir) {
    var e = document.getElementById(id);
    if(intv == '') {
        if(dir == 'g') {
            intv = setInterval(function() { e.scrollLeft = e.scrollLeft - 20; }, 10);
        } else {
            intv = setInterval(function() { e.scrollLeft = e.scrollLeft + 20; }, 10);
        }
    } else {
        stop();
    }
}
function stop() {
    if(intv != ''){
        window.clearInterval(intv);
        intv = '';
    }
}
function display(e,w,h) {
    var m = document.getElementById('modalbg');
    if(e == 0) {
        m.innerHTML = '';
        m.style.display = "none";
        return;
    }
    var ww = getWindowWidth();
    var wh = getWindowHeight();
    var top = (wh - h) / 2;
    m.innerHTML = '<img style="box-shadow:0px 0px 25px #000;display:block;margin:auto;margin-top:'+top+'px;max-height:95%;" src="'+e.src+'" alt=""/>';
    m.style.display = "block";
}
function loadGal(id,of) {
    var w = getWindowWidth();
    var h = getWindowHeight();
    function ajaxReq() {
        var xhr = createXHR();
        xhr.onreadystatechange = function() { 
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    try { 
                        document.getElementById('gal').innerHTML = (xhr.responseText);
                        document.getElementById('saving').innerHTML = '';
                        } catch (e) { document.getElementById('saving').innerHTML = ''; }
                } else if (xhr.status == 404) {
                    alert('Le contenu n\'est pas accessible (404).');
                    document.getElementById('saving').innerHTML = '';
                } else {
                    alert('Un problème inconnu est survenu avec la requête. (Code : '+xhr.status+')');
                    document.getElementById('saving').innerHTML = '';
                }
            }
            else {
                try { 
                    document.getElementById('saving').innerHTML = '<span>chargement</span>'; 
                } catch (e) { document.getElementById('saving').innerHTML = ''; }
            }
        };
        xhr.open("POST","./inc/ajax.inc.php",true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
        xhr.send("a=loadGal&w="+w+"&h="+h+"&id="+id+"&of="+of);
    }
    ajaxReq();  
}
function loadPhoto(p,g) {
    var w = getWindowWidth();
    var h = getWindowHeight();
    
    function ajaxReqPhoto() {
        var xhr1 = createXHR();
        xhr1.onreadystatechange = function() { 
            if (xhr1.readyState == 4) {
                if (xhr1.status == 200) {
                    try { 
                        document.getElementById('tofcont').innerHTML = (xhr1.responseText);
                        document.getElementById('tofcont').className = 'dummy'; 
                        document.getElementById('waiter').className = 'wait hide';
                    } catch (e) { document.getElementById('waiter').className = 'wait hide'; }
                } else if (xhr1.status == 404) {
                    alert('Le contenu n\'est pas accessible (404).');
                    document.getElementById('waiter').className = 'wait hide';
                } else {
                    alert('Un problème inconnu est survenu avec la requête. (Code : '+xhr1.status+')');
                    document.getElementById('waiter').className = 'wait hide';
                }
            }
            else {
                try { 
                    document.getElementById('tofcont').className = 'opa20'; 
                    document.getElementById('waiter').className = 'wait show'; 
                } catch (e) { document.getElementById('waiter').className = 'wait hide'; }
            }
        };
        xhr1.open("POST","./inc/ajax.inc.php",true);
        xhr1.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
        xhr1.send("a=loadTof&id="+p+"&gid="+g+"&w="+w+"&h="+h);
    };
    
    function ajaxReqFilmstrip() {
        var xhr2 = createXHR();
        xhr2.onreadystatechange = function() { 
            if (xhr2.readyState == 4) {
                if (xhr2.status == 200) {
                    try { 
                        document.getElementById('filmstrip').innerHTML = (xhr2.responseText);
                        document.getElementById('saving').innerHTML = '';
                    } catch (e) { document.getElementById('saving').innerHTML = ''; }
                } else if (xhr2.status == 404) {
                    alert('Le contenu n\'est pas accessible (404).');
                    document.getElementById('saving').innerHTML = '';
                } else {
                    alert('Un problème inconnu est survenu avec la requête. (Code : '+xhr2.status+')');
                    document.getElementById('saving').innerHTML = '';
                }
            }
            else {
                try { 
                    document.getElementById('saving').innerHTML = '<span>chargement</span>'; 
                } catch (e) { document.getElementById('saving').innerHTML = ''; }
            }
        };
        xhr2.open("POST","./inc/ajax.inc.php",true);
        xhr2.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
        xhr2.send("a=loadFilmStrip&id="+p+"&gid="+g+"&w="+w);
    };
   
    ajaxReqPhoto();
    ajaxReqFilmstrip();
}
function sendMsg() {
    var n = document.getElementById('nom').value;
    var m = document.getElementById('mel').value;
    var s = document.getElementById('cat').value;
    var b = document.getElementById('msg').value;
    var mf = 0;
    if(n.trim() == '') { 
        document.getElementById('nom').parentNode.className = 'msgf missing';
        mf++;
    } else {
        document.getElementById('nom').parentNode.className = 'msgf';
    }
    if(m.trim() == '') {
        document.getElementById('mel').parentNode.className = 'msgf missing';
        mf++;
    } else {
        document.getElementById('mel').parentNode.className = 'msgf';
    }
    if(b.trim() == '') { 
        document.getElementById('msg').parentNode.className = 'msgf missing';
        mf++;
    } else {
        document.getElementById('msg').parentNode.className = 'msgf';
    }
    function ajaxReq() {
        var xhr = createXHR();
        xhr.onreadystatechange = function() { 
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    try { 
                        document.getElementById('message').innerHTML = (xhr.responseText);
                        document.getElementById('saving').innerHTML = '';
                    } catch (e) { document.getElementById('saving').innerHTML = ''; }
                } else if (xhr.status == 404) {
                    document.getElementById('saving').innerHTML = '';
                    alert('Le contenu n\'est pas accessible (404).');
                } else {
                    document.getElementById('saving').innerHTML = '';
                    alert('Un problème inconnu est survenu avec la requête. (Code : '+xhr.status+')');
                }
            }
            else {
                try { document.getElementById('saving').innerHTML = '<span>Envoie...</span>'; } catch (e) {document.getElementById('saving').innerHTML = '';}
            }
        };
        xhr.open("POST","./inc/ajax.inc.php",true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
        xhr.send("a=sendmsg&n="+n+"&m="+m+"&s="+s+"&b="+b);
    }
    if(mf == 0) { ajaxReq(); } else { alert('Les champs en rouge sont vides.'); }
}


// BASE
function createXHR() 
{
    var httpRequest = false;
    if (window.XMLHttpRequest) {
        httpRequest = new XMLHttpRequest();
        if (httpRequest.overrideMimeType) { httpRequest.overrideMimeType('text/xml'); }
    } 
    else if (window.ActiveXObject) {
        try { httpRequest = new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {
            try { httpRequest = new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
        }
    }
    if (!httpRequest) {
        alert('Abandon :( Impossible de créer une instance XMLHTTP'); return false;
    }
    return httpRequest;
}
