// trim all leading and trailing spaces from a string
function allTrim(theString) {
	while (theString.charAt(0) == " ") {
		theString = theString.substr(1);
	}
	while (theString.charAt(theString.length - 1) == " ") {
		theString = theString.slice(0,theString.length - 1);
	}
	return theString;
}

// trim all leading spaces from a string
function leftTrim(theString) {
	while (theString.charAt(0) == " ") {
		theString = theString.substr(1);
	}
	return theString;
}

// trim all trailing spaces from a string
function rightTrim(theString) {
	while (theString.charAt(theString.length - 1) == " ") {
		theString = theString.slice(0,theString.length - 1);
	}
	return theString;
}

// check if a string variable is empty, i.e. a blank string
function isEmpty(theString) {
	theString = allTrim(theString);
	if (theString == "") {
		return true;
	}
	return false;
}

// confirm if character is alphabetic
function isAlpha(c) {
	if ( c=="a"||c=="b"||c=="c"||c=="d"||c=="e"||c=="f"||c=="g"||c=="h"||
			c=="i"||c=="j"||c=="k"||c=="l"||c=="m"||c=="n"||c=="o"||c=="p"||
			c=="q"||c=="r"||c=="s"||c=="t"||c=="u"||c=="v"||c=="w"||c=="x"||
			c=="y"||c=="z"||c=="A"||c=="B"||c=="C"||c=="D"||c=="E"||c=="F"||
			c=="G"||c=="H"||c=="I"||c=="J"||c=="K"||c=="L"||c=="M"||c=="N"||
			c=="O"||c=="P"||c=="Q"||c=="R"||c=="S"||c=="T"||c=="U"||c=="V"||
			c=="W"||c=="X"||c=="Y"||c=="Z" ) {
		return true;
	}
 	return false;
}	

// validate email address string
function validEmailAddress(email) {
	email = allTrim(email);
	email_at = email.indexOf("@",1);
	email_dot = email.indexOf(".",email_at+2);
	if (email_at==-1 || email_dot==-1 || email.lastIndexOf(".")+1==email.length) {
		return false;
	}
	return true;
}

// validate Canadian postal code
function validCAPostalCode(postalCode) {
	postalCode = allTrim(postalCode);
	for (i=0; i<postalCode.length; i++) {
		if (postalCode.substring(i,i+1)==" " || postalCode.substring(i,i+1)=="-" || postalCode.substring(i,i+1)==".") {
			postalCode = postalCode.substring(0,i)+postalCode.substring(i+1,postalCode.length);
		}
	}
 	a2_pc_ok = (postalCode.length==6 && isAlpha(postalCode.substring(0,1)) &&
		!isNaN(postalCode.substring(1,2)) && isAlpha(postalCode.substring(2,3)) &&
		!isNaN(postalCode.substring(3,4)) && isAlpha(postalCode.substring(4,5)) &&
		!isNaN(postalCode.substring(5,6)));
    if (!a2_pc_ok) {
		return false;
	}
	return true;
}


function FormatNumber(Number,Decimals,Separator)
{
 Number += ""          // Force argument to string.
 Decimals += ""        // Force argument to string.
 Separator += ""       // Force argument to string.
 if((Separator == "") || (Separator.length > 1))
  Separator = "."
 if(Number.length == 0)
  Number = "0"
 var OriginalNumber = Number  // Save for number too large.
 var Sign = 1
 var Pad = ""
 var Count = 0
 // If no number passed, force number to 0.
 if(parseFloat(Number)){
  Number = parseFloat(Number)} else {
  Number = 0}
 // If no decimals passed, default decimals to 2.
 if((parseInt(Decimals,10)) || (parseInt(Decimals,10) == 0)){
  Decimals = parseInt(Decimals,10)} else {
  Decimals = 2}
 if(Number < 0)
 {
  Sign = -1         // Remember sign of Number.
  Number *= Sign    // Force absolute value of Number.
 }
 if(Decimals < 0)
  Decimals *= -1    // Force absolute value of Decimals.
 // Next, convert number to rounded integer and force to string value.
 // (Number contains 1 extra digit used to force rounding)
 Number = "" + Math.floor(Number * Math.pow(10,Decimals + 1) + 5)
 if((Number.substring(1,2) == '.')||((Number + '')=='NaN'))
  return(OriginalNumber) // Number too large to format as specified.
 // If length of Number is less than number of decimals requested +1,
 // pad with zeros to requested length.
 if(Number.length < Decimals +1) // Construct pad string.
 {
  for(Count = Number.length; Count <= Decimals; Count++)
   Pad += "0"
 }
 Number = Pad + Number // Pad number as needed.
 if(Decimals == 0){
  // Drop extra digit -- Decimal portion is formatted.
  Number = Number.substring(0, Number.length -1)} else {
  // Or, format number with decimal point and drop extra decimal digit.
 Number = Number.substring(0,Number.length - Decimals -1) +
          Separator +
          Number.substring(Number.length - Decimals -1,
          Number.length -1)}
 if((Number == "") || (parseFloat(Number) < 1))
  Number="0"+Number // Force leading 0 for |Number| less than 1.
 if(Sign == -1)
  Number = "-" + Number  // Set sign of number.
 return(Number)
}
