/*	Runs at the end of Ajax calls for panning and zooming.
*/
function changeMapImage_callback(response) 
{
    // This runs at the end of the Ajax call and uses the response values to refresh the map image
	clearTimeout(myTimeOut);	// Just in case the timerZoom functions are running
	
	 //mydebug += "callback\n";
	 //mydebug += "response=" + response.value.imageURL + "\n";
	 //alert(mydebug);
	 //document.getElementById('textArea').value = mydebug;
	if (response != null && response.value != null && response.value.imageURL != null)
	{
		if (response.value.imageURL != "reserror")
		{
			var ajaxMap = response.value;
		
			// PreLoad the new image
			tmpImg = new Image();
			tmpImg.src = ajaxMap.imageURL + selectedQuality;
			//tmpImg.src = "ImageViewer.ashx?id=" + ajaxMap.imageURL + selectedQuality;
			
			/* Fake out by setting temp image to the current map
			the background of the drawingSpace to the image, then I'll hide the actual image control 
			and update it's picture and position and then make it visible again.  No hurky jerky...*/
			tempPanMapImage.style.width = actualMapImage.style.width;
			tempPanMapImage.style.height = actualMapImage.style.height;
			tempPanMapImage.style.zIndex = 51;
						
			new Rico.Effect.FadeTo( 'actualMapImage', 0, 1, 1);
			actualMapImage.src = tmpImg.src;
			setTimeout('resetImageLocation()',1200);
			
			mydebug = ajaxMap.AXL + "\n";
			// mydebug += "\n\nENV W=" + envWidth + " H=" + envHeight + "\n\n";
			// alert("ENV *=" + ( (envWidth / drawingSpaceWidth) + (envHeight / drawingSpaceHeight) ));
			//document.getElementById('textArea').value = mydebug;
			
			// Update the global vars for the envelope
			envMinX = ajaxMap.minX;
			envMaxX = ajaxMap.maxX;
			envMinY = ajaxMap.minY;
			envMaxY = ajaxMap.maxY;
			envWidth = envMaxX - envMinX;
			envHeight = envMaxY - envMinY;
			
			// Set the new scale
			SetScale(ajaxMap.scale);
			
			// If we have some html, put it in the identify div and display the div
			if (ajaxMap.html != "")
            {
                document.getElementById("identifyDivBody").innerHTML = ajaxMap.html;
                if (ajaxMap.html.indexOf("DITCH_NAME(s)") >= 0)
                {
					// This is a SHPO result
					document.getElementById("identifyDivHeaderText").innerHTML = "Identify Results for: Cultural Resources";
					showBox('identify');
                }
				if (document.getElementById("identifyDiv").style.display == "none" || document.getElementById("identifyDiv").style.display == "")
				showBox('identify');
            }
            else
            {
                document.getElementById("identifyDiv").style.display == "none";
            }
		}		    
		else
		{
		    document.getElementById("noImage").style.display = "block";
			setNormalCursor();
		}
	}
	else
	{
	    document.getElementById("noImage").style.display = "block";
		setNormalCursor();
	}
	
	glassSheetLayer.style.zIndex = 1000;
}


/*	Runs at the end of Ajax call when going to full extent.
*/
function fullExtent_callback(response) 
{
	// This runs at the end of the Ajax call and uses the response values to refresh the map image
	clearTimeout(myTimeOut);	// Just in case the timerZoom functions are running
	
	// mydebug += "callback\n";
	// mydebug += "response=" + response.value + "\n";
	// document.getElementById('textArea').value = mydebug;

	if (response != null && response.value != null && response.value.imageURL != null)
	{
		var ajaxMap = response.value;
		// PreLoad the new image
		// tmpImg = new Image();
		// tmpImg.src = ajaxMap.imageURL;
		
		/* Fake out by setting temp image to the current map
		the background of the drawingSpace to the image, then I'll hide the actual image control 
		and update it's picture and position and then make it visible again.  No hurky jerky...*/
		tempPanMapImage.src = actualMapImage.src;
		tempPanMapImage.style.width = actualMapImage.style.width;
		tempPanMapImage.style.height = actualMapImage.style.height;
		tempPanMapImage.style.left = actualMapImage.style.left;
		tempPanMapImage.style.top = actualMapImage.style.top;
		//tempPanMapImage.style.zIndex = -1;
		//tempPanMapImage.style.visibility = "visible";
		//tempPanMapImage.style.zIndex = 150;
		
		actualMapImage.src = ajaxMap.imageURL + selectedQuality;
		//actualMapImage.src = "ImageViewer.ashx?id=" + ajaxMap.imageURL + selectedQuality;
		//setTimeout('resetImageLocation()',1200);
		resetImageLocation();

		// Update the global vars for the envelope
		envMinX = ajaxMap.minX;
		envMaxX = ajaxMap.maxX;
		envMinY = ajaxMap.minY;
		envMaxY = ajaxMap.maxY;
		envWidth = envMaxX - envMinX;
		envHeight = envMaxY - envMinY;

		// mydebug = ajaxMap.AXL + "\n";
		// mydebug += "\n\nENV W=" + envWidth + " H=" + envHeight + "\n\n";
		// alert("ENV *=" + ( (envWidth / drawingSpaceWidth) + (envHeight / drawingSpaceHeight) ));
		// document.getElementById('textArea').value = mydebug;
		
		// Set the new scale
		SetScale(ajaxMap.scale);
	}
	else
	{
		document.getElementById("noImage").style.display = "block";
	}
	
	//glassSheetLayer.focus;
	glassSheetLayer.style.zIndex = 1000;
		
}


/*	Receives the response and passes parameters to the identify page for viewing.
*/
function identify_callback(response) {
	if (response != null && response.value != null && response.value.imageURL != null)
	{
	    var ajaxMap = response.value;
	    
	    // PreLoad the new image
		tmpImg = new Image();
		tmpImg.src = ajaxMap.imageURL + selectedQuality;
		//tmpImg.src = "ImageViewer.ashx?id=" + ajaxMap.imageURL + selectedQuality;
		
		/* Fake out by setting temp image to the current map
		the background of the drawingSpace to the image, then I'll hide the actual image control 
		and update it's picture and position and then make it visible again.  No hurky jerky...*/
		tempPanMapImage.src = actualMapImage.src;
		tempPanMapImage.style.width = actualMapImage.style.width;
		tempPanMapImage.style.height = actualMapImage.style.height;
		tempPanMapImage.style.left = actualMapImage.style.left;
		tempPanMapImage.style.top = actualMapImage.style.top;
		
		mydebug = ajaxMap.AXL + "\n";
		document.getElementById('textArea').value = mydebug;
		
		actualMapImage.src = tmpImg.src;
		setTimeout('resetImageLocation()',1200);
					
	    if (ajaxMap.html != "")
        {
            document.getElementById("identifyDivBody").innerHTML = ajaxMap.html;
        }
        else
        {
            document.getElementById("identifyDivBody").innerHTML = "<p>No data for the point you clicked on the map. Please try again.</p>";
        }
    	
	    if (document.getElementById("identifyDiv").style.display == "none" || document.getElementById("identifyDiv").style.display == "")
	        showBox('identify');
	}
	else
	{
	    document.getElementById("identifyDivBody").innerHTML = "<p>No data for the point you clicked on the map. Please try again.</p>";
	    if (document.getElementById("identifyDiv").style.display == "none" || document.getElementById("identifyDiv").style.display == "")
	        showBox('identify');
	}
	
	setNormalCursor();
}


/*  Sets the checkboxes for the visible layers
*/
function setLayerChkBoxes_callback(response)
{
    if (response.value != null && response.value.substring(0,5) != "ERROR")
    {
        var list = response.value;
    
    var layerArray = new Array();
    layerArray = list.split('|');
    
    for (var i=0;i<layerArray.length;i++)
    {
        switch (layerArray[i])
        {
            case "topo250k":
			case "topo100k":
			case "topo24k":
				document.getElementById('topo').checked = true;
				break;
			case "highways":
			case "tgrroad":
			    document.getElementById('roads').checked = true;
			    break;
			case "stream":
			    document.getElementById('stream').checked = true;
			    document.getElementById('streamlabels').checked = true;
			    document.getElementById('streamlabels').disabled = false;
			    break;
			default:
			    var control = document.getElementById(layerArray[i]);
			    if (control != null)
			        control.checked = true;
			    break;
		}
    }
    }
    else
    {
        document.getElementById('textArea').value = response.value;
    }
}


/*  Takes the list of named features returned and puts them into the query box
*/
function GNISQuery_callback(response)
{
    if (response.value != null)
        document.getElementById('queryDivBody').innerHTML = response.value;
    showBox('query');
    
    setNormalCursor();
}

/*  Takes the list of named features returned and puts them into the query box
*/
function getParcelOwnerList_callback(response)
{
    //if (response.value != null)
    //    document.getElementById('queryDivBody').innerHTML = response.value;
    //showBox('query');
    
    if (response.value != null)
    {
        //document.getElementById("identifyDivContent").innerHTML = "";
        //document.getElementById("identifyDivBackground").style.display = "block";
	    //document.getElementById("identifyDivData").style.display = "block";
	    //document.getElementById("identifyDivContent").innerHTML = response.value;
	    	    
	    document.getElementById("identifyDivBody").innerHTML = response.value;
	    showBox('identify');
	}
    setNormalCursor();
}

/*  Puts the returned owner list into the identify div
*/
function findWROwner_callback(response)
{
    if (response.value != null)
    {
        document.getElementById("identifyDivHeaderText").innerHTML = "click on a water right below to zoom";
        document.getElementById("identifyDivBody").innerHTML = response.value;
	    showBox('identify');
	}
    setNormalCursor();
}


/*  Fills the quadList select box with the list of quads starting with the letter they
    selected from the alphabet list
*/
function FillQuadList_callback(response)
{
    if (response.value != null && response.value != "")
    {
        var list = response.value;
        
        // Get the select box
        var quadList = document.getElementById("quadList");
        
        // Clear any previous options out
        quadList.options.length = 0;
        
        quadList.options.add(new Option("- select quad -", ""));
        
        // Fill the box with new options
        for (var i=0;i<list.length;i++)
        {
            var value = list[i].substring(0,list[i].indexOf(','));
            var text = list[i].substring(list[i].indexOf(',') + 1);
            quadList.options.add(new Option(text, value));
        }
    }
    
    setNormalCursor();
}


/* Called by changeMapLayers and filterIdentifyList
*/
function filterIdentifyList_callback(response)
{
    if (response != null && response.value != null)
    {
        var ds = response.value;
        
        if (ds != null)
        {
            if ((ds.Tables != null) && (ds.Tables[0].Rows.length > 0))
            {
                // Set the select list options length to zero, effectively clearing out old items
	            document.getElementById("selectIdentify").options.length = 0;
        		
    		    // Put the dataset into a javascript array so it can be sorted.
    		    var layerArray = new Array();
	            for(i=0; i<ds.Tables[0].Rows.length; i++)
	            {
	                layerArray[i] = ds.Tables[0].Rows[i]["layername"] + "|" + ds.Tables[0].Rows[i]["layerid"];
	            }
	            layerArray.sort();
    	        
	            var i = 0;
	            // Now loop through array and populate select control
	            for(i=0; i<layerArray.length; i++)
	            {
	                var layer = layerArray[i];
		            document.getElementById("selectIdentify").options.add(new Option(layerArray[i].substring(0,layerArray[i].indexOf("|",0)), layerArray[i].substring(layerArray[i].indexOf("|",0) + 1)));
	            }
    	        
	            // The identify button may have previously been hid
	            //document.getElementById("identifyLI").style.display = "block";
            }
            else
            {
                // No layers so hide everything
                document.getElementById("selectIdentify").options.length = 0;
                document.getElementById("identifyList").style.display = "none";
                
                // The Identify tool may have been previously activated. We need to inactivate it.
                // Passing null to function selectTool inactivates any previously active tool.
                //selectTool(null);
            }
        }
        else
        {
            // No layers so hide everything
            document.getElementById("selectIdentify").options.length = 0;
            document.getElementById("identifyList").style.display = "none";
            //document.getElementById("identifyLI").style.display = "none";
            
            // The Identify tool may have been previously activated. We need to inactivate it.
            // Passing null to function selectTool inactivates any previously active tool.
            //selectTool(null);
        }
    }
}


/* Populate the city drop down list with cities
*/
function getCityList_callback(response) 
{
    var ds = response.value;
    
    if (ds != null)
    {
        if ((ds.Tables != null) && (ds.Tables[0].Rows.length > 0))
        {
            // Set the select list options length to zero, effectively clearing out old items
	        document.getElementById("citySelect").options.length = 0;
    		document.getElementById("citySelect").options.add(new Option(" -- Select City --", ""));
    		
    		for(i=0; i<ds.Tables[0].Rows.length; i++)
	        {
	            // Populate drop down list with common_nam and elcode
	            document.getElementById("citySelect").options.add(new Option(ds.Tables[0].Rows[i]["town"], ds.Tables[0].Rows[i]["town"]));
	        }
	    }
    }
    
    // Back to normal.
    setNormalCursor();
}

/* Populate the county drop down list with counties
*/
function getCountyList_callback(response) 
{
    var ds = response.value;
    
    if (ds != null)
    {
        if ((ds.Tables != null) && (ds.Tables[0].Rows.length > 0))
        {
            // Set the select list options length to zero, effectively clearing out old items
	        document.getElementById("countySelect").options.length = 0;
    		document.getElementById("countySelect").options.add(new Option(" -- Select County --", ""));
    		
    		for(i=0; i<ds.Tables[0].Rows.length; i++)
	        {
	            // Populate drop down list
	            document.getElementById("countySelect").options.add(new Option(ds.Tables[0].Rows[i]["countyname"], ds.Tables[0].Rows[i]["countycd"]));
	        }
	    }
    }
    
    // Back to normal.
    setNormalCursor();
}

/* Based on the security level the user has we show them additional parts of the application.
*/
function LogIn_callback(response)
{
    if(response.value != null)
    {
        var user = response.value;
        
        if(user.sLevel != "failed")
        {
            switch (user.sLevel)
            {
                case "PowerUser":
                    document.getElementById("ccaa").style.display = "";
                    document.getElementById("fwp").style.display = "";
                    document.getElementById("nrcs").style.display = "";
                    //document.getElementById("culturalResources").style.display = "";
                    break;
                case "GeneralUser":
                    document.getElementById("ccaa").style.display = "";
                    document.getElementById("fwp").style.display = "";
                    document.getElementById("nrcs").style.display = "";
                    //document.getElementById("culturalResources").style.display = "";
                    break;
                case "RestrictedUser":
                    break;
                default:
                    break;
            }
            
            document.getElementById("email").value = "";
            document.getElementById("password").value = "";
            document.getElementById("login").style.display = "none";
            document.getElementById("affiliates").style.display = "none";
            document.getElementById("failed").style.display = "none";
            
            var welcome = "Welcome, " + user.firstName + " " + user.lastName;
            document.getElementById("welcome").innerHTML = welcome;
        }
        else
        {
            document.getElementById("failed").style.display = "block";
        }
    }
    else
    {
        document.getElementById("failed").style.display = "block";
    }
}


/*	Called by quickChangeMapImage and changeMapImage
*/
function resetImageLocation()
{
	// I put these here so the img.src has time to load before I move the image 
	// so we don't get the flicker in the image
	actualMapImage.style.height = "100%";
	actualMapImage.style.width = "100%";
	actualMapImage.style.left = "0px";
	actualMapImage.style.top = "0px";
	new Rico.Effect.FadeTo( 'tempPanMapImage', 0, 800, 10);
	new Rico.Effect.FadeTo( 'actualMapImage', 1.0, 800, 10, {complete:function() {resetTempImage();}});
	
}
	
function resetTempImage()
{		
	tempPanMapImage.style.left = "0px";
	tempPanMapImage.style.top = "0px";
	tempPanMapImage.style.height = "100%";
	tempPanMapImage.style.width = "100%";
	tempPanMapImage.src = actualMapImage.src;
	new Rico.Effect.FadeTo( 'tempPanMapImage', 1.0, 1, 1);
		
	// And set focus on the glass sheet layer
	//glassSheetLayer.focus;
	glassSheetLayer.style.zIndex = 1000;

	setNormalCursor();
}
