function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function lTrim(str) {
	if (str.length) {
		for (var i=0; str.charAt(i)<=" " && i < str.length; i++);
		return str.substring(i,str.length);
	} else {
		return '';
	}
}

function rTrim(str) {
	if (str.length) {
		for (var i=str.length-1; str.charAt(i) <= " " && i > 0; i--);
		return str.substring(0,i+1);
	} else {
		return '';
	}
}

function trim(str) {
	return lTrim(rTrim(str));
}

function removeLastWord(sentence) {
	for (var i = sentence.length; i > 0; i--) {
		if (sentence.substr(i,1) == " ") {
			return sentence.substr(0,i);
			break;
		}
	}
	//No spaces so return full sentence
	return sentence
}

function fitText(divID, maxHeight, linkRef) {
	var theDiv = document.getElementById(divID);
	var originalHTML = '';
	
	//Only copy original html if we need to shrink the contents
	if (theDiv.scrollHeight > maxHeight) {
		originalHTML = theDiv.innerHTML; 
	}

	while (theDiv.scrollHeight > maxHeight) {
		originalHTML = removeLastWord(originalHTML);
		theDiv.innerHTML = originalHTML + linkRef;		
	}
}


//Pads the content string with spaces to meet the requested length
//if the string is already longer than the requested length then the original string is returned
//otherwise a string of length length is returned with the content right justified
function padLeft(content, length) {
	var contentLength = content.length;
	var spacesRequired = length - contentLength;
	var padding = '';
	 
	if (contentLength > length) return content;
	
	for (var i = 0 ; i < spacesRequired; i++) {
		padding += ' ';
	}
	
	return padding + content;
}





/*
File Name: 	stripNonNumeric.js
Inputs:		theField - the object whose contents are to be stripped of non numeric crap
			allowDollar - allows the number to have a single dollar sign at the front of the 
						number (this may appear before or after the - sign) default is no
			commaFormat - allows the number to have comma separators. default is no
Outputs: 	theField.value - the reformatted contents are stored in the specified field
Desc:		Takes the contents of the specifieid form field and strips its value of any non 
			numeric characters (with the exception of - and ($ and , optionally)).
			If commaFormats is true, the LHS of the decimal point will be reformatted
			to contain commas after every 3rd number eg 333,333,333.00
			If commaFormats is set to false then all commas will be stripped out
Modifications: Date / Author / Mod
28-09-04	BH	Created File
03-09-05	BH	Removed allowDollar option
03-09-05	BH	Added allow negative option 
*/

function stripNonNumeric(theField, allowDollar, allowComma) {
		if (theField == undefined) return false;
		if (allowDollar == undefined) allowDollar = 0;
		if (allowComma == undefined) allowComma = 0;
		
		reString = "[^0-9-/.";
		if (allowDollar) reString += "/$";
		reString += "]";

		re = new RegExp(reString, "g");

		theField.value = theField.value.replace(re, "");

		//Now remove multiple .s
		foundDot = false; 
		for (i=theField.value.length-1; i >= 0; i--) {
			c = theField.value.substr(i,1);
			if (c == '.') {
				if (foundDot) {//remove this dot coz we already got 1
					theField.value = theField.value.substr(0,i) + theField.value.substr(i+1)
					i--;
				} else {
					foundDot = true;
				}
			}
		}

		//Now remove multiple -s
		foundDash = false;
		for (i=0; i < theField.value.length; i++) {
			c = theField.value.substr(i,1);
			if (c == '-') {
				if (foundDash) {//remove this dot coz we already got 1
					theField.value = theField.value.substr(0,i) + theField.value.substr(i+1)
				} else {
					foundDash = true;
				}
			} else if (c.search(/[0-9]/) > -1) {
				foundDash = true;
			}
		}
		
		//add cents if dec point is on the end
		if (theField.value.substr(theField.value.length-1,1) == '.') {
			theField.value += '00';
		}

		//Now do comma formatting
		if (allowComma) {
			sides = new Array(1);
			sides = theField.value.split('.');
			
			lhs = sides[0];
			if (sides.length > 1) {rhs = sides[1];} else {rhs = '';}

			numCount = 0;			
			for (l=lhs.length-1; l >= 0; l--) {
				if (lhs.substr(l,1).search(/[0-9]/) > -1) {
					numCount++;
					if (numCount > 2 && l > 0) {
						lhs = lhs.substr(0,l) + ',' + lhs.substr(l);
						numCount = 0;
					}
				}
			}
			theField.value = lhs;
			if (rhs.length) theField.value += rhs;
		} 
	}

function stripNonNumeric2(theField, allowNegative, commaFormat) {
	if (theField == undefined) return false;
	if (commaFormat == undefined) commaFormat = 0;
	if (allowNegative == undefined) allowNegative = 0;

	//Replace the letter k with 3 zeros
	theField.value = theField.value.replace(/[kK]/g, "000");

	//Check for the presence of the 'm' char shorthand for million	
	var millionFlag = false;
	var millionRE = /[mM]/g;
	if (millionRE.test(theField.value)) {
		millionFlag = true;
	}

	var reString = "[^0-9/.";
	if (allowNegative) reString += "-";
	reString += "]";

	var re = new RegExp(reString, "g");

	theField.value = theField.value.replace(re, "");

	//Now remove multiple .s
	var foundDot = false; 
	for (var i=theField.value.length-1; i >= 0; i--) {
		var c = theField.value.substr(i,1);
		if (c == '.') {
			if (foundDot) {//remove this dot coz we already got 1
				theField.value = theField.value.substr(0,i) + theField.value.substr(i+1)
				i--;
			} else {
				foundDot = true;
			}
		}
	}

	//Now remove multiple -s
	if (allowNegative) {
		var foundDash = false;
		for (i=0; i < theField.value.length; i++) {
			var c = theField.value.substr(i,1);
			if (c == '-') {
				if (foundDash) {//remove this dot coz we already got 1
					theField.value = theField.value.substr(0,i) + theField.value.substr(i+1)
				} else {
					foundDash = true;
				}
			} else if (c.search(/[0-9]/) > -1) {
				foundDash = true;
			}
		}
	}
	
	//add cents if dec point is on the end
	if (theField.value.substr(theField.value.length-1,1) == '.') {
		theField.value += '00';
	}

	//if million flag is set then multiply value by 1,000,000
	if (millionFlag) {
		theField.value = theField.value * 1000000;
	}
	
	//Now do comma formatting
	if (commaFormat) {
		var sides = new Array(1);
		sides = theField.value.split('.');
		
		var lhs = sides[0];
		var rhs = '';
		if (sides.length > 1) {rhs = sides[1];}

		var numCount = 0;			
		for (l=lhs.length-1; l >= 0; l--) {
			if (lhs.substr(l,1).search(/[0-9]/) > -1) {
				numCount++;
				if (numCount > 2 && l > 0) {
					lhs = lhs.substr(0,l) + ',' + lhs.substr(l);
					numCount = 0;
				}
			}
		}
		theField.value = lhs;
		if (rhs.length) theField.value += rhs;
	} 
}

function formatABNACN(theField) {
	var returnString = '';
	var charCount = 0;
	
	theField.value = theField.value.replace(/[^0-9]/g, "");
	for (var i= theField.value.length - 1; i>=0; i--) {
		returnString = theField.value.substr(i,1) + returnString; 
		charCount++;
		if (charCount == 3) {
			charCount = 0;
			returnString = ' ' + returnString ;
		}
	}
	theField.value = returnString;
}

function IsNumeric(strString)
	   //  check for valid numeric strings		  
{
	var strValidChars = "0123456789.";
	var strChar;
	var blnResult = true;
	
	if (strString.length == 0) return false;
	
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	  {
	  strChar = strString.charAt(i);
	  if (strValidChars.indexOf(strChar) == -1)
		 {
		 blnResult = false;
		 }
	  }
	return blnResult;
}

function IsNumericNeg(strString)
	   //  check for valid numeric strings		  
{
	var strValidChars = "0123456789.-";
	var strChar;
	var blnResult = true;
	
	if (strString.length == 0) return false;
	
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	  {
	  strChar = strString.charAt(i);
	  if (strValidChars.indexOf(strChar) == -1)
		 {
		 blnResult = false;
		 }
	  }
	return blnResult;
}

function IsInteger(strString)
	   //  check for valid numeric strings		  
{
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;
	
	if (strString.length == 0) return false;
	
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	  {
	  strChar = strString.charAt(i);
	  if (strValidChars.indexOf(strChar) == -1)
		 {
		 blnResult = false;
		 }
	  }
	return blnResult;
}
