﻿String.prototype.trim = function ()
{
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
String.prototype.convertQuotes = function()
{
    return this.replace(/"/g,"'");
}

jQuery().ready( function()
{
    clearComments();
    $("#shop .show-comments").click(function(e)
    {
        //e.preventDefault();
        clearComments();
        var id = $(this).attr("rel");
        var container = $(this).parent().next(".comments");
        getComments(id, container);
    });
    $("#shop .btn-comment").click(function(e)
    {
        e.preventDefault();
        var id = $(this).attr("value");        
        var container = $(this).parent();
        var textboxName = $(container).children(".txt-name");
        var textbox = $(container).children(".txt-comment");
        var name = textboxName.val().trim().convertQuotes();
        var text = textbox.val().trim().convertQuotes();
        $(container).children("p").remove();
        if(name.length < 1)
        {
            //$(container).append("<p>Yeah, nice one!<br/>Why don't you try entering some text.</p>");
            //alert("Please enter your name");
            //return;
            name = "Anon";
        }
        if(text.length < 1)
        {
            //$(container).append("<p>Yeah, nice one!<br/>Why don't you try entering some text.</p>");
            //alert("Please enter some comments");
            return;
        }
        $.ajax(
        {
            type: "POST",
            url: "/ws/Service.asmx/CreateComment",
            data: '{"id":"' + id + '","name":"' + name + '","text":"' + text + '"}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            error: function(XMLHttpRequest, textStatus, errorThrown)
            {
                $(container).append("<p>Sorry, due to an error your comment could not be submitted. Please try again.</p>");
            },
            success: function(msg)
            {
                // Clear textarea
                $(textboxName).val("");
                $(textbox).val("");
                // Load comments
                if((msg) == "True")
                {
                    $(container).append("<p>Thanks for your comment, it has been submitted for approval.</p>");
                }
            }
        });
    });
});

function getComments(id, container)
{
    $(container).show();
    $(container).addClass('loading');
    var list = $(container).children(".list-comments");
    $(list).html("<div>Loading comments...</div>");
    $.ajax(
    {
        type: "POST",
        url: "/ws/Service.asmx/GetComments",
        data: '{"id":"' + id + '"}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        error: function(XMLHttpRequest, textStatus, errorThrown)
        {
            $(container).addClass('error');
            //$(list).html(textStatus);
            $(list).html("<div>Unable to load comments</div>");
        },
        success: function(msg)
        {
            // Hide the fake progress indicator graphic.
            $(container).removeClass('loading');
     
            // Insert the returned HTML into the <div>.
            $(list).html(msg);
        }
    });
}

function clearComments()
{
    $("#shop .list-comments").html("");
    $("#shop .comments").hide();
    $("#shop .add-comment p").remove();
}

