// add some HTML content when page loads
function writeTags() {
        // checks if element with a "commPrev" id exists at all
        if (document.getElementById('commPrev')) {
                // and adds HTML tags which will carry some content
                document.getElementById('commPrev').innerHTML = "<div>You are <a href=\"\"><span id=\"namePrev\">John Doe<\/span><\/a>, and you are saying:<\/div><div id=\"messagePrev\"><\/div>";
        }
}
// Comments Preview Box
function commPrev() {
        var komentarBox = document.getElementById('commentform');
        // checks if there is existence of form with an id "commentform"
        if (komentarBox) {
                // when "author" field looses focus
                document.getElementById('author').onblur = function() {
                        // write in the "namePrev" content of the "author" field
                        document.getElementById('namePrev').innerHTML = document.getElementById('author').value.replace(/(\n|\r)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'\/p><p>');
                        // write uri into the href attribute of a link to "author's" web site
                        document.getElementById('commPrev').getElementsByTagName('div')[0].getElementsByTagName('a')[0].setAttribute('href','http://'+document.getElementById('url').value.replace('http://',''));
                        // write in the "messagePrev" content of the "comment" field
                        document.getElementById('messagePrev').innerHTML = '<p>'+document.getElementById('comment').value.replace(/(\n|\r)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'\/p><p>')+'<'+'\/p>';
                }
                // following are doing the same thing as above, but are triggered by "onkeyup" event
                document.getElementById('text').onkeyup = function() {
                        document.getElementById('messagePrev').innerHTML = '<p>'+document.getElementById('text').value.replace(/(\n|\r)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'\/p><p>')+'<'+'\/p>';
                }
                document.getElementById('author').onkeyup = function() {
                        document.getElementById('namePrev').innerHTML = document.getElementById('author').value.replace(/(\n|\r)/g,'<br />').replace(/(<br \/>){2,}/gi,'<'+'\/p><p>');
                }
                document.getElementById('url').onkeyup = function() {
                        document.getElementById('commPrev').getElementsByTagName('div')[0].getElementsByTagName('a')[0].setAttribute('href','http://'+document.getElementById('url').value.replace('http://',''));
                }
        }
}
window.onload = function() {
        writeTags();
        commPrev();
}