function encodeCommentHtml()
{
	encodeTextBoxs(document.getElementsByTagName("textarea"));
	encodeTextBoxs(document.getElementsByTagName("input"));
}

function encodeTextBoxs(voTextboxs)
{
	if (voTextboxs != null && voTextboxs.length > 0)
	{
		for (i = 0; i < voTextboxs.length; i++)
		{
			if (voTextboxs[i].attributes.getNamedItem("IsComment") != null && voTextboxs[i].attributes.getNamedItem("IsComment").value == "True")
			{
				voTextboxs[i].value = urlEncode(htmlEncode(voTextboxs[i].value, false));
				voTextboxs[i].style.display = "none";
			}
		}
	}
}

// This function encode a string to be able to be posted to asp without error
function urlEncode(vsString) 
{
	var sEncodedHtml;
	
	sEncodedHtml = escape(vsString);
	sEncodedHtml = sEncodedHtml.replace(/\//g,"%2F");
	sEncodedHtml = sEncodedHtml.replace(/\?/g,"%3F");
	sEncodedHtml = sEncodedHtml.replace(/=/g,"%3D");
	sEncodedHtml = sEncodedHtml.replace(/&/g,"%26");
	sEncodedHtml = sEncodedHtml.replace(/@/g,"%40");
	sEncodedHtml = sEncodedHtml.replace(/''/g,"%27");
	sEncodedHtml = sEncodedHtml.replace(/ /g,"%20");
	sEncodedHtml = sEncodedHtml.replace(/,/g,"%2C");
	return sEncodedHtml;
}

/*
HTMLEncode - Encode HTML special characters.
Copyright (c) 2006 Thomas Peri, http://www.tumuski.com/
*/

/**
 * HTML-Encode the supplied input
 * 
 * Parameters:
 *
 * (String)  source    The text to be encoded.
 * 
 * (boolean) display   The output is intended for display.
 *
 *                     If true:
 *                     * Tabs will be expanded to the number of spaces 
 *                       indicated by the 'tabs' argument.
 *                     * Line breaks will be converted to <br />.
 *
 *                     If false:
 *                     * Tabs and linebreaks get turned into &#__;
 *                       entities just like all other control characters.
 *
 * (integer) tabs      The number of spaces to expand tabs to.  (Ignored 
 *                     when the 'display' parameter evaluates to false.)
 *
 * v 0.3 - January 4, 2006
 */
function htmlEncode(source, display, tabs)
{
	function special(source)
	{
		var result = '';
		for (var i = 0; i < source.length; i++)
		{
			var c = source.charAt(i);
			if (c < ' ' || c > '~')
			{
				c = '&#' + c.charCodeAt() + ';';
			}
			result += c;
		}
		return result;
	}
	
	function format(source)
	{
		// Use only integer part of tabs, and default to 4
		tabs = (tabs >= 0) ? Math.floor(tabs) : 4;
		
		// split along line breaks
		var lines = source.split(/\r\n|\r|\n/);
		
		// expand tabs
		for (var i = 0; i < lines.length; i++)
		{
			var line = lines[i];
			var newLine = '';
			for (var p = 0; p < line.length; p++)
			{
				var c = line.charAt(p);
				if (c === '\t')
				{
					var spaces = tabs - (newLine.length % tabs);
					for (var s = 0; s < spaces; s++)
					{
						newLine += ' ';
					}
				}
				else
				{
					newLine += c;
				}
			}
			// If a line starts or ends with a space, it evaporates in html
			// unless it's an nbsp.
			newLine = newLine.replace(/(^ )|( $)/g, '&nbsp;');
			lines[i] = newLine;
		}
		
		// re-join lines
		var result = lines.join('<br />');
		
		// break up contiguous blocks of spaces with non-breaking spaces
		result = result.replace(/  /g, ' &nbsp;');
		
		// tada!
		return result;
	}

	var result = source;
	
	// ampersands (&)
	result = result.replace(/\&/g,'&amp;');

	// less-thans (<)
	result = result.replace(/\</g,'&lt;');

	// greater-thans (>)
	result = result.replace(/\>/g,'&gt;');
	
	if (display)
	{
		// format for display
		result = format(result);
	}
	else
	{
		// Replace quotes if it isn't for display,
		// since it's probably going in an html attribute.
		result = result.replace(new RegExp('"','g'), '&quot;');
	}

	// special characters
	result = special(result);
	
	return result;
}

