﻿// JScript File


function DoAjaxCacheQuery(pointsArray,ordenPunto,acumulativeDistance,innitialDistance,distance,instruction,idRuta)
{
    try
    {
  //Build the url to call the server
      var url = "cacheRuta.aspx?";
      url += "points=" + pointsArray + "&ordenPunto=" + ordenPunto + "&acumulativeDistance=" + acumulativeDistance + "&innitialDistance=" + innitialDistance + "&distance=" + distance + "&instruction=" + instruction + "&idRuta=" + idRuta;
      
                                                                                                                                                                                                        


      //Start by getting the appropriate XMLHTTP object for the browser
      var 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
          {
         
           
          }
       
        }
        xmlhttp.send(null);
      }
 }
catch (err)
    {
          txt="There was an error on this page.\n\n";
          txt="Function: DoAjaxQuery().\n\n";
         txt+="Error description: " + err.description + "\n\n";
         txt+="Click OK to continue.\n\n";
         alert(txt);
    }
  
}



function DoAjaxQuery(pointsArray,date,time,ordenPunto,acumulativeDistance,innitialDistance,distance,instruction,idRuta)
{
    try
    {
  //Build the url to call the server
      var url = "weatherThreads.aspx?";
      url += "points=" + pointsArray + "&date=" + date + "&time=" +time + "&ordenPunto=" + ordenPunto + "&acumulativeDistance=" + acumulativeDistance + "&innitialDistance=" + innitialDistance + "&distance=" + distance + "&instruction=" + instruction + "&idRuta=" + idRuta;
      
                                                                                                                                                                                                        


      //Start by getting the appropriate XMLHTTP object for the browser
      var 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
          {
         
            try
            {
          
                var result = xmlhttp.responseText;
                var locationIndex = result.indexOf("EndPlaceDescription");
                var index = result.indexOf("EnD");
                var location= result.substring(index+3,locationIndex);
                var iconIndex= result.indexOf("EndIcon");
                var icon=result.substring(locationIndex+19,iconIndex);
                var idIconIndex=result.indexOf("EndIdDiv");
                var idIcon=result.substring(iconIndex+7,idIconIndex);
                var error=result.indexOf("Error");
                var doNothing=result.indexOf("DoNothing");
                location=location.trim();
                //confirm(result);
                result=result.substring(0,index);
                
                
                if (error==-1 && doNothing==-1)
                {
                    pinsToDisplay=pinsToDisplay-1;
                    var encontrado=false;
                    for (var i=0; i<pushPinLocations.length;i++)
                    {
                      if (pushPinLocations[i]==location)
                      {
                        encontrado=true;
                        //alert('encontrado');
                        break;
                      }
                    }
                    

                    if (!encontrado)
                    {
                        pushPinLocations.push(location);
                        
                        eval(result);
                     
                        insertWeatherIcon(idIcon,icon);
                        
                        
                
                    }
                    else
                    
                    {
                        cantPushpins++;
                    }
                    
                  //  setmapView();
                 
                    if (pinsToDisplay<0)
                        {pinsToDisplay=0;}
                    if ((pinsToDisplay>0) && (consultingWeather==true))
                    {
                       ShowLoading('printing weather icons on map');
                    }
                    else
                    
                        if (pinsToDisplay<=0)
                        { 
                        
                            
                              changeAutoSize();
                              if (document.getElementById('noRoute')!=null)
                            {
                                var alert3=document.getElementById('noRoute');
                                alert3.parentNode.removeChild(alert3);
                              }
                              if (document.getElementById('lblPlayAround')!=null)
                              {
                                var alert= document.getElementById('lblPlayAround');
                                alert.parentNode.removeChild(alert);
                                var alert2=document.getElementById('optionalData');
                                alert2.style.visibility="visible";
                                alert2.style.height="125px";
                              }
                            if (document.getElementById('divExportWarning')!=null)
                              {
                                var alert= document.getElementById('divExportWarning');
                                alert.parentNode.removeChild(alert);
                                var alert2=document.getElementById('divExportElements');
                                alert2.style.visibility="visible";
                                alert2.style.height="150px";
                              }
                              
                              if (document.getElementById('divShareWarning')!=null)
                              {
                                var alert=document.getElementById('divShareWarning');
                                alert.parentNode.removeChild(alert);
                                var alert2=document.getElementById('divShareElements');
                                alert2.style.visibility="visible";
                                alert2.style.height="275px"
                              }
                            HideLoading();
                            changeSelectedIndex(1);
                            

                           // printTable(ruta);
                        }
                        else if (consultingWeather==false && pinsToDisplay>2)
                        {
    //                        pasoMillas=pasoMillas-50;
    //                        if (pasoMillas>=0)
    //                           { searchWeather(routePoints);
    //                           }
                        }
               }
               else
               {
                //devolvio error
                if (error>=0)
                {
                     pinsToDisplay=pinsToDisplay-1;
                     
                    if (pinsToDisplay<=0)
                    {
                        HideLoading();
                    }
                }
               }
              
               
            }
            catch(err)
            {
//                txt="There was an error on this page.\n\n";
//                txt="Function: DoAjaxQuery().\n\n";
//                 txt+="Error description: " + err.description + "\n\n";
//                 txt+="Click OK to continue.\n\n";
//                 alert(txt);
            }
          }
       
        }
        xmlhttp.send(null);
      }
 }
catch (err)
    {
          txt="There was an error on this page.\n\n";
          txt="Function: DoAjaxQuery().\n\n";
         txt+="Error description: " + err.description + "\n\n";
         txt+="Click OK to continue.\n\n";
         alert(txt);
    }
  
}

