﻿
var title = "";
var layer = "";
var wetlandsDrainageRegEx = [
    RegExp("[.]*(stream|habitat)[.]*"), 
    RegExp("[^%]*(area){1}$"),
    RegExp("[.]*watershed[.]*"),
    RegExp("[.]*peninsula[.]*"),
    RegExp("[.]*total[.]*"),
]
var wildfireRegEx = [
    RegExp("[.]*area burned{1}$"),
    RegExp("[.]*watershed[.]*"),
    RegExp("[.]*peninsula[.]*"),
    RegExp("[.]*total[.]*"),
]
var landcoverRegEx = [
    RegExp("[.]*area{1}$"),
    RegExp("[.]*watershed[.]*"),
    RegExp("[.]*peninsula[.]*"),
    RegExp("[.]*total[.]*"),
]
var sbbRegEx = [
    RegExp("[.]*present{1}$"),
    RegExp("[.]*watershed[.]*"),
    RegExp("[.]*peninsula[.]*"),
    RegExp("[.]*total[.]*"),
]
var loggingRegEx = [
    RegExp("[.]*area logged{1}$"),
    RegExp("[.]*watershed[.]*"),
    RegExp("[.]*peninsula[.]*"),
    RegExp("[.]*total[.]*"),
]

var landuseDrainageRegEx = [ 
    RegExp("[.]*acres[.]*"),
    RegExp("[.]*watershed[.]*"),
    RegExp("[.]*peninsula[.]*"),
    RegExp("[.]*total[.]*"),
]
var footprintsRegEx = [
    RegExp("[.]*area{1}$"),
    RegExp("[.]*length{1}$"),
    RegExp("[.]*watershed[.]*"),
    RegExp("[.]*peninsula[.]*"),
    RegExp("[.]*total[.]*"),
    RegExp("[.]*density[.]*"),
    RegExp("[.]*length of total footprint type{1}$"),
]

var imperviousRegEx = [
    RegExp("[.]*rea{1}$"),
    RegExp("[.]*watershed[.]*"),
    RegExp("[.]*peninsula[.]*"),
    RegExp("[.]*total[.]*"),
]

var hydrologyRegEx = [
    RegExp("[.]*area[.]*"),
    RegExp("[.]*length{1}$"),
    RegExp("[.]*density{1}$"),
    RegExp("[.]*peninsula[.]*"),
    RegExp("[.]*Number of watersheds{1}$"),
]
var regionRegEx = [
    RegExp("[.]*area{1}$"),
    RegExp("[.]*Perimeter{1}$"),
    RegExp("[.]*miles[.]*"),
    RegExp("[.]*peninsula[.]*"),
    RegExp("[.]*Number[.]*"),
]
var invasivesRegEx = [
    RegExp("[.]*count{1}$"),
    RegExp("[.]*length{1}$"),
    RegExp("[.]*invasives by watershed{1}$"),
    RegExp("[.]*aggressiveness type{1}$"),
    RegExp("[.]*data exists{1}$"),
    RegExp("[.]*pike lake by watershed{1}$"),
    RegExp("[.]*pike stream length by watershed{1}$"),
    RegExp("[.]*pike lakes{1}$"),
    RegExp("[.]*pike stream length on peninsula{1}$"),
    RegExp("[.]*pike streams{1}$"),
    RegExp("[.]*pike[.]*"),
    RegExp("[.]*lake count{1}$"),
    RegExp("^Total[.]*"),
]
var streamCrossingRegEx = [
    RegExp("[.]*count{1}$"),
    RegExp("[.]*length{1}$"),
    RegExp("[.]*culverts by watershed{1}$"),
    RegExp("[.]*total peninsula[.]*"),
    RegExp("[.]*length in watershed{1}$"),
    RegExp("[.]*streams on peninsula{1}$"),
    RegExp("[.]*state salmon stream[.]*"),
    RegExp("[.]*culvert[.]*"),
    RegExp("^Total structures[.]*"),
     RegExp("[.]*culvert count{1}$"),
     RegExp("[.]*estored[.]*"),
     RegExp("[.]*Total length of state salmon stream in watershed{1}$"),
     RegExp("[.]*Miles of stream blocked by red culverts{1}$"),
]
function XmlFromQuery(layerName, serviceTitle, results){
    title = serviceTitle;
    layer = layerName;
    var resultsArray = new StructuredArray(results.features.length);
    var aliases = results.fieldAliases;
    //alert('here');
    for (i = 0; i < results.features.length; i++){
        var attributes = results.features[i].attributes;
        var resultsKV = new Array();
        for (alias in aliases){
          if (aliases[alias].indexOf('%') > 0) {
            // We are formating the numbers for the print reports here
            resultsKV[aliases[alias]] = (attributes[alias] * 100).toFixed(4).toString() + '%';
          }
          else
            if (IsNumeric(attributes[alias]) ) {
              resultsKV[aliases[alias]] = (attributes[alias] * 1).toFixed(2);
            }
            else {
              resultsKV[aliases[alias]] = attributes[alias]
            }
        }
        resultsArray[i] = fillStructuredArray(resultsKV)[0];
    }
    return createXML(resultsArray);
}
function XmlFromIdentify(layerName, serviceTitle, results) {
    title = serviceTitle;
    layer = layerName;
    var resultsArray = new StructuredArray(1);
    resultsArray[0] = fillStructuredArray(results)[0];
    return createXML(resultsArray);
  }

function fillStructuredArray(aliasValuePairs){
    resultsArray = new StructuredArray(1);
   
       if (title == 'Wetlands') {
        resultsArray[0]['FeatureName'] = aliasValuePairs[layer.toString()];
        for(kv in aliasValuePairs){
            //Habitats          
            if(kv.toString().match(wetlandsDrainageRegEx[0])){
                if(kv.toString().match(wetlandsDrainageRegEx[1])){
                  resultsArray[0]['ResultGroups']['Habitats']['Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
                }
                else if(kv.toString().match(wetlandsDrainageRegEx[2])){
                    resultsArray[0]['ResultGroups']['Habitats']['By Total Watershed Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
                }
                else if(kv.toString().match(wetlandsDrainageRegEx[3])){
                    resultsArray[0]['ResultGroups']['Habitats']['By Total Peninsula Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
                }
                else if(kv.toString().match(wetlandsDrainageRegEx[4])){
                    resultsArray[0]['ResultGroups']['Habitats']['By Total Habitat Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
                }
            }
            //Wetland Ecosystems
            else {
                if(kv.toString().match(wetlandsDrainageRegEx[1])){
                    resultsArray[0]['ResultGroups']['Wetland Ecosystems']['Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
                }
                else if(kv.toString().match(wetlandsDrainageRegEx[2])){
                    resultsArray[0]['ResultGroups']['Wetland Ecosystems']['By Total Watershed Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
                }
                else if(kv.toString().match(wetlandsDrainageRegEx[3])){
                    resultsArray[0]['ResultGroups']['Wetland Ecosystems']['By Total Peninsula Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
                }
                else if(kv.toString().match(wetlandsDrainageRegEx[4])){
                    resultsArray[0]['ResultGroups']['Wetland Ecosystems']['By Total Ecosystem Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
                }
            }
        }
      }

      if (title == 'Land Use') {
        resultsArray[0]['FeatureName'] = aliasValuePairs[layer.toString()];
        for (kv in aliasValuePairs) {

          if (kv.toString().match(landuseDrainageRegEx[0])) {
            resultsArray[0]['ResultGroups']['Land Use']['Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(landcoverRegEx[0])) {
            resultsArray[0]['ResultGroups']['Land Use']['Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(landuseDrainageRegEx[1])) {
            resultsArray[0]['ResultGroups']['Land Use']['By Total Watershed Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(landuseDrainageRegEx[2])) {
            resultsArray[0]['ResultGroups']['Land Use']['By Total Peninsula Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(landuseDrainageRegEx[3])) {
            resultsArray[0]['ResultGroups']['Land Use']['By Total Land Use Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
        }
      }

      if (title == 'Wildfires') {
        resultsArray[0]['FeatureName'] = aliasValuePairs[layer.toString()];
        for (kv in aliasValuePairs) {
          if (kv.toString().match(wildfireRegEx[0])) {
            resultsArray[0]['ResultGroups']['Wildfires']['Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(wildfireRegEx[1])) {
          resultsArray[0]['ResultGroups']['Wildfires']['By Total Watershed Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(wildfireRegEx[2])) {
          resultsArray[0]['ResultGroups']['Wildfires']['By Total Peninsula Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(wildfireRegEx[3])) {
          resultsArray[0]['ResultGroups']['Wildfires']['By Total Area Burned']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
        }
      }

      if (title == 'Impervious') {
        resultsArray[0]['FeatureName'] = aliasValuePairs[layer.toString()];
        for (kv in aliasValuePairs) {
          if (kv.toString().match(imperviousRegEx[0])) {
            resultsArray[0]['ResultGroups']['Impervious']['Area']['Rows'][kv] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(imperviousRegEx[1])) {
          resultsArray[0]['ResultGroups']['Impervious']['By Total Watershed Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(imperviousRegEx[2])) {
          resultsArray[0]['ResultGroups']['Impervious']['By Total Peninsula Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          
        }
      }

      if (title == 'Land Cover') {
        resultsArray[0]['FeatureName'] = aliasValuePairs[layer.toString()];
        for (kv in aliasValuePairs) {
          if (kv.toString().match(landcoverRegEx[0])) {
            resultsArray[0]['ResultGroups']['Land Cover']['Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(landcoverRegEx[1])) {
          resultsArray[0]['ResultGroups']['Land Cover']['By Total Watershed Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(landcoverRegEx[2])) {
          resultsArray[0]['ResultGroups']['Land Cover']['By Total Peninsula Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(landcoverRegEx[3])) {
          resultsArray[0]['ResultGroups']['Land Cover']['By Total Ecosystem Type']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
        }
      }

      if (title == 'Spruce Bark Beetle') {
        resultsArray[0]['FeatureName'] = aliasValuePairs[layer.toString()];
        for (kv in aliasValuePairs) {
          if (kv.toString().match(sbbRegEx[0])) {
            resultsArray[0]['ResultGroups']['Spruce Bark Beetle']['Area of SBB Kill Present']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(sbbRegEx[1])) {
          resultsArray[0]['ResultGroups']['Spruce Bark Beetle']['By Total Watershed Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(sbbRegEx[2])) {
          resultsArray[0]['ResultGroups']['Spruce Bark Beetle']['By Total Peninsula Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(sbbRegEx[3])) {
          resultsArray[0]['ResultGroups']['Spruce Bark Beetle']['By Total Area Killed by Year']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
        }
      }

      if (title == 'Logging') {
        resultsArray[0]['FeatureName'] = aliasValuePairs[layer.toString()];
        for (kv in aliasValuePairs) {
          if (kv.toString().match(loggingRegEx[0])) {
            resultsArray[0]['ResultGroups']['Logging']['Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(loggingRegEx[1])) {
          resultsArray[0]['ResultGroups']['Logging']['By Total Watershed Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(loggingRegEx[2])) {
          resultsArray[0]['ResultGroups']['Logging']['By Total Peninsula Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(loggingRegEx[3])) {
          resultsArray[0]['ResultGroups']['Logging']['By Total Area Logged']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
        }
      }

      if (title == 'Land Ownership') {
        resultsArray[0]['FeatureName'] = aliasValuePairs[layer.toString()];
        for (kv in aliasValuePairs) {
          if (kv.toString().match(landcoverRegEx[0])) {
            resultsArray[0]['ResultGroups']['Ownership']['Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(landcoverRegEx[1])) {
          resultsArray[0]['ResultGroups']['Ownership']['By Total Watershed Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(landcoverRegEx[2])) {
          resultsArray[0]['ResultGroups']['Ownership']['By Total Peninsula Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(landcoverRegEx[3])) {
          resultsArray[0]['ResultGroups']['Ownership']['By Total Ownership Type']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
        }
      }

      if (title == 'Soil Drainage') {
        resultsArray[0]['FeatureName'] = aliasValuePairs[layer.toString()];
        for (kv in aliasValuePairs) {
          if (kv.toString().match(landcoverRegEx[0])) {
            resultsArray[0]['ResultGroups']['Soil Drainage']['Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(landcoverRegEx[1])) {
          resultsArray[0]['ResultGroups']['Soil Drainage']['By Total Watershed Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(landcoverRegEx[2])) {
          resultsArray[0]['ResultGroups']['Soil Drainage']['By Total Peninsula Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(landcoverRegEx[3])) {
          resultsArray[0]['ResultGroups']['Soil Drainage']['By Total Drainage Type']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
        }
      }
     
      if (title == 'Invasives') {
        resultsArray[0]['FeatureName'] = aliasValuePairs[layer.toString()];
        for (kv in aliasValuePairs) {
          // Pike invasives
          if (kv.toString().match(invasivesRegEx[10])) {
            if (kv.toString().match(invasivesRegEx[11])) {
              resultsArray[0]['ResultGroups']['Northern Pike']['Northern Pike Lake Status']['Rows'][stripString(kv)] = aliasValuePairs[kv];
            }
            else if (kv.toString().match(invasivesRegEx[5])) {
            resultsArray[0]['ResultGroups']['Northern Pike']['Northern Pike Lake Status By Total Watershed Pike Lakes']['Rows'][stripString(kv)] = aliasValuePairs[kv];
            }
            else if (kv.toString().match(invasivesRegEx[7])) {
            resultsArray[0]['ResultGroups']['Northern Pike']['Northern Pike Lake Status By Each Northern Pike Lake Status Type']['Rows'][stripString(kv)] = aliasValuePairs[kv];
            }
            else if (kv.toString().match(invasivesRegEx[1])) {
            resultsArray[0]['ResultGroups']['Northern Pike']['Northern Pike Stream Length']['Rows'][kv] = aliasValuePairs[kv];
            }
            else if (kv.toString().match(invasivesRegEx[6])) {
            resultsArray[0]['ResultGroups']['Northern Pike']['Northern Pike Stream By Total Length in Watershed']['Rows'][stripString(kv)] = aliasValuePairs[kv];
            }
            else if (kv.toString().match(invasivesRegEx[8])) {
            resultsArray[0]['ResultGroups']['Northern Pike']['Northern Pike Stream By Total Length on Peninsula']['Rows'][stripString(kv)] = aliasValuePairs[kv];
            }
            else if (kv.toString().match(invasivesRegEx[9])) {
            resultsArray[0]['ResultGroups']['Northern Pike']['Northern Pike Stream By Each Northern Pike Stream Type']['Rows'][stripString(kv)] = aliasValuePairs[kv];
            }            
          }
          //AKEPIC Invasives
          else {
            
            if (kv.toString().match(invasivesRegEx[0])) {
              resultsArray[0]['ResultGroups']['Plants: Alaska Exotic Plants Information Clearinghouse (AKEPIC Invasives)']['AKEPIC Invasive Aggressiveness Status']['Rows'][stripString(kv)] = aliasValuePairs[kv];
            }
            else if (kv.toString().match(invasivesRegEx[2])) {
            resultsArray[0]['ResultGroups']['Plants: Alaska Exotic Plants Information Clearinghouse (AKEPIC Invasives)']['AKEPIC Invasive Aggressiveness Status by Total Watershed AKEPIC Invasive']['Rows'][stripString(kv)] = aliasValuePairs[kv];
            }
            else if (kv.toString().match(invasivesRegEx[3])) {
            resultsArray[0]['ResultGroups']['Plants: Alaska Exotic Plants Information Clearinghouse (AKEPIC Invasives)']['AKEPIC Invasive Aggressiveness Status By Each AKEPIC Invasive Status Type']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
            else if (kv.toString().match(invasivesRegEx[12])) {
            resultsArray[0]['ResultGroups']['Plants: Alaska Exotic Plants Information Clearinghouse (AKEPIC Invasives)']['Total Invasives In Watershed']['Rows'][kv] = aliasValuePairs[kv];
          }
        }
        }
      }

     if (title == 'Stream Crossing') {
        resultsArray[0]['FeatureName'] = aliasValuePairs[layer.toString()];
        for (kv in aliasValuePairs) {
          // Culverts
          //if (kv.toString().match(streamCrossingRegEx[7])) {
          if (kv.toString().match(streamCrossingRegEx[9])) {
            resultsArray[0]['ResultGroups']['Stream Crossings']['Culverts Ranked for Juvenile Salmonid Passage']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
            else if (kv.toString().match(streamCrossingRegEx[0])) {
              resultsArray[0]['ResultGroups']['Stream Crossings']['Structure']['Rows'][stripString(kv)] = aliasValuePairs[kv];
            }
            else if (kv.toString().match(streamCrossingRegEx[11])) {
              resultsArray[0]['ResultGroups']['Streams']['Total State Salmon Stream Length In Watershed']['Rows'][kv] = aliasValuePairs[kv];
            }
              else if (kv.toString().match(streamCrossingRegEx[8])) {
              resultsArray[0]['ResultGroups']['Stream Crossings']['Total Structures and Culverts']['Rows'][kv] = aliasValuePairs[kv];
            }
              else if (kv.toString().match(streamCrossingRegEx[9])) {
              resultsArray[0]['ResultGroups']['Stream Crossings']['Culverts Ranked for Juvenile Salmonid Passage']['Rows'][stripString(kv)] = aliasValuePairs[kv];
            }
              else if (kv.toString().match(streamCrossingRegEx[2])) {
              resultsArray[0]['ResultGroups']['Stream Crossings']['Culvert Rank By Total Watershed Culverts']['Rows'][stripString(kv)] = aliasValuePairs[kv];
            }
               else if (kv.toString().match(streamCrossingRegEx[3])) {
              resultsArray[0]['ResultGroups']['Stream Crossings']['Culvert Rank By Each Culvert Rank Type']['Rows'][stripString(kv)] = aliasValuePairs[kv];
            }
              
          
         // }
          //Streams
            //else {
            else if (kv.toString().match(streamCrossingRegEx[10])) {
              resultsArray[0]['ResultGroups']['Streams']['Upstream Restored lengths']['Rows'][kv] = aliasValuePairs[kv];
            }
            else if (kv.toString().match(streamCrossingRegEx[1])) {
            resultsArray[0]['ResultGroups']['Streams']['State Salmon Stream Lengths']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
            else if (kv.toString().match(streamCrossingRegEx[4])) {
            resultsArray[0]['ResultGroups']['Streams']['State Salmon Streams By Total Length in Watershed']['Rows'][stripString(kv)] = aliasValuePairs[kv];
            }
            else if (kv.toString().match(streamCrossingRegEx[5])) {
            resultsArray[0]['ResultGroups']['Streams']['State Salmon Streams By Total Length on Peninsula']['Rows'][stripString(kv)] = aliasValuePairs[kv];
            }
            else if (kv.toString().match(streamCrossingRegEx[6])) {
            resultsArray[0]['ResultGroups']['Streams']['State Salmon Streams By Each State Salmon Stream Type']['Rows'][stripString(kv)] = aliasValuePairs[kv];
            }
            
          //}
        }
      }
      if (title == 'Footprints') {
        resultsArray[0]['FeatureName'] = aliasValuePairs[layer.toString()];
        for (kv in aliasValuePairs) {
        if (kv.toString().match(footprintsRegEx[1])) {
          resultsArray[0]['ResultGroups']['Footprints']['Length']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(footprintsRegEx[5])) {
          resultsArray[0]['ResultGroups']['Footprints']['Density']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(footprintsRegEx[6])) {
            resultsArray[0]['ResultGroups']['Footprints']['By Total Footprint Type']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(footprintsRegEx[0])) {
            resultsArray[0]['ResultGroups']['Footprints']['Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(footprintsRegEx[2])) {
          resultsArray[0]['ResultGroups']['Footprints']['By Total Watershed Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(footprintsRegEx[3])) {
          resultsArray[0]['ResultGroups']['Footprints']['By Total Peninsula Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(footprintsRegEx[4])) {
          resultsArray[0]['ResultGroups']['Footprints']['Area By Total Footprint Type']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
        }
      }

      if (title == 'Hydrology and Watersheds' && layer.toUpperCase() == 'REGION') {
        resultsArray[0]['FeatureName'] = aliasValuePairs[layer.toString()];
        for (kv in aliasValuePairs) {
          if (kv.toString().match(hydrologyRegEx[3])) {
            resultsArray[0]['ResultGroups']['Hydrology']['By Total Peninsula Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(hydrologyRegEx[1])) {
          resultsArray[0]['ResultGroups']['Hydrology']['Length']['Rows'][kv] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(hydrologyRegEx[2])) {
          resultsArray[0]['ResultGroups']['Hydrology']['Drainage Density']['Rows'][kv] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(hydrologyRegEx[0])) {
          resultsArray[0]['ResultGroups']['Hydrology']['Watershed Area']['Rows'][kv] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(hydrologyRegEx[4])) {
          resultsArray[0]['ResultGroups']['Hydrology']['Number Of Watersheds per Region']['Rows'][kv] = aliasValuePairs[kv];
          }
        }
      }
      if (title == 'Hydrology and Watersheds' && layer.toUpperCase() == 'WATERSHED') {
        resultsArray[0]['FeatureName'] = aliasValuePairs[layer.toString()];
        for (kv in aliasValuePairs) {
          if (kv.toString().match(hydrologyRegEx[3])) {
            resultsArray[0]['ResultGroups']['Hydrology']['By Total Peninsula Area']['Rows'][stripString(kv)] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(hydrologyRegEx[1])) {
          resultsArray[0]['ResultGroups']['Hydrology']['Length']['Rows'][kv] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(hydrologyRegEx[2])) {
          resultsArray[0]['ResultGroups']['Hydrology']['Drainage Density']['Rows'][kv] = aliasValuePairs[kv];
          }
          else if (kv.toString().match(hydrologyRegEx[0])) {
          resultsArray[0]['ResultGroups']['Hydrology']['Watershed Area']['Rows'][kv] = aliasValuePairs[kv];
          }
        }
      }
    return resultsArray;
}

function createXML(structuredArray){
    /*
      <Feature Name="">
        <Group Name="">
            <Header Name="">
                <ColumnTitle Name="" Value=""/>
                <Attribute Name="" Value="" />
            </Header>
        </Group>
      </Feature>
    */

    var xmlDoc = getXMLDoc(); 
    
    if(navigator.appName == "Microsoft Internet Explorer")
    {
        xmlDoc.loadXML("<?xml version='1.0' encoding='utf-8' ?><Results></Results>");
        var root = xmlDoc.documentElement;
        for (var i=0; i<structuredArray.length; ++i)
        {   
            //create a node for each feature
            var featureNode = xmlDoc.createNode(1, "Feature", "");
            featureNode.setAttribute("Name", structuredArray[i]['FeatureName'].toString());
            for (group in structuredArray[i]['ResultGroups'])
            {
                // for each resultgroup we need to create a node 
                var groupNode = xmlDoc.createNode(1, "Group", "");
                groupNode.setAttribute("Name", group);
                for (header in structuredArray[i]['ResultGroups'][group])
                {
                    var headerNode = xmlDoc.createNode(1, "Header", "");
                    headerNode.setAttribute("Name", header);               
                    var columnTitle = xmlDoc.createNode(1, "ColumnTitle", "");
                    if (structuredArray[i]['ResultGroups'][group][header]['ColumnTitles'].length > 0)
                    {
                        columnTitle.setAttribute("Name",structuredArray[i]['ResultGroups'][group][header]['ColumnTitles'][0]);
                        columnTitle.setAttribute("Value",structuredArray[i]['ResultGroups'][group][header]['ColumnTitles'][1]);
                    }
                    headerNode.appendChild(columnTitle);
                    // loop on the group node's matched attributes and add them into 
                    // the group as child nodes.
                    for (attribute in structuredArray[i]['ResultGroups'][group][header]['Rows'])
                    {
                        var attrNode = xmlDoc.createNode(1, "Attribute", "");
                        attrNode.setAttribute("Name", attribute);
                        attrNode.setAttribute("Value", structuredArray[i]['ResultGroups'][group][header]['Rows'][attribute]);
                        headerNode.appendChild(attrNode);
                    }
                    groupNode.appendChild(headerNode); 
                }
                featureNode.appendChild(groupNode);   
            }
            // append each group node to the root feature
            root.appendChild(featureNode);  
        }
    }
    else{
   
        xmlDoc = document.implementation.createDocument("", "Results", null);
        var root = xmlDoc.documentElement;

        for (var i=0; i<structuredArray.length; ++i)
        {   
            //create a node for each feature
            var featureNode = xmlDoc.createElement("Feature");
            var newAttr = xmlDoc.createAttribute("Name");
            newAttr.nodeValue = structuredArray[i]['FeatureName'].toString();
            featureNode.setAttributeNode(newAttr);
            
            for (group in structuredArray[i]['ResultGroups']){
                // for each resultgroup we need to create a node 
                var groupNode = xmlDoc.createElement("Group");
                var grpAttr = xmlDoc.createAttribute("Name");
                grpAttr.nodeValue = group;
                groupNode.setAttributeNode(grpAttr);
                
                for (header in structuredArray[i]['ResultGroups'][group]){
                    var headerNode = xmlDoc.createElement("Header");
                    var headerAttr = xmlDoc.createAttribute("Name");
                    headerAttr.nodeValue = header;
                    headerNode.setAttributeNode(headerAttr);
                    var columnTitle = xmlDoc.createElement( "ColumnTitle");
                    if (structuredArray[i]['ResultGroups'][group][header]['ColumnTitles'].length > 0){
                        columnTitle.setAttribute("Name",structuredArray[i]['ResultGroups'][group][header]['ColumnTitles'][0]);
                        columnTitle.setAttribute("Value",structuredArray[i]['ResultGroups'][group][header]['ColumnTitles'][1]);                   
                    }
                   
                    headerNode.appendChild(columnTitle);
                    // loop on the group node's matched attributes and add them into 
                    // the group as child nodes.
                    for (attribute in structuredArray[i]['ResultGroups'][group][header]['Rows'])
                    {
                        var attrNode = xmlDoc.createElement("Attribute");
                        attrNode.setAttribute("Name", attribute);
                        attrNode.setAttribute("Value", structuredArray[i]['ResultGroups'][group][header]['Rows'][attribute]);
                        headerNode.appendChild(attrNode);
                    }
                    groupNode.appendChild(headerNode); 
                }
              featureNode.appendChild(groupNode);   
            }
            // append each group node to the root feature
           root.appendChild(featureNode);  
        }
    }
          
    return xmlDoc;
}
function stripString(string){
    
    if (string.indexOf('%')>0){
        return TrimString(string.substring(0,string.indexOf('%')));
    }
    else if (string.indexOf('area burned') > 0) {
    return TrimString(string.substring(0, string.indexOf('area burned')));
  }
    else if (string.indexOf('area logged') > 0) {
    return TrimString(string.substring(0, string.indexOf('area logged')));
  }
    else if (string.indexOf('area of SBB kill present') > 0) {
    return TrimString(string.substring(0, string.indexOf('area of SBB kill present')));
  }
    else if (string.indexOf('count') > 0) {
    return TrimString(string.substring(0, string.indexOf('count')));
  }
    else if (string.indexOf('density in watershed') > 0) {
    return TrimString(string.substring(0, string.indexOf('density in watershed')));
  }
    else if (string.indexOf('length') > 0) {
    return TrimString(string.substring(0, string.indexOf('length')));
  }
    else if (string.indexOf('area (acres') > 0) {
    return TrimString(string.substring(0, string.indexOf('area (acres)')));
  }
    else {
        return TrimString(string.substring(0,string.indexOf('area')));
    }
}
function TrimString(sInString) {
  sInString = sInString.replace( /^\s+/g, "" );// strip leading
  return sInString.replace( /\s+$/g, "" );// strip trailing
}


StructuredArray = function(featureCount){
    this.structuredArray = new Array();
   // if (title == 'Land Use' && layer == 'REGION') {
//    if  (layer == 'REGION'){
//        this.headers = ['Area', 'By Total Watershed Area', 'By Total Peninsula Area', 'By Total Land Use Area'];
//        this.headersColumnTitles = [['Type', 'Acres'], ['Type', 'Percent'],['Type', 'Percent'],['Type', 'Percent']];
//        for (i=0; i < featureCount; i++){
//            this.structuredArray[i] = new Array();
//            this.structuredArray[i]['FeatureName'] = "";
//            this.structuredArray[i]['ResultGroups'] = new Array();
//            this.structuredArray[i]['ResultGroups']['Land Use'] = new Array();
//            for(j=0; j<this.headers.length; j++){
//               this.structuredArray[i]['ResultGroups']['Land Use'][this.headers[j]] = new Array();
//               this.structuredArray[i]['ResultGroups']['Land Use'][this.headers[j]]['ColumnTitles'] = this.headersColumnTitles[j];
//               this.structuredArray[i]['ResultGroups']['Land Use'][this.headers[j]]['Rows']=new Array();
//            }
//        }
    //    }
//      if  (layer == 'REGION'){
//        this.headers = ['Area', 'Perimeter', 'By Total Peninsula Area', 'Miles', 'Number of Watersheds'];
//        this.headersColumnTitles = [['Type', 'Acres'], ['Type', 'Acres'],['Type', 'Percent'],['Type', 'Acres'],['Type', 'Acres']];
//        for (i=0; i < featureCount; i++){
//            this.structuredArray[i] = new Array();
//            this.structuredArray[i]['FeatureName'] = "";
//            this.structuredArray[i]['ResultGroups'] = new Array();
//            this.structuredArray[i]['ResultGroups']['Region'] = new Array();
//            for(j=0; j<this.headers.length; j++){
//               this.structuredArray[i]['ResultGroups']['Region'][this.headers[j]] = new Array();
//               this.structuredArray[i]['ResultGroups']['Region'][this.headers[j]]['ColumnTitles'] = this.headersColumnTitles[j];
//               this.structuredArray[i]['ResultGroups']['Region'][this.headers[j]]['Rows']=new Array();
//            }
//        }
//     }

    if  (title == 'Wetlands'){
        this.headers = ['Area', 'By Total Watershed Area', 'By Total Peninsula Area'];
        this.headersColumnTitles = [['Type', 'Acres'], ['Type', 'Percent'],['Type', 'Percent']];
        this.ecosystemSpecialHeaders = ['By Total Ecosystem Area'];
        this.ecosystemSpecialHeadersColumnTitles = [['Type', 'Percent']];
        this.habitatSpecialHeaders = ['By Total Habitat Area']
        this.habitatSpecialHeadersColumnTitles = [['Type', 'Percent']];
        for (i=0; i < featureCount; i++){
            this.structuredArray[i] = new Array();
            this.structuredArray[i]['FeatureName'] = "";
            this.structuredArray[i]['ResultGroups'] = new Array();
            this.structuredArray[i]['ResultGroups']['Wetland Ecosystems'] = new Array();
            this.structuredArray[i]['ResultGroups']['Habitats'] = new Array();
            for(j=0; j<this.headers.length; j++){
               this.structuredArray[i]['ResultGroups']['Wetland Ecosystems'][this.headers[j]] = new Array();
               this.structuredArray[i]['ResultGroups']['Wetland Ecosystems'][this.headers[j]]['ColumnTitles'] = this.headersColumnTitles[j];
               this.structuredArray[i]['ResultGroups']['Wetland Ecosystems'][this.headers[j]]['Rows']=new Array();
               this.structuredArray[i]['ResultGroups']['Habitats'][this.headers[j]] = new Array();
               this.structuredArray[i]['ResultGroups']['Habitats'][this.headers[j]]['ColumnTitles'] = this.headersColumnTitles[j];
               this.structuredArray[i]['ResultGroups']['Habitats'][this.headers[j]]['Rows']=new Array();
            }
            for(j=0; j<this.ecosystemSpecialHeaders.length; j++){
               this.structuredArray[i]['ResultGroups']['Wetland Ecosystems'][this.ecosystemSpecialHeaders[j]] = new Array();
               this.structuredArray[i]['ResultGroups']['Wetland Ecosystems'][this.ecosystemSpecialHeaders[j]]['ColumnTitles'] = this.ecosystemSpecialHeadersColumnTitles[j];
               this.structuredArray[i]['ResultGroups']['Wetland Ecosystems'][this.ecosystemSpecialHeaders[j]]['Rows']=new Array();
            }
            for(j=0; j<this.habitatSpecialHeaders.length; j++){
               this.structuredArray[i]['ResultGroups']['Habitats'][this.habitatSpecialHeaders[j]] = new Array();
               this.structuredArray[i]['ResultGroups']['Habitats'][this.habitatSpecialHeaders[j]]['ColumnTitles'] = this.habitatSpecialHeadersColumnTitles[j];
               this.structuredArray[i]['ResultGroups']['Habitats'][this.habitatSpecialHeaders[j]]['Rows']=new Array();
            }
        }
      }

      if (title == 'Invasives') {
        this.headers = ['AKEPIC Invasive Aggressiveness Status', 'Total Invasives In Watershed', 'AKEPIC Invasive Aggressiveness Status by Total Watershed AKEPIC Invasive', 'AKEPIC Invasive Aggressiveness Status By Each AKEPIC Invasive Status Type'];
        this.headersColumnTitles = [['Type', 'Count'], ['Type', 'Count'], ['Type', 'Percent'], ['Type', 'Percent']];

        this.ecosystemSpecialHeaders = ['Northern Pike Lake Status', 'Northern Pike Lake Status By Total Watershed Pike Lakes', 'Northern Pike Lake Status By Each Northern Pike Lake Status Type', 'Northern Pike Stream Length',
        'Northern Pike Stream By Total Length in Watershed', 'Northern Pike Stream By Total Length on Peninsula', 'Northern Pike Stream By Each Northern Pike Stream Type'];
        this.ecosystemSpecialHeadersColumnTitles = [['Type', 'Count'], ['Type', 'Percent'], ['Type', 'Percent'], ['Type', 'Miles'], ['Type', 'Percent'],
        ['Type', 'Percent'], ['Type', 'Percent']];
        for (i=0; i < featureCount; i++){
            this.structuredArray[i] = new Array();
            this.structuredArray[i]['FeatureName'] = "";
            this.structuredArray[i]['ResultGroups'] = new Array();
            this.structuredArray[i]['ResultGroups']['Northern Pike'] = new Array();
            this.structuredArray[i]['ResultGroups']['Plants: Alaska Exotic Plants Information Clearinghouse (AKEPIC Invasives)'] = new Array();
            for (j = 0; j < this.ecosystemSpecialHeaders.length; j++) {
              this.structuredArray[i]['ResultGroups']['Northern Pike'][this.ecosystemSpecialHeaders[j]] = new Array();
              this.structuredArray[i]['ResultGroups']['Northern Pike'][this.ecosystemSpecialHeaders[j]]['ColumnTitles'] = this.ecosystemSpecialHeadersColumnTitles[j];
              this.structuredArray[i]['ResultGroups']['Northern Pike'][this.ecosystemSpecialHeaders[j]]['Rows'] = new Array();
            }
            for(j=0; j<this.headers.length; j++){
              this.structuredArray[i]['ResultGroups']['Plants: Alaska Exotic Plants Information Clearinghouse (AKEPIC Invasives)'][this.headers[j]] = new Array();
              this.structuredArray[i]['ResultGroups']['Plants: Alaska Exotic Plants Information Clearinghouse (AKEPIC Invasives)'][this.headers[j]]['ColumnTitles'] = this.headersColumnTitles[j];
              this.structuredArray[i]['ResultGroups']['Plants: Alaska Exotic Plants Information Clearinghouse (AKEPIC Invasives)'][this.headers[j]]['Rows'] = new Array();
            }
            
        }
      }

      if (title == 'Stream Crossing') {
        this.headers = ['Structure', 'Total Structures and Culverts', 'Culverts Ranked for Juvenile Salmonid Passage', 'Culvert Rank By Total Watershed Culverts', 'Culvert Rank By Each Culvert Rank Type'];
        this.headersColumnTitles = [['Type', 'Count'], ['Type', 'Count'], ['Type', 'Count'], ['Type', 'Percent'], ['Type', 'Percent']];

        this.ecosystemSpecialHeaders = ['State Salmon Stream Lengths', 'Total State Salmon Stream Length In Watershed', 'State Salmon Streams By Total Length in Watershed', 'State Salmon Streams By Total Length on Peninsula', 'State Salmon Streams By Each State Salmon Stream Type', 'Upstream Restored lengths'];
        this.ecosystemSpecialHeadersColumnTitles = [['Type', 'Miles'], ['Type', 'Miles'], ['Type', 'Percent'], ['Type', 'Percent'], ['Type', 'Percent'], ['Type', 'Miles']];
        for (i = 0; i < featureCount; i++) {
          this.structuredArray[i] = new Array();
          this.structuredArray[i]['FeatureName'] = "";
          this.structuredArray[i]['ResultGroups'] = new Array();
          this.structuredArray[i]['ResultGroups']['Stream Crossings'] = new Array();
          this.structuredArray[i]['ResultGroups']['Streams'] = new Array();
          for (j = 0; j < this.headers.length; j++) {
            this.structuredArray[i]['ResultGroups']['Stream Crossings'][this.headers[j]] = new Array();
            this.structuredArray[i]['ResultGroups']['Stream Crossings'][this.headers[j]]['ColumnTitles'] = this.headersColumnTitles[j];
            this.structuredArray[i]['ResultGroups']['Stream Crossings'][this.headers[j]]['Rows'] = new Array();
          }
          for (j = 0; j < this.ecosystemSpecialHeaders.length; j++) {
            this.structuredArray[i]['ResultGroups']['Streams'][this.ecosystemSpecialHeaders[j]] = new Array();
            this.structuredArray[i]['ResultGroups']['Streams'][this.ecosystemSpecialHeaders[j]]['ColumnTitles'] = this.ecosystemSpecialHeadersColumnTitles[j];
            this.structuredArray[i]['ResultGroups']['Streams'][this.ecosystemSpecialHeaders[j]]['Rows'] = new Array();
          }
        }
      }
      
      if (title == 'Land Use') {
        this.headers = ['Area', 'By Total Watershed Area', 'By Total Peninsula Area', 'By Total Land Use Area'];
        this.headersColumnTitles = [['Type', 'Acres'], ['Type', 'Percent'], ['Type', 'Percent'], ['Type', 'Percent']];
        for (i = 0; i < featureCount; i++) {
          this.structuredArray[i] = new Array();
          this.structuredArray[i]['FeatureName'] = "";
          this.structuredArray[i]['ResultGroups'] = new Array();
          this.structuredArray[i]['ResultGroups']['Land Use'] = new Array();
          for (j = 0; j < this.headers.length; j++) {
            this.structuredArray[i]['ResultGroups']['Land Use'][this.headers[j]] = new Array();
            this.structuredArray[i]['ResultGroups']['Land Use'][this.headers[j]]['ColumnTitles'] = this.headersColumnTitles[j];
            this.structuredArray[i]['ResultGroups']['Land Use'][this.headers[j]]['Rows'] = new Array();
          }
        }
      }

      if (title == 'Wildfires') {
        this.headers = ['Area', 'By Total Watershed Area', 'By Total Peninsula Area', 'By Total Area Burned'];
        this.headersColumnTitles = [['Type', 'Acres'], ['Type', 'Percent'], ['Type', 'Percent'], ['Type', 'Percent']];
        for (i = 0; i < featureCount; i++) {
          this.structuredArray[i] = new Array();
          this.structuredArray[i]['FeatureName'] = "";
          this.structuredArray[i]['ResultGroups'] = new Array();
          this.structuredArray[i]['ResultGroups']['Wildfires'] = new Array();
          for (j = 0; j < this.headers.length; j++) {
            this.structuredArray[i]['ResultGroups']['Wildfires'][this.headers[j]] = new Array();
            this.structuredArray[i]['ResultGroups']['Wildfires'][this.headers[j]]['ColumnTitles'] = this.headersColumnTitles[j];
            this.structuredArray[i]['ResultGroups']['Wildfires'][this.headers[j]]['Rows'] = new Array();
          }
        }
      }

      if (title == 'Impervious') {
        this.headers = ['Area', 'By Total Watershed Area', 'By Total Peninsula Area'];
        this.headersColumnTitles = [['Type', 'Acres'], ['Type', 'Percent'], ['Type', 'Percent']];
        for (i = 0; i < featureCount; i++) {
          this.structuredArray[i] = new Array();
          this.structuredArray[i]['FeatureName'] = "";
          this.structuredArray[i]['ResultGroups'] = new Array();
          this.structuredArray[i]['ResultGroups']['Impervious'] = new Array();
          for (j = 0; j < this.headers.length; j++) {
            this.structuredArray[i]['ResultGroups']['Impervious'][this.headers[j]] = new Array();
            this.structuredArray[i]['ResultGroups']['Impervious'][this.headers[j]]['ColumnTitles'] = this.headersColumnTitles[j];
            this.structuredArray[i]['ResultGroups']['Impervious'][this.headers[j]]['Rows'] = new Array();
          }
        }
      }

      if (title == 'Land Cover') {
        this.headers = ['Area', 'By Total Watershed Area', 'By Total Peninsula Area', 'By Total Ecosystem Type'];
        this.headersColumnTitles = [['Type', 'Acres'], ['Type', 'Percent'], ['Type', 'Percent'], ['Type', 'Percent']];
        for (i = 0; i < featureCount; i++) {
          this.structuredArray[i] = new Array();
          this.structuredArray[i]['FeatureName'] = "";
          this.structuredArray[i]['ResultGroups'] = new Array();
          this.structuredArray[i]['ResultGroups']['Land Cover'] = new Array();
          for (j = 0; j < this.headers.length; j++) {
            this.structuredArray[i]['ResultGroups']['Land Cover'][this.headers[j]] = new Array();
            this.structuredArray[i]['ResultGroups']['Land Cover'][this.headers[j]]['ColumnTitles'] = this.headersColumnTitles[j];
            this.structuredArray[i]['ResultGroups']['Land Cover'][this.headers[j]]['Rows'] = new Array();
          }
        }
      }

      if (title == 'Spruce Bark Beetle') {
        this.headers = ['Area of SBB Kill Present', 'By Total Watershed Area', 'By Total Peninsula Area', 'By Total Area Killed by Year'];
        this.headersColumnTitles = [['Type', 'Acres'], ['Type', 'Percent'], ['Type', 'Percent'], ['Type', 'Percent']];
        for (i = 0; i < featureCount; i++) {
          this.structuredArray[i] = new Array();
          this.structuredArray[i]['FeatureName'] = "";
          this.structuredArray[i]['ResultGroups'] = new Array();
          this.structuredArray[i]['ResultGroups']['Spruce Bark Beetle'] = new Array();
          for (j = 0; j < this.headers.length; j++) {
            this.structuredArray[i]['ResultGroups']['Spruce Bark Beetle'][this.headers[j]] = new Array();
            this.structuredArray[i]['ResultGroups']['Spruce Bark Beetle'][this.headers[j]]['ColumnTitles'] = this.headersColumnTitles[j];
            this.structuredArray[i]['ResultGroups']['Spruce Bark Beetle'][this.headers[j]]['Rows'] = new Array();
          }
        }
      }

      if (title == 'Logging') {
        this.headers = ['Area', 'By Total Watershed Area', 'By Total Peninsula Area', 'By Total Area Logged'];
        this.headersColumnTitles = [['Type', 'Acres'], ['Type', 'Percent'], ['Type', 'Percent'], ['Type', 'Percent']];
        for (i = 0; i < featureCount; i++) {
          this.structuredArray[i] = new Array();
          this.structuredArray[i]['FeatureName'] = "";
          this.structuredArray[i]['ResultGroups'] = new Array();
          this.structuredArray[i]['ResultGroups']['Logging'] = new Array();
          for (j = 0; j < this.headers.length; j++) {
            this.structuredArray[i]['ResultGroups']['Logging'][this.headers[j]] = new Array();
            this.structuredArray[i]['ResultGroups']['Logging'][this.headers[j]]['ColumnTitles'] = this.headersColumnTitles[j];
            this.structuredArray[i]['ResultGroups']['Logging'][this.headers[j]]['Rows'] = new Array();
          }
        }
      }
      
      if (title == 'Land Ownership') {
        this.headers = ['Area', 'By Total Watershed Area', 'By Total Peninsula Area', 'By Total Ownership Type'];
        this.headersColumnTitles = [['Type', 'Acres'], ['Type', 'Percent'], ['Type', 'Percent'], ['Type', 'Percent']];
        for (i = 0; i < featureCount; i++) {
          this.structuredArray[i] = new Array();
          this.structuredArray[i]['FeatureName'] = "";
          this.structuredArray[i]['ResultGroups'] = new Array();
          this.structuredArray[i]['ResultGroups']['Ownership'] = new Array();
          for (j = 0; j < this.headers.length; j++) {
            this.structuredArray[i]['ResultGroups']['Ownership'][this.headers[j]] = new Array();
            this.structuredArray[i]['ResultGroups']['Ownership'][this.headers[j]]['ColumnTitles'] = this.headersColumnTitles[j];
            this.structuredArray[i]['ResultGroups']['Ownership'][this.headers[j]]['Rows'] = new Array();
          }
        }
      }

      if (title == 'Soil Drainage') {
        this.headers = ['Area', 'By Total Watershed Area', 'By Total Peninsula Area', 'By Total Drainage Type'];
        this.headersColumnTitles = [['Type', 'Acres'], ['Type', 'Percent'], ['Type', 'Percent'], ['Type', 'Percent']];
        for (i = 0; i < featureCount; i++) {
          this.structuredArray[i] = new Array();
          this.structuredArray[i]['FeatureName'] = "";
          this.structuredArray[i]['ResultGroups'] = new Array();
          this.structuredArray[i]['ResultGroups']['Soil Drainage'] = new Array();
          for (j = 0; j < this.headers.length; j++) {
            this.structuredArray[i]['ResultGroups']['Soil Drainage'][this.headers[j]] = new Array();
            this.structuredArray[i]['ResultGroups']['Soil Drainage'][this.headers[j]]['ColumnTitles'] = this.headersColumnTitles[j];
            this.structuredArray[i]['ResultGroups']['Soil Drainage'][this.headers[j]]['Rows'] = new Array();
          }
        }
      }

      if (title == 'Footprints') {
        this.headers = ['Length', 'Density', 'By Total Footprint Type', 'Area', 'By Total Watershed Area', 'By Total Peninsula Area', 'Area By Total Footprint Type'];
        this.headersColumnTitles = [ ['Type', 'Miles'], ['Type', 'Miles/Acre'],['Type', 'Percent'],['Type', 'Acres'],['Type', 'Percent'], ['Type', 'Percent'], ['Type', 'Percent']];
        for (i = 0; i < featureCount; i++) {
          this.structuredArray[i] = new Array();
          this.structuredArray[i]['FeatureName'] = "";
          this.structuredArray[i]['ResultGroups'] = new Array();
          this.structuredArray[i]['ResultGroups']['Footprints'] = new Array();
          for (j = 0; j < this.headers.length; j++) {
            this.structuredArray[i]['ResultGroups']['Footprints'][this.headers[j]] = new Array();
            this.structuredArray[i]['ResultGroups']['Footprints'][this.headers[j]]['ColumnTitles'] = this.headersColumnTitles[j];
            this.structuredArray[i]['ResultGroups']['Footprints'][this.headers[j]]['Rows'] = new Array();
          }
        }
      }

      if (title == 'Hydrology and Watersheds' && layer.toUpperCase() == 'REGION') {
        this.headers = ['Length', 'Drainage Density', 'Watershed Area', 'By Total Peninsula Area', 'Number Of Watersheds per Region'];
        this.headersColumnTitles = [['Type', 'Miles'], ['Type', 'Miles/Acre'], ['Type', 'Acres'], ['Type', 'Percent'], ['Type', 'Count']];
        for (i = 0; i < featureCount; i++) {
          this.structuredArray[i] = new Array();
          this.structuredArray[i]['FeatureName'] = "";
          this.structuredArray[i]['ResultGroups'] = new Array();
          this.structuredArray[i]['ResultGroups']['Hydrology'] = new Array();
          for (j = 0; j < this.headers.length; j++) {
            this.structuredArray[i]['ResultGroups']['Hydrology'][this.headers[j]] = new Array();
            this.structuredArray[i]['ResultGroups']['Hydrology'][this.headers[j]]['ColumnTitles'] = this.headersColumnTitles[j];
            this.structuredArray[i]['ResultGroups']['Hydrology'][this.headers[j]]['Rows'] = new Array();
          }
        }
      }
      if (title == 'Hydrology and Watersheds' && layer.toUpperCase() == 'WATERSHED') {
        this.headers = ['Length', 'Drainage Density', 'Watershed Area', 'By Total Peninsula Area'];
        this.headersColumnTitles = [['Type', 'Miles'], ['Type', 'Miles/Acre'], ['Type', 'Acres'], ['Type', 'Percent']];
        for (i = 0; i < featureCount; i++) {
          this.structuredArray[i] = new Array();
          this.structuredArray[i]['FeatureName'] = "";
          this.structuredArray[i]['ResultGroups'] = new Array();
          this.structuredArray[i]['ResultGroups']['Hydrology'] = new Array();
          for (j = 0; j < this.headers.length; j++) {
            this.structuredArray[i]['ResultGroups']['Hydrology'][this.headers[j]] = new Array();
            this.structuredArray[i]['ResultGroups']['Hydrology'][this.headers[j]]['ColumnTitles'] = this.headersColumnTitles[j];
            this.structuredArray[i]['ResultGroups']['Hydrology'][this.headers[j]]['Rows'] = new Array();
          }
        }
      }

    return this.structuredArray;
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
 }

