// >>>===========================================================
// file: ck_boxes.js
// This script checks boxes on an entry form
// $Id: script_lib.js,v 1.7 2011/06/20 01:43:42 bill Exp bill $
// author: William Haynes 2004 William.Haynes@usa.net
// 
// <input type=button id="ckboxes" name="ckboxes" value="All"
//  onClick="ck_boxes(document.myform,field,nboxes)" />
//

function ck_boxes(field) {
   var i;
   var nchecked = 0;
   var fieldvalue = true;

// let this act as a toggle, uncheck all if all were already checked
   for (i=0; i<field.length; i++) {
      if (eval(field[i].checked) == true) {
         nchecked += 1;
      }
   }
   if (nchecked == field.length) {
      fieldvalue = false;
   }

// check or un-check all boxes
   for (i=0; i < field.length; i++) {
      field[i].checked = fieldvalue;
   }
   return true;
}
// ==============================================================

// >>>===========================================================
// file: ck_terms.js
// require that terms be acknowledge, if present
// $Id: script_lib.js,v 1.7 2011/06/20 01:43:42 bill Exp bill $
// author: William Haynes 2004 William.Haynes@usa.net
// 
// <form...
//  onSubmit="return ck_terms(document.form.terms_checkbox)" />
//

function ck_terms(terms) {
   var status = true;

   if (terms.type == "checkbox") {
       status = eval(terms.checked);
      if (status == false) {
         window.alert("You must acknowledge the terms for entry acceptance");
      }
   }

   return status;
}
// ==============================================================

// >>>===========================================================
// file: limit_selections.js
// $Id: script_lib.js,v 1.7 2011/06/20 01:43:42 bill Exp bill $

// author: William Haynes, William.Haynes@usa.net
// last revised: Sun Jun  6 16:57:26 EDT 2004

// count the number of checkboxes that are checked
// alarm if the number exceeds the limit set for this form

function limit_selections(form, limit, box_checked, n) {

   var i = 0;
   var n_boxes_checked = 0;
    
// no need to count if there is no limit
   if (limit > 0) {

// count all of the checkboxes checked
      n_boxes_checked = count_checkedboxes(form);

// pop-up alert if too many boxes have been checked
// and clear the last one checked
      if (n_boxes_checked > limit) {
         window.alert("Only " + limit + " selections may be made.");
         box_checked.checked = false;
      }
   } 
   return true;
}
// ==============================================================

// >>>===========================================================
// file: limit_selections_wexclusions.js
// limit selectons with exclusions
// $Id: script_lib.js,v 1.7 2011/06/20 01:43:42 bill Exp bill $

// author: William Haynes, William.Haynes@usa.net
// last revised: Sun Jun  6 16:57:26 EDT 2004

// count the number of checkboxes that are checked
// alarm if the number exceeds the limit set for this form

function limit_selections_wexclusions(form,limit,box_checked,n,exclusions) {

   var i = 0;
   var n_boxes_checked = 0;
   var n_boxes_matched = 0;
    
// no need to count if there is no limit
   if (limit > 0) {

// count all of the checkboxes checked
      n_boxes_checked = count_checkedboxes(form);

// count all of the checkboxes checked matching the exclusion pattern 
      n_boxes_matched = 
         count_matching_checkedboxes(form, exclusions);

// don't count the checked boxes that are excluding
//  confirm("n checked=" + n_boxes_checked + ", n matched=" + n_boxes_matched);
      n_boxes_checked = n_boxes_checked - n_boxes_matched;


// pop-up alert if too many boxes have been checked
// and clear the last one checked
      if (n_boxes_checked > limit) {
         window.alert("Only " + limit + " selections may be made.");
         box_checked.checked = false;
      }
   } 
   return true;
}
// ==============================================================

// >>>===========================================================
// file: min_selected.js
// post alarm if not enough items were selected
// $Id: script_lib.js,v 1.7 2011/06/20 01:43:42 bill Exp bill $
function min_selected(form,minvalue,msg) {
   var n;
   n = count_checkedboxes(form);
 
   if (n > 0) {
      if (n < minvalue) {
         window.alert(msg);
         return false;
      }
   }

   return true;
}
// ==============================================================

// >>>===========================================================
// file: nothing_selected.js
// post alarm if not items were selected
// $Id: script_lib.js,v 1.7 2011/06/20 01:43:42 bill Exp bill $
function nothing_selected(form,msg) {
   var n;
   n = count_checkedboxes(form);
 
   if (n == 0) {
      window.alert(msg);
      return false;
   }

   return true;
}
// ==============================================================

// >>>===========================================================
// file: pop_up.js
// open a pop up window
// author: quirksmode.org
//
// usage: 
// <a href="popupex.html" onclick="return popitup('popupex.html')">
//   Link to popup</a>

function pop_up(url) {
   var newwindow = window.open(url,'name','height=400,width=800,scrollbars=1');
   if (window.focus) {newwindow.focus()}
   return false;
}
// ==============================================================
// >>>===========================================================
/* popUpFeeCalc - open small window for fee calculator
 * author: William Haynes, William.Haynes@usa.net
 */
 function popUpFeeCalc(url) {
   var newwindow = window.open(url,'FeeCalculator','height=200,width=320');
   if (window.focus) {newwindow.focus()}
   return false;
 }

// ==============================================================

// >>>===========================================================
// file: std_trackmark.js
// track time calculator for track event entry forms
// $Id: script_lib.js,v 1.7 2011/06/20 01:43:42 bill Exp bill $

// author: William.Haynes 2004 william.haynes@usa.net

// standardize mark hh:mm:ss.ss
// =============================================================
function std_trackmark(item,mm,ss,mark) {
   var maxmm    = 59;
   var maxss    = 59.99;
   var minvalue = 0;
   var mmvalue = mm.value;
   var no_mark = "NM";
   var ssvalue = ss.value;

// verify time values are numeric
   mmvalue = mkNull0(mmvalue);
// seconds
   ssvalue = mkNull0(ssvalue);

// verfy minute and seconds values are within range
// minute
// ================================================
// permit entry of n|N|nm|NM
   if (isNaN(mmvalue)) {
      mmvalue = mmvalue.toUpperCase();
      if ((mmvalue == "N") || (mmvalue == "NM")) {
         mark.value = no_mark;
         mark.focus();
         warn(item);
         return true;
      }
   }

   if ((! isInt(mmvalue)) || (mmvalue < minvalue) || (mmvalue > maxmm)) {
      window.alert( "minutes value must be numeric and between " + 
         minvalue + " and " + maxmm );
      mm.select();
      mm.focus();
      mark.value = no_mark;
      return false;
   } 

// seconds
// ================================================
// permit entry of n|N|nm|NM
   if (isNaN(ssvalue)) {
      ssvalue = ssvalue.toUpperCase();
      if ((ssvalue == "N") || (ssvalue == "NM")) {
         mark.value = no_mark;
         mark.focus();
         warn(item);
         return true;
      }
   }

   if ((! isFloat(ssvalue)) || (ssvalue < minvalue) || (ssvalue > maxss)) {
      window.alert( "seconds value must be numeric and between " + 
         minvalue + " and " + maxss );
      ss.select();
      ss.focus();
      mark.value = no_mark;
      return false;
   } 

// convert minute-seconds to string to standard format mm:ss.ss
// 1st put seconds value into ss.ss format
   if (isInt(ssvalue)) {
      ssvalue = ssvalue * 1.00;
   }
   if (ssvalue > 0) {
      if (ssvalue < 1) {
        ssvalue = "00" + ssvalue;
      } else if (ssvalue < 10){
        ssvalue = "0" + ssvalue;
      }
   }
   if (isInt(ssvalue)) {
      ssvalue = ssvalue + ".00";
   }
// mm:ss.ss
   if (mmvalue > 0) {
      mark.value = mmvalue + ":" + ssvalue;

   } else {
// ss.ss
// convert mark of zero to NM
      if (ssvalue == 0) {
         mark.value = no_mark;
      } else {
         mark.value = ssvalue;
      }
   }
   warn(item);
   return true;
}
// ==============================================================

// >>>===========================================================
// warn checkbox
function warn(item) {
   if (item != null) {
// WARNING - select box must be checked too
      if (eval(item.checked) == false) {
         window.alert("The select box must also be checked");
      }
   }
   return true;
}
// ==============================================================

// >>>===========================================================
// file: valid_number.js
// post alarm if parameter is not a valid number
// $Id: script_lib.js,v 1.7 2011/06/20 01:43:42 bill Exp bill $
// author: William.Haynes 2004 william.haynes@usa.net
// =============================================================
function valid_number(n) {
   var value = n.value;

// value is a valid number
   if (isInt(value)) {
// all OK
      return true;
   }

// otherwise, error
   window.alert( "Invalid value (" + value + "), not a number" );
   n.select();
   n.focus();
   return false;
}
// ==============================================================

// >>>===========================================================
// file: ck_4submit.js
// This script limits the click of the submit button to 1
// $Id: script_lib.js,v 1.7 2011/06/20 01:43:42 bill Exp bill $
// 
//  init: with nop call in onload in body tag
//  set & check count with onclick in input/submit tag
//
var submitcnt = 0;
function nop() {
   var n = 0;
   return true;
}
function ck_4submit() {
   if (submitcnt == 0) {
      submitcnt++;
      return true;
   } else {
      alert("This form has already been submitted.\n\nIf you used the BACK button on your browser to return to this page to fix an error, you will need to REFRESH this page before it can be submitted again.\n\nThank you.");
      return false;
   }
}
//
// ==============================================================
// >>>===========================================================
// file: count_checkboxes.js
// count_checkedboxes(form) return # checkboxes that have been checked
// $Id: script_lib.js,v 1.7 2011/06/20 01:43:42 bill Exp bill $
// =========================================================================
function count_checkedboxes(form) {
   var count = 0;
   var i;

   for (i=0; i<form.length; i++) {
      if ( (form.elements[i].type == "checkbox") &&
           (eval(form.elements[i].checked) == true) ) {
         count = count + 1;
      }
   }
   return count;
}
// ==============================================================

/* >>>===========================================================
 * file: count_matching_checkedboxes.js
 * count_matching_checkedboxes(form,pattern)
 * return # checkboxes that have been checked that match the pattern
 * $Id: script_lib.js,v 1.7 2011/06/20 01:43:42 bill Exp bill $
 */
function count_matching_checkedboxes(form, pattern) {
   var count = 0;
   var i;
   var evalue;
   var match;

// confirm("pattern=" + pattern);

   for (i=0; i<form.length; i++) {

// examine and count checkboxes
      if ( (form.elements[i].type == "checkbox") &&
           (eval(form.elements[i].checked) == true) ) {

// count only the checked boxes matching the pattern
         evalue = form.elements[i].value;
         match = evalue.match(pattern);
         if (match != null) {
//          confirm("match event=" + evalue);
            count = count + 1;
         }
      }
   }
   return count;
}
/* >>>=======================================
 * file: fee2charge.js
 * author: William Haynes 2009 William.Haynes@usa.net
 */
function fee2charge(netamt,ccamt,rate) {
   var netvalue = netamt.value;
   var value = 0;
   var xfee = 1;

// make sure all values are numeric
   rate *= 1;

// don't do anything if value is null
    netvalue = mkNull0(netvalue);

   if (! isInt(netvalue) && ! isFloat(netvalue)) {
      ccamt.value = 0;
      alert("The entry fee amount, "+netvalue+", is not a numeric value." );
      netamt.value = 0;
      return true;
   }

   if ( netvalue == 0 ) {
      ccamt.value = 0;
      alert("The entry fee amount is zero." );
      netamt.value = 0;
      return true;
   }

// formula
// determine the transaction fee
   netvalue *= 1;

   value = netvalue + xfee;
   value *= rate;
   value += netvalue + xfee

// round up to next $ amount
   value = Math.ceil(value);
   ccamt.value = value;
   return true;
}
// End fee2charge
// ==============================================================

function chgfocus(field) {
   field.focus();
   return true;
}
// End chgfocus
// ==============================================================

/* >>>=======================================
 * function: changeDisplayState - toggle appearance of code
 * in the document
 */
 function changeDisplayState (id,state) {
    var e=document.getElementById(id);
    e.style.display = state;
 }
// End changeDisplayState
// ==============================================================

/* >>>=======================================
 * function sameAs
 * copy value from 1 element to another
 */
 function sameAs(idChecked,fromId,toId) {
    var checked = document.getElementById(idChecked).checked;

    if (checked) {
       document.getElementById(toId).value = 
          document.getElementById(fromId).value;
    } else {
       document.getElementById(toId).value = '';
    }
    return true;
 }
// End sameAs
// ==============================================================

/* >>>=======================================
 * function copy2to1
 * copy value of 2 fields into 1
 */
 function copy2to1(idChecked, fromId1, fromId2, toId) {
    var checked = document.getElementById(idChecked).checked;

    if (checked) {
       document.getElementById(toId).value = 
          document.getElementById(fromId1).value + ' ' +
	  document.getElementById(fromId2).value;
    } else {
       document.getElementById(toId).value = '';
    }
    return true;
 }
// End copy2to1
// ==============================================================

/* >>>=======================================
 * function ckSameEvents
 * select the same events for Females as selected for Males
 */
 function ckSameEvents(idChecked,entryRules,nEvents) {
    var checked = document.getElementById(idChecked).checked;
    var rules = entryRules;
    var idM = 'M' + rules + '_e';
    var idF = 'F' + rules + '_e';
    var ixM;
    var ixF;
    var i;

    if (checked) {
       for (i=1; i<nEvents; i++) {
          ixM = idM + i;
          ixF = idF + i;
	  if (document.getElementById(ixM).checked) {
	     document.getElementById(ixF).checked = true;
	  } else {
	     document.getElementById(ixF).checked = false;
          }
       }
       
    } else {
       for (i=1; i<nEvents; i++) {
          ixF = idF + i;
          document.getElementById(ixF).checked = false;
       }
    }
    return true;
 }
// End ckSameEvents
// ==============================================================
/* >>>===========================================================
 * function: changeIfChecked
 */
 function changeIfChecked(idChecked,id2Change) {
    var checked = document.getElementById(idChecked).checked;

    if (checked) {
       changeDisplayState(id2Change,'block');
    } else {
       changeDisplayState(id2Change,'none');
    }
    return true;
 }
// End changeIfChecked
// ==============================================================
/* >>>===========================================================
 * function: hideIfChecked
 */
 function hideIfChecked(idChecked,id2Change) {
    var checked = document.getElementById(idChecked).checked;

    if (checked) {
       changeDisplayState(id2Change,'none');
    } else {
       changeDisplayState(id2Change,'block');
    }
    return true;
 }
// End hideIfChecked
// ==============================================================


/* >>>===========================================================
 * function: unCheckall(formId)
 */
 function unCheckAll(formId) {
    var i;
    for (i=0; i<document.getElementById(formId).elements.length; i++) {
       document.getElementById(formId).elements[i].checked = false;
    }
    return true;
 }
// End unCheckAll
// ==============================================================
/* >>>=======================================
 * function showImportOptions
 * display/hide import option info
 */
 function showImportOptions(id){
    var elementValue=document.getElementById(id).value;

    switch (elementValue) {
// coacho on-line archive .csv
    case 'coarc':
       changeDisplayState('importSource','none');
       changeDisplayState('archiveSource','block');
       break;
// coacho.csv
    case 'cocsv':
// hytek .csv
    case 'tmcsv':
// hytek .tcl
    case 'tmtcl':
// hytek .zip
    case 'tmzip':
// tfrrs.csv
    case 'tfrrscsv':
       changeDisplayState('importSource','block');
       changeDisplayState('archiveSource','none');
       break;
// usatf
    case 'usatf':
       changeDisplayState('importSource','none');
       changeDisplayState('archiveSource','none');
    }
    return true;
}
// End  showImportOptions
// ==============================================================
// >>>===========================================================
// file: clickNcopy.js
// This script and many more are available free online at
// The JavaScript Source :: http://javascript.internet.com
// Created by: Will Bontrager :: http://www.willmaster.com/ 
// Modified by: William Haynes
//
// use onclick="clickNcopy(this.form.from-field,this.form.to-field);"
//
function clickNcopy(from,to) {
  if(from.checked == true) {
    to.value = from.value;
    /* If more fields are used, just expand the parameters
       above to include the additional fields. */
  } else {
    to.value = "";
  }
}
// End  clickNcopy.js
// ==============================================================
// >>>===========================================================
//
// use onclick="clickNset(this.form.from-field,this.form.to-field,value);"
//
function clickNset(from,to,value) {
  if(from.checked == true) {
    to.value = value;
    /* If more fields are used, just expand the parameters
       above to include the additional fields. */
  } else {
    to.value = "";
  }
}
// End  clickNset.js
// ==============================================================
// >>>===========================================================
// file: ck_nboxes.js
// This script checks n boxes on an entry form
// $Id: script_lib.js,v 1.7 2011/06/20 01:43:42 bill Exp bill $
// author: William Haynes 2011 William.Haynes@usa.net
// 
// <input type=button id="ckboxes" name="ckboxes" value="All"
//  onClick="ck_boxes(document.myform,fieldname)" />
//

function ck_nboxes(form) {
   var i;
   var fieldvalue = true;
   var nboxes = 0;
   var nchecked = 0;

   for (i=0; i<form.length; i++) {
      if (form.elements[i].type == "checkbox") {
         nboxes += 1;
         if (eval(form.elements[i].checked) == true) {
            nchecked += 1;
         }    
      }
   }
   if (nboxes == nchecked) {
      fieldvalue = false;
   }

// check or un-check all boxes
   for (i=0; i<form.length; i++) {
      if (form.elements[i].type == "checkbox") {
         form.elements[i].checked = fieldvalue;
      }
   }
   return true;
}
// ==============================================================

