function getBestPrice(outboundDate, returnDate, dep_air, arr_air, cellId)
{
  var url = '/capitanprice/Search';
  var pars = 'departureAirport='+dep_air+'&arrivalAirport='+arr_air+'&outboundDate=' + outboundDate+'&returnDate=' + returnDate;
  var pars_pax = '&adults=1&childs=0&infants=0';
  pars = pars + pars_pax;
 /* if (true)
  {
    pars = pars + '&returnDate=' + returnDate;
  }*/
  var myAjax = new Ajax.Request( url, { method: 'get', parameters: pars, asynchronous: true,
    onComplete: function(req)
    {
      var text;
      var responseText;
      if ((200 != req.status) || (req.responseText == "-"))
      {
        document.getElementById(cellId).title = 'dato non presente clicca per recuperarlo';
        responseText = "n.d.";
      }
      else
      {
        var pos = req.responseText.indexOf("|");
        var pos2 = req.responseText.indexOf("|", (pos + 1));
        if (pos2 > -1) 
        {
          document.getElementById(cellId).title = 'partenza ore ' + req.responseText.substring(pos+1) + ' - clicca per selezionarlo';
        }
        else
        {
          document.getElementById(cellId).title = 'partenza ore ' + req.responseText.substring(pos+1) + ' - clicca per selezionarlo';
        }                                                             
        responseText = req.responseText.substring(0, pos);
                                                             
      }
      text = '';           
                                                           
      text +=  '<a target="_top" id="a_' + cellId + '" href="/vg1/searching.do?url=search3.do&departureAirport='+dep_air;
      text += '&arrivalAirport='+arr_air;
      text += '&outboundDay=' + outboundDate.substring(6);
      text += '&outboundMonthYear=' + outboundDate.substring(4, 6) + outboundDate.substring(0, 4);
      text += '&roundtrip=true';
      text += pars_pax;
      if (true)
      {
        text += '&returnDay=' + returnDate.substring(6);
        text += '&returnMonthYear=' + returnDate.substring(4, 6) + returnDate.substring(0, 4);
      }
      text += '">';
                                                           
      text += responseText;
                                                           
      text += '</a>';
                                                           
      document.getElementById(cellId).innerHTML = text;    
      if (pos2 > -1)                                                              
      {
        document.getElementById(cellId).style.background='#AEDEA5';                                                               
        document.getElementById("a_" + cellId).style.background='#AEDEA5';                                                              
      }                                                      
      e++;
      if (e < n)
      {
        getBestPrice(outboundDates[ids[e]], returnDates[ids[e]], ids[e]);
      }
    }
  } );
}
// workaround per il best price we
function format(day)
{
  day=day.toString();
  if(day<10)
    day= '0'+day;
  return day;
}
function getBestPriceWe(cobranded,weekend, dep_air, arr_air, cellId)
{
  // semplice algoritmo per calcolare il weekend  86400000
  var nextFriday=new Date(), nextSunday=new Date(),today=new Date();
  var dayToAdd = new Array(5,4,3,2,1,0,6);
  var dayOfWeek= dayToAdd[today.getDay()],
      daysToWE= dayOfWeek % 5;
      nextFriday =new Date(today.getTime()+ (7 * weekend + daysToWE)*86400000);
      nextSunday=new Date(nextFriday.getTime() + 2*86400000);
  var outbounddate=nextFriday.getFullYear().toString()+(format(nextFriday.getMonth()+1))+format(nextFriday.getDate());
  var returndate=nextSunday.getFullYear().toString()+(format(nextSunday.getMonth()+1))+format(nextSunday.getDate());
        //getFullYear()+''+nextFriday.getMonth()+nextFriday.getDay();
   

      getBestPriceDate( outbounddate, returndate,dep_air, arr_air, cellId);
      return;
}
function getBestPriceDate(outboundDate, returnDate, dep_air, arr_air, cellId)
{
  var url = '/capitanprice/Search';
  var pars = 'departureAirport='+dep_air+'&arrivalAirport='+arr_air+'&outboundDate=' + outboundDate+'&returnDate=' + returnDate;
  var pars_pax = '&adults=1&childs=0&infants=0';
  pars = pars + pars_pax;
 /* if (true)
  {
    pars = pars + '&returnDate=' + returnDate;
  }*/
  var myAjax = new Ajax.Request( url, { method: 'get', parameters: pars, asynchronous: true,
    onComplete: function(req)
    {
      var text;
      var responseText;
      if ((200 != req.status) || (req.responseText == "-"))
      {
        document.getElementById(cellId).title = 'dato non presente clicca per recuperarlo';
        responseText = "n.d.";
      }
      else
      {
        var pos = req.responseText.indexOf("|");
        var pos2 = req.responseText.indexOf("|", (pos + 1));
        if (pos2 > -1)
        {
          document.getElementById(cellId).title = 'partenza ore ' + req.responseText.substring(pos+1) + ' - clicca per selezionarlo';
        }
        else
        {
          document.getElementById(cellId).title = 'partenza ore ' + req.responseText.substring(pos+1) + ' - clicca per selezionarlo';
        }
        responseText = req.responseText.substring(0, pos);

      }
      text = '';

      text +=  '<a target="_top" id="a_' + cellId + '" href="/vg1/searching.do?url=search3.do&departureAirport='+dep_air;
      text += '&arrivalAirport='+arr_air;
      text += '&outboundDay=' + outboundDate.substring(6);
      text += '&outboundMonthYear=' + outboundDate.substring(4, 6) + outboundDate.substring(0, 4);
      text += '&roundtrip=true';
      text += pars_pax;
      if (true)
      {
        text += '&returnDay=' + returnDate.substring(6);
        text += '&returnMonthYear=' + returnDate.substring(4, 6) + returnDate.substring(0, 4);
      }
      text += '">';

      text += responseText;

      text += '</a>';
      try { document.getElementById("dataA" + cellId).innerHTML = outboundDate.substring(6) + "/" + outboundDate.substring(4, 6); } catch(e) { /* nothing */ }
      try { document.getElementById("dataR"+cellId).innerHTML = returnDate.substring(6)+"/"+returnDate.substring(4, 6); } catch(e) { /* nothing */ }
      document.getElementById(cellId).innerHTML = text;
      if (pos2 > -1)
      {
        document.getElementById(cellId).style.background='#AEDEA5';
        document.getElementById("a_" + cellId).style.background='#AEDEA5';
      }
      e++;
      if (e < n)
      {
        getBestPrice(outboundDates[ids[e]], returnDates[ids[e]], ids[e]);
      }
    }
  } );
}

/*function getBestPriceWe(cobranded,weekend, dep_air, arr_air, cellId)
{
  var url = '/capitanprice/Search';
  var pars = 'departureAirport='+dep_air+'&arrivalAirport='+arr_air+'&outboundDate=' + outboundDate;
  var pars_pax = '&adults=1&childs=0&infants=0';
  pars = pars + pars_pax;
  
  var myAjax = new Ajax.Request( url, { method: 'get', parameters: pars, asynchronous: true,
    onSuccess: function(req)
    1;
      var text;
      var responseTextnextFriday
      var tmp;
      var reqResponseText;
      reqResponseText=req.responseText;
      var posData=reqResponseText.indexOf("|");
      var outboundDate=reqResponseText.substring(0,posData);
      reqResponseText = reqResponseText.substring(posData+1);
      posData=reqResponseText.indexOf("|");
      var returnDate=reqResponseText.substring(0,posData);
      reqResponseText = reqResponseText.substring(posData+1);
      if (reqResponseText == "-")
      {
        document.getElementById(cellId).title = 'dato non presente clicca per recuperarlo';
        responseText = "n.d.";
      }
      else
      {
        var pos = reqResponseText.indexOf("|");
        var pos2 = reqResponseText.indexOf("|", (pos + 1));
        if (pos2 > -1) 
        {
          document.getElementById(cellId).title = 'partenza ore ' + reqResponseText.substring(pos+1) + ' - clicca per selezionarlo';
        }
        else
        {
          document.getElementById(cellId).title = 'partenza ore ' + reqResponseText.substring(pos+1) + ' - clicca per selezionarlo';
        }                                                             
        responseText = reqResponseText.substring(0, pos);
                                                             
      }
      text = '';           
                                                           
      text +=  '<a target="_top" id="a_' + cellId + '" href="/vg1/searching.do?url=search3.do&departureAirport='+dep_air;
      text += '&arrivalAirport='+arr_air;
      text += '&outboundDay=' + outboundDate.substring(6);
      text += '&outboundMonthYear=' + outboundDate.substring(4, 6) + outboundDate.substring(0, 4);
      text += '&roundtrip=true';
      text += pars_pax;
      if (true)
      {
        text += '&returnDay=' + returnDate.substring(6);
        text += '&returnMonthYear=' + returnDate.substring(4, 6) + returnDate.substring(0, 4);
      }
      text += '">';
                                                           
      text += responseText;
                                                           
      text += '</a>';
                                                           
      document.getElementById(cellId).innerHTML = text;    
      document.getElementById("dataA"+cellId).innerHTML = outboundDate.substring(6)+"/"+outboundDate.substring(4, 6);
      document.getElementById("dataR"+cellId).innerHTML = returnDate.substring(6)+"/"+returnDate.substring(4, 6);
      if (pos2 > -1)                                                              
      {
        document.getElementById(cellId).style.background='#AEDEA5';                                                               
        document.getElementById("a_" + cellId).style.background='#AEDEA5';                                                              
      }                                                      
      e++;
      if (e < n)
      {
        getBestPrice(outboundDates[ids[e]], returnDates[ids[e]], ids[e]);
      }
    }
  } );
                                            
}*/
