﻿

function InitXmlHttp() {
    // Attempt to initialize xmlhttp object
    try
    {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
        // Try to use different activex object
        try
        {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (E)
        {
            xmlhttp = false;
        }
    }
    
    // If not initialized, create XMLHttpRequest object
    if (!xmlhttp && typeof XMLHttpRequest!='undefined')
      {     
            xmlhttp = new XMLHttpRequest();
      }
      // Define function call for when Request obj state has changed
      xmlhttp.onreadystatechange=XMLHttpRequestCompleted;
}

//Sends requests to ASHX
function InvokeASHX(cmd,param)
{
    InitXmlHttp();
    xmlhttp.onreadystatechange= XMLHttpRequestCompleted;
    xmlhttp.open("GET", "Handler.ashx?cmd=" + cmd + "&param=" + param, true);
    xmlhttp.send(null); 
    
    processing = true;
}

//Process responses from ASHX
function XMLHttpRequestCompleted()
{
    if (xmlhttp.readyState==4)
    {
        try
        { 
            var cmd = xmlhttp.responseText.split("|")[0];
            var attributeHTML = xmlhttp.responseText.split("|")[1];
            
            if (cmd=="getWatershed")
            {
                
                var googlePolygons = xmlhttp.responseText.split("|")[2];
                var COMID = xmlhttp.responseText.split("|")[3];
            
                _map.clearOverlays(); 
                eval(googlePolygons);    
                addWatershed(_map); 
                _map.openInfoWindow(_currentClickPoint, "<center>" + attributeHTML + ".<br>  Requesting Report and GML.  Please be patient.<br><img src='images/wait.gif' />");
                   
                InvokeASHX("getReport", COMID);
            }
            else if (cmd=="getReport")
            {
               _map.openInfoWindow(_currentClickPoint, attributeHTML); 
            }
            else if (cmd=="error")
            {
                _map.openInfoWindow(_currentClickPoint, attributeHTML);     
            }
            _map.enableDragging();
            _map.enableScrollWheelZoom();
            _map.enableDoubleClickZoom();
        
            processing = false;
            
        }
        catch (e)
        {
           
        }
    }
}

