String.prototype.trim = function()
{
	return this.trimLeft().trimRight();
}

String.prototype.trimLeft = function()
{
	return this.replace(/^[\s ]*/, "");
}

String.prototype.trimRight = function()
{
	return this.replace(/[\s ]*$/, "");
}

String.prototype.left = function(length)
{
	return this.substr(0, length);
}

String.prototype.right = function(length)
{
	return this.substr(this.length - length, length);
}

String.prototype.onNewLine = function(text)
{
	return this + ((this != "" && text != null && text != "" && typeof(text) != "undefined") ? "\r\n" : "") + text;
}

String.prototype.escapeJS = function(script)
{
	return this.replace(/"/gi, "&quot;").replace(/'/gi, "\\'")
}

String.fromSum = function(sum, mode, countFloatPosition/*количество знаков после запятой*/)
{
	var s = String(Math.abs(sum));		
	var result = "";
	var i = s.indexOf(".");
	var space = (-2 == mode ? " " : "&nbsp;");
	if(countFloatPosition == undefined)
		countFloatPosition = 2;
	
	if (i > 0)
	{
		//для вещественных чисел - установка кол-ва знаков после запятой
		result = s.substr(i, s.length - i > (countFloatPosition + 1) ? (countFloatPosition + 1) : s.length - i);
		if (result.length < (countFloatPosition + 1)) 
		{
			result += "0";
		}
		s = s.substr(0, i);//целая часть
	}
	//3 - разделитель, после которого нужно вставить пробелы
	for (i = s.length - 3; i >= 0; i = i - 3)
		result = s.substr(i, 3) + (i < s.length - 3 ? space : "") + result;

	if (i > - 3)
		result = s.substr(0, 3 + i) + (s.length > 2 ? space : "") + result;

	if (sum < 0) 
		result = "-" + result;
	if (-1 == mode || -2 == mode) 
		return result;
	
	return result + (mode ? "&nbsp;р." : "&nbsp;рублей");
}

String.prototype.getMoneyValue = function(count/*кол-во знаков после запятой*/)
{
	var res = String.fromSum(this, -2, count);			
	var point_index = res.indexOf(".");			
	var suffixStr = "";
	if(count == undefined)
	{
		count = 2;
	}
	
	for(var i = 0; i < count; i++)
	{
		suffixStr += "0";
	}
	
	if (point_index < 0)
	{
		res += "." + suffixStr;
	}
	else if (res.substr(point_index, res.length - point_index).length < count + 1)
	{
		res += "0";
	}
	return res;
}

String.prototype.setEnding = function(number, ending1, ending2, ending3)
{
    var numberStr = new String(number);
    var result = this;

    if ((number > 10 && numberStr.charAt(numberStr.length - 2) == "1") || (numberStr.charAt(numberStr.length - 1) > "4") || (numberStr.charAt(numberStr.length - 1) == "0"))
        result += ending3;
    else if (numberStr.charAt(numberStr.length - 1) == "1")
        result += ending1;
    else result += ending2;

    return result;
}

String.format = function( strFormat )
{
    //arguments[0] - строка формата 
    for( var i = 1;  i < arguments.length; i++)
    {
        var exp = RegExp("\\{" + (i - 1) + "\\}", "g");
        strFormat = strFormat.replace(exp, arguments[i].toString());
    }
    return strFormat;
};
