
    // NOTE: ogcServices.js must be included before this file. This file defines variables for helping
    //       in rendering OGC layers.

    function OGCDataHolder (dispLayers, ogcDataId) {
    
        this.layerPos        = 1; // NBIIOGC for Position of main layer map. Earlier it was used for ArcIMS map layer. Now not used.
        this.mainLayersTitle = 'My ArcIMS Layers'; // CUSTOMIZE Not used but kept if we want to introduce a new Group.
        this.ActiveLayer     = -1; // Used when using ArcIMS or there is extra group/layer other than that of OGC

        this.id = ogcDataId;
    
        // Representing active layer index of permanent or user added OGC layers
        this.OGCPermForegroundActiveLayer = -1;
        this.OGCForegroundActiveLayer = -1;
    
        this.OGCPermForegroundsTitle = 'NBII Base Map';// CUSTOMIZE: Permanent Foreground layers title
        this.OGCForegroundsTitle = 'User added OGC layers';// CUSTOMIZE: User added OGC layers title.
    
        this.defaultURL="nbiiogc/metadefault.htm";// Default metadata URL.
        this.mapData = null;// Assigned later.
    
        this.OGCPermFGs = new Array(); // Array holding permanent background map layers
        this.OGCFGs = Array();// Array holding user added OGC layers.
    
        this.OGCPermNumForegrounds = 0;// Number of permanent map layers. Making it 0 will render no map layer, initialized later.
        this.OGCNumForegrounds=0;// Number of user added OGC layers
    
        // CUSTOMIZE - This number can be increased or decreased
        // This specify maximum number of user specified layers which can be added to the displayed map.
        this.OGCNumForegroundsMax=(dispLayers == DISPLAY_ALL_LYRS?50:0);

        this.updatePermForegrounds = o_UpdatePermForegrounds;
        this.changePermForeground = o_ChangePermForeground;
        this.togglePermForegroundVisible = o_TogglePermForegroundVisible;
        this.isPermForegroundVisible = o_isPermForegroundVisible;
        this.isPermFGVisible = o_isPermFGVisible;
        this.updateOGCPermForegroundLayerPos = o_updateOGCPermForegroundLayerPos;
        this.assignEmptyServices = o_assignEmptyServices;
        this.updateOGCForegroundLayerPos = o_updateOGCForegroundLayerPos;
        this.updateForegrounds = o_UpdateForegrounds;
        this.updateForeground = o_UpdateForeground;
        this.changeForeground = o_ChangeForeground;
        this.toggleForegroundVisible = o_toggleForegroundVisible;
        this.visibleForeground = o_VisibleForeground;
        this.resetOGCPermLayers = o_resetOGCPermLayers;

        this.getOGCPermForegroundidentifyurl = o_getOGCPermForegroundidentifyurl;
        this.getOGCForegroundidentifyurl = o_getOGCForegroundidentifyurl;
        this.getOGCForegroundActiveLayer = o_getOGCForegroundActiveLayer;
        this.getOGCPermForegroundActiveLayer = o_getOGCPermForegroundActiveLayer;

        this.getLegendHTML = o_getLegendHTML;
    }


    // NOTE: Must include ogcVarDefs.js before this file.

    function o_UpdatePermForegrounds() {

        for (var i=0;i<this.OGCPermNumForegrounds;i++) {

             var visscale = ((this.OGCPermFGs[i].visible) &&(this.mapData.myscale>=this.OGCPermFGs[i].minScale) && (this.mapData.myscale<=this.OGCPermFGs[i].maxScale));

             if (visscale) {

                // CUSTOMIZE:  You can add extra parameters like background etc to GetMap Request here.
                var PermFGMapURL = this.OGCPermFGs[i].getMapRequest (this.mapData.mWidth, this.mapData.mHeight, this.mapData.eLeft, this.mapData.eRight, this.mapData.eTop, this.mapData.eBottom);
                var str = 'document.theImage' + this.mapData.id + this.OGCPermFGs[i].layerPos + '.src = "' + PermFGMapURL + '"';

                eval(str);

                showLayer('theMap' + this.mapData.id + this.OGCPermFGs[i].layerPos);
             }
             else {
                hideLayer('theMap' + this.mapData.id + this.OGCPermFGs[i].layerPos);
             }
        }
        return true;
    }

    //Function to change layer visibility - called from toc.htm (Layer List)
    function o_ChangePermForeground(i) {

      if (this.OGCPermFGs[i].visible) { 	  
        hideLayer('theMap' + this.mapData.id + this.OGCPermFGs[i].layerPos);
        this.OGCPermFGs[i].visible = false;
      } else {
        showLayer('theMap' + this.mapData.id + this.OGCPermFGs[i].layerPos);
        this.OGCPermFGs[i].visible = true;
      }
      return true;
    }

    // It just toggles visibility of perm layer and does not make foreground visible.
    function o_TogglePermForegroundVisible (i) {

      this.OGCPermFGs [i].visible = ! this.OGCPermFGs [i].visible;
      return true;
    }


    function o_isPermForegroundVisible (i) {
        return (((this.OGCPermFGs[i].visible) &&(this.mapData.myscale>=this.OGCPermFGs[i].minScale) && (this.mapData.myscale<=this.OGCPermFGs[i].maxScale)));
    }

    function o_isPermFGVisible (permLayer) {
        return (((permLayer.visible) &&(this.mapData.myscale>=permLayer.minScale) && (this.mapData.myscale<=permLayer.maxScale)));
    }

    // Update layer rendering indices, because whole group was moved up/down.

    function o_updateOGCPermForegroundLayerPos (newIx, targetPos) {

	var oldPos = this.OGCPermFGs [0].layerPos;

	for (var i =1; i < this.OGCPermNumForegrounds; i++ ) {
		if (this.OGCPermFGs[i].layerPos < oldPos)
			oldPos = this.OGCPermFGs[i].layerPos;
	}

	var adder = targetPos - oldPos;

	// Update positions now.
	for (var i =0; i < this.OGCPermNumForegrounds; i++ ) {
		this.OGCPermFGs [i].layerPos = this.OGCPermFGs [i].layerPos + adder;
	}

    }
    //*********************************************************************
    //*		END of Code to Add NBIIOGC Permanent Foreground Layers      *
    //*********************************************************************
    //*********************************************************************
    //*	        START of Code to Add NBIIOGC On-the-Fly Foreground Layers   *
    //*********************************************************************


    function o_assignEmptyServices () {
       for (i=0;i<=this.OGCNumForegroundsMax;i++) 
       {
          this.OGCFGs [i] = new OGCMapService (null, null, null, null, null, null, false,
                                          false, 0, 10000000, null, null, null, true,
                                          null, null, null, null);
       }
    }

    // Update layer rendering indices, because whole group was moved up/down.

    function o_updateOGCForegroundLayerPos (newIx, targetPos) {

	var oldPos = this.OGCFGs[0].layerPos;

	for (var i =1; i < this.OGCNumForegroundsMax; i++ ) {
		if (this.OGCFGs[i].layerPos < oldPos)
			oldPos = this.OGCFGs[i].layerPos;
	}

	var adder = targetPos - oldPos;
	//alert (" Foreground TargetPos = " + targetPos + " oldPos min = " + oldPos + " adder  = " + adder);

	// Update positions now.
	for (var i =0; i < this.OGCNumForegroundsMax; i++ ) {
		this.OGCFGs[i].layerPos = this.OGCFGs[i].layerPos + adder;
	}

    }

				
    function o_UpdateForegrounds() {

        // TBD: Can we execute this loop for actual layers only.
        // Currently it is executed,because some older layer might
        // corrupt assigned theImage when no user defined URLs present.
        for (var i=0;i<this.OGCNumForegroundsMax;i++) {
            this.updateForeground (i);
        }
        return false;
    }

    function o_UpdateForeground(lnum) {

        if (this.OGCFGs[lnum].visible == false || this.OGCFGs [lnum].url == null) {
            hideLayer ('theMap' + this.mapData.id + this.OGCFGs[lnum].layerPos);
        }
        else {
            // CUSTOMIZE:  You can add extra parameters like background etc to GetMap Request here.
            var ForegroundMapURL = this.OGCFGs[lnum].getMapRequest (this.mapData.mWidth, this.mapData.mHeight, this.mapData.eLeft, this.mapData.eRight, this.mapData.eTop, this.mapData.eBottom);                                                         
            var str = 'document.theImage' + this.mapData.id + this.OGCFGs[lnum].layerPos + '.src = "' + ForegroundMapURL + '"';
            eval(str);
            showLayer ('theMap' + this.mapData.id + this.OGCFGs[lnum].layerPos);
        }
        return false;
    }

    //Function to toggle layer visibility - called from toc.htm (Layer List)
    function o_ChangeForeground(i) {
      if (this.OGCFGs[i].visible) { 
        hideLayer('theMap' + this.mapData.id + this.OGCFGs[i].layerPos);
        this.OGCFGs[i].visible = false;
      } else {
        showLayer('theMap' + this.mapData.id + this.OGCFGs[i].layerPos);
        this.OGCFGs[i].visible = true;
      }
      return;
    }

    // It just sets foreground but does not make them visible or invisible on screen.
    function o_toggleForegroundVisible (i) {

      this.OGCFGs [i].visible = ! this.OGCFGs [i].visible;
      return;
    }
	
    //Function to change layer visibility - called from toc.htm (Layer List)
    function o_VisibleForeground(i) {
        showLayer('theMap' + this.mapData.id + this.OGCFGs[i].layerPos);
        this.OGCFGs[i].visible = true;
      return;
    }
	

function o_resetOGCPermLayers() {
     this.OGCPermForegroundActiveLayer=-1;
     this.OGCForegroundActiveLayer=-1;
     this.ActiveLayerIndex = -1;
}

function o_getOGCPermForegroundidentifyurl( layernum, mousex, mousey )
{
  this.mapData.getMapXY(mousex,mousey);

  var lurl = this.OGCPermFGs[layernum].getFeatureRequest (this.mapData.mWidth,
                                      this.mapData.mHeight, this.mapData.eLeft,
                                      this.mapData.eRight, this.mapData.eTop,
                                      this.mapData.eBottom);

  // CUSTOMIZE - Though not required, but you can build your own getFeatureInfo request here.
  lurl =  lurl + "&EXCEPTIONS=application/vnd.ogc.se_xml&QUERY_LAYERS=" + this.OGCPermFGs[layernum].layers + "&INFO_FORMAT=MIME&FEATURE_COUNT=1" + "&X=" + mousex + "&Y=" + mousey; 
  return (lurl);
}

function o_getOGCForegroundidentifyurl( layernum, mousex, mousey )
{

  this.mapData.getMapXY(mousex,mousey);

  var lurl = this.OGCFGs [layernum].getFeatureRequest (this.mapData.mWidth,
                                      this.mapData.mHeight, this.mapData.eLeft,
                                      this.mapData.eRight, this.mapData.eTop,
                                      this.mapData.eBottom);

  // CUSTOMIZE - Though not required, but you can build your own getFeatureInfo request here.
  lurl =  lurl + "&EXCEPTIONS=application/vnd.ogc.se_xml&QUERY_LAYERS=" + this.OGCFGs[layernum].layers + "&INFO_FORMAT=MIME&FEATURE_COUNT=1" + "&X=" + mousex + "&Y=" + mousey; 
  return (lurl);
}

function o_getOGCForegroundActiveLayer()
{
  return( this.OGCForegroundActiveLayer );
}

function o_getOGCPermForegroundActiveLayer()
{
  return( this.OGCPermForegroundActiveLayer );
}


		
//------- Write legend --------------


function o_getLegendHTML () {


	var str = '<table border="0">'+
	          '<tr><td bgcolor="gray"><FONT FACE="Arial" SIZE="2" COLOR="#000000"><B><CENTER>Legend</CENTER>' +
	          '</B></FONT></td></tr>';

        var legendPresent = false;

	str = str + '<tr><td><table border="0">';

	for (var j=0;j < this.OGCPermNumForegrounds;j++) {

		var visscale = (this.mapData.myscale>=this.OGCPermFGs[j].minScale) && (this.mapData.myscale<=this.OGCPermFGs[j].maxScale);

		if ((this.OGCPermFGs[j].legendURL != '') && (this.OGCPermFGs[j].visible) && (visscale)) {		
                        legendPresent = true;
			str = str + '<tr><td align="left"><font size="-2" face="arial"><b>' + this.OGCPermFGs[j].displayName+ '</b></font></td></tr>' +
			            '<tr><td align="left"><IMG SRC="' + this.OGCPermFGs[j].legendURL + '" HSPACE=0 VSPACE=0 BORDER=0 ALT="' + this.OGCPermFGs[j].displayName + ' Legend' + '"><hr color="#0000FF" size="1"></td></tr>' ;
		}
	}

	for (var i=0;i<this.OGCNumForegroundsMax;i++) {
		if ((this.OGCFGs[i].legendURL != '') && (this.OGCFGs[i].visible)) {
                        legendPresent = true;
			str = str + '<tr><td align="left"><font size="-2" face="arial"><b>' + this.OGCFGs[j].displayName+ '</b></font></td></tr>' +
			            '<tr><td align="left"><IMG SRC="' + this.OGCFGs[j].legendURL + '" HSPACE=0 VSPACE=0 BORDER=0 ALT="' + this.OGCFGs[j].displayName + ' Legend' + '"><hr color="#0000FF" size="1"></td></tr>' ;
		}
	}

        if (legendPresent == false) {
             str = str + "<tr><td><br><br><b> No legend is found for displayed layers.</b></td></tr>";
        }

	str = str + '</table>' +
	            '</td></tr>' +
	            '</table>';

        return str;
}



