var xmlHttpAddComment = getXmlHttp();

function AddComment(videoId)
{
    var texar = document.getElementById("tbComment");
    
    if(!IsNull(texar))
    {           
        if(!IsNullEmpty(texar.value))
        {
            var text = texar.value;
            
            if(xmlHttpAddComment)
            {
                try
                {
                    if(xmlHttpAddComment.readyState == 4 || xmlHttpAddComment.readyState == 0)
                    {
                        text = encode(text);
                        videoId = encode(videoId);
                        
                        xmlHttpAddComment.open("POST", "/App_Ajax/comment_add.php", true);
                        xmlHttpAddComment.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                        xmlHttpAddComment.onreadystatechange = handleAddComment;
                        xmlHttpAddComment.send("videoid=" + videoId + "&comment=" + text);
                    } 
                }
                catch(e)
                {
                }
            }
        }  
    } 
}

function handleAddComment()
{
    if(xmlHttpAddComment.readyState == 4)
    {
        if(xmlHttpAddComment.status == 200)
        {
            try
            {
                UpdateComment();
            }
            catch(e)
            {
            }
        }
    }
}

function UpdateComment()
{
    var response = xmlHttpAddComment.responseText;
    
    if(response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 || response.length == 0)
    {
        return;
    }
    
    var texar = document.getElementById("tbComment"); 
    
    if(!IsNull(texar))
    {
        texar.parentNode.innerHTML = response;    
    }  
}

/*get comments */
var xmlHttpGetComment = getXmlHttp(); 

function GetCommentsByPage(sender, videoId, page)
{
    if(xmlHttpGetComment)
    {
        try
        {
            if(xmlHttpGetComment.readyState == 4 || xmlHttpGetComment.readyState == 0)
            {
                
		  var commList = document.getElementById("CommentListing");
 
                if(!IsNull(commList))
                {
                    commList.innerHTML = "wait...loading...";    
                } 

                page = encode(page);  
                videoId = encode(videoId);    
                           
                xmlHttpGetComment.open("POST", "/App_Ajax/comment_get.php", true);
                xmlHttpGetComment.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                xmlHttpGetComment.onreadystatechange = handleGetComment;
                xmlHttpGetComment.send("page=" + page + "&videoid=" + videoId);
            } 
        }
        catch(e)
        {
        }
    }
}

function handleGetComment()
{
    if(xmlHttpGetComment.readyState == 4)
    {
        if(xmlHttpGetComment.status == 200)
        {
            try
            {
                var response = xmlHttpGetComment.responseText;

                if(response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 || response.length == 0)
                {
                    
                    return;
                }
                
                var commList = document.getElementById("CommentListing"); 
                
                if(!IsNull(commList))
                {
                    commList.innerHTML = response;    
                } 
                
                
            }
            catch(e)
            {
            }
        }
    }
}

