<?php
class EmailForm extends Controller {

   function EmailForm(){
      parent::Controller();
    session_start();
	$this->lang->load('public');
    $this->load->library('cart');
		#check to see if the default lang_id exists, and if not set it
	if($this->session->userdata('lang_id')==null){
		$ld='1';
		$this->session->set_userdata('lang_id',$ld);
		//echo $ld;
	}else{
		$ld=$this->session->userdata('lang_id');
	}
   }

	function index() {
		//$ld=$this->session->userdata('lang_id');
	  	# Validations
		//echo $ld;
	  	//$this->load->library('validation');

		
		$this->validation->set_error_delimiters('<div class="warning">', '</div>');
		$data['liID']=$this->uri->segment(1);
		$data['title'] = $this->lang->line('links_contact');
		$data['left_nav'] = 'left_nav';
		$data['right_nav'] = 'right_nav';

	
	  	$rules['name'] = "trim|required|min_length[5]|xss_clean";
	  	$rules['email'] = "trim|required|valid_email";
		$rules['comments']="required";
		$this->validation->set_rules($rules);
	  	
	  	$fields['name']	= $this->lang->line('email_form_name');
		$fields['email']	= $this->lang->line('email_form_email');
		$fields['comments']	= $this->lang->line('email_form_comments');
		$this->validation->set_fields($fields);
		
		# Input and textarea field attributes
		
		$data["name"] = array('name' => 'name', 'id' => 'name');
		$data['email'] = array('name' => 'email', 'id' => 'email');
		$data['comments'] = array('name' => 'comments', 'id' => 'comments', 'rows' => 10, 'cols' => 40);
		$data['key'] = $this->MFuncs->getKeywords();
		$data['keywords'] = $data['key']['keywords'];
		
		$data['main'] = 'emailform';
//		$this->load->vars($data);
//	$this->load->view('template_master');  


		# Checkbox attributes
	//	$data['purpose'] = array('name' => 'seminar[]', 'id' => 'purpose', 'value' => 'Purpose of Prayer', 'checked' => FALSE);
	//	$data['prepare'] = array('name' => 'seminar[]', 'id' => 'prepare', 'value' => 'Prepare for Prayer', 'checked' => FALSE);
	//	$data['principles'] = array('name' => 'seminar[]', 'id' => 'principles', 'value' => 'Principles of Prayer', 'checked' => FALSE);
	//	$data['power'] = array('name' => 'seminar[]', 'id' => 'power', 'value' => 'Power in Prayer', 'checked' => FALSE);
		
		
		if ($this->validation->run() == FALSE)
		{
			$this->load->view('template_master', $data);
		}else{
			$this->load->helper('typography');
			
			$name = $this->input->post('name');
			$email = $this->input->post('email');
			$comments = $this->input->post('comments');
			//$comments=auto_typography($comments);
			
			$message = $comments;
			
			$data['msg_title'] = $name." send the following comments from the contact form";
			$data['comments'] = $message;
			$data['post_msg'] = "";

			$this->load->library('parser');
			$emsg = $this->parser->parse('template_contact', $data, TRUE);

			#Email send to the company!
			//$this->email->from('info@wines2u.gr', $name.' '.$email);
			$this->email->from('no-reply@wines2u.gr', $name.' '.$email);
			$this->email->to('info@wines2u.gr', 'www.wines2u.gr');
			//$this->email->to('dtangos@getid.gr', 'www.wines2u.gr');
			$this->email->subject('www.wines2u.gr - Φόρμα Επικοινωνίας - Contact form');
			$this->email->message($emsg);
			$this->email->send();
	
			$data['msg_title'] = "<strong>".$this->lang->line('dear')."</strong> ".$name.br(2).$this->lang->line('contact_email_message');
			//$data['comments_title'] = $this->lang->line('contact_email_post_message');
			$data['comments'] = $this->lang->line('contact_email_post_message').br(2).$message;
			$data['post_msg'] = $this->lang->line('contact_dept');

			$this->load->library('parser');
			$emsg = $this->parser->parse('template_contact', $data, TRUE);
		
			#Email send to the user
			$this->email->from('no-reply@wines2u.gr', 'www.wines2u.gr');
			//$this->email->from('info@wines2u.gr', 'www.wines2u.gr');
			$this->email->to($email);
			$this->email->subject('www.wines2u.gr - Φόρμα Επικοινωνίας - Contact form');
			$this->email->message($emsg);
			$this->email->send();

			$this->session->set_flashdata('message', $this->lang->line('thank_you'));
			redirect('contact','refresh');
			
		}	
	}
}
?>