// check field for numeric, alert them or return error, round
function errNumeric(formField,fieldLabel, min, max, len, noBlank, round, showalert, strictmode) {
// numeric field check
	var errorStr = ""
	var numcomma = 0;
	// kent added to ensure that the value is a number
    // Modified by bryan to allow strict number check
    // in strict mode, If the raw text (unparsed) is not a number, error out
	var notanum = false;
    if(formField.value.length > 0){
        if( strictmode == true ){
            notanum = isNaN(formField.value);
        }else{
            notanum = isNaN(parseFloat(formField.value));
        }
    }
    // If not a number, error out
	if(notanum){
		errorStr += fieldLabel + " is not a number.\n";
		// rich added to return the error
		if (showalert)
			alert(errorStr);
		return errorStr;
	}
	// if length is 0, just return.  if no blanks allowed set errorstr first
	if (formField.value.length == 0) {
		if (noBlank == true) {
			errorStr += fieldLabel + " cannot be blank.\n"
			if (showalert == true) 
				alert(errorStr);
		}
		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);
				return 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
}

// add by Tobias 10/29/2004 e.g. check for double '..' in a number
function checkdoublechar(formField,delims,fieldLabel,showalert){
	var errorStr = ""
	var delims_array = delims.split("/");
	for (var i=0; i < delims_array.length; i++){
		if (OccursNoCase(delims_array[i],formField.value) > 1){
			i = delims_array.length; // get out of loop
			errorStr = fieldLabel + " contains invalid characters.\n"
			if (showalert == true)
				alert(errorStr);
		}	
	}
	if (errorStr == ""){
		errorStr = errNumPunc(formField,fieldLabel,'.,-',0,true);
		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);
}


function formatNumber(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) 
		num = "0";
		cents = Math.floor((num*100+0.5)%100);
		num = Math.floor((num*100+0.5)/100).toString();
		if(cents < 10) cents = "0" + cents;
			for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
				num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
		return (num + "." + cents);
}
