/**
* Library to provide standard formatting methods.
* History
* Version	Date		By		Description
*----------------------------------------------------------------
* 1.1		09/08/2004	ml		Minor fix to formatAsPhoneNumber to replace '-' and correctly phone number
* 1.0		08/11/2004	ml		Released
*/

/*
* Format a string denoting a phone number to (nnnn) nnn-nnnn
* 1.	Remove all , -,(,) and spaces
* 2.	If area code is supplied and 3 digits, then append leading 0 (i.e. 505 -> 0505)
* 3.	Format the area code
* 4.	Add the rest of the string in the format nnn-nnnn
* Note that no validation is supplied for the number itself.  This is only a formatting method.  Therefore
* it is possible to pass in something like "mystringok" and have it returned as "(0mys) tri-ngok".  Caveat emptor.
* Param:		thePhoneNo - the phone number to pass in
* Returns:		"" if unable to format, else the formatted string
*/
function formatAsPhoneNumber(thePhoneNo){
	var retValue = "";
	var tempStr = thePhoneNo.replace(/[\(\)\-, ]/g, "");
	var phoneNoStart = 0;
	var phoneNextPart = 0;
	
	if(tempStr.length == 10)
		tempStr = '0' + tempStr;
	
	if(tempStr.length == 11){
		phoneNoStart = 4;
		retValue += '(' + tempStr.substring(0, phoneNoStart) + ')';
	}
	
	if(tempStr.length - phoneNoStart >= 7){
		phoneNextPart = phoneNoStart + 3;
		retValue += (retValue.length != 0 ? " " : "") +
			tempStr.substring(phoneNoStart, phoneNextPart) +
			'-' +
			tempStr.substring(phoneNextPart);
	}
	
	return(retValue);
}

/*
* Format a string denoting a SSN to nnn-nn-nnnn
* 1.	Remove all , -,(,) and spaces
* 2.	Format the number
* Note that no validation is supplied for the number itself.  This is only a formatting method.  Therefore
* it is possible to pass in something like "mystringo" and have it returned as "mys-tr-ingo".  Caveat emptor.
* Param:		theSSN - the SSN to pass in
* Returns:		theSSN if unable to format, else the formatted string
*/
function formatAsSSN(theSSN){
	var retValue = theSSN;
	var tempStr = theSSN.replace(/[- ,.]/, "");
	
	if(tempStr.length == 9){
		retValue = tempStr.substring(0, 4) + '-' + tempStr.substring(4, 6) + '-' + tempStr.substring(6);
	}
	
	return(retValue);
}

/*
Formats a given number to give the required precision - e.g. 33 with precision of 2 will convert to 33.00
299.999 with a precision of 2 will convert to 300.00 .
This was written to work in a JavaScript independent fashion as the equivalent call in Number only covers JavaScript 1.5 or later
Params:		numberToFormat	- String containing the number to format
			precision		- int representing the number of zeroes after the dp
Returns:	Formatted string if number to format is a number
			the original string if isNan() == true
*/
function formatAsNumber(number, precision){
	var lhs = null;
	var rhs = null;
	var retValue = null;
	var counter = -1;
	var index = -1;
	var maxForPrecision = 1;
	var numberToFormat = number.toString();
//If there's a dp but no prefixing 0, then add one in.  This will stop NAN.iiiii formatting errors.
	if(numberToFormat.indexOf('.') == 0)
		numberToFormat = '0' + numberToFormat;
	
	if(isNaN(numberToFormat) == false){
	
		for(counter = 0; counter < precision; counter++)
			maxForPrecision *= 10;
	
		if(numberToFormat.indexOf('.') == -1){
//If we haven't got a dp, then put one in.
			numberToFormat += ".";
			
			for(counter = 0; counter < precision; counter ++)
				numberToFormat += '0';
		}
		numberToFormat += '0';	//Padding to make sure that we've always got trailing 0 in the case of "12346."
		
		index = numberToFormat.indexOf('.');
		lhs = parseInt(numberToFormat.substring(0, index));	//This could have been left as string but would have been confusing
		rhs = parseFloat("0." + numberToFormat.substring(index + 1));
		
		for(counter = 0; counter < precision; counter ++)
			rhs *= 10;										//Left shift for the number of dp's
		
		rhs = Math.round(rhs);								//Round the remainder of the number to get an integer value
		
//Carry over to lhs if rhs rounding up takes it to the max (stops 299.999 becoming 299.100 and makes it 300.00)
		if(rhs == maxForPrecision){
			lhs++;
			rhs = 0;
		}
		
		if(rhs < 10){
			for(counter = 0; counter < precision -1; counter++)
				rhs= "0" + rhs;
		}
		retValue = lhs + '.' + rhs;
	}
	else
		retValue = numberToFormat;
		
	return(retValue.toString());
}