var objChartArray = new Array();
var objChartArrayDisplay = new Array();
var objChartContentKeyArray = new Array();

function generateReport()
{
    var strReportUrlParams = "";

    // Build a parameter of content keys.
    for (oChartContentKeyIndex in objChartContentKeyArray)
    {
        strReportUrlParams = strReportUrlParams + objChartContentKeyArray[oChartContentKeyIndex];

        if (oChartContentKeyIndex < (objChartContentKeyArray.length - 1))
            strReportUrlParams = strReportUrlParams + ",";
    }

    // Display the report in a separate window.
    window.open("./jsp/reportCharts.jsp" + "?contentKeys=" + strReportUrlParams, "report");
}

function reportChartExtent(pButton)
{
    var strOldButtonText = pButton.value;
    var min = oGMap.getBounds().getSouthWest();
    var max = oGMap.getBounds().getNorthEast();

    // Change the text of the button back.
    changeButtonText(pButton, strPleaseWaitText);
    pButton.disabled = true;    // For IE
    pButton.disabled = "disabled";  // For Firefox

    // If the extent crosses the IDL then set the left extent to -180.
    // This should be changed to accomodate a generic approach (make 2 requests)
    // at some point.
    if (!isValidMinMax(min, max))
    {
        newMax = new GLatLng(max.lat(), min.lng());
        newMin = new GLatLng(min.lat(), -180);
    }

    // Get a list of content keys based on Google Maps extent.
    // Create a new XML HTTP object for AJAX operation.
    var oTempXmlHttp = getNewXmlHTTPObject();

    //Verify which layers are selected NC
    var checkedLayersArray = new Array(6);
    var checkedLayers = "";

    frmLayersDocument = document.getElementById("frmLayers");

    if (frmLayersDocument.chkLayers[0].checked)
        checkedLayersArray[0] = "0";

    if (frmLayersDocument.chkLayers[1].checked)
        checkedLayersArray[1] = "1";

    if (frmLayersDocument.chkLayers[2].checked)
        checkedLayersArray[2] = "2";

    if (frmLayersDocument.chkLayers[3].checked)
        checkedLayersArray[3] = "3";

    if (frmLayersDocument.chkLayers[4].checked)
        checkedLayersArray[4] = "4";

    if (frmLayersDocument.chkLayers[5].checked)
        checkedLayersArray[5] = "5";

    for(ind = 0; ind < checkedLayersArray.length; ind++)
    {
        if (checkedLayersArray[ind] != null)
        {
            if (checkedLayers == "")
                checkedLayers = checkedLayersArray[ind];
            else
                checkedLayers += "," + checkedLayersArray[ind];
        }
    }

    //GLog.write("repositioning ==> centerx=" + oGMap.getCenter().lng() + "&centery=" + oGMap.getCenter().lat() + "&zoom=" + oGMap.getZoom());

    // Call the server side code pull the array of charts in the region.
    if (!isXmlHTTPNull(oTempXmlHttp))
        doXMLRequest(oTempXmlHttp, "./jsp/getChartArray.jsp?bounds=" + min.lat() + "," + min.lng() + "," + max.lat() + "," + max.lng() + "&layers=" + checkedLayers, "post", false, "", "");

    //Format returned results to be able to add them to the chart list on the left.
    var chartArray = oTempXmlHttp.responseText.split(",");
    var objTemp;
    var moreThanOne = false;

    // Set the status of the "Generate Report" and "Clear" button.
    document.getElementById("btnGenerateReport").disabled = (chartArray.length == 0);
    document.getElementById("btnClearMyChartsList").disabled = document.getElementById("btnGenerateReport").disabled;

    var intArrayLength = chartArray.length;

    // Loop through each chart to add.
    for(intCounter = 0; intCounter < intArrayLength; intCounter ++)
    {
        objTemp = eval("v" + chartArray[intCounter]);

        // Check to see if the chart is already within the list (compare content key).
        if (existsInArray(chartArray[intCounter], objChartContentKeyArray))
            moreThanOne = true;
        else
        {
            // Add the chart to the array.
            objChartArray.push(objTemp.chart_number + " - " + objTemp.chart_name);
            objChartArrayDisplay.push(generateChartString(objTemp.chart_number + " - " + objTemp.chart_name, intCounter));
            objChartContentKeyArray.push(chartArray[intCounter]);
        }
    }

    // Refresh the My Charts List.
    refreshChartList()

    // Set the focus to the bottom chart list.
    document.getElementById("divMyChartsListContent").scrollTop = document.getElementById("divMyChartsListContent").scrollHeight;

    if (moreThanOne)
        alert(strMoreThanOneError);

    // Close the chart details.
    closeDivPopup();

    // Change the text of the button back.
    changeButtonText(pButton, strOldButtonText);
    pButton.disabled = false;   // For IE
    pButton.disabled = "";  // For Firefox
}

function clearMyChartsList()
{
    // Erase the array.
    objChartArray.length = 0;
    objChartArrayDisplay.length = 0;
    objChartContentKeyArray.length = 0;

    // Clear the div.
    document.getElementById("divMyChartsListContent").innerHTML = "";

    // Set the status of the "Generate Report" and "Clear" button.
    document.getElementById("btnGenerateReport").disabled = (document.getElementById("divMyChartsListContent").innerHTML == "");
    document.getElementById("btnClearMyChartsList").disabled = document.getElementById("btnGenerateReport").disabled;
}

function addToChartList(pChartInfo, pContentKey)
{
    // Check to see if the chart is already within the list.
    if (existsInArray(pContentKey, objChartContentKeyArray))
    {
        // Notify the user that chart already exists.
        alert(strErrorChartExists);

        return false;
    }
    else
    {
        // Add the chart to the array.
        objChartArray.push(pChartInfo);
        objChartArrayDisplay.push(generateChartString(pChartInfo, objChartArrayDisplay.length));
        objChartContentKeyArray.push(pContentKey);

        // Refresh the My Charts List.
        refreshChartList()

        // Set the focus to the bottom chart list.
        document.getElementById("divMyChartsListContent").scrollTop = document.getElementById("divMyChartsListContent").scrollHeight;

        // Set the status of the "Generate Report" and "Clear" button.
        document.getElementById("btnGenerateReport").disabled = (document.getElementById("divMyChartsListContent").innerHTML == "");
        document.getElementById("btnClearMyChartsList").disabled = document.getElementById("btnGenerateReport").disabled;
    }

    // Close the chart details.
    closeDivPopup();
}

function deleteFromChartList(pIndex)
{
    // Delete the item from the array.
    objChartArray.splice(pIndex, 1);
    objChartArrayDisplay.length = 0;
    objChartContentKeyArray.splice(pIndex, 1);

    var intArrayLength = objChartArray.length;

    // Loop through each chart to update.
    for(intCounter = 0; intCounter < intArrayLength; intCounter ++)
    {
        // Add the chart to the array.
        objChartArrayDisplay.push(generateChartString(objChartArray[intCounter], intCounter));
    }

    // Refresh the My Charts List.
    refreshChartList()

    // Set the status of the "Generate Report" and "Clear" button.
    document.getElementById("btnGenerateReport").disabled = (document.getElementById("divMyChartsListContent").innerHTML == "");
    document.getElementById("btnClearMyChartsList").disabled = document.getElementById("btnGenerateReport").disabled;
}

function refreshChartList()
{
    var divChartList = document.getElementById("divMyChartsListContent");

    // Build the chart list from the array.
    divChartList.innerHTML = objChartArrayDisplay.join("");
}

function generateChartString(pChartInfo, pIndex)
{
    // Build the HTML for each chart.
    return "<div id='divChartList" + pIndex + "' name='divChartList" + pIndex + "'><div class='myChartsListDelete'><img src='images/delete.gif' border='0' align='left' onClick='deleteFromChartList(" + pIndex + ")'></img></div><div class='myChartsListContent'>" + pChartInfo + "</div><div class='clear' style='height: 5px'></div></div>"
}