var Booking = {
  lineHTML: '' +
    '<li>'+
    '<div class="portlet_ticket" id="INDEX">' +
      '<div class="portlet_title big">Ticket #INDEX</div>' +
        '<div class="portlet_body">' +
        '<div class="portlet_content no_ico">'+
          '<div class="fields_2cols">'+
          '<p>' +
          '<label for="in_progress_ticket_INDEX_ticket_type_price_id">I18N_0</label>'+
          'TICKET_TYPES'+
          '</p>' +
          '<div class="ticket_details" style="display:none;"><p>' +
            '<label for="in_progress_ticket_INDEX_first_name">I18N_1</label>' +
            '<input class="fieldbox wide" id="in_progress_ticket_INDEX_first_name" name="in_progress_ticket[INDEX][first_name]" size="30" tabindex="INDEX2" type="text" value="FIRST_NAME" />' +
          '</p>' +
          '<p>' +
            '<label for="in_progress_ticket_INDEX_last_name">I18N_2</label>' +
            '<input class="fieldbox wide" id="in_progress_ticket_INDEX_last_name" name="in_progress_ticket[INDEX][last_name]" size="30" tabindex="INDEX3" type="text" value="LAST_NAME" />' +
          '</p>' +
          '<p>' +
            '<label for="in_progress_ticket_INDEX_company">I18N_3</label>' +
            '<input class="fieldbox wide" id="in_progress_ticket_INDEX_company" name="in_progress_ticket[INDEX][company]" size="30" tabindex="INDEX4" type="text" value="COMPANY" />' +
          '</p>' +
          '<p>' +
            '<label for="in_progress_ticket_INDEX_email">I18N_4</label>' +
            '<input class="fieldbox wide" id="in_progress_ticket_INDEX_email" name="in_progress_ticket[INDEX][email]" size="30" tabindex="INDEX5" type="text" value="EMAIL" />' +
          '</p>'+
          '</div>' +
          '<div class="custom_ticket_details"><p>' +
          'CUSTOM_FIELDS' +
          '</div>' +
          '</div>' +
          '<div class="center">' +
             '<input type="submit" class="button ticketadd" value="I18N_5" onclick="Booking.addLineWithBookingDetails(); return false;"/>'+
             '<input type="submit" class="button ticketdelete" value="I18N_6" onclick="Booking.deleteLine(\'INDEX\'); return false;"/>'+
          '</div>' +
        '</div>' +
       '</div>' +
    '</div>' +
    '</li>',

  lineIndex: 1,
  custom_fields: '',
  empty_custom_values: [],

  setup: function(i18n) {
    this.i18n = i18n;
  },

  isValid: function() {
    //check number of tickets
    return (this.total() >= 0)
  },

  validate: function() {
    if(this.isValid()) { return true; } else { alert("You must buy at least one ticket, and the booking total cannot be negative."); return false; }
  },

  indexedHTML: function(first_name, last_name, company, email, additional_values) {
    t_lineHTML = this.lineHTML;
    for (var i=0; i < this.i18n.length; i++) {
      t_lineHTML = t_lineHTML.replace("I18N_" + i, this.i18n[i])
    };
    t_lineHTML = t_lineHTML.replace(/CUSTOM_FIELDS/g, this.custom_fields)

    for (var i=0; i < additional_values.length; i++) {             
      t_lineHTML = t_lineHTML.replace(additional_values[i].key+'_VAL', additional_values[i].value)
    };
    
    return t_lineHTML.replace(/TICKET_TYPES/g, this.ticket_types).replace(/FIRST_NAME/g, first_name).replace(/LAST_NAME/g, last_name).replace(/COMPANY/g, company).replace(/EMAIL/g, email).replace(/INDEX/g, this.lineIndex++);
  },

  createLine: function(ticket_type_price_id, first_name, last_name, company, email, custom_values) {
    new Insertion.Bottom($('list4'), this.indexedHTML(first_name, last_name, company, email, custom_values));
    this.last_ticket_type().value = ticket_type_price_id;
    this.update();
  },

  addLine: function(custom_values) {
    type = this.last_ticket_type() ? this.last_ticket_type().value : ''
    this.createLine(type, '', '', '', '', this.empty_custom_values);
    $$('.ticket_details').each(function(div) {div.show();});
  },

  addLineWithBookingDetails: function() {
    if ($('use_booking_details_check').checked){
                      // default to the last ticket type
      this.createLine(this.last_ticket_type().value, 
                      // and copy the booking information
                      $('in_progress_booking_first_name').value, 
                      $('in_progress_booking_last_name').value, 
                      $('in_progress_booking_company').value, 
                      $('in_progress_booking_email').value,
                      this.empty_custom_values);
    } else {
      this.addLine();
    }
  },
  
  last_ticket_type : function(){
    var s = $$('.select_type');
    return s[s.length-1];
  },
  
  deleteLine: function(index) {
    if ($$(".portlet_ticket").size() > 1) {
      Element.remove(Element.ancestors(index)[0]);  
      this.update();
    }
  },

  loadTicketTypes: function(ticket_types) {
    this.ticket_types = ticket_types
  },

  // Returns the total (the sum of each line's line total)
  total: function() {
    sum = 0.0;
    $$('.ticket_type_option').each(function(s) {
      if (ticket_prices[s.value]) sum+=ticket_prices[s.value];
    });
    return sum
  },

  update: function() {
    /*$('cost').innerHTML = this.total().toFixed(2) +" "+ this.currencySymbol;*/
  },
  
  render_ticket_details: function(){
    if ($('use_booking_details_check').checked){
      $$('.portlet_ticket').each(function(ticket) {
        idx = ticket.id;
        $('in_progress_ticket_'+ idx +'_first_name').value = $('in_progress_booking_first_name').value;
        $('in_progress_ticket_'+ idx +'_last_name').value = $('in_progress_booking_last_name').value;
        $('in_progress_ticket_'+ idx +'_company').value = $('in_progress_booking_company').value;
        $('in_progress_ticket_'+ idx +'_email').value = $('in_progress_booking_email').value;
      });
      $$('.ticket_details').each(function(div) {div.hide();});
    } else {
      $$('.ticket_details').each(function(div) {div.show();});
    }
  }
  
}
