﻿//vars:
//inputID_parentCommentID
//inputID_commentText
//inputID_commenterName
//inputID_submitBtn
//commenterName_defaultText
//commentText_defaultText
//CannotPostMessage
function showCommentInterface(comID){
    var div = getCommentInterfaceDiv(comID);
    if(div.innerHTML == ""){
         div.innerHTML = "<input id=\"nameInput_"+comID+"\" onfocus=\"registerDefaultText(this);\" type=\"text\"  class=\"commentInput nameInput\"  style=\"color:#b698a2\" />" + 
        "<textarea onfocus=\"registerDefaultText(this);\" id=\"commentText_"+comID+"\" rows=\"4\" cols=\"30\" class=\"commentInput textAreaInput\"  style=\"color:#b698a2\">תוכן התגובה</textarea> " + 
        "<input type=\"button\" value=\"שלח\" class=\"commentInput buttonInput\" onclick=\"sendComment('"+comID+"')\"/>";
        document.getElementById("nameInput_"+comID).value = commenterName_defaultText;
        document.getElementById("commentText_"+comID).value = commentText_defaultText;
    }
    showElement(div)  ;
    hideElement("showAnchor_"+comID);
    showElement("closeAnchor_"+comID );
}
function hideCommentInterface(comID){
    var div = getCommentInterfaceDiv(comID);
    hideElement(div);
    hideElement(getCloseAnchor(comID));
    showElement(getShowAnchor(comID) );
}
function sendComment(comID){
    //alert("comID = "+comID + "\nname = " + getNameInput(comID).value + "\ntext=" + getTextInput(comID).value );
    document.getElementById(inputID_parentCommentID).value = comID;
    var name = getNameInput(comID).value;
    var text = getTextInput(comID).value;
    if(!contentIsValid(name,text)){
        return;
    }
    document.getElementById(inputID_commentText).value = text;
    document.getElementById(inputID_commenterName).value = name;
    document.getElementById(inputID_submitBtn).click();
}

function contentIsValid(name,text){
    if(name == "" || text == "" || name == commenterName_defaultText || text == commentText_defaultText){
        alert(CannotPostMessage);
        return false ;
    }
    return true;
}

function getCommentInterfaceDiv(comID){
    var div = document.getElementById("commentInterface_"+comID);
    return div;
}
function getNameInput(comID){
    var input = document.getElementById("nameInput_"+comID);
    return input;
}
function getTextInput(comID){
    var input = document.getElementById("commentText_"+comID);
    return input;
}
function getShowAnchor(comID){
    var result = document.getElementById("showAnchor_"+comID);
    return result;
}
function getCloseAnchor(comID){
    var result = document.getElementById("closeAnchor_"+comID);
    return result;
}
function hideCommentAnchors(commaSep){
    var commentIds = commaSep.split(",");
    for(var i=0; i<commentIds.length; i++){
        if(commentIds[i] == null || commentIds[i] == ""){
            continue;
        }
        hideElement(getShowAnchor(commentIds[i]));
    }
}

function registerDefaultText(target){
   var defText = target.value;
   var defaultTextColor = target.style.color;
   target.value = "";
   target.style.color = "";
   
   target.onfocus = function(){
        if(this.value == defText){
            this.value = "";
            this.style.color = "";
         }
   };
   target.onblur = function(){
        if(this.value == ""){
            this.value = defText;
            this.style.color = defaultTextColor;
        }
   };
   
}