﻿/* Extensioner till AJAX String objektet, för att efterlikna C# String objekt */
/* 
* Determines whether the specified string occurs in within this string. 
* @return {Boolean} 
*/
String.prototype.contains = function(pValue)
{   
	return this.indexOf(pValue) != -1;
}

/* 
* Determines whether the end of this string matches the specified string. 
* @return {Boolean} 
*/
String.prototype.endsWith = function(pValue,pBlnIgnoreCase)
{   
	var strSource = this;   
	var strValue = pValue;   
	if(pBlnIgnoreCase)   
	{      
		strSource = strSource.toLowerCase();      
		strValue = strValue.toLowerCase();   
	}   
	return strSource.indexOf(strValue) == (strSource.length - strValue.length);
}

/* 
* Inserts a string within this string at the specified index 
* @return {String} 
*/
String.prototype.insert = function(pValue,pIndex)
{   
	if(pValue == null)   
	{      
		throw "Argument null exception";   
	}   
	
	if(pIndex < 0 || pIndex >= this.length)   
	{      
		throw "Argument out of range exception";   
	}   
	
	if(pIndex == 0)   
	{      
		return pValue + this;   
	}   
	else if (pIndex == this.length - 1)   
	{      
		return this + pValue;   
	}   
	else   
	{      
		var strStart = this.substring(0,pIndex);      
		var strEnd = this.substring(pIndex,this.length);      
		return strStart + pValue + strEnd;   
	}
}

/* 
* Pads this string on the left with a specified number of character strings. 
* @return {String} 
*/
String.prototype.padLeft = function(pValue,pCount)
{   
	var strLeft = "";   
	
	for(var i = 0; i < pCount; i++)   
	{       
		strLeft += pValue;   
	}   
	return strLeft + this;
}

/* 
* Pads this string on the right with a specified number of character strings. 
* @return {String} 
*/
String.prototype.padRight = function(pValue,pCount)
{   
	var strRight = "";   
	
	for(var i = 0; i < pCount; i++)   
	{       
		strRight += pValue;   
	}   
	return this + strRight;
}

/* 
* Removes all carriage returns and new lines from this string
* @return {String} 
*/
String.prototype.removeLines = function() 
{   
	return this.replace(/(\n\r|\n|\r)/gm,"");
}

/* 
* Determines whether the start of this string matches the specified string.
* @return {Boolean} 
*/
String.prototype.startsWith = function(pValue,pBlnIgnoreCase)
{    
	var strSource = this;    
	var strValue = pValue;    
	if(pBlnIgnoreCase)    
	{       
		strSource = strSource.toLowerCase();       
		strValue = strValue.toLowerCase();    
	}    
	return strSource.indexOf(strValue) == 0;
}

/* 
* Removes white space from this string 
* @return {String} 
*/
String.prototype.trim = function()
{    
	return this.replace(/^\s+|\s+$/g,"");
}

/* 
*	Removes white space from the start of this string 
*	@return {String} 
*/
String.prototype.trimLeft = function()
{    
	return this.replace(/^\s+/,"");
}

/* Removes white space from the end of this string 
* @return {String} 
*/
String.prototype.trimRight = function()
{    
	return this.replace(/\s+$/,"");
}

/*
Strip html from string
*/
String.prototype.stripHtml = function() {
    return this.replace(/<("[^"]*"|'[^']*'|[^'">])*>/gi, "");
}