<?php
	$val = array(
		'name' => '',    'email' => '',   'email_conf' => '', 'type' => '4', 
		'type1-1' => '', 'type1-2' => '', 'type1-3' => '',    'body' => '',
		'type2-1' => '', 'type2-2' => '', 'type2-3' => '',    'type3-1' => '',
	);

	$error = array(
		'name' => '',    'email' => '',   'email_conf' => '', 'type' => '', 
		'type1-1' => '', 'type1-2' => '', 'type1-3' => '',    'body' => '',
		'type2-1' => '', 'type2-2' => '', 'type2-3' => '',    'type3-1' => '',
	);
	if(isset($_POST['confirm'])){
		$val = $_POST;
		$flg = false;
		//validate
		if($val['name'] == ''){
			$error['name'] = 'Enter Name.';
			$flg = true;
		}
		if($val['email'] == ''){
			$error['email'] = 'Enter Email Address.';
			$flg = true;
		}else if($val['email'] != '' and !preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $val['email'])){
			$error['email'] = 'Email Format is incorrect.';
			$flg = true;
		}
		if($val['email'] != '' and $val['email'] != $val['email_conf']){
			$error['email_conf'] = 'Email(Confirm) does not match.';
			$flg = true;
		}

		switch($val['type']){
			case '1':
				if($val['type1-1'] == ''){
					$error['type1-1'] = 'Enter Preferable Time.';
					$flg = true;
				}
				if($val['type1-2'] == ''){
					$error['type1-2'] = 'Enter Preferable Lecturer';
					$flg = true;
				}
				if($val['type1-3'] == ''){
					$error['type1-3'] = 'Enter Preferable Topics';
					$flg = true;
				}
				break;
			case '2':
				if($val['type2-1'] == ''){
					$error['type2-1'] = 'Enter Research topics';
					$flg = true;
				}
				if($val['type2-2'] == ''){
					$error['type2-2'] = 'Enter Priority/Issue to be solved';
					$flg = true;
				}
				if($val['type2-3'] == ''){
					$error['type2-3'] = 'Enter Sample.';
					$flg = true;
				}
				break;
			case '3':
				if($val['type3-1'] == ''){
					$error['type3-1'] = 'Enter Specify Article(s) (News Letter, Publication).';
					$flg = true;
				}
				break;
		}

		if($val['body'] == ''){
			$error['body'] = 'Enter Details.';
			$flg = true;
		}
		if($flg){
			include 'inq_form.html';
		}else{
			include 'inq_freeze.html';
		}
	}elseif(isset($_POST['back'])){
		$val = $_POST;
		include 'inq_form.html';
	}elseif(isset($_POST['send'])){
		$val = $_POST;
		mb_internal_encoding('utf8');
		mb_language('japanese');

		switch($val['type']){
			case '1': 
				$val['type'] = 'Lecture on Demand/Science Cafe (Domestic)';
				$val['type_text'] = "Preferable Time:{$val['type1-1']}
Preferable Lecturer:{$val['type1-2']}
Preferable Topics:{$val['type1-3']}";
				break;
			case '2': 
				$val['type'] = 'Trial Use (In Preparation)';
				$val['type_text'] = "Research Topics :{$val['type2-1']}
Priority/Issue to be solved :{$val['type2-2']}
Sample:{$val['type2-3']}";
				break;
			case '3':
				$val['type'] = 'Request for References';
				$val['type_text'] = "Enter Specify Article(s) (News Letter, Publication):{$val['type3-1']}";
				break;
			case '4':
				$val['type'] = 'Others';
				$val['type_text'] = '';
				break;

		}

		$to = $val['email'];
		$title1 = 'USM - Inquiry from Web Form';
		$title2 = '超低速ミュオン顕微鏡 - お問合せがありました';
				
		$body1 = <<<TEXT
Dear $val[name]

Thank you for your inquiry.
I accepted with the following contents.
Please wait until the reply

Name$val[name]
Email Address:$val[email]
Inquiry Type:$val[type]
$val[type_text]
Details:
$val[body]

------------------------------------
(MEXT) Grant-in-Aid for Scientific Research on Innovative Areas 
 Frontier of Materials, Life and Particle Science 
 Explored by Ultra Slow Muon Microscope 
 E-mail:slowmuon@yamanashi.ac.jp 
 TEL: +81-55-220-8681/FAX: +81-55-220-8682 
 University of Yamanashi 
 4-3-11 Takeda,Kofu,Yamanashi 400-8511 Japan
 
------------------------------------
TEXT;
		$body2 = <<<TEXT

下記の内容でお問合せがありました。

お名前:$val[name]
メールアドレス:$val[email]
問合せ区分:$val[type]
$val[type_text]
内容:
$val[body]

TEXT;
		$target = 'slowmuon@yamanashi.ac.jp';
		//$target = 'kurokodile@gmail.com';
		
		mb_send_mail($to, $title1, $body1, 'From:'. $target);
		mb_send_mail($target, $title2, $body2, 'From:'. $target);
		
		header('location:http://'.$_SERVER['SERVER_NAME'].'/english/inq_done.html');
	}
	else{
		include 'inq_form.html';
	}
?>
