var showCommentsBox = function(desiredHeight){
	var animComments = new YAHOO.util.Anim('commentsBox', {height:{to: desiredHeight}}, 1, YAHOO.util.Easing.bounceOut);
	animComments.animate();
};

var toggleCommentsBox = function(){
	if(parseInt(YAHOO.util.Dom.getStyle('commentsBox', 'height'), 10) == 0){
		showCommentsBox(240);
	}
	else{
		showCommentsBox(0);
	}
};

var successComment = function(o){
	var jsonData = YAHOO.lang.JSON.parse(o.responseText);
	
	if(jsonData.error == undefined){
		YAHOO.util.Dom.get('commentsBox').innerHTML = '<p class="message">merci pour votre avis</p>';
	
		// hide the message box
		setTimeout("showCommentsBox(0);", 5000);
	}
	else{
		YAHOO.util.Dom.get('commentsBox').innerHTML = '<p class="message">an error has occurred.</p>';
	}
};

var failureComment = function(o){
	YAHOO.util.Dom.get('commentsBox').innerHTML = '<p class="message">an error has occurred</p>';
};

var submitComment = function(){
	var name = YAHOO.util.Dom.get('name').value;
	var comment = YAHOO.util.Dom.get('comments').value;
	var id = YAHOO.util.Dom.get('film_id').value;
	if(comment.length != 0){
		// show loading
		YAHOO.util.Dom.get('commentsBox').innerHTML = '<p class="loading">&nbsp;</p>';
		showCommentsBox(240);

		// send request
		var requestComment = YAHOO.util.Connect.asyncRequest('POST', '/cinema/sendComment.php', {success:successComment, failure:failureComment}, 'id=' + id + '&name=' + encodeURI(name) +'&comment=' + encodeURI(comment));
	}
	else{
		alert('Please enter a comment');
	}
};

var counterUpdate = function(opt_countedTextBox, opt_countBody, opt_maxSize) {
        var countedTextBox = opt_countedTextBox ? opt_countedTextBox : "counttxt";
        var countBody = opt_countBody ? opt_countBody : "countBody";
        var maxSize = opt_maxSize ? opt_maxSize : 1024;

        var field = document.getElementById(countedTextBox);

        if (field && field.value.length >= maxSize) {
                field.value = field.value.substring(0, maxSize);
        }
        var txtField = document.getElementById(countBody);
                if (txtField) { 
                txtField.innerHTML = field.value.length;
        }
}
