var PREFIX_SIGN = 0, POSTFIX_SIGN = 1, PAREN_SIGN = 2;
function validateNumber(getFn){
return doValidateBasicNumeric(this, getFn);
}
function validateInteger(getFn){
this.precision = 0; this.pad = 0;
return doValidateBasicNumeric(this, getFn);
}
function validateMoney(getFn){
this.precision = 2; this.pad = 2;
return doValidateBasicNumeric(this, getFn);
}
function validateWholeDollars(getFn){
this.precision = 0; this.pad = 2;
return doValidateBasicNumeric(this, getFn);
}
function validateNumberList() {
var newNums = "", token, cleanStr, num;
var tokenizer = new Tokenizer(this.get(), this.commas ? "; " : ",; ");
for (token = tokenizer.nextToken(); token != null; token = tokenizer.nextToken()) {
cleanStr = getCleanNum(token);
if (cleanStr != "") {
num = smartParseFloat(cleanStr);
if (typeof this.minimum == "string") this.minimum = smartParseFloat(this.minimum);
if (typeof this.maximum == "string") this.maximum = smartParseFloat(this.maximum);
if(this.minimum != null && num < this.minimum)
return this.setMessage("The entry cannot be less than " + formatNum(smartParseFloat(this.minimum + "") + "", this) + ".");
if(this.maximum != null && num > this.maximum)
return this.setMessage("The entry cannot be greater than " + formatNum(smartParseFloat(this.maximum + "") + "", this) + ".");
token = formatNum(cleanStr, this);
if (newNums.length > 0) newNums += this.commas ? "; " : ", ";
newNums += token;
}
}
this.set(newNums);
if (newNums == "") return this.setMessage("This is a required field.");
return true;
}

function doValidateBasicNumeric(obj, getFn){
if(getFn){ obj.converter = smartParseFloat; return compareConvertedFEW; }
var cleanStr = getCleanNum(obj.get());
if(cleanStr == "") return obj.setMessage("This is a required field.");
obj.set(formatNum(cleanStr, obj));
return inRange(obj);
}
function getCleanNum(strNum){
var baseStr = extract(strNum, NUMERIC + '.-()');
var cleanStr = extract(baseStr, NUMERIC + ".");
if(cleanStr == "" || cleanStr.charAt(0) == ".")
cleanStr = "0" + cleanStr;
if(baseStr.charAt(0) == "-" || baseStr.charAt(baseStr.length - 1) == "-" || (baseStr.charAt(0) == "(" && baseStr.charAt(baseStr.length - 1) == ")"))
cleanStr = "-" + cleanStr;
var decimal = cleanStr.indexOf(".");
if(decimal != -1)
cleanStr = cleanStr.substring(0, decimal + 1) + extract(cleanStr.substring(decimal), NUMERIC);
if(extract(cleanStr, NUMERIC) == ""){
elementObj.set("");
return "";
}
return cleanStr;
}
function round(strNum, precision){
var num = parseFloat(strNum), multiplier = 1, i;
var sign = (num < 0) ? -1.0 : 1.0, p = (precision < 0 ? -precision : precision);
for(i = 0; i < p; i++) multiplier *= 10;
if(precision < 0)
return parseInt("0" + (num * sign / multiplier + 0.50000000000001), 10) * multiplier * sign;
return parseInt("0" + (num * sign * multiplier + 0.50000000000001), 10) / multiplier * sign;
}
function inRange(elementObj){
var overMax = elementObj.compareNullable(elementObj.maximum, ">");
if(elementObj.compareNullable(elementObj.minimum, "<"))
return elementObj.setMessage("The entry cannot be less than " + formatNum(getValueFor(elementObj.minimum, smartParseFloat).toString(), elementObj) + ".");
if(overMax)
return elementObj.setMessage("The entry cannot be greater than " + formatNum(getValueFor(elementObj.maximum, smartParseFloat).toString(), elementObj) + ".");
return true;
}
function smartParseFloat(strNum){
return parseFloat(getCleanNum(strNum));
}
function formatNum(strNum, obj){
strNum = (obj.precision != null) ? round(strNum, obj.precision).toString() : parseFloat(strNum).toString();
var firstDigitPos = strNum.charAt(0) == "-" ? 1 : 0;
var decimal = strNum.indexOf(".") == -1 ? strNum.length : strNum.indexOf(".")
if(obj.commas)
for(var i = decimal - 3; i > firstDigitPos; i -= 3)
strNum = strNum.substring(0, i) + "," + strNum.substring(i);
if(obj.pad != null && obj.pad > 0){
var decimal = strNum.indexOf('.');
if(decimal == -1){ decimal = strNum.length; strNum += "."; }
var targetLen = decimal + obj.pad + 1;
while(targetLen > strNum.length)
strNum += "0";
}
strNum = strNum.substring(firstDigitPos);
if(firstDigitPos == 0 || obj.signFormat != PAREN_SIGN){
if(obj.prefix != null) strNum = obj.prefix + strNum;
if(obj.postfix != null) strNum += obj.postfix;
}
if(firstDigitPos == 1){
if(obj.signFormat == POSTFIX_SIGN)
strNum = strNum + "-";
else if(obj.signFormat == PAREN_SIGN){
strNum = "(" + strNum + ")";
if(obj.prefix != null) strNum = obj.prefix + strNum;
if(obj.postfix != null) strNum += obj.postfix;
}else
strNum = "-" + strNum;
}
return strNum;
}
var _SR_;
if(_SR_ != null) _SR_.notify("validatebasicnumeric.js");
