﻿/* commonly used string functions */

/* FLOAT - STRING */
/* float validation functions need beefing up, check for 2 decimals, dollar signs and so on */
function isFloatChar (num) {
	var string = ".,1234567890";

	if (num.length > 1) {return false;}
	if (string.indexOf (num) != -1) {return true;}

	return false;
	}
function isFloat (val) {
	for (var i = 0; i < val.length; i++) {
			if (!isFloatChar (val.charAt(i))){return false;}
		}
	return true;
	}

/* WHITE SPACE */
/* remove white space from beginning */
function LTrimAll (str) {
var i = 0;

	if (str == null) {return str;}
    while (str.charAt(i) == " " || str.charAt (i) == "\n" || str.charAt (i) == "\t")
        i++;
	return str.substring (i, str.length);
	}

/* remove white space from end */
function RTrimAll (str) {
var i = str.length - 1;

	if (str == null) {return str;}
    while (str.charAt (i) == " " || str.charAt (i) == "\n" || str.charAt (i) == "\t")
        i--;
	return str.substring (0, i + 1);
	}

/* remove white space from both ends of string */
function TrimAll (str) {
	return (LTrimAll (RTrimAll (str)));
	}

/* Empty? */
function isEmpty (elem, helperMsg) {
	elem.value = TrimAll (elem.value);
	if (elem.value.length == 0) {
		alert (helperMsg);
		elem.focus ();
		return true;
	}
	return false;
}

/* STRING FORMATTING */
function valueToStr (v, showDecimal) {
	var i;
	var s = v + '';
	
	i = s.indexOf ('.');
	if (showDecimal) {
		if (i != -1) {
			if ((s.length - i) > 3) s = s.substring (0, i + 3); // reduce to two decimal places
			else if ((s.length - i) == 2) s = s + '0'; // only 1 zero so add 1
		} else { // no decimal so add
			s = s + '.00';
		}
	} else { // don't show decimals
		if (i != -1) 
			s = s.substring (0, i);
	}

	return s;
}
/* make a nice dollar amount inluding dollar sign and 2 decimal places */
function valueToDollarStr (v, showDecimal) {

	var s = valueToStr (v, showDecimal);
	s = '$' + s;
	return s;
}












