function redirect_to_new_search(id_of_search) {
  redirect_to_new_generic_search(id_of_search, 'index');
}

function redirect_to_new_advanced_search(id_of_search) {
  redirect_to_new_generic_search(id_of_search, 'advanced');
}

function show_query_tab() {$("#query_tab").show(); $("#results_tab").hide();}
function show_results_tab() {$("#results_tab").show(); $("#query_tab").hide();}
function toggle_chevrons(id) {
  id1 = "#" + id + "_chev";
  id2 = "#" + id + "_chev_down";
  $(id1).toggle();
  $(id2).toggle();
}

function check_chevron_status(id) {
  alert(id);
  id1 = "#" + id + "_chev";
  id2 = "#" + id + "_s";
  if ($(id1).is(':visible')) {
    alert(true);
    return true;
  }
  else {
    $(id2).hide();
    toggle_chevrons(id);
    return false;
  }
}

function redirect_to_new_generic_search(id_of_search, axn) {
  var loc = "aers";
  if (id_of_search == "vaccines")  loc = "vaers";
  else if (id_of_search == "prrs") loc = "analysis";
  else if (id_of_search == "vprrs") {
      loc = "vaers_analysis";
      axn = "index";
  }
  else if (id_of_search == "warn") loc = "warning_letters";
  else if (id_of_search == "pmc")  {
    loc = "postmarketing_study_commitments";
    axn = "index";
  }
  else if (id_of_search == "cln")  {
    loc = "clinical_trials";
  }
  else if (id_of_search == "news") {
    loc = "fda_news";
    axn = "index";
  } else if (id_of_search == "wik") {
    loc = "wiki";
    axn = "";
  }
  window.location = "/" + loc + "/" + axn;    
}

$.fn.clearForm = function() {
  return this.each(function() {
    var type = this.type, tag = this.tagName.toLowerCase();
    if (tag == 'form')
      return $(':input',this).clearForm();
    if (type == 'text' || type == 'password' || tag == 'textarea')
      this.value = '';
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    else if (tag == 'select')
      this.selectedIndex = 0;
  });
};

function check_if_loaded(id) {
  if ($('#eager_' + id).html().indexOf("table") >= 0) return true;
  return false;
}

function eagerly_toggle(id, type, encoded_value, case_position) {
  eager     = $('#eager_' + id);
  triangle  = $('#triangle_' + id);
  triangled = $('#triangle_d_' + id);
  if (eager.is(':visible')) { //is the subtable already being displayed?
    //flip the triangle up
    triangled.hide();
    triangle.show();
    // close the table
    eager.hide();
  } else {
    //flip the triangle down
    triangle.hide();
    triangled.show();
    if (check_if_loaded(id)) { // has the subtable already been loaded?
      // show the table
      eager.show();
    } else {
      // load the table and show it
      load_table(id, type, encoded_value, case_position);
      eager.show();
    }
  }
}

function load_table(id, type, encoded_value, case_position) {
  $('#busy_' + id).show();
  var url = "/" + type + "/load_adverse_event_eagerly/" + id + "/" + encoded_value + "/" + case_position;
  $.get(url, {}, function(html) {
    $('#eager_' + id).html(html);
    $('#busy_' + id).hide();
  });
}


function verify_buy_now_information(username) {
  if (username.length < 1 || username == "guest") {
    address = $('#email_address').value;
    //validation of proper email address syntax
    var email_regex = new RegExp("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$", "i");
    valid = email_regex.test(address);
    //validation that the email address exists
    if (!valid) {
      alert('Please enter a valid email address');
      return false;
    }
  }
  $('#custom').value = $('#email_address').value;
  $('#pp_form').submit();
  return false;
}