// FORMAT.JS
// Validates and reformats text fields containing numbers.
// ------------------------------------------------------------------------------------
// noCommas(refVal)				- Strip out commas, %, $.
// validNums(refVal)			-	Allow only numbers in the field.
// validNumsDec(refVal)		-	Allow only numbers, one decimal, commas, % or $ in the field.
// addzeros(refVal)				- Convert numbers from 555555.5 to 555,555.50 .
// removeDblspace(refVal)	- Convert multiple spaces into a single space

function trim(str){ return str.replace(/^\s*|\s*$/g,""); }

function formatNum(num) {

	num = parseFloat(num);
	num = Math.round(100*num);
	num = num.toString();
	var p1 = num.substring(0, num.length - 2);
	var p2 = num.substring(num.length - 2, num.length);
	num = p1 + '.' + p2;

	var txtNumber = '' + num;
	var rxSplit = new RegExp('([0-9])([0-9][0-9][0-9][,.])');
	var arrNumber = txtNumber.split('.');
	arrNumber[0] += '.';
	do {
	arrNumber[0] = arrNumber[0].replace(rxSplit, '$1,$2');
	} while (rxSplit.test(arrNumber[0]));

	if (arrNumber.length > 1) {
	return arrNumber.join('');
	}
	else {
	return arrNumber[0].split('.')[0];
    }	
}	

function noCommas(refVal) {
	tmpVar = refVal.value;
	if (tmpVar.length != 0) { 
		for (count = 0; count < tmpVar.length; count ++) {
			if (tmpVar.charCodeAt(count) == 44 || tmpVar.charCodeAt(count) == 36 || tmpVar.charCodeAt(count) == 37) {
				tmpVar = tmpVar.substring(0, count) + tmpVar.substring(count+1, tmpVar.length); }
		}}
	return tmpVar;
}

function validNums(refVal) {
	if (refVal.value.length == 0) { return true; }
	var bValid=true;
	for (var i=0; i < refVal.value.length; i++) {
		var c = refVal.value.charCodeAt(i);
    if ((c > 57 || c < 48)) {	bValid=false; }
	}
	if (bValid==false) { alert('You can only enter numbers into this field.');
		refVal.focus();	refVal.select(); return false; }
	else { return true; } } 

function validNumsDec(refVal) {
	if (refVal.value.length == 0) { return true; }
	var decm = false;	var bDec = true; var bValid = true;
	for (var i=0; i < refVal.value.length; i++) {
		var c = refVal.value.charCodeAt(i);
        if ((c > 57 || c < 48)) {
			if (c==46) {
				if (decm == true) {
					var bDec = false; }
				else {
					decm = true; } }
			else if (c==44 || c==36 || c==37) { }
			else {
				bValid = false; }
			}
	}
	if (bValid==false || bDec==false) {
		alert('You can only enter numbers, commas and one decimal into this field.');
		refVal.focus();	refVal.select(); return false; }
	else { return true; } }

function addzeros(num) {  

	// this function just takes numbers like 1234.5 and changes them to 1,234.50
	if (num == null) { return 0; }
	if (isNaN(num)) { return 0; }
	if (Number(num) == 0) { return 0; }
	num = parseFloat(num);
	num = Math.round(100*num);
	num = num.toString();
	var p1 = num.substring(0, num.length - 2);
	var p2 = num.substring(num.length - 2, num.length);
	num = p1 + '.' + p2;

	var txtNumber = '' + num;
	var rxSplit = new RegExp('([0-9])([0-9][0-9][0-9][,.])');
	var arrNumber = txtNumber.split('.');
	arrNumber[0] += '.';
	do {
	arrNumber[0] = arrNumber[0].replace(rxSplit, '$1,$2');
	} while (rxSplit.test(arrNumber[0]));

	if (arrNumber.length > 1) {
	return arrNumber.join('');
	}
	else {
	return arrNumber[0].split('.')[0];
    }	
}	

function addcommas(num) {  

	// this function just takes numbers like 1234.5 and changes them to 1,235 (no decimal)
	if (num == null) { return 0; }
	if (isNaN(num)) { return 0; }
	if (Number(num) == 0) { return 0; }
	num = parseFloat(num);
	num = Math.round(num);

	var txtNumber = '' + num;
	var rxSplit = new RegExp('([0-9])([0-9][0-9][0-9][,.])');
	var arrNumber = txtNumber.split('.');
	arrNumber[0] += '.';
	do {
	arrNumber[0] = arrNumber[0].replace(rxSplit, '$1,$2');
	} while (rxSplit.test(arrNumber[0]));

	if (arrNumber.length > 1) {
	return arrNumber.join('');
	}
	else {
	return arrNumber[0].split('.')[0];
    }	
}	


function removeDblspace(testsub) {

	for (x=testsub.length; x > 0; x--) {
		var y = testsub.charCodeAt(x);
		if ((y > 64 && y<91) || (y>97 && y < 123)) {
			testsub = testsub.substring(0, x + 1);
			return testsub; }
	}
}