var aOpinionCommentAddIns = {};

function OCADD_OpinionCommentAdd( sInstanceId, bLoggedIn ) {

	var sInstanceId = sInstanceId;
	this.bLoggedIn = bLoggedIn;
	
	this.checkCommentText = function( oObj, iMinChars, iMaxChars ) {
		
		// remove white-space from beginning and end of the comment
		var sComment = oObj.value.replace( /^\s+|\s+$/g, '' );
		
		if ( sComment.length >= iMaxChars ) {
			$( '#' + sInstanceId + '_errorMsgNewComment' ).css( 'visibility', 'visible' );
			oObj.value = oObj.value.substring( 0, iMaxChars );
			this.setSaveButtonState( true );
		} else if ( sComment.length < iMinChars ) { 
			this.setSaveButtonState( false );
			$( '#' + sInstanceId + '_errorMsgNewComment' ).css( 'visibility', 'hidden' );
		} else {
			this.setSaveButtonState( true );
			$( '#' + sInstanceId + '_errorMsgNewComment' ).css( 'visibility', 'hidden' );
		}
		$(oObj).removeAttr( 'onfocus' );
	}
	this.setSaveButtonState = function( bEnable ) {

		$( '#' + sInstanceId + '_postCommentButton' ).unbind();

		if ( bEnable ) {
			$( '#' + sInstanceId + '_postCommentButton' ).bind( 'click', function (e) { aOpinionCommentAddIns[sInstanceId].saveComment();return false; } );
			$( '#' + sInstanceId + '_postCommentButton' ).removeClass( 'CWButtonDisabled' );
		} else {
			$( '#' + sInstanceId + '_postCommentButton' ).addClass( 'CWButtonDisabled' );
		}
	}

	this.saveComment = function() {
		this.setSaveButtonState( false );
		xajax_CommentAdd_saveComment( sInstanceId,  $('#' + sInstanceId + '_comment').val() );
	}
}
