//////////////////////////////////////////////////////////
//
//  File: Event Registration Tools
//  Author: Craig Nelson / Classic Labs
//

  var MP = MP || {};

  MP.Register = function (config) {
    // scope correction
    var that = this;

    // elements
    this.attendeeTypes = config.attendeeTypes || $$("input[id^='attendee-type']");
    this.paypalItemName = config.paypalItemName || $("item-name"); // for paypal
    this.paypalItemAmount = config.paypalItemAmount || $("item-amount"); // for paypal
    this.submitBtn = config.submitBtn || $("submit");
    this.regForm = config.regForm || $("event-reg");
    
    // defaults
    this.attendeeDefaultType = config.attendeeDefaultType || "Member";
    this.itemDefaultAmount = config.itemDefaultAmount;
    this.itemDefaultSuffix = " - " + this.attendeeDefaultType;
    
    // misc configs
    this.memberPrice = config.memberPrice || null; // string, not number for paypal
    this.nonMemberPrice = config.nonMemberPrice || null; // string, not number for paypal
    this.studentPrice = config.studentPrice || null; // string, not number for paypal
    this.singleItemEvent = config.singleItemEvent || false;

    // events
    if (!this.singleItemEvent) {
      this.attendeeTypes.each(function (e) {
        Event.observe(e, "change", function (event) {
          switch (this.value) {
            case "member":
              that.paypalItemAmount.value = that.memberPrice;
              that.setItemSuffix(that.paypalItemName, " - Member");
            break;
            case "non-member":
              that.paypalItemAmount.value = that.nonMemberPrice;
              that.setItemSuffix(that.paypalItemName, " - Non-Member");
            break;
            case "student":
              that.paypalItemAmount.value = that.studentPrice;
              that.setItemSuffix(that.paypalItemName, " - Student");
            break;
          }
        });
      });
    }
    
    // methods
    this.setItemSuffix = function (el, itemSuffix) {
      if (el.value.match(/(member)|(non-member)|(student)$/i) != null) {
        el.value = el.value.replace(/\s-\s.+$/i, itemSuffix);
      }
      else if (!this.singleItemEvent) {
        el.value += itemSuffix;
      }
    };
    
    this.setDefaultAttendee = function () {
      this.attendeeTypes.each(function (e) {
        if (e.value == that.attendeeDefaultType.toLowerCase()) {
          e.checked = true;
        }
      });
    };
    
    // apply defaults on every page load
    this.setItemSuffix(this.paypalItemName, this.itemDefaultSuffix);
    this.paypalItemAmount.value = this.itemDefaultAmount;
    this.setDefaultAttendee();
    
    // enable submit button
    this.submitBtn.disabled = false;
  }; // constructor