//This script adapted from examples on http://members.aol.com/paypalhelper/ which is linked at
//https://www.paypal.com/au/cgi-bin/webscr?cmd=p/xcl/rec/sc-techniques for techniques that can be used
//to manipulate interaction with the PayPal Shopping Basket.
var BaseAmt, BaseDesc, NoTickets;
var pc = 5;                               // No. of qty/percent pairs
var pqty = new Array (1, 7, 14, 21, 28);  // qty brkpt
var pper = new Array (0, 10, 15, 20, 25); // percent to discount

function Dollar (val) {  // force to valid dollar amount
var str,pos,rnd=0;
  if (val < .995) rnd = 1;  // for old Netscape browsers
  str = escape (val*1.0 + 0.005001 + rnd);  // float, round, escape
  pos = str.indexOf (".");
  if (pos > 0) str = str.substring (rnd, pos + 3);
  return str;
}

function ReadForm (obj1) // quantity based discounts
{
  var i, ArrivalDate, DepartDate, DifferenceInDays, elNoInGrp, elNoOfPeople;
  var NoInGrp, NoNights, ReturnDate, strPackage;
  var datArrivalDateEng, datDepartDateEng, datMonth, datReturnDateEng, aDateSelected = new Array(3);
  var dtMar52007 = new Date(2007,2,5), dtDepartDate = new Date();
  IsTrainRet = new Boolean(0);
  var idxPeopleMax = 12; // table for No. of People/Commission pairs
  var aNumPeople = new Array (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12); // Ticket Commission Breakpoints
  var aCommission = new Array (6.52, 6.52, 6.52, 6.52, 7.56, 8.6, 9.63, 10.66, 11.7, 12.73, 13.49, 14.8); //Ticket Commission Cost
 
  if (BaseDesc == "NoOfPeople") //Process Commission
  {
    elNoOfPeople = document.getElementById('inpNoOfPeople');
    NoOfPeople = elNoOfPeople.options[elNoOfPeople.selectedIndex].text*1.0;
    strPackage = "Comisión de Procesamiento de Boleto para " + NoOfPeople + " pasajero";

    if (NoOfPeople > 1)
    {
      strPackage=strPackage + "s"
    }

    strPackage=strPackage + "."
    obj1.item_name.value = strPackage;

    for (i=idxPeopleMax-1; i>=0; i=i-1) //run table backwards
    {
      if (NoOfPeople == aNumPeople[i]) //use this entry
      {
        obj1.amount.value = Dollar (aCommission[i]);
        break; //get out, we're done
      }
    }

    obj1.item_number.value = "";
        //Because the button activating the Posting of the Form is outside of the Form,
        //we need to set the Target parameter to force the Shopping Cart to open in
        //another window so that the Continue Shopping button appears.
    obj1.target = "paypal";
    obj1.submit ();
  }
  else if (BaseDesc.substr(0,3) == "Trn") //Determine if Reservation is for Train Ticket
  {
    DepartDate = document.getElementById("inpDepartDateTrn").value;

    if (DepartDate == "")
    {
      alert("Se requiere una Fecha de Salida!");
      return false;
    }
    else
    {
      aDateSelected = DepartDate.split(' ');
      datMonth = aDateSelected[0];

      switch(datMonth) {
        case 'Ene':
          datMonth = 'Jan'; break;
        case 'Abr':
          datMonth = 'Apr'; break;
        case 'Ago':
          datMonth = 'Aug'; break;
        case 'Set':
          datMonth = 'Sep'; break;
        case 'Dic':
          datMonth = 'Dec'; break;};

      datDepartDateEng = datMonth + ' ' + aDateSelected[1] + ' ' +aDateSelected[2];
      dtDepartDate = Date.parse(datDepartDateEng);

      if (dtDepartDate > dtMar52007)
      {
        if (BaseDesc.search(/OM/) == -1 && BaseDesc.search(/O-M/) == -1 && BaseDesc.search(/M-O/) == -1 && BaseDesc.search(/M-C/) == -1)
        {
          alert("¡Después del 5 de marzo de 2007, los boletos para el servicio de Urubamba no pueden ser comprados hasta el aviso adicional!");
          return false;
        }
      }
    }

    if (BaseDesc.substr(6,3) == "Ret")
    {
      IsTrainRet = true;
    }
    
    if (IsTrainRet == true)
    {
      ReturnDate = document.getElementById("inpReturnDateTrn").value;

      if (ReturnDate == "")
      {
        alert("Se requiere una Fecha de Vuelta!");
        return false;
      }
      else
      {
        aDateSelected = ReturnDate.split(' ');
        datMonth = aDateSelected[0];

        switch(datMonth) {
        case 'Ene':
          datMonth = 'Jan'; break;
        case 'Abr':
          datMonth = 'Apr'; break;
        case 'Ago':
          datMonth = 'Aug'; break;
        case 'Set':
          datMonth = 'Sep'; break;
        case 'Dic':
          datMonth = 'Dec'; break;};

        datReturnDateEng = datMonth + ' ' + aDateSelected[1] + ' ' +aDateSelected[2];
        DifferenceInDays = DaysDelta(datDepartDateEng,datReturnDateEng);

        if (DifferenceInDays < 0)
        {
          alert("¡La Fecha de Vuelta debe ser >= la Fecha de Salida!");
          return false;
        }
      }
    }

    if (NoTickets == "")
    {
      alert("¡Por favor entre en el número de boletos requeridos!");
      return false;
    }
    else
    {
      if (isNaN(NoTickets))
      {
        alert("¡Se requiere un valor numérico para el número de boletos!");
        return false;
      }
      else
      {
        NoTickets = NoTickets*1.0;
        
        if (NoTickets < 1)
        {
          alert("¡El número de boletos debe ser >= 1!");
          return false;
        }
      }
    }

    obj1.item_number.value = ": " + BaseDesc;
    BaseAmt = BaseAmt*1.0;
    strPackage = NoTickets + " Boleto";

    if (NoTickets > 1)
    {
      strPackage = strPackage + "s";
    }

    if (IsTrainRet == true)
    {
      strPackage = strPackage + " de Vuelta de Tren";
    }
    else
    {
      strPackage = strPackage + " sin Retorno de Tren";
    }

    if (BaseDesc.indexOf ("Child") > 0)
    {
      strPackage = strPackage + " de Niño";
    }
    else
    {
      strPackage = strPackage + " de Adulto";
    }

    if (IsTrainRet == false)
    {
      switch (BaseDesc.substr(10,3))
      {
        case "U-M":
          strPackage = strPackage + ", Urubamba - Machu Picchu";
          break;
        case "M-U":
          strPackage = strPackage + ", Machu Picchu - Urubamba";
          break;
        case "O-M":
          strPackage = strPackage + ", Ollantaytambo - Machu Picchu";
          break;
        case "M-O":
          strPackage = strPackage + ", Machu Picchu - Ollantaytambo";
          break;
        case "M-C":
          strPackage = strPackage + ", Machu Picchu - Cusco";
      }
    }
    
    strPackage = strPackage + "; Salida"

    if (IsTrainRet == true)
    {
      if (BaseDesc.search(/OM/) == -1)
      {
        strPackage = strPackage + " de Urubamba: ";
      }
      else
      {
        strPackage = strPackage + " de Ollantaytambo: ";
      }

      strPackage = strPackage + DepartDate + ", Vuelta de Machu Picchu: " + ReturnDate;
    }
    else
    {
      strPackage = strPackage + ": " + DepartDate
    }

    obj1.item_name.value = strPackage;
    obj1.amount.value = Dollar (BaseAmt * NoTickets);

        //Because the button activating the Posting of the Form is outside of the Form,
        //we need to set the Target parameter to force the Shopping Cart to open in
        //another window so that the Continue Shopping button appears.
    obj1.target = "paypal";
    obj1.submit ();
  }
  else //Process Accommodation Reservations
  {
    ArrivalDate = document.getElementById("inputArrivalDate").value;

    if (ArrivalDate == "")
    {
      alert("Se requiere una Fecha de Llegada!");
      return false;
    }

    DepartDate = document.getElementById("inputDepartDate").value;

    if (DepartDate == "")
    {
      alert("Se requiere una Fecha de Salida!");
      return false;
    }

    aDateSelected = ArrivalDate.split(' ');
    datMonth = aDateSelected[0];

    switch(datMonth) {
      case 'Ene':
        datMonth = 'Jan'; break;
      case 'Abr':
        datMonth = 'Apr'; break;
      case 'Ago':
        datMonth = 'Aug'; break;
      case 'Set':
        datMonth = 'Sep'; break;
      case 'Dic':
        datMonth = 'Dec'; break;};

    datArrivalDateEng = datMonth + ' ' + aDateSelected[1] + ' ' +aDateSelected[2];
    aDateSelected = DepartDate.split(' ');
    datMonth = aDateSelected[0];

    switch(datMonth) {
      case 'Ene':
        datMonth = 'Jan'; break;
      case 'Abr':
        datMonth = 'Apr'; break;
      case 'Ago':
        datMonth = 'Aug'; break;
      case 'Set':
        datMonth = 'Sep'; break;
      case 'Dic':
        datMonth = 'Dec'; break;};

    datDepartDateEng = datMonth + ' ' + aDateSelected[1] + ' ' +aDateSelected[2];
    NoNights = DaysDelta(datArrivalDateEng, datDepartDateEng);

    if (NoNights <= 0)
    {
      alert("La Fecha de Salida debe ser mayor que la Fecha de Llegada!");
      return false;
    }

    BaseAmt = BaseAmt*1.0;                             // base amount
    NoNights = NoNights*1.0;                           // force to numeric

    for (i=pc-1; i>=0; i=i-1)                          // run table backwards
    {
      if (NoNights >= pqty[i])                         // use this entry
      {                     
        BaseAmt = BaseAmt - (BaseAmt/100.0 * pper[i]);
        break;                                         // get out, we're done
      }
    }

    elNoInGrp = document.getElementById('selectNoInGrp');
    NoInGrp = elNoInGrp.options[elNoInGrp.selectedIndex].text*1.0;

    if (NoNights == 1)
    {
      if (BaseDesc == "Grupo")
      {
        strPackage = " noche para " + NoInGrp + " personas @ $" + Dollar (BaseAmt) + " por persona";
      }
      else
      {
        strPackage = " noche para $" + Dollar (BaseAmt);
      }
    }
    else
    {
      if (BaseDesc == "Grupo")
      {
        strPackage = " noches para " + NoInGrp + " personas @ $" + Dollar (BaseAmt) + " por persona por noche";
      }
      else
      {
        strPackage = " noches @ $" + Dollar (BaseAmt) + " por noche";
      }
    }
    strPackage = strPackage + ", Llegada: " + ArrivalDate + ", Salida: " + DepartDate + ".";
    obj1.ap_description.value=BaseDesc + ", paquete de " + NoNights + strPackage;
    obj1.ap_returnurl.value="http://" + location.host + location.pathname + "?DatArr=" + ArrivalDate + "&DatDep=" + DepartDate + "#Accommodation";
    obj1.ap_cancelurl.value=obj1.ap_returnurl.value;
    if (BaseDesc=="Grupo")
    {
      obj1.ap_amount.value=Dollar(BaseAmt*NoNights*NoInGrp);
      obj1.ap_itemcode.value="ITEM CODE - Grp:";
      AddToCart(obj1);//16/11/2008, Michael Holberton - Integrated with nopERcart for AlertPay
      obj1.submit();
    }
    else
    {
      obj1.ap_amount.value=Dollar(BaseAmt*NoNights);
      obj1.ap_itemcode.value="ITEM CODE - "+obj1.ap_itemcode.value+":";
      AddToCart(obj1);
    }
  }
}
function ViewCart(frm) //Set the ReturnURL and CancelURL for AlertPay & open the nopERcart
{
  var ArrivalDate,DepartDate;
  ArrivalDate=document.getElementById("inputArrivalDate").value;
  DepartDate=document.getElementById("inputDepartDate").value;
  frm.ap_returnurl.value="http://"+location.host+location.pathname+"?DatArr="+ArrivalDate+"&DatDep="+DepartDate+"#Accommodation";
  frm.ap_cancelurl.value=frm.ap_returnurl.value;
  frm.submit();
}