if(typeof String.prototype.trim !== 'function') {
  String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, '');
  }
}

$(document).ready(function() {
  $(".tabs").tabs();
  $(".rollover").rollover(true);
  $("ul.sf-menu").superfish({
    animation: { opacity: 1, height: "show" },
    autoArrows: false
  });

highlight_menu();

});

function highlight_menu() {
  var active = $("a[href="+window.location.pathname+"]");
  active.parents("li").addClass("active");
  active.parents(".bluebutton").addClass("active");
}

/* Registrations */
$(document).ready(function() {
  // registration courses

  $('div.registration_courses table').attr('class', 'course-schedule');
  $('div.registration_courses tr').each(function(index, element) {
    var course_name = $(element).children('td').html();
    var course_name_stripped = course_name.replace(/[^A-za-z\s]*/g, '').replace(/course|level/gi, '').replace('nbsp', '').trim().replace(/[^A-Za-z*]/g, '');
    $(element).prepend("<td width='10px;'><input type='checkbox' onclick=\"$('tr." + course_name_stripped + "').toggle();\" value='" + course_name + "'></td>")
  });
  $('div.registration_courses table').prepend("<tr><th></th><th>Course</th><th>Register Fee</th></tr>");

  // fetch course dates for registration form
  fetch_date_courses();

  // pyament methods
  $('div.payment_methods table').attr('class', 'payment-methods');
  $('div.payment_methods tr').each(function(index, element) {
    var payment_name = $(element).children('td').html();
    $(element).prepend("<td width='10px'><input type='radio' value='" + payment_name + "' name='payment_name'></td>")
  });
});

function collect_selected_courses_and_payment_method() {
  var courses = '';
  var payment_method = '';

  $('div.registration_courses table input[type=checkbox]').each(function(index, element) {
    if($(element).attr('checked')) {
      var new_course = '';
      var course_class = $(element).attr('onclick').toString().match(/tr\..*/)[0].replace(/\"\).toggle.*/gm, '').replace('tr.', '');
      $('tr.' + course_class + ' input[type=radio]').each(function(radio_index, radio) {
        if($(radio).attr('checked')) {
          new_course = ' ' + $(element).val() + ', ' + $(radio).val() + ';';
        }
      });

      if(new_course) {
        courses += new_course;
      } else {
        courses += ' ' + $(element).val() + ';';
      }
    }
  });

  $('div.payment_methods table input[type=radio]').each(function(index, element) {
    if($(element).attr('checked')) {
      payment_method = $(element).val();
    }
  });

  $('#registration_selected_courses').val(courses.trim());
  $('#registration_payment_method').val(payment_method);

  if(courses == '' && payment_method == '') {
    alert('You have to pick up one or more courses and one payment method before continue.');
  }
  else if(courses == '') {
    alert('You have to pick up one or more courses before continue.');
  }
  else if(payment_method == '') {
    alert('You have to pick up one payment method before continue.');
  }
  else {
    return true;
  }

  return false;
}

function fetch_date_courses() {
  $('table.course-schedule td:first-child + td').each(function(index, element) {
    var course_name = $(element).html().replace(/[^A-za-z\s]*/g, '').replace(/course|level/gi, '').replace('nbsp', '').trim();
    $.ajaxSetup ({ cache: false});
    $.getJSON('/dates_for_event', {title:course_name}, function(data, textStatus) {
      for(var i=0; i<data.length; i++) {
        var start_date = new Date(data[i].start_at).toDateString();
        var class_course_name = course_name.replace(/[^A-Za-z*]/g, '');
        $(element).parent().after("<tr style='display: none;' class='course_date " + class_course_name + "'><td><input type='radio' name='" + class_course_name + "' value='" + start_date + "'/></td><td colspan='2' style='text-align:right'>" + start_date + "</td></tr>");
      }
    });
  });
}

