<?php

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

class Orders extends CI_Controller {

    public $site_code;
    public $third_part;

    /**
     * Orders constructor.
     */
    function __construct() {
        parent::__construct();
        $this->load->model('Orders_model');
        $this->load->model('CustomerLineInfo_model');
        $this->site_code = strtolower($this->config->item('Site_Code'));
		//屏蔽掉非法IP49.157.0.226
		if($this->input->ip_address() == '116.93.127.114' || $this->input->ip_address() == '116.202.14.3'){
			send_404();
			die();
		}
    }

    public function index() {
        redirect();
    }
	
	//水灯节表单入库
	public function lantern_save() {
		$this->form_validation->set_rules('fullname', 'lang:Firstname', 'required');
		$this->form_validation->set_rules('email', 'lang:email', 'required|valid_email');
        if ($this->form_validation->run() == false) {
            send_404();
        } else {
            $this->Orders_model->COLI_ID = $this->Orders_model->MakeOrderNumber();
            $this->Orders_model->COLI_SenderIP = $this->input->ip_address();
            $this->Orders_model->COLI_OrderDetailText = ReplaceFieldName($_POST);

			$this->Orders_model->COLI_Servicetype = 'T';
			$emailtitle = 'Asia Highlights Enquiry Confirmation';
			$this->Orders_model->COLI_sourcetype = '32001';
			
			//Trace GOOGLE
            $COOKIE_inquireTrackGoogle = get_cookie('inquireTrackGoogle');
            if (!empty($COOKIE_inquireTrackGoogle)) {
                $this->Orders_model->COLI_OrderDetailText .= "\n\rGOOGLE关键字：" . $COOKIE_inquireTrackGoogle;
                delete_cookie('inquireTrackGoogle');
            }
			
            //HT订单跟踪代码
            $COOKIE_inquireTrackFrom = get_cookie('inquireTrackFrom');
            if (!empty($COOKIE_inquireTrackFrom) && is_numeric($COOKIE_inquireTrackFrom)) {
                $this->Orders_model->COLI_sourcetype = (int) $COOKIE_inquireTrackFrom;
                delete_cookie('inquireTrackFrom');
            }
			
			//utm广告跟踪链接
			$utm_source = get_cookie('__ah_utm_source');
			$utm_source_map = array(
            'google' => 78001,
            'newsletters' => 78005,
			'chinahighlights' => 99999
			);
			
			if (!empty($utm_source) && array_key_exists($utm_source, $utm_source_map)) {
				$this->Orders_model->COLI_LineClass = $utm_source_map[$utm_source];
				
				switch ($utm_source_map[$utm_source]){
					case 78001:
						$this->Orders_model->COLI_sourcetype = 32034;
						break;
					case 78005:
						$this->Orders_model->COLI_sourcetype = 32030;
						break;
					case 99999:
						$this->Orders_model->COLI_sourcetype = 32108;
						break;
				}
				
				// 关联订单后统一删除 utm 的 cookie
				delete_cookie('__ah_utm_source', '.asiahighlights.com');
				delete_cookie('__ah_utm_medium', '.asiahighlights.com');
				delete_cookie('__ah_utm_campaign', '.asiahighlights.com');
			}
			
            $this->Orders_model->COLI_GroupType = '19009';
			$this->Orders_model->COLI_PersonNum = $this->input->post('adult_number');
			$this->Orders_model->COLI_ChildNum = $this->input->post('child_number');
            
            $this->Orders_model->MEI_MailList = $this->input->post('email');
			$this->Orders_model->MEI_FirstName = $this->input->post('fullname');
			$this->Orders_model->MEI_LastName = $this->input->post('fullname');
			$fullname = $this->input->post('fullname');
            $this->Orders_model->MEI_Nationality = $this->Orders_model->GetNationalityID($this->input->post('Nationality'));
			
			$this->Orders_model->COLI_ProductSN = $this->input->post('cli_sn');
            $this->Orders_model->COLI_OrderStartDate = date('Y-m-d',strtotime($this->input->post('date')));
			
            $this->Orders_model->MEI_Gender = $this->input->post('gender');
			$this->Orders_model->MEI_Phone = $this->input->post('PhoneNo');
			
            if (!empty($third_webcode)) {
                $this->Orders_model->COLI_WebCode = $third_webcode;
            }
			
            $this->Orders_model->TourOrderSave();
            $this->Orders_model->SendMail($fullname, $this->input->post('email'), $this->config->item('Site_ServiceName'), $this->config->item('Site_ServiceEmail'),$emailtitle , $this->Orders_model->COLI_OrderDetailText);
            //发送邮件给客人
            $this->Orders_model->SendMail($this->config->item('Site_ServiceName'), $this->config->item('Site_ServiceEmail'), $fullname, $this->input->post('email'), $emailtitle, $this->load->view('ah/email/lantern_email', '', true));
			
		    //生成支付链接
			$this->load->library('Currency');
			$addurl = '';
			$totalprice = $this->input->post('totalprice');
			$total_rmb = $this->currency->get_USD_RMB_SUM($totalprice);
			$signstr = 'currency=USD&order_id='.$this->Orders_model->COLI_ID.'_T&rmb_amount='.$total_rmb.'&total_amount='.$totalprice.'&key=7a46484300f04031b42fdd44559578e4';
			$sign = md5($signstr);
			$addurl .= base64_encode('order_id='.$this->Orders_model->COLI_ID.'_T&subject=Booking Yi Peng Festival Tickets&body=Booking Yi Peng Festival Tickets&total_amount='.$totalprice.'&currency=USD&rmb_amount='.$total_rmb.'&sign='.$sign.'&lg=en_AH&return_url=https://www.asiahighlights.com/orders/inquiry_thankyou');
			$payurl = 'https://secure.chinahighlights.com/pay/paymentservice/?'.$addurl;
		    //跳转到thankyou页面
		    redirect($payurl);
        }
    }
	
	public function tailormade_save(){
		$this->form_validation->set_rules('name', 'lang:Firstname', 'required');
		$this->form_validation->set_rules('email', 'lang:email', 'required|valid_email');
		if ($this->form_validation->run() == false) {
			send_404();
			die();
		}else{
			$this->Orders_model->COLI_ID = $this->Orders_model->MakeOrderNumber();
            $this->Orders_model->COLI_SenderIP = $this->input->ip_address();
            $this->Orders_model->COLI_OrderDetailText = ReplaceFieldName($_POST);
			
			$this->Orders_model->COLI_Servicetype = 'T';
			$emailtitle = 'Asia Highlights Enquiry Confirmation';
			
			//默认就是TM表单
			$this->Orders_model->COLI_sourcetype = '32003';
			
			//Trace GOOGLE
            $COOKIE_inquireTrackGoogle = get_cookie('inquireTrackGoogle');
            if (!empty($COOKIE_inquireTrackGoogle)) {
                $this->Orders_model->COLI_OrderDetailText .= "\n\rGOOGLE关键字：" . $COOKIE_inquireTrackGoogle;
                delete_cookie('inquireTrackGoogle');
            }
			
            //HT订单跟踪代码
            $COOKIE_inquireTrackFrom = get_cookie('inquireTrackFrom');
            if (!empty($COOKIE_inquireTrackFrom) && is_numeric($COOKIE_inquireTrackFrom)) {
                $this->Orders_model->COLI_sourcetype = (int) $COOKIE_inquireTrackFrom;
                delete_cookie('inquireTrackFrom');
            }
			
			//utm广告跟踪链接
			$utm_source = get_cookie('__ah_utm_source');
			$utm_source_map = array(
            'google' => 78001,
            'newsletters' => 78005,
			'chinahighlights' => 99999
			);
			
			if (!empty($utm_source) && array_key_exists($utm_source, $utm_source_map)) {
				$this->Orders_model->COLI_LineClass = $utm_source_map[$utm_source];
				
				switch ($utm_source_map[$utm_source]){
					case 78001:
						$this->Orders_model->COLI_sourcetype = 32034;
						break;
					case 78005:
						$this->Orders_model->COLI_sourcetype = 32030;
						break;
					case 99999:
						$this->Orders_model->COLI_sourcetype = 32108;
						break;
				}
				
				// 关联订单后统一删除 utm 的 cookie
				delete_cookie('__ah_utm_source', '.asiahighlights.com');
				delete_cookie('__ah_utm_medium', '.asiahighlights.com');
				delete_cookie('__ah_utm_campaign', '.asiahighlights.com');
			}
			
            $this->Orders_model->COLI_GroupType = '19006';
            $this->Orders_model->COLI_OrderType = '19006';
            $this->Orders_model->COLI_PersonNum = $this->input->post('adultnumber');
			$this->Orders_model->COLI_ChildNum = $this->input->post('kidnumber');
            $this->Orders_model->MEI_MailList = $this->input->post('email');
			$this->Orders_model->MEI_LastName = $this->input->post('name');
			$fullname = $this->input->post('name');
            $this->Orders_model->MEI_Nationality = $this->Orders_model->GetNationalityID($this->input->post('Nationality'));
			
			$this->Orders_model->COLI_ProductSN = $this->input->post('cli_sn');
            
			if(!empty($this->input->post('Date_Start'))){
				$this->Orders_model->COLI_OrderStartDate = $this->input->post('Date_Start');
			}else{
				$this->Orders_model->COLI_OrderStartDate = $this->input->post('Date_Start_Mobile');
			}
			
			
            $this->Orders_model->MEI_Gender = $this->input->post('gender');
			$this->Orders_model->MEI_Phone = $this->input->post('PhoneNo');
			
            //是否来至第三方合作网站
            $third_webcode = $this->input->post('third_webcode');
            if (empty($third_webcode)) {
                $third_webcode = $this->input->cookie('third_code');
                $this->input->set_cookie('third_code', '', '');
                $this->input->set_cookie('third%5Fcode', '', '');
            }
            if (!empty($third_webcode)) {
                $this->Orders_model->COLI_WebCode = $third_webcode;
            }
			/*print_r($this->load->view('ah/email/tailormade_email', '', true));
			die();*/
            $this->Orders_model->TourOrderSave();
            $this->Orders_model->SendMail($fullname, $this->input->post('email'), $this->config->item('Site_ServiceName'), $this->config->item('Site_ServiceEmail'),$emailtitle , $this->Orders_model->COLI_OrderDetailText);
            //发送邮件给客人
            $this->Orders_model->SendMail($this->config->item('Site_ServiceName'), $this->config->item('Site_ServiceEmail'), $fullname, $this->input->post('email'), $emailtitle, $this->load->view('ah/email/tailormade_email', '', true));

			redirect(site_url('orders/thankyou?tailormade'));
		}
	}
	
	public function inquiry_save(){
		$this->form_validation->set_rules('name', 'lang:Firstname', 'required');
		$this->form_validation->set_rules('email', 'lang:email', 'required|valid_email');
		if ($this->form_validation->run() == false) {
			send_404();
			die();
		}else{
			$this->Orders_model->COLI_ID = $this->Orders_model->MakeOrderNumber();
            $this->Orders_model->COLI_SenderIP = $this->input->ip_address();
            $this->Orders_model->COLI_OrderDetailText = ReplaceFieldName($_POST);
			$this->Orders_model->COLI_Servicetype = 'T';
			$emailtitle = 'Asia Highlights Enquiry Confirmation';
			
			//默认就是产品订单
			$this->Orders_model->COLI_sourcetype = '32001';
			
			//Trace GOOGLE
            $COOKIE_inquireTrackGoogle = get_cookie('inquireTrackGoogle');
            if (!empty($COOKIE_inquireTrackGoogle)) {
                $this->Orders_model->COLI_OrderDetailText .= "\n\rGOOGLE关键字：" . $COOKIE_inquireTrackGoogle;
                delete_cookie('inquireTrackGoogle');
            }
			
            //HT订单跟踪代码
            $COOKIE_inquireTrackFrom = get_cookie('inquireTrackFrom');
            if (!empty($COOKIE_inquireTrackFrom) && is_numeric($COOKIE_inquireTrackFrom)) {
                $this->Orders_model->COLI_sourcetype = (int) $COOKIE_inquireTrackFrom;
                delete_cookie('inquireTrackFrom');
            }
			
			//utm广告跟踪链接
			$utm_source = get_cookie('__ah_utm_source');
			$utm_source_map = array(
            'google' => 78001,
            'newsletters' => 78005,
			'chinahighlights' => 99999
			);
			
			if (!empty($utm_source) && array_key_exists($utm_source, $utm_source_map)) {
				$this->Orders_model->COLI_LineClass = $utm_source_map[$utm_source];
				
				switch ($utm_source_map[$utm_source]){
					case 78001:
						$this->Orders_model->COLI_sourcetype = 32034;
						break;
					case 78005:
						$this->Orders_model->COLI_sourcetype = 32030;
						break;
					case 99999:
						$this->Orders_model->COLI_sourcetype = 32108;
						break;
				}
				
				// 关联订单后统一删除 utm 的 cookie
				delete_cookie('__ah_utm_source', '.asiahighlights.com');
				delete_cookie('__ah_utm_medium', '.asiahighlights.com');
				delete_cookie('__ah_utm_campaign', '.asiahighlights.com');
			}
			
            $this->Orders_model->COLI_GroupType = '19006';
            $this->Orders_model->COLI_OrderType = '19006';
            $this->Orders_model->COLI_PersonNum = $this->input->post('adultnumber');
			$this->Orders_model->COLI_ChildNum = $this->input->post('kidnumber');
            $this->Orders_model->MEI_MailList = $this->input->post('email');
			$this->Orders_model->MEI_LastName = $this->input->post('name');
			$fullname = $this->input->post('name');
            $this->Orders_model->MEI_Nationality = $this->Orders_model->GetNationalityID($this->input->post('Nationality'));
			
			$this->Orders_model->COLI_ProductSN = $this->input->post('cli_sn');
            if(empty($this->input->post('Date_Start'))){
				$this->Orders_model->COLI_OrderStartDate = $this->input->post('Date_Start_Mobile');
			}else{
				$this->Orders_model->COLI_OrderStartDate = $this->input->post('Date_Start');
			}
			
			
            $this->Orders_model->MEI_Gender = $this->input->post('gender');
			$this->Orders_model->MEI_Phone = $this->input->post('PhoneNo');
			
            //是否来至第三方合作网站
            $third_webcode = $this->input->post('third_webcode');
            if (empty($third_webcode)) {
                $third_webcode = $this->input->cookie('third_code');
                $this->input->set_cookie('third_code', '', '');
                $this->input->set_cookie('third%5Fcode', '', '');
            }
            if (!empty($third_webcode)) {
                $this->Orders_model->COLI_WebCode = $third_webcode;
            }
			
            $this->Orders_model->TourOrderSave();
            $this->Orders_model->SendMail($fullname, $this->input->post('email'), $this->config->item('Site_ServiceName'), $this->config->item('Site_ServiceEmail'),$emailtitle , $this->Orders_model->COLI_OrderDetailText);
            //发送邮件给客人 
            $this->Orders_model->SendMail($this->config->item('Site_ServiceName'), $this->config->item('Site_ServiceEmail'), $fullname, $this->input->post('email'), $emailtitle, $this->load->view('ah/email/inquiry_email', '', true));
			redirect(site_url('orders/thankyou'));
		}
	}
	
	//快速表单
	public function quick_inquiry_save(){
		$this->form_validation->set_rules('name', 'lang:Firstname', 'required');
		$this->form_validation->set_rules('email', 'lang:email', 'required|valid_email');
		if ($this->form_validation->run() == false) {
			send_404();
			die();
		}else{
			$this->Orders_model->COLI_ID = $this->Orders_model->MakeOrderNumber();
            $this->Orders_model->COLI_SenderIP = $this->input->ip_address();
            $this->Orders_model->COLI_OrderDetailText = ReplaceFieldName($_POST);
			$this->Orders_model->COLI_Servicetype = 'T';
			$emailtitle = 'Asia Highlights Enquiry Confirmation';
			$this->Orders_model->COLI_sourcetype = '32113';
			
			//Trace GOOGLE
            $COOKIE_inquireTrackGoogle = get_cookie('inquireTrackGoogle');
            if (!empty($COOKIE_inquireTrackGoogle)) {
                $this->Orders_model->COLI_OrderDetailText .= "\n\rGOOGLE关键字：" . $COOKIE_inquireTrackGoogle;
                delete_cookie('inquireTrackGoogle');
            }
			
            //HT订单跟踪代码
            $COOKIE_inquireTrackFrom = get_cookie('inquireTrackFrom');
            if (!empty($COOKIE_inquireTrackFrom) && is_numeric($COOKIE_inquireTrackFrom)) {
                $this->Orders_model->COLI_sourcetype = (int) $COOKIE_inquireTrackFrom;
                delete_cookie('inquireTrackFrom');
            }
			
			//utm广告跟踪链接
			$utm_source = get_cookie('__ah_utm_source');
			$utm_source_map = array(
            'google' => 78001,
            'newsletters' => 78005,
			'chinahighlights' => 99999
			);
			
			if (!empty($utm_source) && array_key_exists($utm_source, $utm_source_map)) {
				$this->Orders_model->COLI_LineClass = $utm_source_map[$utm_source];
				
				switch ($utm_source_map[$utm_source]){
					case 78001:
						$this->Orders_model->COLI_sourcetype = 32034;
						break;
					case 78005:
						$this->Orders_model->COLI_sourcetype = 32030;
						break;
					case 99999:
						$this->Orders_model->COLI_sourcetype = 32108;
						break;
				}
				
				// 关联订单后统一删除 utm 的 cookie
				delete_cookie('__ah_utm_source', '.asiahighlights.com');
				delete_cookie('__ah_utm_medium', '.asiahighlights.com');
				delete_cookie('__ah_utm_campaign', '.asiahighlights.com');
			}
			
            $this->Orders_model->COLI_GroupType = '19006';
            $this->Orders_model->COLI_OrderType = '19006';
            $this->Orders_model->COLI_PersonNum = $this->input->post('adultnumber');
			$this->Orders_model->COLI_ChildNum = $this->input->post('kidnumber');
            $this->Orders_model->MEI_MailList = $this->input->post('email');
			$this->Orders_model->MEI_LastName = $this->input->post('name');
			$fullname = $this->input->post('name');
            $this->Orders_model->MEI_Nationality = $this->Orders_model->GetNationalityID($this->input->post('Nationality'));
			
			$this->Orders_model->COLI_ProductSN = $this->input->post('cli_sn');
            
			$this->Orders_model->COLI_OrderStartDate = $this->input->post('starting_date');
			
            $this->Orders_model->MEI_Gender = $this->input->post('gender');
			$this->Orders_model->MEI_Phone = $this->input->post('PhoneNo');

            $cli_no = $this->input->post('cli_no');
            // 根据产品编号(CLI_NO)查找并绑定产品SN(CLI_SN)        
            if ($cli_no) {
                $tour_obj = $this->CustomerLineInfo_model->search($cli_no, 1);
                if (!empty($tour_obj)) {
                    $this->Orders_model->COLI_ProductSN = $tour_obj->CLI_SN;
                }
            }
			
            //是否来至第三方合作网站
            $third_webcode = $this->input->post('third_webcode');
            if (empty($third_webcode)) {
                $third_webcode = $this->input->cookie('third_code');
                $this->input->set_cookie('third_code', '', '');
                $this->input->set_cookie('third%5Fcode', '', '');
            }
            if (!empty($third_webcode)) {
                $this->Orders_model->COLI_WebCode = $third_webcode;
            }
			
            $this->Orders_model->TourOrderSave();
            $this->Orders_model->SendMail($fullname, $this->input->post('email'), $this->config->item('Site_ServiceName'), $this->config->item('Site_ServiceEmail'),$emailtitle , $this->Orders_model->COLI_OrderDetailText);
            //发送邮件给客人 
            $this->Orders_model->SendMail($this->config->item('Site_ServiceName'), $this->config->item('Site_ServiceEmail'), $fullname, $this->input->post('email'), $emailtitle, $this->load->view('ah/email/quickinquiry_email', '', true));

			redirect(site_url('orders/thankyou?inquire'));
		}
	}
	
	//新版tailormade入库程序
	public function triprequest_save() {
		//$this->form_validation->set_rules('Destination', 'lang:Destination', 'required');
		//$this->form_validation->set_rules('Date_Start', 'lang:Date_Start', 'required');
        //$this->form_validation->set_rules('hotelStyle', 'lang:hotelStyle', 'required');
		//$this->form_validation->set_rules('additionalrequirements', 'lang:additionalrequirements', 'required');
		//$this->form_validation->set_rules('gender', 'lang:Title', 'required');
		$this->form_validation->set_rules('Firstname', 'lang:Firstname', 'required');
		//$this->form_validation->set_rules('Lastname', 'lang:Lastname', 'required');
		$this->form_validation->set_rules('email', 'lang:email', 'required|valid_email');
        //$this->form_validation->set_rules('Nationality', 'lang:Nationality', 'required');
        
        //$this->form_validation->set_rules('hotel', 'lang:hotel', 'required');
        //$this->form_validation->set_rules('tourdays', 'lang:tourdays', 'required');
        if ($this->form_validation->run() == false) {
            $data = array();
            foreach ($this->form_validation->_error_array as $key => $value) {
				if($key == 'Destination'){
					$key = 'destinations';
				}
                $data[] = array('name' => $key, 'value' => $value);
            }
            if ($this->input->post('no_ajax')) {
                $vali_string = '';
                foreach ($data as $v) {
                    $vali_string .= $v['name'] . ' => ' . $v['value'] . '  ';
                }
                echo('<script>alert("' . $vali_string . '");history.back();</script>');
            } else { 
                echo json_encode($data);
            }
        } else {
            $this->Orders_model->COLI_ID = $this->Orders_model->MakeOrderNumber();
            $this->Orders_model->COLI_SenderIP = $this->input->ip_address();
            $this->Orders_model->COLI_OrderDetailText = ReplaceFieldName($_POST);
			
			$this->Orders_model->COLI_Servicetype = 'T';
			$emailtitle = 'Asia Highlights Enquiry Confirmation';
			if($this->input->post('cli_sn')){
				//含有cli_sn的是产品订单
				$this->Orders_model->COLI_sourcetype = '32001';
			}else{
				//不含有的是tailormade订单
				$this->Orders_model->COLI_sourcetype = '32003';
			}
			
			
			
			//Trace GOOGLE
            $COOKIE_inquireTrackGoogle = get_cookie('inquireTrackGoogle');
            if (!empty($COOKIE_inquireTrackGoogle)) {
                $this->Orders_model->COLI_OrderDetailText .= "\n\rGOOGLE关键字：" . $COOKIE_inquireTrackGoogle;
                delete_cookie('inquireTrackGoogle');
            }
			
            //HT订单跟踪代码
            $COOKIE_inquireTrackFrom = get_cookie('inquireTrackFrom');
            if (!empty($COOKIE_inquireTrackFrom) && is_numeric($COOKIE_inquireTrackFrom)) {
                $this->Orders_model->COLI_sourcetype = (int) $COOKIE_inquireTrackFrom;
                delete_cookie('inquireTrackFrom');
            }
			
			//utm广告跟踪链接
			$utm_source = get_cookie('__ah_utm_source');
			$utm_source_map = array(
            'google' => 78001,
            'newsletters' => 78005,
			'chinahighlights' => 99999
			);
			
			if (!empty($utm_source) && array_key_exists($utm_source, $utm_source_map)) {
				$this->Orders_model->COLI_LineClass = $utm_source_map[$utm_source];
				
				switch ($utm_source_map[$utm_source]){
					case 78001:
						$this->Orders_model->COLI_sourcetype = 32034;
						break;
					case 78005:
						$this->Orders_model->COLI_sourcetype = 32030;
						break;
					case 99999:
						$this->Orders_model->COLI_sourcetype = 32108;
						break;
				}
				
				// 关联订单后统一删除 utm 的 cookie
				delete_cookie('__ah_utm_source', '.asiahighlights.com');
				delete_cookie('__ah_utm_medium', '.asiahighlights.com');
				delete_cookie('__ah_utm_campaign', '.asiahighlights.com');
			}
			
            $this->Orders_model->COLI_GroupType = '19006';
            $this->Orders_model->COLI_OrderType = '19006';
            if (is_numeric($this->input->post('adultsNumber'))) {
                $this->Orders_model->COLI_PersonNum = $this->input->post('adultsNumber');
            }
            if (is_numeric($this->input->post('ChildrenNumber'))) {
                $this->Orders_model->COLI_ChildNum = $this->input->post('ChildrenNumber');
            }
            if (is_numeric($this->input->post('BabiesNumber'))) {
                $this->Orders_model->COLI_BabyNum = $this->input->post('BabiesNumber');
            }
            $this->Orders_model->MEI_MailList = $this->input->post('email');
            $this->Orders_model->MEI_Mail = $this->input->post('email2');
			/*
            $pos = strstr($this->input->post('realname'), ' ');
            if ($pos === false) {
                $this->Orders_model->MEI_FirstName = $this->input->post('realname');
            } else {
                $this->Orders_model->MEI_FirstName = str_replace($pos, '', $this->input->post('realname'));
                $this->Orders_model->MEI_LastName = $pos;
            }
			*/
			$this->Orders_model->MEI_FirstName = $this->input->post('Firstname');
			$this->Orders_model->MEI_LastName = $this->input->post('Lastname');
			$fullname = $this->input->post('Firstname').$this->input->post('Lastname');
            $this->Orders_model->MEI_Nationality = $this->Orders_model->GetNationalityID($this->input->post('Nationality'));
			
			$this->Orders_model->COLI_ProductSN = $this->input->post('cli_sn');
            
            if ($this->input->post('Date_Start')) {
                $this->Orders_model->COLI_OrderStartDate = $this->input->post('Date_Start');
            }else{
				$this->Orders_model->COLI_OrderStartDate = $this->input->post('Date_Start_Mobile');
			}
			
            $this->Orders_model->MEI_Gender = $this->input->post('gender');
			$this->Orders_model->MEI_Phone = $this->input->post('PhoneNo');
			
            //是否来至第三方合作网站
            $third_webcode = $this->input->post('third_webcode');
            if (empty($third_webcode)) {
                $third_webcode = $this->input->cookie('third_code');
                $this->input->set_cookie('third_code', '', '');
                $this->input->set_cookie('third%5Fcode', '', '');
            }
            if (!empty($third_webcode)) {
                $this->Orders_model->COLI_WebCode = $third_webcode;
            }

            //指定操作人
            if ($this->input->post('COLI_OPI_SN')) {
                $this->Orders_model->COLI_OPI_SN = $this->input->post('COLI_OPI_SN');
            }
			
			//print_r($this->load->view('orders/newemail_template', '', true));
			//die();
            $this->Orders_model->TourOrderSave();
            $this->Orders_model->SendMail($fullname, $this->input->post('email'), $this->config->item('Site_ServiceName'), $this->config->item('Site_ServiceEmail'),$emailtitle , $this->Orders_model->COLI_OrderDetailText);
            //发送邮件给客人 (20160504tailor_make)
            $this->Orders_model->SendMail($this->config->item('Site_ServiceName'), $this->config->item('Site_ServiceEmail'), $fullname, $this->input->post('email'), $emailtitle, $this->load->view('orders/newemail_template', '', true));

            //是否AJAX提交？
            $no_ajax = $this->input->post('no_ajax');
            if (empty($no_ajax)) {
                $data[] = array('name' => 'go', 'value' => site_url('orders/thankyou'));
                echo json_encode($data);
            } else {
                $file_alias = $this->input->post('file_alias');
                redirect(site_url('orders/thankyou' . $file_alias));
            }
        }
    }
	
    public function question_save() {
        $this->form_validation->set_rules('realname', 'lang:realname', 'required');
        $this->form_validation->set_rules('email', 'lang:email', 'required|valid_email');
        $this->form_validation->set_rules('aquestion', 'lang:aquestion', 'required');
        if ($this->form_validation->run() == false) {
            $data = array();
            foreach ($this->form_validation->_error_array as $key => $value) {
                $data[] = array('name' => $key, 'value' => $value);
            }
            echo json_encode($data);
        } else {
            bind_ip($this->input->ip_address());
            $this->Orders_model->COLI_ID = $this->Orders_model->MakeOrderNumber();
            $this->Orders_model->COLI_SenderIP = $this->input->ip_address();
            $this->Orders_model->COLI_OrderDetailText = ReplaceFieldName($_POST);
            $this->Orders_model->COLI_Servicetype = 'T';
            $this->Orders_model->COLI_sourcetype = '32002';

            //HT订单跟踪代码
            $COOKIE_inquireTrackFrom = get_cookie('inquireTrackFrom');
            if (!empty($COOKIE_inquireTrackFrom) && is_numeric($COOKIE_inquireTrackFrom)) {
                $this->Orders_model->COLI_sourcetype = (int) $COOKIE_inquireTrackFrom;
                delete_cookie('inquireTrackFrom');
            }
            //Trace GOOGLE
            $COOKIE_inquireTrackGoogle = get_cookie('inquireTrackGoogle');
            if (!empty($COOKIE_inquireTrackGoogle)) {
                $this->Orders_model->COLI_OrderDetailText .= "\n\rGOOGLE关键字：" . $COOKIE_inquireTrackGoogle;
                delete_cookie('inquireTrackGoogle');
            }
			
			//utm广告跟踪链接
			$utm_source = get_cookie('__ah_utm_source');
			$utm_source_map = array(
            'google' => 78001,
            'newsletters' => 78005,
			'chinahighlights' => 99999
			);
			
			if (!empty($utm_source) && array_key_exists($utm_source, $utm_source_map)) {
				if($utm_source_map[$utm_source] != 99999){
					$this->Orders_model->COLI_LineClass = $utm_source_map[$utm_source];
				}else{
					$this->Orders_model->COLI_LineClass = '';
				}
				
				switch ($utm_source_map[$utm_source]){
					case 78001:
						$this->Orders_model->COLI_sourcetype = 32034;
						break;
					case 78005:
						$this->Orders_model->COLI_sourcetype = 32030;
						break;
					case 99999:
						$this->Orders_model->COLI_sourcetype = 32108;
						break;
				}
				
				// 关联订单后统一删除 utm 的 cookie
				delete_cookie('__ah_utm_source', '.asiahighlights.com');
				delete_cookie('__ah_utm_medium', '.asiahighlights.com');
				delete_cookie('__ah_utm_campaign', '.asiahighlights.com');
			}
			
            $this->Orders_model->COLI_GroupType = '19006';
            $this->Orders_model->COLI_OrderType = '19006';
            $this->Orders_model->MEI_MailList = $this->input->post('email');
            $pos = strstr($this->input->post('realname'), ' ');
            if ($pos === false) {
                $this->Orders_model->MEI_FirstName = $this->input->post('realname');
            } else {
                $this->Orders_model->MEI_FirstName = str_replace($pos, '', $this->input->post('realname'));
                $this->Orders_model->MEI_LastName = $pos;
            }
            $this->COLI_ProductSN = '';
            bind_email($this->Orders_model->MEI_MailList);
            $this->Orders_model->TourOrderSave();
            $this->Orders_model->SendMail($this->input->post('realname'), $this->input->
                            post('email'), $this->config->item('Site_ServiceName'), $this->config->item('Site_ServiceEmail'), 'Question Orders', $this->Orders_model->COLI_OrderDetailText);

            //发送邮件给客人
            $this->Orders_model->SendMail($this->config->item('Site_ServiceName'), $this->config->item('Site_ServiceEmail'), $this->input->post('realname'), $this->input->
                            post('email'), 'Question Orders', $this->load->view('ah/email/email_template', '', true));


            //是否AJAX提交？
            $no_ajax = $this->input->post('no_ajax');
            if (empty($no_ajax)) {
                $data[] = array('name' => 'ok', 'value' => site_url('orders/question-thankyou/'));
                echo json_encode($data);
            } else {
                $file_alias = $this->input->post('file_alias');
                redirect(site_url('orders/question-thankyou/' . $file_alias));
            }
        }
    }

    public function question_thankyou() {
        $data = array();
        $this->html_do->head($data, 'r');
        $data['PPCCode'] = $this->lang->line('PPC_question');
        $this->load->view('orders/r_thankyou/' . $this->site_code, $data);
    }

    public function detect_spam($param_name) {
        $html_tag = '#<\s*\/?(meta|script|html|a|div|body|input|img|title|link|form)\s+[^>]*?>#im';
        $http_url = '/((https|http):[\/]{2})*[\w]+[.]{1}[a-z\d\-]+[.a-z\d-\/.htm]*/';
        $chinese_string = '/(赢|美女|注册|领取|红包|彩金)/';
        $html_encode = '/&#(.*);/';// 包含 HTML 编码的字符，如：&#25105;
        $param_value = $this->input->post($param_name);
        $end_str = $param_value;
        $end_str = preg_replace($html_tag, '', $end_str);
        $end_str = preg_replace($http_url, '', $end_str);
        $end_str = preg_replace($chinese_string, '', $end_str);        
        $end_str = preg_replace($html_encode, '', $end_str);
        return strcasecmp($param_value, $end_str) === 0;
    }

    /**
     * 
     * contact-us.htm 订单入库
     * 
     */
    public function contactus_save() {
        $this->form_validation->set_rules('realname', 'lang:realname', 'required');
        $this->form_validation->set_rules('email', 'lang:email', 'required');
        // $this->form_validation->set_rules('comments', 'lang:comments', 'required');
        if ($this->form_validation->run() == false) {
            $data = array();
            foreach ($this->form_validation->_error_array as $key => $value) {
                $data[] = array('name' => $key, 'value' => $value);
            }
            echo json_encode($data);
        } else {
			//添加隐藏字段判断是否为扫描提交
			if ( $this->input->get_post("nullemail") != NULL) {
				log_message('error', "reject by Robot Detected.".json_encode($_POST));
                redirect(site_url('orders/thankyou'));
				exit;
			}
            $this->Orders_model->COLI_ID = $this->Orders_model->MakeOrderNumber();
            $this->Orders_model->COLI_SenderIP = $this->input->ip_address();

            $this->Orders_model->COLI_OrderDetailText = ReplaceFieldName($_POST);
            $this->Orders_model->COLI_Servicetype = 'T';
            $this->Orders_model->COLI_sourcetype = '32007';

            //HT订单跟踪代码
            $COOKIE_inquireTrackFrom = get_cookie('inquireTrackFrom');
            if (!empty($COOKIE_inquireTrackFrom) && is_numeric($COOKIE_inquireTrackFrom)) {
                $this->Orders_model->COLI_sourcetype = (int) $COOKIE_inquireTrackFrom;
                delete_cookie('inquireTrackFrom');
            }
            //Trace GOOGLE
            $COOKIE_inquireTrackGoogle = get_cookie('inquireTrackGoogle');
            if (!empty($COOKIE_inquireTrackGoogle)) {
                $this->Orders_model->COLI_OrderDetailText .= "\n\rGOOGLE关键字：" . $COOKIE_inquireTrackGoogle;
                delete_cookie('inquireTrackGoogle');
            }
			
			//utm广告跟踪链接
			$utm_source = get_cookie('__ah_utm_source');
			$utm_source_map = array(
            'google' => 78001,
            'newsletters' => 78005,
			'chinahighlights' => 99999
			);
			
			if (!empty($utm_source) && array_key_exists($utm_source, $utm_source_map)) {
				if($utm_source_map[$utm_source] != 99999){
					$this->Orders_model->COLI_LineClass = $utm_source_map[$utm_source];
				}else{
					$this->Orders_model->COLI_LineClass = '';
				}
				
				switch ($utm_source_map[$utm_source]){
					case 78001:
						$this->Orders_model->COLI_sourcetype = 32034;
						break;
					case 78005:
						$this->Orders_model->COLI_sourcetype = 32030;
						break;
					case 99999:
						$this->Orders_model->COLI_sourcetype = 32108;
						break;
				}
				
				// 关联订单后统一删除 utm 的 cookie
				delete_cookie('__ah_utm_source', '.asiahighlights.com');
				delete_cookie('__ah_utm_medium', '.asiahighlights.com');
				delete_cookie('__ah_utm_campaign', '.asiahighlights.com');
			}

            if (FALSE === $this->detect_spam('realname') ||
                FALSE === $this->detect_spam('AdditionalRequirements2')) {
                $this->Orders_model->COLI_Purpose = '-1';
				
                redirect(site_url('orders/thankyou'));
				exit;
            }
			
            $this->Orders_model->COLI_GroupType = '19006';
            $this->Orders_model->COLI_OrderType = '19006';
            $this->Orders_model->MEI_MailList = $this->input->post('email');
            $pos = strstr($this->input->post('realname'), ' ');
            if ($pos === false) {
                $this->Orders_model->MEI_FirstName = $this->input->post('realname');
            } else {
                $this->Orders_model->MEI_FirstName = str_replace($pos, '', $this->input->post('realname'));
                $this->Orders_model->MEI_LastName = $pos;
            }
            $this->COLI_ProductSN = '';
            $this->Orders_model->TourOrderSave();
            $this->Orders_model->SendMail($this->input->post('realname'), $this->input->post('email'), $this->config->item('Site_ServiceName'), $this->config->item('Site_ServiceEmail'), 'Asia Highlights Enquiry Confirmation', $this->Orders_model->COLI_OrderDetailText);
            //发送邮件给客人
            $this->Orders_model->SendMail($this->config->item('Site_ServiceName'), $this->config->item('Site_ServiceEmail'), $this->input->post('realname'), $this->input->post('email'), 'Asia Highlights Enquiry Confirmation', $this->load->view('ah/email/email_template', '', true));

            
            redirect(site_url('orders/thankyou?contactus'));
        }
    }

    public function thankyou() {
        $data = array();
        $data['seo_title'] = 'Thank you for your inquiry | Asia Highlights';
        //$data['PPCCode'] = $this->lang->line('PPC_contact_us');
        $this->load->view('header', $data);
        $this->load->view('orders/thank_you', $data);
        $this->load->view('footer');
    }

    /**
     * 联系我们订单入库
     * @author lmr 
     */
    public function callus_save() {
        $this->form_validation->set_rules('realname', 'lang:realname', 'required');
        $this->form_validation->set_rules('email', 'lang:email', 'required|valid_email');
        $this->form_validation->set_rules('comments', 'lang:comments', 'required');
        if ($this->form_validation->run() == false) {
            $data = array();
            foreach ($this->form_validation->_error_array as $key => $value) {
                $data[] = array('name' => $key, 'value' => $value);
            }
            echo json_encode($data);
        } else {
            $this->Orders_model->COLI_ID = $this->Orders_model->MakeOrderNumber();
            $this->Orders_model->COLI_SenderIP = $this->input->ip_address();
            $this->Orders_model->COLI_OrderDetailText = ReplaceFieldName($_POST);
            $this->Orders_model->COLI_Servicetype = 'T';
            $this->Orders_model->COLI_sourcetype = '32004';

            //HT订单跟踪代码
            $COOKIE_inquireTrackFrom = get_cookie('inquireTrackFrom');
            if (!empty($COOKIE_inquireTrackFrom) && is_numeric($COOKIE_inquireTrackFrom)) {
                $this->Orders_model->COLI_sourcetype = (int) $COOKIE_inquireTrackFrom;
                delete_cookie('inquireTrackFrom');
            }
            //Trace GOOGLE
            $COOKIE_inquireTrackGoogle = get_cookie('inquireTrackGoogle');
            if (!empty($COOKIE_inquireTrackGoogle)) {
                $this->Orders_model->COLI_OrderDetailText .= "\n\rGOOGLE关键字：" . $COOKIE_inquireTrackGoogle;
                delete_cookie('inquireTrackGoogle');
            }
			
			//utm广告跟踪链接
			$utm_source = get_cookie('__ah_utm_source');
			$utm_source_map = array(
            'google' => 78001,
            'newsletters' => 78005,
			'chinahighlights' => 99999
			);
			
			if (!empty($utm_source) && array_key_exists($utm_source, $utm_source_map)) {
				if($utm_source_map[$utm_source] != 99999){
					$this->Orders_model->COLI_LineClass = $utm_source_map[$utm_source];
				}else{
					$this->Orders_model->COLI_LineClass = '';
				}
				
				switch ($utm_source_map[$utm_source]){
					case 78001:
						$this->Orders_model->COLI_sourcetype = 32034;
						break;
					case 78005:
						$this->Orders_model->COLI_sourcetype = 32030;
						break;
					case 99999:
						$this->Orders_model->COLI_sourcetype = 32108;
						break;
				}
				
				// 关联订单后统一删除 utm 的 cookie
				delete_cookie('__ah_utm_source', '.asiahighlights.com');
				delete_cookie('__ah_utm_medium', '.asiahighlights.com');
				delete_cookie('__ah_utm_campaign', '.asiahighlights.com');
			}

            $this->Orders_model->COLI_GroupType = '19006';
            $this->Orders_model->COLI_OrderType = '19006';
            $this->Orders_model->MEI_MailList = $this->input->post('email');
            $pos = strstr($this->input->post('realname'), ' ');
            if ($pos === false) {
                $this->Orders_model->MEI_FirstName = $this->input->post('realname');
            } else {
                $this->Orders_model->MEI_FirstName = str_replace($pos, '', $this->input->post('realname'));
                $this->Orders_model->MEI_LastName = $pos;
            }
            $this->COLI_ProductSN = '';
            bind_email($this->Orders_model->MEI_MailList);
            $this->Orders_model->TourOrderSave();
            $this->Orders_model->SendMail($this->input->post('realname'), $this->input->
                            post('email'), $this->config->item('Site_ServiceName'), $this->config->item('Site_ServiceEmail'), 'Contactus Orders', $this->Orders_model->COLI_OrderDetailText);
            //发送邮件给客人
            $this->Orders_model->SendMail($this->config->item('Site_ServiceName'), $this->config->item('Site_ServiceEmail'), $this->input->post('realname'), $this->input->
                            post('email'), 'Contactus Orders', $this->load->view('ah/email/email_template', '', true));
            $data[] = array('name' => 'ok', 'value' => site_url('orders/callus-thankyou/'));
            echo json_encode($data);
        }
    }

    public function callus_thankyou() {
        $data = array();
        $data['PPCCode'] = $this->lang->line('PPC_callus');
		$data['seo_title'] = 'Thank you for your inquiry | Asia Highlights';
        $this->load->view('header', $data);
        $this->load->view('orders/thank_you', $data);
        $this->load->view('footer');
    }
	
    //ch 过来的
    public function ch_tailormade_save() {
		$this->form_validation->set_rules('city', 'lang:city', 'required');
        $this->form_validation->set_rules('realname', 'lang:realname', 'required');
        //$this->form_validation->set_rules('nationality', 'lang:nationality', 'required');
        $this->form_validation->set_rules('email', 'lang:email', 'required|valid_email');
        //$this->form_validation->set_rules('additionalrequirements', 'lang:additionalrequirements', 'required');
        //$this->form_validation->set_rules('cli_tourdays', 'lang:cli_tourdays', 'required');
        if ($this->form_validation->run() == false) {
            $data = array();
            foreach ($this->form_validation->_error_array as $key => $value) {
                $data[] = array('name' => $key, 'value' => $value);
            }
            if ($this->input->post('no_ajax')) {
                $vali_string = '';
                foreach ($data as $v) {
                    $vali_string .= $v['name'] . ' => ' . $v['value'] . '  ';
                }
                echo('<script>alert("' . $vali_string . '");history.back();</script>');
            } else {
                echo json_encode($data);
            }
        } else {
            $this->Orders_model->COLI_ID = $this->Orders_model->MakeOrderNumber();
            $this->Orders_model->COLI_SenderIP = $this->input->ip_address();
            $this->Orders_model->COLI_OrderDetailText = ReplaceFieldName($_POST);
            $this->Orders_model->COLI_Servicetype = 'T';
            $this->Orders_model->COLI_sourcetype = '32108';

            //HT订单跟踪代码
            $COOKIE_inquireTrackFrom = get_cookie('inquireTrackFrom');
            if (!empty($COOKIE_inquireTrackFrom) && is_numeric($COOKIE_inquireTrackFrom)) {
                $this->Orders_model->COLI_sourcetype = (int) $COOKIE_inquireTrackFrom;
                delete_cookie('inquireTrackFrom');
            }
            //Trace GOOGLE
            $COOKIE_inquireTrackGoogle = get_cookie('inquireTrackGoogle');
            if (!empty($COOKIE_inquireTrackGoogle)) {
                $this->Orders_model->COLI_OrderDetailText .= "\n\rGOOGLE关键字：" . $COOKIE_inquireTrackGoogle;
                delete_cookie('inquireTrackGoogle');
            }
			
			//utm广告跟踪链接
			$utm_source = get_cookie('__ah_utm_source');
			$utm_source_map = array(
            'google' => 78001,
            'newsletters' => 78005,
			'chinahighlights' => 99999
			);
			
			if (!empty($utm_source) && array_key_exists($utm_source, $utm_source_map)) {
				if($utm_source_map[$utm_source] != 99999){
					$this->Orders_model->COLI_LineClass = $utm_source_map[$utm_source];
				}else{
					$this->Orders_model->COLI_LineClass = '';
				}
				
				switch ($utm_source_map[$utm_source]){
					case 78001:
						$this->Orders_model->COLI_sourcetype = 32034;
						break;
					case 78005:
						$this->Orders_model->COLI_sourcetype = 32030;
						break;
					case 99999:
						$this->Orders_model->COLI_sourcetype = 32108;
						break;
				}
				
				// 关联订单后统一删除 utm 的 cookie
				delete_cookie('__ah_utm_source', '.asiahighlights.com');
				delete_cookie('__ah_utm_medium', '.asiahighlights.com');
				delete_cookie('__ah_utm_campaign', '.asiahighlights.com');
			}
			
            $this->Orders_model->COLI_GroupType = '19006';
            $this->Orders_model->COLI_OrderType = '19006';
            if (is_numeric($this->input->post('adultsNumber'))) {
                $this->Orders_model->COLI_PersonNum = $this->input->post('adultsNumber');
            }
            if (is_numeric($this->input->post('ChildrenNumber'))) {
                $this->Orders_model->COLI_ChildNum = $this->input->post('ChildrenNumber');
            }
            if (is_numeric($this->input->post('BabiesNumber'))) {
                $this->Orders_model->COLI_BabyNum = $this->input->post('BabiesNumber');
            }
            $this->Orders_model->MEI_MailList = $this->input->post('email');
            $this->Orders_model->MEI_Mail = $this->input->post('email2');
            $pos = strstr($this->input->post('realname'), ' ');
            if ($pos === false) {
                $this->Orders_model->MEI_FirstName = $this->input->post('realname');
            } else {
                $this->Orders_model->MEI_FirstName = str_replace($pos, '', $this->input->post('realname'));
                $this->Orders_model->MEI_LastName = $pos;
            }
            $this->Orders_model->MEI_Nationality = $this->Orders_model->GetNationalityID($this->
                    input->post('nationality'));
            $this->COLI_ProductSN = '';
            if ($this->input->post('Date_Start')) {
                $this->Orders_model->COLI_OrderStartDate = $this->input->post('Date_Start');
            }
            $this->Orders_model->MEI_Gender = $this->input->post('gender');
            $this->Orders_model->MEI_Phone = $this->input->post('PhoneNo');

            //是否来至第三方合作网站
            $third_webcode = $this->input->post('third_webcode');
            if (empty($third_webcode)) {
                $third_webcode = $this->input->cookie('third_code');
                $this->input->set_cookie('third_code', '', '');
                $this->input->set_cookie('third%5Fcode', '', '');
            }
            if (!empty($third_webcode)) {
                $this->Orders_model->COLI_WebCode = $third_webcode;
            }

            //指定操作人
            if ($this->input->post('COLI_OPI_SN')) {
                $this->Orders_model->COLI_OPI_SN = $this->input->post('COLI_OPI_SN');
            }
            $this->Orders_model->TourOrderSave();
            $this->Orders_model->SendMail($this->input->post('realname'), $this->input->
                            post('email'), $this->config->item('Site_ServiceName'), $this->config->item('Site_ServiceEmail'), 'Tailor Made Orders', $this->Orders_model->COLI_OrderDetailText);
            //发送邮件给客人 (20160504tailor_make)
            $this->Orders_model->SendMail($this->config->item('Site_ServiceName'), $this->config->item('Site_ServiceEmail'), $this->input->post('realname'), $this->input->post('email'), 'Tailor Made Orders', $this->load->view('ah/email/email_template', '', true));

            //是否AJAX提交？
            $no_ajax = $this->input->post('no_ajax');
            if (empty($no_ajax)) {
                $data[] = array('name' => 'go', 'value' => site_url('orders/thankyou'));
                echo json_encode($data);
            } else {
                // $file_alias = $this->input->post('file_alias');
                redirect("https://www.chinahighlights.com/forms/thankyou-asia-tours.asp");
            }
        }
    }
	
    public function tailormade_thankyou() {
        $data = array();
		$data['seo_title'] = 'Asia Highlights Travel';
        $this->load->view('header', $data);
        $this->load->view('orders/thank_you', $data);
        $this->load->view('footer');
		$this->output->cache(99999);
    }

    public function asia_tour() {
        $data = array();
		
        $this->form_validation->set_rules('cli_no', 'lang:cli_no', 'required');
        if ($this->form_validation->run() == false) {
            redirect();
        }
		
        $data['post_cli_no'] = $this->input->post('cli_no');
        $data['post_cli_sn'] = $this->input->post('cli_sn');
        $data['post_days'] = $this->input->post('cli_days');
        $data['post_destinations'] = $this->input->post('destinations');
        $data['adultsNumber'] = $this->input->post('adultsNumber');
        $data['ChildrenNumber'] = $this->input->post('ChildrenNumber');
        $data['BabiesNumber'] = $this->input->post('BabiesNumber');
        $data['post_Starting_Date'] = $this->input->post('Starting_Date');
		
        $this->load->view('header', $data);
        $this->load->view('orders/asia_tour_form');
        $this->load->view('footer');
    }

    public function asia_tour_save() {
        $this->form_validation->set_rules('realname', 'lang:realname', 'required');
        $this->form_validation->set_rules('nationality', 'lang:nationality', 'required');
        $this->form_validation->set_rules('email', 'lang:email', 'required|valid_email');
        $this->form_validation->set_rules('additionalrequirements', 'lang:additionalrequirements', 'required');
        $this->form_validation->set_rules('hotel', 'lang:hotel', 'required');
        $this->form_validation->set_rules('starting_date', 'lang:starting_date', 'required');
        if ($this->form_validation->run() == false) {
            $data = array();
            foreach ($this->form_validation->_error_array as $key => $value) {
                $data[] = array('name' => $key, 'value' => $value);
            }
            echo json_encode($data);
        } else {
            $this->Orders_model->COLI_ID = $this->Orders_model->MakeOrderNumber();
            $this->Orders_model->COLI_SenderIP = $this->input->ip_address();
            $this->Orders_model->COLI_OrderDetailText = ReplaceFieldName($_POST);
            $this->Orders_model->COLI_Servicetype = 'T';
            $this->Orders_model->COLI_sourcetype = '32001';

            //HT订单跟踪代码
            $COOKIE_inquireTrackFrom = get_cookie('inquireTrackFrom');
            if (!empty($COOKIE_inquireTrackFrom) && is_numeric($COOKIE_inquireTrackFrom)) {
                $this->Orders_model->COLI_sourcetype = (int) $COOKIE_inquireTrackFrom;
                delete_cookie('inquireTrackFrom');
            }
            //Trace GOOGLE
            $COOKIE_inquireTrackGoogle = get_cookie('inquireTrackGoogle');
            if (!empty($COOKIE_inquireTrackGoogle)) {
                $this->Orders_model->COLI_OrderDetailText .= "\n\rGOOGLE关键字：" . $COOKIE_inquireTrackGoogle;
                delete_cookie('inquireTrackGoogle');
            }
			
			//utm广告跟踪链接
			$utm_source = get_cookie('__ah_utm_source');
			$utm_source_map = array(
            'google' => 78001,
            'newsletters' => 78005,
			'chinahighlights' => 99999
			);
			
			if (!empty($utm_source) && array_key_exists($utm_source, $utm_source_map)) {
				if($utm_source_map[$utm_source] != 99999){
					$this->Orders_model->COLI_LineClass = $utm_source_map[$utm_source];
				}else{
					$this->Orders_model->COLI_LineClass = '';
				}
				
				switch ($utm_source_map[$utm_source]){
					case 78001:
						$this->Orders_model->COLI_sourcetype = 32034;
						break;
					case 78005:
						$this->Orders_model->COLI_sourcetype = 32030;
						break;
					case 99999:
						$this->Orders_model->COLI_sourcetype = 32108;
						break;
				}
				
				// 关联订单后统一删除 utm 的 cookie
				delete_cookie('__ah_utm_source', '.asiahighlights.com');
				delete_cookie('__ah_utm_medium', '.asiahighlights.com');
				delete_cookie('__ah_utm_campaign', '.asiahighlights.com');
			}

            $this->Orders_model->COLI_GroupType = '19001';
            $this->Orders_model->COLI_OrderType = '19001';
            $this->Orders_model->COLI_Name = $this->input->post('cli_no') . ' ' . $this->
                    input->post('cli_tourtitle');
            $this->Orders_model->COLI_PersonNum = $this->input->post('adultsNumber');
            $this->Orders_model->COLI_ChildNum = $this->input->post('ChildrenNumber');
            $this->Orders_model->COLI_BabyNum = $this->input->post('BabiesNumber');
            $this->Orders_model->COLI_OrderStartDate = $this->input->post('starting_date');
            $this->Orders_model->COLI_Days = $this->input->post('cli_tourdays');

            $this->Orders_model->MEI_MailList = $this->input->post('email');
            $this->Orders_model->MEI_Mail = $this->input->post('email2');
            $pos = strstr($this->input->post('realname'), ' ');
            if ($pos === false) {
                $this->Orders_model->MEI_FirstName = $this->input->post('realname');
            } else {
                $this->Orders_model->MEI_FirstName = str_replace($pos, '', $this->input->post('realname'));
                $this->Orders_model->MEI_LastName = $pos;
            }
            $this->Orders_model->MEI_Nationality = $this->Orders_model->GetNationalityID($this->
                    input->post('nationality'));
            $this->Orders_model->MEI_Gender = $this->input->post('gender');
            if (empty($this->Orders_model->MEI_Phone) || !isset($this->Orders_model->
                            MEI_Phone)) {
                $this->Orders_model->MEI_Phone = @$this->input->post('PhoneNo');
            }
            $this->Orders_model->COLI_ProductSN = $this->input->post('cli_sn');

            //是否来至第三方合作网站
            $third_webcode = $this->input->post('third_webcode');
            if (empty($third_webcode)) {
                $third_webcode = $this->input->cookie('third_code');
                $this->input->set_cookie('third_code', '', '');
                $this->input->set_cookie('third%5Fcode', '', '');
            }
            if (!empty($third_webcode)) {
                $this->Orders_model->COLI_WebCode = $third_webcode;
            }
            $this->Orders_model->TourOrderSave();
            $this->Orders_model->SendMail($this->input->post('realname'), $this->input->
                            post('email'), $this->config->item('Site_ServiceName'), $this->config->item('Site_ServiceEmail'), 'Tour Orders', $this->Orders_model->COLI_OrderDetailText);
            //发送邮件给客人(20160504inquiry_form)
            $this->Orders_model->SendMail($this->config->item('Site_ServiceName'), $this->config->item('Site_ServiceEmail'), $this->input->post('realname'), $this->input->
                            post('email'), 'Tour Orders', $this->load->view('ah/email/email_template', '', true));

            $data[] = array('name' => 'go', 'value' => site_url('orders/asia_tour_thankyou'));
            echo json_encode($data);
        }
    }

    public function asia_tour_thankyou() {
        $data = array();
		$data['seo_title'] = 'Tour Inquiry | Asia Highlights';
        $this->load->view('header', $data);
        $this->load->view('orders/thank_you', $data);
        $this->load->view('footer');
    }
}
