function TrimString(value){
    return value.replace(/^\s+|\s+$/g, '');
}
function IsEmail(value)
{
    var val = TrimString(value);
    var re = new RegExp(/\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}/g);
    return val.match(re);
}

jQuery(document).ready(
    function() {
        jQuery('fieldset.conmentFields ol input, fieldset.conmentFields ol textarea').not('#comment-submit').click(function() {
            jQuery(this).val('');
	    jQuery(this).unbind('click');
        });
    
        jQuery('#comment-submit').click(
            function() {
                try {
                    var values = {
                        "comment": "1",
                        "SubmitId": document.getElementById('submitId').value,
                        "Date": document.getElementById('commentsDate').value,
                        "Subject": document.getElementById('subject').value,
                        "Name": document.getElementById('name').value,
                        "Email": document.getElementById('email').value,
                        "Comments": document.getElementById('comment').value
                    };
                    var isValid = true;
                    jQuery('#errors').html('');
                    if (TrimString(values.Name) == '' || TrimString(values.Name) == 'Navn') {
                        isValid = false;
                        jQuery('#errors').append('<p>' + jQuery('#lblName').val() + '</p>');
                    }
                    values.Email = TrimString(values.Email);
                    if (!IsEmail(values.Email)) {
                        isValid = false;
                        jQuery('#errors').append('<p>' + jQuery('#lblEmail').val() + '</p>');
                    }

                    if (TrimString(values.Subject) == '' || TrimString(values.Subject) == 'Emne') {
                        isValid = false;
                        jQuery('#errors').append('<p>' + jQuery('#lblSubject').val() + '</p>');
                    }
                    if (TrimString(values.Comments) == '' || TrimString(values.Comments) == 'Kommentar') {
                        isValid = false;
                        jQuery('#errors').append('<p>' + jQuery('#lblComment').val() + '</p>');
                    }
                    if (isValid) {
                        jQuery('span.loader:first').show();
                        jQuery('input#comment-submit').hide();
                        jQuery.getJSON('/layouts/comments.ashx?d=' + (new Date()).getTime(), values,
                            function(data) {
                                jQuery('span.loader:first').hide();
                                jQuery('input#comment-submit').show();
                                jQuery('fieldset.conmentFields ol input, fieldset.conmentFields ol textarea').not('#comment-submit').val('')
                                
                                var list = eval(data);
                                jQuery('#comments').html('');
                                for (var i = 0; i < list.length; i++)
                                    jQuery('#comments').append("<h2 class=\"commentTitlePost\">" + list[i].Title + "</h2><p class=\"commentName\">" + list[i].Name + "</p><p class=\"comment\">" + list[i].Comment + "</p>");
                                if (list.length > 0)
                                    document.getElementById('commentsDate').value = list[list.length - 1].Date;
                            }
                        );
                    }
                }
                finally {
                    return false;
                }
            }
        )
    }
);

