﻿// JScript File
//This file contains the client side functions required for thematic selections functionality
var isIE = (window.navigator.appName.toLowerCase().indexOf("microsoft")>=0);
var _targetLayerDrpElemId;
var _aggFunctionsDrpElemId;
var _orderByElemId;

function btnMapResultsThematically_Click(searchId)
{
    // NPR - Modified to use HandleTMDBSearchFrame instead of NGOpenPopup to resolve issue after symbol refresh issue was fixed in ThemeControl
    window.top.HandleTMDBSearchFrame(window.top.NGServerUrl()+'/WebPages/ThematicMap/ThematicSelections.aspx');
}

function showInfo(elem, divElemId)
{
    if(elem != null && divElemId != null)
    {       
        var divElem = document.getElementById(divElemId);
        if(divElem != null)
        {        
            divElem.style.visible = true;
            divElem.style.display = 'block';            
            SetBoxPosition(elem, window.event, divElemId);
        }
    }
}

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 HandlePostData(postData)
{

}

function ToogleOrderByControlsVisibility(drpChooseElem, drpOrderByElemId)
{
    if(drpChooseElem != null && drpOrderByElemId != null)
    {
        var aggregateValue = drpChooseElem.value;        
        var drpOrderByElem = document.getElementById(drpOrderByElemId);
        
        
        if(drpOrderByElem != null && aggregateValue != null)
        {
            if(aggregateValue != "LAST" && aggregateValue != "FIRST")
            {                
                drpOrderByElem.disabled= true;
                drpOrderByElem.selectedIndex = -1;
            }
            else
            {                
                drpOrderByElem.disabled= false;
                if(drpOrderByElem.selectedIndex == -1)
                {   
                    if(drpOrderByElem.options.length > 1)
                        drpOrderByElem.selectedIndex = 0;
                }
            }            
        }
    }
}
/*
    This function refreshes the target layers dropdown based on the map tab selected.
*/
function mapTab_OnChange(drpMaptabElem, drpTargetLayerId)
{    
    if(drpMaptabElem != null && drpTargetLayerId != null)
    {
        _targetLayerDrpElemId = drpTargetLayerId;
        PageMethods.GetThematicLayers(drpMaptabElem.value, HandleGetThematicLayerPostBack);
    }
}
/*
    This function refreshes the layer dropdown
*/
function HandleGetThematicLayerPostBack(postData)
{    
//    ClearDropdownItems(_targetLayerDrpElemId);    
//    if(postData != null)
//    {        
//        var optionsList = getOptionsItems(postData);       
//        if(optionsList != null)
//        {            
//	        //add items to the dropdown.
//	        AddDropDownItems(_targetLayerDrpElemId, optionsList);	    
//        }
//    }

    AddElementsToDropDown(_targetLayerDrpElemId, postData);
}

function thematicColumns_OnChange(drpThematicColElem, drpAggFunctionElemId, drpOrderByElemId)
{
    if(drpThematicColElem != null && drpAggFunctionElemId != null)
    {
        _aggFunctionsDrpElemId = drpAggFunctionElemId;
        _orderByElemId = drpOrderByElemId;
        PageMethods.GetSupportedFunctions(drpThematicColElem.value, HandleAggrFunctionsPostBack);        
    }
}

function HandleAggrFunctionsPostBack(postData)
{    
    AddElementsToDropDown(_aggFunctionsDrpElemId, postData);
    ToogleOrderByControlsVisibility(_aggFunctionsDrpElemId, _orderByElemId);
}

function AddElementsToDropDown(drpElemId, dropdownDataInXml)
{
    ClearDropdownItems(drpElemId);    
    if(dropdownDataInXml != null)
    {        
        var optionsList = getOptionsItems(dropdownDataInXml);       
        if(optionsList != null)
        {            
	        //add items to the dropdown.
	        AddDropDownItems(drpElemId, optionsList);	    
        }
    }
}

/*
    This function creates the option items for the string passed.
*/
function getOptionsItems(xmlStr)
{
    var optionsList = new Array();  
    var xmlDoc = getXMLDoc(xmlStr);
    if(xmlDoc != null)
    {        
        //fetch LISTITEM node and create the option list
        var listItemsList = xmlDoc.getElementsByTagName("LISTITEM");    
        
        if(listItemsList != null && listItemsList.length > 0)
        {	    	                 	    
            for( i=0; i<listItemsList.length; i++)
	        {		    
		        var id = getXMLAttributeValue(listItemsList.item(i),"id");
		        var name= getXMLAttributeValue(listItemsList.item(i),"name");
		        var selected = getXMLAttributeValue(listItemsList.item(i),"selected");
		        
		        var option = createOption(name, id);		        
		        if(selected != null && selected == "true")
		        {
		            option.selected = true;
		        }
    		    
		        if(option != null)
		        {
                    optionsList.push(option);
                }                
	        }		
        }
    }
    
    return optionsList;
}
/*
    Returns the xmldocument object.
*/
function getXMLDoc(xmlStr)
{    
    var objXmlDoc = null;
    if(isIE)
    {
	    objXmlDoc = new ActiveXObject("Msxml2.DOMDocument");
	    objXmlDoc.loadXML(xmlStr);	            	    
    }
    else
    {
        objXmlDoc = new DOMParser().parseFromString(xmlStr, "text/xml");	                    
    }    
    return objXmlDoc;
}
/*
    Clears the items in the maptip object and adds passed option items to it.
*/
function ClearDropdownItems(dropDownElemId)
{        
    if(dropDownElemId != null)
    {
        var drpElem = document.getElementById(dropDownElemId);
        if(drpElem != null)
        {                    
            var count = drpElem.options.length;                        
            for(i=count-1; i>=0; i--)
            {                   
                drpElem.options[i] = null;
            }            
        }
    }
}
/*
    Returns the attribute value in the given node.
*/
function getXMLAttributeValue(node,attributeName)
{    
	if(node && node.attributes)
	{	    
		attrNode=node.attributes.getNamedItem(attributeName);
		if(attrNode)
			return attrNode.nodeValue;
	}
}
/*
    Create an option element and sets passed value and text to it.
*/
function createOption(itemText, itemValue)
{
    var option = document.createElement("OPTION");
    if(option != null)
    {
        option.text = itemText;
        option.value = itemValue;        
    }
    return option;
}
/*
   Adds items to the option button.
*/
function AddDropDownItems(dropDownElemId, items)
{    
    if(dropDownElemId != null && items != null)
    {
        var drpElem = document.getElementById(dropDownElemId);    
        if(drpElem != null)
        {        
            for(i=0; i<items.length; i++)
            {
                if(items[i] != null)
                {
                    drpElem.options.add(items[i]);
                }
            }            
        }
    }
}

function columnInfo()
{
    function column( id, name, type)
    {
        this.ColumnId = id;
        this.ColumnName = name;
        this.ColumnDataType = type;
    }
}
function getObject(id)
{
	return document.getElementById(id);		
}
function showLoading()
{
return;
	var loadObj=getObject("LoadDiv");
	showObject(loadObj);
}
function showObject(theObj) 
{		
  	if(theObj)
  		theObj.style.visibility = "visible";
}	
function hideLoading()
{
	var loadObj=getObject("LoadDiv");
	hideObject(loadObj);
}
function hideObject(theObj) 
{		
  	if(theObj)
		theObj.style.visibility = "hidden";
}