/*
	Javascript Function :
		String and Number Related Fucnctions
		
	Index :
		1. Trim(thisValue)						- Truncate the space in head and tail of string
		2. Left(thisString, thisLength)			- Cut left Nth lenght character from input string
		3. Right(thisString, thisLength)		- Cut right Nth lenght character from input string
		4. IsValidNumber(thisNO)				- Validate the input string whether is numeric data
		5. IsDoubleByte(thisObj)				- Check whether is double byte object
		6. ToCurrencyFormat(thisValue)			- Format Currency - Add Currency
*/


// Truncate the space in head and tail of string
function Trim(thisValue) {
	while (thisValue.charAt(0) == " ") { thisValue = thisValue.substring(1) }
	while (thisValue.charAt(thisValue.length - 1) == " ") { thisValue = thisValue.substring(0,thisValue.length - 1) }
	while (thisValue.indexOf('"') > -1) { thisValue = thisValue.replace('"',"'") }
	return thisValue;
}

// Cut left Nth lenght character from input string
function Left(thisString, thisLength){
	if (thisLength <= 0)
	    return "";
	else if (thisLength > String(thisString).length)
	    return thisString;
	else
	    return String(thisString).substring(0, thisLength);
}

// Cut right Nth lenght character from input string
function Right(thisString, thisLength){
    if (thisLength <= 0)
       return "";
    else if (thisLength > String(thisString).length)
       return thisString;
    else {
       var iLen = String(thisString).length;
       return String(thisString).substring(iLen, iLen - thisLength);
    }
}

// Validate the input string whether is numeric data
function IsValidNumber(thisNO){
	var NumList = "0123456789";

	with(thisNO){
		if (value.length <= 0) return false;
		for (var i=0; i < value.length; i++)
		if ((NumList.indexOf(value.charAt(i)) == -1) || 
				(value.indexOf(".") != value.lastIndexOf("."))) {
			thisNO.focus();
			return false;
		}		
		return true;
	}
}

// Check whether is double byte object
function IsDoubleByte(thisObj) {
	with(thisObj){
		if (value.length <= 0) return false
		for (var i=0; i < value.length; i++)
		if (value.charCodeAt(i)<1 || value.charCodeAt(i)>255) {
			thisObj.focus();
			return true;
		}		
		return false;
	}
}

// Format Currency - Add Currency
function ToCurrencyFormat(thisValue)
{
	thisValue += '';
	x = thisValue.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


