var sourceObj = null;
var typeWindow = null;
var message = '';
var typedPortion = '';
var cursorChar = '';
var cursorHTML = '<span class="cursorChar">_</span>';
var workHTML = '';

function initTypewriter(sourceId, newSpeed)
{
sourceObj = sourceId;
typeWindow = document.getElementById('typeWindow');
typeWindow.innerHTML = '';
message = sourceObj;
msgLength = message.length;
HTMLstr = '';
workChar = '';
count = 0;
speed = newSpeed;

typing = setInterval('typeText();', speed);
}

function typeText()
{
if (count == msgLength)
{
clearInterval(typing);
return;
}
else if (count == 0)
typedPortion = '';
else
typedPortion = message.substring(0, count)
cursorChar = message.charAt(count);
if (/</.test(cursorChar))
{
var tag = message.substring(count).match(/<\/?[^>]+>/);
if (tag)
{
typedPortion += tag[0];
count += tag[0].length;
}
}
else
{
workHTML = '';
workHTML += typedPortion;
if (count != msgLength - 1)
workHTML += cursorHTML.replace(/@/, cursorChar);
typeWindow.innerHTML = workHTML;
count++;
}
}