function displayAddDialog( sName ) {
	document.getElementById( sName ).style.display = 'block';
}

var aAddProductInstances = {};
function ADDPL_AddProduct( sInstanceId, bExtended ) {
	var sInstanceId	= sInstanceId;
	var sBody	  = '';
	var sSignature	  = 'ADDPL';	
	var sDiv	  = sInstanceId + '_div';
	var bExtended 	  = bExtended;
	var bInitialized  = 0;
	var oProgressBar  = 0;
	
	this.productListSelected = function() {
		this.init();
		if( document.getElementById( sInstanceId + '_users_list' ).value == 'add_new_list' ) {
			$( '#' + sInstanceId + '_hidden_fields' ).show();
			if ( bExtended ) {
				$( '#' + sInstanceId + '_popup' ).find( '.progressbar' ).show();
				this.iMaxLength = parseInt( this.gs('description_max_length' ) );
				$( '#' + sInstanceId + '_popup' ).find( '.description_min_length' ).show();
				$( '#' + sInstanceId + '_popup' ).find( '.comment_title' ).empty().append( this.gs( 'enter_description_of_your_new_list' ) );
			}
		} else {
			$('#' + sInstanceId + '_hidden_fields' ).hide();
			this.iMaxLength = parseInt( this.gs( 'comment_max_length' ) );
			if ( bExtended ) {
				$( '#' + sInstanceId + '_popup' ).find( '.progressbar' ).hide();
				$( '#' + sInstanceId + '_popup' ).find( '.description_min_length' ).hide();
				$( '#' + sInstanceId + '_popup' ).find( '.comment_title' ).empty().append( this.gs( 'add_your_comment_here' ) );
			}
		}
		$( '#' + sInstanceId + '_popup' ).find( '.max_length' ).empty().append( this.iMaxLength );
		this.descriptionUpdated();
	}

	this.limitFieldLength = function( oField, iLimit ) {
		this.init();
		if( oField.value.length > iLimit ) {
			oField.value = oField.value.substr( 0, iLimit );
		}
	}

	this.validateForm = function( aValues ) {
		this.init();
		sSelect = document.getElementById( sInstanceId + '_users_list' ).value;
		sType	= document.getElementById( sInstanceId + '_list_type' ).value;
		sName	= document.getElementById( sInstanceId + '_name' ).value;

		
		if( sSelect == 'add_new_list' ) {
			if( sType == '' ) {
				alert( this.gs( 'please_select_a_list_type' ) );
				return false;
			}

			if( sName == '' ) {
				alert( this.gs( 'please_enter_a_name_for_this_product_list' ) );
				return false;
			}
			
			if ( bExtended ) {
				var iMinLength = parseInt( this.gs( 'description_min_length' ) );
				if ( isNaN( iMinLength ) || iMinLength < 0 ) {
					iMinLength = 0;
				}
				if ( iMinLength && oCrux.wordCount( $( '#' + sInstanceId + '_comment' ).val() ) < this.gs( 'description_min_length' ) ) {
					alert( this.gs( 'description_too_short' ) );
					return false;
				}
			}
		} else if( sSelect == '' ) {
			alert( this.gs( 'please_select_a_list_to_add_this_product_to' ) );
			return false;
		}

		xajax_AddProduct_addProduct( sInstanceId, aValues );
	}

	this.gs = function( sKey ) {
		return oCruxStrings.getString( sSignature, sKey );
	}
	
	this.addProductDialog = function ( iLoginResult ) {
		if ( iLoginResult >=0 && iLoginResult < 2 ) {
			xajax_AddProduct_displayAddProductDialog( this.sInstanceId, this.iProductId );
		}
	}
	
	this.descriptionUpdated = function() {
		if ( bExtended ) {
			this.oProgressBar.value(
				oCrux.wordCount( $( '#' + sInstanceId + '_comment' ).val() ) * 100 / parseInt( this.gs( 'description_min_length' ) )
			);
		}
		this.limitFieldLength( $( '#' + sInstanceId + '_comment' )[0], this.iMaxLength );
	}
	
	this.init = function() {
		if ( ! bInitialized ) {
			if ( bExtended ) {
				this.oProgressBar = new JProgressBar( $( "#" + sInstanceId + "_progress_bar" ) );
			}
			$( '#' + sInstanceId + '_comment' )
			.bind(
				'keyup',
				{ sInstance: sInstanceId },
				function( e ) {
					aAddProductInstances[ e.data.sInstance ].descriptionUpdated();
				}
			).bind(
				'keydown',
				{ sInstance: sInstanceId },
				function( e ) {
					aAddProductInstances[ e.data.sInstance ].descriptionUpdated();
				}
			).bind(
				'change',
				{ sInstance: sInstanceId },
				function( e ) {
					aAddProductInstances[ e.data.sInstance ].descriptionUpdated();
				}
			);
			bInitialized = 1;
		}
	}
	
}
