﻿var xmlhttp;

function GetXmlHttp()
{
    var x = null;
    try
    {
        x = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
        try
        {
            x = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (e)
        {
            x = null;
        }            
    }
    
    if (!x && typeof XMLHttpRequest != "undefined")
    {
        x = new XMLHttpRequest();            
    }
    
    return x;
}

function DoAjaxQuery()
{
    //Update the message area to give the user a status.
    document.getElementById('QueryResults').innerHTML = "Beginning query...";
    
    //Built the url to call the server
    var url = "getInfoFromServer.aspx?";
    url += "&Y=" + map.GetCenterLatitude();
    url += "&X=" + map.GetCenterLongitude();
    
    //Start by getting the appropriate XMLHTTP object for the browser
    xmlhttp = GetXmlHttp();
    
    //If we have a valid xmlhttp object
    if (xmlhttp)
    {
        
        xmlhttp.open("GET", url, true);  // varAsync = true;
        
        //Set the callback.  This function is called when we 
        xmlhttp.onreadystatechange = function()
        {
            if (xmlhttp.readyState == 4)  //4 is a success
            {
                //Server code creates javascript "on the fly" for us to
                //execute using eval()
                var result = xmlhttp.responseText;
                eval(result);
            }
        }
        
        xmlhttp.send(null);
    }
}
    function loadTrack(stormNameCHK)
    {
        map.clearOverlays();
        loadSwellWindow();
        
        if(stormNameCHK.checked==true)
        {
            var stormName = stormNameCHK.name;
            
            //Built the url to call the server
            var url = "getStormTrack.aspx?stormName=" + stormName;
            
            //Start by getting the appropriate XMLHTTP object for the browser
            xmlhttp = GetXmlHttp();
            
            //If we have a valid xmlhttp object
            if (xmlhttp)
            {
                xmlhttp.open("GET", url, true);  // varAsync = true;
                
                //Set the callback.  This function is called when we 
                xmlhttp.onreadystatechange = function()
                {
                    if (xmlhttp.readyState == 4)  //4 is a success
                    {
                        //Server code creates javascript "on the fly" for us to
                        //execute using eval()
                        var result = xmlhttp.responseText;
                        eval(result);
                    }
                }
                
                xmlhttp.send(null);
            }
        }
    }
function loadInitialStormList()
{
    var curdate = new Date();
    var year = curdate.getFullYear();
    getStorms(year);
}

function loadStormList(stormYear)
{
    //getStorms(selectObj.options[selectObj.selectedIndex].value);
    getStorms(stormYear);
}

function insertStormListHTML()
{
    if (xmlhttp.readyState == 4)  //4 is a success
    {
        var result = xmlhttp.responseText;
        var el = document.getElementById('stormsByYear');
        el.innerHTML = result;
    }
}
function getStorms(stormYear)
{
    //Built the url to call the server
    var url = "stormList.aspx?stormYear=" + stormYear;
    
    //Start by getting the appropriate XMLHTTP object for the browser
    xmlhttp = GetXmlHttp();
    
    //If we have a valid xmlhttp object
    if (xmlhttp)
    {
        xmlhttp.onreadystatechange = insertStormListHTML;
        try {
              xmlhttp.open("GET", url, true);  // varAsync = true; 
            } 
        catch (err) 
            {
              alert("XMLHttpRequest.open() failed.\n"+err);
              return;
            }
        try {
              xmlhttp.send(null);
            } 
        catch (err) 
        {
              alert("XMLHttpRequest.send() failed.\n"+err);
              return;        
        }
    }    
}