<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

	class Contact extends Site_Controller {

	   function __construct()
	   {
			parent::__construct();
	   }		


        public function index()
        {
			$this->load->library(array('form_validation','email'));
			$this->form_validation->set_rules('first-name', 'First Name','required|max_length[20]');
			$this->form_validation->set_rules('last-name', 'Last Name','required|max_length[20]');
			$this->form_validation->set_rules('email', 'Email', 'required|valid_email');
			$this->form_validation->set_rules('telephone', 'Mobile', 'required|integer');
			$this->form_validation->set_message('integer', 'Mobile field  mustbe number');
			$this->form_validation->set_rules('message', 'Message', 'required');
			
			if ($this->form_validation->run() == TRUE)
			{
				$firstName = trim($this->input->post('first-name',TRUE));
				$lastname = trim($this->input->post('last-name',TRUE));
				$phone = trim($this->input->post('telephone',TRUE));
				$email = trim($this->input->post('email',TRUE));
				$message = trim($this->input->post('message',TRUE));
				$to = 'eungsovan@gmail.com';
				$msg ="<table width=500 style='border:solid #BBD0F7 2px;'><tr style='background-color:#EEE;'>
						<td><font color=red>Detail</font></td>
						<tr><td>First Name <td>: $firstName \n</td></tr>
						<tr><td>Last Name <td>: $lastname
						<tr><td>Phone Number <td>: $phone
						<tr><td>Email <td>: $email
						<tr><td>Message <td>: $message
					  </table><br/>";	  
	
				if($this->sendMail($email,$to,$msg))
				{
					$this->session->set_flashdata('message',"Message had send successfully");
					redirect('Contact');
					exit();
				}
				else
				{
					$this->session->set_flashdata('message',"Message had send failure!");
				    redirect('Contact');
					exit();
				}		
			}

			$this->load->view('header');
			$this->load->view('pages/contact');
			$this->load->view('footer');
        }

	
		private function sendMail($from,$to,$msg)
		{
			$config['charset'] = 'iso-8859-1';
			$config['mailtype'] = 'html';
			
			$this->email->initialize($config);
			$this->email->from($from);
			$this->email->to($to);
			$this->email->subject('Recommendation');
			$this->email->message($msg);
			
			if($this->email->send())
			{
				return TRUE;
			}
			else
			{
				return FALSE;
			}
			$this->email->print_debugger();	
		}

	}