//<SCRIPT LANGUAGE="JavaScript">

String.prototype.GetDate = function()
{
	var sText = this;
	var sMsg = ''
	var sRes = ''
	
	if ( sText != '' )
	{
		var d = sText.split('/')

		if ( d.length == 2 )
		{
			var d = new Date()
			d[2] = d.getFullYear()
		}
		
		if ( d.length != 3 )
		{
			sMsg = 'Invalid date format - ' + sText
		}
		else
		{
			if ( isNaN(parseInt(d[0],10)) || d[0] > 31 || d[0] < 1 )
			{
				sMsg = 'Invalid day of month - ' + d[0]
			}
			else if ( isNaN(parseInt(d[1],10)) || d[1] > 12 || d[1] < 1 )
			{
				sMsg = 'Invalid month - ' + d[1]			
			}
			else if ( isNaN(parseInt(d[2],10)) )
			{
				sMsg = 'Invalid day of year - ' + d[2]			
			}
			else if ( d[2] > -1 && d[2] < 100 )
			{
				d[2] = parseInt(d[2],10) + 2000
			}
			
			if ( d[2] < 1900 || d[2] > 3000 )
			{
				sMsg = 'Invalid year - ' + d[2]
			}
			
			var day, month
			
			day = parseInt(d[0],10)
			if (day<10)
				day = '0' + day
			
			month = parseInt(d[1],10)
			if ( month<10 )
				month = '0' + month
			
			//sRes = day + '/' + month  + '/' + parseInt(d[2],10)
			sRes =  month + '/' +  day + '/' + parseInt(d[2],10)  
		}
	}
	
	if ( sMsg != '' )
	{
		return null;
	}
	else
	{
		return new Date(Date.parse( sRes ))
	}
}
String.prototype.Left = function( nChars )
{
	return this.substr(0, nChars)
}

String.prototype.TruncateWord = function( nChars )
{
	if (!nChars)
		nChars = 0;
	
	var nLastSpace = this.lastIndexOf( ' ' )
	
	if ( nLastSpace > 0 && this.length > nChars )
		return this.Left( nLastSpace ) + ' ...'
	else
		return this
}

String.prototype.StripTags = function( strTag )
{
	var sLeft = this
	var sResult = ""

	if (!strTag)
		strTag = '';

	while (sLeft != "")
	{
		var i = sLeft.toUpperCase().indexOf( "<" + strTag.toUpperCase() )
	
		if ( i == -1 )
		{	
			sResult += sLeft
			sLeft = "";
		}
		else
		{
			var strnext = sLeft.slice( i )
			// j is the length of the tag
			j = strnext.indexOf( ">" )
			
			if ( j== -1 )
			{
			// no closing ">" !
				sLeft = ""
			}
			else
			{
				if ( i==0 )
					sLeft =  sLeft.slice( j+1 )
				else
				{
					sResult += sLeft.slice(0,i)
					sLeft = sLeft.slice( i+j+1 )
				}
			}
		}
	}

	if (strTag == '' )
		return sResult.replace2( '&nbsp;', ' ' );
	else
		return sResult
}

String.prototype.replace2 = function( sSearch, sReplace )
{
	var res = '';

	var i=0;

	while ( i< this.length )
	{
		if (this.substr(i, sSearch.length) == sSearch)
		{
			res += sReplace;
			i+= sSearch.length;
		}
		else
		{
			res += this.charAt(i);
			i++;
		}
	
	}

	return res;
}

function XML( s )
{
	if ( typeof(s) != 'undefined' )
	{
		var res = s.toString();

		var res = res.replace( /&/g, '&amp;')
		res = res.replace( /</g, '&lt;')
		
		return res;
	}
	else
		return '';
}

String.prototype.IsWhiteSpace = function()
{
	return this == ' ' || this == '\t';
}

String.prototype.TrimLeft = function()
{
	var i=0;
	
	while (this.charAt(i).IsWhiteSpace())
		i++;
		
	return this.substr(i)
}

String.prototype.TrimRight = function()
{
	var i=this.length-1;
	
	while (this.charAt(i).IsWhiteSpace())
		i--;
		
	return this.substr(0,i+1)
}

String.prototype.Trim = function()
{
	return this.TrimLeft().TrimRight();
}

String.prototype.Left = function( nChars )
{
	return this.substr(0, nChars)
}

String.prototype.EscapeJScript = function()
{
	var res = this.replace2( '\\', '\\\\')
	return res.replace2( '\'', '\\\'' )
}

String.prototype.Collapse = function()
{
	return this.replace2( ' ', '' )
}

String.prototype.Truncate = function( iLen )
{
	var i=this.length-1;
	
	if (!iLen)
		iLen = 20
	
	if (i > iLen)
	{
		var res = this.Left( iLen)
		
		while (!res.charAt(i).IsWhiteSpace() )
			i--;

		return res.substr(0,i+1).TrimRight() + '...'
	}
	else
		return this;
			
	
}

String.prototype.CountLines = function()
{
	var res=0;
	
	for( var i=0; i<=this.length ; i++ )
		if (this.charAt(i) == '\n' )
			res++
	
	return res;
}

String.prototype.CountCols = function()
{
	var res=0;
	
	for( var i=this.length-1; i>=0 ; i-- )
	{
		if (this.charAt(i) == '\n' )
			break;
		res++
	}
	
	return res;
}

function Line( startchar, len )
{
	this.start = startchar
	this.len = len
}	

String.prototype.CountToLine = function( nLine )
{
	var res=new Line( 0,0 )
	
	var i =0
	
	if ( nLine > 1 )
		for( i=0; i< this.length ; i++ )
		{
			if (this.charAt(i) == '\n' )
			{
				nLine--
				if ( nLine == 1 )
				{
					break;
				}
			}
			else
				res.start++
		}

	for ( var j=i+1; j< this.length; j++)
	{
		if (this.charAt(j) == '\n' )
			break		

		res.len++
	}
	
	return res;
}


//</SCRIPT>
