<?php

include_once ("includes/setup.inc.php");
//Header("Content-type: application/xml");
Header("Cache-Control: no-cache");

$num_errors = 0;
$dateTimeNow = dateTimeNow();

$indicator_Query = "SELECT t_pemohon.*
				  FROM t_pemohon
				  WHERE login = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, fetch_postget_val("pemohon_login", $_POST, $_GET), PDO::PARAM_STR);
$PDO_stmt->execute();
$indicator_List = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

if($indicator_List['id_pemohon'] > 0)
{
	echo -888;
	exit(0);
}

/*$indicator_Query = "SELECT t_pemohon.*
				  FROM t_pemohon
				  WHERE emel = ?";
$PDO_stmt = $PDO->prepare($indicator_Query);
$PDO_stmt->bindValue(1, fetch_postget_val("emel", $_POST, $_GET), PDO::PARAM_STR);
$PDO_stmt->execute();
$indicator_List2 = $PDO_stmt->fetch(PDO::FETCH_ASSOC);

if($indicator_List2['id_pemohon'] > 0)
{
	echo -777;
	exit(0);
}*/

try
{
	$PASS_COUNTER = 0; //COUNTER FOR DOUBLE CONFIRM
	$PASS_VALUE = 2; //NUMBER OF QUERY TO EXECUTE. IMPORTANT FOR CHECKING!!

	//PDO INIT
	$PDO->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
	$PDO->beginTransaction();
	
	$icno = fetch_postget_val("pemohon_login", $_POST, $_GET);
	$year = substr($icno , 0, 2);
	$year = (($year < date('y'))?('20'.$year):('19'.$year));
	$month = substr($icno , 2, 2);
	$day = substr($icno , 4, 2);

	$indicator_Array = array();
	$indicator_Array[] = array('nama', fetch_postget_val("nama", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('no_tel', fetch_postget_val("no_tel", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('emel', fetch_postget_val("emel", $_POST, $_GET), PDO::PARAM_STR,1);
	$indicator_Array[] = array('id_peranan', 10, PDO::PARAM_INT);
	$indicator_Array[] = array('aktif', 1, PDO::PARAM_INT);
	$indicator_Array[] = array('login', fetch_postget_val("pemohon_login", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('kpbaru', fetch_postget_val("pemohon_login", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('tkh_lahir', $year.'-'.$month.'-'.$day, PDO::PARAM_STR);
	$indicator_Array[] = array('katalaluan', md5(fetch_postget_val("pemohon_pwd", $_POST, $_GET)), PDO::PARAM_STR,1);
	$indicator_Array[] = array('id_soalanrahsia', fetch_postget_val("id_soalanrahsia", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array[] = array('jawapan_rahsia', fetch_postget_val("jawapan_rahsia", $_POST, $_GET), PDO::PARAM_STR);
	$PASS_COUNTER += PDO_INSERT($PDO, 't_pemohon', $indicator_Array);
	$current_id = $PDO->lastInsertId();

	$indicator_Array2 = array();
	$indicator_Array2[] = array('id_pemohon', $current_id, PDO::PARAM_STR);
	$indicator_Array2[] = array('nama_pemohon', fetch_postget_val("nama", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array2[] = array('kpbaru_pemohon', fetch_postget_val("pemohon_login", $_POST, $_GET), PDO::PARAM_STR);
	$indicator_Array2[] = array('tkh_lahir', $year.'-'.$month.'-'.$day, PDO::PARAM_STR);
	$indicator_Array2[] = array('tkh_daftar', $dateTimeNow, PDO::PARAM_STR);
	$PASS_COUNTER += PDO_INSERT($PDO, 'm_borang', $indicator_Array2);

	//*****************************************
	//COMMIT if PASS_COUNTER == PASS_VALUE
	//*****************************************
	if ($PASS_COUNTER == $PASS_VALUE)
	{
		$PDO->commit();
		echo $PASS_COUNTER;
	}
	else
	{
		$PDO->rollBack();
		echo -1;
	}
}
catch (Exception $e) 
{
	$PDO->rollBack();
	echo "Failed: " . $e->getMessage();
}

?>