﻿var currentListIndex = -1;
var currentTextBox = "";
var currentHyperLink = "";
function CommentListShort_AddComment(listIndex, tb) {

    // Collect Info
    text = document.getElementById(tb).value;
    text = escape(encodeURI(text));

    currentTextBox = tb;
    currentListIndex = listIndex;

    BusinessObjectType = CommentListsInfos[listIndex].ObjectToComment;
    BusinessObjectID = CommentListsInfos[listIndex].ObjectToCommentID;
    BusinessObjectOwnerID = CommentListsInfos[listIndex].ObjectToCommentOwnerID;

    if (text == "") {
        alert(ResNoText);
        return;
    }

    page = "/Controls/CommentListAsyncWorker.aspx?Method=AddComment&Bo=" + BusinessObjectType + "&BoID=" + BusinessObjectID + "&BoOwnerID=" + BusinessObjectOwnerID + "&Text=" + text + "&IsGeocoded=false";
    SendXMLHttpRequest(page, "CommentListShort_OnAddCommentSuccess", "CommentListShort_OnAddCommentFail")
}

function CommentListShort_OnAddCommentSuccess(obj) {
    // Show AddCommentBox + Hide AddCommentSavingBox
    //document.getElementById("commentListDivAddComment").style.display = "block";
    //document.getElementById("commentListDivAddCommentSaving").style.display = "none";

    // Fill CommentsArray
    if (obj.Status == "FAIL") {
        //alert(ResSaveErrorMsg);
        if (obj.ErrorMessage == "")
            alert(ResError);
        else
            alert(obj.ErrorMessage);
        return;
    }
    commentListEntry = new Object();
    commentListEntry.CommentID = obj.NewCommentID;
    commentListEntry.CreatedByUserLink = obj.CreatedByUserLink;
    commentListEntry.CreatedByUserAvatar = obj.CreatedByUserAvatar;
    commentListEntry.CreatedByUserDisplayName = obj.CreatedByUserDisplayName;
    commentListEntry.IsDeleteable = true;
    if (obj.IsGeocoded.toLowerCase() == "true")
        commentListEntry.IsGeocoded = true;
    else
        commentListEntry.IsGeocoded = false;
    if (commentListEntry.IsGeocoded) {
        commentListEntry.Latitude = obj.Latitude; // parseFloat() ?!?
        commentListEntry.Longitude = obj.Longitude;
    }
    else {
        commentListEntry.Latitude = 0;
        commentListEntry.Longitude = 0;
    }
    commentListEntry.Text = obj.Text;
    commentListEntry.PostedTime = obj.PostedTime;

    // Das ist hier überflüssig, wenn man nur hinzufügen kann!!!!
    //CommentLists[currentListIndex].push(commentListEntry);
    CommentListShort_AddCommentInHTML(currentListIndex, commentListEntry);
    // moved down -> after reset box
    //currentListIndex = -1;

    //document.getElementById(currentTextBox).value = "";
    // Reset the current textbox
    CommentListShort_ResetCurrentAddBox();

    currentListIndex = -1;
}
function CommentListShort_OnAddCommentFail(statustxt) {
    // Show AddCommentBox + Hide AddCommentSavingBox
    //document.getElementById("commentListDivAddComment").style.display = "block";
    //document.getElementById("commentListDivAddCommentSaving").style.display = "none";

    if (statustxt == 500) {
        alert(ResSaveErrorMsg);
    }
}

function CommentListShort_AddCommentInHTML(listIndex, c) {
    div = document.getElementById(CommentListsInfos[listIndex].DivName);

    str = "<div class=\"commentLS_itemDiv\">";
    str += "<a href=\"" + c.CreatedByUserLink + "\">";
    str += "<img src=\"" + c.CreatedByUserAvatar + "\" class=\"commentLS_creatorIcon\" />";
    str += "</a>";
    str += "<div class=\"commentLS_text\">";
    str += "<a href=\"" + c.CreatedByUserLink + "\" class=\"commentLS_creatorLink\">" + c.CreatedByUserDisplayName + "</a> ";
    str += c.Text;
    str += "</div>";
    str += "<img class=\"commentLS_itemArrowUp\" src=\"App_Themes/CWDefault/Grafix/Newsfeed_Arrow_Up.png\" alt=\"\" />";
    str += "<div class=\"commentLS_itemDate\">" + c.PostedTime + "</div>";
    str += "</div>";

    div.innerHTML += str;
}

function CommentListShortAdd_OnFocus(listIndex, tb, hl) {
    // Reset the current tb
    // If a user types in a text + do not press the post comment link
    // but elsewhere -> next click in another comment add box
    // will reset the old one!
    if (currentTextBox != tb) {
        CommentListShort_ResetCurrentAddBox();
    }

    hl_element = document.getElementById(hl);
    hl_element.style.display = "block";
    tb_element = document.getElementById(tb);
    if (tb_element.value == CommentListsInfos[listIndex].LeaveCommentText)
        tb_element.value = "";
    tb_element.rows = 4;
    tb_element.style.height = "30px";
    tb_element.style.color = "#000000";
}

function CommentListShortAdd_OnBlur(listIndex, tb, hl) {
    tb_element = document.getElementById(tb);
    if (tb_element.value == "") {
        //tb_element.value = ResPostCommentText;
        tb_element.value = CommentListsInfos[listIndex].LeaveCommentText;
        tb_element.rows = 2;
        tb_element.style.height = "15px";
        tb_element.style.color = "#999999";
        hl_element = document.getElementById(hl);
        hl_element.style.display = "none";
    }
    else {
        currentListIndex = listIndex;
        currentTextBox = tb;
        currentHyperLink = hl;
    }
}

function CommentListShort_ResetCurrentAddBox() {
    tb_element = document.getElementById(currentTextBox);
    if (tb_element != null && currentListIndex != -1) {
        //tb_element.value = ResPostCommentText;
        tb_element.value = CommentListsInfos[currentListIndex].LeaveCommentText;
        tb_element.rows = 2;
        tb_element.style.height = "15px";
        tb_element.style.color = "#999999";
        hl_element = document.getElementById(currentHyperLink);
        hl_element.style.display = "none";
    }
}

function CommentListShort_ShowGroupedComments(el) {
    document.getElementById(el).style.display = "block";
}
function CommentListShort_HideGroupedInfo(el) {
    document.getElementById(el).style.display = "none";
}
