/**
  * Handles display, validation and sending of company application form
  */
 public function companyApplicationAction()
 {
     // Instantiate form
     $pageForm = new Connect_Form_ReferencingCompanyApplication();
     $request = $this->getRequest();
     if ($request->isPost()) {
         // We have post data from the company app form - so attempt
         //   validation
         if ($pageForm->isValid($request->getPost())) {
             // Form valid!
             // Format text data ready for e-mail
             $agentManager = new Manager_Core_Agent($this->_agentSchemeNumber);
             $address = $agentManager->getPhysicalAddressByCategory(Model_Core_Agent_ContactMapCategory::OFFICE);
             $agentAddress = $address->toString();
             switch ($request->getParam('how_rg_offered')) {
                 case 1:
                     $howOffered = "Free of charge";
                     break;
                 case 2:
                     $howOffered = "Included in Management Fees";
                     break;
                 case 3:
                     $howOffered = "Separate charge for Rent Guarantee to the landlord";
                     break;
                 case 4:
                     $howOffered = "Referening Only";
                     break;
             }
             switch ($request->getParam('property_managed')) {
                 case 1:
                     $howManaged = "Let Only";
                     break;
                 case 2:
                     $howManaged = "Managed";
                     break;
                 case 3:
                     $howManaged = "Rent Collect";
                     break;
             }
             //Convert the product id into a product name
             $cleanData = $pageForm->getValues();
             $productManager = new Manager_Referencing_Product();
             $product = $productManager->getById($cleanData['subform_product']['product']);
             $productName = empty($product->name) ? '' : $product->name;
             $message = 'Agent Scheme Number: ' . $this->_agentSchemeNumber . "\n" . 'Agent Name: ' . $this->_agentObj->name . "\n" . 'Agent address: ' . $agentAddress . "\n\n" . 'Product: ' . $product->name . "\n" . "\nProperty To Let:\n" . 'Address: ' . $request->getParam('property_address') . "\n" . 'Postcode: ' . $request->getParam('property_postcode') . "\n" . 'Managed Property: ' . $howManaged . "\n" . 'How is Rent Guarantee offered to the landlord: ' . $howOffered . "\n" . 'Total Rent: ' . $request->getParam('tenant_renttotal') . "\n" . 'Rent Share: ' . $request->getParam('tenant_rentshare') . "\n" . 'Tenancy Term: ' . $request->getParam('tenant_term') . "\n" . 'Start Date: ' . $request->getParam('tenant_startdate') . "\n" . 'Total Reference Count: ' . $request->getParam('tenant_number') . "\n" . "\nLandlord Details: \n" . 'First Name: ' . $request->getParam('landlord_firstname') . "\n" . 'Last Name: ' . $request->getParam('landlord_lastname') . "\n" . 'Address: ' . $request->getParam('landlord_address') . "\n" . 'Postcode: ' . $request->getParam('landlord_postcode') . "\n" . 'Telno: ' . $request->getParam('landlord_landlinenumber') . "\n" . 'Mobile: ' . $request->getParam('landlord_mobilenumber') . "\n" . "\nCompany Details: \n" . 'Company Name: ' . $request->getParam('company_name') . "\n" . 'Trading Name: ' . $request->getParam('company_tradingname') . "\n" . 'Registered Number: ' . $request->getParam('company_registration') . "\n" . 'Incorporation Date: ' . $request->getParam('company_incorporation') . "\n" . 'Contact Name: ' . $request->getParam('company_contactname') . "\n" . 'Telno: ' . $request->getParam('company_phone') . "\n" . "\nCompany Registered Address: \n" . 'Address: ' . $request->getParam('registered_address') . "\n" . 'Postcode: ' . $request->getParam('registered_postcode') . "\n" . 'Period at Address: ' . $request->getParam('registered_years') . " years, " . $request->getParam('registered_months') . " months\n" . "\nCompany Trading Address: \n" . 'Address: ' . $request->getParam('trading_address') . "\n" . 'Postcode: ' . $request->getParam('trading_postcode') . "\n" . 'Period at Address: ' . $request->getParam('trading_years') . " years, " . $request->getParam('trading_months') . " months\n" . "\nAdditional Info: \n" . $request->getParam('additional_info') . "\n" . "\nCompany Signature Details: \n" . 'Name: ' . $request->getParam('representive_name') . "\n" . 'Position: ' . $request->getParam('representive_position') . "\n" . 'Date: ' . $request->getParam('application_date') . "\n";
             // Instantiate mailer manager
             $mailManager = new Application_Core_Mail();
             $mailManager->setTo($this->_params->connect->companyapps->emailToAddress, $this->_params->connect->companyapps->emailToName)->setFrom($this->_params->connect->companyapps->emailFromAddress, $this->_params->connect->companyapps->emailFromName)->setSubject($this->_params->connect->companyapps->emailSubject)->setBodyText($message);
             // Check for uploaded file and persist it if so
             list($fileResult, $fileData) = $this->_uploadPersistentCompanyApplicationFile();
             if ($fileResult === true) {
                 $this->view->fileUploaded = $fileData;
                 // If there's a file, attach it
                 if (isset($fileData['pathToFile'])) {
                     $mailManager->addAttachment($fileData['pathToFile'], $fileData['originalName']);
                 }
                 $mailManager->send();
                 // Clean up uploaded file
                 $this->_deleteCompanyApplicationFile();
                 // Show user confirmation that form submission has been successful
                 $this->_helper->viewRenderer('company-confirmation');
             } else {
                 $this->_helper->flashmessages->addMessage('Problem(s) uploading file:');
                 $this->_helper->flashmessages->addMessage($fileData);
             }
         } else {
             // Tell user there are problems
             $this->_helper->flashmessages->addMessage('Problem(s) in form data:');
             $this->_helper->flashmessages->addMessage($pageForm->getMessagesFlattened(true));
             // Check for uploaded file and persist it if so
             list($fileResult, $fileData) = $this->_uploadPersistentCompanyApplicationFile();
             if ($fileResult === true) {
                 $this->view->fileUploaded = $fileData;
             } else {
                 $this->_helper->flashmessages->addMessage('Problem(s) uploading file:');
                 $this->_helper->flashmessages->addMessage($fileData);
             }
         }
     } else {
         // Form first shown, set a couple of default values
         $pageForm->subform_additional->getElement('additional_info')->setValue('Use this space to provide any additional information that may help us when processing your application.');
         $pageForm->subform_declaration->getElement('application_date')->setValue(date('d/m/Y'));
         // Ensure any previously uploaded file is deleted
         $this->_deleteCompanyApplicationFile();
     }
     $this->view->flashMessages = $this->_helper->flashmessages->getCurrentMessages();
     $this->view->form = $pageForm;
 }