﻿// JScript File
//function GetPopulationPyramid(ppCommunity,ppYear)
//{
//    var hfArgs = $get('hfViewFigure');
//    hfArgs.value = ppCommunity+":"+ppYear;
//    __doPostBack('hfViewFigure','');
//}
var ActiveAccordionPane = 1;
var isLoadingPlaceToBin = false;
var Loading = false;
var Browser = getBrowser();
var isMapLoaded = false;
var isOnLoad = true;


 function getBrowser()
        {
        var Browser = navigator.appName; 
        if (navigator.userAgent.search(/Safari/) != -1)
        {
        alert('This tool is not currently fully supported by Safari.  Please use either Mozilla Firefox 2 or greater, or Internet Explorer 7.  We are sorry for the inconvienience.');
        }            
        return Browser;
        }

function setAccordionDisplay(srcElement)
{
    var AccordionContent = getElementsByClassName(document,"div", "accordionContent");
    AccordionNumber = srcElement-1;    
    AccordionContent[AccordionNumber].style.MozOpacity="1.0";
    AccordionContent[AccordionNumber].style.opacity="1.0";        
    AccordionContent[AccordionNumber].parentNode.style.MozOpacity="1.0";
    AccordionContent[AccordionNumber].parentNode.style.opacity="1.0";
    
    for (var i=1;i<18;i++)
    {     
    var imgExpandCollapse = "APimg"+i;
    imgExpandCollapse = $get(imgExpandCollapse);
    if (i == srcElement)
    {
        if (ActiveAccordionPane == srcElement)
            {
            imgExpandCollapse.src = 'img/expand.gif';
            }
        else
        {
        imgExpandCollapse.src = 'img/collapse.gif';
        }
    }        
    else
    {
    imgExpandCollapse.src = 'img/expand.gif';  
    }
    }
    ActiveAccordionPane = srcElement;
}
function toggleTabs(tab)
{
var overviewTab = $get('overviewtab');
var divOverviewPage = $get('divOverviewPage');
var divFind = $get('divFind');
var divDashboard = $get('divDashboard');
var selectplaceTab = $get('selectplacetab');
var viewreportTab = $get('viewreporttab');
var divMapPage = $get('divMapPage');
var divReportPage = $get('divReportPage');
var divNoPlaceSelectedMsg = $get('divNoPlaceSelectedMsg');
switch (tab)
    {
    case 'overview':
        overviewTab.src = 'img/overviewtabactive.gif';
        divOverviewPage.style.display = 'block';
        divDashboard.style.display = 'none';
        divFind.style.display = 'none';
        selectplaceTab.src = 'img/selectcommunitytab.gif';
        viewreportTab.src = 'img/viewreporttab.gif';  
        if (!isOnLoad)//Workaround for the slider display
        {
        divMapPage.style.display = 'none';
        }  
        else
        {
        isOnLoad = false;
        }                  
        divReportPage.style.display = 'none';     
        divNoPlaceSelectedMsg.style.display = "none"; 
        break;       
    case 'selectplace':        
        overviewTab.src = 'img/overviewtab.gif';
        divOverviewPage.style.display = 'none';
        divDashboard.style.display = 'block';
        divFind.style.display = 'block';
        selectplaceTab.src = 'img/selectcommunitytabactive.gif';
        viewreportTab.src = 'img/viewreporttab.gif';        
        divMapPage.style.display = 'block';   
        divReportPage.style.display = 'none';     
        divNoPlaceSelectedMsg.style.display = "none";
        if (!isMapLoaded)
        {
            GetMap();
            isMapLoaded = true;
            divMapPage.style.visibility = 'visible';            
        }
        break;
    case 'report':
        overviewTab.src = 'img/overviewtab.gif';
        divOverviewPage.style.display = 'none';
        divDashboard.style.display = 'block';
        divFind.style.display = 'block';
        selectplaceTab.src = 'img/selectcommunitytab.gif';
        viewreportTab.src = 'img/viewreporttabactive.gif';
        divMapPage.style.display = 'none';   
        if (PlaceBin.length > 0)
        {        
        divReportPage.style.display = 'block';  
        }
        else
        {        
        divNoPlaceSelectedMsg.style.display = "block";
        }   
        break;   
    }
}

function toggelMapDisplay()
{
    var divMapPage = $get('divMapPage');
    divMapPage.style.display = 'block'; 
    if (!isMapLoaded)
        {
            GetMap();
            isMapLoaded = true;
            divMapPage.style.visibility = 'visible';            
        }
}

function toggleSearchBox(geoType)
{
    var divCountiesSearch = $get('divCountiesSearch');
    var divCommunitiesSearch = $get('divCommunitiesSearch');
    var divDropDownLable = $get('divDropDownLabel');
    if (geoType == 'counties')
    {
    divCountiesSearch.style.display = 'block';
    divCommunitiesSearch.style.display = 'none';
    divDropDownLable.innerHTML = 'Counties';
    }
    else
    {
    divCountiesSearch.style.display = 'none';
    divCommunitiesSearch.style.display = 'block';
    divDropDownLable.innerHTML = 'Places';
    }
}

function ToggleDash(Show)
{ 
    var divShowDash = $get('divShowDash');  
    var divStepsParent = $get('divStepsParent');  
    if (Show == 'on')
    {
    divStepsParent.style.display = 'block';
    divShowDash.style.display = 'none';
    }
    else
    {
     divStepsParent.style.display = 'none';
    divShowDash.style.display = 'block';
    }
}

function getVariables(AccordionNumber)
{
    var srcElement = e.srcElement;    
    setAccordionDisplay(srcElement);
    //var aLinkColor = e.srcElement;
    //aLinkColor.style.color = 'white';
//  var ParamPanelID = {"PanelID": PanelID}; 
//  var SelectedCommunity = $get('DropDownList1').value;
//  var ParamCommunity = {"Community": SelectedCommunity}; 
//  WebServices.GetVariables.GetVariablesSQL(ParamPanelID.PanelID, ParamCommunity.Community, OnSucceeded, OnFailed);    
}

function OnSucceededCommunityData(result)
{
 
 var PanelID = result[0];
 var PanelContent = 'VariableContent'+PanelID;
 PanelContent = $get(PanelContent);
 PanelContent.innerHTML = result[1]; 
 var LoadingPane = 'LoadingPane'+PanelID;
 LoadingPane = $get(LoadingPane);
 LoadingPane.style.display = 'none'; 
 //if (hdfTopics.length == PanelID)
 if (!Loading)
 {
 var LoadingDiv = $get('LoadingDiv');
 LoadingDiv.style.display = "none";    
 var divMyAccordion = $get('MyAccordion');
 divMyAccordion.children[0].value = 1; 
 }
}

function OnFailedCommunityData(result)
{
var test= "";
}

function ValidateBin()
{
    var isValid = true;
    var errorMessage = "";
    var ReferenceType = "";
    var PreviousReferenceType = "";
    var CDPArray = new Array();
    var CTArray = new Array();
    var CountyArray = new Array();
    var CityArray = new Array();
    for (var i=0; i < PlaceBin.length; i++)
    {                  
            var Place = PlaceBin[i].split(/\~/)[0];
            var PlaceInformation = PlaceBin[i].split(/\~/)[1].split(/\,/);
            var ReferenceType = PlaceInformation[0];
            var sContains = PlaceInformation[2].split(/\;/);
            var sPartOf = PlaceInformation[3].split(/\;/);
            switch (ReferenceType)
            {
                case "Census Desig Places 2000":
                CDPArray.push(Place);                           
                break;
                case "Census Tracts 2000":                
                if (CTArray.length > 0)
                {
                    //check to see if partof or contains has place name
                    for (var j= 0; j<CTArray.length;j++)
                    {
                      for (h=0;h< sContains.length;h++)
                      if (CTArray[j] == sContains[h].trim())
                      {
                      isValid = false;
                      errorMessage = "You have selected two places within the same Census Tract.  Data will not be unique for a report.  Select a place in a different Census Tract, or view report for just one Census Tract.";
                      }
                    }
                }     
                CTArray.push(Place);
                break;
                case "County":
                CountyArray.push(Place);                
                break;
                case "Incorporated Cities 2005":
                CityArray.push(Place);
                break;
            }
            if (CDPArray.length > 0 && CTArray.length > 0)
            {
            isValid = false;
            errorMessage = "You have selected to combine a place and Census Tract which is not allowed by this tool.";
            }           
            if (!isValid)
            {
            alert(errorMessage);
            }
       
    }
    return isValid;
}


function getDataforCommunity(value)
{
    var tdSelectedCommunity = $get('tdSelectedCommunity');
    var SelectedCommunity = $get('DropDownList1').value;
    tdSelectedCommunity.innerHTML = SelectedCommunity;
    var hdfTopics = $get('hdfTopics').value;
    hdfTopics = hdfTopics.split(/\;/);
   for (var i=0;i<hdfTopics.length;i++)
  {
  var ParamPanelID = {"PanelID": i+1};   
  var ParamCommunity = {"Community": SelectedCommunity}; 
  WebServices.GetVariables.GetVariablesSQL(ParamPanelID.PanelID, ParamCommunity.Community, OnSucceededCommunityData, OnFailedCommunityData); 
  }
}
  
function generateReport()
{
    map.HideInfoBox();
    var divNoPlaceSelectedMsg = $get('divNoPlaceSelectedMsg');
    var isValidToCombine = true;
    if (PlaceBin.length > 0)
    {
    divNoPlaceSelectedMsg.style.display = 'none';     
    var ReportType = "";   
        if (PlaceBin.length > 1)
        {
             //Check to see if Combine/Compare    
            var rBtnCombine = $get('rBtnCombine');
            if (rBtnCombine.checked)
            {
                ReportType = "Combined";
                isValidToCombine = ValidateBin();    
            }
            else 
            {
            ReportType = "Comparison";
            }
        }   
    var Places = "";
    var PlaceTypes = "";
    var _STFIDs = "";    
    if (isValidToCombine)
        {
            toggleTabs('report');
            var LoadingDiv = $get('LoadingDiv');
            LoadingDiv.style.display = "block";        
            for (var h=0;h<PlaceBin.length;h++)
            {
                var PlaceParent = PlaceBin[h];
                PlaceParent = PlaceParent.split(/\~/);
                Place = PlaceParent[0];
                PlaceType = PlaceParent[1];
                _STFID = PlaceType.split(/\,/)[1];
                PlaceType = PlaceType.split(/\,/)[0];  
            if (h == PlaceBin.length-1)
                {
                    Places += Place;
                    PlaceTypes += PlaceType;
                    _STFIDs += _STFID;
                }
            else
                {
                    Places += Place + ',';
                    PlaceTypes += PlaceType+ ',';
                    _STFIDs += _STFID+ ',';
                }      
            }
            var hdfTopics = $get('hdfTopics').value;
            hdfTopics = hdfTopics.split(/\;/); 
            var ParamCommunity = {"Place": Places}; 
            var ParamSTFID = {"STFID": _STFIDs};
            var ParamPlaceType = {"PlaceType": PlaceTypes};  
            var ParamReportType = {"ReportType": ReportType};
           for (var i=0;i<hdfTopics.length;i++)
          {
              if (i != hdfTopics.length-1)
              {
              Loading = true;
              }
              else
              {
              Loading = false;
              }
           var LoadingPane = 'LoadingPane'+(i+1);
           LoadingPane = $get(LoadingPane);
           LoadingPane.style.display = 'block';    
           var ParamPanelID = {"PanelID": i+1};   
          WebServices.GetVariables.GetVariablesSQL(ParamPanelID.PanelID, ParamCommunity.Place, ParamPlaceType.PlaceType, ParamSTFID.STFID,ParamReportType.ReportType, OnSucceededCommunityData, OnFailedCommunityData);   
          }
//          var ParamPanelID = {"PanelID": 3};   
//          WebServices.GetVariables.GetVariablesSQL(ParamPanelID.PanelID, ParamCommunity.Place, ParamPlaceType.PlaceType, ParamSTFID.STFID,ParamReportType.ReportType, OnSucceededCommunityData, OnFailedCommunityData);   

          var divReportTypeLabel = $get('divReportTypeLabel');
          var divReportScript = 
          divReportScript = '<table><tr><td width=900px align="center">';
          divReportScript += ReportType+" Report for "
          var PlacesForReportTitle = Places.split(/\,/);
          var PlaceTypesForReportTitle = PlaceTypes .split(/\,/);           
          for (var q=0;q<PlacesForReportTitle.length;q++)
          {
            if (PlaceTypesForReportTitle[q] == "Census Tracts 2000")
            {
                PlaceTypesForReportTitle[q] = "Census Tracts, 2000";
            }
            if (q == PlacesForReportTitle.length -1 && q != 0)
            {
                divReportScript += "<br> and ";
            }
            else if (q != PlacesForReportTitle.length -1 && q !=0)
            {
                divReportScript += ", <br>";
            }
            if (PlaceTypesForReportTitle[q] == "County")
            {
                divReportScript += PlacesForReportTitle[q] + ' '+PlaceTypesForReportTitle[q];
            }
            else
            {
                divReportScript += PlacesForReportTitle[q] + ' ('+PlaceTypesForReportTitle[q]+')';
            }   
          }  
          //divReportScript += '</td><td><span style="display:inline; width:100px;"><img src="img/download.gif"/ width=20px></span><span style="display:inline;"><a class="aDownload" href="DownloadSpeciesList.aspx?DataType=csv" target=_blank>.csv</a><a class="aDownload" href="DownloadSpeciesList.aspx?DataType=xml" target=_blank>.xml</a><a class="aDownload" href="DownloadSpeciesList.aspx?DataType=xls" target=_blank>.xls</a><a class="aDownload" href="DownloadSpeciesList.aspx?DataType=txt" target=_blank>.txt</a></span></td></tr></table>';
         // divReportScript += '</td><td><span style="display:inline; width:100px;"><a class="aDownload" href="PrintReport.aspx?Place='+Places+'&PlaceType='+PlaceTypes+'&STFID='+_STFIDs+'&ReportType='+ReportType+'" target=_blank><img src="img/print.gif" alt="Print"/></a></span><span style="display:inline;"><a class="aDownload" href="DownloadCommunityData.aspx?DataType=xls&Place='+Places+'&GeoType='+PlaceTypes+'" target=_blank><img src="img/download.gif"/ width=20px alt="Download (Excel)" ></a></span></td></tr></table>';
          divReportScript += '</td><td><span style="display:inline; width:100px;"><a class="aDownload" href="PrintReport.aspx?Place=' + Places + '&PlaceType=' + PlaceTypes + '&STFID=' + _STFIDs + '&ReportType=' + ReportType + '" target=_blank><img src="img/print.gif" alt="Print"/></a></span></td></tr></table>'; //<span style="display:inline;"><a class="aDownload" href="DownloadCommunityData.aspx?DataType=xls&Place=' + Places + '&GeoType=' + PlaceTypes + '" target=_blank><img src="img/download.gif"/ width=20px alt="Download (Excel)" ></a></span></td></tr></table>';

          divReportTypeLabel.innerHTML = divReportScript;         
          
          //WebServices.GetVariables.GetCommunityGeoHeading(ParamCommunity.Place, ParamPlaceType.PlaceType, ParamSTFID.STFID, OnSucceededReportHeading, OnFailedReportHeading); 
          //var ContainsPart = Place1Parent[1].split(/\,/)[2].replace(/\;/g,",");
         
        //alert(value);
        }
        else//Not valid to combine
        {
        toggleTabs('selectplace');
        }         
    }
    else //No place selected to generate report
    {
        toggleTabs('report');
    }             
  
} 

function toggleImgMapTips()
{
    var imgMapTips = $get('imgMapTips');
    var divMapTipsDesc = $get('divMapTipsDesc');
    if (divMapTipsDesc.style.display == 'none')
    {
        divMapTipsDesc.style.display = 'block';
        imgMapTips.src = 'img/collapse.gif';
    }
    else
    {
        divMapTipsDesc.style.display = 'none';
        imgMapTips.src = 'img/expand.gif';
    }    
}

function toggleImgScope()
{
    var imgScope = $get('imgScope');
    var divScopeDesc = $get('divScopeDesc');
    if (divScopeDesc.style.display == 'none')
    {
        divScopeDesc.style.display = 'block';
        imgScope.src = 'img/collapse.gif';
    }
    else
    {
        divScopeDesc.style.display = 'none';
        imgScope.src = 'img/expand.gif';
    }    
}

function toggleCommunityHeader(Community)
{
 var spanCommunity = 'sp'+Community;
 spanCommunity = $get(spanCommunity);
 var imgCommunity = 'img'+Community;
 imgCommunity = $get(imgCommunity);
 var divCommunity = 'div'+Community;
 divCommunity = $get(divCommunity);
 if (divCommunity.style.display == "none")
 {
    spanCommunity.innerHTML = 'Hide';
    divCommunity.style.display = 'block';
    imgCommunity.src = 'img/collapse.gif';
}
else
{
    spanCommunity.innerHTML = ' More Info';
    divCommunity.style.display = 'none';
    imgCommunity.src = 'img/expand.gif';
}
}
function popUp(objectID) {     
   var PopUpDiv = document.getElementById(objectID);
   if (PopUpDiv.style.visibility == "visible")
   {
    PopUpDiv.style.visibility = "hidden";
   }
   else
   {
    PopUpDiv.style.visibility = "visible";
   }
}

function toggleCheckBoxQuery(type)
{
    //var cbQueryCity = $get('cbQueryCity');
    var cbQueryPlace = $get('cbQueryPlace');
    var cbQueryTract = $get('cbQueryTract');
    switch (type)
    {
//        case "city":
//        cbQueryCity.checked = true;
//        cbQueryPlace.checked = false;
//        cbQueryTract.checked = false;
//        break;
        case "place":
        //cbQueryCity.checked = false;
        cbQueryPlace.checked = true;
        cbQueryTract.checked = false;
        break;
        case "tract":
        //cbQueryCity.checked = false;
        cbQueryPlace.checked = false;
        cbQueryTract.checked = true;
        break;
    }
}

function OnSucceededReportHeading(result)
{
    var ResultSplit = result.split(/\~/);
    var CommunityGeo = ResultSplit[0];
    var _Contains = ResultSplit[1].replace(/\;/g,',');
    var _PartOf = ResultSplit[2];
    var Place1Parent = PlaceBin[0];
    Place1Parent = Place1Parent.split(/\~/);
    var Place1 = Place1Parent[0];
    var PlaceType = Place1Parent[1];
    var _STFID = PlaceType.split(/\,/)[1];
    PlaceType = PlaceType.split(/\,/)[0];  
     var tdSelectedCommunity = $get('tdSelectedCommunity');  
  tdSelectedCommunity.innerHTML = '<span>'+Place1 + '</span><span style="font-size:10px;"> ('+ CommunityGeo+')</span>'; 
  
      if (_Contains != ' ')
      {
      tdSelectedCommunity.innerHTML += '<span style="font-size:9px; display:block">CONTAINS: '+_Contains+'</span>'; 
      }   
      if (_PartOf != ' ')
      {
      tdSelectedCommunity.innerHTML += '<span style="font-size:9px; display:block">PART OF: '+_PartOf+'</span>'; 
      } 
}

function OnFailedReportHeading(result)
{
}

function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/-/g, "\-");
	var oRegExp = new RegExp("(^|\s)" + strClassName + "(\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function addBtnPlaceToBin()
{
    if (!isLoadingPlaceToBin)
    {
        if (infoBtnStatus)
        {
            ToggleInfoFunction();
        }
        if (selectBtnStatus)
        {
            ToggleSelectFunction();
        }
        var rbtnCounties = $get('rbtnCounties');
        var rbtnCommunities = $get('rbtnCommunites');
        var searchText = '';
        if (rbtnCounties.checked == true)
        {
            var tbCounties = $get('tbCounties');
            searchText = tbCounties.value.trim();  
            var count = 1;         
            var ParamText = {"Text": searchText};   
            var ParamCount = {"Count": count}; 
            WebServices.GeoSearchService.RunForAutoCompleteCounties(ParamText.Text, ParamCount.Count, goToSelection); 
        }
        if (rbtnCommunities.checked == true)
        {
            var tbCommunities = $get('tbCommunities');
            searchText = tbCommunities.value.trim();     
            var count = 1;         
            var ParamText = {"Text": searchText};   
            var ParamCount = {"Count": count}; 
            WebServices.GeoSearchService.RunForAutoCompleteCommunities(ParamText.Text, ParamCount.Count, goToSelection);   
        }
        
    }
}

function toggleMapTips(onOff)
{
    var divMapTips = $get('divMapTips');
    if (onOff == 'on')
    {
        //divMapTips
    }
    else
    {
    }
}