// JScript File

function CombineAllLinkClicked(hidElemId, mode)
{
    hidElem = document.getElementById(hidElemId);
    if(hidElem != null)
    {
        hidElem.value = mode;        
    }    
}
function checkboxClicked(currchkElem, selElemId, checkAllElemId, checkElemId, start, end)
{
    AddToHiddenElement(currchkElem, selElemId);
    
    checkAllElem = document.getElementById(checkAllElemId);
    startIndex = parseInt(start);
    endIndex = parseInt(end);
    if(startIndex != "NaN" && endIndex != "NaN" && checkAllElem != null)
    {        
        for(i= startIndex; i <=endIndex ; i++)
        {            
            checkSelectedElem = document.getElementById(checkElemId + i);
            if(checkSelectedElem != null)
            {
                checkAllElem.checked = checkSelectedElem.checked;
                if(! checkSelectedElem.checked)
                {
                    break;
                }                
            }            
        }        
    }    
}

function CheckAll(checkAllElemId, chkElemId, start, end, selElemId)
{         
    checkAllElem = document.getElementById(checkAllElemId);
    startIndex = parseInt(start);
    endIndex = parseInt(end);
    if(startIndex != "NaN" && endIndex != "NaN" && checkAllElem != null)
    {        
        for(i= startIndex; i <=endIndex ; i++)
        {            
            checkSelectedElem = document.getElementById(chkElemId + i);
            if(checkSelectedElem != null)
            {
                checkSelectedElem.checked = checkAllElem.checked;                                
                AddToHiddenElement(checkSelectedElem, selElemId);
            }
            else
            {
                alert("checkSelectedElem is null - " + (chkElemId + i));
            }
        }
    }            
}


function AddToHiddenElement(chkElem, hidElemId)
{     	
    hidElem = document.getElementById(hidElemId);    
    if(chkElem != null && hidElem != null)
    {                
        value = chkElem.value;
        if(chkElem.checked)
        {            
            if(hidElem.value.length > 0)
            {
                hidElem.value += "," + value;
            }
            else
            {
                hidElem.value += value;
            }
        }
        else
        {                        
            hidValue = hidElem.value;
            arr = hidValue.split(",");            
            newhidValue = "";                        
            
            for(j = 0; j < arr.length; j++)
            {                                                   
                if( arr[j] != "" && arr[j] != value)
                {                              
                    newhidValue += arr[j] + ",";
                }              
            }         
            
            if(newhidValue != "" && newhidValue.length > 0)
            {
               newhidValue = newhidValue.substring(0, newhidValue.length - 1);
            }
            hidElem.value = newhidValue;           
        }
    }
}

function ToggleClick(chkElemId, start, end)
{            
    startIndex = parseInt(start);
    endIndex = parseInt(end);
    for(i= startIndex; i <=endIndex ; i++)
    {
        checkSelectedElem = document.getElementById(chkElemId + i);
        if(checkSelectedElem != null)
        {
            checkSelectedElem.checked = ! checkSelectedElem.checked;
        }
    }            
}

function ImageClick(currElemId, currentPage)
{
    currElem = document.getElementById(currElemId);
    if(currElem != null)
    {
        var currentValue = parseInt(currentPage);
        if(currentValue == "NaN")
            currentValue = 0;                                
        currElem.value = currentValue;        
    }   
}

function toggleIndexing()
{
	var parentWindow;
	if(!sessionExpired)
    { 
	    if(window.opener)
	    {
	        parentWindow=window.opener;
	    }
	    else
	    {
	        parentWindow=window.top;
	    }
    	  
        if(parentWindow!=null && typeof(parentWindow.showLoading) == 'function')
            parentWindow.showLoading();
    	
        PageMethods.ToggleIndexing(toggleIndexingCallback);
    }
    else
    {
        HandleSessionExpired();
    }
}

function toggleIndexingCallback(obj)
{
    if(obj!=null )
    {
         RefreshMap();
    }
}

/////
function SelectionObject(selectionSetId, divElementId, currElemId, showNumbering, showNumberingAsHyperlink,  numberHyperlinkUrl,
        clientID, selectedRowsClientID, tableCssClass, checkAllResourceCaption, prevButtonImageUrl, nextButtonImageUrl, selectionListContentId)
{
    this.SelectionListContentId = selectionListContentId;
    this.SelectionSetId = selectionSetId;
    this.DivElementId = divElementId;    
    this.ShowNumbering = showNumbering;
    this.CurrentElementId = currElemId;    
    this.ShowNumberingAsHyperlink = showNumberingAsHyperlink;
    this.NumberHyperlinkUrl = numberHyperlinkUrl;
    this.ClientID = clientID;
    this.SelectedRowsClientID = selectedRowsClientID;
    this.TableCssClass = tableCssClass;
    this.CheckAllResourceCaption = checkAllResourceCaption;
    this.PrevButtonImageUrl = prevButtonImageUrl;
    this.NextButtonImageUrl = nextButtonImageUrl;
}

function NavigationButtonClick(selectionSetId, currentPage)
{
    var obj = eval("_selObject_" + selectionSetId);    
    if(obj != null)
    {
        RefreshSelectionListContent(obj, currentPage);        
    }
}

function RefreshSelectionListContent(selectionObject, currentPage)
{
    var selectionListObj = SelectionListContainer;
    
    if(selectionListObj != null && selectionObject != null)
    {    
        if(!sessionExpired)
        {           
            var divElement = document.getElementById(selectionObject.DivElementId);        
            if(divElement != null)
            {
                var currElem = document.getElementById(selectionObject.CurrentElementId);
                var currSelected = document.getElementById(selectionObject.SelectedRowsClientID);
                
                if(currElem  != null && currSelected != null)            
                {
                    ImageClick(selectionObject.CurrentElementId, currentPage);
                    var currentPage = currElem.value;
                    var selectedItems = currSelected.value;                  
                 
                    var reponse = selectionListObj.GetSelectionContentHtml(selectionObject.SelectionSetId, 
                                                                        currentPage,
                                                                        Boolean(selectionObject.ShowNumbering),
                                                                        Boolean(selectionObject.ShowNumberingAsHyperlink),
                                                                        selectionObject.NumberHyperlinkUrl,
                                                                        selectionObject.ClientID,
                                                                        selectionObject.SelectedRowsClientID,
                                                                        selectionObject.TableCssClass,
                                                                        selectionObject.CheckAllResourceCaption,
                                                                        selectionObject.PrevButtonImageUrl,
                                                                        selectionObject.NextButtonImageUrl,
                                                                        selectionObject.CurrentElementId,
                                                                        selectedItems,
                                                                        selectionObject.SelectionListContentId
                                                                        );
                    if(reponse != null && reponse.value != null)
                    {                                                                        
                        divElement.innerHTML = reponse.value.SelectionControlHTML; 
                        currSelected.value = reponse.value.SelectedItemsIds; 
                        RefreshMap();
                    }
                                   
                }
            }
        }
        else
	    {
	        HandleSessionExpired();
	    }
    }
}

function itemAttr_onClick(eventArg, argValue)
{
    var formElements = document.getElementsByTagName("form");
    if(formElements!= null && formElements.length > 0 && formElements[0] != null)
    {    
       
        formElements[0].__EVENTTARGET.value = eventArg;
        formElements[0].__EVENTARGUMENT.value = argValue;
       if(Sys.WebForms.PageRequestManager.getInstance()._postBackSettings.async==true) 
       {
            Sys.WebForms.PageRequestManager.getInstance()._onFormSubmit(); 
       }
       else
       { 
            formElements[0].submit();
       } 
        
        
        
    }
}



function RefreshSelectionList(selectionSetId)
{
    var obj = eval("_selObject_" + selectionSetId);    
    if(obj != null)
    {
        var currElem = document.getElementById(obj.CurrentElementId);            
        if(currElem  != null)            
        {        
            RefreshSelectionListContent(obj, currElem.value);
        }
    }
}

    //-----------------------------------------
     function ExportButtonClicked(selectionSetId)
       {
         if(selectionSetId != null)
          {   
           var url = '/Webpages/Reports/Reports.aspx?SELECTIONSETID=' + selectionSetId; 
               if (window.NGServerUrl)
                {
                   url = NGServerUrl()+ url;        
                }
               
             if (window.HandleReportsFrame && PageMethods.RefreshMaps)
             {
               HandleReportsFrame(url);
             }
             else
             {
               // NPR - changes for DBSelections
               // window.top will handle situations when the Reports is launced in a popup from within another popup
               // which is the case in the DB selections that is launched from the Results main tab
               if (window.top.HandleReportsFrame)
               {
                 // Reports iFrame Needs to be within the Common Popups Content within the MapViewer Page
                 // This will make it show in the Results tab, else it will be hidden and present in the Map main tab
                 window.top.HandleReportsFrame(url);
               }
               else
               {
                 var SelectionReportWindow = window.open(url,"reportWin","toolbar=no, directories=no, location=no, status=yes, menubar=no, resizable=yes, scrollbars=yes, width=400, height=200");    
               }
             }
            
            }
        } 


function NGExpandCollapse(LayerToShow, imageElem, selectionSetId)
{       
   
	              
    var serverName= window.location.protocol + '//' + window.location.hostname;
    var collapseimage= serverName + imageElem.getAttribute('collapseimage');
    var expanded = true;
    if   (String(imageElem.src)!= String(collapseimage))
        {
            
            imageElem.src=imageElem.getAttribute('collapseimage');
             
            HideLayer(LayerToShow, null, null);
            expanded = false;
        }
    else{
            
           imageElem.src=imageElem.getAttribute('expandimage'); 
            
            ShowLayer(LayerToShow, null, null);
           expanded = true;
        }
        
    if(!sessionExpired)
    {                
        PageMethods.UpdateExpandState(selectionSetId, expanded);
    }
    else
    {
        HandleSessionExpired();
    } 
}


function InfoToolClicked(url)
{   
  if(url != null)
    {
     
    //If the function to open this in a iFrame exist then call it else open to a new window.        
	if(window.HandleInfoToolFrame)
		{
			HandleInfoToolFrame(url);
		}
	else
		{
   			window.open(url,"InfoWindow","toolbar=no, directories=no, location=no,status=yes, menubar=no, resizable=yes, scrollbars=auto, width=550, height=500");
		}
	}
	return;
}


function ZoomToAllSelections()
{    
    if(window.displayMapOverlay)
    {
        if(!sessionExpired)
        {                    
            PageMethods.GetSelectionsEnvelope(HandleZoomEnvelope);        
        }
        else
        {
            HandleSessionExpired();
        }    
    }
    else
    {
        if(!sessionExpired)
        {                    
            PageMethods.SelectionZoomToAll(HandleZoomToSelection);        
        }
        else
        {
            HandleSessionExpired();
        }    
    }
}
function HandleZoomEnvelope(response)
{
    if(response!=null && response.length > 0 && window.RefreshVEandOnPointMap)
    {
        RefreshVEandOnPointMap(response);
    }
}
function HandleZoomToSelection(response)
{
    if(response != null  && Boolean(response))
    {       
       RefreshMap();
    } 
}

function ZoomToActiveSelections()
{ 
    if(window.__activeSelectionSetId)
    {        
        var selectionSetId = window.__activeSelectionSetId;
        if(selectionSetId != null)
        {
            var selectionObject = eval("_selObject_" + selectionSetId);    
            //var selectionListObj = PageMethods;
            if(selectionObject != null )
            {                
                if(!sessionExpired)
                {                
                    var currSelected = document.getElementById(selectionObject.SelectedRowsClientID);                        
                    if(currSelected != null)            
                    {                        
                        //ImageClick(selectionObject.CurrentElementId, currentPage);                    
                        var selectedItems = currSelected.value;
                        if(window.displayMapOverlay)
                            PageMethods.GetActiveSelectionsEnvelope(selectionSetId, selectedItems,HandleZoomEnvelope);                    
                        else
                            PageMethods.SelectionsZoomToActive(selectionSetId, selectedItems,HandleZoomToSelection);                                                
                        
                    }
                }
                else
	            {
	                HandleSessionExpired();
	            }
            }
        }
    }    
}

function ClientExtenderClick(setId, itemId, url)
{
    if (url != null)
    {
        if (url.indexOf("http") < 0)
        {
           url = NGServerUrl() + url;
        }
        if (url.indexOf("?") > 0)
            url = url + "&";
        else
            url = url + "?";
    }
    url = url + "setid=" + setId + "&itemid=" + itemId;
    NgOpenPopup(url,400,300,"OnPoint");
}

var divElement=null;
var currSelected =null;
function RefreshSelectionListContent(selectionObject, currentPage)
{
    
    var selectionListObj = PageMethods;
    
    if(selectionListObj != null && selectionObject != null)
    {     
        if(!sessionExpired)
        {        
        
            divElement = document.getElementById(selectionObject.DivElementId);        
            if(divElement != null)
            {
                var currElem = document.getElementById(selectionObject.CurrentElementId);
                currSelected = document.getElementById(selectionObject.SelectedRowsClientID);
                
                if(currElem  != null && currSelected != null)            
                {
                    ImageClick(selectionObject.CurrentElementId, currentPage);
                    var currentPage = currElem.value;
                    var selectedItems = currSelected.value;
					try
                    {						
                        selectionListObj.GetSelectionContentHtml(selectionObject.SelectionSetId, 
                                                                            currentPage,
                                                                            Boolean(selectionObject.ShowNumbering),
                                                                            Boolean(selectionObject.ShowNumberingAsHyperlink),
                                                                            selectionObject.NumberHyperlinkUrl,
                                                                            selectionObject.ClientID,
                                                                            selectionObject.SelectedRowsClientID,
                                                                            selectionObject.TableCssClass,
                                                                            selectionObject.CheckAllResourceCaption,
                                                                            selectionObject.PrevButtonImageUrl,
                                                                            selectionObject.NextButtonImageUrl,
                                                                            selectionObject.CurrentElementId,
                                                                            selectedItems,
                                                                            selectionObject.SelectionListContentId,
                                                                            HandleSelectionNavigation    
                                                                            );
                       
                    }
                    catch(e)
                    {
						alert(e.message);
                    }
                }
            }
        }
        else
	    {
	        HandleSessionExpired();
	    }
    }
}

function HandleSelectionNavigation(response)
{   
    
    if(response != null)
    {
        divElement.innerHTML = response.SelectionControlHTML; 
        currSelected.value = response.SelectedItemsIds; 
        
        // NPR - changes for DBSelections
        // refresh map only when in Map tab, this is not needed for DB selections currently
        if (PageMethods.RefreshMaps)
            RefreshMap();
        
    }
} 


function SaveSelections()
{ 
    if(window.__activeSelectionSetId)
    {                        
        var selectionSetId = window.__activeSelectionSetId;
        if(selectionSetId != null)
        {
            var selectionObject = eval("_selObject_" + selectionSetId);    
            //var selectionListObj = PageMethods;
            if(selectionObject != null )
            {                
                if(!sessionExpired)
                {                
                    var currSelected = document.getElementById(selectionObject.SelectedRowsClientID);                        
                    if(currSelected != null)            
                    {                        
                        //ImageClick(selectionObject.CurrentElementId, currentPage);                    
                        var selectedItems = currSelected.value;
                        //                        
                        PageMethods.UpdateSelections(selectionSetId, selectedItems,HandleZoomToSelection);                        
                        if (PageMethods.RefreshMaps)
                        {
                          openSavePopup(NGServerUrl() + '/WebPages/Selections/SaveSelections.aspx');
                        }
                        // NPR - changes for DBSelections
                        // window.top will handle situations when the Save Selections is launced in a popup from within another popup
                        // which is the case in the DB selections that is launched from the Results main tab
                        else if (window.top.HandleSaveSelectionFrame)
                        {
                          // Save Selection iFrame Needs to be within the Common Popups Content within the MapViewer Page
                          // This will make it show in the Results tab, else it will be hidden and present in the Map main tab
                          window.top.HandleSaveSelectionFrame(NGServerUrl() + '/WebPages/Selections/SaveSelections.aspx');
                        }
                        else
                        {
                          window.open(NGServerUrl() + '/WebPages/Selections/SaveSelections.aspx',"SaveDBSelectionsWindow","toolbar=no, directories=no, location=no,status=yes, menubar=no, resizable=yes, scrollbars=auto, width=450, height=200");
                        }
                    }
                }
                else
	            {
	                HandleSessionExpired();
	            }
            }
        }
    }    
}

function AppendSelections()
{ 
    if(window.__activeSelectionSetId)
    {                        
        var selectionSetId = window.__activeSelectionSetId;
        if(selectionSetId != null)
        {
            var selectionObject = eval("_selObject_" + selectionSetId);    
            //var selectionListObj = PageMethods;
            if(selectionObject != null )
            {                
                if(!sessionExpired)
                {                
                    var currSelected = document.getElementById(selectionObject.SelectedRowsClientID);                        
                    if(currSelected != null)            
                    {                        
                        //ImageClick(selectionObject.CurrentElementId, currentPage);                    
                        var selectedItems = currSelected.value;
                        PageMethods.UpdateSelections(selectionSetId, selectedItems,HandleZoomToSelection);                        
                        if (PageMethods.RefreshMaps)
                        {
                          openAppendPopup(NGServerUrl() + '/WebPages/Selections/AppendSelections.aspx');
                        }
                        // NPR - changes for DBSelections
                        // window.top will handle situations when the Append Selections is launced in a popup from within another popup
                        // which is the case in the DB selections that is launched from the Results main tab
                        else if (window.top.HandleAppendSelectionFrame)
                        {
                          // Append Selection iFrame Needs to be within the Common Popups Content within the MapViewer Page
                          // This will make it show in the Results tab, else it will be hidden and present in the Map main tab
                          window.top.HandleAppendSelectionFrame(NGServerUrl() + '/WebPages/Selections/AppendSelections.aspx');
                        }
                        else
                        {
                          window.open(NGServerUrl() + '/WebPages/Selections/AppendSelections.aspx',"AppendSelections","toolbar=no, directories=no, location=no,status=yes, menubar=no, resizable=yes, scrollbars=auto, width=450, height=200");
                        }
                    }
                }
                else
	            {
	                HandleSessionExpired();
	            }
            }
        }
    }    
}

function SetBoxPosition(imageElem, evt, boxElemId)
{
    if(imageElem != null && boxElemId != null)
    {     
         var boxElem = document.getElementById(boxElemId);
         if(boxElem != null)
         {       
            getClientMouseXXYY(evt);          
            boxElem.style.top = __clientMouseYY + "px";
            boxElem.style.left = (__clientMouseXX + 5) + "px";            
         }
    }
}

var __clientMouseXX;
var __clientMouseYY;
function getClientMouseXXYY(e) 
{    
	if(!e)
	{
		e=window.event;
	}
	if(e)
	{	
		if(e.pageX)
		{			  		    		    				    		
			__clientMouseXX = e.pageX;
			__clientMouseYY = e.pageY;			
		}
		else if (e.x)
		{            
            //__clientMouseXX = e.clientX + document.body.scrollLeft;
	        //__clientMouseYY = e.clientY + document.body.scrollTop;		    
	        __clientMouseXX = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
	        __clientMouseYY = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;	        	
		}
	}	
}

function btnDBSelections_Click(searchId)
{
    NgOpenPopup(NGServerUrl() + '/WebPages/Selections/DBSelectionListContainer.aspx?SEARCHID=' + searchId, '250','600','Database Selections')
}