// --------------------------------------------------------------------
// accept an inputbox and its value as arguments: remove non-digit
// characters from the value, then strip repeated '.', ',' or '-' chars.
// --------------------------------------------------------------------
function allowOnlyFloatingPointNumbers(textbox, val){
	val = val.replace(/,/g, '.'); // strip non-digit chars
	val = val.replace(/[^0-9\.-]/g, ''); // strip non-digit chars
	val = stripDuplicateChars(val, '.', 1, 0); // strip excess decimals
	val = stripDuplicateChars(val, '-', 0, 1); // strip excess minus signs
	textbox.value = val; // replace textbox value
}

// --------------------------------------------------------------------
// checks string against regular expression for floating point number.
// --------------------------------------------------------------------
// note: we use '\d*' rather than the more correct '\d+', as we have
// to allow that the user is typing the input, not pasting it in ready
// formed. (ie: '-98.' is allowed with '\d*', but not '\d+', which
// would require '-98.0').
// --------------------------------------------------------------------
// returns boolean result
// ---------------------------------------------------------------------
function isFloatingPointNumber(val){
	var fpnum = /^-?\d*\.?\d*$/g;
	if (fpnum.test(val)){return true;} else {return false;}
}

// --------------------------------------------------------------------
// accepts two strings (str) and (strip), and two integers (n) and (s)
// as arguments: the 'strip' character specified is allowed to
// occur 'n' times in the 'str' string, counting from the starting
// index 's'.
// ie: stripDuplicateChars("xxxYYYxxxYYY", "x", 2, 0) --> "xxYYYYYY"
// stripDuplicateChars("xxxYYYxxxYYY", "Y", 1, 4) --> "xxxYYxxx"
// --------------------------------------------------------------------
// returns the modified string
// --------------------------------------------------------------------
function stripDuplicateChars(str, strip, n, s){
	var count=0; var stripped=str.substring(0, s); var chr;
	for (var i=s; i<str.length; i++){ chr = str.substring(i, i+1);
	if (chr == strip){ count++; if (count<n+1){ stripped = stripped + chr;}}
	else {stripped = stripped + chr;}} return stripped;
}
