$(document).ready(function() {
    $('p.error').html('').hide();
    expandBox();
	cancelBox();
    countChars();
});
function expandBox() {
  $('p.flagIt').click(function() {
  $('p.error').html('').hide();
  
  var answer = $(this).next('div.flagCommentDetails');
         if (answer.is (':visible')) {
             answer.slideUp('slow');
         } else {

    $('div.flagCommentDetails:visible').slideUp('slow');
             answer.slideDown ('slow');
   
         }
  
        return false;
    });
}
function cancelBox() {
	
	$('.cancelButton').click(function() {
		
		$('div.flagCommentDetails:visible').slideUp('slow');
		return false;
	});
}
function countChars() {
 
    $('form').submit(function() {
        var outStr = "";
        if (($(this).find('.flagName').val().length == 0)) {
            outStr += "Name is required.";
        }
        if (($(this).find('.flagReason').val().length == 0)) {
            if (outStr.length > 0) outStr += "<br/>";
            outStr += "Reason is required.";
        } else if (($(this).find('.flagReason').val().length > 1024)) {
            if (outStr.length > 0) outStr += "<br/>";
            outStr += "Reason can not be greater than 1024 characters.";
        }
       
        if (outStr.length > 0) {
             $(this).prev("p.error").html(outStr).fadeIn('slow');
            return false;
        } else {
             $(this).prev("p.error").hide();
        }
        return true;
    });
}