/**
	 * Save mail on submit
	 *
	 * @param array $field Field values
	 * @param int $form Form uid
	 * @return Tx_Powermail_Domain_Model_Mails Mail object
	 */
	protected function saveMail($field, $form) {
		// tx_powermail_domain_model_mails
		$marketingInfos = Tx_Powermail_Utility_Div::getMarketingInfos();
		$newMail = t3lib_div::makeInstance('Tx_Powermail_Domain_Model_Mails');
		$newMail->setPid(Tx_Powermail_Utility_Div::getStoragePage($this->settings['main']['pid']));
		$newMail->setForm($form);
		$newMail->setSenderMail($this->div->getSenderMailFromArguments($field));
		$newMail->setSenderName($this->div->getSenderNameFromArguments($field));
		$newMail->setSubject($this->settings['receiver']['subject']);
		$newMail->setBody(t3lib_utility_Debug::viewArray($this->div->getVariablesWithLabels($field)));
		$newMail->setReceiverMail($this->settings['receiver']['email']);
		if (intval($GLOBALS['TSFE']->fe_user->user['uid']) > 0) {
			$newMail->setFeuser($GLOBALS['TSFE']->fe_user->user['uid']);
		}
		$newMail->setSpamFactor($GLOBALS['TSFE']->fe_user->getKey('ses', 'powermail_spamfactor'));
		$newMail->setTime((time() - Tx_Powermail_Utility_Div::getFormStartFromSession($form)));
		if (isset($this->settings['global']['disableIpLog']) && $this->settings['global']['disableIpLog'] == 0) {
			$newMail->setSenderIp(t3lib_div::getIndpEnv('REMOTE_ADDR'));
		}
		$newMail->setUserAgent(t3lib_div::getIndpEnv('HTTP_USER_AGENT'));
		$newMail->setMarketingSearchterm($marketingInfos['marketingSearchterm']);
		$newMail->setMarketingReferer($marketingInfos['marketingReferer']);
		$newMail->setMarketingPayedSearchResult($marketingInfos['marketingPayedSearchResult']);
		$newMail->setMarketingLanguage($marketingInfos['marketingLanguage']);
		$newMail->setMarketingBrowserLanguage($marketingInfos['marketingBrowserLanguage']);
		$newMail->setMarketingFunnel($marketingInfos['marketingFunnel']);
		if ($this->settings['main']['optin'] || $this->settings['db']['hidden']) {
			$newMail->setHidden(1);
		}
		$this->mailsRepository->add($newMail);
		$persistenceManager = t3lib_div::makeInstance('Tx_Extbase_Persistence_Manager');
		$persistenceManager->persistAll();

		// tx_powermail_domain_model_answers
		foreach ((array) $field as $uid => $value) { // one loop for every received field
			if (!is_numeric($uid)) {
				continue;
			}
			$newAnswer = t3lib_div::makeInstance('Tx_Powermail_Domain_Model_Answers');
			$newAnswer->setPid(Tx_Powermail_Utility_Div::getStoragePage($this->settings['main']['pid']));
			$newAnswer->setValue($value);
			$newAnswer->setField($uid);
			$newAnswer->setMail($newMail->getUid());

			$this->answersRepository->add($newAnswer);
		}

		return $newMail;
	}
	/**
	 * Session Check: Checks if session was started correct on form delivery
	 *
	 * @param float $indication Indication if check fails
	 * @return void
	 */
	protected function sessionCheck($indication = 1.0) {
		// Stop sessionCheck if indicator was turned to 0 OR if last action was optinConfirm
		if (!$indication || $this->referrer == 'optinConfirm') {
			return;
		}
		$gp = t3lib_div::_GP('tx_powermail_pi1');
		$formUid = $gp['form'];
		$time = Tx_Powermail_Utility_Div::getFormStartFromSession($formUid);

		// if check failes
		if (!isset($time) || !$time) {
			$this->spamIndicator += $indication;
			$this->msg[] = __FUNCTION__ . ' failed';
		}
		return;
	}