﻿// JScript File
var shapeLayerWatersheds4;
var shapeLayerWatersheds5_1;
var shapeLayerWatersheds5_2;
var shapeLayerWatersheds5_3;
var shapeLayerWatersheds5_4;
var shapeLayerWatersheds6_1;
var shapeLayerWatersheds6_2;
var shapeLayerWatersheds6_3;
var shapeLayerWatersheds6_4;
var shapeLayerWatersheds6_5;
var shapeLayerWatersheds6_6;
var shapeLayerWatersheds6_7;
var shapeLayerWatersheds6_8;
var shapeLayerWatersheds6_9;
var shapeLayerWatersheds6_10;
var shapeLayerWatersheds6_11;
var shapeLayerWatersheds6_12;
var shapeLayerWatersheds6_13;
var shapeLayerWatersheds6_14;
var shapeLayerWatersheds6_15;
var shapeLayerWatersheds6_16;
//var Watersheds6Array = new Array(shapeLayerWatersheds6_1, shapeLayerWatersheds6_2, shapeLayerWatersheds6_3, shapeLayerWatersheds6_4, shapeLayerWatersheds6_5, shapeLayerWatersheds6_6, shapeLayerWatersheds6_7, shapeLayerWatersheds6_8, shapeLayerWatersheds6_9, shapeLayerWatersheds6_10, shapeLayerWatersheds6_11, shapeLayerWatersheds6_12, shapeLayerWatersheds6_13, shapeLayerWatersheds6_14, shapeLayerWatersheds6_15, shapeLayerWatersheds6_16); 
var shapeLayerCounties;
var shapeLayerEcoregions;
var shapeLayerBasins;
var selectedShape;
var initializeCounties = true;
var intializeWatersheds = true;
var intializeEcoregions = true;
var intializeBasins = true;
//var shapeLayerIndex = 1;
var hdfActivePlaceView; 
//var loadingDiv;
var keepSelectedPoly = true;
var isHuc4Initialized = false;
var isHuc5Initialized = false;
var isHuc6Initialized = false;
var isMapLoaded = false;
var mouseOverSelectedShape;
var control;

function toggleTabs(tab)
{
var overviewTab = $get('overviewtab');
var speciecTab = $get('speciestab');
var placeTab = $get('placetab');
var overviewDiv = $get('DivOverviewParent');
var SpeciesNav = $get('DivFrontPage');
var speciesDiv = $get('divSpeciesContent');
var placeDiv = $get('DivSpeciesListParent');
var overviewIntroDiv = $get('overviewSpeciesDiv');
switch (tab)
    {
    case 'overview':
//        if (!isMapLoaded)
//        {
//        GetMap();
//        }
        overviewTab.src = 'img/overviewtabactive.gif';
        speciecTab.src = 'img/speciestab.gif';
        placeTab.src = 'img/listTab.gif';   
        overviewDiv.style.visibility = 'visible';     
        overviewDiv.style.display = 'block';        
        speciesDiv.style.display = 'none';
        SpeciesNav.style.display = 'block';
        placeDiv.style.display = 'none'; 
        overviewIntroDiv.style.display = 'block';      
        break;
    case 'species':
        overviewTab.src = 'img/overviewtab.gif';
        speciecTab.src = 'img/speciestabactive.gif';
        placeTab.src = 'img/listTab.gif';
        overviewDiv.style.visibility = 'hidden';
        if (isMapLoaded)
        {
        overviewDiv.style.display = 'none';     
        }
        speciesDiv.style.display = 'block';
        SpeciesNav.style.display = 'block';
        placeDiv.style.display = 'none'; 
        overviewIntroDiv.style.display = 'none';  
        break;
    case 'place':
        overviewTab.src = 'img/overviewtab.gif';
        speciecTab.src = 'img/speciestab.gif';
        placeTab.src = 'img/listTabactive.gif';
        overviewDiv.style.display = 'none';
        speciesDiv.style.display = 'none';
        SpeciesNav.style.display = 'none';
        placeDiv.style.display = 'block'; 
        overviewIntroDiv.style.display = 'none';  
        break;    
    }
}

function DisplayVEMap(MapTab)
{
    var VEMapDiv = $get('MapDivParent');
    var VEMapTabsDiv = $get('MapDivTabs');   
    var MapIcon = $get('MapIcon');     
    VEMapDiv.style.display = 'block';
    VEMapDiv.style.visibility = 'visible';
    VEMapTabsDiv.style.display = 'block';  
    VEMapTabsDiv.style.visibility = 'visible';  
    MapIcon.style.display = 'none';  
    //var rect = new VELatLongRectangle(new VELatLong(46.2991,-124.552441), new VELatLong(41.991795, -116.463262), new VELatLong(46.2991, -116.463262), new VELatLong(41.991795, -124.552441)); 
    //shapeLayerCounties.GetBoundingRectangle();
    //map.SetMapView(rect);
    isMapLoaded = true;
    GetMap(MapTab);
}
function GetMap(MapTab)      
{   
//    if (!isMapLoaded)
//    {      
    map = new VEMap('myWildlifeMap');  
    //3d is disabled  
    map.LoadMap(new VELatLong(44.000717834, -121.706542), 6, 's',0, VEMapMode.Mode2D, 0); 
    hdfActivePlaceView = $get('hdfActivePlaceView');     
    //loadingDiv = $get('LayerLoading');
    addShapeLayers();  
    switch (MapTab)
    {
    case "Counties":
    ShowShapes('counties');      
    togglePlaceTabs('counties');   
    break;
    case "Ecoregions":
    ShowShapes('ecoregions');        
    togglePlaceTabs('ecoregions');
    break;
    case "Basins":
    ShowShapes('basins');        
    togglePlaceTabs('basins');   
    break;
    case "Watersheds":
    ShowShapes('watersheds');       
    togglePlaceTabs('watesheds');
    break;
    case "Address":
    //loadingDiv.style.display = "none";
    RemoveMyControl();
    break;
    default:
    break;
    }    
    map.EnableShapeDisplayThreshold(false);       
    map.AttachEvent("onmouseover", onMouseOverHandler);  
    map.AttachEvent("onmouseout", onMouseOutHandler);
    map.AttachEvent("onclick", onClickHandler);
    map.AttachEvent("onchangeview", onChangeView);      
    checkPlaceQuery();
    //isMapLoaded = true;
    //}   
}

function checkPlaceQuery()
{
isPlaceQuery = $get('hdfQueryStringPlace').value;
if (isPlaceQuery != "")
    {
    sPlace = isPlaceQuery.split(/\;/)[0];
    sGeoType = isPlaceQuery.split(/\;/)[1];     
    QueryStringSelection(sPlace, sGeoType);
    }    
}

function addShapeLayers()
{
    shapeLayerCounties = new VEShapeLayer();
    map.AddShapeLayer(shapeLayerCounties);
    
    shapeLayerEcoregions = new VEShapeLayer();
    map.AddShapeLayer(shapeLayerEcoregions);
    
    shapeLayerBasins = new VEShapeLayer();  
    map.AddShapeLayer(shapeLayerBasins); 
    
    shapeLayerWatersheds4 = new VEShapeLayer();
    map.AddShapeLayer(shapeLayerWatersheds4);   
    
    shapeLayerWatersheds5_1 = new VEShapeLayer();
    map.AddShapeLayer(shapeLayerWatersheds5_1);    
    shapeLayerWatersheds5_2 = new VEShapeLayer();
    map.AddShapeLayer(shapeLayerWatersheds5_2);    
    shapeLayerWatersheds5_3 = new VEShapeLayer();
    map.AddShapeLayer(shapeLayerWatersheds5_3);    
    shapeLayerWatersheds5_4 = new VEShapeLayer();    
    map.AddShapeLayer(shapeLayerWatersheds5_4); 
    
    shapeLayerWatersheds6_1 = new VEShapeLayer();    
    map.AddShapeLayer(shapeLayerWatersheds6_1);    
    shapeLayerWatersheds6_2 = new VEShapeLayer();    
    map.AddShapeLayer(shapeLayerWatersheds6_2);    
    shapeLayerWatersheds6_3 = new VEShapeLayer();    
    map.AddShapeLayer(shapeLayerWatersheds6_3);    
    shapeLayerWatersheds6_4 = new VEShapeLayer();    
    map.AddShapeLayer(shapeLayerWatersheds6_4);    
    shapeLayerWatersheds6_5 = new VEShapeLayer();    
    map.AddShapeLayer(shapeLayerWatersheds6_5);    
    shapeLayerWatersheds6_6 = new VEShapeLayer();    
    map.AddShapeLayer(shapeLayerWatersheds6_6);    
    shapeLayerWatersheds6_7 = new VEShapeLayer();    
    map.AddShapeLayer(shapeLayerWatersheds6_7);    
    shapeLayerWatersheds6_8 = new VEShapeLayer();    
    map.AddShapeLayer(shapeLayerWatersheds6_8);    
    shapeLayerWatersheds6_9 = new VEShapeLayer();    
    map.AddShapeLayer(shapeLayerWatersheds6_9);    
    shapeLayerWatersheds6_10 = new VEShapeLayer();    
    map.AddShapeLayer(shapeLayerWatersheds6_10);   
    shapeLayerWatersheds6_11 = new VEShapeLayer();    
    map.AddShapeLayer(shapeLayerWatersheds6_11);    
    shapeLayerWatersheds6_12 = new VEShapeLayer();    
    map.AddShapeLayer(shapeLayerWatersheds6_12);    
    shapeLayerWatersheds6_13 = new VEShapeLayer();    
    map.AddShapeLayer(shapeLayerWatersheds6_13);    
    shapeLayerWatersheds6_14 = new VEShapeLayer();    
    map.AddShapeLayer(shapeLayerWatersheds6_14);    
    shapeLayerWatersheds6_15 = new VEShapeLayer();    
    map.AddShapeLayer(shapeLayerWatersheds6_15);
    shapeLayerWatersheds6_16 = new VEShapeLayer();    
    map.AddShapeLayer(shapeLayerWatersheds6_16);
    
    
}
 function onFeedLoad(feed)
 {
    setShapeProps(feed);
//    if (feed.Name != 'Counties' && feed.Name != 'Basins')
//    {
//        feed.Hide();
//    }    
    if (feed.Name.search(/Huc/) == -1)
    {
    feed.Show();
    }
    else
    {
    onChangeView();
    }
    if (ProcessQuery == true)
    {
    doProcessQuery();
    }
//    if (!isMapLoaded)
//    {
//    var rect = shapeLayerCounties.GetBoundingRectangle();
//    map.SetMapView(rect);
//    }
//    if (add6thWatershed)
//    {
//    map.AddShape(selected6thField);
//    add6thWatershed = false;
//    }   
    //loadingDiv.style.display = "none";    
    RemoveMyControl();
 }
 
 function setShapeLayerColors(shape, shapeLayer, selected)
 {    
    switch (shapeLayer)
    {
        case "Counties":
            if (!selected)
            {
            shape.SetLineWidth(1);
            shape.SetLineColor(new VEColor(100,150,100,1.0));
            shape.SetFillColor(new VEColor(150,250,150,0.2)); 
            }
            else
            {
            shape.SetLineWidth(1);
            shape.SetLineColor(new VEColor(0,150,100,1.0));
            shape.SetFillColor(new VEColor(0,200,150,0.2)); 
            }
        break;
        case "ecoregions":
            if (!selected)
            {
            shape.SetLineWidth(1);
            shape.SetLineColor(new VEColor(68, 117, 91,1.0));
            shape.SetFillColor(new VEColor(68, 117, 91,0.2)); 
            }
            else
            {
            shape.SetLineWidth(1);
            shape.SetLineColor(new VEColor(0,150,100,1.0));
            shape.SetFillColor(new VEColor(0,200,150,0.2)); 
            }
         break;
        case "Basins":
            if (!selected)
            {
            shape.SetLineWidth(1);
            shape.SetLineColor(new VEColor(79, 48, 179,1.0));
            shape.SetFillColor(new VEColor(79, 48, 179,0.2)); 
            }
            else
            {
            shape.SetLineWidth(1);
            shape.SetLineColor(new VEColor(0,150,100,1.0));
            shape.SetFillColor(new VEColor(0,200,150,0.2)); 
            }
         break;
        case "Watersheds":
            if (!selected)
            {
            shape.SetLineWidth(1);
            shape.SetLineColor(new VEColor(61, 103, 153,1.0));
            shape.SetFillColor(new VEColor(61, 103, 153,0.2)); 
            }
            else
            {
            shape.SetLineWidth(1);
            shape.SetLineColor(new VEColor(0,150,100,1.0));
            shape.SetFillColor(new VEColor(0,200,150,0.2)); 
            }
         break;
        default:
        break;
        
    }  
    
 }
 
function turnOffMapDiv()
{
try
{
var MapDiv = $get('MapDivParent');
MapDiv.style.display = 'none';
}
catch (ex)
{
var test  = ex;
}

} 
function txtBoxSubmit(e)
 {    
    if (e && e.keyCode == 13)
    {
        if (e.srcElement.value != "" && e.srcElement.id != "tbSpecies")
        {
            if (!isMapLoaded)
            {
                DisplayVEMap('Address');
            }
         map.Find(null, e.srcElement.value);
        }
        else{
        if (e && e.preventDefault)
        //autoComplete1_completionListElem
            e.preventDefault(); // DOM style  
            e.returnValue = false;  
            var browser = getBrowser();
            if (browser != "Netscape")
            {            
                var srcElement = e.srcElement.id.replace("tb", "");
                var AlertDiv = 'SelectAlert'+srcElement;                
                    AlertDiv = $get(AlertDiv);                    
                    AlertDiv.style.display = 'block';
                    AlertDiv.focus();
            }
}
        return false;
    }
 }
 
function getBrowser()
        {
        var Browser = navigator.appName;       
        return Browser;
        }
                 
function onChangeView()
{
    var divHUCinViewLabel = $get('HUCLevelinView');
    if (hdfActivePlaceView.value == 'watersheds')
    {
        var zoomLevel = map.GetZoomLevel();        
        if (zoomLevel <9)
        {
            if (isHuc4Initialized)
            {   
                divHUCinViewLabel.innerHTML = '<b>4th</b> Field Watersheds in view';
                divHUCinViewLabel.style.display = 'block';
                shapeLayerWatersheds4.Show();
                toggle5thFieldHucs('hide');   
                toggle6thFieldHucs('hide');         
            }
            else
            {
            loadWatersheds4th();
            }
        }    
        else if (zoomLevel <11)
        {
            if (isHuc5Initialized)
            {   
                divHUCinViewLabel.innerHTML = '<b>5th</b> Field Watersheds in view';
                divHUCinViewLabel.style.display = 'block';
                shapeLayerWatersheds4.Hide();
                toggle5thFieldHucs('show');   
                toggle6thFieldHucs('hide');    
            }
            else
            {
            loadWatersheds5th();
            }
        }    
        else
        {
            if (isHuc6Initialized)
            {
                divHUCinViewLabel.innerHTML = '<b>6th</b> field watersheds in view';
                divHUCinViewLabel.style.display = 'block';
                shapeLayerWatersheds4.Hide();
                toggle5thFieldHucs('hide');   
                toggle6thFieldHucs('show');    
            }
            else
            {
            loadWatersheds6th();
            }
        }
    }
    else
    {
        divHUCinViewLabel.style.display = 'none';
        shapeLayerWatersheds4.Hide();
        toggle5thFieldHucs('hide');   
        toggle6thFieldHucs('hide');      
    }    
}

function toggle5thFieldHucs(visible)
{
    if (visible == 'hide')
    {
    shapeLayerWatersheds5_1.Hide();
    shapeLayerWatersheds5_2.Hide();
    shapeLayerWatersheds5_3.Hide();
    shapeLayerWatersheds5_4.Hide();
    }
    else
    {
    shapeLayerWatersheds5_1.Show();
    shapeLayerWatersheds5_2.Show();
    shapeLayerWatersheds5_3.Show();
    shapeLayerWatersheds5_4.Show();
    }
}

function toggle6thFieldHucs(visible)
{
    if (visible == 'hide')
    {
    shapeLayerWatersheds6_1.Hide();
    shapeLayerWatersheds6_2.Hide();
    shapeLayerWatersheds6_3.Hide();
    shapeLayerWatersheds6_4.Hide();
    shapeLayerWatersheds6_5.Hide();
    shapeLayerWatersheds6_6.Hide();
    shapeLayerWatersheds6_7.Hide();
    shapeLayerWatersheds6_8.Hide();
    shapeLayerWatersheds6_9.Hide();
    shapeLayerWatersheds6_10.Hide();
    shapeLayerWatersheds6_11.Hide();
    shapeLayerWatersheds6_12.Hide();
    shapeLayerWatersheds6_13.Hide();
    shapeLayerWatersheds6_14.Hide();
    shapeLayerWatersheds6_15.Hide();
    shapeLayerWatersheds6_16.Hide();
    }
    else
    {
    shapeLayerWatersheds6_1.Show();
    shapeLayerWatersheds6_2.Show();
    shapeLayerWatersheds6_3.Show();
    shapeLayerWatersheds6_4.Show();
    shapeLayerWatersheds6_5.Show();
    shapeLayerWatersheds6_6.Show();
    shapeLayerWatersheds6_7.Show();
    shapeLayerWatersheds6_8.Show();
    shapeLayerWatersheds6_9.Show();
    shapeLayerWatersheds6_10.Show();
    shapeLayerWatersheds6_11.Show();
    shapeLayerWatersheds6_12.Show();
    shapeLayerWatersheds6_13.Show();
    shapeLayerWatersheds6_14.Show();
    shapeLayerWatersheds6_15.Show();
    shapeLayerWatersheds6_16.Show();
    }
}
function togglePlaceTabs(extent)
{   
    var countyTab = $get('countyTab');
    var ecoregionTab = $get('ecoregionTab');
    var basinTab = $get('basinTab');
    var watershedTab = $get('watershedsTab');
    switch (extent)
    {    
    case 'counties':
        countyTab.src = 'img/countiestabactive.gif';
        ecoregionTab.src = 'img/ecoregionstab.gif';
        basinTab.src = 'img/basinstab.gif';   
        watershedTab.src = 'img/watershedstab.gif';  
        break;
    case 'ecoregions':
        countyTab.src = 'img/countiestab.gif';
        ecoregionTab.src = 'img/ecoregionstabactive.gif';
        basinTab.src = 'img/basinstab.gif';   
        watershedTab.src = 'img/watershedstab.gif';   
        break;
    case 'basins':
        countyTab.src = 'img/countiestab.gif';
        ecoregionTab.src = 'img/ecoregionstab.gif';
        basinTab.src = 'img/basinstabactive.gif';   
        watershedTab.src = 'img/watershedstab.gif';   
        break;
    case 'watersheds':
        countyTab.src = 'img/countiestab.gif';
        ecoregionTab.src = 'img/ecoregionstab.gif';
        basinTab.src = 'img/basinstab.gif';   
        watershedTab.src = 'img/watershedstabactive.gif';  
        break;      
                 
        }
}

function ShowShapes(Extent)
{
    
    //map.HideAllShapeLayers();  
    if (hdfActivePlaceView == undefined)
    {
    hdfActivePlaceView = $get('hdfActivePlaceView'); 
    }
    var OldShapeLayer = hdfActivePlaceView.value;    
    hdfActivePlaceView.value = Extent;
    switch(OldShapeLayer)
    {
    case 'counties':  
    shapeLayerCounties.Hide();
    break;
    case 'ecoregions':
    shapeLayerEcoregions.Hide();
    break;
    case 'basins':
    shapeLayerBasins.Hide();
    break;
    case 'watersheds':
    onChangeView();
    break;
    default:
    break;
    }
    
    togglePlaceTabs(Extent);
   if (selected6thField != undefined)
        {
            map.DeleteShape(selected6thField);
        }
    switch(Extent)
    {
    case 'counties':
      if (initializeCounties == true)
      {
      loadCounties();
      initializeCounties = false;
      }
      shapeLayerCounties.Show();
      //setShapeProps(shapeLayerCounties);        
      break;    
    case 'ecoregions':
     //shapeLayerEcoregions.Show();
        if (intializeEcoregions == true)
        {
        loadEcoregions();
        intializeEcoregions = false;
        }            
        else
        {
        shapeLayerEcoregions.Show();
        }      
      break;
    case 'basins':
    //shapeLayerBasins.Show();
     if (intializeBasins == true)
        {
        loadBasins();
        intializeBasins = false;
        shapeLayerBasins.Show();
        }            
        else
        {
        shapeLayerBasins.Show();
        }                 
      break;
    case 'watersheds':
        if (intializeWatersheds == true)
        {
        loadWatersheds();
        intializeWatersheds = false;
        }            
        else
        {
        onChangeView();
        }
      break;
    default:
      break;
    }
}

//function LoadShapeCollections()
//{    
//    loadCounties();
//    //loadWatersheds();    
//    //loadEcoregions();
//    //loadBasins();    
//}

function loadCounties()
{
  //loadingDiv.style.display = "block";
   AddMyControl();
  //var veLayerSpec = new VEShapeSourceSpecification(VEDataType.VECollection, '4485AF2FFC19B758!105', shapeLayerCounties);
  //var veLayerSpec = new VEShapeSourceSpecification(VEDataType.VECollection, '4485AF2FFC19B758!5062', shapeLayerCounties);  
  var veLayerSpec = new VEShapeSourceSpecification(VEDataType.VECollection, '4485AF2FFC19B758!5157', shapeLayerCounties);   
  map.ImportShapeLayerData(veLayerSpec, onFeedLoad, 0);        
}

function setShapeProps(shapeLayer)
{
  var shapeCount = shapeLayer.GetShapeCount();
  var shapeLayerName = shapeLayer.Name;
  for (var i = 0; i < shapeCount; i++)
  {  
       var icon = new VECustomIconSpecification();      
       var shape = shapeLayer.GetShapeByIndex(i);       
       icon.Image = 'img/blankIcon.gif';                        
       shape.SetCustomIcon(icon);
       shape.ShowDetailOnMouseOver = false;        
       var PopUpDescription = shape.GetDescription();
       setShapeLayerColors(shape,shapeLayerName,false);          
       //shape.SetLineWidth(1);
       PopUpDescription = PopUpDescription.replace(/\n/g," ");  
       if (shapeLayerName == 'Counties')
           {                   
           //PopUpDescription = PopUpDescription.split(/\ /);
           County = PopUpDescription.split(/\:/)[1].replace("MaxSimpTol", "").replace(/^\s+/,"").replace(/\s+$/,"");
           County = changeCase(County);            
           var descText = '<table><tr><td><a class="aLink" onclick="getSpeciesList(\''+County+'\',\'Counties\'); toggleTabs(\'place\');">View wildlife species list for '+County+' County.</a></td></tr></table>';       
           shape.SetDescription(descText);
           var PopUpTitle = shape.GetTitle();
           shape.SetTitle('<B>'+County+' County</B>');          
           }
       else if (shapeLayerName.search(/Huc5/) != -1) 
           {
           try{           
           var HUC5Num = PopUpDescription.split(/\:/)[1].replace(" WATERSHED_","").replace(/^\s+/,"");
           var HUC5Name = PopUpDescription.split(/\:/)[2].replace(" GRID_MEMB","").replace(/^\s+/,"");                     
           var descText = '<table><tr><td><a class="aLink" onclick="getSpeciesList(\''+HUC5Num+'%\',\'Watersheds5th\'); toggleTabs(\'place\');">View wildlife species list for '+HUC5Name+'('+HUC5Num+') 5th field watershed.</a></td></tr></table>'; 
           shape.SetDescription(descText);
           shape.SetTitle('<B>'+HUC5Name+'('+HUC5Num+') 5th field watershed</B>');  
           setShapeLayerColors(shape,"Watersheds",false);       
            }
           catch (ex)
           {}
           }                                       
       else if (shapeLayerName.search(/Huc6/) != -1) 
           {
           try{                      
           var HUC6Num = PopUpDescription.split(/\:/)[1].replace(" SUBWAT_NAM","").replace(/^\s+/,"");
           var HUC6Name = PopUpDescription.split(/\:/)[2].replace(" GRID_MEMB","").replace(/^\s+/,"");                     
           var descText = '<table><tr><td><a class="aLink" onclick="getSpeciesList(\''+HUC6Num+'\',\'Watersheds6th\'); toggleTabs(\'place\');">View wildlife species list for '+HUC6Name+'('+HUC6Num+') 6th field watershed.</a></td></tr></table>'; 
           shape.SetDescription(descText);
           shape.SetTitle('<B>'+HUC6Name+'('+HUC6Num+') 6th field watershed</B>');          
           setShapeLayerColors(shape,"Watersheds",false);                   
           }
           catch (ex)
           {}
           }           
       else if (shapeLayerName == 'Huc4')
           {
           try{            
           var HUC4Name = PopUpDescription.split(/\:/)[7].replace(" MaxSimpTol", "").replace(/^\s+/,"");    
           var HUC4Num = PopUpDescription.split(/\:/)[6].replace("SUBBASIN_N", "").replace(/^\s+/,"").replace(" ", "");   
           //var HUC4Num = PopUpDescription.split(/\:/)[1].replace(" SUBBASIN_N","").replace(/^\s+/,"");                 
           //var HUC4Name = PopUpDescription.split(/\:/)[2].replace(" GRID_MEMB","").replace(/^\s+/,"");                     
           var descText = '<table><tr><td><a class="aLink" onclick="getSpeciesList(\''+HUC4Num+'%\',\'Watersheds4th\'); toggleTabs(\'place\');">View wildlife species list for '+HUC4Name+'('+HUC4Num+') 4th field watershed.</a></td></tr></table>'; 
           shape.SetDescription(descText);
           shape.SetTitle('<B>'+HUC4Name+'('+HUC4Num+') 4th field watershed</B>'); 
           setShapeLayerColors(shape,"Watersheds",false);         
           }
           catch (ex)
           {}
           }
       else if (shapeLayerName == 'Basins')
           {                                
           PopUpDescription = PopUpDescription.split(/\:/)[1].replace("COUNT_", "").replace(/^\s+/,"")           
           var Basin = PopUpDescription;             
           var descText = '<table><tr><td><a class="aLink" onclick="getSpeciesList(\''+Basin+'\',\'Basins\'); toggleTabs(\'place\');">View wildlife species list for '+Basin+' Basin.</a></td></tr></table>'; 
           shape.SetDescription(descText);
           shape.SetTitle('<B>'+Basin+' Basin</B>');          
           }
       else if (shapeLayerName == 'ecoregions')
           {                             
           PopUpDescription = PopUpDescription.split(/\:/)[7];
           PopUpDescription = PopUpDescription.replace(" area_1","");
           PopUpDescription = PopUpDescription.replace(/^\s+/,"");
           Ecoregion = PopUpDescription;           
           var descText = '<table><tr><td><a class="aLink" onclick="getSpeciesList(\''+Ecoregion+'\',\'Ecoregions\'); toggleTabs(\'place\');">View wildlife species list for '+Ecoregion+' Ecoregion.</a></td></tr></table>'; 
           shape.SetDescription(descText);
           shape.SetTitle('<B>'+Ecoregion+' Ecoregion</B>');            
           }       
   }  
}

function loadBasins()
{
  //loadingDiv.style.display = "block";
   AddMyControl();
  var veLayerSpec = new VEShapeSourceSpecification(VEDataType.VECollection, '4485AF2FFC19B758!4943', shapeLayerBasins);
  map.ImportShapeLayerData(veLayerSpec, onFeedLoad, 0);
  //shapeLayerBasins.Hide();
}
function loadEcoregions()
{
  //loadingDiv.style.display = "block";
   AddMyControl();
  var veLayerSpec = new VEShapeSourceSpecification(VEDataType.VECollection, '4485AF2FFC19B758!4976', shapeLayerEcoregions);
  map.ImportShapeLayerData(veLayerSpec, onFeedLoad, 0);
  //shapeLayerEcoregions.Hide();
}

function loadWatersheds4th()
{
    //var Huc4 = '4485AF2FFC19B758!3720';   
    //var Huc4 = '4485AF2FFC19B758!5207'; 
    var Huc4 = '4485AF2FFC19B758!5551';  
           
    if (!isHuc4Initialized)
    {
    //loadingDiv.style.display = "block";
     AddMyControl();
    var veLayerSpecHuc4 = new VEShapeSourceSpecification(VEDataType.VECollection, Huc4, shapeLayerWatersheds4);
    map.ImportShapeLayerData(veLayerSpecHuc4, onFeedLoad, 0);   
    isHuc4Initialized = true;    
    }
}

function  loadWatersheds5th()
{   
    if (!isHuc5Initialized)
    {
    //loadingDiv.style.display = "block";
     AddMyControl();
    var Huc5 =new Array();
    Huc5[0]="4485AF2FFC19B758!3812";
    Huc5[1]="4485AF2FFC19B758!4661";
    Huc5[2]="4485AF2FFC19B758!4243";
    Huc5[3]="4485AF2FFC19B758!4344";
    
//    Huc5[0]="4485AF2FFC19B758!5882";
//    Huc5[1]="4485AF2FFC19B758!6083";
//    Huc5[2]="4485AF2FFC19B758!6284";
//    Huc5[3]="4485AF2FFC19B758!6435";
    
    var veLayerSpecHuc5_1 = new VEShapeSourceSpecification(VEDataType.VECollection, Huc5[0], shapeLayerWatersheds5_1);
    map.ImportShapeLayerData(veLayerSpecHuc5_1, onFeedLoad, 0);        
    var veLayerSpecHuc5_2 = new VEShapeSourceSpecification(VEDataType.VECollection, Huc5[1], shapeLayerWatersheds5_2);
    map.ImportShapeLayerData(veLayerSpecHuc5_2, onFeedLoad, 0);    
    var veLayerSpecHuc5_3 = new VEShapeSourceSpecification(VEDataType.VECollection, Huc5[2], shapeLayerWatersheds5_3);
    map.ImportShapeLayerData(veLayerSpecHuc5_3, onFeedLoad, 0);    
    var veLayerSpecHuc5_4 = new VEShapeSourceSpecification(VEDataType.VECollection, Huc5[3], shapeLayerWatersheds5_4);
    map.ImportShapeLayerData(veLayerSpecHuc5_4, onFeedLoad, 0);    
    isHuc5Initialized = true;
    }
} 

function loadWatersheds6th()
{
    if (!isHuc6Initialized)
    {
    //loadingDiv.style.display = "block";
     AddMyControl();
    var Huc6 =new Array();
    Huc6[0]="4485AF2FFC19B758!574";
    Huc6[1]="4485AF2FFC19B758!775";
    Huc6[2]="4485AF2FFC19B758!976";
    Huc6[3]="4485AF2FFC19B758!1177";
    Huc6[4]="4485AF2FFC19B758!1378";
    Huc6[5]="4485AF2FFC19B758!1579";
    Huc6[6]="4485AF2FFC19B758!1780";
    Huc6[7]="4485AF2FFC19B758!1981";
    Huc6[8]="4485AF2FFC19B758!2182";
    Huc6[9]="4485AF2FFC19B758!2383";
    Huc6[10]="4485AF2FFC19B758!2584";
    Huc6[11]="4485AF2FFC19B758!2785";
    Huc6[12]="4485AF2FFC19B758!2986";
    Huc6[13]="4485AF2FFC19B758!3187";
    Huc6[14]="4485AF2FFC19B758!3388";
    Huc6[15]="4485AF2FFC19B758!3589";   
    
    var veLayerSpecHuc6_1 = new VEShapeSourceSpecification(VEDataType.VECollection, Huc6[0], shapeLayerWatersheds6_1);
    map.ImportShapeLayerData(veLayerSpecHuc6_1, onFeedLoad, 0);        
    var veLayerSpecHuc6_2 = new VEShapeSourceSpecification(VEDataType.VECollection, Huc6[1], shapeLayerWatersheds6_2);
    map.ImportShapeLayerData(veLayerSpecHuc6_2, onFeedLoad, 0);    
    var veLayerSpecHuc6_3 = new VEShapeSourceSpecification(VEDataType.VECollection, Huc6[2], shapeLayerWatersheds6_3);
    map.ImportShapeLayerData(veLayerSpecHuc6_3, onFeedLoad, 0);    
    var veLayerSpecHuc6_4 = new VEShapeSourceSpecification(VEDataType.VECollection, Huc6[3], shapeLayerWatersheds6_4);
    map.ImportShapeLayerData(veLayerSpecHuc6_4, onFeedLoad, 0);  
    var veLayerSpecHuc6_5 = new VEShapeSourceSpecification(VEDataType.VECollection, Huc6[4], shapeLayerWatersheds6_5);
    map.ImportShapeLayerData(veLayerSpecHuc6_5, onFeedLoad, 0);            
    var veLayerSpecHuc6_6 = new VEShapeSourceSpecification(VEDataType.VECollection, Huc6[5], shapeLayerWatersheds6_6);
    map.ImportShapeLayerData(veLayerSpecHuc6_6, onFeedLoad, 0);        
    var veLayerSpecHuc6_7 = new VEShapeSourceSpecification(VEDataType.VECollection, Huc6[6], shapeLayerWatersheds6_7);
    map.ImportShapeLayerData(veLayerSpecHuc6_7, onFeedLoad, 0);       
    var veLayerSpecHuc6_8 = new VEShapeSourceSpecification(VEDataType.VECollection, Huc6[7], shapeLayerWatersheds6_8);
    map.ImportShapeLayerData(veLayerSpecHuc6_8, onFeedLoad, 0);      
    var veLayerSpecHuc6_9 = new VEShapeSourceSpecification(VEDataType.VECollection, Huc6[8], shapeLayerWatersheds6_9);
    map.ImportShapeLayerData(veLayerSpecHuc6_9, onFeedLoad, 0);            
    var veLayerSpecHuc6_10 = new VEShapeSourceSpecification(VEDataType.VECollection, Huc6[9], shapeLayerWatersheds6_10);
    map.ImportShapeLayerData(veLayerSpecHuc6_10, onFeedLoad, 0);        
    var veLayerSpecHuc6_11 = new VEShapeSourceSpecification(VEDataType.VECollection, Huc6[10], shapeLayerWatersheds6_11);
    map.ImportShapeLayerData(veLayerSpecHuc6_11, onFeedLoad, 0);        
    var veLayerSpecHuc6_12 = new VEShapeSourceSpecification(VEDataType.VECollection, Huc6[11], shapeLayerWatersheds6_12);
    map.ImportShapeLayerData(veLayerSpecHuc6_12, onFeedLoad, 0);      
    var veLayerSpecHuc6_13 = new VEShapeSourceSpecification(VEDataType.VECollection, Huc6[12], shapeLayerWatersheds6_13);
    map.ImportShapeLayerData(veLayerSpecHuc6_13, onFeedLoad, 0);            
    var veLayerSpecHuc6_14 = new VEShapeSourceSpecification(VEDataType.VECollection, Huc6[13], shapeLayerWatersheds6_14);
    map.ImportShapeLayerData(veLayerSpecHuc6_14, onFeedLoad, 0);        
    var veLayerSpecHuc6_15 = new VEShapeSourceSpecification(VEDataType.VECollection, Huc6[14], shapeLayerWatersheds6_15);
    map.ImportShapeLayerData(veLayerSpecHuc6_15, onFeedLoad, 0);        
    var veLayerSpecHuc6_16 = new VEShapeSourceSpecification(VEDataType.VECollection, Huc6[15], shapeLayerWatersheds6_16);
    map.ImportShapeLayerData(veLayerSpecHuc6_16, onFeedLoad, 0);   
    isHuc6Initialized = true;
    }
}


function loadWatersheds()
{
   var zoomLevel = map.GetZoomLevel();
   if (zoomLevel <9)
        {        
        loadWatersheds4th();                
        }    
        else if (zoomLevel <11)
        {
         loadWatersheds5th(); 
        }                    
        else
        {
        loadWatersheds6th();               
        }    
}

function onMouseOverHandler(e) {

    if (e.elementID) {
        try {
            if (map.GetShapeByID(e.elementID).Type != "Point") {
                if (mouseOverSelectedShape != undefined) {
                    var icon = new VECustomIconSpecification();
                    icon.Image = 'img/blankIcon.gif';
                    mouseOverSelectedShape.SetCustomIcon(icon);
                    mouseOverSelectedShapeLayer = mouseOverSelectedShape.GetShapeLayer();
                    mouseOverSelectedShapeLayer = mouseOverSelectedShapeLayer.Name;
                    if (mouseOverSelectedShapeLayer.search(/Huc/) != -1) {
                        setShapeLayerColors(mouseOverSelectedShape, "Watersheds", false);
                    }
                    else {
                        setShapeLayerColors(mouseOverSelectedShape, mouseOverSelectedShapeLayer, false)
                    }
                }
                var shape = map.GetShapeByID(e.elementID);
                if (shape != selectedShape) {
                    shape.SetLineWidth(2);
                    shape.SetLineColor(new VEColor(0, 150, 100, 1.0));
                    shape.SetFillColor(new VEColor(0, 100, 150, 0.5));
                    var shapeLayer = shape.GetShapeLayer();
                    var zoomLevel = map.GetZoomLevel();
                    var shapeLayerName = shapeLayer.Name;
                    if (shapeLayerName.search(/Huc/) == -1 || zoomLevel > 6) {
                        var PopUpTitle = shape.GetTitle();
                        var icon = new VECustomIconSpecification();
                        icon = "<div class='divShapeLabel' style='background-color:#6F99A5; font-size:10px;font-weight:bold; color:white; width=100px'>" + PopUpTitle + "</div>";
                        shape.SetCustomIcon(icon);
                        mouseOverSelectedShape = shape;
                    }
                }
            }
        }
        catch (ex)
        { }
        keepSelectedPoly = false;
        return true;

    }
}

function onMouseOutHandler(e) {
    if (e.elementID && keepSelectedPoly == false) {
        if (map.GetShapeByID(e.elementID).Type != "Point") {
            var shape = map.GetShapeByID(e.elementID);
            var icon = new VECustomIconSpecification();
            icon.Image = 'img/blankIcon.gif';
            shape.SetCustomIcon(icon);
            //map.HideInfoBox(shape);

            if (shape != selectedShape) {
                var shapeLayer = shape.GetShapeLayer();
                var shapeLayerName = shapeLayer.Name;
                if (shapeLayerName.search(/Huc5/) != -1 || shapeLayerName == "Huc4" || shapeLayerName.search(/Huc6/) != -1) {
                    setShapeLayerColors(shape, "Watersheds", false);
                }
                else {
                    setShapeLayerColors(shape, shapeLayerName, false)
                }
                mouseOverSelectedShape = undefined;
            }
        }
        //keepSelectedPoly = false;   
        return true;
    }
}
function onClickHandler(e)
{
if (e.elementID)
  {
        
        var shape = map.GetShapeByID(e.elementID);
        map.ShowInfoBox(shape);      
        keepSelectedPoly = true;
        return true;
  }
}

function changeCase(string) {
var index;
var tmpStr;
var tmpChar;
var preString;
var postString;
var strlen;
tmpStr = string.toLowerCase();
strLen = tmpStr.length;
if (strLen > 0)  {
for (index = 0; index < strLen; index++)  {
if (index == 0)  {
tmpChar = tmpStr.substring(0,1).toUpperCase();
postString = tmpStr.substring(1,strLen);
tmpStr = tmpChar + postString;
}
else {
tmpChar = tmpStr.substring(index, index+1);
if (tmpChar == " " && index < (strLen-1))  {
tmpChar = tmpStr.substring(index+1, index+2).toUpperCase();
preString = tmpStr.substring(0, index+1);
postString = tmpStr.substring(index+2,strLen);
tmpStr = preString + tmpChar + postString;
         }
      }
   }
}
return tmpStr;
}

function ClearSearchBox(searchBox)
{    
    var hdfSearchBox = 'hdfSearchBox'+searchBox;  //hdfSearchBoxQuick
    var hdfSearch = $get(hdfSearchBox);
    if (hdfSearch.value == 'first')
    {
    var SearchBox = 'tb'+searchBox;
    SearchBox = $get(SearchBox);
    SearchBox.value = '';
    SearchBox.style.color = 'black';
    }    
    hdfSearch.value = 'notFirst'; 
    var SearchAlert = 'SelectAlert'+searchBox;
    SearchAlert = $get(SearchAlert);
    SearchAlert.style.display = 'none';    
   
}

function AddMyControl()
         {
            if (control == null)
            {
               control = document.createElement("div"); 
               control.id = "myControl";
               control.style.top ="185px"; 
               control.style.left = "200px"; 
               control.className = "LoadingDivControl"; 
               control.innerHTML = '<img src="img/indicator_mozilla_blu.gif" /><strong> Loading layer</strong>';  
               map.AddControl(control);
               addShim(control);
            }
         }
         function addShim(el)
         {
            var shim = document.createElement("iframe");
            shim.id = "myShim";
            shim.frameBorder = "0";
            shim.style.position = "absolute";
            shim.style.zIndex = "1";
            shim.style.top  = el.offsetTop;
            shim.style.left = el.offsetLeft;
            shim.width  = el.offsetWidth;
            shim.height = el.offsetHeight;
            el.shimElement = shim;
            el.parentNode.insertBefore(shim, el);
         }
         function RemoveMyControl()
         {
            if (control != null)
            {
               map.DeleteControl(control);
               control = null;
            }
         }