function postComment(elem, elem1){
    var comment = $("#comment").val();
    if($.trim(comment) != ""){
        $("#postingComment").html("Posting...<img src='images/ajax-loader2.gif' />");
        $.ajax({
            url: 'postComment.php',
            type: 'POST',
            data: {
                videoId : elem,
                comment : comment,
                userName : elem1
            },
            success: function(data){
                $("#postingComment").html(data);
                getComments(elem);
            },
            cache: false
        });
    } else {
        alert("Comment is required");
        $("#comment").focus();
    }
}

function loginNPost(){

    var videoId = $("#videoId").val();
    var comment = $("#comment").val();
    var userName = $("#userName").val();
    var password = $("#password").val();
    var replyTo = $("#replyTo").val();

    if( $.trim(userName) == "" ){
        alert("User name is required");
        $("#userName").focus();
    } else if( $.trim(password) == "" ){
        alert("Password is required");
        $("#password").focus();
    } else if( $.trim(comment) == "" ){
        alert("Comment is required");
        $("#comment").focus();
    } else {
        $("#postingComment").html("Posting...<img src='images/ajax-loader2.gif' />");
        $.ajax({
            url: 'loginNPost.php',
            type: 'POST',
            data: {
                videoId : videoId,
                comment : comment,
                userName : userName,
                password : password,
                replyTo : replyTo
            },
            success: function(data){
                window.location.reload();
            },
            cache: false
        });
    }
}

function getComments(elem1){
    elem2 = $("#userId").val();
    $.ajax({
        url: 'getComments.php',
        type: 'GET',
        data: {
            videoId : elem1,
            userId : elem2
        },
        success: function(data){
            $("#commentList").html(data);
            $("#cmttbl").css("display","none");
            $("#cmttbl").slideDown(1000);
            
        },
        cache: false
    });
}

function updateVideo(elem1, elem2){
    var title = $("#title").val();
    var description = $("#description").val();
    
    if( $.trim(description) == "" ){
        alert("Description is required");
        $("#title").focus();
    } else {
        $("#update").html("Updating...<img src='images/ajax-loader.gif' />");
        $.ajax({
            url: 'updateVideo.php',
            type: 'POST',
            data: {
                videoId : elem1,
                userId : elem2,
                title : title,
                description : description
            },
            success: function(data){
                $("#update").html(data);
            },
            cache: false
        });
    }
}

function deleteVideo(elem1, elem2, elem3){
    status = confirm("Do you really want to delete this video?");
    
    if( status ){
        $("#delete"+elem1).html("<img src='images/ajax-loader3.gif' />");
        $.ajax({
            url: 'deleteVideo.php',
            type: 'POST',
            data: {
                videoId : elem1,
                userId : elem2
            },
            success: function(data){
                if ( elem3 == "refresh" ){
                    $("#vidRow"+elem1).fadeOut(1000);
                } else {
                    window.location = "?page=userhome";
                }
            },
            cache: false
        });
    }
}

function deleteComment(elem1, elem2){
    status = confirm("Do you really want to delete this comment?");

    if( status ){
        $("#delcom"+elem1).html("<img src='images/ajax-loader3.gif' />");
        elem3 = $("#userId").val();
        $.ajax({
            url: 'deleteComment.php',
            type: 'POST',
            data: {
                commentId : elem1,
                comUserId : elem2,
                vidUserId : elem3
            },
            success: function(data){
                $("#comRow"+elem1).fadeOut(1000);
            },
            cache: false
        });
    }
}



