/**
  * Validate and handle adding and removing possessions via AJAX
  *
  * @return void
  */
 public function possessionAction()
 {
     $output = array();
     $pageSession = new Zend_Session_Namespace('tenants_insurance_quote');
     // TODO: This needs to stop using datasources directly and use the quote manager
     // Add the bikes details to the database
     $possession = new Datasource_Insurance_Policy_SpecPossessions($pageSession->PolicyNumber);
     $ajaxForm = new TenantsInsuranceQuote_Form_Json_Possessions();
     // Ignore 'away_from_home' and 'above_x' fields for AJAX requests
     $ajaxForm->subform_possessions->getElement('away_from_home')->setRequired(false);
     $ajaxForm->subform_possessions->getElement('above_x')->setRequired(false);
     $request = $this->getRequest();
     $postdata = $request->getPost();
     // Filter to mirror the subform element definitions.
     $currencyFilterElements = array('possession_value');
     foreach ($currencyFilterElements as $filterElement) {
         if (isset($postdata[$filterElement])) {
             $postdata[$filterElement] = preg_replace(array('/[^\\d\\.]/'), array(''), $postdata[$filterElement]);
         }
     }
     // Force the elements to be required as this is an add item click - ugly :(
     if (isset($postdata['addPossession']) && $postdata['addPossession'] == 1) {
         $ajaxForm->subform_possessions->getElement('possession_categoryId')->setRequired(true);
         $ajaxForm->subform_possessions->getElement('possession_description')->setRequired(true);
         $ajaxForm->subform_possessions->getElement('possession_value')->setRequired(true);
     }
     $ajaxForm->populate($postdata);
     $quoteManager = new Manager_Insurance_TenantsContentsPlus_Quote(null, null, $pageSession->PolicyNumber);
     if ($ajaxForm->isValid($postdata)) {
         // Check if a new possession's details are being added and possessions is below max
         if (isset($postdata['addPossession']) && $postdata['addPossession'] == 1) {
             $cleanData = $ajaxForm->getValues();
             // According to the Zend manual these *should* be the clean values
             $possession->addNew($cleanData['subform_possessions']);
         }
         // Check if an existing possession's details are being removed
         if (isset($postdata['removePossession']) && $postdata['removePossession'] == 1) {
             $possession->remove($postdata['possessionNum']);
         }
         $totalValue = $possession->getTotalValue();
         // Now we need to update the total amounts covered in the quote manager
         $quoteManager->setCoverAmount($totalValue, Manager_Insurance_TenantsContentsPlus_Quote::SPECIFIEDPOSSESSIONS);
     }
     $errorMessages = $ajaxForm->getMessagesFlattened();
     $output['errorJs'] = $errorMessages;
     $output['errorCount'] = count($errorMessages);
     $output['errorHtml'] = $this->view->partial('partials/error-list.phtml', array('errors' => $errorMessages));
     // Tell page if max possessions reached
     $output['disableAdd'] = $possession->countPossessions() == $possession->maxPossessions ? 1 : 0;
     $output['html'] = $this->view->getHelper('yourPossessions')->yourPossessions();
     $premiums = $quoteManager->calculatePremiums();
     $fees = $quoteManager->getFees();
     $output['premiums'] = $premiums;
     $output['fees'] = $fees;
     echo Zend_Json::encode($output);
 }