//global variables:
//  profileUsername
//  username

function subscribe() {
    UserSubscriptionController.subscribe(username, subscribeResponse);
}

function subscribeResponse(retval) {
    if (retval < 0) {
        handleSubscribeError(retval);
        return;
    }

    alert("You are successfully subscribed to " + profileUsername);
    setInnerHtml("subscribe",
        "<a href='#' onClick='javascript:unsubscribe();return false;'><img src='/img/btn-unsubscribe.gif' alt='unsubscribe' name='Image52' width='15' height='16' border='0' id='Image52' /> Unsubscribe</a>");
}

function handleSubscribeError(retval) {
    if (retval == -1) {
        alert("You need to log in first");
    }
    else if (retval == -2) {
        alert("You didn't specify a user");
    }
    else if (retval == -3) {
        alert("The specified useris not found");
    }
    else {
        alert("Unknown error");
    }
}

function unsubscribe() {
    UserSubscriptionController.unsubscribe(profileUsername, unsubscribeResponse);
}

function unsubscribeResponse(retval) {
    if (retval < 0) {
        handleUnsubscribeError(retval);
        return;
    }
    alert("You are successfully unsubscribed from " + profileUsername);
    setInnerHtml("subscribe",
        "<a href='#' onClick='javascript:subscribe();return false;'><img src='/img/btn-subscribeup.gif' alt='subscribe' name='Image52' width='15' height='16' border='0' id='Image52' /> Subscribe</a>");
}

function handleUnsubscribeError(retval) {
    handleSubscribeError(retval);
}

function addFriend() {
    window.open('/addAsFriend.do?u=' + username, 'AddAsFriend','width=670,height=550,resizable=yes,scrollbars=yes,status=0');
    //CheckLoginController.isLogin(checkLoginResponse);
}

/*function checkLoginResponse(res) {
    if (res == 0) {
        alert("You need to log in first");
    }
    else {
        window.open('/addAsFriend.do?u=' + username, 'AddAsFriend','width=670,height=550,resizable=yes,scrollbars=yes,status=0');
    }
}*/

function setInnerHtml(id, content) {
    document.getElementById(id).innerHTML = content;
}

function publishPlick(plickId) {
    PlickPublishController.publish(plickId, publishResponse);
}

function publishResponse(res) {
    if (res.status < 0)
        handlePublishError(res.status);
    var elem = document.getElementById("publish_" + res.plickId);
    elem.innerHTML="";
    elem = document.getElementById("publish_text_" + res.plickId);
    elem.innerHTML="Plick published";
}

function handlePublishError(status) {
    if (status == -1) {
        alert("You need to log in first");
    }
    else if (status == -2) {
        alert("The specified plick is not found");
    }
    else if (status == -3) {
        alert("Internal error: user not found");
    }
    else if (status == -4) {
        alert("You didn't specify a plick");
    }
    else {
        alert("Unknown error");
    }
}

function addComment() {
    var comment = DWRUtil.getValue("comment_text");
    if (comment.length > 3000) {
        alert("Comment is too long, please keep it within 3000 characters or split up to two comments.");
        return;
    }

    var name = DWRUtil.getValue("anon_name");
    var email = DWRUtil.getValue("anon_email");
    var url = DWRUtil.getValue("url");
    AddUserCommentController.addComment(comment, username, name, email, addCommentResponse);
}
    
function insertCell(row) {
    var cell=document.createElement("td");
    row.appendChild(cell);
    return cell;
}

function cleanTable(index, table) {
    for (var i = index; i < table.rows.length;) {
        //deleteRow has side effect, so do not do i++
        table.deleteRow(table.rows[i].rowIndex);
    }
}

function addCommentResponse(acr) {
    if (acr.status < 0) {
        handleAddCommentError(acr.status);
        return;
    }
    var table = document.getElementById("comments_table");
    cleanTable(1, table);

    for (i = 0; i < acr.comments.length; i++) {
        var thisComment = acr.comments[i];
        var tr = table.insertRow(i+1);
        var td1 = insertCell(tr);
        td1.width = "60";
        td1.height="62";
        td1.setAttribute("valign", "top");
        td1.setAttribute("align", "center");
        if (i % 2 == 0)
          td1.setAttribute("background", "/img/commentbg.gif");
        else
          td1.setAttribute("background", "/img/commentbg2.gif");
        td1.setAttribute("style", "background-repeat: no-repeat;");

        if (thisComment.anonymousPoster)
          td1.innerHTML = "<img class='pic' src='" + thisComment.posterPhotoUrl +
            "' alt='plicker' width='60' height='60' />";
        else
          td1.innerHTML = "<a href='/user/" + thisComment.posterName +
           "'><img class='pic' src='" + thisComment.posterPhotoUrl +
            "' alt='plicker' width='60' height='60' /></a>";

        var td2 = insertCell(tr);
        if (i == 0) td2.width="408";
        td2.setAttribute("align", "left");
        td2.setAttribute("valign", "top");
        td2.setAttribute("class", "tdnottop");
        var td2Inner = '<strong>';
        if (thisComment.anonymousPoster) {
          td2Inner = td2Inner + thisComment.posterName;
        }
        else {
          td2Inner = td2Inner + '<a href="/user/' + thisComment.posterName +
           '">' + thisComment.posterName + '</a>';
        }
        td2Inner = td2Inner + ' (' + thisComment.elapsedText + ')</strong><br/>' +
          thisComment.i18nBodyWithBR;
        td2.innerHTML = td2Inner;
    }
    DWRUtil.setValue("comment_text", "");
}


function handleAddCommentError(retval) {
    if (retval == -1) {
        alert("You need to log in first");
    }
    else if (retval == -3) {
        alert("The specified user is not found");
    }
    else if (retval == -4) {
        alert("Internal error: user not found");
    }
    else if (retval == -5) {
        alert("Comment is too long.  Please keep it within 3000 characters or split up to two comments.");
    }
    else if (retval == -105) {
        alert("Please do not add spam comments.");
    }
    else {
        alert("Unknown error");
    }
}

