﻿// JScript File
var QueryText= new Array();
QueryText[0] = "";
QueryText[1] = "";
var ProcessQuery = false;
var selected6thField;
var add6thWatershed = false;
//var sType;
var FamilySource;
var Text;
var isQuery = false;
var AlternateName;
var QuerySciName;
var PushPinShape = null;

function goToSelection(source, eventArgs)
{
    var Value = eventArgs.get_value();
    var ValueArray = Value.split(/\,/);
    AlternateName = ValueArray[0];
    sType = ValueArray[1];
    FamilySource = ValueArray[2];
    Text = eventArgs.get_text();
    ProcessSelection(sType)
}

function QueryStringSelection(place, geoType)
{
sType= geoType;
Text = place;
ProcessSelection(sType);
//window.onDomReady(function(){alert("dom ready")});
}

////create onDomReady Event
//window.onDomReady = DomReady;

////Setup the event
//function DomReady(fn)
//{
//	//W3C
//	if(document.addEventListener)
//	{
//		document.addEventListener("DOMContentLoaded", fn, false);
//	}
//	//IE
//	else
//	{
//		document.onreadystatechange = function(){readyState(fn)}
//	}
//}

////IE execute function
//function readyState(fn)
//{
//	//dom is ready for interaction
//	if(document.readyState == "interactive")
//	{
//		fn();
//	}
//}

function ProcessSelection(sType)
{
    switch (sType)
    {
    case "CommonName":
        toggleTabs('species');
        isQuery = true;
        QuerySciName = AlternateName;
        SetHDFforTaxonomy('species',FamilySource);
        //getSpeciesContent(Text);   
        //var sciNameShort = AlternateName.replace(/\ /, ""); 
        //toggleSelectedSpecies(sciNameShort);    
        //isQuery = false;                
        break;
    case "ScientificName":
        toggleTabs('species');
        isQuery = true;
        QuerySciName = Text;
        SetHDFforTaxonomy('species',FamilySource);
        //getSpeciesContent(Text);        
        //var sciNameShort = Text.replace(/\ /, "");
        //toggleSelectedSpecies(sciNameShort);     
        //isQuery = false;            
        break;
    case "Address":
        {      
        if (!isMapLoaded)
        {
         DisplayVEMap('Address');
        }  
        DeleteShape();          
        var LatLong = AlternateName.split(/\;/);   
        LatLong[1] = parseFloat(LatLong[1]);
        LatLong[0] = parseFloat(LatLong[0]);
        var LL = new VELatLong(LatLong[1],LatLong[0]);
        var pin = map.AddPushpin(LL);
        PushPinShape = pin;
        pin.SetTitle(Text);        
        map.SetCenterAndZoom(LL,16);
        toggleTabs('overview');
        }
        break;
    case "Cities":
        {      
        if (!isMapLoaded)
        {
         DisplayVEMap('Address');
        }  
        DeleteShape();          
        var LatLong = AlternateName.split(/\;/);   
        LatLong[1] = parseFloat(LatLong[1]);
        LatLong[0] = parseFloat(LatLong[0]);
        var LL = new VELatLong(LatLong[1],LatLong[0]);
        var pin = map.AddPushpin(LL);
        PushPinShape = pin;
        pin.SetTitle(Text);        
        map.SetCenterAndZoom(LL,12);
        toggleTabs('overview');
        }
        break;
    case "Counties":
        //map.DeleteAllShapeLayers();
        //toggleTabs('overview');
        if (!isMapLoaded)
        {
         DisplayVEMap('Counties');
        }
        //ShowShapes('counties');
        var CountyLayerCount = shapeLayerCounties.GetShapeCount();
        var CountyID;
        if (CountyLayerCount != 0)
        {
        for (var i = 0; i < CountyLayerCount; i++)
        {
            var shape = shapeLayerCounties.GetShapeByIndex(i);
            var title = shape.GetTitle();
            if (title.match(Text) != null)
            {
                CountyID = i;
            }
        }
        }
        if (CountyID != undefined)
        {
            //Turn off old selected shape
            if (selectedShape != undefined)
                {
                    var shapeLayer = selectedShape.GetShapeLayer();              
                    var shapeLayerName = shapeLayer.Name;
                    if(shapeLayerName.search(/Huc5/) != -1 || shapeLayerName == "Huc4")
                    {
                    setShapeLayerColors(selectedShape,"watersheds",false) 
                    }
                    else
                    {
                    setShapeLayerColors(selectedShape,shapeLayerName,false) 
                    }           
                }        
            selectedShape = shapeLayerCounties.GetShapeByIndex(CountyID);        
            selectedShape.SetLineWidth(2);
            selectedShape.SetLineColor(new VEColor(250,100,100,1.0));
            selectedShape.SetFillColor(new VEColor(250,100,100,0.5));
            map.ShowInfoBox(selectedShape);  
            var selecteShapePoints = selectedShape.GetPoints();
            map.SetMapView(selecteShapePoints);           
        }
        else
        {
        QueryText[0] = Text;
        QueryText[1] = sType;
        ProcessQuery = true;        
        }
        toggleTabs('overview');
        ShowShapes('counties');  
//        LatLong = Coords.split(/\;/);        
//        var QueryText = Text.replace(/ County/g,'');        
//        LatLong[1] = parseFloat(LatLong[1]);
//        LatLong[0] = parseFloat(LatLong[0]);
//        var ParamGeoType = {"GeoType": Type};   
//        var ParamQueryText = {"QueryText": QueryText};   
//        WebServices.GetFeatureWebService.GetFeatureCityCounty(ParamGeoType.GeoType, ParamQueryText.QueryText, OnSucceededCounty, OnFailed);          
        break;
    case "Ecoregions":
//        toggleTabs('overview');
//        ShowShapes('ecoregions');
        if (!isMapLoaded)
        {
         DisplayVEMap('Ecoregions');
        }
        var EcoregionLayerCount = shapeLayerEcoregions.GetShapeCount();
        if (EcoregionLayerCount != 0)
        {
        var EcoregionID;
        for (var i = 0; i < EcoregionLayerCount; i++)
        {
            var shape = shapeLayerEcoregions.GetShapeByIndex(i);
            var title = shape.GetTitle();
            if (title.match(Text) != null)
            {
                EcoregionID = i;
            }
        }
        if (EcoregionID != undefined)
        {
        //Turn off old selected shape
        if (selectedShape != undefined)
            {
                var shapeLayer = selectedShape.GetShapeLayer();              
                var shapeLayerName = shapeLayer.Name;
                if(shapeLayerName.search(/Huc5/) != -1 || shapeLayerName == "Huc4")
                {
                setShapeLayerColors(selectedShape,"watersheds",false) 
                }
                else
                {
                setShapeLayerColors(selectedShape,shapeLayerName,false) 
                }           
            }        
        selectedShape = shapeLayerEcoregions.GetShapeByIndex(EcoregionID);        
        selectedShape.SetLineWidth(2);
        selectedShape.SetLineColor(new VEColor(250,100,100,1.0));
        selectedShape.SetFillColor(new VEColor(250,100,100,0.5));
        map.ShowInfoBox(selectedShape);  
        var selecteShapePoints = selectedShape.GetPoints();
        map.SetMapView(selecteShapePoints);
        }
        }
        else
        {
        QueryText[0] = Text;
        QueryText[1] = sType;
        ProcessQuery = true;        
        }
        toggleTabs('overview');
        ShowShapes('ecoregions');    
        break;
    case "Basins":
        var BasinLayerCount = shapeLayerBasins.GetShapeCount();
        if (!isMapLoaded)
        {
         DisplayVEMap('Basins');
        }
        if (BasinLayerCount != 0)
        {
        var BasinID;
        for (var i = 0; i < BasinLayerCount; i++)
        {
            var shape = shapeLayerBasins.GetShapeByIndex(i);
            var title = shape.GetTitle();
            if (title.match(Text) != null)
            {
                BasinID = i;
            }
        }
        if (BasinID != undefined)
        {
        //Turn off old selected shape
        if (selectedShape != undefined)
            {
                var shapeLayer = selectedShape.GetShapeLayer();              
                var shapeLayerName = shapeLayer.Name;
                if(shapeLayerName.search(/Huc5/) != -1 || shapeLayerName == "Huc4")
                {
                setShapeLayerColors(selectedShape,"watersheds",false) 
                }
                else
                {
                setShapeLayerColors(selectedShape,shapeLayerName,false) 
                }           
            }        
        selectedShape = shapeLayerBasins.GetShapeByIndex(BasinID);        
        selectedShape.SetLineWidth(2);
        selectedShape.SetLineColor(new VEColor(250,100,100,1.0));
        selectedShape.SetFillColor(new VEColor(250,100,100,0.5));
        map.ShowInfoBox(selectedShape);  
        var selecteShapePoints = selectedShape.GetPoints();
        map.SetMapView(selecteShapePoints);
        }
        }
        else
        {
        QueryText[0] = Text;
        QueryText[1] = sType;
        ProcessQuery = true;        
        }
        toggleTabs('overview');
        ShowShapes('basins');    
        break;
    case "Watersheds6th":
        if (!isMapLoaded)
        {
         DisplayVEMap('Watersheds');
        }
        var ParamGeoType = {"GeoType": sType};   
        var ParamQueryText = {"QueryText": Text};                     
        WebServices.GetFeatureWebService.GetFeatureWatershed(ParamGeoType.GeoType, ParamQueryText.QueryText, OnSucceededWatershed, OnFailedWatershed);  
        break;   
    case "Watersheds5th":   
         if (!isMapLoaded)
        {
         DisplayVEMap('Watersheds');
        }     
        var ParamGeoType = {"GeoType": sType};   
        var ParamQueryText = {"QueryText": Text};                     
        WebServices.GetFeatureWebService.GetFeatureWatershed(ParamGeoType.GeoType, ParamQueryText.QueryText, OnSucceededWatershed, OnFailedWatershed);       
        break;   
     case "Watersheds4th":
        if (!isMapLoaded)
        {
         DisplayVEMap('Watersheds');
        }
       var Watersheds4thLayerCount = shapeLayerWatersheds4.GetShapeCount();
       if (Watersheds4thLayerCount != 0)
        {
        var Watersheds4thID;
        for (var i = 0; i < Watersheds4thLayerCount; i++)
        {
            var shape = shapeLayerWatersheds4.GetShapeByIndex(i);
            var title = shape.GetTitle();
            if (title.match(Text) != null)
            {
                Watersheds4thID = i;
            }
        }
        if (Watersheds4thID != undefined)
        {
        //Turn off old selected shape
        if (selectedShape != undefined)
            {
                var shapeLayer = selectedShape.GetShapeLayer();              
                var shapeLayerName = shapeLayer.Name;
                if(shapeLayerName.search(/Huc5/) != -1 || shapeLayerName == "Huc4")
                {
                setShapeLayerColors(selectedShape,"watersheds",false) 
                }
                else
                {
                setShapeLayerColors(selectedShape,shapeLayerName,false) 
                }           
            }        
        selectedShape = shapeLayerWatersheds4.GetShapeByIndex(Watersheds4thID);        
        selectedShape.SetLineWidth(2);
        selectedShape.SetLineColor(new VEColor(250,100,100,1.0));
        selectedShape.SetFillColor(new VEColor(250,100,100,0.5));
        map.ShowInfoBox(selectedShape);  
        var selecteShapePoints = selectedShape.GetPoints();
        map.SetMapView(selecteShapePoints);
        }
        }
        else
        {
        QueryText[0] = Text;
        QueryText[1] = sType;
        ProcessQuery = true;        
        }
        toggleTabs('overview');
        ShowShapes('watersheds');    
        break;
        default:
        break;
    }
}

 function DeleteShape()
      {
         if(PushPinShape != null)
         {
            map.DeleteShape(PushPinShape);
            PushPinShape = null;
         }
      }
function doProcessQuery()
{
    ProcessQuery = false;
    QueryTexttoProcess = QueryText[0];
    QueryType = QueryText[1];
    switch(QueryType)
    {
       case "Counties":
     var CountyLayerCount = shapeLayerCounties.GetShapeCount();
        var CountyID;
        for (var i = 0; i < CountyLayerCount; i++)
        {
            var shape = shapeLayerCounties.GetShapeByIndex(i);
            var title = shape.GetTitle();
            if (title.match(QueryTexttoProcess) != null)
            {
                CountyID = i;
            }
        }
        if (CountyID != undefined)
        {
            //Turn off old selected shape
            if (selectedShape != undefined)
                {
                    var shapeLayer = selectedShape.GetShapeLayer();              
                    var shapeLayerName = shapeLayer.Name;
                    if(shapeLayerName.search(/Huc5/) != -1 || shapeLayerName == "Huc4")
                    {
                    setShapeLayerColors(selectedShape,"watersheds",false) 
                    }
                    else
                    {
                    setShapeLayerColors(selectedShape,shapeLayerName,false) 
                    }           
                }        
            selectedShape = shapeLayerCounties.GetShapeByIndex(CountyID);        
            selectedShape.SetLineWidth(2);
            selectedShape.SetLineColor(new VEColor(250,100,100,1.0));
            selectedShape.SetFillColor(new VEColor(250,100,100,0.5));
            map.ShowInfoBox(selectedShape);  
            var selecteShapePoints = selectedShape.GetPoints();
            map.SetMapView(selecteShapePoints);           
        }
        break;    
    
    case "Ecoregions":
     var EcoregionLayerCount = shapeLayerEcoregions.GetShapeCount();
        if (EcoregionLayerCount != 0)
        {
        var EcoregionID;
        for (var i = 0; i < EcoregionLayerCount; i++)
        {
            var shape = shapeLayerEcoregions.GetShapeByIndex(i);
            var title = shape.GetTitle();
            if (title.match(QueryTexttoProcess) != null)
            {
                EcoregionID = i;
            }
        }
        if (EcoregionID != undefined)
        {
        //Turn off old selected shape
        if (selectedShape != undefined)
            {
                var shapeLayer = selectedShape.GetShapeLayer();              
                var shapeLayerName = shapeLayer.Name;
                if(shapeLayerName.search(/Huc5/) != -1 || shapeLayerName == "Huc4")
                {
                setShapeLayerColors(selectedShape,"watersheds",false) 
                }
                else
                {
                setShapeLayerColors(selectedShape,shapeLayerName,false) 
                }           
            }        
        selectedShape = shapeLayerEcoregions.GetShapeByIndex(EcoregionID);        
        selectedShape.SetLineWidth(2);
        selectedShape.SetLineColor(new VEColor(250,100,100,1.0));
        selectedShape.SetFillColor(new VEColor(250,100,100,0.5));
        map.ShowInfoBox(selectedShape);  
        var selecteShapePoints = selectedShape.GetPoints();
        map.SetMapView(selecteShapePoints);
        }
        }
        break;
        case "Basins":
        var BasinLayerCount = shapeLayerBasins.GetShapeCount();
        if (BasinLayerCount != 0)
        {
        var BasinID;
        for (var i = 0; i < BasinLayerCount; i++)
        {
            var shape = shapeLayerBasins.GetShapeByIndex(i);
            var title = shape.GetTitle();
            if (title.match(QueryTexttoProcess) != null)
            {
                BasinID = i;
            }
        }
        if (BasinID != undefined)
        {
        //Turn off old selected shape
        if (selectedShape != undefined)
            {
                var shapeLayer = selectedShape.GetShapeLayer();              
                var shapeLayerName = shapeLayer.Name;
                if(shapeLayerName.search(/Huc5/) != -1 || shapeLayerName == "Huc4")
                {
                setShapeLayerColors(selectedShape,"watersheds",false) 
                }
                else
                {
                setShapeLayerColors(selectedShape,shapeLayerName,false) 
                }           
            }        
        selectedShape = shapeLayerBasins.GetShapeByIndex(BasinID);        
        selectedShape.SetLineWidth(2);
        selectedShape.SetLineColor(new VEColor(250,100,100,1.0));
        selectedShape.SetFillColor(new VEColor(250,100,100,0.5));
        map.ShowInfoBox(selectedShape);  
        var selecteShapePoints = selectedShape.GetPoints();
        map.SetMapView(selecteShapePoints);
        }
        }        
        break;
        case "Watersheds4th":
        var Watersheds4thLayerCount = shapeLayerWatersheds4.GetShapeCount();
        Text = Text.split(/\(/)[1].split(/\)/)[0];
       if (Watersheds4thLayerCount != 0)
        {
        var Watersheds4thID;
        for (var i = 0; i < Watersheds4thLayerCount; i++)
        {
            var shape = shapeLayerWatersheds4.GetShapeByIndex(i);
            var title = shape.GetTitle();            
            if (title.match(Text) != null)
            {
                Watersheds4thID = i;
            }
        }
        if (Watersheds4thID != undefined)
        {
        //Turn off old selected shape
        if (selectedShape != undefined)
            {
                var shapeLayer = selectedShape.GetShapeLayer();              
                var shapeLayerName = shapeLayer.Name;
                if(shapeLayerName.search(/Huc5/) != -1 || shapeLayerName == "Huc4")
                {
                setShapeLayerColors(selectedShape,"watersheds",false) 
                }
                else
                {
                setShapeLayerColors(selectedShape,shapeLayerName,false) 
                }           
            }        
        selectedShape = shapeLayerWatersheds4.GetShapeByIndex(Watersheds4thID);        
        selectedShape.SetLineWidth(2);
        selectedShape.SetLineColor(new VEColor(250,100,100,1.0));
        selectedShape.SetFillColor(new VEColor(250,100,100,0.5));
        map.ShowInfoBox(selectedShape);  
        var selecteShapePoints = selectedShape.GetPoints();
        map.SetMapView(selecteShapePoints);
        }
        }
        break;  
        default:
        break;        
    }
}


function SelectPlace(source, eventArgs)
{
    //alert(" Key : "+ eventArgs.get_text() +"  Value :  "+eventArgs.get_value());
    
     map.DeleteAllShapeLayers();
    var LatLong = eventArgs.get_value();
    var QueryText = eventArgs.get_text();
    LatLong = LatLong.split(/\;/);
    var geoType = LatLong[LatLong.length-1];
    if (geoType == 'Counties')
    {
    QueryText = QueryText.replace(/ County/g,'');
    }
    LatLong[1] = parseFloat(LatLong[1]);
    LatLong[0] = parseFloat(LatLong[0]);
    var ParamGeoType = {"GeoType": geoType};   
    var ParamQueryText = {"QueryText": QueryText};   
    
    if (geoType == 'Counties')
    {             
        WebServices.GetFeatureWebService.GetFeatureCityCounty(ParamGeoType.GeoType, ParamQueryText.QueryText, OnSucceededCounty, OnFailed);       }
    else if (geoType == 'Cities')
    {             
        WebServices.GetFeatureWebService.GetFeatureCityCounty(ParamGeoType.GeoType, ParamQueryText.QueryText, OnSucceededCity, OnFailed);       
        }     
     var LL = new VELatLong(LatLong[1],LatLong[0]);
        if (geoType == 'Address')
        {       
        var pin = map.AddPushpin(LL);
        pin.SetTitle(QueryText);        
        map.SetCenterAndZoom(LL,16);
        }
        else if (geoType == 'Cities')
        {
         map.SetCenterAndZoom(LL,11);
        }    
}

function OnSucceededWatershed(result)
{
// map.DeleteAllShapeLayers();
if (result != null){
var polyRings = result.getElementsByTagName("RING").length;
var title;
var sDescription;
var HucNum;

for (var h=polyRings-1; h > -1; h--)
{
var PolyCoords = result.getElementsByTagName("RING")[h].text;
var Points = PolyCoords.split(/\;/);
var polyPoints = new Array();
var LatLong = new Array();
for (var i=0; i< Points.length; i++)
{
    LatLong = Points[i].split(/\ /);
    LatLong[1] = parseFloat(LatLong[1]);
    LatLong[0] = parseFloat(LatLong[0]);
    polyPoints.push(new VELatLong(LatLong[1],LatLong[0]));
}
if (result.xml.match("5TH") != null)
{
title = result.getElementsByTagName("FIELDS").item(0).attributes[1].value + " 5th field watershed";
HucNum = title.split(/\(/)[1].replace(") 5th field watershed","");
sDescription = '<table><tr><td><a class="aLink" onclick="getSpeciesList(\''+HucNum+'%\',\'Watersheds5th\'); toggleTabs(\'place\');">View wildlife species list for '+title+' 5th field watershed.</a></td></tr></table>';
}
else
{
title = result.getElementsByTagName("FIELDS").item(0).attributes[1].value + " 6th field watershed";
HucNum = title.split(/\(/)[1].replace(") 6th field watershed","");
sDescription = '<table><tr><td><a class="aLink" onclick="getSpeciesList(\''+HucNum+'%\',\'Watersheds6th\'); toggleTabs(\'place\');">View wildlife species list for '+title+' 6th field watershed.</a></td></tr></table>';
}

AddFeaturePoly(polyPoints, title, sDescription, 'Watershed')
}
}
}

function OnFailedWatershed(result)
{
    var test = result;
}
function OnSucceededCounty(result)
{
// map.DeleteAllShapeLayers();
if (result != null){
var polyRings = result.getElementsByTagName("RING").length;
for (var h=polyRings-1; h > -1; h--)
{
var PolyCoords = result.getElementsByTagName("RING")[h].text;
var Points = PolyCoords.split(/\;/);
var polyPoints = new Array();
var LatLong = new Array();
for (var i=0; i< Points.length; i++)
{
    LatLong = Points[i].split(/\ /);
    LatLong[1] = parseFloat(LatLong[1]);
    LatLong[0] = parseFloat(LatLong[0]);
    polyPoints.push(new VELatLong(LatLong[1],LatLong[0]));
}
var title = 'County';
var sDescription = '';

AddFeaturePoly(polyPoints, title, sDescription, 'County')
}
}
}

function AddFeaturePoly(polyPoints, title, sDescription, isCityCounty)
           {             
            if (selected6thField != undefined)
            {
                map.DeleteShape(selected6thField);
            }
            shape = new VEShape(VEShapeType.Polygon, polyPoints);           
            var icon = new VECustomIconSpecification();
            var layer = null;
            icon.Image = 'img/blankIcon.gif';
            shape.SetTitle(title);
            shape.SetDescription(sDescription);shape.SetCustomIcon(icon);
            shape.ShowDetailOnMouseOver = false;
            shape.SetCustomIcon(icon);            
            
            shape.SetLineWidth(3);            
            shape.SetLineColor(new VEColor(102,0,0,1.0));
            shape.SetFillColor(new VEColor(102,0,0,0.1));   
            shape.SetZIndex(1000,60);
             
                        
            map.SetMapView(polyPoints);   
            selected6thField = shape;                
            toggleTabs('overview');
            ShowShapes('watersheds'); 
            //add6thWatershed = true; 
            
            map.AddShape(shape);                                            
                       
         }

