//******************************************************************************************************************
//
//
// USGS_Download_List.js
//
// Installation:
//   01.  Copy USGS_Download_List.js and USGS_OGC_Download_Layers.js into the javascript directory of the HTML Viewer
//
//   02.  Add the following lines to MapFrame.htm 
//	  <script type="text/javascript" language="JavaScript" src="javascript/USGS_Download_List.js"></script>
//	  <script type="text/javascript" language="JavaScript" src="javascript/USGS_OGC_Download_Layers.js"></script>
//
//   03.  Copy the download.gif, display.gif and tabspacer.gif into the images folder.
//
//   04. Remove the following lines from toc.htm
//        
//    //*********************************************************************
//    //*			  START of Code to Add USGS Combined Layer List               *
//    //*********************************************************************
//    //Draw the Combined Layer list - pass in the current page also...
//    if (parent.MapFrame.USGS_OGC_Loaded) { document.writeln(parent.MapFrame.getOGCTOC(window.location.pathname)); }
//    //if (parent.MapFrame.USGS_OGC_Loaded) { document.writeln(parent.MapFrame.simpleOGCTOC()); }
//
//   05. Add the following lines to toc.htm
//
//    if (parent.MapFrame.UseDownloadList) {
//	if (parent.MapFrame.ShowingLayerList) {
//	    document.writeln(parent.MapFrame.getOGCLayerList(window.location.pathname));
//	} else {
//	    document.writeln(parent.MapFrame.getOGCDownloadList(window.location.pathname));
//	}
//    } else {
//	if (parent.MapFrame.USGS_OGC_Loaded) { 
//            document.writeln(parent.MapFrame.getOGCTOC(window.location.pathname)); 
//        }
//    }
//
//   06. Add the following line to MapFrame.htm in the section near the top 
//       entitled "START of Code to Add USGS JavaScripts"
//
//       var Download_USGS_Group_Loaded=false;
//
//******************************************************************************************************************
//
var Download_Category_Order = new Array();
var Download_Category_Layer_Order = new Array();
var Download_Category_Expanded = new Array();

var Download_SRC_Layer_Name = new Array();
var Download_SRC_Layer_Icon_URL = new Array();
var Download_SRC_Layer_Documentation_URL = new Array();
var Download_SRC_Layer_Metadata_URL = new Array();
var Download_SRC_Layer_Max_Scale = new Array();
var Download_SRC_Layer_Min_Scale = new Array();
var Download_SRC_Layer_Checked = new Array();
var Download_SRC_Layer_TheLayer = new Array();
var Download_SRC_Layer_Weights = new Array();
var Download_SRC_Layer_Extent_Right = new Array();
var Download_SRC_Layer_Extent_Left = new Array();
var Download_SRC_Layer_Extent_Bottom = new Array();
var Download_SRC_Layer_Extent_Top = new Array();

var UseDownloadList = true;
var ShowingLayerList = true;
var TOC_DL_Hash   = '';
var DL_TempArray  = new Array();
//var mf = parent.MapFrame;
var THROTTLE_MAX_DEC_DEG = 30;
//Include Layers that are out of scale for the current view in the layer list
//This is also used for Bad Extent - Layers that are outside the current view extent
//var Include_DL_Bad_Scale = false;
var check_Layer_Download_Extent_and_Scale = false;
//var Download_SRC_Layer_Checked = false;


function displaylayers() {
    parent.MapFrame.ShowingLayerList = true;
//        parent.TOCFrame.document.writeln(parent.MapFrame.Vis_ID_Style(parent.USGSBackgroundColor,parent.USGSTextColor,parent.USGSTextColor,parent.USGSBanner));
//        parent.TOCFrame.document.writeln('<STYLE type = "text/css"><!-- td { color: ' + parent.USGSTextColor + '; } --></STYLE>');

		 parent.TOCFrame.document.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
		 parent.TOCFrame.document.writeln('<LINK REL="stylesheet" HREF="/website/includes/webmapping.css" TYPE="text/css">');
	     	 parent.TOCFrame.document.writeln('<style type="text/css">');
		 parent.TOCFrame.document.writeln('body { font-family: Verdana, Arial, Helvetica, sans-serif; margin-left: 5px; margin-right: 5px; color:' +parent.USGSTextColor+'; background-color: '+parent.USGSBackgroundColor+'; background-repeat: no-repeat; font-size: 10pt;}');
		 parent.TOCFrame.document.writeln('a { text-decoration: none; color: '+parent.USGSTextColor+'; background-color: '+parent.USGSBackgroundColor+';}');
		 parent.TOCFrame.document.writeln('a:hover { text-decoration: underline; color: '+parent.USGSTextColor+'; background-color: '+parent.USGSBackgroundColor+'}');
		 parent.TOCFrame.document.writeln('a.visited {font-family:  Verdana, Arial, Helvetica, sans-serif; color: '+parent.USGSTextColor+'; background-color: '+parent.USGSBackgroundColor+'; }');
		 parent.TOCFrame.document.writeln('th {  background-color:  '+parent.USGSBackgroundColor+';}');
		 parent.TOCFrame.document.writeln('td {  background-color: '+parent.USGSBackgroundColor+'; color: '+parent.USGSTextColor+';}');
		 parent.TOCFrame.document.writeln('iframe { background-color: #FFFFFF; }');
		 parent.TOCFrame.document.writeln('hr {background-color: #FFFFFF; color: '+parent.USGSTextColor+'; line-height: 4px; border-style: none; height: 4px; margin: 2px;}');
		 parent.TOCFrame.document.writeln('.link4main {color: '+parent.USGSTextColor+'; ! important; FONT-FAMILY: Verdana,Arial,Helvetica; FONT-SIZE: 14px;}');
		 parent.TOCFrame.document.writeln('.c2 {  color: '+parent.USGSTextColor+'; ! important; FONT-FAMILY: "Univers Condensed", Verdana,Arial,Helvetica;    FONT-WEIGHT: bold;	TEXT-DECORATION:none;	font-style : normal;	FONT-SIZE: 18px;	}');
		 parent.TOCFrame.document.writeln('.linkfoot	{    color: '+parent.USGSTextColor+'; ! important;    FONT-FAMILY: Verdana,Arial,Helvetica;    FONT-SIZE: 12px;	TEXT-DECORATION:underline; !important;	}');
		 parent.TOCFrame.document.writeln('.link	{    color: '+parent.USGSTextColor+'; ! important;    FONT-FAMILY: Verdana,Arial,Helvetica;    FONT-SIZE: 12px;	TEXT-DECORATION:underline; !important;	}');
		 parent.TOCFrame.document.writeln('.tnm {	color: '+parent.USGSTextColor+'; ! important; 	FONT-FAMILY: "Univers Condensed", Verdana,Arial,Helvetica;    FONT-WEIGHT: bold;	TEXT-DECORATION:none;	font-style : italic !important;	fonst-size: 14px;}');
		 parent.TOCFrame.document.writeln('.small {color: '+parent.USGSTextColor+'; FONT-FAMILY: Verdana,Arial,Helvetica; FONT-SIZE: 7px; TEXT-DECORATION:none; !important;}');
		 parent.TOCFrame.document.writeln('</style>');

        if (parent.MapFrame.UseScaleTool) { parent.TOCFrame.document.writeln(parent.MapFrame.DrawScaleTool()); }
        parent.TOCFrame.document.writeln(parent.MapFrame.getOGCLayerList(parent.TOCFrame.location.pathname));
        parent.TOCFrame.document.close();
    }

function downloadlayers() {
    parent.MapFrame.ShowingLayerList = false;
  //      parent.TOCFrame.document.writeln(parent.MapFrame.Vis_ID_Style(parent.USGSBackgroundColor,parent.USGSTextColor,parent.USGSTextColor,parent.USGSBanner));
  //      parent.TOCFrame.document.writeln('<STYLE type = "text/css"><!-- td { color: ' + parent.USGSTextColor + '; } --></STYLE>');
		 parent.TOCFrame.document.writeln('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">');
		 parent.TOCFrame.document.writeln('<LINK REL="stylesheet" HREF="/website/includes/webmapping.css" TYPE="text/css">');
		 parent.TOCFrame.document.writeln('<style type="text/css">');
		 parent.TOCFrame.document.writeln('body { font-family: Verdana, Arial, Helvetica, sans-serif; margin-left: 5px; margin-right: 5px; color:' +parent.USGSTextColor+'; background-color: '+parent.USGSBackgroundColor+'; background-repeat: no-repeat; font-size: 10pt;}');
		 parent.TOCFrame.document.writeln('a { text-decoration: none; color: '+parent.USGSTextColor+'; background-color: '+parent.USGSBackgroundColor+';}');
		 parent.TOCFrame.document.writeln('a:hover { text-decoration: underline; color: '+parent.USGSTextColor+'; background-color: '+parent.USGSBackgroundColor+'}');
		 parent.TOCFrame.document.writeln('a.visited { color: '+parent.USGSTextColor+'; background-color: '+parent.USGSBackgroundColor+'; }');
		 parent.TOCFrame.document.writeln('th {  background-color:  '+parent.USGSBackgroundColor+';}');
		 parent.TOCFrame.document.writeln('td {  background-color: '+parent.USGSBackgroundColor+'; color: '+parent.USGSTextColor+';}');
		 parent.TOCFrame.document.writeln('iframe { background-color: #FFFFFF; }');
		 parent.TOCFrame.document.writeln('hr {background-color: #FFFFFF; color: '+parent.USGSTextColor+'; line-height: 4px; border-style: none; height: 4px; margin: 2px;}');
		 parent.TOCFrame.document.writeln('.link4main {color: '+parent.USGSTextColor+'; ! important; FONT-FAMILY: Verdana,Arial,Helvetica; FONT-SIZE: 14px;}');
		 parent.TOCFrame.document.writeln('.c2 {  color: '+parent.USGSTextColor+'; ! important; FONT-FAMILY: "Univers Condensed", Verdana,Arial,Helvetica;    FONT-WEIGHT: bold;	TEXT-DECORATION:none;	font-style : normal;	FONT-SIZE: 18px;	}');
		 parent.TOCFrame.document.writeln('.linkfoot	{    color: '+parent.USGSTextColor+'; ! important;    FONT-FAMILY: Verdana,Arial,Helvetica;    FONT-SIZE: 12px;	TEXT-DECORATION:underline; !important;	}');
		 parent.TOCFrame.document.writeln('.link	{    color: '+parent.USGSTextColor+'; ! important;    FONT-FAMILY: Verdana,Arial,Helvetica;    FONT-SIZE: 12px;	TEXT-DECORATION:underline; !important;	}');
		 parent.TOCFrame.document.writeln('.tnm {	color: '+parent.USGSTextColor+'; ! important; 	FONT-FAMILY: "Univers Condensed", Verdana,Arial,Helvetica;    FONT-WEIGHT: bold;	TEXT-DECORATION:none;	font-style : italic !important;	fonst-size: 14px;}');
		 parent.TOCFrame.document.writeln('.small {color: '+parent.USGSTextColor+'; FONT-FAMILY: Verdana,Arial,Helvetica; FONT-SIZE: 7px; TEXT-DECORATION:none; !important;}');
		 parent.TOCFrame.document.writeln('</style>');

        if (parent.MapFrame.UseScaleTool) { parent.TOCFrame.document.writeln(parent.MapFrame.DrawScaleTool()); }
        parent.TOCFrame.document.writeln(parent.MapFrame.getOGCDownloadList(parent.TOCFrame.location.pathname));
        parent.TOCFrame.document.close();
	
        }

//function test(tempID) {
//        alert(parent.MapFrame.Download_SRC_Layer_Checked[tempID]);
//}

function chkboxvalue(tempID) {
//alert(tempID);
        if (parent.MapFrame.Download_SRC_Layer_Checked[tempID]==true) {
                parent.MapFrame.Download_SRC_Layer_Checked[tempID]=false;
        }else {
                parent.MapFrame.Download_SRC_Layer_Checked[tempID]=true;
        }       
                //parent.MapFrame.test(tempID);

	updateMaxOrderArea();
}

function updateMaxOrderArea() {
    var LayerID = '';
    var weights = 0;

	for (var m=0;m<parent.MapFrame.Download_Category_Order.length;m++) {
            if (Download_Category_Layer_Order[m].length>0) {  // will always be true
                DL_TempArray = parent.MapFrame.Download_Category_Layer_Order[m].split(',');

                //Cycle through the layers from the Download_Category_Layer_Order which has been split into TempArray
                for (var n=0;n<DL_TempArray.length;n++) {
			LayerID = DL_TempArray[n];

               		if (parent.MapFrame.Download_SRC_Layer_Checked[LayerID]) weights = weights + parent.MapFrame.Download_SRC_Layer_Weights[LayerID];
                }    
            }

	}
	//alert("weights" + weights);
	parent.MapFrame.MBperSqDeg = weights;
}

//get download layer list
function getDownloadLayerList() {
	var LayerID = '';
	var theLayers = ''

	for (var i=0;i<parent.MapFrame.Download_Category_Order.length;i++) {
            if (Download_Category_Layer_Order[i].length>0) {  // will always be true
                DL_TempArray = parent.MapFrame.Download_Category_Layer_Order[i].split(',');

                //Cycle through the layers from the Download_Category_Layer_Order which has been split into TempArray
                for (var j=0;j<DL_TempArray.length;j++) {
			LayerID = DL_TempArray[j];

               		if (parent.MapFrame.Download_SRC_Layer_Checked[LayerID]) theLayers = theLayers + Download_SRC_Layer_TheLayer[LayerID] + ",";
			//alert("theLayers" + theLayers);
                }    
            }

	}
	
	return theLayers;
}
 

//*****************************************************************************
// Function getOGCDownloadList
// This function will generate HTML code to draw the OGC Download list with the
// categories expanded/collapsed and the Layer classification, if shown.
// It returns the code as an HTML table to do indenting properly.  The 
// Category expand/collapse functions, do reload the window in order to 
// get the client to call this function again and redraw the window.
// page2Load is used for the expanding/collapsing of categories.  It must 
// specify the page that called the function.  This can be done by passing
// window.location.pathname in the call to this function.  
//*****************************************************************************
function getOGCDownloadList(page2Load) {
    //alert("entering getOGCDownloadList");
/*                                            //If using group layers reset the display counter variables
    if (USGS_Group_Loaded) { 
        IVM_OGC_Reset_Display(); 
    }
  
*/                                            //Initialize the String to return with the table header
    var LocalString = '';
    if (Show_TOC_Title) {
        LocalString += ('<div align="left" class="title_label">&nbsp;Layers</div>');
        LocalString += ('<img src="/website/ImageArchive/secondary/tabspacer.gif">');
		if (parent.UseDownload=='Y') {
	        //LocalString += ('<img src="/website/ImageArchive/secondary/download.gif" usemap="#tabs" border=0 /><map name="tabs" id="tabs"><area shape="poly" coords="6,1,1,6,1,21,74,21,74,5,70,1" href="#" onClick="parent.MapFrame.displaylayers();" title="show display layer categories" /><area shape="poly" coords="78,1,74,5,74,21,147,21,147,6,142,1" href="#" onClick="parent.MapFrame.downloadlayers();" alt="show download layer categories"/></map>');
		LocalString += ('<img src="/website/ImageArchive/secondary/download.gif" usemap="#tabs" border=0 /><map name="tabs" id="tabs"><area shape="poly" coords="6,1,1,6,1,21,74,21,74,5,70,1" href="' + page2Load + '" onClick="parent.MapFrame.displaylayers();" title="show display layer categories" /><area shape="poly" coords="78,1,74,5,74,21,147,21,147,6,142,1"  href="' + page2Load + '"  onClick="parent.MapFrame.downloadlayers();return;" alt="show download layer categories"/></map>');
	        //LocalString += ('<img src="/website/ImageArchive/secondary/download.gif" usemap="#tabs" border=0 /><map name="tabs" id="tabs"><area shape="poly" coords="6,1,1,6,1,21,74,21,74,5,70,1" href="javascript: void(0);" onClick="parent.MapFrame.displaylayers();" title="show display layer categories" /><area shape="poly" coords="78,1,74,5,74,21,147,21,147,6,142,1" href="javascript: void(0);" onClick="parent.MapFrame.downloadlayers();" alt="show download layer categories"/></map>');

		} 
    }
    LocalString += '<form name="OGCDownloadlist"><table border="0" cellpadding="0" cellspacing="0" summary="Categorized list Download layers">';

                                            //Get the Current Scale from USGS Scale Tool
    var CenterAdjust = Math.abs(eRight - eLeft);
    var CurrentMapScale = HaversineScale((eLeft + CenterAdjust), eBottom, (eLeft + CenterAdjust), eTop, mHeight, ScaleToolDPI);
    
                                            //Cycle through the Categories array
    for (var i=0;i<parent.MapFrame.Download_Category_Order.length;i++) {
                                            //First show the Category Expanded/Collapsed Icon
        LocalString += ('<tr><td valign=top align=right><a name="C' + i + '"></a><a href="' + page2Load + '"');
        if (parent.MapFrame.Download_Category_Expanded[i]) {
            LocalString += (' onclick="parent.MapFrame.Download_Category_Expanded[' + i + ']=false;parent.MapFrame.TOC_DL_Hash=\'C' + i + '\';" title="Collapse Category (Hide Layer List for this category)">');
            LocalString += ('<img src="' + parent.MapFrame.Category_Expanded_Image + '" alt="Collapse Category (Hide Layer List for this category)" border="0" height="18" width="18" valign="middle" />');
        } else {
            LocalString += (' onclick="parent.MapFrame.Download_Category_Expanded[' + i + ']=true;parent.MapFrame.TOC_DL_Hash=\'C' + i + '\';" title="Expand Category (Show Layer List for this category)">');
            LocalString += ('<img src="' + parent.MapFrame.Category_Collapsed_Image + '" alt="Expand Category (Show Layer List for this category)" border="0" height="18" width="18" valign="middle" />');
        }
        LocalString += ('</a></td>');
         
                                            //Now show the Category Name
        LocalString += ('<td colspan="2" valign="top" align="left"><strong>' + parent.MapFrame.Download_Category_Order[i] + '</strong></td></tr>');
                                            //If the Category is Expanded, then we need to draw out the Layer names under that Category.
        if (parent.MapFrame.Download_Category_Expanded[i]) {
            //alert("now in expanded");
            if (Download_Category_Layer_Order[i].length>0) {  // will always be true
                DL_TempArray = parent.MapFrame.Download_Category_Layer_Order[i].split(',');
        //Do the check for Bad Scale - This makes sure that if all layers are out of scale we present a message to the user, if 
        //out of scale layers are not being displayed...
        CheckForBadScaleLayer = 'All Layers out of Scale';
        //Cycle through the layers from the Category_Layer_Order which has been split into TempArray


                                            //Cycle through the layers from the Download_Category_Layer_Order which has been split into TempArray
                for (var j=0;j<DL_TempArray.length;j++) {
               
                        //LocalString += parent.MapFrame.GetOGCTOC_Layer_Download(j, i, DL_TempArray[j], CurrentMapScale);


          //If we are not displaying layers that are out of scale, then check the scale here
          //before drawing the layer.
	  /*
          if (Include_DL_Bad_Scale) {
            //We are displaying layers even if they are out of scale, so change the CheckForBadScalelayer variable...
            CheckForBadScaleLayer = 'Has Some in Scale';
            LocalString += parent.MapFrame.GetOGCTOC_Layer_Download(j, i, DL_TempArray[j], CurrentMapScale);
          } else {
	  */
          //Check the layer's scale factor and extent to see if we should display it
          if (Check_Layer_Download_Extent_and_Scale(DL_TempArray[j], eLeft, eBottom, eRight, eTop, CurrentMapScale)) {
            //Passes Extent/Scale check, so display the layer
            CheckForBadScaleLayer = 'Has Some in Scale';
            LocalString += parent.MapFrame.GetOGCTOC_Layer_Download(j, i, DL_TempArray[j], CurrentMapScale);
           }else{
	          if (parent.MapFrame.Download_SRC_Layer_Checked[DL_TempArray[j]]==true) {
			parent.MapFrame.Download_SRC_Layer_Checked[DL_TempArray[j]] = false;
			//updateMaxOrderArea(); //this one is not working
			parent.MapFrame.MBperSqDeg = parent.MapFrame.MBperSqDeg - parent.MapFrame.Download_SRC_Layer_Weights[DL_TempArray[j]];
		  }
          }


        }// for loop

        //Report an error message if all layers were out of scale...
        if (CheckForBadScaleLayer == 'All Layers out of Scale') {
          LocalString += ('<tr><td>&nbsp;</td><td colspan="2" valign="top" align="left"><span style="color:#CCCCCC">No layers in the current scale</span></td></tr>');
        }



               
      } else {
        //Report an error message if there are no layers in this category.
        LocalString += ('<tr><td>&nbsp;</td><td colspan="2" valign="top" align="left"><span style="color:#CCCCCC">No Layers available in this category</span></td></tr>');
      }//if length
        }  // if expanded
    }  // for loop
                                            //Now close the table up
    LocalString += ('</table></form>');
    return LocalString;
}

//*****************************************************************************
// Function GetOGCTOC_Layer_Download
// This function generates the HTML code for the individual layers downloading 
// by getOGCDownloadList above.  This was broken out to make the code a little easier
// to read, parse and work with.  It will take a Category, LayerID and 
// information on the current scale and layer within a category to generate
// table rows showing the layer names.  This allows for a different style
// of TOC to be drawn, yet display the same layers.  The table rows should 
// be 3 columns.
//*****************************************************************************       
function GetOGCTOC_Layer_Download(CategoryLayerID, CategoryID, LayerID, CurrentMapScale) {
    //alert("entering GetOGCTOC_Layer_Download");
                                              //If using the Group rendering then grouped layers only get 1 TOC entry, so
                                              //we do a check to see if one TOC entry has already been displayed...
    Temp_Keyword = -1;
    Test_For_Group_Display = false;
//    if (USGS_Group_Loaded) {
//        Temp_Keyword = IVM_Keyword_check(Download_SRC_Layer_Name[LayerID]);
//        if (Temp_Keyword>-1) {
//            Test_For_Group_Display = IVM_Selected_Layer_Displayed[Temp_Keyword];
//        }
//    }
    if (!Test_For_Group_Display) {
                                            //Indent the layer by putting a space in the first column where the expand/collapse icon is in the category name
      var LocalString = ('<tr><td>&nbsp;</td><td valign="top" align="left">');
                                            //Construct the Check box by getting the visibility string and checking the current visibility of the layer
      LocalString += ('<input type="checkbox" name="'+LayerID+'"');
//alert(this.name);
        if (parent.MapFrame.Download_SRC_Layer_Checked[LayerID]==true){
        LocalString += (' checked="checked"');
        }
                  LocalString += (' onclick="parent.MapFrame.chkboxvalue(this.name);"');



                //And redraw the OGC background layers when the checkbox is clicked
                //LocalString += ('parent.MapFrame.afterMapRefresh();"');
                //enable/disable the checkbox and set a string used later to color-code the label
      if ((Download_SRC_Layer_Max_Scale[LayerID]!=0 && Download_SRC_Layer_Max_Scale[LayerID]<CurrentMapScale) || ((Download_SRC_Layer_Min_Scale[LayerID]!=0 && Download_SRC_Layer_Min_Scale[LayerID]>CurrentMapScale) ) ){
          //LocalString += (' disabled="disabled"');
          DisableColor = '#CCCCCC';
      } else {
          LocalString += (' readonly="readonly"');
          DisableColor = parent.USGSTextColor;
      }
                //and finally close the check box
      LocalString += (' />');
                //If the Layer has a documentation URL then write our layer name hot-linked to that URL
      if (Download_SRC_Layer_Documentation_URL[LayerID].length>0) {
          LocalString += ('</td><td valign="top" align="left"><a href="' + Download_SRC_Layer_Documentation_URL[LayerID] + '" target="_blank" title="View Documentation about this layer."><span style="color: ' + DisableColor + ';">' + Download_SRC_Layer_Name[LayerID] + '</span></a></td></tr>');
      } else {
                //No Documentation URL, so if there is a Metadata URL hot-link the layer name to that
      	  if (Download_SRC_Layer_Metadata_URL[LayerID].length>0) {
          	LocalString += ('</td><td valign="top" align="left"><a href="' + Download_SRC_Layer_Metadata_URL[LayerID] + '" target="_blank" title="View Documentation about this layer."><span style="color: ' + DisableColor + ';">' + Download_SRC_Layer_Name[LayerID] + '</span></a></td></tr>');
      	  } else {
                //No Documentation URL or Metadata URL, so just print the Layer name.
          	LocalString += ('</td><td valign="top" align="left"><span style="color: ' + DisableColor + ';">' + Download_SRC_Layer_Name[LayerID] + '</span></td></tr>');
      	  }
    }
    //If we are displaying layer classification and this layer has a legend image URL defined, then we display that
    if (Display_Layer_Classification && Download_SRC_Layer_Icon_URL[LayerID].length > 0) {
      LocalString += ('<tr><td>&nbsp;</td><td>&nbsp;</td><td valign="top" align="left">');
      LocalString += ('<img src="' + Download_SRC_Layer_Icon_URL[LayerID] + '" style="background-color: #FFFFFF;" alt="Classification for ' + Download_SRC_Layer_Name[LayerID] + '" /></td></tr>');
    }
    return LocalString;
  }
 
}
//*****************************************************************************
// Function getOGCLayerList
// This function will generate HTML code to draw the OGC layer list with the
// categories expanded/collapsed and the Layer classification, if shown.
// It returns the code as an HTML table to do indenting properly.  The 
// Category expand/collapse functions, do reload the window in order to 
// get the client to call this function again and redraw the window.
// page2Load is used for the expanding/collapsing of categories.  It must 
// specify the page that called the function.  This can be done by passing
// window.location.pathname in the call to this function.  
//*****************************************************************************
function getOGCLayerList(page2Load) {
  //alert("entering getOGCLayerList");
    //*********************************************************************                     
    //*			  START of Code to Add USGS Grouping Layers                   *
    //*********************************************************************
  //If using group layers reset the display counter variables
  if (USGS_Group_Loaded) { IVM_OGC_Reset_Display(); }
    //*********************************************************************
    //*			  END of Code to Add USGS Grouping Layers                     *
    //*********************************************************************  
  
  //Initialize the String to return with the table header
  var LocalString = '';

if (Show_TOC_Title) {
        LocalString += ('<div align="left" class="title_label">&nbsp;Layers</div>');
        LocalString += ('<img src="/website/ImageArchive/secondary/tabspacer.gif">');
		if (parent.UseDownload=='Y') {
        	//LocalString += ('<img src="/website/ImageArchive/secondary/display.gif" usemap="#tabs" border=0 /><map name="tabs" id="tabs"><area shape="poly" coords="6,1,1,6,1,21,74,21,74,5,70,1" href="#" onClick="parent.MapFrame.displaylayers();" title="show display layer categories" /><area shape="poly" coords="78,1,74,5,74,21,147,21,147,6,142,1" href="#" onClick="parent.MapFrame.downloadlayers();" alt="show download layer categories"/></map>');
		LocalString += ('<img src="/website/ImageArchive/secondary/display.gif" usemap="#tabs" border=0 /><map name="tabs" id="tabs"><area shape="poly" coords="6,1,1,6,1,21,74,21,74,5,70,1"  href="' + page2Load + '" onClick="parent.MapFrame.displaylayers();" title="show display layer categories" /><area shape="poly" coords="78,1,74,5,74,21,147,21,147,6,142,1"  href="' + page2Load + '" onClick="parent.MapFrame.downloadlayers();" alt="show download layer categories"/></map>');
		//LocalString += ('<img src="/website/ImageArchive/secondary/display.gif" usemap="#tabs" border=0 /><map name="tabs" id="tabs"><area shape="poly" coords="6,1,1,6,1,21,74,21,74,5,70,1" href="javascript: void(0);" onClick="parent.MapFrame.displaylayers();" title="show display layer categories" /><area shape="poly" coords="78,1,74,5,74,21,147,21,147,6,142,1" href="javascript: void(0);" onClick="parent.MapFrame.downloadlayers();" alt="show download layer categories"/></map>');
		} 
  }
  LocalString += '<form name="OGCLayerlist"><table border="0" cellpadding="0" cellspacing="0" summary="Categorized Layer list of OGC layers">';
  
  //Get the Current Scale from USGS Scale Tool
  var CenterAdjust = Math.abs(eRight - eLeft);
  var CurrentMapScale = HaversineScale((eLeft + CenterAdjust), eBottom, (eLeft + CenterAdjust), eTop, mHeight, ScaleToolDPI);
  //Cycle through the Categories array
        for (var i=0;i<Category_Order.length;i++) {
    //First show the Category Expanded/Collapsed Icon
    LocalString += ('<tr><td valign=top align=right><a name="C' + i + '"></a><a href="' + page2Load + '"');
    if (Category_Expanded[i]) {
      LocalString += (' onclick="parent.MapFrame.Category_Expanded[' + i + ']=false;parent.MapFrame.TOC_Hash=\'C' + i + '\';" title="Collapse Category (Hide Layer List for this category)">');
      LocalString += ('<img src="' + Category_Expanded_Image + '" alt="Collapse Category (Hide Layer List for this category)" border="0" height="18" width="18" valign="middle" />');
    } else {
      LocalString += (' onclick="parent.MapFrame.Category_Expanded[' + i + ']=true;parent.MapFrame.TOC_Hash=\'C' + i + '\';" title="Expand Category (Show Layer List for this category)">');
      LocalString += ('<img src="' + Category_Collapsed_Image + '" alt="Expand Category (Show Layer List for this category)" border="0" height="18" width="18" valign="middle" />');
    }
    LocalString += ('</a></td>');
    //Now show the Category Name
    LocalString += ('<td colspan="2" valign="top" align="left"><strong>' + Category_Order[i] + '</strong></td></tr>');
    //If the Category is Expanded, then we need to draw out the Layer names under that Category.
    if (Category_Expanded[i]) {
      if (Category_Layer_Order[i].length>0) {
        TempArray = Category_Layer_Order[i].split(',');
        TempArray2 = Category_Layer_Visibility[i].split(',');
        //Do the check for Bad Scale - This makes sure that if all layers are out of scale we present a message to the user, if 
        //out of scale layers are not being displayed...
        CheckForBadScaleLayer = 'All Layers out of Scale';
        //Cycle through the layers from the Category_Layer_Order which has been split into TempArray
                    for (var j=0;j<TempArray.length;j++) {
          //If we are not displaying layers that are out of scale, then check the scale here
          //before drawing the layer.
          if (Include_Bad_Scale) {
            //We are displaying layers even if they are out of scale, so change the CheckForBadScalelayer variable...
            CheckForBadScaleLayer = 'Has Some in Scale';
            LocalString += GetOGCTOC_Layer(j, i, TempArray[j], CurrentMapScale);
          } else {
          //Check the layer's scale factor and extent to see if we should display it
          if (Check_Layer_Extent_and_Scale(TempArray[j], eLeft, eBottom, eRight, eTop, CurrentMapScale)) {
            //Passes Extent/Scale check, so display the layer
            CheckForBadScaleLayer = 'Has Some in Scale';
            LocalString += GetOGCTOC_Layer(j, i, TempArray[j], CurrentMapScale);
          }
          }
        }
        //Report an error message if all layers were out of scale...
        if (CheckForBadScaleLayer == 'All Layers out of Scale') {
          LocalString += ('<tr><td>&nbsp;</td><td colspan="2" valign="top" align="left"><span style="color:#CCCCCC">No layers in the current scale</span></td></tr>');
        }
      } else {
        //Report an error message if there are no layers in this category.
        LocalString += ('<tr><td>&nbsp;</td><td colspan="2" valign="top" align="left"><span style="color:#CCCCCC">No Layers available in this category</span></td></tr>');
      }
    }
  }
  //Now close the table up
  LocalString += ('</table></form>');
  return LocalString;
}


//*****************************************************************************
// Function Check_Layer_Download_Extent_and_Scale
// This function checks a layer to see if it is outside of the current extent
// or the current scale is outside of the layer's scale.  It returns true only
// if the layer's extent is inside the current extent or the current scale is
// inside the layer's scale range.
//*****************************************************************************
function Check_Layer_Download_Extent_and_Scale(LayerID, MapLeft, MapBottom, MapRight, MapTop, MapScale) {
  var tempCheck = true;
  //Check the layer's scale factor to see if we should display it
  if (Download_SRC_Layer_Min_Scale[LayerID] != 0 || Download_SRC_Layer_Max_Scale[LayerID] != 0) {
    if (Download_SRC_Layer_Min_Scale[LayerID] != 0 && Download_SRC_Layer_Min_Scale[LayerID] > MapScale) {
      //Below Minimum Scale
      tempCheck = false;
    } else if (Download_SRC_Layer_Max_Scale[LayerID] != 0 && Download_SRC_Layer_Max_Scale[LayerID] < MapScale) {
      //Above Maximum Scale
      tempCheck = false;
    }
  }
  //If we have not already gotten a negative, then don't need to check the layer extent
  if (tempCheck) {
    if (Download_SRC_Layer_Extent_Right[LayerID] <= MapLeft) {
      //Layer extent is to the left of the map
      tempCheck = false;
    } else if (Download_SRC_Layer_Extent_Left[LayerID] >= MapRight) {
      //Layer extent is to the right of the map
      tempCheck = false;
    } else if (Download_SRC_Layer_Extent_Bottom[LayerID] >= MapTop) {
      //Layer extent is above the map
      tempCheck = false;
    } else if (Download_SRC_Layer_Extent_Top[LayerID] <= MapBottom) {
      //Layer extent is below the map
      tempCheck = false;
    }
  }
  return tempCheck;
}
