// JavaScript library of  string related functions

/*
  Function name: dollarFormat.js
  Author:   Nate Weiss (NMW)
  Purpose:  Mimics the CFML DollarFormat() function
  Note:     With IE, you could just use num.toLocaleString() instead
*/

// Utility function that formats a number to a "dollar format"
function dollarFormat(num) {
   var arParts, dollarPart, centPart

  // Split number based on the position of the period character, if any
  arParts=num.toString().split(".");
  // The dollar part is the part before the period
  dollarPart=arParts[0];

   // If there is a cent portion of the number, use it, otherwise use "00"
  if (arParts.length > 1) {
    centPart=(arParts[1] + "00").substr(0,2);
  } else {  
    centPart="00";      
  }

  // Reset arParts to an empty array
  arParts=new Array();

  // Number of digits before first comma 
  // (but zero if there will be 3 digits before first comma)
  var offset=dollarPart.length % 3;

  // If there should be some digits (other than 3) before first comma,
  // add an element to the array with that number of digits in it
  if (offset > 0) {
    arParts[0]=dollarPart.substr(0, offset);
  }

   // For all remaining groups of three digits, add additional
  // elements to the array with the next 3 digits in it
  for (var i=offset; i < dollarPart.length; i=i + 3) {
    arParts[arParts.length]=dollarPart.substr(i, 3);
  };
  
  // Join the array back into a string, separated by commas
  dollarPart=arParts.join(",")
  
  // Return the various parts, concatenated together
  return "$" + dollarPart +  "." + centPart;
}


		// Utility function that formats a number to a any number of decimal places passed in as a parameter
		function numberFormat(num, scale) {
		   var arParts, integerPart, decimalPart
		   
		   	// strip minus sign if this is a negative number
		   	if (num < 0){
		   		var sign = '-';
				num = num.toString().slice(1);
			}
			else
				var sign = '';
		   
		   // check for decimal places
		   if (! scale)
		   	var scale = 2;
		
		  // Split number based on the position of the period character, if any
		  arParts=num.toString().split(".");
		  
		  // The dollar part is the part before the period
		  integerPart=arParts[0];	
		  	  
		   // If there is a decimal portion of the number, use it, otherwise use "00"
		  if (arParts.length > 1) {
			decimalPart=(arParts[1] + "000000000").slice(0,scale);
		  } else {  
			decimalPart="000000000".slice(0, scale);      
		  }
		
		  // Reset arParts to an empty array
		  arParts=new Array();
		
		  // Number of digits before first comma 
		  // (but zero if there will be 3 digits before first comma)
		  var offset=integerPart.length % 3;
		
		  // If there should be some digits (other than 3) before first comma,
		  // add an element to the array with that number of digits in it
		  if (offset > 0) {
			arParts[0]=integerPart.substr(0, offset);
		  }
		
		   // For all remaining groups of three digits, add additional
		  // elements to the array with the next 3 digits in it
		  for (var i=offset; i < integerPart.length; i=i + 3) {
			arParts[arParts.length]=integerPart.substr(i, 3);
		  };
		  
		  // Join the array back into a string, separated by commas
		  integerPart=arParts.join(",")
		  
		  // Return the various parts, concatenated together
		  num = integerPart +  "." + decimalPart;
		  if (sign == '-')
		  	num = '< ' + num + ' >';
			
		  return num
		}


// utility function that converts the text argument into  title case (first letter of each word is capitalized except for small words
function toTitleCase(text){
			var temp = text.split(' ');
			text = '';
			for (var i = 0; i < temp.length; i++){
				if (i != 0 && ',the,a,an,or,of,'.indexOf(',' + temp[i] + ',') != -1)
					text += temp[i] + ' ';
				else{
					text += temp[i].slice(0,1).toUpperCase();
					text += temp[i].slice(1).toLowerCase() + ' ';
				}
			}
			return text.slice(0, text.length - 1);
}




