// Author Kurt Gubi
// HotHouse Interactive 2009

function PurchaseWhen(formSelector, sysdate) {
  var purchaseWhen = this;
  
  purchaseWhen._form = $(formSelector);
  purchaseWhen._sysdate = new Date(sysdate);

  purchaseWhen._purchaseWhen = purchaseWhen._form.find('input[name="purchaseWhen"]');
  purchaseWhen._purchaseDate = purchaseWhen._form.find('input[name="purchaseDate"]');
  purchaseWhen._purchaseMonth = purchaseWhen._form.find('select[name="purchase-month"]');
  purchaseWhen._purchaseYear = purchaseWhen._form.find('select[name="purchase-year"]');
  purchaseWhen._purchase18months = purchaseWhen._form.find('input[name="purchase-18months"]');
  
  purchaseWhen._purchaseMonth.change(function() {
    purchaseWhen._purchase18months.attr('checked', false);
    purchaseWhen._purchaseWhenChange(false);
  });
  purchaseWhen._purchaseYear.change(function() {
    purchaseWhen._purchase18months.attr('checked', false);
    purchaseWhen._purchaseWhenChange(false);
  });
  purchaseWhen._purchase18months.click(function() {
    purchaseWhen._purchaseWhenChange( $(this).attr('checked') );
  });
}

PurchaseWhen.prototype._getPurchaseDateString = function( time, months )
{
  var newTime = new Date(time.getTime());
  newTime.setMonth(newTime.getMonth()+months);
  return (newTime.getDate() + '/' + (newTime.getMonth()+1) + '/' + newTime.getFullYear());
}

PurchaseWhen.prototype._purchaseWhenChange = function(resetFields)
{
  // reset selected year/month if required
  if(resetFields)
  {
    this._purchaseYear[0].selectedIndex = 0;
    this._purchaseMonth[0].selectedIndex = 0;
  }
  
  // reset the hidden input values to default (before calculating)
  this._purchaseWhen.val('-1');
  this._purchaseDate.val('');
  
  // if they check the "More than 18 months" checkbox, don't worry about the month/year dropdowns
  if(this._purchase18months.attr('checked'))
  {
    this._purchaseWhen.val('4');
    this._purchaseDate.val(this._getPurchaseDateString(this._sysdate, 18));
  }
  // double-check they have selected both a month and a year
  else if(this._purchaseYear.val() != 0 && this._purchaseMonth.val() != 0)
  {
    // 'purchaseTime' is the number of months until they plan to purchase
    var purchaseTime = (this._purchaseMonth.val() - (this._sysdate.getUTCMonth()+1)) + 12*(this._purchaseYear.val() - this._sysdate.getUTCFullYear());
    // 'DD/MM/YYYY'
    this._purchaseDate.val(this._getPurchaseDateString(this._sysdate, purchaseTime));
    
    // reset the values to default if they have selected a date in the past
    if(purchaseTime < 0)
    {
      this._purchaseWhen.val('-1');
      this._purchaseDate.val('');
    }
    else
    {
      switch(purchaseTime)
      {
        case 0:
          this._purchaseWhen.val('5');
          break;
        case 1:
        case 2:
        case 3:
          this._purchaseWhen.val('1');
          break;
        case 4:
        case 5:
        case 6:
          this._purchaseWhen.val('2');
          break;
        case 7:
        case 8:
        case 9:
        case 10:
        case 11:
        case 12:
          this._purchaseWhen.val('3');
          break;
        default:
          this._purchaseWhen.val('4');
          break;
      }
    }
  }
}

