function startUpload()
{
    //document.getElementById('f1_upload_process').style.visibility = 'visible';
    //document.getElementById('f1_upload_form').style.visibility = 'hidden';
    logMsg("Uploading Mapit file - begin", "info", "mapit.js (startUpload)");

    return true;
}

function stopUpload(success)
{
    var result = "";
    if (success == 1)
    {
        logMsg("Uploading Mapit file - successful", "info", "mapit.js (stopUpload)");

        result = "<span class='msg'>The file was uploaded successfully!<\/span><br/><br/>";

        // Update the legend and map.
        var strExcludedLayers = getExcludedLayers().join(",");
        showLegend(document.getElementById("divLegend"), numMapID, objMap.getScale());
        refreshMap(numMapID, strExcludedLayers, true);

        // Hide the file browse field.
        document.getElementById("rowMapitFile").style.display = "none";

        // Hide the upload button.
        document.getElementById("btnUpload").style.display = "none";

        // Show the update and clear buttons.
        document.getElementById("divMapitButtons").style.display = "";

        closeMapitPopUp();
    }
    else
    {
        logMsg("Uploading Mapit file - unsuccessful", "info", "mapit.js (stopUpload)");
        result = "<span class='emsg'>There was an error during file upload!<\/span><br/><br/>";
    }

    logMsg("Uploading Mapit file - end, status: " + success, "info", "mapit.js (stopUpload)");

    return true;
}

function showMapitForm(pStrPopUpTitle)
{
    var strUrl = "./jsp/showMapitForm.jsp";
    var strUrlParams = "lang=" + strLang;

    // Create a new XML HTTP object for AJAX operation.
    var oTempXmlHttp = getNewXmlHTTPObject();

    // Process the AJAX request.
    if (!isXmlHTTPNull(oTempXmlHttp))
        doXMLRequest(oTempXmlHttp, strUrl, strUrlParams, "post", false, "application/x-www-form-urlencoded");

    // Show the popup div.
    var objDivMapitPopUp = createDivPopUp(pStrPopUpTitle, oTempXmlHttp.responseText, "divMapitPopUp", "popUpDefaultModal popUpMapit", true, true);

    // Size the popup window to it contents.
    sizeDivPopUpToContent(objDivMapitPopUp);

    oTempXmlHttp = null;

    // Position the div.
    centerObjectWithin(objDivMapitPopUp, (document.body || window));

    getMapitInfo();
}

function getMapitInfo()
{
    // Check to see if there is a Mapit layer that is loaded. If not, exit this procedure.
    if ((document.getElementById("tblRowMapit") == null) || (document.getElementById("tblRowMapit") == undefined))
    {
        // Show the file browse field.
        document.getElementById("rowMapitFile").style.display = "";

        // Show the upload button.
        document.getElementById("btnUpload").style.display = "";

        // Hide the update and clear buttons.
        document.getElementById("divMapitButtons").style.display = "none";

        // Hide/show the appropriate help message.
        document.getElementById("rowMsgLoad").style.display = "";
        document.getElementById("rowMsgUpdate").style.display = "none";

        return;
    }
    else
    {
        // Hide the file browse field.
        document.getElementById("rowMapitFile").style.display = "none";

        // Hide the upload button.
        document.getElementById("btnUpload").style.display = "none";

        // Show the update and clear buttons.
        document.getElementById("divMapitButtons").style.display = "";

        // Hide/show the appropriate help message.
        document.getElementById("rowMsgLoad").style.display = "none";
        document.getElementById("rowMsgUpdate").style.display = "";
    }

    var strUrl = "./jsp/showMapit.jsp";

    // Create a new XML HTTP object for AJAX operation.
    var oTempXmlHttp = getNewXmlHTTPObject();

    // Process the AJAX request.
    if (!isXmlHTTPNull(oTempXmlHttp))
    {
        logMsg("Retrieving Mapit data. url: " + encodeURIComponent(strUrl + "?" + strUrlParams), "info", "mapit.js (getMapitInfo)");
        doXMLRequest(oTempXmlHttp, strUrl, null, "post", false, "application/x-www-form-urlencoded");
    }

    // Get the Mapit information.
    var objXMLMapitInfo = oTempXmlHttp.responseXML.getElementsByTagName("mapit")[0];

    oTempXmlHttp = null;

    // Set the title.
    document.getElementById("txtLayerName").value = objXMLMapitInfo.getElementsByTagName("title")[0].childNodes[0].nodeValue;

    // Set the color.
    var intCounter = 0;
    while ((document.getElementById("selMapitColor").options[intCounter].value != objXMLMapitInfo.getElementsByTagName("color")[0].childNodes[0].nodeValue) && (intCounter < document.getElementById("selMapitColor").options.length))
        intCounter ++;

    if (intCounter < document.getElementById("selMapitColor").options.length)
        document.getElementById("selMapitColor").selectedIndex = intCounter;

    // Set the symbol.
    intCounter = 0;
    while ((document.getElementById("selMapitIcon").options[intCounter].value != objXMLMapitInfo.getElementsByTagName("symbol")[0].childNodes[0].nodeValue) && (intCounter < document.getElementById("selMapitIcon").options.length))
        intCounter ++;

    if (intCounter < document.getElementById("selMapitIcon").options.length)
        document.getElementById("selMapitIcon").selectedIndex = intCounter;
}

function deleteMapit()
{
    var strUrl = "./jsp/processMapit.jsp";
    var strUrlParams = "action=delete&lang=" + strLang;

    // Create a new XML HTTP object for AJAX operation.
    var oTempXmlHttp = getNewXmlHTTPObject();

    // Remove the uploaded Mapit data.
    if (!isXmlHTTPNull(oTempXmlHttp))
    {
        logMsg("Deleting Mapit data. url: " + encodeURIComponent(strUrl + "?" + strUrlParams), "info", "mapit.js (deleteMapit)");
        doXMLRequest(oTempXmlHttp, strUrl, strUrlParams, "post", false, "application/x-www-form-urlencoded");
        logMsg("*** result: " + encodeURI(oTempXmlHttp.responseText), "info", "mapit.js (deleteMapit)");
    }

    if (oTempXmlHttp.responseText == 1)
    {
        document.getElementById("divMapitPopUp").style.display = "none";

        // Show the file browse field.
        document.getElementById("rowMapitFile").style.display = "";

        // Show the upload button.
        document.getElementById("btnUpload").style.display = "";

        // Hide the update and clear buttons.
        document.getElementById("divMapitButtons").style.display = "none";

        document.getElementById("txtLayerName").value = "";

        // Update the legend and map.
        var strExcludedLayers = getExcludedLayers().join(",");
        showLegend(document.getElementById("divLegend"), numMapID, objMap.getScale());
        refreshMap(numMapID, strExcludedLayers, true);

        getMapitInfo();

        closeMapitPopUp();
    }

    oTempXmlHttp = null;
}

function updateMapit()
{
    var strUrl = "./jsp/processMapit.jsp";
    var strUrlParams = "action=update&title=" + document.getElementById("txtLayerName").value + "&color=" + document.getElementById("selMapitColor").options[document.getElementById("selMapitColor").selectedIndex].value + "&symbol=" + document.getElementById("selMapitIcon").options[document.getElementById("selMapitIcon").selectedIndex].value + "&lang=" + strLang;

    // Create a new XML HTTP object for AJAX operation.
    var oTempXmlHttp = getNewXmlHTTPObject();

    // Process the AJAX request.
    if (!isXmlHTTPNull(oTempXmlHttp))
    {
        logMsg("Updating Mapit data. url: " + encodeURIComponent(strUrl + "?" + strUrlParams), "info", "mapit.js (updateMapit)");
        doXMLRequest(oTempXmlHttp, strUrl, strUrlParams, "post", false, "application/x-www-form-urlencoded");
        logMsg("*** result: " + encodeURI(oTempXmlHttp.responseText), "info", "mapit.js (updateMapit)");
    }

    oTempXmlHttp = null;

    // Update the legend and map.
    var strExcludedLayers = getExcludedLayers().join(",");
    showLegend(document.getElementById("divLegend"), numMapID, objMap.getScale());
    refreshMap(numMapID, strExcludedLayers, true);

    closeMapitPopUp();
}

function closeMapitPopUp()
{
    // Remove the div.
    removePopUp("divMapitPopUp");
}
