public int[] obtainIndexForOptionalLayers(String[] menuSelected) {

    if (menuSelected.length == 0) { // if not entry return null
      return null;
    }

    ArrayList<Integer> result = new ArrayList(); // temporal list to hold values
    Layer tempLayer = null;
    for (int index = 0;
        index < layersManager.getVectorLayers().size();
        index++) { // loop each vector layer
      tempLayer = layersManager.getVectorLayers().get(index); // temp variable

      // each tree value has to be send separately.
      // becuase the vector layers are all on one level of the tree
      for (int menuNumber = 0; menuNumber < menuSelected.length; menuNumber++) {
        if (tempLayer.isThisLayer(
            StringAndNumbers.strArrayFromStringColon(menuSelected[menuNumber]))) {
          // check to see if the layer is selected
          result.add(Integer.valueOf(index));
          break;
        }
      }
    }
    // convert into array of ints and then return.
    return ConvertionTools.convertObjectArrayToIntArray(result.toArray());
  }
  /**
   * This function creates the url to bring the data when the user clicks somewhere on the map.
   *
   * @param {int} baseLayer
   */
  private String agregaURLparaTraerDatos(int baseLayer) {
    String layersScript = "";
    Layer actualLayer = null;
    int layerCount = 0;
    layersScript =
        "\n\tfunction punctualData(evt) {\n"; // Se agrega al evento click del div map la siguiente
                                              // funcion
    layersScript +=
        "\t\t if(mobile){owgis.mobile.closePanels();"
            + "\t\t\t owgis.interf.loadingatmap(true);}\n"
            + "\t\t var coordinate = evt.coordinate;\n"
            + "\t\t var currBBOX =  ol3view.calculateExtent(map.getSize());\n"
            + "\t\t $('#popup').hide();\n"
            + "\t\t currPopupText = '<b>Lon: </b>'+coordinate[0].toFixed(2)+ ' <b>Lat: </b>'+coordinate[1].toFixed(2)+'<br>'\n"
            + "\t\t ol_popup.setPosition(coordinate);\n";

    // En este for agrega las capas que son de fondo
    for (int i = 0; i < layersManager.getBackgroundLayers().size(); i++) {
      actualLayer = layersManager.getBackgroundLayers().get(i);
      if (actualLayer.getFeatureInfoLayer() != null) {
        layersScript += generateURLhelper(actualLayer, layerCount);
      } // If layer featureInfo not null
      layerCount++;
    }
    // Se agrega el URL de la capa base
    actualLayer = layersManager.getMainLayers().get(baseLayer);
    if (actualLayer.getFeatureInfoLayer() != null) {
      layersScript += generateURLhelper(actualLayer, layerCount);
    } // If layer  featureInfo  not null

    layerCount++; // Increment current layer (for  javascript, 'layer0' or 'layer1'....

    for (int i = 0; i < layersManager.getVectorLayers().size(); i++) {
      actualLayer = layersManager.getVectorLayers().get(i);
      if (actualLayer.getFeatureInfoLayer() != null) {
        layersScript += generateURLhelper(actualLayer, layerCount);
      } // If layer  featureInfo not null
      layerCount++;
    }

    layersScript += // "\t\tMapViewersubmitForm();\n" +
        "\t}\n";
    return layersScript;
  }
  /**
   * Obtains the index of the list of vector layers or raster depending on the selection.
   *
   * @param {MenuEntry[]} menuEntry User selection array
   * @param {boolean} layerType layer type checking it can be HtmlTools.RASTER_LAYERS
   * @return int[] we return an array with the index of the layers.
   */
  public int[] obtainArrayIndexOfLayers(MenuEntry[] menuEntry) {
    int index = 0;
    if (menuEntry.length == 0) { // if not entry return null
      return null;
    }

    ArrayList<Integer> resultado = new ArrayList(); // temporal list to hold values
    Layer tempLayer = null; // temp layer used to check
    // loop alentries until find the one selected
    for (index = 0; index < layersManager.getMainLayers().size(); index++) {
      tempLayer = layersManager.getMainLayers().get(index); // get index of this layer
      if (tempLayer.isThisLayer(menuEntry)) { // if this is the layer we are looking for
        resultado.add(Integer.valueOf(index)); // add layer to list of index
        break; // if found then break from for loop.
      }
    }

    // convert into array of ints and then return.
    return ConvertionTools.convertObjectArrayToIntArray(resultado.toArray());
  }
  /**
   * Creates the javascript of the layers that are going to be shown separatly. Each layer is an
   * object first is the raster layers then the vector layers.
   *
   * @param idsBaseLayers
   * @param idsExtraLayers
   * @return
   */
  private String createSeparateLayerScript(int[] idsBaseLayers, int[] idsExtraLayers) {
    String layersScript = "";
    Layer actualLayer = null;
    int layerCount = 0;

    String backgroundLayer = mapConfig.getProperty("backgroundLayer");

    switch (backgroundLayer) {
      case "wms": // Read the background layer from xml file
        // add the layers that are the background.
        for (int i = 0; i < layersManager.getBackgroundLayers().size(); i++) {
          actualLayer = layersManager.getBackgroundLayers().get(i);
          if (actualLayer.getName() != null) {
            layersScript += layerHelper(actualLayer, layerCount, true);
            layerCount++;
          } // If layer not null
        }
        break;
      case "osm": // Add OpenStreetMap as the background layer
        layersScript += addOpenStreetMapLayer(layersScript, layerCount);
        layerCount++;
        break;
      case "binga": // Add Aerial Bing layer as the background layer
        layersScript += addBingLayer(layersScript, layerCount, "Aerial");
        layerCount++;
        break;
      case "bingr": // Add Road Bing layer as the background layer
        layersScript += addBingLayer(layersScript, layerCount, "Road");
        layerCount++;
        break;
      case "bingh": // Add Hybrid Bing layer as the background layer
        layersScript += addBingLayer(layersScript, layerCount, "AerialWithLabels");
        layerCount++;
        break;
      case "mapquesta": // Add MapQuest as the background layer
      case "mapquestr": // Add MapQuest as the background layer
      case "mapquesth": // Add MapQuest as the background layer
        layersScript += addMapQuest(layersScript, layerCount, backgroundLayer);
        layerCount++;
        break;
    }

    // Generates the layer configuration for OpenLayers
    // The name of the layer variable inside OpenLayers is of the form layer'number of layer'
    for (int i = 0; i < idsBaseLayers.length; i++) {
      actualLayer = layersManager.getMainLayers().get(idsBaseLayers[i]);
      if (actualLayer.getName() != null) {
        layersScript += layerHelper(actualLayer, layerCount, true);
        layerCount++;
      } // If layer not null
    }

    for (int i = 0; i < layersManager.getVectorLayers().size(); i++) {
      actualLayer = layersManager.getVectorLayers().get(i);
      if (StringAndNumbers.intArrayContains(
          idsExtraLayers, i)) { // Si esta en los seleccionados lo mostramos
        // Si no no
        layersScript += layerHelper(actualLayer, layerCount, true);
      } else {
        layersScript += layerHelper(actualLayer, layerCount, false);
      }
      layerCount++;
    }
    return layersScript;
  }
  /**
   * It helps to create each layer in a open layer script
   *
   * @param actualLayer
   * @param layerCount
   * @param visible Boolean indicates if the layer should be visible
   * @return
   */
  private String layerHelper(Layer actualLayer, int layerCount, boolean visible) {
    String layersScript = "";
    // If they layer is a jsonp (dynamic vector) layer, we do not add it to the map
    if (!actualLayer.isJsonp()) {
      if (actualLayer.isTiled()) {
        layersScript += "\tlayer" + layerCount + " = new ol.layer.Tile({\n";
        layersScript += "\t\t source: new ol.source.TileWMS({\n";
      } else {
        layersScript += "\tlayer" + layerCount + " = new ol.layer.Image({\n";
        layersScript += "\t\t source: new ol.source.ImageWMS({\n";
        layersScript += "\t\t extent: [\n" + actualLayer.getBbox().toString() + "],";
      }
      layersScript +=
          "\t\t url: '"
              + actualLayer.getServer()
              + "',\n"
              //				+ "\t\t crossOrigin: 'null',\n"
              + "\t\t params: {LAYERS: '"
              + actualLayer.getName()
              + "'";

      if (actualLayer.isncWMS()) {
        if (actualLayer.getMaxColor() != -1 && actualLayer.getMinColor() != -1) {
          layersScript +=
              ", numcolorbands:250,  colorscalerange: '"
                  + actualLayer.getMinColor()
                  + ","
                  + actualLayer.getMaxColor()
                  + "'";
        }
      }
      layersScript += ", STYLES: '" + actualLayer.getStyle() + "'";

      layersScript += ", SRS: _map_projection";

      layersScript += "}\n\t\t\t})\n";
      layersScript += "\t\t});\n";

      // In this case the layer has some CQL that we need to add in its configuration
      if (!actualLayer.getCql().equals("")) {
        layersScript +=
            "\tlayer"
                + layerCount
                + ".getSource().getParams().CQL_FILTER = \""
                + actualLayer.getCql()
                + "\";\n";
      }

      if (!visible) {
        layersScript +=
            "\tlayer" + layerCount + ".setVisible(false);\n"; // we make the layer not visible.
      }
      layersScript += "\tmap.addLayer(layer" + layerCount + ");\n";
      //			layersScript += "\tlayer"+layerCount+".getSource().on('change'))\n";
    } else {
      layersScript +=
          "\t owgis.vector.manager.requestJSONLayer("
              + actualLayer.getLayerDetails()
              + ","
              + layerCount
              + ","
              + visible
              + ");\n";
    }
    return layersScript;
  }
  //	private String generateURLhelper(Layer actualLayer, int ajaxCallNumber, int layerNumber) {
  private String generateURLhelper(Layer actualLayer, int layerNumber) {

    String URLscript = "";

    URLscript += "\t\tif(typeof(layer" + layerNumber + ") !== 'undefined'){\n";
    URLscript += "\t\t\tif(layer" + layerNumber + ".getVisible()){\n";
    URLscript +=
        "\t\t\t\tvar url"
            + layerNumber
            + " = basepath+\"/redirect?server="
            + actualLayer.getServer()
            + "&";

    URLscript += "LAYERS=" + actualLayer.getFeatureInfoLayer() + "&";
    URLscript +=
        "STYLES=&"
            + "WIDTH=\"+ map.getSize()[0] +\"&"
            + "HEIGHT=\"+ map.getSize()[1] +\"&"
            + "SRS=\"+ _map_projection+ \"&"
            + "FORMAT="
            + actualLayer.getFormat()
            + "&"
            + "VERSION=1.1.1&"
            + "REQUEST=GetFeatureInfo&"
            + "EXCEPTIONS=application/vnd.ogc.se_xml&"
            + "x=\"+ Math.floor(evt.pixel[0]) +\"&"
            + "y=\"+ Math.floor(evt.pixel[1]) +\"&"
            + "BBOX=\"+ currBBOX +\"&"
            + "SERVICE=WMS&";

    if (!actualLayer.getCql().equals("")) {
      URLscript += "CQL_FILTER=" + actualLayer.getCql() + "&";
    }

    // In this case we also need the time information
    if (actualLayer.isncWMS()) {
      // The two variables: elevation and startDate have to match
      // javascript variable names.
      //			URLscript += "ELEVATION=\"+layerDetails.zaxis.values[owgis.ncwms.zaxis.globcounter]+\"&"
      URLscript +=
          "\"+owgis.ncwms.zaxis.addElevationText()+\""
              + "TIME=\"+owgis.ncwms.calendars.getCurrentlySelectedDate(\"yy-mm-dd\")+\"&"
              + "BOTHTIMES=\"+getUserSelectedTimeFrame()+\"&"
              + "INFO_FORMAT=text/xml&"
              + "NETCDF=true&";
    } else {
      URLscript += "INFO_FORMAT=text/html&" + "NETCDF=false&";
    }

    URLscript += "QUERY_LAYERS=" + actualLayer.getFeatureInfoLayer() + "&";
    URLscript += "FEATURE_COUNT=50\";\n";
    URLscript +=
        "\t\t\t\t var asynchronous"
            + layerNumber
            + " = new Asynchronous();\n"
            + "\t\t\t\t asynchronous"
            + layerNumber
            + ".complete = AsyncPunctualData;\n"
            //				+ "\t\t\t alert(url" + layerNumber + ");\n"
            + "\t\t\t\t asynchronous"
            + layerNumber
            + ".call(url"
            + layerNumber
            + ");\n"
            + "\t\t\t}\n"
            + "\t\t}\n";
    return URLscript;
  }