/**
 * js/maps.js
 * This script is part of www.siedler-maps.de.
 *
 * This functions are common used for the map modules.
 */

/**
 * (last update: 10/08/2006)
 * Opens a window that shows a screenshot of the map in full size.
 *
 * @param modname The name of the module.
 * @param mapid The ID of the image.
 */
function maps_show_image(modname, imageid)
{
    // Build the url of the window
    var url = site_url + "/" + modname + "/image-" + imageid + ".htm" + sid_1st;

    window.open(url, "showimage", "dependent=yes,location=no,menubar=no,status=yes,resizable=yes,scrollbars=yes,width=500,height=300");

    // Don't remove this return!
    // This function is used in HTML-Links and an empty window would be opened without this.
    return;
}

/**
 * (last update: 10/08/2006)
 * Opens a window that shows the maplog of the map.
 *
 * @param modname The name of the module.
 * @param mapid The ID of the map.
 */
function maps_show_maplog(modname, mapid)
{
    // Build the url of the window
    var url = site_url + "/" + modname + "/maplog-" + mapid + ".htm" + sid_1st;

    window.open(url, "showimage", "dependent=yes,location=no,menubar=no,status=yes,resizable=yes,scrollbars=yes,width=600,height=400");
    return;
}

/**
 * (last update: 10/08/2006)
 * Shows the installer applet.
 *
 * @param modname The name of the module.
 * @param mapid The ID of the map.
 */
function maps_install_map(mapid)
{
    // Build the url of the window
    var url = site_url + "/s6maps.php?action=install&mapid=" + mapid + sid_1st;

    window.open(url, "showimage", "dependent=yes,location=no,menubar=no,status=yes,resizable=yes,scrollbars=yes,width=450,height=500");
    return;
}

/**
 * (last update: 10/08/2006)
 * Simply displays the comment form in the mapview or hides it.
 */
var maps_is_comment_form_visible = false;
function maps_toggle_comment_form()
{
    maps_is_comment_form_visible = !maps_is_comment_form_visible;

    var display = "none";

    if (maps_is_comment_form_visible)
    {
        display = "block";
    }

    document.getElementById("commentform").style.display = display;
}

/**
 * (last update: 10/08/2006)
 * This is the script for the jump menü at the bottom of the mapview.
 *
 * @param modname The name of the module.
 * @param mapid The ID of the map.
 * @param action The action to do.
 */
function maps_goto(modname, mapid, action)
{
    // If action is "0": Nothing to do
    if (action == "0")
    {
        return;
    }

    // Delete comments is spacial: simply submit the form on this page
    if (action == "deletecomment")
    {
        document.forms["admin"].submit();
    }
    // In all other cases go to the page for this action
    else
    {
        window.location.href = modname + ".php?action=" + action + "&mapid=" + mapid + sid_2nd;
    }
}

/**
 * (last update: 23/08/2006)
 * Opens a new window with the upload form for map srceenshots.
 * Used with upload and edit action.
 *
 * @param modname The name of the module.
 * @param mapid The ID of the map.
 * @param time Timestamp
 * @param idhash ID-Hash to identify the map and the user
 */
function maps_add_screenshots(modname, mapid, time, idhash)
{
    var url = modname + ".php?action=addimages&mapid=" + mapid + "&time=" + time + "&idhash=" + idhash + sid_2nd;

    window.open(url, "addscreenshots", "dependent=yes,location=no,menubar=no,status=yes,resizable=yes,scrollbars=yes,width=600,height=300");
}

/**
 * (last update: 25/08/2006)
 * Disables the upload button in an upload form.
 *
 * @parem form The parent form object of the button.
 * @param value The text that is shown after the button was clicked.
 */
function maps_disable_upload_button(form, value)
{
    form.submitbutton.disabled = true;
    form.submitbutton.value = value;
}

/**
 * (last update: 25/08/2006)
 * Checks some important user inputs. Returns true and disables the submitbutton
 * on success. Shows an error message on failure and returns false.
 *
 * @param form The form object with the users input.
 */
function maps_check_userinput(form)
{
    // No mapfile
    if (form.mapfile.value == "")
    {
        alert(lng_AlertMapFile);
        return false;
    }
    // Invalid category
    else if (form.catid.value == -1)
    {
        alert(lng_AlertCategory);
        return false;
    }
    // Everything's ok
    else
    {
        maps_disable_upload_button(form, lng_ButtonText);
        return true;
    }
}
