
// script highlighting <script>
TV2CommentSubmit = {
    trim : function (str) {
        return str.replace(/^\s*|\s*$/g,"");
    },
    checkArticleCommentForm : function(form) {
        if (this.trim(form.articleComment.value) == '') {
            alert('Husk at udfylde kommentarfeltet før du sender din kommentar.');
            return false;
        }
        return true;
    }
};

(function() {
    var actionUrl = 'http://1234-dyn.tv2.dk/js/uu/comments/action.php';
    var nodeId = 22528864;
    var scope = 'programmer';
    var username = 'anonym';
    var loggedIn = false;
    var tv2login = {};
    var pageURL = '';
    if (window.location) { pageURL = window.location; }
    else { pageURL = document.location.href; }
    
    // Check if user is logged in - if so, get username and id, save it in the array
    var c_entries = document.cookie.split(';');
    for (var i = 0; i < c_entries.length; i++) {
        // Handle cookies here
        var ce = c_entries[i].split('=');
        if (ce.length < 2) continue;
        
        var key = ce[0].replace(/^\s+|\s+$/g, '');
        var value = unescape(ce[1]);
        if (key != 'tv2login') continue;
        
        // We have got tv2login from cookie
        var keyvalpairs = value.split('&');
        for (var j = 0; j < keyvalpairs.length; j++ ) {
            var tmp = keyvalpairs[j].split('=');
            if (tmp.length < 2) continue;
            tv2login[tmp[0]]= tmp[1];
        }
        
        break;
    }

    // Check if user is logged in
    if (tv2login.username && tv2login.id) loggedIn = true;
    
    var h =
        '<div style="display:block; float: left; width: 480px; clear:both">' + 
        '<a name="writeArticleComment">&nbsp;</a>' +
        '<form class="articleCommentForm" id="articleCommentForm" name="articleCommentForm" onsubmit="return TV2CommentSubmit.checkArticleCommentForm(this);" method="post" action="'+actionUrl+'">' +
        '  <fieldset>' +
        '    <legend>Skriv din kommentar</legend>' +
        '    <label><strong>Afsender</strong></label><br />' +
        '    <input disabled="" value="' + tv2login.username + '" /><br /><br />' +
        '    <label><strong>Kommentar</strong></label><br />' +
        '    <textarea class="articleCommentMessage" id="articleComment" name="articleComment" rows="6"></textarea><br />' +
        '    <input type="submit" value="Send kommentar" class="articleCommentSubmit" />' +
        '    <input type="hidden" name="nodeId" value="' + nodeId + '" /><input type="hidden" name="scope" value="' + scope + '" />' +
        '    <input type="hidden" name="pageURL" value="' + pageURL + '" />' +
        '  </fieldset>' +
        '</form>'+
        '</div>';

    if (loggedIn) { document.write(h); }
    else { document.write('<div class="commentsLoginNotice" style="color:red;">Log ind med dit TV 2|Login i boksen øverst til højre for at kommentere artiklen.</div>'); }
})();
// end of script highlighting </script>

