/**
	 * save invite
	 *
	 * @param int       $id
	 * @param UserTable $user
	 */
	private function saveInviteEdit( $id, $user )
	{
		global $_CB_framework, $_CB_database, $_PLUGINS;

		$row							=	new InviteTable();

		$row->load( (int) $id );

		$isModerator					=	CBGroupJive::isModerator( $user->get( 'id' ) );
		$groupId						=	$this->input( 'group', null, GetterInterface::INT );

		if ( $groupId === null ) {
			$group						=	$row->group();
		} else {
			$group						=	CBGroupJive::getGroup( $groupId );
		}

		$returnUrl						=	$_CB_framework->pluginClassUrl( $this->element, false, array( 'action' => 'groups', 'func' => 'show', 'id' => (int) $group->get( 'id' ) ) );

		if ( ! CBGroupJive::canAccessGroup( $group, $user ) ) {
			cbRedirect( $returnUrl, CBTxt::T( 'Group does not exist.' ), 'error' );
		} elseif ( $row->get( 'id' ) && ( $user->get( 'id' ) != $row->get( 'user_id' ) ) ) {
			cbRedirect( $returnUrl, CBTxt::T( 'You do not have sufficient permissions to edit this invite.' ), 'error' );
		} elseif ( ! $isModerator ) {
			if ( ( $group->get( 'published' ) == -1 ) || ( ( ! $this->params->get( 'groups_invites_display', 1 ) ) && ( $group->get( 'type' ) != 3 ) ) ) {
				cbRedirect( $returnUrl, CBTxt::T( 'You do not have access to invites in this group.' ), 'error' );
			} elseif ( ( ! $row->get( 'id' ) ) && ( ! CBGroupJive::canCreateGroupContent( $user, $group, 'invites' ) ) ) {
				cbRedirect( $returnUrl, CBTxt::T( 'You do not have sufficient permissions to create an invite in this group.' ), 'error' );
			}
		}

		$skipCaptcha					=	false;

		$row->set( 'message', $this->input( 'post/message', $row->get( 'message' ), GetterInterface::STRING ) );

		if ( ! $row->get( 'id' ) ) {
			$row->set( 'user_id', (int) $row->get( 'user_id', $user->get( 'id' ) ) );
			$row->set( 'group', (int) $group->get( 'id' ) );

			$to							=	$this->input( 'post/to', null, GetterInterface::STRING );
			$selected					=	(int) $this->input( 'post/selected', 0, GetterInterface::INT );

			if ( $selected ) {
				$token					=	$this->input( 'post/token', null, GetterInterface::STRING );

				if ( $token ) {
					if ( $token == md5( $row->get( 'user_id' ) . $to . $row->get( 'group' ) . $row->get( 'message' ) . $_CB_framework->getCfg( 'secret' ) ) ) {
						$skipCaptcha	=	true;

						$row->set( 'user', (int) $selected );
					}
				} elseif ( $this->params->get( 'groups_invites_list', 0 ) ) {
					$connections		=	array();
					$cbConnection		=	new cbConnection( (int) $user->get( 'id' ) );

					foreach( $cbConnection->getConnectedToMe( (int) $user->get( 'id' ) ) as $connection ) {
						$connections[]	=	(int) $connection->id;
					}

					if ( in_array( $selected, $connections ) ) {
						$row->set( 'user', (int) $selected );
					}
				}
			} else {
				$inviteByLimit			=	explode( '|*|', $this->params->get( 'groups_invites_by', '1|*|2|*|3|*|4' ) );

				if ( ! $inviteByLimit ) {
					$inviteByLimit		=	array( 1, 2, 3, 4 );
				}

				$recipient				=	new UserTable();

				if ( in_array( 1, $inviteByLimit ) && $recipient->load( (int) $to ) ) {
					$row->set( 'user', (int) $recipient->get( 'id' ) );
				} elseif ( in_array( 4, $inviteByLimit ) && cbIsValidEmail( $to ) ) {
					if ( $recipient->load( array( 'email' => $to ) ) ) {
						$row->set( 'user', (int) $recipient->get( 'id' ) );
					} else {
						$row->set( 'email', $to );
					}
				} elseif ( in_array( 2, $inviteByLimit ) && $recipient->load( array( 'username' => $to ) ) ) {
					$row->set( 'user', (int) $recipient->get( 'id' ) );
				} elseif ( in_array( 3, $inviteByLimit ) ) {
					$query				=	'SELECT cb.' . $_CB_database->NameQuote( 'id' )
										.	"\n FROM " . $_CB_database->NameQuote( '#__comprofiler' ) . " AS cb"
										.	"\n LEFT JOIN " . $_CB_database->NameQuote( '#__users' ) . " AS j"
										.	' ON j.' . $_CB_database->NameQuote( 'id' ) . ' = cb.' . $_CB_database->NameQuote( 'id' )
										.	"\n LEFT JOIN " . $_CB_database->NameQuote( '#__groupjive_users' ) . " AS u"
										.	' ON u.' . $_CB_database->NameQuote( 'user_id' ) . ' = cb.' . $_CB_database->NameQuote( 'id' )
										.	' AND u.' . $_CB_database->NameQuote( 'group' ) . ' = ' . (int) $group->get( 'id' )
										.	"\n LEFT JOIN " . $_CB_database->NameQuote( '#__groupjive_invites' ) . " AS i"
										.	' ON i.' . $_CB_database->NameQuote( 'group' ) . ' = ' . (int) $group->get( 'id' )
										.	' AND i.' . $_CB_database->NameQuote( 'user' ) . ' = cb.' . $_CB_database->NameQuote( 'id' )
										.	"\n WHERE j." . $_CB_database->NameQuote( 'name' ) . " LIKE " . $_CB_database->Quote( '%' . $_CB_database->getEscaped( $to, true ) . '%', false )
										.	"\n AND cb." . $_CB_database->NameQuote( 'approved' ) . " = 1"
										.	"\n AND cb." . $_CB_database->NameQuote( 'confirmed' ) . " = 1"
										.	"\n AND j." . $_CB_database->NameQuote( 'block' ) . " = 0"
										.	"\n AND u." . $_CB_database->NameQuote( 'id' ) . " IS NULL"
										.	"\n AND i." . $_CB_database->NameQuote( 'id' ) . " IS NULL"
										.	"\n ORDER BY j." . $_CB_database->NameQuote( 'registerDate' ) . " DESC";
					$_CB_database->setQuery( $query, 0, 15 );
					$users				=	$_CB_database->loadResultArray();

					if ( $users ) {
						if ( count( $users ) > 1 ) {
							CBGroupJive::getTemplate( 'invite_list' );

							CBuser::advanceNoticeOfUsersNeeded( $users );

							HTML_groupjiveInviteList::showInviteList( $to, $users, $row, $group, $user, $this );
							return;
						} else {
							$row->set( 'user', (int) $users[0] );
						}
					}
				}
			}
		}

		if ( ( ! $isModerator ) && $this->params->get( 'groups_create_captcha', 0 ) && ( ! $skipCaptcha ) ) {
			$_PLUGINS->loadPluginGroup( 'user' );

			$_PLUGINS->trigger( 'onCheckCaptchaHtmlElements', array() );

			if ( $_PLUGINS->is_errors() ) {
				$row->setError( $_PLUGINS->getErrorMSG() );
			}
		}

		$new							=	( $row->get( 'id' ) ? false : true );

		if ( $row->getError() || ( ! $row->check() ) ) {
			$_CB_framework->enqueueMessage( CBTxt::T( 'GROUP_INVITE_FAILED_TO_SAVE', 'Invite failed to save! Error: [error]', array( '[error]' => $row->getError() ) ), 'error' );

			$this->showInviteEdit( $id, $user );
			return;
		}

		if ( $row->getError() || ( ! $row->store() ) ) {
			$_CB_framework->enqueueMessage( CBTxt::T( 'GROUP_INVITE_FAILED_TO_SAVE', 'Invite failed to save! Error: [error]', array( '[error]' => $row->getError() ) ), 'error' );

			$this->showInviteEdit( $id, $user );
			return;
		}

		if ( $new ) {
			cbRedirect( $returnUrl, CBTxt::T( 'Invite created successfully!' ) );
		} else {
			cbRedirect( $returnUrl, CBTxt::T( 'Invite saved successfully!' ) );
		}
	}