// ************************************************************************************************************************
// *** СТРОКИ ***
// ************************************************************************************************************************

// Убирает пробелы слева
String.prototype.trimStart = function()
{
	var result = this;	
	if (result.length > 0)
	{
		result = result.replace(/^\s+/, "");
	}
	
    return result;
}

// Убирает пробелы справа
String.prototype.trimEnd = function()
{
	var result = this;
	if (result.length > 0)
	{
		result = result.replace(/\s+$/, "");
	}

    return result;
}

// Убирает оконечные пробелы
String.prototype.trim = function ()
{
	var result = this;
	if (result.length > 0)
	{
		result = result.replace(/^\s+|\s+$/g, "");
	}
	
    return result;
}

// Сравнивает строку с указанным префиксом
String.prototype.startsWith = function(prefix)
{
	return (this.substr(0, prefix.length) === prefix);
}

// Сравнивает строку с указанным суффиксом
String.prototype.endsWith = function(suffix)
{
	return (this.substr(this.length - suffix.length) === suffix);
}

// Возвращает указанное число символов с левого конца строки
String.prototype.left = function(length)
{
	if (!/\d+/.test(length))
	{
		return this;
	}
	
	var result = this.substr(0, length);

	return result;
}

// Возвращает указанное число символов с правого конца строки
String.prototype.right = function(length)
{
	if (!/\d+/.test(length))
	{
		return this;
	}
	
	var result = this.substr(this.length - length);
	
	return result;
}

// ************************************************************************************************************************
// *** МАССИВЫ ***
// ************************************************************************************************************************

// Определяет индекс элемента в массиве
Array.prototype.indexOf = function(item, start)
{
    if (typeof(item) === "undefined")
    {
		return -1;
	}
	
    var length = this.length;
    if (length !== 0)
    {
		start = start - 0;
		if (isNaN(start))
		{
            start = 0;
        }
        else
        {
			if (isFinite(start))
			{
				start = start - (start % 1);
            }
            
			if (start < 0)
			{
                start = Math.max(0, length + start);
            }
        }

		for (var i = start; i < length; i++)
		{
            if ((typeof(this[i]) !== "undefined") && (this[i] === item))
            {
                return i;
            }
        }
    }
    
    return -1;
}

// Проверяет наличие элемента в массиве
Array.prototype.contains = function(item)
{
	return (this.indexOf(item) >= 0);
}

// Добавляет элемент в массив
Array.prototype.add = function(value)
{
	this[this.length] = value;
}

// Добавляет в массив элементы другого массива
Array.prototype.addRange = function(items)
{
	this.push.apply(this, items);
}

// Удаление элемента из массива
Array.prototype.remove = function(item)
{
    var index = this.indexOf(item);
    if (index >= 0)
    {
		this.splice(index, 1);
    }
    
    return (index >= 0);
}

// Удаление элемента из массива по заданному индексу
Array.prototype.removeAt = function(index)
{
	this.splice(index, 1);
}

// Удаление диапазона элементов
Array.prototype.removeRange = function(beginIndex, endIndex)
{
	this.splice(beginIndex, (endIndex - beginIndex + 1))
}

// Клонирует массив
Array.prototype.clone = function()
{
    if (this.length === 1)
    {
		return [this[0]];
    }
    else
    {
		return Array.apply(null, this);
    }
}

// Удаляет все элементы массива
Array.prototype.clear = function()
{
	this.length = 0;
}