﻿/* This file contains functions that are not specific to the CSA Secure site */
/* Copyright 2007 BluTrend, LLC. */

//--==--==--==--==--==--==--==-- BEGIN STRING FUNCTIONS --==--==--==--==--==--==--==--
///	<summary>
///	Removes all leading spaces from the passed in string.
///	</summary>
function LTrim(str)
{
	while (str.substring(0, 1) == " ")
	{
		str = str.substring(1, str.length);
	}
	
	return str;
}

///	<summary>
///	Removes all trailing spaces from the passed in string.
///	</summary>
function RTrim(str)
{
	while (str.substring(str.length - 1, str.length) == " ")
	{
		str = str.substring(0, str.length - 1);
	}
	
	return str;
}

///	<summary>
///	Removes all leading and trailing spaces from the passed in string.
///	</summary>
function Trim(str) 
{
	str = LTrim(str);
	str = RTrim(str);
	
	return str;
}

//--==--==--==--==--==--==--==--  END STRING FUNCTIONS  --==--==--==--==--==--==--==--

//--==--==--==--==--==--==--==-- BEGIN NUMBER FUNCTIONS --==--==--==--==--==--==--==--

/// <summary>
///	Verifies if the passed in string is an integer (negative or positive and commas allowed).
///	Returns true if the string is an integer.
/// </summary>
function IsInt(s) { return /^\-?([0-9]*|[0-9]{1,3}(,[0-9]{3})*)$/.test(s); }

/// <summary>
///	Verifies if the passed in string is an acceptable US monetary number (negative or positive
///	and commas allowed).  Returns true if the string is a US monetary number.
/// </summary>
function IsMoney(s) {return /^\$?\-?([0-9]*|[0-9]{1,3}(,[0-9]{3})*)(\.[0-9]{1,})?$/.test(s); }

/// <summary>
///	Verifies if the passed in string is a number (negative or positive, decimal and commas
///	allowed).  Returns true if the string is an acceptable number.
/// </summary>
function IsNumber(s) { return /^\-?([0-9]*|[0-9]{1,3}(,[0-9]{3})*)(\.[0-9]{1,})?$/.test(s); }

///	<summary>
///	Removes the commas from number so it can be converted into a true number.
///	</summary>
function RemoveCommas(s) { return s.replace(/\,/g, ""); }

///	<summary>
/// Inserts commas into a number
///	</summary>
function AddCommas(s)
{
	var outValue = "";
	var workingString = String(s);
	var L = workingString.length - 1;
	var P = workingString.indexOf('.') - 1;
	var C;
		
	if (P < 0)
		P = L;
		
	for (i = 0; i <= L; i++)
	{
		outValue += C = workingString.charAt(i)
		
		if ((i < P) && ((P - i) % 3 == 0) && (C != '-'))
			outValue += ',';
	}
	
	return outValue;
}

//--==--==--==--==--==--==--==--  END NUMBER FUNCTIONS  --==--==--==--==--==--==--==--