function trim(str, ch)
{
	if (!ch)
		return str.replace(/^\s*|\s*$/g, '');						// trim spaces
	else
	{
		var pattern = new RegExp("^" + ch + "*|" + ch + "*$", "g");	// trim the given character
		return str.replace(pattern, '');
	}
}

function truncateMultiLineTextBox(ctrl, maxLength)	// use only with textarea
{
	if (ctrl.value.length > maxLength)
	{
		ctrl.value = ctrl.value.slice(0, maxLength);
		ctrl.scrollTop = (ctrl.scrollHeight - ctrl.clientHeight);	// scroll to end
	}
}

function isMaxLengthMultiLineTextBox(evt, maxLength)			// use only with textarea
{
	evt = getEvent(evt);
	if (!evt)
		return false;

	if (!isIE())
	{
		if (evt.ctrlKey || evt.altKey)
			return false;
		if (isNavigation(evt.keyCode))
			return false;
	}

	var ctrl = getTarget(evt);
	if (!ctrl || (ctrl.type.toLowerCase() != 'textarea'))
		return false;

	if (ctrl.value.length >= maxLength)
	{
		if (ctrl.value.length > maxLength)
			truncateMultiLineTextBox(ctrl, maxLength);
		if (!isIE())
			cancelEvent(evt)
		return true;
	}

	return false;
}


function ellipsize(s, chars)
{
	return ((s && (s.length <= chars)) ? s : (s.substring(0, chars) + "..."));
}

function updateRemainingChars(input, labelID)
{
	if (!input)
		return;

	var label = getById(labelID);
	if (!label)
		return;
	label.style.display = 'block';

	var format = label.getAttribute('format');
	var maxLength = (input.maxLength ? input.maxLength : input.getAttribute('MaxLengthMultiLine'));
	var remainingChars = Math.max(0, (maxLength - input.value.length));
	var html = format.replace('{0}', remainingChars);
	if (remainingChars == 1)
	{
		html = html.replace('{+s}', '');
		html = html.replace('{-s}', 's');
	}
	else
	{
		html = html.replace('{+s}', 's');
		html = html.replace('{-s}', '');
	}
	label.innerHTML = html;
}

//The role of this function is to remove carriage returns and
//line feeds from the string unless they are the last character
function removeSpecialChar(str, chr)
{
    var strLength = str.length;
    var newString = "";
    for(var i=0; i < strLength; i++)
    {
        if (str.charCodeAt(i) != chr || i < strLength - 1 )
        {
            newString += str.charAt(i);
        }
    }
    
    return newString;
}
