/*
*   javascript for the gallery admin interface
*/

// AJAX handle
var xmlhttp = null;
// code for Mozilla, etc.
if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest()
}
// code for IE
else if (window.ActiveXObject) {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP")
} 
else {
    alert("Your browser doesn't support AJAX!\nPlease upgrade to one of:\nFirefox, Safari, Opera or Internet Explorer 6 or 7");
}

function refresh_window()
{
    if (xmlhttp.readyState == 4) {
    window.location.reload();
    }
}

function login()
{
    window.open("login.html", "login", "width=350,height=250,menubar=0,scrollbars=1,toolbar=0,resizable=0,location=0");
}

function send_login()
{
    var loginParams = "user=" + document.getElementById('user').value + "&pass=" + document.getElementById('pass').value;
    xmlhttp.open("POST", "login.php");
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.onreadystatechange = login_response;
    xmlhttp.send(loginParams);
}

function login_response()
{
    if (xmlhttp.readyState == 4) {
        response = xmlhttp.responseText;
        if (response == "success") {
            opener.location.reload();
            window.close();
        } else {
            login_error();
        }
    } 
}

function login_error()
{
    document.getElementById('error').innerHTML = "Wrong User Name or Password. <br />Please try again.";
}

function logout()
{
    xmlhttp.open("GET", "logout.php");
    xmlhttp.onreadystatechange = refresh_window;
    xmlhttp.send();
}

function delete_image(img,path) 
{
    if (confirm("Are you sure you want to delete this image?")) {
        var deleteParams = "img=" + img + "&path=" + path;
        xmlhttp.open("POST", "deleteImage.php");
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlhttp.onreadystatechange = handle_file_delete;
        xmlhttp.send(deleteParams);
    }
}

// deals with renaming items
function set_background(element,color)
{   
    document.getElementById(element).style.backgroundColor = color;
}

function show_input(args)
{
    document.getElementById(args.hide).style.display = 'none';
    document.getElementById(args.show).style.display = 'block';
    document.getElementById(args.inputName).disabled = false;
    document.getElementById(args.inputName).focus();
    document.getElementById(args.inputName).select();
}

function hide_input(args)
{
    document.getElementById(args.hide).style.display = 'none';
    document.getElementById(args.show).style.display = 'block';
    document.getElementById(args.inputName).disabled = true;
}

function save_caption(img, path)
{
    xmlhttp.open("POST", "saveCaption.php");
    xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlhttp.onreadystatechange = handle_caption_save;
    xmlhttp.send("img=" + img + "&path=" + path + "&caption=" + document.getElementById('caption_input').value);
}

function handle_caption_save()
{
    if (xmlhttp.readyState == 4) {
        response = xmlhttp.responseText;
        document.getElementById('caption').innerHTML = response;
        document.getElementById('caption_input_p').style.display = 'none';
        document.getElementById('caption').style.display = 'block';
    }
}

function save_gallery_title()
{
    if (document.getElementById('gallery_title_input').value.indexOf("/") != -1) {
         alert("The new gallery name may not contain the '/' character.");
    } else {
        //find out what we're currently calling this gallery, and what directory it's in
        var query = location.search.substring(1);
        var querySplit = query.split("&");
        for (var param in querySplit) {
           if (querySplit[param].substring(0,3) == "gal") {
                var paramSplit = querySplit[param].split("=");
                var oldTitle = paramSplit[1];
            }
        }
        xmlhttp.open("POST", "renameGallery.php");
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlhttp.onreadystatechange = handle_title_save;
        xmlhttp.send("oldTitle=" + oldTitle + "&newTitle=" + document.getElementById('gallery_title_input').value);
    }
}

function handle_title_save()
{
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.responseText == "success") {
            // update url to reflect new gallery name
            var newURL = "./";
            var query = location.search;
            var querySplit = query.split("&");
            for (var query in querySplit) {
                if (querySplit[query].substring(1,4) == "gal") {
                    // this parameter needs to be updated
                    //look for a / in the query
                    if (querySplit[query].indexOf("/") != -1) {
                        // put the new gallery name after the /
                        querySplit[query] = querySplit[query].substring(0, querySplit[query].indexOf("/")) + "/" + document.getElementById('gallery_title_input').value;
                    } else {
                        querySplit[query] = querySplit[query].substring(0, querySplit[query].indexOf("=")) + "=" + document.getElementById('gallery_title_input').value;
                    }
                }
                // add the parameter to the url
                if (newURL == "./") {
                    newURL += querySplit[query];
                } else {
                    newURL += "&" + querySplit[query];
                }
            }
            location.replace(newURL);
        } else {
            alert("Error changing the gallery name");
        }
    }
}

function delete_gallery(gallery)
{
    if (confirm("Are you sure you want to delete this gallery?")) {
        xmlhttp.open("POST", "deleteGallery.php");
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlhttp.onreadystatechange = handle_gallery_delete;
        xmlhttp.send("gallery=" + gallery);
    }
}

function handle_gallery_delete()
{
    if (xmlhttp.readyState == 4) {
        if (xmlhttp.responseText == "success") {
            location.replace("./");
        } else {
            alert ("Error in gallery deletion");
        }
    }
}