var HappendanceStore = Class.create({
  
  initialize: function(store_id, max_items, item_class_name){
    if(store_id) {
      this.store_id = store_id;
    } else {
      throw {toString: function() { return "StoreID not provided" } };
    }
    
    this.item_class_name = item_class_name || "item";
    
    this.items = this.get_items();

    $$("input[type='checkbox']").each(function(item) {
      item.observe("click", this.handle_item.bindAsEventListener(this));
    }.bind(this) );
    
    $$("input[type='submit']").first().observe("click", function(e) {
      Event.stop(e);
      this.assign_form_elements();
      this.form.submit();
    }.bind(this));
    
    var donation = $$("tr.donation input[type='text']").first();
    donation.value = '';
    donation.observe("change", function(e) {
      var el = Event.element(e);
      var price = el.getValue().gsub(/\$/, "").strip();
      var checkbox = el.up('tr').down("input[type='checkbox']");
      checkbox.checked = ( price ) ? true : false;
      
      if(checkbox.checked) {
        this.selected_items().select(function(item) {
          return item.box == checkbox;
        }).first().price = price;
      }
    }.bind(this));
    
    this.max_items = max_items || 8;
    
    this.build_form();
  },
  
  get_items: function(){
    var rows = $$("tr." + this.item_class_name);
    
    function getValue(el) {
      return (el.nodeName.toLowerCase() == "span") ? el.innerHTML : el.getValue();
    }
    
    /* store checkbox reference, item name, and item price */
    return rows.inject([], function(array, value) {
      array.push({
        box: value.down("input[type='checkbox']"),
        name: value.down(".item_name").innerHTML.strip(),
        price: getValue( value.down(".item_price") ).gsub(/\$/, "").strip()
      });
      return array;
    });
    
  },
  
  selected_items: function(){
    return this.items.select(function(item) {
      return item.box.getValue();
    });
  },
  
  handle_item: function(e) {
    var box = Event.element(e);
    
    if(box.getValue()) {
      if(this.selected_items().length >= this.max_items) {
        this.lock_boxes();
      }
      box.up("tr").addClassName("selected");
    } else {
      this.unlock_boxes();
      box.up("tr").removeClassName("selected");
    }
  },
  
  lock_boxes: function() {
    this.items.reject(function(item) {
      return this.selected_items().include(item);
    }.bind(this)).each(function(item) {
      item.box.disable();
    });
    alert("You may only register for a maximum of 8 classes at a time.\nSorry!");
  },
  
  unlock_boxes: function() {
    this.items.each(function(item) {
      item.box.enable();
    });
  },
  
  build_form: function() {
    if(Prototype.Browser.IE) {
      this.form = new Element("form", {action:"http://www.romancart.com/cart.asp", method:"post", style:"display:none"});
    } else {
      this.form = $form({action:"http://www.romancart.com/cart.asp", method:"post", style:"display:none"});
    }
    $$("body").first().appendChild(this.form);
  },
  
  
  assign_form_elements: function(){
  //begin comment here to disable registration fee
  [
     this.input_element({type:"hidden", name:"itemname", value:'Required Annual Registration Fee'}),
     this.input_element({type:"hidden", name:"price", value: 15}),
     this.input_element({type:"hidden", name:"storeid", value:this.store_id}),
     this.input_element({type:"hidden", name:"quantity", value: (parseInt($('number_of_students').value) || 1)}),
     this.input_element({type:"hidden", name:"mustbuy", value: "no"}),
     this.input_element({type:"checkbox", name:"include", checked:"checked"})
   ].each(function(element) {
     this.form.appendChild(element);
     if(Prototype.Browser.IE) {
       element.checked = true;
     }
   }.bind(this));
   //end commented section here to disable registration fee
     var count = 1; // <-- set this back to 0 if/when you disable the annual fee item above. Set to 1 when re-enabling.
  this.selected_items().each(function(item){
    var name_prefix = (count == 0) ? "" : "X" + count.toString();
    [
      this.input_element({type:"hidden", name:name_prefix+"itemname", value:item.name}),
      this.input_element({type:"hidden", name:name_prefix+"price", value:item.price}),
      this.input_element({type:"hidden", name:name_prefix+"storeid", value:this.store_id}),
      this.input_element({type:"hidden", name:name_prefix+"quantity", value: 1}),
      this.input_element({type:"hidden", name:name_prefix+"mustbuy", value: "no"}),
      this.input_element({type:"checkbox", name:name_prefix+"include", checked:"checked"})
    ].each(function(element) {
      this.form.appendChild(element);
      if(Prototype.Browser.IE) {
        element.checked = true;
      }
    }.bind(this));
    count++;
  }.bind(this));
}, 
  
  
  input_element: function(params){
    if(Prototype.Browser.IE) {
      return new Element("input", params);
    } else {
      return $input(params);
    }
  }
});
