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);
}
/* Show/hide selected reaction form */
function showForm(divId, contentItemId) {
	if ( $( divId + "_" + contentItemId ).visible() ) {
		$( divId + "_" + contentItemId ).toggle();
	} 
        else if ("reageerDiv" == divId && !loggedIn()) {
                $("loginDiv_" + contentItemId).show();
        }
        else {
                $("loginDiv_" + contentItemId).hide();
		$("reageerDiv_" + contentItemId).hide();
		$("stuurdoorDiv_" + contentItemId).hide();
		$("klachtDiv_" + contentItemId).hide();
		
		clearCommentForm(contentItemId);
		clearForwardForm(contentItemId);
		clearKlachtenForm(contentItemId);		
		$( divId + "_" + contentItemId ).show();
	}
	return false;
}

/* Rate content item */
function rateContentItem(contentItemId) {
	updateRateBox = function( numberOfRates ){
		var rateBox = $( "rateBox_" + contentItemId );
		rateBox.update( numberOfRates );
        // Omniture:
        if (typeof(s) != 'undefined') {
            s.events="event4";
            void(s.t());
        }
	}
	ajaxService.rateContentItem(contentItemId, updateRateBox);
}

/* Send comment */
function sendComment(contentItemId) {
	comment = $( "comment_" + contentItemId );
	comment_info = $( "comment_info_" + contentItemId );
	commentText = trim(comment.value);
	if ( commentText == "" ) {
		comment_info.update( "U kunt geen leeg veld versturen, voer eerst een reactie in." );
		comment_info.className = "error";
	} else {
		handleCommentResponse = function(success) {
			if (success) {
				comment_info.update( "Uw reactie is geplaatst, ververs uw scherm als u de reactie niet direct ziet." );
				comment_info.className = "success";
				comment.value = "";
                // Omniture:
                if (typeof(s) != 'undefined') {
                    s.events="event3";
                    s.eVar20='article';
                    void(s.t());
                }
			} else {
				comment_info.update( "Error sending comment. Please, try again later." );
				comment_info.className = "error";
			}
		}
		ajaxService.addComment(contentItemId, commentText, handleCommentResponse);
	}
}

/* Send complaint */
function sendComplaint(contentItemId) {
	complaint = $( "complaint_" + contentItemId );
	complaint_info = $( "complaint_info_" + contentItemId );
	if ( complaint.value == 0 ) {
		complaint_info.update( "Kies een klachttype." );
		complaint_info.className = "error";
	} else {
		handleComplaintResponse = function(success) {
			if (success) {
				complaint_info.update( "Uw klacht is verzonden." );
				complaint_info.className = "success";
				complaint.selectedIndex = 0;
                // Omniture:
                if (typeof(s) != 'undefined') {
                    s.pageName="klacht-over-artikel";
                    void(s.t());
                }
			} else {
				complaint_info.update( "Error sending complaint. Please, try again later." );
				complaint_info.className = "error";
			}
		}
		ajaxService.addComplaint(contentItemId, complaint.value, handleComplaintResponse);
	}
}

/* Forward to email */
function forwardToEmail(contentItemId) {
	email = $( "forward_" + contentItemId + "_email" );
	senderName = $( "forward_" + contentItemId + "_senderName" );
	remark = $( "forward_" + contentItemId + "_remark" );
	
	emailText = trim(email.value);
	senderText = trim(senderName.value);
	remarkText = trim(remark.value);
	
	forward_info = $( "forward_info_" + contentItemId );
	
	if (emailText == "") {
		forward_info.update( "Emailadres is verplicht." );
		forward_info.className = "error";
	} else if (senderText == "") {
		forward_info.update( "Naam opgeven verplicht." );
		forward_info.className = "error";
	} else {
		handleForwardResponse = function(success) {
			if (success) {
				forward_info.update( "Het emailbericht is verzonden." );
				forward_info.className = "success";
				email.value = "";
				senderName.value = "";
				remark.value = "";
                // Omniture:
                if (typeof(s) != 'undefined') {
                    s.events="event6";
                    s.eVar18=''+article_id;
                    s.eVar20='article';
                    void(s.t());
                }
			} else {
				forward_info.update( "Error sending email. Please, try again later." );
				forward_info.className = "error";
			}
		}
		ajaxService.forwardToEmail(contentItemId, senderText, emailText, remarkText, handleForwardResponse);
	}
}

/* Clear comment form fields */
function clearCommentForm(contentItemId) {
	comment = $( "comment_" + contentItemId );
	comment.value = "";
	comment_info = $( "comment_info_" + contentItemId );
	comment_info.update( "" );
	comment_info.className = "";
}

/* Clear forward form fields */
function clearForwardForm(contentItemId) {
	email = $( "forward_" + contentItemId + "_email" );
	senderName = $( "forward_" + contentItemId + "_senderName" );
	remark = $( "forward_" + contentItemId + "_remark" );
	email.value = "";
	senderName.value = "";
	remark.value = "";
	forward_info = $( "forward_info_" + contentItemId );
	forward_info.update( "" );
	forward_info.className = "";
}

/* Clear complaint form fields */
function clearKlachtenForm(contentItemId) {
	complaint = $( "complaint_" + contentItemId );
	complaint.selectedIndex = 0;
	complaint_info = $( "complaint_info_" + contentItemId );
	complaint_info.update( "" );
	complaint_info.className = "";
}

/* Trim string */
function trim(str) {
	if (str == null) {
		return "";
	} else {
		return str.replace(/^\s+|\s+$/g, '') ;
	}
}
