function WordWrap(strSrcControl, strDstControl, lLineLength)
{
	this.SrcControl = document.getElementById(strSrcControl)
	this.DstControl = document.getElementById(strDstControl)
	this.Tokenize = Tokenize;
	this.Wrap = Wrap;
	this.LineLength = lLineLength;
	
	this.Tokens = new Array();
	this.WrappedText = "";

	this.Tokenize();
	this.WrappedText = this.Wrap();

	if(this.DstControl.outerHTML)
		this.DstControl.outerHTML = "<pre id='FRMCVPRECV'>" + this.WrappedText + "</pre>";
	else
		this.DstControl.innerHTML = this.WrappedText;
}

function Tokenize()
{
	var strText = this.SrcControl.innerHTML;
	var objToken = new Token();

	for(var i = 0; i < strText.length; ++i)
	{
		var strChar = strText.substring(i, i + 1);
		var strChar = strText.charAt(i);
		var lCharType = 0;
		//Replace tabs with space
		if (strChar == "\t")
			strChar = " ";
		//Do we have a word breaker?
		if(strChar == " ")
			lCharType = 1;
		else if (strChar == "\r" ||
			strChar == "\n")
			lCharType = 2;
		//If we are adding the same char type as the token type
		if(objToken.Type == lCharType)
		{
			//Start a new token if we would exceed line length
			if (objToken.Text.length + 1 > this.LineLength)
			{
				//Add the current to the token array
				this.Tokens.push(objToken);
				//Add the WB to the token array
				objToken = new Token();
				objToken.Text = strChar;
				objToken.Type = lCharType;
			}
			else
				objToken.Text = objToken.Text.concat(strChar);
		}
		else
		{
			//Add the current to the token array
			this.Tokens.push(objToken);
				
			//Add the WB to the token array
			objToken = new Token();
			objToken.Text = strChar;
			objToken.Type = lCharType;
		}
		
		//Append the token if this is the last token
		if(i == strText.length - 1)
			this.Tokens.push(objToken);
	}
}

function Wrap()
{
	var strLine = ""; var arText = new Array();
	//Go through the tokens
	
	for(var i = 0; i < this.Tokens.length; ++i)
	{
		var objToken = this.Tokens[i];
		//Do we have a new line in the token?
		var bHasNewline = false
		//Can we add another word to the line?
		if (strLine.length + objToken.Text.length <= this.LineLength)
		{
			strLine += objToken.Text;
			if (objToken.Type == 2)
			{
				arText.push(strLine);
				strLine = "";
			}
				
		}
		//Start a new line
		else
		{
			//No need to force CRLF for CRLF tokens
			if(objToken.Type == 2)
			{
				arText.push(strLine);
				arText.push(objToken.Text);
				strLine = "";
			}
			else
			{
				arText.push(strLine + "\r\n");
				strLine = objToken.Text;
			}
		}
	}
	//Push the last line
	arText.push(strLine);
	
	return arText.join("");;
}

function Token()
{
	this.Text = "";
	this.Type = 0;
}

