var APIURI = "/";
function getCookie(c_name) {
    if (document.cookie.length>0) {
        c_start=document.cookie.indexOf(c_name + "=");
        if (c_start!=-1) {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) {
                c_end=document.cookie.length;
            }
            return unescape(document.cookie.substring(c_start,c_end));
        }
    }
    return null;
}
function loggedIn() {
    var loginCookie = getCookie("WUZ-Login");
    return (loginCookie != null);
} 
var commentform_visible = false;
var activeForm = null;
function showCommentForm(_id) {        
    if (commentform_visible) {
        commentform_visible = false;
        if ($("comment.1.anonymous."+_id)) $("comment.1.anonymous."+_id).hide();
        if ($("comment.1.loggedin."+_id)) $("comment.1.loggedin."+_id).hide();	
        return;   
    }
    commentform_visible = true;
    // Show/hide forms	
    //if (Element.visible($("comment.1."+_id))) {
    if ($("comment.1.err.email." +      _id)) $("comment.1.err.email." +      _id).hide();
    if ($("comment.1.err.comment." +    _id)) $("comment.1.err.comment." +    _id).hide();
    if ($("comment.1.err.agree." +      _id)) $("comment.1.err.agree." +      _id).hide();            
    //}        
    if (loggedIn()) {
        activeForm = activeForm != null ? null : "comment.1.form."+_id;
        //if ($("comment.1.userinfo."+_id)) $("comment.1.userinfo."+_id).hide();
        if ($("comment.1.anonymous."+_id)) $("comment.1.anonymous."+_id).hide();
        if ($("comment.1.loggedin."+_id)) $("comment.1.loggedin."+_id).show();
        if ($("commentArea")) $("commentArea").focus();
            
    } else {      
        activeForm = activeForm != null ? null : "comment.1.anonymous.form."+_id;      
        //if ($("comment.1.userinfo."+_id)) $("comment.1.userinfo."+_id).show();
        if ($("comment.1.loggedin."+_id)) $("comment.1.loggedin."+_id).hide();
        if ($("comment.1.anonymous."+_id)) $("comment.1.anonymous."+_id).show();                        
        if ($("nameField")) $("nameField").focus();
    } 
    //$( "comment.1."+_id ).toggle();
    //$( 'comment.2.'+_id ).hide();
    $( 'forward.1.'+_id ).hide();
    $( 'forward.2.'+_id ).hide();
    $( 'complaint.1.'+_id ).hide();
    $( 'complaint.2.'+_id ).hide();        
    clearFields( 'comment.1.form.'+_id );    
}
function showForwardForm(_id) {
    activeForm = activeForm != null ? null : "forward.1.form."+_id;
    // Show/hide forms	
    if ($("comment.1.anonymous."+_id)) $("comment.1.anonymous."+_id).hide();
    if ($("comment.1.loggedin."+_id)) $("comment.1.loggedin."+_id).hide();	
    commentform_visible=false;
    $( 'forward.1.'+_id ).toggle();
    $( 'forward.2.'+_id ).hide();
    $( 'complaint.1.'+_id ).hide();
    $( 'complaint.2.'+_id ).hide();
    clearFields( 'forward.1.form.'+_id );    
}

/*
function showRateForm(_id) {
    $("comment.1." + _id).hide();
    $("comment.2." + _id).hide();
    $("forward.1." + _id).hide();
    $("forward.2." + _id).hide();
    $("rate.1." + _id).show();
    $("rate.2." + _id).hide();
}
 */

// Show the complaints form and hide all other, possibly, opened forms
// _id => element id (string)
function showComplaintForm( _id ) {
    activeForm = activeForm != null ? null : "complaint.1.form."+_id;
    // Show/hide forms  
    if ($("comment.1.anonymous."+_id)) $("comment.1.anonymous."+_id).hide();
    if ($("comment.1.loggedin."+_id)) $("comment.1.loggedin."+_id).hide();	
    commentform_visible=false;
    if ($( 'forward.1.'+_id   )) $( 'forward.1.'+_id   ).hide();
    if ($( 'forward.2.'+_id   )) $( 'forward.2.'+_id   ).hide();
    if ($( 'complaint.1.'+_id )) $( 'complaint.1.'+_id ).toggle();
    if ($( 'complaint.2.'+_id )) $( 'complaint.2.'+_id ).hide();
    zeroField( 'complaint.1.form.'+_id );
}

function handleButtonRoll(_elem, _op, _img) {
    if (_op == "over") {
        _elem.setAttribute("prev_src", _elem.src);
        if (_img.lastIndexOf("/") <= 0 ) {
            _elem.src = "/images/buttons/" + _img + "_over.gif";
        }
        else {
            _elem.src = _img + "_over.gif";
        }
	_elem.style.cursor = "pointer";        
    } else {
        _elem.src = _elem.getAttribute("prev_src");
	_elem.style.cursor = "";        
    }
}
function handleButtonClick(_formname, _id) {
    if ( _formname == "comment" ) {
        var loginCookie = getCookie( "WUZ-Login" );
        if (loginCookie == null) {
            processCommentForm( _id, true);
        }
        else {
            processCommentForm( _id, false);
        }
    } else if ( _formname== "comment.loggedin" ) {        
        processCommentForm( _id, false);
    } else if ( _formname == "forward" ) {
        processForwardForm( _id );
    } else if ( _formname == "rate" ) {
        processRateForm( _id );
    }	else if ( _formname == "complaint" ) {
        processComplaintForm( _id );
    }		 
}

// ========================
// = Process Comment Form =
// ========================
function processCommentForm(_id, checkForEmail) { 
    // prepare form    
    if (checkForEmail) {
        $("comment.1.err.email." + _id).hide();
    }
    $("comment.1.err.comment." + _id).hide();
    // prepare vars
    var form = $("comment.1.form." + _id);
    var fields  = form.getElements();   
    var comment = escape(fields[0].value.replace(/\n/gi,'<br />').strip()); /* fields[3].value.strip(); */        
    var comment_status  = is_not_blank(comment);
    var agree_status    = ($("comment.agree."+ _id).checked == true);
    // In case of no client-side validation errors, let's see what the server thinks...
    if (/* email_status && */ comment_status && agree_status /* && alias_status && city_status */) {
        $("comment.1.spinner." + _id).show();
        args = "article_id=" + _id + /* "&email=" + email + */ "&comment=" + comment /* + "&alias=" + alias + "&city=" + city */;
        /*
        new Ajax.Request(APIURI + "react?" + args, {method:"get", onSuccess:function (transport) {
            commentResponseOK(transport.responseText);
        }, onFailure:function (transport) {
            commentResponseError(transport);
        }});
         */
        $("void-frame").src=APIURI + "react?" + args;
        commentResponseOK("[{ \"rtype\": \"response\", \"key\": \"article_id\", \"value\": \""+ _id +"\" }]");
        //window.location.href
       
    } else {
        // Display client-side validation errors
        processCommentResult(_id, /* email_status, */ comment_status, true, agree_status /*, alias_status, city_status */);
    }
}
/*
	JSON return values:

	On succes => 200 OK + JSON response =>
	  [
	    { "rtype": "response", "key": "article_id", "value": "{article_id}" }
	  ]

	On error  => 400 Bad Request + JSON response =>
	  [
	    { "type": "response", "key": "article_id", "value": "{article_id}" }
	    { "type": "error", "code": "1", "msg": "email argument is missing"},
	    { "type": "error", "code": "2", "msg": "comment argument is missing"},
	    { "type": "error", "code": "3", "msg": "article_id argument is missing"}
	  ]
 */
function commentResponseOK(transportTxt) {    
    commentResponseOK_(eval("(" + transportTxt + ")"));        
}

function commentResponseOK_(obj) {
    if (obj[0].rtype == "response") {
        if (obj[0].key == "article_id") {            
            processCommentResult(obj[0].value, /*true,*/ true, true, true /*, true, true*/);
            // Omniture:            
            if (typeof(s) != 'undefined') {
                s.events="event3";            
                s.eVar20='article';
            }
            hrf=window.location.href;
            if (hrf.indexOf("#") > -1) {
                hrf=hrf.substring(0, hrf.indexOf("#"));
            }	                        
            tok = hrf.indexOf("?") == -1 ? "?":"&";
            evnt="window.location.href='"+hrf+tok+"reloaded=true'";           
            setTimeout(evnt,600);
            void(s.t());                   
        }        
    } 
}
function commentResponseError(transport) {
    var email_status = true;
    var comment_status = true;
    var other_status = true;
    var agree_status = true;
    
    // Create object from JSON payload
    var obj = eval("(" + transport.responseText + ")");
    // Parse response
    var article_id = null;
    for (i = 0; i < obj.length; i++) {
        if (obj[i].rtype == "response") {
            if (obj[i].key == "article_id") {
                article_id = obj[i].value;
            }
        } else {
            if (obj[i].rtype == "error") {
                if (obj[i].code == "1") {
                    email_status = false;
                } else {
                    if (obj[i].code == "2") {
                        comment_status = false;
                    } else {
                        other_status = false;
                    }
                }
            }
        }
    }      
    // Display server-side (validation) errors
    if (article_id != null) {        
        processCommentResult(article_id, /*email_status,*/ comment_status, other_status, agree_status /*, true, true */);
    }
}
function hide(_id) {
    if ($(_id)) $(_id).hide();
}
function show(_id) {
    if ($(_id)) $(_id).show();
}
function toggle(_id) {
    if ($(_id)) $(_id).toggle();
}
function update(_id, value) {
    if ($(_id)) $(_id).update(value);
}

function showOrHideError(_id, _status, fieldname, message) {
    if (!_status) {
        update("comment.1.err." +fieldname + "." + _id, "<span class='err2'>" + message + "</span>");
        show("comment.1.err."   +fieldname + "." + _id);        
    }
    else {
        hide("comment.1.err."   +fieldname + "." + _id);        
    }   
}

function processCommentResult(_id, _comment_status, _other_status, _agree_status) {    
    showOrHideError(_id, _agree_status,     "agree",    "U dient akkoord te gaan met de huisregels");    
    showOrHideError(_id, _comment_status,   "comment",  "Het reactieveld is nog leeg");
       
    if ((_comment_status && _agree_status /* &&  _alias_status && _city_status */) || !_other_status) {
        if (!_other_status) {
            update("comment.2.response." + _id, "Er is een fout opgetreden. Probeert u het svp later opnieuw.");
        } else {
            update("comment.2.response." + _id, "Dank voor uw reactie.");
            if (document.location.href.indexOf('-')>0)
            {
            	document.location.href=document.location.href+(Math.random()+"").substring(3,8);
	    }
        }
        hide("comment.1." + _id);
        show("comment.2." + _id);
    }
    hide("comment.1.spinner." + _id);
}

// ========================
// = Process Forward Form =
// ========================
function processForwardForm(_id) {
    // prepare form
    $("forward.1.err.email." + _id).hide();
    // prepare vars
    var form = $("forward.1.form." + _id);
    var fields = form.getElements();
    var email = fields[0].value.strip();
    var name = fields[1].value.strip();
    var remarks = fields[2].value.strip();
    var email_status = is_valid_email_address(email);
    // In case of no client-side validation errors, let's see what the server thinks...
    if (email_status) {
        $("forward.1.spinner." + _id).show();
        args = "article_id=" + _id + "&email=" + email +"&name=" + name + "&remarks=" + remarks;
        new Ajax.Request(APIURI + "forward?" + args, {method:"get", onSuccess:function (transport) {
                forwardResponseOK(transport);
            }, onFailure:function (transport) {
                forwardResponseError(transport);
            }});
    } else {
        // Display client-side validation errors
        processForwardResult(_id, email, email_status, true);
    }
}
/*
	JSON return values:

	On succes => 200 OK + JSON response =>
	  [
	    { "rtype": "response", "key": "article_id", "value": "{article_id}" }
	  ]

	On error  => 400 Bad Request + JSON response =>
	  [
	    { "type": "response", "key": "article_id", "value": "{article_id}" }
		  { "type": "error", "code": "1", "msg": "email argument is missing"},
	    { "type": "error", "code": "2", "msg": "article_id argument is missing"}
	  ]
 */
function forwardResponseOK(transport) {
    // Create object from JSON payload
    var obj = eval("(" + transport.responseText + ")");
    // Parse response
    var article_id = null;
    var email = null;
    for (i = 0; i < obj.length; i++) {
        if (obj[i].rtype == "response") {
            if (obj[i].key == "article_id") {
                article_id = obj[i].value;
            }
            if (obj[i].key == "email") {
                email = obj[i].value;
            }
        }
    }
    if (article_id != null && email != null) {
        processForwardResult(article_id, email, true, true);     
        // Omniture:
        if (typeof(s) != 'undefined') {
            s.events="event6";
            s.eVar18=''+article_id;
            s.eVar20='article';
            void(s.t());
        }
    }
}
function forwardResponseError(transport) {
    var email_status = true;
    var other_status = true;
    // Create object from JSON payload
    var obj = eval("(" + transport.responseText + ")");
    // Parse response
    var article_id = null;
    var email = null;
    for (i = 0; i < obj.length; i++) {
        if (obj[i].rtype == "response") {
            if (obj[i].key == "article_id") {
                article_id = obj[i].value;
            }
            if (obj[i].key == "email") {
                email = obj[i].value;
            }
        } else {
            if (obj[i].rtype == "error") {
                if (obj[i].code == "1") {
                    email_status = false;
                } else {
                    other_status = false;
                }
            }
        }
    }      
    // Display server-side (validation) errors
    if (article_id != null && email != null) {
        processForwardResult(article_id, email, email_status, other_status);
    }
}
function processForwardResult(_id, _email, _email_status, _other_status) {
    if (!_email_status) {
        $("forward.1.err.email." + _id).update("<span class='err2'>Dit lijkt niet op een geldig e-mailadres</span>");
        $("forward.1.err.email." + _id).show();
    }
    if (_email_status || !_other_status) {
        if (!_other_status) {
            $("forward.2.response." + _id).update("Er is een fout opgetreden. Probeert u het svp later opnieuw.");
        } else {
            $("forward.2.response." + _id).update("Het artikel is doorgestuurd naar " + _email);
        }
        $("forward.1." + _id).hide();
        $("forward.2." + _id).show();
    }
    $("forward.1.spinner." + _id).hide();
}

// =====================
// = Process Rate Form =
// =====================
function processRateForm(_id) {
    // prepare form
    $("rate.1.err.rating." + _id).hide();
    // prepare vars
    var form = $("rate.1.form." + _id);
    var fields = form.getElements();
    var rating = fields[0].value;
    var rating_status = rating >= 1 && rating <= 5 ? true : false;
    // In case of no client-side validation errors, let's see what the server thinks...
    if (rating_status) {
        $("rate.1.spinner." + _id).show();
        args = "article_id=" + _id + "&rating=" + rating;
        new Ajax.Request(APIURI + "rate?" + args, {method:"get", onSuccess:function (transport) {
                rateResponseOK(transport);
            }, onFailure:function (transport) {
                rateResponseError(transport);
            }});
    } else {
        // Display client-side validation errors
        processRateResult(_id, rating_status, true);
    }
}
/*
	JSON return values:

	On succes => 200 OK + JSON response =>
	  [
	    { "rtype": "response", "key": "article_id", "value": "{article_id}" }
	  ]

	On error  => 400 Bad Request + JSON response =>
	  [
	    { "rtype": "response", "key": "article_id", "value": "{article_id}" }
	    { "rtype": "error", "code": "1", "msg": "rating argument is missing"},
	  ]
 */
function rateResponseOK(transport) {
    // Create object from JSON payload
    var obj = eval("(" + transport.responseText + ")");
    // Parse response
    var article_id = null;
    for (i = 0; i < obj.length; i++) {
        if (obj[i].rtype == "response") {
            if (obj[i].key == "article_id") {
                article_id = obj[i].value;
            }
        }
    }
    if (article_id != null) {
        processRateResult(article_id, true, true);        
        // Omniture:
        if (typeof(s) != 'undefined') {
            s.events="event4";
            void(s.t());
        }
    }
}
function rateResponseError(transport) {
    var rating_status = true;
    var other_status = true;
    // Create object from JSON payload
    var obj = eval("(" + transport.responseText + ")");
    // Parse response	
    var article_id = null;
    for (i = 0; i < obj.length; i++) {
        if (obj[i].rtype == "response") {
            if (obj[i].key == "article_id") {
                article_id = obj[i].value;
            }
        } else {
            if (obj[i].rtype == "error") {
                if (obj[i].code == "1") {
                    rating_status = false;
                } else {
                    other_status = false;
                }
            }
        }
    }      
    // Display server-side (validation) errors
    if (article_id != null) {
        processRateResult(article_id, rating_status, other_status);
    }
}
function processRateResult(_id, _rating_status, _other_status) {
    if (!_rating_status) {
        $("rate.1.err.rating." + _id).update("<span class='err2'>Kies svp een waardering.</span>");
        $("rate.1.err.rating." + _id).show();
    }
    if (_rating_status || !_other_status) {
        if (!_other_status) {
            $("rate.2.response." + _id).update("Er is een fout opgetreden. Probeert u het svp later opnieuw.");
        } else {
            $("rate.2.response." + _id).update("Hartelijk dank voor uw beoordeling.");
        }
        $("rate.1." + _id).hide();
        $("rate.2." + _id).show();
    }
    $("rate.1.spinner." + _id).hide();
}

// ==========================
// = Process Complaint Form =
// ==========================

// This method tries to validate the complaint form client-side
// If OK, it sends the data to the server
// The server either accepts the data or returns a validation error
// _id => article ID (string)
function processComplaintForm( _id ) {
    // prepare form
    $( 'complaint.1.err.'+_id ).hide();
    // prepare vars
    var form = $( 'complaint.1.form.'+_id );
    var fields = form.getElements();
    var complaint_type = fields[0].value.strip();
    // In case of no client-side validation errors, let's see what the server thinks...
    if ( Number(complaint_type) != 0 ) {
        $( 'complaint.1.spinner.'+_id ).show();
        args = "article_id="+_id+"&complaint_type="+complaint_type;
        new Ajax.Request( APIURI + 'complaint?'+args,
        { method: 'post',
            onSuccess: function(transport) { complaintResponseOK(transport); } ,
            onFailure: function(transport) { complaintResponseError(transport); } 
        } );
    } else {
        // Display client-side validation errors
        processComplaintResult( _id, false );              
    }
}

/*
# The AJAX call (see above) may return this data:
# On succes => 200 OK + JSON response =>
#   [
#     { "rtype": "response", "key": "article_id", "value": "{article_id}" }
#   ]
#
# On error  => 400 Bad Request + JSON response =>
#   [
#     { "rtype": "response", "key": "article_id", "value": "{article_id}" }
#     { "rtype": "error", "code": "1", "msg": "complaint_type argument is missing" },
#   ]
 */

// On success...
// transport => Response object
function complaintResponseOK( transport ) {
    // Unmarshal JSON
    var obj = eval( '(' + transport.responseText + ')' );
    // Parse response
    var article_id = null;
    for ( i=0; i<obj.length; i++ ) {
        if ( obj[i].rtype=="response" ) {
            if ( obj[i].key=="article_id" ) { article_id = obj[i].value; }
        }
    }          
    if ( article_id != null ) {
        processComplaintResult( article_id, true );
    }
    // Omniture:
    if (typeof(s) != 'undefined') {
        s.pageName="klacht-over-artikel";
        void(s.t());
    }
}

// On error...
// transport => Response object
function complaintResponseError( transport ) {
    // prepare vars
    var complainttype_status = true;
    var article_id = null;
    // Unmarshal JSON
    var obj = eval( '(' + transport.responseText + ')' );
    // Parse response
    for ( i=0; i<obj.length; i++ ) {
        if ( obj[i].rtype=="response" ) {
            if ( obj[i].key=="article_id" ) { article_id = obj[i].value; }
        } else if ( obj[i].rtype=="error" ) {
            if ( obj[i].code=="1" ) { 
                complainttype_status = false;
            }
        }
    }      
    // Display server-side (validation) errors
    if ( article_id != null ) {
        processComplaintResult( article_id, complainttype_status );
    }
}

// This method displays client-side errors, server-side errors and success message
// _id    => article_id (string)
// _error => (boolean)
function processComplaintResult( _id, _complainttype_status ) {
    if ( _complainttype_status == false ) {
        $( 'complaint.1.err.'+_id ).update( "<span class='err2'>Kies svp een klachttype</span>" );
        $( 'complaint.1.err.'+_id ).show();
    } else {
        $( 'complaint.2.response.'+_id ).update( "Uw klacht is ontvangen en zal zorgvuldig behandeld worden." );
        $( 'complaint.1.'+_id ).hide();
        $( 'complaint.2.'+_id ).show();
    }
    $( 'complaint.1.spinner.'+_id ).hide();
}


// ================
// = Form Helpers =
// ================

// Clears previously filled in form fields
function clearFields( _id ) {
    var form = $( _id );
    var fields = form.getElements();
    for ( i=0; i<fields.length; i++ ) { fields[i].value = ""; }	
}

// Zeros drop-down form field
function zeroField( _id ) {
    var form = $( _id );
    var fields = form.getElements();
    for ( i=0; i<fields.length; i++ ) { fields[i].value = 0; }	
}

// This still sucks
function is_valid_email_address(_str) {
    var r = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
    if (r.test(_str)) {
        return true;
    }
    return false;
}
function is_not_blank(_str) {
    if (_str.length > 0) {
        return true;
    }
    return false;
}

//DP_Debug.logInfo( "here" );

// ========
// = Poll =
// ========
function getPoll() {
    new Ajax.Request(APIURI + "poll/get_poll", {method:"post", onSuccess:function (transport) {
            pollResponseOK(transport, false);
        }, onFailure:function (transport) {
            pollResponseError(transport, false);
        }});
}
function voteForPoll(_poll_id, _answer_id) {
    new Ajax.Request(APIURI + "poll/put_vote?pid=" + _poll_id + "&aid=" + _answer_id, {method:"post", onSuccess:function (transport) {
            pollResponseOK(transport, true);
        }, onFailure:function (transport) {
            pollResponseError(transport, true);
        }});
}

// send is_answer=false in order to present the question + possible answers
// send is_answer=true in order to present the question + answer stats so far
function pollResponseOK(transport, _is_answer) {

    // unmarshal JSON
    var obj = eval("(" + transport.responseText + ")");

    // poll.question's decendants are the answer elements (before and after casting vote)
    pq = $("poll.question");
    Element.extend(pq);
    pq.update(obj.poll.question);

    // remove any decendants
    var pollquestions = $("poll.questions");
    Element.extend(pollquestions);
    e = pollquestions.immediateDescendants();
    e.each(function (item) {
        item.remove();
    });

    // insert questions
    for (i = 0; i < obj.poll.answers.length; i++) {

        // show results
        if (_is_answer) {	
            // build <p> ... </p> and append to poll.questions
            var p = document.createElement("p");
            Element.extend(p);
            var str = "<strong>" + obj.poll.answers[i].percentage + "</strong>";
            str += " &mdash; " + obj.poll.answers[i].value;
            p.update(str);
            pollquestions.appendChild(p);

            // show questions
        } else {
            // build <p> <a ...> ... </a> </p> and append to poll.questions
            var a = document.createElement("a");
            Element.extend(a);
            var f = "voteForPoll(" + obj.poll.id + "," + (i + 1) + ");";
            a.setAttribute("href", "javascript:" + f);
            a.update(obj.poll.answers[i].value);
            var p = document.createElement("p");
            p.appendChild(a);
            pollquestions.appendChild(p);
        }
    }
}
function pollResponseError(transport) {
    $("polloftheday").hide();
}

// =================
// = Message Board =
// =================

// Obtains a list of messages for a particular zipcode
/// TODO: document JSON
var last_call_time = null;
function doMessageBoard( _zipcode ) {
    /*
	call_time = new Date();
	if ( last_call_time!=null && ( call_time - last_call_time ) < 5000 ) {
		return; // discard this event (prevents events avalance)
	}	
	last_call_time = call_time;
	var tbody = $( "msg.board" );
	var msgs = tbody.immediateDescendants();
	var last_msg_id = msgs[0].getAttribute( "msg_id" );
	new Ajax.Request( APIURI + 'msgboard/get_messages?zipcode='+_zipcode+'&last_msg_id='+last_msg_id+'&rnd='+Math.random(),
								  { method: 'get',
									  onSuccess: function(transport) { msgboardResponseOK(transport); } ,
									  onFailure: function(transport) { msgboardResponseError(transport); } 
									} );
     */
}

// Displays msgboard
function msgboardResponseOK( transport ) {
    var obj = eval( '(' + transport.responseText + ')' );
    // insert messages
    var tbody = $( "msg.board" );
    for ( var i=0; i<obj.length; i++ ) {
        // create tr
        var tr = document.createElement( 'tr' );
        tr.className = 'dotted_row';
        tr.setAttribute( 'msg_id', obj[i].msg_id );
        tr.id = "msgrow."+obj[i].msg_id;
        // create tr > td
        var td = document.createElement( 'td' );
        td.colSpan = 2;
        td.width = '395px';
        tr.appendChild( td );
        // create tr > td > div
        var div = document.createElement( 'div' );
        div.className = 'rowpad';
        div.setAttribute( 'style', 'overflow:hidden;width:395px;margin:0 0 3px;');
        td.appendChild( div );
        // create tr > td > div > a
        var a = document.createElement( 'a' );
        Element.extend( a );
        a.href = '/wuzlog/' + obj[i].user;
        a.update( obj[i].user ); 
        div.appendChild( a );

        var t = null;
        if (obj[i].minutesago<59)
        {
            t = document.createTextNode( " zei " + obj[i].minutesago + " minuten geleden: '" + obj[i].message + "'" );
        }
        else
            if (obj[i].minutesago>59 && obj[i].minutesago <1440 )
        {
            t = document.createTextNode( " zei " + Math.round(obj[i].minutesago/60) + " uur geleden: '" + obj[i].message + "'" );
        }
        else
        {
            t = document.createTextNode( " zei " + Math.round(obj[i].minutesago/1440) + " dagen geleden: '" + obj[i].message+ "'" );
        }



        div.appendChild( t );
        // insert new table row before topmost tr
        tbody.insertBefore( tr, tbody.firstChild );
    }
    // Delete old messages
    var msgs = tbody.immediateDescendants();
    if ( msgs.length > 7 ) { // msg board has always 2 more rows (textarea + send button)
        for ( var i=5; i<msgs.length; i++ ) {
            $( msgs[i].id ).remove();
        }
    }
    $( 'msg.board.spinner' ).hide();
}

function msgboardResponseError( transport ) {
    $( 'msg.board.spinner' ).hide();
}

function sendMsgBoardMessage(postcode) {
    $("msg.board.spinner").show();
    msg = $("msg.board.message");
    new Ajax.Request(APIURI + "msgboard/post_message?msg=" + msg.value + "&zipcode=" + postcode, {method:"post", onSuccess:function (transport) {
            msgboardResponseOK(transport);
        }, onFailure:function (transport) {
            msgboardResponseError(transport);
        }});
    msg.value = "";
}

// This methods adds 1 to the nr of votes and prints a thank you note.
// It then calls the server to send in the vote.
// It's up to the server to ignore repeat votes from the same user for the same article.
function voteForArticle( _id ) {

    args = "article_id="+_id;
    new Ajax.Request( APIURI + 'rate?'+args, { method: 'post', 
        onSuccess: function(transport) { checkForMultipleRating(transport, _id); } , 
        onFailure: function(transport) { } 
    } );                                                                               
}

function checkForMultipleRating(transport, _id) {    
    var alreadyVoted = new Boolean(false);
    var obj = eval("(" + transport.responseText + ")");         
    if (obj[0].rtype == "response") {      
        if (obj[0].key == "alreadyVoted") {
            alreadyVoted = new Boolean(true);
        }  
    }      
    if (alreadyVoted == true) {             
        alert("U heeft reeds gestemd op dit artikel");     
    }
    else {        
        /*  count = Number( $( "vote.count."+_id ).innerHTML );
        if ( count == Number.NaN ) count=0;
        $( "vote.count."+_id ).update( count+1 ); */
        var voteDiv = $("article-vote-button");
        var voteDivCount = $("article-vote-count");
        voteDiv.style.display ="none";
        var myCount = parseInt(  voteDivCount.innerHTML.replace(/<.*?>/g,'') );
        myCount++;
        voteDivCount.innerHTML = '<a style="color:#FF6600">'+myCount+'</a>';        
    }
}
