__string_version=1;

String.prototype.Contains=function(value)
{
    if(value.search(value)>-1)return true;
    return false;
}

String.prototype.PadZeroes=function(required_string_size,leading) 
{
    leading=(leading==null)?true:leading;
    var zeroes='';
    for(var i=0;i<required_string_size;i++)zeroes+="0";
    if(leading)
        return (zeroes+this).substr(0,required_string_size);
    else
        return (this+zeroes).substr(0,required_string_size);
        
}

String.prototype.Trim=function()
{
	if(this.replace(/ /g,'')=='')
		return '';
		
	return this.replace(/^(\s*)([\W\w]*)(\b\s*$)/,'$2');
}

String.prototype.toCurrency=function()
{
    var val=parseInt(this)
    if(isNaN(val))return this;

    var return_val=this,
        dollars_cents=return_val.split(".");
    dollars_cents[0]=dollars_cents[0].toNumber();
    if(dollars_cents.length>1)
        return_val=dollars_cents[0]+"."+dollars_cents[1].PadZeroes(2,false).substr(0,2);
    else
        return_val=dollars_cents[0]+".00";
    return (val<0)?'$('+return_val.replace('-','')+')':'$'+return_val;
}

String.prototype.toNumber=function()
{
    if(this.indexOf(',')<0)
    {
        var val=this.split('.')[0],
            iterations=parseInt(val.length/3),
            new_val='';
        for(var i=0;i<iterations;i++)
            new_val=','+val.substr(val.length-3*(i+1),3)+new_val;
        new_val=val.substr(0,val.length-iterations*3)+new_val;
        if(val.length%3==0)new_val=new_val.substr(1);
        return new_val;
    }
}

String.prototype.Escape=function()
{
    var re=new RegExp("\n", "gi"),
        new_string=this.replace(re,"\\n");
    re=new RegExp("\r", "gi");
    new_string=new_string.replace(re,"\\r");
    re=new RegExp("\"", "gi");
    new_string=new_string.replace(re,"\\\"");
    re=new RegExp("'", "gi");
    return new_string.replace(re,"\\'");
}

String.prototype.RemoveControlCharacters=function()
{
	return this.replace(/\s/g,'');
}

String.prototype.IsEmpty=function()
{
	if(this==undefined||this==null||this=='')
		return true;
	return false;
}

String.prototype.GetQueryParameterValue = function(key)
{
	return(this.match(new RegExp("[?|&]?" + key + "=([^&]*)"))[1]);
}

String.prototype.insert=function(value, startPosition)
{
	return this.substr(0,startPosition)+value+this.substr(startPosition);
}

String.prototype.count=function(value)
{
	if(!value || value.IsEmpty())
		return 0;
		
	var	occurrences = 0,
			i=0;
	
	while(true)
	{
		i=this.indexOf(value,i);
		if(i<0) break;
		i+=value.length;
		occurrences++;
	}
	
	return occurrences;
}

String.prototype.indexAfter=function(value,occurrences)
{
	if(!value || value.IsEmpty() || !occurrences || occurrences<=0)
		return 0;
		
	var	index=0;
	
	for(var i=0;i<occurrences;i++)
	{
		var idx=this.indexOf(value,index);
		
		if(idx<0) 
		{
			return -1;
			break;
		}
		
		index=idx+value.length;
	}
	
	return index;
}
