﻿function CreateFilter(pType,pStart)
    {
        var stringResult=pStart;

        if (pType=='name')
        {
            //            if (dijit.byId('chkEq').checked || dijit.byId('chkTc').checked || dijit.byId('chkFl').checked || dijit.byId('chkVo').checked || dijit.byId('chkVw').checked|| dijit.byId('chkTs').checked)
            if (dijit.byId('chkEq').checked || dijit.byId('chkTc').checked || dijit.byId('chkFl').checked || dijit.byId('chkVo').checked || dijit.byId('chkTs').checked)
                    stringResult +='|eventtypes';
            //add other option        
        }
        else if (pType=='value')
        {
            var stringValue='|';
            if (dijit.byId('chkEq').checked) stringValue+=dijit.byId('chkEq').attr('value');
            if (dijit.byId('chkTc').checked) if (stringValue != '|') stringValue += ',' + dijit.byId('chkTc').attr('value'); else stringValue += dijit.byId('chkTc').attr('value');
            if (dijit.byId('chkTs').checked) if (stringValue != '|') stringValue += ',' + dijit.byId('chkTs').attr('value'); else stringValue += dijit.byId('chkTs').attr('value');
            if (dijit.byId('chkFl').checked) if (stringValue != '|') stringValue += ',' + dijit.byId('chkFl').attr('value'); else stringValue += dijit.byId('chkFl').attr('value');
            if (dijit.byId('chkVo').checked) if (stringValue != '|') stringValue += ',' + dijit.byId('chkVo').attr('value'); else stringValue += dijit.byId('chkVo').attr('value');
//            if (dijit.byId('chkVw').checked) if (stringValue != '|') stringValue += ',' + dijit.byId('chkVw').attr('value'); else stringValue += dijit.byId('chkVw').attr('value');
            // Windstorm section hides temporarily                                    
//            if (dijit.byId('checkVW').checked) if (stringValue!='|') stringValue+=','+ dijit.byId('checkVW').attr('value'); else stringValue+=dijit.byId('checkVW').attr('value');            
            if (stringValue!='|') stringResult += stringValue;
            stringValue='|';
            try {
                if (dijit.byId('groupbyEvent').checked) 
                    stringValue+=dijit.byId('groupbyEvent').attr('value');            
                else if (dijit.byId('groupbyAlertLevel').checked) 
                    stringValue+=dijit.byId('groupbyAlertLevel').attr('value');
                if (stringValue!='|') stringResult += stringValue;
            } catch (x) {
                //filter is defined in pane that is loaded later
            }
        }
        return stringResult;
    }

function DestroyDojoToc()
{
    dijit.registry.forEach(function(w) {
        if (w.id.substring(0, 10) == "toc_gdacs_") w.destroy();
    });
              
}

function ApplyFilter()
{
    DestroyDojoToc();
    LoadElements();
}

function AddMonth(pDateTime,pOffset)
{
    var dd = new Date(pDateTime).getDay();
    var mm = new Date(pDateTime).getMonth();
    var yy = new Date(pDateTime).getFullYear();
    mm = mm+pOffset;
    return new Date(yy,mm,dd);
}


function UpdateTimeline()
{
    eventSource.clear();
    pathTimeline = initialPath + "transform.aspx?xmlurl=" + xmlurl + "&xslurl=" + initialPath + "gdacs2simile.xslt&ContentType=xml&pname="+CreateFilter('name','')+"&pvalue="+CreateFilter('value','');
    Timeline.loadXML(pathTimeline, function(xml, url) { eventSource.loadXML(xml, url); });
    
}

function RefreshTimeLine()
{
    if (tl!=null)
        tl.layout();
}

function getUrlParam(ParamName) 
{
    qString=location.toString(); 
    var indSta=qString.indexOf(ParamName); 
    if (indSta==-1 || ParamName=="") 
        return null; 
    var indEnd=qString.indexOf('&amp;',indSta); 
    if (indEnd==-1) 
        indEnd=qString.length; 
    var valore = (qString.substring(indSta+ParamName.length+1,indEnd)); 
  
    return valore + ''; 
}


function FilterQuery()
{
    DestroyDojoToc();
    BuildNewUrl();
    LoadElements();
    RefreshJrcMap();

}

function BuildNewUrl() 
{
    var strEventtype = "";
    xmlurl = initialPath + "rss.aspx%3Fprofile%3DARCHIVE";
    xmlurl += "%26from%3D" + dijit.byId("fromDateAll").attr('displayedValue');
    xmlurl += "%26to%3D" + dijit.byId("toDateAll").attr('displayedValue');
    xmlurl += "%26alertlevel%3D" + dojo.trim(dijit.byId("alertlevelAll").value);
    if (!isNaN(dijit.byId("populationAll").value)) xmlurl += "%26population%3D" + dijit.byId("populationAll").value;
    if (!isNaN(dijit.byId("severityAll").value)) xmlurl += "%26severity%3D" + dijit.byId("severityAll").value;
    if (dijit.byId('chkEq').checked) strEventtype += dijit.byId('chkEq').attr('value');
    if (dijit.byId('chkTc').checked) if (strEventtype != '') strEventtype += ',' + dijit.byId('chkTc').attr('value'); else strEventtype += dijit.byId('chkTc').attr('value');
    if (dijit.byId('chkTs').checked) if (strEventtype != '') strEventtype += ',' + dijit.byId('chkTs').attr('value'); else strEventtype += dijit.byId('chkTs').attr('value');
    if (dijit.byId('chkFl').checked) if (strEventtype != '') strEventtype += ',' + dijit.byId('chkFl').attr('value'); else strEventtype += dijit.byId('chkFl').attr('value');
    if (dijit.byId('chkVo').checked) if (strEventtype != '') strEventtype += ',' + dijit.byId('chkVo').attr('value'); else strEventtype += dijit.byId('chkVo').attr('value');
    xmlurl += "%26eventtype%3D" + strEventtype;
    if (strEventtype == ""
            && dijit.byId("fromDateAll").attr('displayedValue') == ""
            && dijit.byId("toDateAll").attr('displayedValue') == ""
            && dojo.trim(dijit.byId("alertlevelAll").value.toString()) == ""
            && dojo.trim(dijit.byId("populationAll").value.toString()) == "0"
            && dojo.trim(dijit.byId("severityAll").value.toString()) == "0")
        xmlurl = initialPath + "rss.aspx%3Fprofile%3DGDACS";
    xmlurlmap = decodeURLformap(xmlurl);
}

function decodeURLformap(url) 
{
    var result = url;
    while (result.indexOf('%26') >= 0) {
        result = result.replace("%26", "&");
    }
    while (result.indexOf('%3D') >= 0) {
        result = result.replace("%3D", "=");
    }
    while (result.indexOf('%3F') >= 0) {
        result = result.replace("%3F", "?");
    }
    return result;
}


    


function gdacsZoomMap(x,y,strEvent,strId) {

        if (dijit.byId('TabMaps').attr('selected')) // Tab Maps
        {
            //alert('clicked, zooming to '+x+' '+y+' --> to do');
            //get map element in iframe; see http://www.dyn-web.com/tutorials/iframes/
            var m;
            //document.getElementById("mapiframe").contentWindow.map should work for all????
            var d=document.getElementById("mapiframe");
            if (d.map) {
                m = d.map;
            } else {
                var d=(d.contentWindow || d.contentDocument);
                //if (y.document) y=y.document;
                if (d.map) {
                    m = d.map
                } else {
                    alert('your browser does not support this map');
                }
            }
            //alert(m.getCenter());
            var koeff = 4;
            var bounds = new d.OpenLayers.Bounds(x-koeff,y-koeff,x+koeff,y+koeff).transform(document.getElementById("mapiframe").contentWindow.map.displayProjection,  
            document.getElementById("mapiframe").contentWindow.map.projection);

            document.getElementById("mapiframe").contentWindow.map.zoomToExtent(bounds);
            
//            var p = new d.OpenLayers.LonLat.fromString(x+','+y);
//            m.setCenter(p,8);
        }
        else if (dijit.byId('TabDetails').attr('selected'))// Tab Details
        {

            var strUrl="";
            strUrl=initialPath + "transform.aspx?xmlurl=" + xmlurl + "&xslurl=" + initialPath + "gdacs_detail_dojo.xslt&pname=guid&pvalue="+strId;

//                
            if (strUrl!="")
            {
                dojo.byId("details_details").innerHHTML="<asp:Literal ID='Literal004' runat='server' Text='<%$ Resources:MasterContent, ListArchive%>' />";
                dijit.registry.forEach(function(w){
                                  if (w.id.substring(0,15)=="detail_resource")
                                    w.destroy();             
                          });
                


                dojo.xhrGet({
                    url: strUrl,
                    load: function(response, ioArgs){
                      dojo.byId("details_details").innerHTML = response;
                      dojo.parser.parse("details_details");
//                      dijit.registry.forEach(function(w){
//                                  if (w.id.substring(0,15)=="detail_resource")
//                                    //w.destroy();
//                                    dojo.parser.parse(w.id); 
//                          });
//                      return response;
                    },
                    error: function(response, ioArgs){
                      dojo.byId("details_details").innerHTML = "An error occurred, with response: " + response;
                        alert('error');
                      return response;
                    },
                    handleAs: "text"
                });
            }
            else
               strUrl="";
        
        }
}


function gdacsDojoActiveTab(strTab) 
    {
        var tabContainer = dijit.byId(strTab);
        var tp = dijit.byId('TabPane');
        tp.selectChild(tabContainer);
        tp.startup();
    }

function gdacsDojoGetActiveTab() 
    {
        var result="";
        if (dijit.byId('TabMaps').attr('selected')==true)
            {
                result='TabMaps';
            }
        else if (dijit.byId('TabDetails').attr('selected')==true)   
            {
                result='TabDetails';
            }
        else if (dijit.byId('TabTimeLine').attr('selected')==true)   
            {
                result='TabTimeLine';
            }
        else if (dijit.byId('TabArchive').attr('selected')==true)   
            {
                result='TabArchive';
            }
        return result;    
    }
    
function gdacsDojoRestorePrevTab(strTab) 
    {
        var tabContainer = dijit.byId(strTab);
        var tp = dijit.byId('TabPane');
        tp.selectChild(tabContainer);
    }


function gdacsShowDetail(strModel,strId) 
    {
        
        if (TabShow=='M') // Tab Maps
        {
            alert('Zoom - ' + strModel + ' , ' + strId);
        }
        else if (TabShow=='D')// Tab Details
        {
            var strUrl="";
            if (strModel=="EQ")
                strUrl=initialPath + "transform.aspx?xmlurl=" + initialPath + "rsseq.aspx%3Fprofile%3DGDACS%26id%3D" + strId + "&xslurl=" + initialPath + "gdacs_full.xslt";
            else if (strModel=="TC")
                strUrl=initialPath + "transform.aspx?xmlurl=" + initialPath + "rsstc.aspx%3Fprofile%3DGDACS%26id%3D" + strId + "&xslurl=" + initialPath + "gdacs_full.xslt";
            else if (strModel=="FL")
                strUrl=initialPath + "transform.aspx?xmlurl=" + initialPath + "rssfl.aspx%3Fprofile%3DGDACS%26id%3D" + strId + "&xslurl=" + initialPath + "gdacs_full.xslt";
                
            if (strUrl!="")
            {
                alert(strUrl);
                dojo.byId("contentDetails").innerHTML = "...Loading...";
                dojo.xhrGet({
                    url: strUrl,
                    load: function(response, ioArgs){
                      dojo.byId("contentDetails").innerHTML = response;
                      return response;
                    },
                    error: function(response, ioArgs){
                      dojo.byId("contentDetails").innerHTML = "An error occurred, with response: " + response;
                      return response;
                    },
                    handleAs: "text"
                });
            }
            else
               return;


        }
        
    }          

