// example use would be onBlur="(this.form, this)"
function calc(form, fld)
{
  var old_val = fld.value;

  if ( !isDollar(fld,"",1) )
    { alert('the field ('+ old_val +') is not a valid entry'); }

  form.total.value = calcCols(form, "row", 1, 5);
  isDollar(form.total,"0.00",2);
}

// field names to be automatically totaled should be of the form
// hours1, hours2, hours3, hours4, hours5, hours6.
// if we wanted 1 decimal place and the total was to be place in "total_hours"
// we'd have the example:
// example use  -> onBlur="(this.form, this, 'hours', 1, 6, 'total_hours', 1, 2)"
// form 	-> the form
// fld	 	-> the field this call is associated with
// fldNamePrefix -> the common beginning of the fields to be totaled (characters)
// startSuffix	-> the suffix of the first fieldname (integer)
// endSuffix	-> the suffix of the last fieldname (integer)
// totalName	-> the field name of the field where the total should be placed
// precision	-> how many decimal places
// totalPrecision -> how many decimal places for the total value
function genericCalc(form, fld, fldNamePrefix, startSuffix, 
   endSuffix, totalName, precision, totalPrecision)
{
  var old_val = fld.value;

  if ( !isDollar(fld,"",precision) )
    { alert('the field ('+ old_val +') is not a valid entry'); }

  form.elements[totalName].value = calcCols(form, fldNamePrefix, startSuffix, endSuffix);
  isDollar(form.elements[totalName],"0",totalPrecision);

}

// form -> the form
// colName -> the name of the row element minus the number
// i -> the initial number (colName1, colName2... :i would be 1)
// j -> the ending number (...colName7, colName8 :j would be 8)
function calcCols(form, colName, i, j)
{
  var		runningTotal=0;
  for ( ; i <= j; i++ ) {
    var f = form.elements[ (colName + i) ].value;
    if ( f != "" ) { f = parseFloat(f); }
    else { f = 0.0; }
    if ( isNaN(f) ) { f = 0.0; }
    runningTotal = my_add(runningTotal, f);
  }

  return(runningTotal);
}


function isDollar(fld, badValue, scale)
{
  var i, len;

        // return true if empty field
  if ( fld == null || fld.value == null || fld.value == "" || fld.value.length == 0 )
    { return(true); }

        // if has a bad char, return
  //for ( i=0; i < fld.value.length; i++ ) {
  //  var ch = fld.value.charAt(i);
  //  if ( (ch < '0' || ch > '9') &&
  //         ch != '.' && ch != '-' )
  //    { fld.value = badValue; return(false); }
  //}
	// just strip out the crap instead
  fld.value = strip_crap(fld.value, fld.value.length);


  f = parseFloat(fld.value);
  if ( isNaN(f) ) { fld.value = badValue; return(false); }

  f = parseFloat(mult(fld.value, Math.pow(10, scale)));
  f = Math.round(f);
  f = mult(f, Math.pow(10, -scale));

  fld.value = f;

  len = fld.value.indexOf(".");
  if ( len == -1 ) { 
    len = scale; 
    if ( scale > 0 ) { fld.value = fld.value +"."; }
  }
  else { len = fld.value.length - len; }

        // add 0's
  for ( i=len; i < scale; i++ )
    { fld.value = fld.value + "0"; }

  return(true);
}

//
// Strips everything but numbers, the decimal point, and the negative sign
//
function strip_crap(val, len) 
{
  var tmp = "";
  
  for ( var j=0; j < len; j++ ) {
    if ( (val.charAt(j) >= '0' && val.charAt(j) <= '9') || 
        val.charAt(j) == '.' || val.charAt(j) == '-' ) 
      { tmp += val.charAt(j); }
  }
  return(tmp);
}

// 1) convert to int
// 2) mult by int
// 3) convert back to float
function mult(val1, val2)
{
  var		int1, int2;
  var		numDec1=0, numDec2=0;
  var		intT, numDecT;
  var		i, j;

  val1 = ""+ val1;
  val2 = ""+ val2;
  if ( val1 == "" ) { val1 = "0"; } 
  if ( val2 == "" ) { val2 = "0"; } 

	// break apart val1
  if ( (i=val1.indexOf(".")) < 0 ) { int1 = val1; }
  else if ( (i+1) == val1.length ) { int1 = val1.substring(0, i); }
  else {
    numDec1 = val1.length - i - 1;
    int1 = val1.substring(0, i) +""+ val1.substring(i+1, val1.length);
  }

  // strip leading 0's 
  while ( int1.indexOf("0") == 0 ) { 
    if (int1.length == 1) { break; }
    else { int1 = int1.substring(1,int1.length); }   
  } 

	// break apart val2
  if ( (i=val2.indexOf(".")) < 0 ) { int2 = val2; }
  else if ( (i+1) == val2.length ) { int2 = val2.substring(0, i); }
  else {
    numDec2 = val2.length - i - 1;
    int2 = val2.substring(0, i) +""+ val2.substring(i+1, val2.length);
  }

  // strip leading 0's 
  while ( int2.indexOf("0") == 0 ) { 
    if (int2.length == 1) { break; }
    else { int2 = int2.substring(1,int2.length); }   
  } 

  intT = ""+ (parseInt(int1) * parseInt(int2));
  numDecT = numDec1 + numDec2;

  if ( numDecT == 0 ) { intT = ""+ intT; }
  else if ( numDecT < intT.length ) {
    intT = intT.substring(0, (intT.length-numDecT)) +"."+ 
        intT.substring((intT.length-numDecT), intT.length);
  } else if ( numDecT > intT.length ) {
    for ( i=intT.length; i < numDecT; i++ ) { intT = "0"+ intT; }
    intT = "0."+ intT;
  } else { intT = "0."+ intT; }
  
  return(intT);
}

// 1) find max dec
// 2) mult by max dec
// 3) add
// 3) convert back to 
function my_add(val1, val2)
{
  var		int1, int2;
  var		numDec1=0, numDec2=0;
  var		intT, numDecT;
  var		i, j;

  val1 = ""+ val1;
  val2 = ""+ val2;
  if ( val1 == "" ) { val1 = "0"; } 
  if ( val2 == "" ) { val2 = "0"; } 

	// break apart val1
  if ( (i=val1.indexOf(".")) < 0 ) { int1 = val1; }
  else if ( (i+1) == val1.length ) { int1 = val1.substring(0, i); }
  else {
    numDec1 = val1.length - i - 1;
    int1 = val1.substring(0, i) +""+ val1.substring(i+1, val1.length);
  }

  // strip leading 0's 
  while ( int1.indexOf("0") == 0 ) { 
    if (int1.length == 1) { break; }
    else { int1 = int1.substring(1,int1.length); }   
  } 

	// break apart val2
  if ( (i=val2.indexOf(".")) < 0 ) { int2 = val2; }
  else if ( (i+1) == val2.length ) { int2 = val2.substring(0, i); }
  else {
    numDec2 = val2.length - i - 1;
    int2 = val2.substring(0, i) +""+ val2.substring(i+1, val2.length);
  }

  // strip leading 0's 
  while ( int2.indexOf("0") == 0 ) { 
    if (int2.length == 1) { break; }
    else { int2 = int2.substring(1,int2.length); }   
  } 

  // make them the same size
  if ( numDec1 < numDec2 ) {
    for ( i=0; i < (numDec2-numDec1); i++ ) { int1 = int1 +"0"; }
    numDecT = numDec2;
  } else if ( numDec1 > numDec2 ) {
    for ( i=0; i < (numDec1-numDec2); i++ ) { int2 = int2 +"0"; }
    numDecT = numDec1;
  }
  else { numDecT = numDec1; }
  

  intT = ""+ (parseInt(int1) + parseInt(int2));

  if ( numDecT == 0 ) { intT = ""+ intT; }
  else if ( numDecT < intT.length ) {
    intT = intT.substring(0, (intT.length-numDecT)) +"."+ 
        intT.substring((intT.length-numDecT), intT.length);
  } else if ( numDecT > intT.length ) {
    for ( i=intT.length; i < numDecT; i++ ) { intT = "0"+ intT; }
    intT = "0."+ intT;
  } else { intT = "0."+ intT; }
  
  return(intT);
}

