﻿// JScript File

function Search_clientClick()
{
    if(!sessionExpired)
    {
        var searchByCoordObj = jsSearchByCoordsObj;
        if(searchByCoordObj  != null)
        {
            searchByCoordObj.ApplySearch(); 
        }
    }
    else
    {
        HandleSessionExpired();
    }  
}

function Reset_clientClick()
{
    if(!sessionExpired)
    {
        var searchByCoordObj = jsSearchByCoordsObj;
        if(searchByCoordObj  != null)
        {
            searchByCoordObj.ResetAll(); 
        }
    }
    else
    {
        HandleSessionExpired();
    }   
        
}

function HideSearchByCoords(divElemId)
{
    //hide the div element
    var divElem = document.getElementById(divElemId);
    if(divElem  != null)
    {        
        divElem.style.visibility = "hidden";
        divElem.style.display = "none"; 
    }
}

function DisplaySearchByCoords(divElemId)
{
    if(!sessionExpired)
    {     
        var divElem = document.getElementById(divElemId);
        if(divElem  != null)
        {                
            var searchByCoordObj = jsSearchByCoordsObj;
            if(searchByCoordObj != null)
            {
               searchByCoordObj.ResetScale();
            }
            divElem.style.visibility = "visible";
            divElem.style.display = "block"; 
              xZIndex(divElem, highZ++); 
        }
     }
    else
    {
        HandleSessionExpired();
    }
        
}

/*
    Client SearchByCoord object.
*/
function SearchByCoordsClient(txtXCoordElemId, txtYCoordElemId, txtScaleElemId, drpProjectionElemId)
{
    var m_scaleElementId = txtScaleElemId;
    this.XCoordElemId = txtXCoordElemId;
    this.YCoordElemId = txtYCoordElemId;
    this.ScaleElemId = txtScaleElemId;
    this.ProjectionElemId = drpProjectionElemId;    
    this.ApplySearch = processSearchByCoord;
    this.ResetAll = clearControls;
    this.ResetScale = ResetMaptabScale;
    
    function clearControls()
    {
        if(!sessionExpired)
        {
            var xCoord =  document.getElementById(this.XCoordElemId);
            var yCoord = document.getElementById(this.YCoordElemId);
            var scale = document.getElementById(this.ScaleElemId);  
            if(xCoord != null)
            {            
                xCoord.value = "";
            }
            if(yCoord !=null)
            {
                yCoord.value="";
            }
            if(scale!=null)
            {
                scale.value="";
            }
             var drpElem = document.getElementById(this.ProjectionElemId);
            if(drpElem != null && drpElem != 'undefined')
            {            
                //drpElem.options[0].selected = true;
                drpElem.selectedIndex =0;
            }  
        }
        else
        {
            HandleSessionExpired();
        }      
    }
    
    var status = false;
    function processSearchByCoord()
    {
        
        var xCoord = getElementValue(this.XCoordElemId);
        var yCoord = getElementValue(this.YCoordElemId);
        var scale = getElementValue(this.ScaleElemId);        
        var projection = null;
        
        var flag=true;
        
        //check whether the project dropdown element is spooled onto the client
        var drpElem = document.getElementById(this.ProjectionElemId);
        if(!sessionExpired)
         {
        if(drpElem != null && drpElem != 'undefined')
        {
            var selectedIndex =  drpElem.selectedIndex;
            if(selectedIndex == -1)
            {
                 alert("Please select a coordinate system.");
                 flag=false;
            }
            //set selected Project system 
            projection = getOptionValue(this.ProjectionElemId);
        }
        else
        {
            //set unknow projectsystemid
            projection = -1;            
        }
        
        if(flag && xCoord == "")
        {
            if(flag && yCoord == "")
            {
                
                alert("X-Coordinate, Y-Coordinate cannot be null.");
                flag=false;                
            }
            else
            {
                alert("X-Coordinate cannot be null.");
                flag=false;
            }
        }    
        if(flag && xCoord != "" && yCoord == "")
        {            
            alert("Y-Coordinate cannot be null.");
            flag=false;            
        } 
        
//        var RegularExpression=new RegExp("[+]?[0-9]*\.?[0-9]*");
//        var xmatch=xCoord.match(RegularExpression);
//        if(xmatch==null)
//        {
//            alert("Xcoord must be numeric and positive");
//            flag=false;
//        }
//        if(yCoord.match(RegularExpression))
//        {
//            alert("Ycoord must be numeric and positive");
//            flag=false;
//        }
//        if(scale.match(RegularExpression))
//        {
//            alert("Scale must be numeric and positive");
//            flag=false;
//        }

        var xValue=parseFloat(xCoord);
        var yValue=parseFloat(yCoord);
        var sValue=parseFloat(scale);
       
        if(flag && xValue != null && (xValue.toString()== "NaN" ))
        {
            if(flag && yValue!=null && (yValue.toString() == "NaN" ))
            {                
                alert("X-Coordinate, Y-Coordinate must be numeric.");
                flag=false;                
            }
            else
            {                
                alert("X-Coordinate must be numeric.");
                flag=false;               
            }
        }
        if(flag && xValue != null && (xValue.toString() != "NaN") && yValue!=null && (yValue.toString() == "NaN"))
        {            
            alert("Y-Coordinate must be numeric.");
            flag=false;            
        }
        
        //alert("xCoord-" + xCoord + ":yCoord-" + yCoord + ":scale-" + scale + ":projection-" + projection);
        if(flag && xCoord != null && yCoord != null && scale != null && projection != null)
        {               
            var searchByCoordObject = PageMethods;
            //alert(MapTipObject);
            if(searchByCoordObject != null)
            {             
//                var sValue=parseFloat(scale);
//                if(sValue =="NaN" || scale ==null || scale < MinimumZoomScale)
//                {
//                    scale= currentScale;
//                    flag = false;
//                }
                //ProcessSearch(double xCoord, double yCoord, double scale, int coordSysId) return bool
                 searchByCoordObject.ProcessSearch(xCoord, yCoord, scale, projection,HandleSearchByCoords);     
                 
//                 if(!flag)
//                 {
//                    alert("An invalid scale is entered, as a result the search would be executed using the maptab scale.");
//                 }                            
            }
        }
        
       
        }
        else
	    {
	        HandleSessionExpired();
	    }
        return status;
    } 
    
    function ResetMaptabScale()
    {            
        if(PageMethods !=null)
        {                        
            PageMethods.GetCurrentMapTabScale(HandleGetCurrentMapTabScale);                     
        }
    }
    
    function HandleGetCurrentMapTabScale(postData)
    {       
        var scaleElement = document.getElementById(m_scaleElementId);
        if(scaleElement != null)
        {            
            scaleElement.value = postData;
        }        
    }
    
    function HandleSearchByCoords(postData)
   {
        if(postData != null )
        {                    
            status = postData;
        }
        
        if(status)
        {
            //calls mapcontrol's RefreshMap() method
            RefreshMap();
            
            //Add a point at the specified X and Y location            
            var shape = new Shape();
            shape.type=DrawMode.POINT;
            shape.xmin = mapWidth/2;
            shape.ymin = mapHeight/2;
            AddShape(shape);
            
        } 
   
   }
   
    
    /*
        Returns element value
    */
    function getElementValue(elementId)
    {
        var elemValue = null;
        var elem = document.getElementById(elementId);
        if(elem != null)
        {
            elemValue = elem.value;
        }        
        return elemValue;
    }
    /*
        Returns option value
    */
    function getOptionValue(drpElemId)
    {        
        var optionValue = null;        
        if(drpElemId != null)
        {
            var drpElem = document.getElementById(drpElemId);
            if(drpElem != null && drpElem != 'undefined')
            {
                var selectedIndex =  drpElem.selectedIndex;
                if(drpElem != null && selectedIndex != null && selectedIndex != -1)
                {
                    var option = drpElem.options[selectedIndex];                
                    if(option != null)
                    {
                        optionValue = option.value;
                    }
                }
            }
        }
        
        return optionValue;
    }   
}
