function do_fn(fn_name) {
  document.hidden_form.fn.value = fn_name;
  document.hidden_form.submit();
}

function menu_fn(fn_name) {
  document.menu_form.fn.value = fn_name;
  document.menu_form.submit();
}

function bb_fn(fn_name) {
  document.hidden_form.fn.value = fn_name;
  document.hidden_form.find.value ="";
  document.hidden_form.submit();
}

function del_users() {
  if (confirm('This will delete all user accounts that are not active or have expired (including their Black Book entries!).  Are you sure you wish to do this?')) {
    document.hidden_form.fn.value = 'del_inactive';
    document.hidden_form.submit();
  }
}

function do_page_fn(page) {
  document.hidden_form.page_no.value = page;
  document.hidden_form.submit();
}

function view_entry(horse) {
  document.hidden_form.horse_name.value = horse;
  document.hidden_form.fn.value = 'view';
  document.hidden_form.submit();
}

function confirm_logout() {
  if (confirm("Are you sure you wish to quit?"))
    menu_fn('logout');
}

function new_window(entrant_id) {
  var target = 'horse.php?entrant_id=' + entrant_id;
  window.open(target, 'RecentForm', config='height=600,width=750,resizable=yes,scrollbars=yes');
}

function view_margins(race_id) {
  var target = 'margins.php?race_id=' + race_id;
  window.open(target, 'Margins', config='height=350,width=550,resizable=yes,scrollbars=yes');
}

function view_speeds(race_id) {
  var target = 'speeds.php?race_id=' + race_id;
  window.open(target, 'Speeds', config='height=500,width=500,resizable=yes,scrollbars=yes');
}

function addresses() {
  var target = 'send_mail.php?fn=addresslist';
  window.open(target, 'Addresses', config='height=600,width=500,resizable=yes,scrollbars=yes');
}

function open_members() {
  var target = 'members/best_bets.php';
  var new_window = window.open(target, 'GRS', config='height=550,width=750,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,directories=no,status=yes');
}

function open_link(link) {
  window.open(link, 'Link', config='height=600,width=800,resizable=yes,scrollbars=yes,toolbar=yes,menubar=yes,location=yes,directories=no,status=yes');
}

function print_page() {
  alert('Please note:\n- this command will only work in newer browsers\n- to avoid printing menus etc, print pages 2-3 only');
  window.print();
}

function add_address(address) {
  address = ', ' + address;
  var adds = document.hidden_form.added.value;
  var pos = adds.indexOf(address);

  if (pos == -1)
    adds += address;
  else
    adds = adds.substring(0, pos) + adds.substring(pos + address.length, adds.length);
  document.hidden_form.added.value = adds;
}

function insert_addresses() {
  var content = document.hidden_form.added.value;

  if (content.length > 0)
    content = content.substring(2, content.length);
  top.opener.document.write_form.bcc.value = content;
  window.close();
}

function checktext(field) {
  if (field.value == "") {
    alert("Please fill in all fields!");
    field.focus();
    return false;
  }
  return true;
}

function checkzip(field) {
  if (field.value.substring(field.value.length - 3, field.value.length).toLowerCase() != "zip") {
    alert("Please select a zip file!");
    field.focus();
    field.select();
    return false;
  }
  return true;
}

function checktxt(field) {
  if (field.value.substring(field.value.length - 3, field.value.length).toLowerCase() != "txt") {
    alert("Please select a txt file!");
    field.focus();
    field.select();
    return false;
  }
  return true;
}

function checkpwd(field1, field2) {
  if (field1.value != field2.value) {
    alert("Passwords do not match!");
    field1.focus();
    field1.select();
    return false;
  }
  if (field1.value.length < 6) {
    alert("Password must be at least 6 characters in length!");
    field1.focus();
    field1.select();
    return false;
  }
  return true;
}

function confirmAdmin(field) {
  if (field.value == 'ADMIN')
    return confirm("You have selected this user to be an ADMINISTRATIVE USER.  Please confirm that you wish to do this: ");
  return true;
}

function checkdiv(field) {
  if (field.value.search(/^[0-9]+\.[0-9]{2}$/) != 0) {
    alert("Please enter a valid dividend!");
    field.focus();
    field.select();
    return false;
  }
  return checktext(field);
}

function checkdate(field) {
  if (field.value.search(/^(3[01]|0[1-9]|[12]\d)\/(0[1-9]|1[012])\/\d\d$/) != 0) {
    alert("Please enter a valid date!");
    field.focus();
    field.select();
    return false;
  }
  return true;
}

function checktdc(field) {
  if (field.value.search(/^([tdc]?)+$/) != 0) {
    alert("Only t,d and c are allowed!");
    field.focus();
    field.select();
    return false;
  }
  for (j = 0; j < field.value.length - 1; j++) {
    for (k = j; k < field.value.length - 1; k++) {
      if (field.value.charAt(j) == field.value.charAt(k+1)) {
	alert("Please do not repeat t, d or c!");
	field.focus();
	field.select();
	return false;
      }
    }
  }
  return true;
}

function checkentry(form) {
  return (checkdate(form.race_date) && checktext(form.notes));
}

function check_add_user(form) {
  if (form.date.value.length > 0) {
    if (!checkdate(form.date))
      return false;
  }
  return (checktext(form.a_username) && checkpwd(form.password, form.password) && confirmAdmin(form.a_user_type));
}

function check_add_affiliate(form) {
  return (checktext(form.aff_name) && checktext(form.aff_url));
}

function check_edit_user(form) {
  if (form.password.value.length > 0) {
    if (!checkpwd(form.password, form.password))
      return false;
  }
  if (form.date.value.length > 0) {
    if (!checkdate(form.date))
      return false;
  }
  return confirmAdmin(form.a_user_type);
}

function check_changes(form) {
  for (i = 0; i < form.length - 7; i += 5) {
    if (!(checktdc(form[i+3])))
      return false;
  }
  return true;
}

function change_date(form) {
  form.venue.value = -1;
  form.submit();
}

