//<script>

function togglePages(areaindex)
{
    var ul = document.getElementById("pages" + areaindex);

    if ((ul.style.display == "none") || (!ul.style.display))
    {
        ul.style.display = "block";
    }
    else
    {
        ul.style.display = "none";
    }
}

//-----------------------------------------------------------------------------
//  Publish all pages (or not)
//-----------------------------------------------------------------------------

function publishAll(checked)
{
    var boxes = document.getElementsByName("pages");

    for (i=0; i < boxes.length; i++)
    {
        boxes[i].checked = checked;
    }
}

function unsetAll (checked)
{
    if (!checked)
    {
        document.getElementById("allpages").checked = false;
    }
}

//-----------------------------------------------------------------------------
//  Select All Areas
//-----------------------------------------------------------------------------

function selectAllAreas(checked)
{
    var boxes = document.getElementsByName("userareas");

    for (i=0; i < boxes.length; i++)
    {
        boxes[i].checked = checked;
    }
}

function unsetAllAreas (checked)
{
    var boxes;
    var checkcount = 0;

    if (checked)
    {
        boxes = document.getElementsByName("userareas");

        for (i=0; i < boxes.length; i++)
        {
            if (boxes[i].checked)
            {
                checkcount++;
            }
        }

        if (checkcount == boxes.length)
        {
            document.getElementById("allareas").checked = true;
        }
        else
        {
            document.getElementById("allareas").checked = false;
        }
    }
    else
    {
        document.getElementById("allareas").checked = false;
    }
}

function protectAllAreas(checked)
{
    document.getElementById("allareas").checked = checked;
    document.getElementById("allareas").disabled = checked;

    var boxes = document.getElementsByName("userareas");

    for (i=0; i < boxes.length; i++)
    {
        boxes[i].checked = checked;
        boxes[i].disabled = checked;
    }
}

//-----------------------------------------------------------------------------
//  Section Drag + Drop Functions
//-----------------------------------------------------------------------------

var resetCursorAllowed = true;
var movingsection = false;
var startZone;
var startPosition;
var scrolling;

function setMoveCursor()
{
    document.body.style.cursor = "move";
    resetCursorAllowed = false;
}

function resetCursor()
{
    resetCursorAllowed = true;
    if (!movingsection)
    {
        document.body.style.cursor = 'default';
    }
}

function startMove(zone, id)
{
    startZone = zone;
    startPosition = id;
    movingsection=true;
    document.onselectstart = "return false;";
    document.onmousemove = function()
    {
        if (event.clientY < 50)
        {
            if (!scrolling)
            {
                scrolling = setInterval("scrollUp();", 20);
            }
            return;
        }

        if (event.clientY > document.body.clientHeight - 10)
        {
            if (!scrolling)
            {
                scrolling = setInterval("scrollDown();", 20);
            }
            return;
        }

        if (scrolling)
        {
            clearInterval(scrolling);
            scrolling = "";
        }
    }
}

function scrollUp()
{
    window.scrollBy(0,-10);

    if (window.pageYOffset < 1)
    {
        clearInterval(scrolling);
        scrolling = "";
    }
}

function scrollDown()
{
    window.scrollBy(0,10);
}

function stopMove()
{
    movingsection = false;
    document.onmousemove = function(){};
}

function addResetCursor()
{
    document.onmouseup = function()
    {
        if (resetCursorAllowed)
        {
            document.body.style.cursor = 'default';
            movingsection = false;
            document.onmousemove = function(){};
        }
    };
}

function checkMove(row, zone, endPosition)
{
    if (movingsection)
    {
        if (zone != startZone)
        {
            row.style.backgroundColor = "blue";
        }
        else if ((endPosition != startPosition) && (endPosition != startPosition + 1))
        {
            row.style.backgroundColor = "blue";
        }
    }
}

function removeHighlight(row)
{
    row.style.backgroundColor = "";
}

function moveSection(row, page, zone, endPosition)
{
    if (movingsection)
    {
        if (zone != startZone)
        {
            document.location.href = "movesection.pl?page=" + page + "&fromzone=" + startZone + "&start=" + startPosition + "&tozone=" + zone + "&end=" + endPosition;
        }
        else if ((endPosition != startPosition) && (endPosition != startPosition + 1))
        {
            document.location.href = "movesection.pl?page=" + page + "&fromzone=" + startZone + "&start=" + startPosition + "&tozone=" + zone + "&end=" + endPosition;
        }
    }
}

//-----------------------------------------------------------------------------
//  confirmAction
//-----------------------------------------------------------------------------

function confirmAction(question, link)
{
    var OK = confirm(question);

    if (OK)
    {
        window.location = link;
    }
}

//-----------------------------------------------------------------------------
//  checkDeleteArea
//-----------------------------------------------------------------------------

function checkDeleteArea(area, link)
{
    var OK = confirm('Are you sure you want to delete this area?\n' + area);

    if (OK)
    {
        window.location = link;
    }
}

//-----------------------------------------------------------------------------
//  checkDeleteImage
//-----------------------------------------------------------------------------

function checkDeleteImage(gallery, image, caption, start)
{
    var OK = confirm('Are you sure you want to delete this photo?\n' + caption);

    if (OK)
    {
        window.location = "deleteimage.pl?gallery=" + gallery + "&image=" + image + "&start=" + start;
    }
}

//-----------------------------------------------------------------------------
//  checkDeletePage
//-----------------------------------------------------------------------------

function checkDeletePage(page, link)
{
    var OK = confirm('Are you sure you want to move "' + page +  '" to the Recycle Bin? ');

    if (OK)
    {
        window.location = link;
    }
}

//-----------------------------------------------------------------------------
//  checkDeleteSection
//-----------------------------------------------------------------------------

function checkDeleteSection(page, zone, section, title)
{
    var OK = confirm('Are you sure you want to delete this section?\n' + title);

    if (OK)
    {
        window.location = "deletesection.pl?page=" + page + "&zone=" + zone + "&id=" + section;
    }
}

//-----------------------------------------------------------------------------
//  checkDeleteMenu
//-----------------------------------------------------------------------------

function checkDeleteMenu(link)
{
    var OK = confirm('Are you sure you want to delete this menu?');

    if (OK)
    {
        window.location = link;
    }
}

//-----------------------------------------------------------------------------
//  checkDeleteUser
//-----------------------------------------------------------------------------

function checkDeleteUser(user, link)
{
    var OK = confirm('Are you sure you want to delete this user?\n' + user);

    if (OK)
    {
        window.location = link;
    }
}

//-----------------------------------------------------------------------------
//  checkPublishGallery
//-----------------------------------------------------------------------------

function checkPublishGallery(gallery, name)
{
    var OK = confirm('Are you sure you want to publish this gallery?\n' + name);

    if (OK)
    {
        window.location = "publishgallery.pl?gallery=" + gallery;
    }
}

//-----------------------------------------------------------------------------
//  checkDeleteGallery
//-----------------------------------------------------------------------------

function checkDeleteGallery(gallery, link)
{
    var OK = confirm('Are you sure you want to move this gallery to the Recycle Bin?\n' + gallery);

    if (OK)
    {
        window.location = link;
    }
}





//-----------------------------------------------------------------------------
//  toggleDir
//-----------------------------------------------------------------------------

function toggleDir(dir)
{
    var div = document.getElementById(dir);

    if (div.style.display == "none")
    {
        div.style.display = "block";
    }
    else
    {
        div.style.display = "none";
    }
}

//</script>
