function preprocesschat( str )
{
	oldstr = str;
	newstr = client_side_smileify( str );
	newstr = process_inline_photos( newstr );

	return newstr;
}

function encode_ex( str )
{
}

function str_replace_mid( str, offset, length, replacement_text )
{
	// replaces a midportion of a string, given the starting character offset
	// and the length, with a replacement block (which can be a different length,
	// or "" to remove the midportion entirely).
	if ( offset > 0 )
	{
		prefix = str.substr(0,offset-1);
	}
	else
		prefix = "";

	if ( offset + length < str.length )
	{
		suffix = str.substr( offset+length );	// to the end of str.
	}
	else
		suffix = "";

	new_str = prefix + replacement_text + suffix;
	return new_str;
}

function process_inline_photos( str )
{
	new_str = str;
	for ( ; ; )
	{
		inl_idx = new_str.indexOf("INLINEPHOTO:");
		if ( inl_idx != -1 )
		{
			// 48; 36=fn length, 12=header (INLINEPHOTO:)
			if ( new_str.substr( inl_idx+48, 1 ) != ":" )
			{
				// malformed request
				//alert( "malformed!" );
				return str;
			}
			else
			{
				fn = new_str.substr( inl_idx+12, 36 );
				//link = "<a href=\"/photos/"+fn+"\">view your photo</a>";
				link = '<a href="#" onclick="javascript:showphoto( \'/photos/' +fn + '\', 480, -1, -1 ); return false;">view your photo</a>';
				//alert(link);
				new_str = str_replace_mid( new_str, inl_idx, 49, link );	// 49 to remove the trailing :
				//return new_str;
				//alert( fn );
			}
		}
		else
			break;
	}

	return new_str;
}

function client_side_smileify(str)
{
	i = "<img src=\"/smiles/";
	if (1)
		a = "a";	// alterates
	else
		a = "";

	// must escape ( ) [ ] { } * and / and \ and | and $
	xstr = str;

	xstr = xstr.replace( /<\):\)/g, i + "50.gif\">" );
	xstr = xstr.replace( /=:\)/g,  i + "47.gif\">" );
	xstr = xstr.replace( /\*-:\)/g, i + "45.gif\">" );
	xstr = xstr.replace( /~:>/g,  i + a + "39.gif\">" );
	xstr = xstr.replace( /0:\)/g,  i + a + "21.gif\">" );
	xstr = xstr.replace( /\/:\)/g,  i + a + "20.gif\">" );
	xstr = xstr.replace( /;;\)/g,  i + "05.gif\">" );
	xstr = xstr.replace( />:\)/g,  i + a + "16.gif\">" );
	xstr = xstr.replace( /:-"/g,  i + "54.gif\">" );
	xstr = xstr.replace( /:\(\(/g,  i + "17.gif\">" );
	xstr = xstr.replace( /:\)\)/g,  i + "18.gif\">" );
	xstr = xstr.replace( /\(:\|/g,  i + a + "31.gif\">" );
	xstr = xstr.replace( /:\(\|\)/g, i + a + "38.gif\">" );
	xstr = xstr.replace( /:\)>-/g, i + "57.gif\">" );
	xstr = xstr.replace( /\[-X/g,  i + "58.gif\">" );
	xstr = xstr.replace( /\\:D\//g, i + "59.gif\">" );
	xstr = xstr.replace( />:D</g, i + "60.gif\">" );

	xstr = xstr.replace( /:\)/g,   i + "01.gif\">" );
	xstr = xstr.replace( /:\(/g,   i + "02.gif\">" );
	xstr = xstr.replace( /;\)/g,   i + "03.gif\">" );
	xstr = xstr.replace( /:D/g,   i + a + "04.gif\">" );
	xstr = xstr.replace( /:-\//g,  i + a + "06.gif\">" );
	xstr = xstr.replace( /:x/g,   i + "07.gif\">" );
	xstr = xstr.replace( /:">/g,  i + "08.gif\">" );
	xstr = xstr.replace( /:p/g,   i + a + "09.gif\">" );
	xstr = xstr.replace( /:\*/g,   i + "10.gif\">" );
	xstr = xstr.replace( /:O/g,   i + a + "11.gif\">" );
	xstr = xstr.replace( /X-\(/g,  i + a + "12.gif\">" );
	xstr = xstr.replace( /:>/g,   i + a + "13.gif\">" );
	xstr = xstr.replace( /B-\)/g,  i + a + "14.gif\">" );
	xstr = xstr.replace( /:-s/g,  i + a + "15.gif\">" );
	xstr = xstr.replace( /:\|/g,   i + a + "19.gif\">" );
	xstr = xstr.replace( /:-B/g,  i + "22.gif\">" );
	xstr = xstr.replace( /=;/g,   i + a + "23.gif\">" );
	xstr = xstr.replace( /I-\)/g,  i + a + "24.gif\">" );
	xstr = xstr.replace( /8-\|/g,  i + "25.gif\">" );
	xstr = xstr.replace( /:-&/g,  i + a + "26.gif\">" );
	xstr = xstr.replace( /:-$/g,  i + a + "27.gif\">" );
	xstr = xstr.replace( /\[-\(/g,  i + "28.gif\">" );
	xstr = xstr.replace( /:o\)/g,  i + a + "29.gif\">" );
	xstr = xstr.replace( /8-\}/g,  i + a + "30.gif\">" );
	xstr = xstr.replace( /=P~/g,  i + "32.gif\">" );
	xstr = xstr.replace( /:-\?/g,  i + "33.gif\">" );
	xstr = xstr.replace( /#-o/g,  i + "34.gif\">" );
	xstr = xstr.replace( /=D>/g,  i + a + "35.gif\">" );
	xstr = xstr.replace( /:@\)/g,  i + a + "36.gif\">" );
	xstr = xstr.replace( /3:-O/g, i + a + "37.gif\">" );
	xstr = xstr.replace( /@\};-/g, i + "40.gif\">" );
	xstr = xstr.replace( /%%-/g,  i + "41.gif\">" );

	xstr = xstr.replace( /\*\*==/, i + "42.gif\">" );
	xstr = xstr.replace( /\(~~\)/, i + "43.gif\">" );
	xstr = xstr.replace( /~o\)/,  i + "44.gif\">" );
	xstr = xstr.replace( /8-X/,  i + "46.gif\">" );
	xstr = xstr.replace( />-\)/,  i + "48.gif\">" );
	xstr = xstr.replace( /:-L/,  i + "49.gif\">" );
	xstr = xstr.replace( /\[-o</, i + "51.gif\">" );
	xstr = xstr.replace( /@-\)/,  i + "52.gif\">" );
	xstr = xstr.replace( /\$-\)/,  i + "53.gif\">" );

	xstr = xstr.replace( /:\^o/, i + "55.gif\">" );
	xstr = xstr.replace( /b-\(/, i + "56.gif\">" );


	//alert(xstr);
	return xstr;

}

