﻿function submitComment()
{
    var commentId = $("#commentId").val();
    var parentCommentId = $("#parentCommentId").val();
    var baseCommentId = $("#baseCommentId").val();
    var commentName = $("#commentName").val();
    commentName = trim(commentName);

    var commentEmail = $("#commentEmail").val();
    commentEmail = trim(commentEmail);

    var commentWebsite = $("#commentWebsite").val();
    commentWebsite = trim(commentWebsite);

    var commentText = $("#commentText").val();
    commentText = trim(commentText);
    
    var valid = true;
    
    if (commentText.length < 1)
    {
        valid = false;
        $("#commentText").focus();
        $("#commentText")[0].style.border = "solid 1px red";
    } else {
        $("#commentText")[0].style.border = "";
    }
    
    $("#commentEmail")[0].style.border = "";
    if (commentEmail.length > 0)
    {
        var re = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
        if (re.test(commentEmail)==false)
        {
            valid = false;
            $("#commentEmail")[0].style.border = "solid 1px red";
            $("#commentEmail").focus();
        }
    }
    
    if (commentName.length < 1)
    {
        valid = false;
        $("#commentName").focus();
        $("#commentName")[0].style.border = "solid 1px red";        
    } else
    {
        $("#commentName")[0].style.border = "";
    }
    
    if (valid)
    {
        var parameters = "action=post&commentId="+encodeURIComponent(commentId)+"&parentCommentId="+encodeURIComponent(parentCommentId)+"&baseCommentId="+encodeURIComponent(baseCommentId)+"&commentName="+encodeURIComponent(commentName)+"&commentEmail="+encodeURIComponent(commentEmail)+"&commentWebsite="+encodeURIComponent(commentWebsite)+"&commentText="+encodeURIComponent(commentText);
        $.ajax({
            url: '../Services/comments.ashx',
            type: "POST",
            data: parameters,
            success: function (data) {
                alert("Your comment has been posted and is awaiting approval.");
            },
            error: function () {
                alert("Your comment could not be posted at this time. Please try again later. Sorry.");
            }
        });
        $("commentText").text("");
    }
}

function trim(stringToTrim)
{
    return stringToTrim.replace( /^\s+/g,'').replace(/\s+$/g,'') 
}

