// Common Javascript Functions
// started 03/11/2003 by Herb Rubin

var saved_confirm_data = new Array();

function rollover(target,imgName) {
	if (document.images) {
		imgchange = 'images/' + imgName;
		document[target].src = imgchange;
		}
}

function smartdate (t) {
  // assist user in entering a valid date (mm-dd-yyyy)
  // This is a required field.
  // It converts 4 digit years to 2 digit years. It looks for
  // leap year correctness and defaults to today's date.
  var result;
  var pattern_1d = /^\d$/;
  var pattern_2d = /^\d\d$/;
  var pattern_3ds = /^(\d)(\d\d)$/; // assume m-dd
  var pattern_4d = /^\d\d\d\d$/;
  var pattern_4ds = /^(\d\d)(\d\d)$/; // assume mm-dd
  var pattern_6d = /^(\d\d)(\d\d)(\d\d)$/;
  var pattern_8d = /^(\d\d)(\d\d)(\d\d\d\d)$/;
  var datefield = t.value;
  
  // convert slashes and dots to dashes
  if (datefield != null) {
      datefield = (datefield.split("/")).join("-");
      datefield = (datefield.split(".")).join("-");
  } else {
     t.value = get_date("0");
     return true;
  }
  
  // now check for no separators
  result = datefield.match(pattern_8d);
  if (result != null) datefield = result[1] + '-' + result[2] + '-' + result[3];
  result = datefield.match(pattern_6d);
  if (result != null) datefield = result[1] + '-' + result[2] + '-' + result[3];
  result = datefield.match(pattern_3ds);
  if (result != null) datefield = '0' + result[1] + '-' + result[2] + '-' + get_year();;
  result = datefield.match(pattern_4ds);
  if (result != null) datefield = result[1] + '-' + result[2] + '-' + get_year();
  
  // Assume now its properly formatted with dashes. Split into 3 fields: mm dd yyyy
  var a = datefield.split("-");
  if ((a[0] == "") || (a[0] == null)) a[0] = get_chaser_month();
  result = a[0].match(pattern_1d); // one digit month test
  if (result != null) a[0] = "0" + a[0]; // matched one digit month
  result = a[0].match(pattern_2d); // 2 digit month test
  if (result == null) a[0] = get_chaser_month(); // not 2 digit month, use today
  if ((a[0] > 12) || (a[0] < 1)) a[0] = get_chaser_month();
  
  if ((a[1] == "") || (a[1] == null)) a[1] = get_chaser_day();
  result = a[1].match(pattern_1d); // 1 digit day test
  if (result != null) a[1] = "0" + a[1]; // matched 1 digit day
  result = a[1].match(pattern_2d); // 2 digit day test
  if (result == null) a[1] = get_chaser_day(); // not 2 digit day, use today
  if (a[1] < 1) a[1] = get_chaser_day();
  
  if ((a[2] == "") || (a[2] == null)) a[2] = get_year();
  result = a[2].match(pattern_2d); // 2 digit year test
  if (result == null) {
      result = a[2].match(pattern_4d); // 4 digit year test
      if (result == null) { 
          // not 2 or 4 digit year, use this year
          a[2] = get_year();
      }
  } else {
      // 2 digit year, convert to 4 digit year
      a[3] = get_year();
      a[3] = a[3].substr(0,2); // get century
      a[2] = a[3] + a[2]; // convert to 4 digits
  }

  var lastdayofmonth = get_last_day(a[0],a[2]);
  if (a[1] > lastdayofmonth) a[1] = lastdayofmonth;
  
  t.value = a[0] + "-" + a[1] + "-" + a[2];
  return true;
}

function opt_smartdate (t) {
  // assist user in entering a valid date (mm-dd-yyyy)
  // but a blank is allowed. This is the optional part!
  // It converts 4 digit years to 2 digit years. It looks for
  // leap year correctness and defaults to today's date.
  var result;
  var pattern_1d = /^\d$/;
  var pattern_2d = /^\d\d$/;
  var pattern_3ds = /^(\d)(\d\d)$/; // assume m-dd
  var pattern_4d = /^\d\d\d\d$/;
  var pattern_4ds = /^(\d\d)(\d\d)$/; // assume mm-dd
  var pattern_6d = /^(\d\d)(\d\d)(\d\d)$/;
  var pattern_8d = /^(\d\d)(\d\d)(\d\d\d\d)$/;
  var pattern_garbage = /^([a-zA-Z]+)$/;
  var datefield = t.value;

  // convert dashes and dots to dashes to slashes
  if (datefield != null) {
      datefield = (datefield.split("-")).join("/");
      datefield = (datefield.split(".")).join("/");
  } else {
     t.value = '';
     return true;
  }

  result = datefield.match(pattern_garbage);
  if (result != null) {
      t.value = '';
      return true;
  }
  
  result = datefield.match(pattern_8d);
  if (result != null) datefield = result[1] + '/' + result[2] + '/' + result[3];
  result = datefield.match(pattern_6d);
  if (result != null) datefield = result[1] + '/' + result[2] + '/' + result[3];
  result = datefield.match(pattern_3ds);
  if (result != null) datefield = '0' + result[1] + '/' + result[2] + '/' + get_year();
  result = datefield.match(pattern_4ds);
  if (result != null) datefield = result[1] + '/' + result[2] + '/' + get_year();

  // Assume now its properly formatted with dashes. Split into 3 fields: mm dd yyyy
  var a = datefield.split("/");
  if ((a[0] == "") || (a[0] == null)) a[0] = get_chaser_month();
  result = a[0].match(pattern_1d); // one digit month test
  if (result != null) a[0] = "0" + a[0]; // matched one digit month
  result = a[0].match(pattern_2d); // 2 digit month test
  if (result == null) a[0] = get_chaser_month(); // not 2 digit month, use today
  if ((a[0] > 12) || (a[0] < 1)) a[0] = get_chaser_month();
  
  if ((a[1] == "") || (a[1] == null)) a[1] = get_chaser_day();
  result = a[1].match(pattern_1d); // 1 digit day test
  if (result != null) a[1] = "0" + a[1]; // matched 1 digit day
  result = a[1].match(pattern_2d); // 2 digit day test
  if (result == null) a[1] = get_chaser_day(); // not 2 digit day, use today
  if (a[1] < 1) a[1] = get_chaser_day();
  
  if ((a[2] == "") || (a[2] == null)) a[2] = get_year();
  result = a[2].match(pattern_2d); // 2 digit year test
  if (result == null) {
      result = a[2].match(pattern_4d); // 4 digit year test
      if (result == null) { 
          // not 2 or 4 digit year, use this year
          a[2] = get_year();
      }
  } else {
      // 2 digit year, convert to 4 digit year
      a[3] = get_year();
      a[3] = a[3].substr(0,2); // get century
      a[2] = a[3] + a[2]; // convert to 4 digits
  }

  var lastdayofmonth = get_last_day(a[0],a[2]);
  if (a[1] > lastdayofmonth) a[1] = lastdayofmonth;
  
  t.value = a[0] + "/" + a[1] + "/" + a[2];
  return true;
}

function smart_full_date (t) {
  // assist user in entering a valid date (mm-dd-yyyy)
  // This is a required field.
  // It converts 4 digit years to 2 digit years. It looks for
  // leap year correctness and defaults to today's date.
  // Then propagate this date to all LDOC fields for check on c3s_confirmwa.php
  var result, r, i, rowcount;
  var pattern_1d = /^\d$/;
  var pattern_2d = /^\d\d$/;
  var pattern_3ds = /^(\d)(\d\d)$/; // assume m-dd
  var pattern_4d = /^\d\d\d\d$/;
  var pattern_4ds = /^(\d\d)(\d\d)$/; // assume mm-dd
  var pattern_6d = /^(\d\d)(\d\d)(\d\d)$/;
  var pattern_8d = /^(\d\d)(\d\d)(\d\d\d\d)$/;
  var datefield = t.value;
  
  // convert slashes and dots to dashes
  if (datefield != null) {
      datefield = (datefield.split("/")).join("-");
      datefield = (datefield.split(".")).join("-");
  } else {
      // null means optional date no force to today
      return true;
  }
  if (datefield == '') {
      // blank means optional date no force to today
      return true;  
  }
  
  // now check for no separators
  result = datefield.match(pattern_8d);
  if (result != null) datefield = result[1] + '-' + result[2] + '-' + result[3];
  result = datefield.match(pattern_6d);
  if (result != null) datefield = result[1] + '-' + result[2] + '-' + result[3];
  result = datefield.match(pattern_3ds);
  if (result != null) datefield = '0' + result[1] + '-' + result[2] + '-' + get_year();
  result = datefield.match(pattern_4ds);
  if (result != null) datefield = result[1] + '-' + result[2] + '-' + get_year();
  
  // Assume now its properly formatted with dashes. Split into 3 fields: mm dd yyyy
  var a = datefield.split("-");
  if ((a[0] == "") || (a[0] == null)) a[0] = get_chaser_month();
  result = a[0].match(pattern_1d); // one digit month test
  if (result != null) a[0] = "0" + a[0]; // matched one digit month
  result = a[0].match(pattern_2d); // 2 digit month test
  if (result == null) a[0] = get_chaser_month(); // not 2 digit month, use today
  if ((a[0] > 12) || (a[0] < 1)) a[0] = get_chaser_month();
  
  if ((a[1] == "") || (a[1] == null)) a[1] = get_chaser_day();
  result = a[1].match(pattern_1d); // 1 digit day test
  if (result != null) a[1] = "0" + a[1]; // matched 1 digit day
  result = a[1].match(pattern_2d); // 2 digit day test
  if (result == null) a[1] = get_chaser_day(); // not 2 digit day, use today
  if (a[1] < 1) a[1] = get_chaser_day();
  
  if ((a[2] == "") || (a[2] == null)) a[2] = get_year();
  result = a[2].match(pattern_2d); // 2 digit year test
  if (result == null) {
      result = a[2].match(pattern_4d); // 4 digit year test
      if (result == null) { 
          // not 2 or 4 digit year, use this year
          a[2] = get_year();
      }
  } else {
      // 2 digit year, convert to 4 digit year
      a[3] = get_year();
      a[3] = a[3].substr(0,2); // get century
      a[2] = a[3] + a[2]; // convert to 4 digits
  }

  var lastdayofmonth = get_last_day(a[0],a[2]);
  if (a[1] > lastdayofmonth) a[1] = lastdayofmonth;
  
  t.value = a[0] + "-" + a[1] + "-" + a[2];
  no_future(t);
  // propagate value to all fields in this check on c3s_confirmwa.php
  for (i = 0; i < t.form.elements.length; i++) if (t == t.form.elements[i]) break;
  rowcount = t.form.elements[i+1].value;
  i = i + 3;  // line up with first row LDOC
  for (r = 0; r < rowcount; r++) {
      t.form.elements[i].value = t.value;
      i = i + 6;
  }
  return true;
}

function get_chaser_month() {
   // return string of 2 digit current month
                                                                                
   var d = new Date();
   var mon = d.getMonth() + 1;
   if (mon < 10) mon ="0" + mon;
   return mon.toString();
}

function get_chaser_day() {
   // return string of 2 digit current day in month
                                                                                
   var d = new Date();
   var day = d.getDate();
   if (day < 10) day = "0" + day;
   return day.toString();
}

function get_chaser_year() {
   // return string of 2 digit current year
                                                                                
   var d = new Date();
   var year = d.getFullYear();
   year = year.toString();
   year = year.substr(2,2);
   return year.toString();
}
                                                                                
function get_chaser_fullyear() {
   // return string of 4 digit current year
                                                                                
   var d = new Date();
   var year = d.getFullYear();
   year = year.toString();
   return year.toString();
}

function get_chaser_mmccyy(days) {
   // return date string mm-ccyy with number of "days" into future from today
   // if days = 1 then return tommorrows date
   
   var d = new Date();
   if (days != "") {
      var milli = d.getTime();
      days = days * 24 * 60 * 60 * 1000 + milli; // milliseconds
      d.setTime(days);
   }
   var mon = d.getMonth() + 1;
   var year = d.getFullYear();
   year = year.toString();
   if (mon < 10) mon ="0" + mon;
   return mon + "-" + year;
}

function get_date(days) {
// Give a number of days into the future, return the actual date
// in DD-MM-YY format
// for example: if days = 1 then return tommorrows date
   
   var d = new Date();
   if (days != "") {
      var milli = d.getTime();
      days = days * 24 * 60 * 60 * 1000 + milli; // milliseconds
      d.setTime(days);
   }
   var day = d.getDate();
   var mon = d.getMonth() + 1;
   var year = d.getFullYear();
   year = year.toString();
   if (day < 10) day = "0" + day;
   if (mon < 10) mon ="0" + mon;
   return mon + "-" + day + "-" + year;
   // return days;
}

function get_month() {
   // return string of 2 digit current month
   
   var d = new Date();
   var mon = d.getMonth() + 1;
   if (mon < 10) mon ="0" + mon;
   return mon.toString();
}

function get_day() {
   // return string of 2 digit current day in current month
   
   var d = new Date();
   var day = d.getDate();
   if (day < 10) day = "0" + day;
   return day.toString();
}

function get_year() {
   // return string of 4 digit current year
   
   var d = new Date();
   var year = d.getFullYear();
   return year.toString();
}

function get_last_day(month, year) {
   // return last day of the month
   
   var daylist = [31,28,31,30,31,30,31,31,30,31,30,31];
   if (year == 0) { 
      daylist[1] = 29; // century leap year
   } else {
      var leap = year / 4;
      var leap2 = Math.floor(leap);
      if (leap == leap2) daylist[1] = 29;  // regular leap year
   }
   month = month - 1;
   var lastday = daylist[month];
   return lastday.toString();
}

function trim_decimal_places(num) {
  /**********************************
   * Ensure always 2 decimal places *
   **********************************/
   num = num + '00';
   var nums = num.substring(0,2);
   return nums.toString();
}

function smartmoney (t) {
 /*********************************************
  * assist user in entering a + dollar amount *
  * This is a required field.                 *
  * It chops off more than 2 decimal places.  *
  * If garbage then return 0.00               *
  *********************************************/
  var result;
  var pattern_close  = /(\d+)\.(\d+)/;
  var pattern_close2 = /(\d+)/;
  var datefield = t.value;
  
  result = datefield.match(pattern_close);
  if (result != null) {
     /**************************************************
      * Found a number with decimal places, so trim it *
      * down to 2 decimal places.                      *
      *************************************************/
      t.value = result[1] + "." + trim_decimal_places(result[2]);
      return true;
  }
  
  result = datefield.match(pattern_close2);
  if (result != null) {
     /******************************************
      * Append decimal place since its missing.*
      ******************************************/
      t.value = result[1] + "." + "00";
      return true;
  }

  t.value = '0.00';
  return true;
}

function opt_smartmoney (t) {
 /*********************************************
  * assist user in entering a + dollar amount *
  * It chops off more than 2 decimal places.  *
  * If garbage then return null (blank field) *
  *********************************************/
  var result;
  var pattern_close  = /(\d+)\.(\d+)/;
  var pattern_close2 = /(\d+)/;
  var datefield = t.value;

  result = datefield.match(pattern_close);
  if (result != null) {
     /**************************************************
      * Found a number with decimal places, so trim it *
      * down to 2 decimal places.                      *
      *************************************************/
      t.value = result[1] + '.' + trim_decimal_places(result[2]);
      return true;
  }

  result = datefield.match(pattern_close2);
  if (result != null) {
     /******************************************
      * Append decimal place since its missing.*
      ******************************************/
      t.value = result[1] + '.00';
      return true;
  }

  t.value = '';
  return true;
}

function opt_smartdollars (t) {
 /*********************************************
  * assist user in entering a + dollar amount *
  * It chops off more than 2 decimal places.  *
  * If garbage then return null (blank field) *
  *********************************************/
  var newfield = '';
  var i, j, result;
  var pattern_close  = /(\d+)\.(\d+)/;
  var moneyfield = t.value;

  if (moneyfield == "") return true;

 /******************************
  * strip all non-digits chars *
  ******************************/
  for(i = 0; i < moneyfield.length; i++) {
       letter = moneyfield.charAt(i);
       if (letter.match(/\d/)) newfield = newfield + letter;
       if (letter.match(/\./)) newfield = newfield + letter;
  }
  moneyfield = newfield;

  result = moneyfield.match(pattern_close);
  if (result != null) {
     /**************************
      * Chop off decimal place *
      **************************/
      moneyfield = result[1];
  }

 /*****************
  * put in commas *
  *****************/
  newfield = '';
  j=0;
  for(i = moneyfield.length - 1; i >= 0; i--) {
      j++;
      letter = moneyfield.charAt(i);
      newfield = letter + newfield;
      if (j % 3 == 0) {
          if (i != 0) newfield = ',' + newfield;
      }
  }
  moneyfield = newfield;
 
  t.value = moneyfield;
  return true;
}

function convert_enter (field, event) {
/****************************************************************************
 * cause enter key to act like tab key and avoid it causing form submission *
 * usage: onkeypress="convert_enter(this,event);"                           *
 ****************************************************************************/
  var keycode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
  
  if (keycode == 13) {
     /**********************************
      * set i to field number in form, *
      * then set focus to next one.    *
      **********************************/
      var i;
      for (i = 0; i < field.form.elements.length; i++) if (field == field.form.elements[i]) break;
      i = (i + 1) % field.form.elements.length;  // wraparound to first
      field.form.elements[i].focus();
      return false;
  } else return true;
}

function disable_enter_key() {
/****************************************************
 * cause enter key to not be recognized as an entry *
 * usage: onkeypress="disable_enter_key();"         *
 ****************************************************/
   if (window.event.srcElement.type != "submit") {
      if (window.event.keyCode == 13) window.event.keyCode = 0; 
   }
} 

function move_along (field) {
/************************************************
 * automatically shift focus to next tab field  *
 * and allow for skipping up to 4 hidden fields *
 * usage: onclick="move_along(this);"           *
 ************************************************/
   var i;
   for (i = 0; i < field.form.elements.length; i++) {
     if (field == field.form.elements[i]) break;
   }
   i = (i + 1) % field.form.elements.length;  // wraparound to first
   if (field.form.elements[i].type == "hidden") {
       i = (i + 1) % field.form.elements.length;  // skip this hidden field
   }
   if (field.form.elements[i].type == "hidden") {
       i = (i + 1) % field.form.elements.length;  // skip this hidden field
   }
   if (field.form.elements[i].type == "hidden") {
       i = (i + 1) % field.form.elements.length;  // skip this hidden field
   }
   if (field.form.elements[i].type == "hidden") {
       i = (i + 1) % field.form.elements.length;  // skip this hidden field
   }
   field.form.elements[i].focus();
   return;
}

function smartinteger (t) {
 /****************************************
  * assist user in entering a + integer  *
  ****************************************/
  var result;
  var pattern_close = /(\d+)/;
  var datefield = t.value;
  
  result = datefield.match(pattern_close);
  if (result != null) {
     /***************************
      * Return positive integer *
      ***************************/
      t.value = result[1];
      return true;
  }
  
  t.value = '0';
  return true;
}

function opt_smartinteger (t) {
 /****************************************
  * assist user in entering a + integer  *
  * If garbage return null (blank field) *
  ****************************************/
  var result;
  var pattern_close = /(\d+)/;
  var datefield = t.value;
  
  result = datefield.match(pattern_close);
  if (result != null) {
     /***************************
      * Return positive integer *
      ***************************/
      t.value = result[1];
      return true;
  }
  
  t.value = '';
  return true;
}

function smartsigned (t) {
 /**************************************************
  * assist user in entering a + or - dollar amount *
  * This is a required field.                      *
  * It chops off more than 2 decimal places.       *
  * If garbage then it returns 0.00                *
  **************************************************/
  var result;
  var pattern_close  = /(\d+)\.(\d+)/;
  var pattern_close2 = /(\d+)/;
  var pattern_close3 = /(-\d+)\.(\d+)/;
  var pattern_close4 = /(-\d+)/;
  var datefield = t.value;
  
  result = datefield.match(pattern_close3);
  if (result != null) {
     /*************************************************
      * Found negative number with decimal places, so *
      * trim down to 2 digit decimal place.           *
      *************************************************/
      t.value = result[1] + "." + trim_decimal_places(result[2]);
      return true;
  }
  
  result = datefield.match(pattern_close4);
  if (result != null) {
     /******************************************
      * Found negative number with no decimals.*
      * Append decimal place since its missing.*
      ******************************************/
      t.value = result[1] + "." + "00";
      return true;
  }
  result = datefield.match(pattern_close);
  if (result != null) {
     /*************************************************
      * Found positive number with decimal places, so *
      * trim down to 2 digits in decimal places.      *
      *************************************************/
      t.value = result[1] + '.' + trim_decimal_places(result[2]);
      return true;
  }
  
  result = datefield.match(pattern_close2);
  if (result != null) {
     /*******************************************
      * Found a positive number with no decimal.*
      * Append decimal place since its missing. *
      *******************************************/
      t.value = result[1] + '.00';
      return true;
  }

  t.value = '0.00';
  return true;
}

function opt_smartsigned (t) {
  // assist user in entering a + or - dollar amount
  // This is a required field.
  // It chops off more than 2 decimal places.
  var result;
  var pattern_close  = /(\d+)\.(\d+)/;
  var pattern_close2 = /(\d+)/;
  var pattern_close3 = /(-\d+)\.(\d+)/;
  var pattern_close4 = /(-\d+)/;
  var datefield = t.value;
  
  result = datefield.match(pattern_close3);
  if (result != null) {
     /*************************************************
      * Found negative number with decimal places, so *
      * trim down to 2 digit decimal place.           *
      *************************************************/
      t.value = result[1] + '.' + trim_decimal_places(result[2]);
      return true;
  }
  
  result = datefield.match(pattern_close4);
  if (result != null) {
     /******************************************
      * Found negative number with no decimals.*
      * Append decimal place since its missing.*
      ******************************************/
      t.value = result[1] + '.00';
      return true;
  }
  result = datefield.match(pattern_close);
  if (result != null) {
     /*************************************************
      * Found positive number with decimal places, so *
      * trim down to 2 digits in decimal places.      *
      *************************************************/
      t.value = result[1] + '.' + trim_decimal_places(result[2]);
      return true;
  }
  
  result = datefield.match(pattern_close2);
  if (result != null) {
     /*******************************************
      * Found a positive number with no decimal.*
      * Append decimal place since its missing. *
      *******************************************/
      t.value = result[1] + '.00';
      return true;
  }

  t.value = '';
  return true;
}

function are_you_sure(message) {
    return window.confirm(message);
}

function clear_confirm_row (cbox, check, row) {
/*****************************************************************
 * onclick allow the clearing and retrieving of 4 fields of data *
 * that make up a row on the confirm pages.                      *
 * Example HTML:                                                 *
 * <input type="checkbox" name="copy[]"                          *
 *   OnClick="javascript:clear_confirm_row(this, 1, 3);"         *
 *   value="checkbox">                                           *
 *****************************************************************/
 var i;
 
 for (i = 0; i < cbox.form.elements.length; i++) {
     if (cbox == cbox.form.elements[i]) break;
     if (cbox.form.elements[i].name == "sum_total2") {
       /**********************************************
        * End of form data, beginning of hidden data *
        **********************************************/
        break;
     }                 
 }
  
  if (cbox.checked) {
      // user wants to clear the row
      
      saved_confirm_data[check] = new Array(); 
      saved_confirm_data[check][row] = new Array();
      
      //saved_confirm_data[check][row][0] = cbox.form.elements[i-5].value; // ldoc
      saved_confirm_data[check][row][1] = cbox.form.elements[i-4].value; // name
      saved_confirm_data[check][row][2] = cbox.form.elements[i-3].value; // ssn
      saved_confirm_data[check][row][3] = cbox.form.elements[i-2].value; // parid
      saved_confirm_data[check][row][4] = cbox.form.elements[i-1].value; // amount

      //cbox.form.elements[i-5].value = ""; // ldoc
      cbox.form.elements[i-4].value = ""; // name
      cbox.form.elements[i-3].value = ""; // ssn
      cbox.form.elements[i-2].value = ""; // parid
      cbox.form.elements[i-1].value = ""; // amount
  } else {
      // user unchecked the box, so put back the values we saved
      //cbox.form.elements[i-5].value = saved_confirm_data[check][row][0]; // ldoc
      cbox.form.elements[i-4].value = saved_confirm_data[check][row][1]; // name
      cbox.form.elements[i-3].value = saved_confirm_data[check][row][2]; // ssn
      cbox.form.elements[i-2].value = saved_confirm_data[check][row][3]; // parid
      cbox.form.elements[i-1].value = saved_confirm_data[check][row][4]; // amount
   }
}

function focus_confirmwa(check) {
/**************************************
 * change focus to master LDOC field  *
 **************************************/
  var i, rowcount;
  var chkpos = -1;
  for (i = 0; i < document.form1.sum_total.form.elements.length; i++) {
      if (document.form1.sum_total.form.elements[i].type.indexOf("select") != -1) {
         /*******************************
          * Found beginning of a check. *
          *******************************/
          chkpos ++;
          ncppos = i + 2;
          ldocpos = i - 3;
          rowcount = document.form1.sum_total.form.elements[i-1].value * 6 - 1;
          if (chkpos == check) {
              document.form1.sum_total.form.elements[ncppos + rowcount].focus();
              //document.form1.sum_total.form.elements[ncppos].focus();
              document.form1.sum_total.form.elements[ldocpos].focus();
              return true;
          }
      }
      if (document.form1.sum_total.form.elements[i].name == "sum_total2") {
        /**********************************************
         * End of form data, beginning of hidden data *
         **********************************************/
         return true;
      }

  }

}

function strip_nonalpha(text) {
/**************************************
 * formats string for only alpha characters or comma *
 **************************************/
	var newtext = '';
	for(i=0; i<text.length; i++) {
		letter = text.charAt(i);
		if (!letter.match(/\s/)) {
			if (letter.match(/\w/)) {
				if (!letter.match(/\d/))
					newtext = newtext + letter;
			}	
		}
		if (letter == ',')
			newtext = newtext + letter;
	}
	return newtext.toUpperCase();
}

function strip_nondigit(text) {
/**********************************
 * formats string for only digits *
 **********************************/
        var newtext = '';
        for(i=0; i<text.length; i++) {
            letter = text.charAt(i);
            if (letter.match(/\d/)) newtext = newtext + letter;
        }
        return newtext;
}

function no_future(entered, cutoff) {
/************************************************
 * Makes sure date entered is not in the future *
 * The reason the cuttoffdate is passed in is   *
 * be sure the server date is used.             *
 ************************************************/
        if (entered.value == '') { return true; }
        smartdate(entered); // clean date
        var tempdate, cutoffdate;
        tempdate = entered.value;
        var userdate = new Date(tempdate);
        if (isNaN(cutoff) == false) {
            // Server supplied cutoff date
            cutoffdate = new Date(cutoff);
        } else {
            // Browser supplied cutoff date
            cutoffdate = new Date();
        }
//        tempdate = cutoffdate.toString();
                                                                                                                  
        if (userdate > cutoffdate) {                                                                              
            var monthnumber = cutoffdate.getMonth() + 1;
            var day         = cutoffdate.getDate();
            var year        = cutoffdate.getYear();

            if (year < 2000) { 
                year = year + 1900;
            }
            entered.value = monthnumber + "-" + day + "-" + year;
        }       
        smartdate(entered); // clean date
        return true;
}

function checkAll(formptr) {
/***************************************
 * ensure all fields not empty in form *
 ***************************************/
  var i;
  
  for (i = 4; i < form1.elements.length; i++) {
      if (form1.elements[i].value == null) {
         /*********************
          * found empty field *
          *********************/
          window.alert('All fields are required to confirm and continue');
          return false;
      }
      if (form1.elements[i].value == "") {
         /*********************
          * found empty field *
          *********************/
          window.alert('All fields are required to confirm and continue');
          return false;
      }
  }
  return true;
}

function PopUpCalculator() {
  window.open('/c3scan/calculator.php','','height=400,width=200');
}

function PopUpSpecialCalculator(formname, field) {
 /***********************************************
  * Create a popup calculator that will update  *
  * the field that preceded it in parent window *
  ***********************************************/
  var url = '/c3scan/calculator.php?f=' + formname + '&i=' + field;
  var child = window.open(url,'','height=450,width=200');
}

function select_all_checkboxes(myobj) {
 var i;
 for (i = 0; i < myobj.form.elements.length; i++) {
     if (myobj.form.elements[i].type == "checkbox") {
         if (myobj.form.elements[i].checked) {
             myobj.form.elements[i].checked = false;
         } else {
             myobj.form.elements[i].checked = true;
         }
     }                 
 }
}

function smart_cc_numbers (t) {
 /**************************************
  * assist user in entering a 16 digit *
  * credit card number, spaces and -   *
  * are stripped as a courtesy.        *
  **************************************/
  var result;
  var pattern     = /^(\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d\d)$/;
  var testpattern = /^(4007000000027)$/;
  var numfield    = t.value;
                                                                                                             
  if (numfield != null) {
      numfield = (numfield.split("-")).join(" ");
      numfield = (numfield.split(" ")).join("");
  }
                                                                                                             
  result = numfield.match(testpattern);
  if (result != null) {
     /***************************
      * Return positive integer *
      ***************************/
      t.value = result[1];
      return true;
  }
                                                                                                             
  result = numfield.match(pattern);
  if (result != null) {
     /***************************
      * Return positive integer *
      ***************************/
      t.value = result[1];
      return true;
  }
                                                                                                             
  t.value = '';
  return true;
}

function opt_smartzip (t) {
 /*********************************************
  * assist user in entering a legal zipcode   *
  * If garbage then return null (blank field) *
  *********************************************/
  var result;
  var pattern_close  = /^(\d\d\d\d\d)\-(\d\d\d\d)$/;
  var pattern_close2 = /^(\d\d\d\d\d)$/;
  var datefield = t.value;
                                                                                                             
  result = datefield.match(pattern_close);
  if (result != null) {
     /*********************************************
      * Found 5 digit zip code with 4 digit plus4 *
      *********************************************/
      t.value = result[1] + '-' + result[2];
      return true;
  }
                                                                                                             
  result = datefield.match(pattern_close2);
  if (result != null) {
     /*********************************
      * Found simple 5 digit zip code *
      *********************************/
      t.value = result[1];
      return true;
  }
                                                                                                             
  t.value = '';
  return true;
}

/**
 * Sets/unsets the pointer and marker in browse mode
 *
 * @param   object    the table row
 * @param   interger  the row number
 * @param   string    the action calling this script (over, out or click)
 * @param   string    the default background color
 * @param   string    the color to use for mouseover
 * @param   string    the color to use for marking a row
 *
 * @return  boolean  whether pointer is set or not
 */
function setPointer(theRow, theRowNum, theAction, theDefaultColor, thePointerColor, theMarkColor)
{
    var theCells = null;

    // 1. Pointer and mark feature are disabled or the browser can't get the
    //    row -> exits
    if ((thePointerColor == '' && theMarkColor == '')
        || typeof(theRow.style) == 'undefined') {
        return false;
    }

    // 2. Gets the current row and exits if the browser can't get it
    if (typeof(document.getElementsByTagName) != 'undefined') {
        theCells = theRow.getElementsByTagName('td');
    }
    else if (typeof(theRow.cells) != 'undefined') {
        theCells = theRow.cells;
    }
    else {
        return false;
    }

    // 3. Gets the current color...
    var rowCellsCnt  = theCells.length;
    var domDetect    = null;
    var currentColor = null;
    var newColor     = null;
    // 3.1 ... with DOM compatible browsers except Opera that does not return
    //         valid values with "getAttribute"
    if (typeof(window.opera) == 'undefined'
        && typeof(theCells[0].getAttribute) != 'undefined') {
        currentColor = theCells[0].getAttribute('bgcolor');
        domDetect    = true;
    }
    // 3.2 ... with other browsers
    else {
        currentColor = theCells[0].style.backgroundColor;
        domDetect    = false;
    } // end 3

    // 4. Defines the new color
    // 4.1 Current color is the default one
    if (currentColor == ''
        || currentColor.toLowerCase() == theDefaultColor.toLowerCase()) {
        if (theAction == 'over' && thePointerColor != '') {
            newColor              = thePointerColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.2 Current color is the pointer one
    else if (currentColor.toLowerCase() == thePointerColor.toLowerCase()
             && (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])) {
        if (theAction == 'out') {
            newColor              = theDefaultColor;
        }
        else if (theAction == 'click' && theMarkColor != '') {
            newColor              = theMarkColor;
            marked_row[theRowNum] = true;
        }
    }
    // 4.1.3 Current color is the marker one
    else if (currentColor.toLowerCase() == theMarkColor.toLowerCase()) {
        if (theAction == 'click') {
            newColor              = (thePointerColor != '')
                                  ? thePointerColor
                                  : theDefaultColor;
            marked_row[theRowNum] = (typeof(marked_row[theRowNum]) == 'undefined' || !marked_row[theRowNum])
                                  ? true
                                  : null;
        }
    } // end 4

    // 5. Sets the new color...
    if (newColor) {
        var c = null;
        // 5.1 ... with DOM compatible browsers except Opera
        if (domDetect) {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].setAttribute('bgcolor', newColor, 0);
            } // end for
        }
        // 5.2 ... with other browsers
        else {
            for (c = 0; c < rowCellsCnt; c++) {
                theCells[c].style.backgroundColor = newColor;
            }
        }
    } // end 5

    return true;
} // end of the 'setPointer()' function

function opt_smartphone (t) {
  // assist user in entering a valid US phone number
  // but a blank is allowed. This is the optional part!
  var result;
  var pattern1 = /^(\d\d\d)(\d\d\d)(\d\d\d\d)$/;
  var phonefield = strip_nondigit(t.value);
                                                                                                                 
  result = phonefield.match(pattern1);
  if (result != null) {
      t.value = "(" + result[1] + ") " + result[2] + "-" + result[3];
      return true;
  } else {
      t.value = "";
      return true;
  }
}

function popUp(URL) {
  day = new Date();
  id = day.getTime();
  eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=800,height=600,left = 112,top = 84');");
}

function quickSignupValidation(theform) {
  // validate and process the signup via AJAX

  if (theform.userid.value == "") {
      alert("Please select a userid");
      theform.userid.focus();
      return false;
  }
  if (theform.password.value == "") {
      alert("Password is a required field");
      theform.password.focus();
      return false;
  }
  if (theform.password2.value == "") {
      alert("Please enter your password again in the Repeat Password box");
      theform.password2.focus();
      return false;
  }
  if (theform.email.value == "") {
      alert("Email is a required field");
      theform.email.focus();
      return false;
  }
  if (theform.city.value == "") {
      alert("City is a required field");
      theform.city.focus();
      return false;
  }
  if (theform.state.options[theform.state.selectedIndex].value == "") {
      alert("State is a required field");
      theform.state.focus();
      return false;
  }
  if (theform.iagree.checked == false) {
      alert("You must agree to the terms");
      theform.iagree.focus();
      return false;
  }
  if (theform.password.value != theform.password2.value) {
      alert("Both password fields do not match");
      theform.password.focus();
      return false;
  }

  // submit to server now via AJAX allow <script></script> sections to be executed upon return.
  var postfields;

  postfields = Form.serialize(theform);

  var opt = {
      method: 'post',
      //parameters: '',
      postBody: postfields,
      evalScripts: true,
      onSuccess: function(t) {
          //document.getElementById('it').innerHTML= t.responseText;
      },
      on404: function(t) {
          alert('Error 404: location "' + t.statusText + '" was not found.');
      },
      onFailure: function(t) {
          alert('Error ' + t.status + ' -- ' + t.statusText);
      }
  }
  new Ajax.Updater('error', 'ajax_signup.php', opt);
  return false; // don't let form submit itself
}
