function OutputEncoder_EncodeHtml( strInput )
{
	var c;
	var EncodeHtml = '';
	
	for(var cnt = 0; cnt < strInput.length; cnt++)
	{
		c = strInput.charCodeAt(cnt);
		
		if (( ( c > 96 ) && ( c < 123 ) ) ||
			( ( c > 64 ) && ( c < 91 ) ) ||
			( c == 32 ) ||
			( ( c > 47 ) && ( c < 58 ) ) ||
			( c == 46 ) ||
			( c == 44 ) ||
			( c == 45 ) ||
			( c == 95 ))
		{
			EncodeHtml = EncodeHtml + String.fromCharCode(c);
		}
		else
		{
			EncodeHtml = EncodeHtml + '&#' + c + ';';
		}
	}
	
	return EncodeHtml;
}

function OutputEncoder_EncodeHtmlAttribute( strInput )
{
	var c;
	var EncodeHtmlAttribute = '';
	
	for(var cnt = 0; cnt < strInput.length; cnt++)
	{
		c = strInput.charCodeAt(cnt);

		if (( ( c > 96 ) && ( c < 123 ) ) ||
			( ( c > 64 ) && ( c < 91 ) ) ||
			( ( c > 47 ) && ( c < 58 ) ) ||
			( c == 46 ) ||
			( c == 44 ) ||
			( c == 45 ) ||
			( c == 95 ))
		{
			EncodeHtmlAttribute = EncodeHtmlAttribute + String.fromCharCode(c);
		}
		else
		{
			EncodeHtmlAttribute = EncodeHtmlAttribute + '&#' + c + ';';
		}
	}
	
	return EncodeHtmlAttribute;
}

function OutputEncoder_EncodeXml( strInput )
{
	return OutputEncoder_EncodeHtml( strInput );
}

function OutputEncoder_EncodeXmlAttribute( strInput )
{
	return OutputEncoder_EncodeHtmlAttribute( strInput );
}
	
function OutputEncoder_EncodeJs( strInput )
{
	var c;
	var EncodeJs = '';
	
	for(var cnt = 0; cnt < strInput.length; cnt++)
	{
		c = strInput.charCodeAt(cnt);

		if (( ( c > 96 ) && ( c < 123 ) ) ||
			( ( c > 64 ) && ( c < 91 ) ) ||
			( c == 32 ) ||
			( ( c > 47 ) && ( c < 58 ) ) ||
			( c == 46 ) ||
			( c == 44 ) ||
			( c == 45 ) ||
			( c == 95 ))
		{
			EncodeJs = EncodeJs + String.fromCharCode(c);
		}
		else if ( c > 127 )
		{
			EncodeJs = EncodeJs + '\\u' + OutputEncoder_TwoByteHex(c);
		}
		else
		{
			EncodeJs = EncodeJs + '\\x' + OutputEncoder_SingleByteHex(c);
		}
	}
	
	return '\'' + EncodeJs + '\'';
}

function OutputEncoder_EncodeVbs( strInput )
{
	var c;
	var EncodeVbs = '';
	var bInQuotes = false;
	
	for(var cnt = 0; cnt < strInput.length; cnt++)
	{
		c = strInput.charCodeAt(cnt);

		if (( ( c > 96 ) && ( c < 123 ) ) ||
			( ( c > 64 ) && ( c < 91 ) ) ||
			( c == 32 ) ||
			( ( c > 47 ) && ( c < 58 ) ) ||
			( c == 46 ) ||
			( c == 44 ) ||
			( c == 45 ) ||
			( c == 95 ))
		{

			if ( !bInQuotes )
			{
				EncodeVbs = EncodeVbs + '&\"';
				bInQuotes = true;
			}
			
			EncodeVbs = EncodeVbs + String.fromCharCode(c);
		}
		else
		{

			if ( bInQuotes )
			{
				EncodeVbs = EncodeVbs + '\"';
				bInQuotes = false;
			}
			
			EncodeVbs = EncodeVbs + '&chrw(' + c + ')';
		}
	}

	if ( EncodeVbs.charAt(0) == '&' )
	{

		EncodeVbs = EncodeVbs.substring(1);
	}
	
	if ( EncodeVbs.length == 0 )
	{

		EncodeVbs = '\"\"';
	}
	
	if ( bInQuotes )
	{

		EncodeVbs = EncodeVbs + '\"';
	}
	
	return EncodeVbs;
}

function OutputEncoder_AsUrl( strInput )
{
	if( strInput.search(/^(?:http|https|ftp):\/\/[a-zA-Z0-9\.\-]+(?:\:\d{1,5})?(?:[A-Za-z0-9\.\;\:\@\&\=\+\$\,\?\/]|%u[0-9A-Fa-f]{4}|%[0-9A-Fa-f]{2})*$/i) )
	{
		throw ("Unsanitized value passed to AsUrl");
	}
	
	return strInput;
}


function OutputEncoder_QualifyUrl( strInput )
{
	if ( strInput.search(/^(?:http|https|ftp):\/\//i) )
	{
		if (document.location.protocol == 'HTTPS' )
		{
			return "https://" + document.location.hostname + OutputEncoder_QualifyUrl_MakePath(strInput);
		}
		else
		{
			return "http://" + document.location.hostname + OutputEncoder_QualifyUrl_MakePath(strInput);
		}
	}
	else
	{
		// bah, they passed us a fully-qualified URL - so pass it back
		return strInput;
	}
}

function OutputEncoder_QualifyUrl_MakePath( strInput )
{
	if( ! strInput.search(/^[\/\\]/) )
	{
		return strInput;
	}
	
	// Figure out just the path component of current url
	var re = /^(\/(?:.*\/|))(?:[^\/\\]*\.\w+|\w*)$/;
	if( ! document.location.pathname.search(re) )
	{
		var path = re.exec(document.location.pathname);
		return path[1] + strInput;
	}
	
	return '/' + strInput;
}

function OutputEncoder_AsNumeric( strInput )
{
	if( isNaN(parseFloat(strInput)) )
	{

		throw "IOSec.AsNumeric(): Error input ["+strInput+"] not a valid number.";
	}
	
	return strInput;
}
	
function OutputEncoder_EncodeUrl( strInput )
{
	var c;
	var EncodeUrl = '';
	
	for(var cnt = 0; cnt < strInput.length; cnt++)
	{
		c = strInput.charCodeAt(cnt);

		if (( ( c > 96 ) && ( c < 123 ) ) ||
			( ( c > 64 ) && ( c < 91 ) )  ||
			( ( c > 47 ) && ( c < 58 ) ) ||
			( c == 46 ) ||
			( c == 45 ) ||
			( c == 95 ))
		{
			EncodeUrl = EncodeUrl + String.fromCharCode(c);
		}
		else if ( c > 127 )
		{
			EncodeUrl = EncodeUrl + '%u' + OutputEncoder_TwoByteHex(c);
		}
		else
		{
			EncodeUrl = EncodeUrl + '%' + OutputEncoder_SingleByteHex(c);
		}
	}
	
	return EncodeUrl;
}
	
function OutputEncoder_SingleByteHex( charC )
{
	var SingleByteHex = charC.toString(16);

	for ( var cnt = SingleByteHex.length; cnt < 2; cnt++ )
	{
		SingleByteHex = "0" + SingleByteHex;
	}
	
	return SingleByteHex;
}

function OutputEncoder_TwoByteHex( charC )
{
	var TwoByteHex = charC.toString(16);

	for( var cnt = TwoByteHex.length; cnt < 4; cnt++ )
	{
		TwoByteHex = "0" + TwoByteHex;
	}
	
	return TwoByteHex;
}

function OutputEncoder()
{
	this.EncodeHtml = OutputEncoder_EncodeHtml;
	this.EncodeHtmlAttribute = OutputEncoder_EncodeHtmlAttribute;
	this.EncodeXml = OutputEncoder_EncodeXml;
	this.EncodeXmlAttribute = OutputEncoder_EncodeXmlAttribute;
	this.EncodeJs = OutputEncoder_EncodeJs;
	this.EncodeVbs = OutputEncoder_EncodeVbs;
	this.AsNumeric = OutputEncoder_AsNumeric;
	this.EncodeUrl = OutputEncoder_EncodeUrl;
	this.SingleByteHex = OutputEncoder_SingleByteHex;
	this.TwoByteHex = OutputEncoder_TwoByteHex;
	this.AsUrl = OutputEncoder_AsUrl;
	this.QualifyUrl = OutputEncoder_QualifyUrl;
}

var IOSec = new OutputEncoder();


