var GraduationForm = Class.create({
  initialize: function(prices, longstanding) {
    this.tbody = $('invoice');
    this.ceremony = $('graduate_ceremony');
    this.honorCord = $('graduate_honor_cord');
    this.announcements = $('graduate_announcement_total');
    this.diploma = $('graduate_diploma');
    this.diplomaLabel = $('diploma_label');
    this.recognition = $('graduate_recognition_recognition');
    this.noPhoto = $('graduate_recognition_photo_type_none');
    this.hardCopyPhoto = $('graduate_recognition_photo_type_hard_copy');
    this.digitalPhoto = $('graduate_recognition_photo_type_digital');
    this.extraWords = $('graduate_recognition_extra_words_1');
    this.lessWords = $('graduate_recognition_extra_words_0');
    this.booklets = $('graduate_recognition_recognition_booklets');
    this.donationSection = $('donation');
    this.donation = $('financial_transaction_donation');
    this.scholarshipFund = $('financial_transaction_scholarship_fund_100');
    this.noScholarshipFund = $('financial_transaction_scholarship_fund_000');

    this.ceremonyFields = $$("#ceremony input, #ceremony select");
    this.diplomaFields = $$("#diploma input");
    this.recognitionFields = $$("#recognition input, #recognition textarea");
    
    this.paymentFields = $('payment_fields');
    this.noPayment = $('no_payment');

    this.prices = prices;
    this.longstanding = longstanding;
    this.addListeners();
    this.setupInitialState();
  },
  setupInitialState: function() {
    this.updateCeremony();
    this.updateDiploma();
    this.updateRecognition();
  },
  addListeners: function() {
    this.ceremony.observe('click', this.updateCeremony.bind(this));
    this.diploma.observe('click', this.updateDiploma.bind(this));
    this.recognition.observe('click', this.updateRecognition.bind(this));
    
    [this.honorCord, this.noPhoto, this.hardCopyPhoto, this.digitalPhoto, this.extraWords, this.lessWords].each(function(checkbox) {
      checkbox.observe('click', this.updateLineItems.bind(this));
    }, this);
    
    if (this.donationSection) {
      this.donation.observe('keyup', this.updateLineItems.bind(this));
      this.scholarshipFund.observe('click', this.updateLineItems.bind(this));
      this.noScholarshipFund.observe('click', this.updateLineItems.bind(this));
    }
    
    this.booklets.observe('keyup', this.updateLineItems.bind(this));
    this.announcements.observe('change', this.updateLineItems.bind(this));
  },
  updateCeremony: function() {
    if (this.ceremony.checked) {
      this.ceremonyFields.each(M.set('disabled', false));
      this.diploma.checked = true;
      this.diploma.disabled = true;
      this.diplomaFields.each(M.set('disabled', false));
      this.diplomaLabel.update("Diploma (included with ceremony)");
    } else {
      this.ceremonyFields.each(M.set('disabled', true));
      this.diploma.disabled = false;
      this.diplomaLabel.update("Diploma ($" + this.prices.diploma.toFixed(2) + (this.longstanding ? ' S&H' : '') + ")");
    }
    this.updateLineItems();
  },
  updateDiploma: function() {
    if (this.diploma.checked)
      this.diplomaFields.each(M.set('disabled', false));
    else
      this.diplomaFields.each(M.set('disabled', true));
    this.updateLineItems();
  },
  updateRecognition: function() {
    if (this.recognition.checked)
      this.recognitionFields.each(M.set('disabled', false));
    else
      this.recognitionFields.each(M.set('disabled', true));
    this.updateLineItems();
  },
  updateLineItems: function() {
    this.total = 0;
    this.tbody.update('');
    if (this.ceremony.checked) {
      this.lineItem("Graduation Package", this.prices.ceremony);
      if (this.honorCord.checked)
        this.lineItem("Honor Cord", this.prices.honor_cord);
      if (parseInt(this.announcements.value) > 0)
        this.quantityLineItem("Graduation Announcements", this.prices.announcements, parseInt(this.announcements.value));
    } else if (this.diploma.checked) {
      this.lineItem("Diploma" + (this.longstanding ? ' S&H' : ''), this.prices.diploma);
    }
    if (this.recognition.checked) {
      if (this.hardCopyPhoto.checked)
        this.lineItem("Hard Copy Photo", this.prices.hard_copy_photo);
      else if (this.digitalPhoto.checked)
        this.lineItem("Digital Photo", this.prices.digital_photo);
      if (this.extraWords.checked)
        this.lineItem("Extended Bio", this.prices.extra_words);
      ensureInteger(this.booklets);
      if (parseInt(this.booklets.value) > 0)
        this.quantityLineItem("Recognition Booklets", this.prices.recognition_booklets, parseInt(this.booklets.value));
    }
    if (this.donationSection) {
      ensureInteger(this.donation);
      if (parseInt(this.donation.value) > 0)
        this.lineItem("Donation", parseInt(this.donation.value));
      if (this.scholarshipFund.checked)
        this.lineItem("Scholarship Fund", 1);
    }
    this.tbody.insert("<tr><td style='font-size:large;font-weight:bold'>Total</td><td></td><td></td><td style='font-size:large;font-weight:bold'>$" + this.total.toFixed(2) + "</td></tr>");
    
    if (this.total > 0) {
      this.paymentFields.show();
      this.noPayment.hide();
    } else {
      this.paymentFields.hide();
      this.noPayment.show();
    }
  },
  lineItem: function(category, amount) {
    this.total += amount;
    this.tbody.insert("<tr><td>" + category + "</td><td>$" + amount.toFixed(2) + "</td><td>1</td><td>$" + amount.toFixed(2) + "</td></tr>");
  },
  quantityLineItem: function(category, unit_price, quantity) {
    var amount = unit_price * quantity;
    this.total += amount;
    this.tbody.insert("<tr><td>" + category + "</td><td>$" + unit_price.toFixed(2) + "</td><td>" + quantity + "</td><td>$" + amount.toFixed(2) + "</td></tr>");
  }
});