function filterCaseDup($str, $threshold, $dup)
{
	var $c;
	var $count = 0;
	var $plain = 0;
	var $convert = false;
	var $html = false;
	// Calculating amount of uppercase chars.
	for (var $i = 0; $str.length > $i; ++$i) {
		$c = $str.charAt($i);
		if ('<' == $c) {
			$html = true;
		}
		// If we are in html tag then no case check is made
		if (true == $html) {
			if ('>' == $c) {
				$html = false;
			}
			continue;
		}
		if ('A' <= $c && 'Z' >= $c || 'А' <= $c && 'Я' >= $c) {
			++$count;
		}
		++$plain;
	}
	if ($count >= $plain * $threshold) {
		$convert = true;
	}
	var $low;
	var $res = '';
	var $last = ' ';
	var $lastCnt = $dup;
	var $brk = true;
	$html = false;
	for (var $i = 0; $str.length > $i; ++$i) {
		$c = $str.charAt($i);
		if ('<' == $c) {
			$html = true;
		}
		// If we are in html tag then no convertion is made
		if (true == $html) {
			if ('>' == $c) {
				$html = false;
			}
			$res += $c;
			continue;
		}
		// Checking for ending of the sentence.
		if (true == $convert && ('.' == $c || '!' == $c || '?' == $c)) {
			$brk = true;
			$low = $c;
		

		// Performing case convertion.
		} else if ('A' <= $c && 'Z' >= $c ||
				'А' <= $c && 'Я' >= $c) {
			$low = $c.toLowerCase();
		} else {
			$low = $c;
		}
		// Checking for duplicate characters ignoring the case.
		if ($last == $low) {
			++$lastCnt;
			if ($dup < $lastCnt) {
				continue;
			}
		} else {
			$last = $low;
			$lastCnt = 1;
		}

		
		// If we aren't at the begining of the sentence.
		if (false == $brk || true == $html) {
			$res += $low;
		} else {
			// If got any meaningful char, we are not more
			// at the sentence start.
			if (true == $convert && true == $brk && (
					'0' <= $low && '9' >= $low ||
					'a' <= $low && 'z' >= $low ||
					'а' <= $low && 'я' >= $low)) {
				$brk = false;
			}
			$res += $c;
		}
	}
	return $res;
}

/*function renderSmiles1($res1)
{
	var $dict1 = ['123123'];

	var $res1 = '123123';
			$res1 += '<img src=\'http://detskij.chatium.com/pages/channel/img/supermoder.gif\'>';
	return $res1;
}
*/


function renderSmiles($src, $limit)
{
	var $smiles = [ ':)', ':(', ';)', ':*', ':D' ];
	// Keep first elements of $dict array mathcing to elements in $smiles
	var $dict = [ 'smile', 'sad', 'wink', 'kiss', 'biggrin', 'blum1', 'blush', 'bomb', 'bye2', 'cool', 'cray', 'crazy', 'dance', 'diablo', 'drinks', 'gamer', 'girl_angel', 'give_heart', 'give_rose', 'good', 'hang1', 'hi', 'ireful', 'i_am_so_happy', 'kiss3', 'lol', 'mad', 'man_in_love', 'mocking', 'music', 'nea', 'pardon', 'rofl', 'scratch_one-s_head', 'shok', 'shout', 'air_kiss', 'sorry', 'unknown', 'wacko1', 'dash1', 'mail1', 'sarcastic', 'wacko2', 'wacko3', 'mamba', 'bad', 'yahoo', 'yes', 'girl_phone1', 'girl_haha', 'girl_flag_of_truce', 'girl_in_dreams', 'girl_blush', 'angel_old', 'ban_old', 'bigeyes_old', 'censored_old', 'clap_old', 'doctor_old', 'fight_old', 'help_old', 'insane_old', 'laught_old', 'ninja_old', 'love_old', 'sad_old', 'sleep_old', 'smile2_old', 'smile3_old', 'smile_old', 'super_old', 'tease_old', 'think_old', 'verybigeyes_old', 'weep_old', 'fool', 'connie_threaten', 'flag_of_truce' ,'wink_old'];
	var $sp = $src.indexOf(':');
	var $wp = $src.indexOf(';');
	var $dp = $src.indexOf('!');
	var $lp = 0;
	if (-1 == $sp && -1 == $wp && -1 == $dp) {
		return $src;
	}
	var $i = 0;
	var $res = '';
	var $last = 0;
	var $count = 0;
        while (-1 != $sp || -1 != $wp || -1 != $dp) {
        	$lp = -1 != $sp && $sp < $wp || -1 == $wp ? $sp : $wp;
		if (-1 != $lp && ($lp < $dp || -1 == $dp)) {
			for ($i = 0; $smiles.length > $i; ++$i) {
				$sml = $smiles[$i];
				if ($src.substring($lp, $lp + $sml.length) == $sml) {
					$res += $src.substring($last, $lp);
					if ($limit > $count) {
						$res += '<img src=\'/pages/channel/smiles/gal1/' + $dict[$i] + '.gif\'>';
					}
					$last = $lp + $sml.length;
					if (-1 != $sp && $sp < $wp || -1 == $wp) {
						$sp = $src.indexOf(':', $last);
					} else {
						$wp = $src.indexOf(';', $last);
					}
					++$count;
					break;
				}
			}
			if ($smiles.length == $i) {
				if (-1 != $sp && $sp < $wp || -1 == $wp) {
					$sp = $src.indexOf(':', $sp + 1);
				} else {
					$wp = $src.indexOf(';', $wp + 1);
				}
			}
			continue;
		}
		if (-1 != $dp && ($dp < $lp || -1 == $lp)) {
			for ($i = 0; $dict.length > $i; ++$i) {
				$sml = $dict[$i];
				if ($src.substring($dp + 1, $dp + 1 + $sml.length) == $sml &&
						'!' == $src.charAt($dp + 1 + $sml.length)) {
					$res += $src.substring($last, $dp);
					if ($limit > $count) {
						$res += '<img src=\'/pages/channel/smiles/gal1/' + $dict[$i] + '.gif\'>';
					}
					$last = $dp + $sml.length + 2;
					$dp = $src.indexOf('!', $last);
					++$count;
					break;
				}
			}
			if ($dict.length == $i) {
				$dp = $src.indexOf('!', $dp + 1);
			}
		}
	}
	$res += $src.substring($last, $src.length);
	return $res;
}

