function populateName()
{
    var elm = document.getElementById("fullname");
    if (elm) {
        var sName = getCookie("forumName");
        if (sName != "" && sName != null) {
            if (elm.value == "") {
                elm.value = sName;
            }
        }
    }
}

function getNiceDomain(url)
{
    var maxLength = 22;
    url = url.replace(/www./, "");

    var leadSlashes = url.indexOf('//');
    var domainStart = leadSlashes+2;
    var withoutResource = url.substring(domainStart, url.length);
    var nextSlash = withoutResource.indexOf('/');
    var domain = withoutResource.substring(0, nextSlash);
    if (domain.indexOf(':') > -1) {
        var portColon = domain.indexOf(':');
        domain = domain.substring(0, portColon);
    }

    if (domain.length >= maxLength) {
        domain = domain.substring(0, maxLength) + "...";
    }

    return domain;
}

function reduceUrl(url) {
    var maxLength = 32;
    url = url.replace(/www./, "");
    url = url.replace(/www./, "");

    var leadSlashes = url.indexOf('//');
    var domainStart = leadSlashes + 2;
    url = url.substring(domainStart, url.length);

    if (url.length >= maxLength) {
        url = url.substring(0, maxLength) + "...";
    }
    return url;
}

function backlinkUrlOk(url, title, postTitle)
{
    /*
    The purpose of this function is not to guard against spam (well, that too),
    but to try to restrict to only those backlinks to a post where one can find
    additional commentary to read up on -- hence the inclusion of the backlinks
    in the comments section of a post. That is why link wrap-up blog posts,
    translations, and blog post using the same title (often automated reprints)
    are currently excluded.
    */

    title = normalizeTitle(title);
    postTitle = normalizeTitle(postTitle);

    return !(url.indexOf("blogoscoped.cn") > -1 ||
            url.indexOf("blogoscoped.com") > -1 ||
            url.indexOf("big-boobs") > -1 ||
            url.indexOf("mansbags") > -1 ||
            url.indexOf("kudela.pl") > -1 ||
            title == postTitle ||
            title.indexOf("searchcap") > -1 ||
            title.indexOf("link finds") > -1 ||
            title.indexOf("daily") > -1 ||
            title.indexOf("linkpost") > -1
            );
}

function normalizeTitle(s)
{
    var newS = "";
    s = s.toLowerCase();
    var abc = "abcdefghijklmnopqrstuvwxyz ";
    for (var i = 0; i < s.length; i++) {
        var letter = s.charAt(i);
        if ( abc.indexOf(letter) >= 0 ) {
            newS += letter;
        }
    }
    return newS;
}

function getPersonImage(fullname, url)
{
    s = '';

    switch ( fullname.toLowerCase() ) {
        case 'brian mingus': s = 'brian-mingus'; break;
        case 'justin flavin': s = 'justin-flavin'; break;
        // function currently not needed

        /* Backlinks-specific icons */
        case 'mediadonis': s = 'mediadonis'; break;
        case 'jens': if ( url.indexOf('googlewatchblog.de') > -1 ) { s = 'google-watch-blog'; } break;
    }

    if (s == "") { s = 'forum-author'; }

    s = "/forum/image/" + s + ".gif";

    return s;
}

function showForumVideo(n, videoId)
{
    expand(n);
    var elm = document.getElementById("expandee" + n);
    if (elm) {
        elm.innerHTML = "<a href=\"javascript:hideForumVideo(" + n + ")\">- Hide video</a><br />" +
                "<object style=\"width: 500px; height: 412px\"><param name=\"movie\" " +
                "value=\"http://www.youtube.com/v/" + videoId + "&amp;autoplay=1\"></param>" +
                "<embed src=\"http://www.youtube.com/v/" + videoId + "&amp;autoplay=1\" type=\"application/x-shockwave-flash\" " +
                "style=\"width: 500px; height: 412px\"></object>" +
                "<br /><a href=\"http://youtube.com/watch?v=" + videoId + "\" style=\"color: #888; font-size: 85%\">" +
                "youtube.com/watch?v=" + videoId + "</a>";
    }
}

function showForumSketchcast(n, videoId)
{
    expand(n);
    var elm = document.getElementById("expandee" + n);
    if (elm) {
        elm.innerHTML = "<a href=\"javascript:hideForumVideo(" + n + ")\">- Hide sketchcast</a><br />" +
                "<object width=\"425\" height=\"350\"><param name=\"movie\" value=\"http://sketchcast.com/swf/player.swf?id=" + videoId + "&amp;autoplay=yes\"></param>" +
                "<param name=\"wmode\" value=\"transparent\"></param><embed src=\"http://sketchcast.com/swf/player.swf?id=" + videoId + "&amp;autoplay=yes\" " +
                "type=\"application/x-shockwave-flash\" wmode=\"transparent\" width=\"425\" height=\"350\"></embed></object>" +
                "<br /><a href=\"http://sketchcast.com/view/" + videoId + "\" style=\"color: #888; font-size: 85%\">" +
                "sketchcast.com/view/" + videoId + "</a>";
    }
}

function showForumFlash(n, videoUrl)
{
    expand(n);
    var elm = document.getElementById("expandee" + n);
    if (elm) {
        elm.innerHTML = "<a href=\"javascript:hideForumVideo(" + n + ")\">- Hide Flash</a><br />" +
                "<iframe src=\"" + videoUrl + "\" style=\"width: 500px; height: 450px; border: 1px solid #888\"></iframe>" +
                "<br /><a href=\"" + videoUrl + "\" style=\"color: #888; font-size: 85%\">" + reduceUrl(videoUrl) + "</a>";
    }
}

function hideForumVideo(n)
{
    var elm = document.getElementById("expandee" + n);
    if (elm) {
        elm.innerHTML = "&nbsp;";
    }
    collapse(n);
}

function UCWords(str)
{
    // via http://wiki.eclipse.org/Global_Functions_-_Useful_JavaScript_Functions_(BIRT)

   arrStr = str.split(" ");

   var strOut = "";
   for (i=0; i < arrStr.length; i++) {
       firstChar = arrStr[i].substring(0,1);
       remainChar = arrStr[i].substring(1);

       firstChar = firstChar.toUpperCase(); 
       remainChar = remainChar.toLowerCase();

       strOut += firstChar + remainChar + " ";
   }

   return strOut.substring(0, strOut.length - 1);
}