//encoding: utf-8

var cal = new CalendarPopup();
document.write(getCalendarStyles());


function fromDatePicker(inpObj,toInpObj,imgName)
{
  var chk=false;
  var cal17 = new CalendarPopup("testdiv2");
  var now = GdToday;
  now.setDate(now.getDate()-1);
  cal17.addDisabledDates(null,formatDate(now,"yyyy-MM-dd"));
  if (RemoveFun(document.forms['IEXBookingEngine'].elements['Origin'].value,' ') == "")
  {
    alert(GaText[2]);
    chk=true;
    return false;
  }
  else if (RemoveFun(document.forms['IEXBookingEngine'].elements['Destination'].value,' ') == "")
  {
    alert(GaText[3]);
    chk=true;
    return false;
  }

  if (!chk)
  {
    DisableDates(cal17,document.forms['IEXBookingEngine'].elements['Origin'].value,document.forms['IEXBookingEngine'].elements['Destination'].value);
    now.setDate(now.getDate()-1);

    cal17.addDisabledDates(null,formatDate(now,"yyyy-MM-dd"));
    var first = getLimits(document.forms['IEXBookingEngine'].elements['Origin'].value,document.forms['IEXBookingEngine'].elements['Destination'].value,'first');
    var tp_first = first.split('/');
    var last = getLimits(document.forms['IEXBookingEngine'].elements['Origin'].value,document.forms['IEXBookingEngine'].elements['Destination'].value,'last');
    var from = document.getElementById('date1');
    var to = document.getElementById('date2');
    cal17.select(inpObj,imgName,'dd/MM/yyyy','01/'+tp_first[1]+'/'+tp_first[2],last,from,to,tp_first[0]+'/'+tp_first[1]+'/'+tp_first[2]);
  }
  return false;

}


function changeOutBound()
{
  try
  {
    var fromObj = document.getElementById('date1');
    var toObj = document.getElementById('date2');

    var fromDate = new Date();
    if (fromObj.value.length > 0)
    {
      fromDate=formatDates(fromObj.value);
    }
    var toDate = new Date();
    if (toObj.value.length > 0)
    {
      toDate=formatDates(toObj.value);
    }
    if (DateDiffarence(fromDate.getDate()+'/'+Math.round(fromDate.getMonth()+1)+'/'+fromDate.getFullYear(),'00:00',toDate.getDate()+'/'+Math.round(toDate.getMonth()+1)+'/'+toDate.getFullYear(),'00:00') >=0)
    {
    }
    else
    {
      if (toObj.value.length != 0)
        toObj.value = fromObj.value;
    }

  }
  catch(e)
  {}
}


function getFlightMethod(optName)
{
  var rValue=""
  for (i=0;i<document.getElementsByName(optName).length;i++)
  {
    if (document.getElementsByName(optName).item(i).checked)
    {
      rValue = document.getElementsByName(optName).item(i).id;
    }
  }
  return rValue.toLowerCase();
}


function toDatePicker(frmInpObj,toInpObj,imgName)
{

  var chk=false;
  var now = GdToday;
  now.setDate(now.getDate()-1);
  var cal17 = new CalendarPopup("testdiv2");
  if (RemoveFun(document.forms['IEXBookingEngine'].elements['Origin'].value,' ') == "")
  {
    alert(GaText[2]);
    chk=true;
    return false;
  }
  else if (RemoveFun(document.forms['IEXBookingEngine'].elements['Destination'].value,' ') == "")
  {
    alert(GaText[3]);
    chk=true;
    return false;
  }

  if ((frmInpObj.value).length == 0)
  {
    alert(GaText[39]);
    chk=true;
    return false;
  }
  if (!chk)
  {
    DisableDates(cal17,document.forms['IEXBookingEngine'].elements['Destination'].value,document.forms['IEXBookingEngine'].elements['Origin'].value);
    var fromDate = new Date();
    fromDate=formatDates(frmInpObj.value);
    fromDate.setDate(fromDate.getDate()-1);
    cal17.addDisabledDates(null,formatDate(fromDate,"yyyy-MM-dd"));
    var first = getLimits(document.forms['IEXBookingEngine'].elements['Destination'].value,document.forms['IEXBookingEngine'].elements['Origin'].value,'first');
    var tp_first = first.split('/');
    var last = getLimits(document.forms['IEXBookingEngine'].elements['Destination'].value,document.forms['IEXBookingEngine'].elements['Origin'].value,'last');
    cal17.addDisabledDates(null,formatDate(fromDate,"yyyy-MM-dd"));
    var from = document.getElementById('date2');
    var to = document.getElementById('date1');
    if (first < (Math.round(fromDate.getDate()+1)+'/'+Math.round(fromDate.getMonth()+1)+'/'+fromDate.getFullYear())) {
      first = Math.round(fromDate.getDate()+1)+'/'+Math.round(fromDate.getMonth()+1)+'/'+fromDate.getFullYear()
    }
    cal17.select(toInpObj,imgName,'dd/MM/yyyy','01/'+tp_first[1]+'/'+tp_first[2],last,from,to,first);
  }
}


function formatDates(dateStr)
{
  if (RemoveFun(dateStr,' ')!="")
  {
    var tp =dateStr.split(' ');
    var date = RemoveFun(tp[1],'.');
    var month = 1;
    for (i=0;i<GaMonths.length;i++)
    {
      if (GaMonths[i].toLowerCase() == tp[2].toLowerCase())
      {
        month = i+1;
      }
    }
    var year = tp[3];
    return fncNewDate(date,month,year)
  }
}


function getLimits(strOrgin,strDestination,options)
{

  options = options.toLowerCase();
  var str = strOrgin+" | "+strDestination;
  var avalable ="";
  for (i=0;i<GaRoutes.length;i++)
  {
    if (GaRoutes[i].indexOf(str)!=-1)
    {
      avalable = GaRoutes[i];
    }
  }
  avalable = RemoveFun(avalable,str+' | ');
  var temp = avalable.split('|');

  // sort the date
  for (i=0;i<temp.length;i++)
  {
    for (j=0;j<temp.length-1;j++)
    {
      var GdToday1 = new Date();
      var temp1 = temp[i].split('/');
      if (fncIsDate(Math.round(temp1[0]),Math.round(temp1[1]),Math.round(temp1[2])))
      {
        GdToday1 = fncNewDate(Math.round(temp1[0]),Math.round(temp1[1]),Math.round(temp1[2]));
      }
      var GdToday2 = new Date();

      var temp2 = temp[j].split('/');
      if (fncIsDate(Math.round(temp2[0]),Math.round(temp2[1]),Math.round(temp2[2])))
      {
        GdToday2 = fncNewDate(Math.round(temp2[0]),Math.round(temp2[1]),Math.round(temp2[2]));
      }
      if (GdToday1.getTime() < GdToday2.getTime())
      {
        var tp = temp[i];
        temp[i] = temp[j];
        temp[j] = tp;
      }
    }
  }
  if (options=='first')
  {
    return temp[0];
  }
  else
  {
    return temp[temp.length-1];
  }

}


function DisableDates(cal17,strOrgin,strDestination)
{
  //GaRoutes[0] = "KEF | CPH | 21/02/2006 | 15/09/2006";
  var str = strOrgin+" | "+strDestination;
  var avalable ="";
  for (i=0;i<GaRoutes.length;i++)
  {
    if (GaRoutes[i].indexOf(str)!=-1)
    {
      avalable = GaRoutes[i];
    }
  }
  avalable = RemoveFun(avalable,str+' | ');
  var temp = avalable.split('|');
  try
  {
    // sort the date
    for (i=0;i<temp.length;i++)
    {
      for (j=0;j<temp.length-1;j++)
      {
        var GdToday1 = new Date();
        var temp1 = temp[i].split('/');
        if (fncIsDate(Math.round(temp1[0]),Math.round(temp1[1]),Math.round(temp1[2])))
        {
          GdToday1 = fncNewDate(Math.round(temp1[0]),Math.round(temp1[1]),Math.round(temp1[2]));
        }
        var GdToday2 = new Date();

        var temp2 = temp[j].split('/');
        if (fncIsDate(Math.round(temp2[0]),Math.round(temp2[1]),Math.round(temp2[2])))
        {
          GdToday2 = fncNewDate(Math.round(temp2[0]),Math.round(temp2[1]),Math.round(temp2[2]));
        }
        if (GdToday1.getTime() < GdToday2.getTime())
        {
          var tp = temp[i];
          temp[i] = temp[j];
          temp[j] = tp;
        }
      }
    }
    var i = 0;
    while (i<temp.length)
    {

      if (i==0)
      {
        var GdToday = new Date();
        var temp2 = temp[i].split('/');
        if (fncIsDate(Math.round(temp2[0]),Math.round(temp2[1]),Math.round(temp2[2])))
        {
          GdToday = fncNewDate(Math.round(temp2[0]),Math.round(temp2[1]),Math.round(temp2[2]));
        }
        GdToday.setDate(GdToday.getDate()-1);
        cal17.addDisabledDates(null,formatDate(GdToday,"yyyy-MM-dd"));
        i++
      }

      else if (i==temp.length-1)
      {
        var GdToday = new Date();
        var temp2 = temp[i].split('/');
        if (fncIsDate(Math.round(temp2[0]),Math.round(temp2[1]),Math.round(temp2[2])))
        {
          GdToday = fncNewDate(Math.round(temp2[0]),Math.round(temp2[1]),Math.round(temp2[2]));
        }
        GdToday.setDate(GdToday.getDate()+1);
        cal17.addDisabledDates(formatDate(GdToday,"yyyy-MM-dd"),null);
        i++
      }
      else
      {
        var GdToday2 = new Date();
        var temp3 = temp[i].split('/');

        if (fncIsDate(Math.round(temp3[0]),Math.round(temp3[1]),Math.round(temp3[2])))
        {
          GdToday2 = fncNewDate(Math.round(temp3[0]),Math.round(temp3[1]),Math.round(temp3[2]));
        }

        var GdToday3 = new Date();
        var temp4 = temp[i+1].split('/');

        if (fncIsDate(Math.round(temp4[0]),Math.round(temp4[1]),Math.round(temp4[2])))
        {
          GdToday3 = fncNewDate(Math.round(temp4[0]),Math.round(temp4[1]),Math.round(temp4[2]));
        }
        GdToday2.setDate(GdToday2.getDate()+1);
        GdToday3.setDate(GdToday3.getDate()-1);
        cal17.addDisabledDates(formatDate(GdToday2,"yyyy-MM-dd"),formatDate(GdToday3,"yyyy-MM-dd"));
        i+=2;
      }

    }
  }
  catch(e)
  {
    //alert(e);
  }
}


function RemoveFun(s,rem)
{
  try
  {
    var a=s;
    if (parseInt(a, 10)==0 || parseInt(a, 10)==1)
    return(parseInt(a, 10));
    else
    {
      var temp_44=a.split(rem);
      return(temp_44.join(""));
    }
  }
  catch(e)
  {}
}

