/// <reference path="intellisense/intellisense.js" />

/******************************************************************************
**      Desc: Extends the String type.
**		Auth: Aleksey Fomichenko
**		Date: 09/14/2007
*******************************************************************************
**		Change History
*******************************************************************************
**		Date:		Author:		Description:
**		-----------	-----------	-----------------------------------------------
**		09/14/2007	Aleksey F.	File created.
*******************************************************************************/

String.empty = "";

String.isNullOrEmpty = function (value)
{
	/// <summary>Returns true if the value is null or empty.</summary>
	/// <param name="value" type="String" optional="false" mayBeNull="true">The value to test.</param>

	return typeof(value) == "undefined" || value === null || value === String.empty || value.toString() === String.empty;
};

String.parse = function (value)
{
	/// <summary>Parses the specified value to a string.</summary>
	/// <param name="value" type="Object" optional="false" mayBeNull="true">The value to parse to a string.</param>
	
	return "{0}".format(value);
};

String.format = function (format, args)
{
	/// <summary>Overrides the ASP.NET AJAX String.format method. Our implementation of 'format' proved to be much more efficient!</summary>
    /// <param name="format" type="String">The format string.</param>
    /// <param name="args" parameterArray="true" mayBeNull="true">The arguments for formatting.</param>
    
    if (arguments.length === 2)
		return format.format(arguments[1]);
	else
		return format.format(Array.prototype.slice.call(arguments, 1));
};

String.prototype.format = function(args)
{
    /// <summary>Formats a string based on the parameters passed in.</summary>
    /// <param name="args" parameterArray="true" mayBeNull="true">The arguments for formatting.</param>
		
	var params = String.prototype.format.arguments;
    var toReturn = this;

    if ( params !== null )
    {
		if ( params.length > 0 && params[0] !== null && typeof(params[0]) == "object" )
		{
			params = params[0];	
		}
		
		for (var i = 0; i < params.length; i++)
		{
			if (params[i] === null) params[i] = String.empty;
			var regex = new RegExp("\\{" + i + "\\}", "g");
			toReturn = toReturn.replace(regex, params[i]);
		}
	}

	return toReturn;
};

String.prototype.isNumeric = function ()
{
	/// <summary>Returns true if the string can be evaluated a number.</summary>

	var value = this.trim();
	return !isNaN(value) && !String.isNullOrEmpty(value);
};

String.prototype.toInteger = function (defaultValue)
{
	/// <summary>Converts the value of a string to a integer type.</summary>
	/// <param name="defaultValue" type="Number" optional="true" mayBeNull="true">The return value in case the conversion fails.</param>

	return parseInt(this.toFloat(defaultValue), null);
};

String.prototype.toFloat = function (defaultValue)
{
	/// <summary>Converts the value of a string to a float type.</summary>
	/// <param name="defaultValue" type="Number" optional="true" mayBeNull="true">The return value in case the conversion fails.</param>

	var value = this.trim();
	
	if (this.isNumeric())
		return parseFloat(this);
	else
	{
		if (typeof(defaultValue) !== $const.undefined && defaultValue !== null && !isNaN(defaultValue))
			return defaultValue;
		else
			return 0.0;
	}
};

String.prototype.replaceBasic = function (searchFor, replaceWith)
{
	/// <summary>Performs a basic search and replace without using regular expressions.</summary>
	/// <param name="searchFor" type="String" optional="false" mayBeNull="false">The string to search for.</param>
	/// <param name="replaceWith" type="String" optional="false" mayBeNull="false">The string to replace the matches with.</param>

	var str = this;
	var i = str.indexOf(searchFor);
	
	while ( i!=-1 )
	{
		var j = i+searchFor.length;
		str = str.substring(0,i)+replaceWith+str.substring(j,str.length);
		i = str.indexOf(searchFor,i+replaceWith.length);
	}
	
	return str;
};

String.prototype.encode = function()
{
	/// <summary>Wraps the string in a <pre></pre> tag.</summary>
	
	return "<pre>{0}</pre>".format(this);
};


// Notify ScriptManager that this is the end of the script.
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();