// check field for numeric, alert them or return error, round
function errNumeric(formField,fieldLabel, min, max, len, noBlank, round, showalert) {
// numeric field check
    var errorStr = ""
    var numcomma = 0;

    // no blanks & if length is 0, then just return
    if (formField.value.length == 0) {
	if (noBlank == true) {
        errorStr += fieldLabel + " cannot be blank.\n"
        if (showalert == true) 
		alert(errorStr);
	  return errorStr
	}
	else {
		return errorStr;
	}
    }
    // if field ends in period, truncate
    var vlength = formField.value.length;
    if (formField.value.substring(vlength-1,vlength) == '.') {
        formField.value = formField.value.substring(0,vlength-1);
    }
    // field must be numeric or comma or period
    for (var i = 0; i < formField.value.length; i++) {
        var ch = formField.value.substring(i, i + 1)
        if (ch == ",")
           numcomma++

        if ((ch < "0" || ch > "9") && (ch != ",") && (ch != ".") && (ch != "-")) {
           errorStr += fieldLabel + " must be numeric.\n"
           if (showalert == true) 
		   alert(errorStr);
           return errorStr
        }
    }
    // add rounding back to number
    if (round == true) {
	    // Check for decimal and round value past decimal
	    var decindex = formField.value.lastIndexOf(".");
	    var dec=0;
          var left = '0';
	    if (decindex > -1) {
	      // if number in front of decimal
            if (decindex > 0) { 
 	         left = formField.value.substring(0,decindex);
            }
	      dec = 0 + Math.round(formField.value.substring(decindex,formField.value.length));
    	    }
    	    else
    	      left = formField.value;
          // get string with only digits and store string + round back
	    var numwoutcomma = "";
	    for (var i = 0; i < left.length; i++) {
	        var ch = left.substring(i, i + 1)
	        if (ch != ",")
	           numwoutcomma += ch;
	    }
              if (parseInt(numwoutcomma) >= 0)
  	            formField.value = parseInt(numwoutcomma,10) + dec;
              else
  	            formField.value = parseInt(numwoutcomma,10) - dec;
    }

    // max/min
    if ((min != 0 || max != 0) && len > -1) {
        if ((formField.value < min) || (formField.value > max)) {
            errorStr += fieldLabel + " must be between " + min + " and " + max + ".\n";
            if (showalert == true) 
	  	    alert(errorStr);
            return errorStr
        }
    }
    // max/min when only warning for negatives
    // only alert if -1, -2 means do not alert (used in CheckAndSubmit)
    if ((min != 0 || max != 0) && len < 0) {
        if ((((parseInt(formField.value) < 0) && (Math.abs(parseInt(formField.value))) > Math.abs(min)) && ((parseInt(formField.value) > 0) && (Math.abs(parseInt(formField.value))) < Math.abs(min))) || (Math.abs(parseInt(formField.value)) < Math.abs(max))) {
            // within bounds so check if need to warn (do not return since not error)
            if ((len != -2) && ((formField.value < min) || (formField.value > max))) {
                errorStr += fieldLabel + " is typically between between " + min + " and " + max + ".\n";
                if (showalert == true) 
    	    	        alert(errorStr);
            }
        }
	  else {
		// outside bounds
            if ((formField.value < min) || (formField.value > max)) {
				if (len == -1)
	                errorStr += fieldLabel + " is typically between between " + min + " and " + max + ".\n";
				else
				     errorStr += fieldLabel + " must be between " + min + " and " + max + ".\n";
                if (showalert == true) 
		      	    alert(errorStr);
                return errorStr
            }
        }
    }
    // length
    if (len > 0 && formField.value.length != len-numcomma && formField.value.length > 0) {
        errorStr += fieldLabel + " must be " + len + " characters long.\n"
	    if (showalert == true)
	        alert(errorStr);
    }
    return errorStr
}

//
// Checks a field for numbers and punctuation.
// allows user to specify the punctuation and required length
function errNumPunc(formField,fieldLabel,delims,numdigits,showalert) {
    var errorStr = ""
    var testStr = "";

    testStr = stripCharsNotInBag(formField.value,delims+'0123456789');
	if (testStr.length != formField.value.length) {
		errorStr += fieldLabel + " contains invalid characters.\n"
		if (showalert == true)
			alert(errorStr);
    }
	else {
		testStr = stripCharsNotInBag(formField.value,'0123456789');
		// normal check is length vs number of digits
		if (numdigits != 0) {
			if (formField.value.length != numdigits) {
				// but if created by system will include single alpha
				//if ((numdigits < 0) && (testStr.length < (Math.abs(numdigits)-1))) {
					errorStr += fieldLabel + " must be " + Math.abs(numdigits) + " digits.\n"
					if (showalert == true)
						alert(errorStr);
				//}
			} 
		}
	}
	return errorStr
}
// added by kent 6/16/2000
function dollarFormat(expr){
	return "$" + decimalFormat(expr, 2);
}
function decimalFormat(expr, decplaces){
	// raise by power of 10 times the decplaces; round to an int; convert to str
	var str = "" + Math.round (eval(expr) * Math.pow(10,decplaces));
	// pad with zeros 
	while (str.length <= decplaces){
		str = "0" + str	;
	}
	// get location of decimal point
	var decpoint = str.length - decplaces;
	return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}
