// CheckCommentForm.js


// Checks the validity of given comment form
function checkCommentForm(theForm)
{
    try
    {
        // Variable that contains error information
        var errors = "";

        // The maximum length of a field input
        var NAME_MAX_LENGTH = 40;
        var LOCATION_MAX_LENGTH = 40;
        var EMAIL_MAX_LENGTH = 128;
        var HOMEPAGE_MAX_LENGTH = 128;


        // Checking for errors and reporting about them if found
        
        if (theForm.name.value.length < 1)
        {
            errors = errors + "Given name is too short. \r\n";
        }
        else if (theForm.name.value.length > NAME_MAX_LENGTH)
        {
            errors = errors + "Given name is too long. \r\n";
        }

        if (theForm.location.value.length > LOCATION_MAX_LENGTH)
        {
            errors = errors + "Given location is too long. \r\n";
        }

        if (theForm.email.value.length > EMAIL_MAX_LENGTH)
        {
            errors = errors + "Given email address is too long. \r\n";
        }

        if (theForm.homepage.value.length > HOMEPAGE_MAX_LENGTH)
        {
            errors = errors + "Given homepage address is too long. \r\n";
        }

        if (theForm.botcheck.value.toLowerCase() != "white")
        {
            errors = errors + "Spam bot prevention field is incorrect. \r\n";
        }
        
        
        if (theForm.comment.value.length > 1100)
        {
            errors = errors + "The comment is too long. \r\n";
        }
        else if (theForm.comment.value.length < 5)
        {
            errors = errors + "The comment is too short. \r\n";
        }

        if (!theForm.terms.checked)
        {
            errors = errors + "You must accept the terms of use to submit comments. \r\n";
        }


        // Checking if errors have been reported
        if (errors != "")
        {
            // Showing error dialog
            alert(errors);

            return false;
        }
        else
        {
            return true;
        }
    }
    catch (err)
    {
        return true;
    }
}
