﻿function submitComment()
{
    var commentId = $("commentId").value;
    var parentCommentId = $("parentCommentId").value;
    var baseCommentId = $("baseCommentId").value;
    var commentName = $("commentName").value;
    commentName = trim(commentName);
    
    var commentEmail = $("commentEmail").value;
    commentEmail = trim(commentEmail);
    
    var commentWebsite = $("commentWebsite").value;
    commentWebsite = trim(commentWebsite);
    
    var commentText = $("commentText").value;
    commentText = trim(commentText);
    
    var valid = true;
    
    if (commentText.length < 1)
    {
        valid = false;
        $("commentText").focus();
        $("commentText").style.border = "solid 1px red";
    } else {
        $("commentText").style.border = "";
    }
    
    $("commentEmail").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").style.border = "solid 1px red";
            $("commentEmail").focus();
        }
    }
    
    if (commentName.length < 1)
    {
        valid = false;
        $("commentName").focus();
        $("commentName").style.border = "solid 1px red";        
    } else
    {
        $("commentName").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);
        new Ajax.Request('http://www.yafla.com/blogs/Services/comments.ashx', {
            method: 'post',
            asynchronous: false,
            postBody: parameters
            });
        alert("Your comment has been posted and is awaiting approval.");
        $("commentText").value = "";
    }
}

function trim(stringToTrim)
{
    return stringToTrim.replace( /^\s+/g,'').replace(/\s+$/g,'') 
}
