<? include("../settings.php"); ?>
<?
	$code_error = 0;
	if ($_REQUEST['confirm']!=""){
        switch ($_REQUEST['registration_type']){
           case "general":
                $payment_status = "NOT PAID";
                $registration_cost = $_REQUEST['total_registration'];
                $source = "http://www.events-sp.com/SINCE/register/since_general.php";
                $insert = array(
                    "registrant_type"=>$_REQUEST['registration_type'],
                    "registrant_papernumber"=>"N.A.",
                    "registrant_salutation"=>$_REQUEST['registrant_salutation'],
                    "registrant_familyname"=>$_REQUEST['registrant_familyname'],
                    "registrant_firstname"=>$_REQUEST['registrant_firstname'],
                    "registrant_organisation"=>$_REQUEST['registrant_organisation'],
                    "registrant_mailing"=>$_REQUEST['registrant_mailing'],
                    "registrant_city"=>$_REQUEST['registrant_city'],
                    "registrant_postal"=>$_REQUEST['registrant_postal'],
                    "registrant_country"=>$_REQUEST['registrant_country'],
					"registrant_cc"=>$_REQUEST['registrant_contactcc'],
                    "registrant_telephone"=>$_REQUEST['registrant_contact'],
                    "registrant_email"=>$_REQUEST['registrant_email'],
                    "registrant_diet"=>$_REQUEST['registrant_diet'],
                    "registrant_cocktail_attend"=>$_REQUEST['registrant_reception'],
                    "registrant_dinner_attend"=>$_REQUEST['registrant_dinner'],
                    "registrant_extra_dinner"=>$_REQUEST['registrant_extra_dinner'],
                    "registrant_total"=>$_REQUEST['total_registration'],
                    "registrant_paymentmode"=>$_REQUEST['payment_mode'],
                    "registrant_paid"=>$payment_status,
                    "registrant_selection"=>$_REQUEST['general_registration'],
					"registrant_accompany"=>$_REQUEST['registrant_accompany'],
					"accompany_lastname"=>$_REQUEST['registrant_accompanying_lastname'],
					"accompany_firstname"=>$_REQUEST['registrant_accompanying_firstname'],
					"accompany_reception"=>$_REQUEST['registrant_accompanying_reception'],
					"accompany_dinner"=>$_REQUEST['registrant_accompanying_dinner']
                );
                break;
            case "author":
                $payment_status = "NOT PAID";
                $registration_cost = $_REQUEST['total_registration'];
                $source = "http://www.events-sp.com/SINCE/register/author.php";
                $insert = array(
                    "registrant_type"=>$_REQUEST['registration_type'],
                    "registrant_papernumber"=>$_REQUEST['registrant_papernum'],
                    "registrant_salutation"=>$_REQUEST['registrant_salutation'],
                    "registrant_familyname"=>$_REQUEST['registrant_familyname'],
                    "registrant_firstname"=>$_REQUEST['registrant_firstname'],
                    "registrant_organisation"=>$_REQUEST['registrant_organisation'],
                    "registrant_mailing"=>$_REQUEST['registrant_mailing'],
                    "registrant_city"=>$_REQUEST['registrant_city'],
                    "registrant_postal"=>$_REQUEST['registrant_postal'],
                    "registrant_country"=>$_REQUEST['registrant_country'],
					"registrant_cc"=>$_REQUEST['registrant_contactcc'],
                    "registrant_telephone"=>$_REQUEST['registrant_contact'],
                    "registrant_email"=>$_REQUEST['registrant_email'],
                    "registrant_diet"=>$_REQUEST['registrant_diet'],
                    "registrant_cocktail_attend"=>$_REQUEST['registrant_reception'],
                    "registrant_dinner_attend"=>$_REQUEST['registrant_dinner'],
                    "registrant_extra_dinner"=>$_REQUEST['registrant_extra_dinner'],
                    "registrant_total"=>$_REQUEST['total_registration'],
                    "registrant_paymentmode"=>$_REQUEST['payment_mode'],
                    "registrant_paid"=>$payment_status,
                    "registrant_selection"=>$_REQUEST['general_registration'],
					"registrant_accompany"=>$_REQUEST['registrant_accompany'],
					"accompany_lastname"=>$_REQUEST['registrant_accompanying_lastname'],
					"accompany_firstname"=>$_REQUEST['registrant_accompanying_firstname'],
					"accompany_reception"=>$_REQUEST['registrant_accompanying_reception'],
					"accompany_dinner"=>$_REQUEST['registrant_accompanying_dinner']
                );
                break;
            case "ndtss":
				if ($_REQUEST['registrant_code']=="NDT45x" || $_REQUEST['registrant_code']=="Is25Z"){
					$code_error = 0;
				}else
					$code_error = 1;
                $payment_status = "NOT PAID";
                $registration_cost = $_REQUEST['total_registration'];
                $source = "http://www.events-sp.com/SINCE/register/ndtss.php";
                $insert = array(
                    "registrant_type"=>$_REQUEST['registration_type'],
                    "registrant_papernumber"=>$_REQUEST['registrant_papernumber'],
                    "registrant_salutation"=>$_REQUEST['registrant_salutation'],
                    "registrant_familyname"=>$_REQUEST['registrant_familyname'],
                    "registrant_firstname"=>$_REQUEST['registrant_firstname'],
                    "registrant_organisation"=>$_REQUEST['registrant_organisation'],
                    "registrant_mailing"=>$_REQUEST['registrant_mailing'],
                    "registrant_city"=>$_REQUEST['registrant_city'],
                    "registrant_postal"=>$_REQUEST['registrant_postal'],
                    "registrant_country"=>$_REQUEST['registrant_country'],
					"registrant_cc"=>$_REQUEST['registrant_contactcc'],
                    "registrant_telephone"=>$_REQUEST['registrant_contact'],
                    "registrant_email"=>$_REQUEST['registrant_email'],
                    "registrant_diet"=>$_REQUEST['registrant_diet'],
                    "registrant_cocktail_attend"=>$_REQUEST['registrant_reception'],
                    "registrant_dinner_attend"=>$_REQUEST['registrant_dinner'],
                    "registrant_extra_dinner"=>$_REQUEST['registrant_extra_dinner'],
                    "registrant_total"=>$_REQUEST['total_registration'],
                    "registrant_paymentmode"=>$_REQUEST['payment_mode'],
                    "registrant_paid"=>$payment_status,
                    "registrant_selection"=>$_REQUEST['general_registration'],
					"registrant_accompany"=>$_REQUEST['registrant_accompany'],
					"accompany_lastname"=>$_REQUEST['registrant_accompanying_lastname'],
					"accompany_firstname"=>$_REQUEST['registrant_accompanying_firstname'],
					"accompany_reception"=>$_REQUEST['registrant_accompanying_reception'],
					"accompany_dinner"=>$_REQUEST['registrant_accompanying_dinner']
                );
                break;
        }
        //print_r($insert);
        $db->insert("tbl_registrant",$insert);
        $registrant_idarr = $db->run("SELECT LAST_INSERT_ID() as id;");
        $registrant_id = $registrant_idarr[0]['id'];
        $invoice = "SINCE2019-".$registrant_id;

        if (isset($_FILES['student_pass']) && $_FILES['student_pass']['name']!=""){
		$ext = substr(strrchr($_FILES['student_pass']['name'],'.'),1);
		$filename1 = $registrant_id."registrantpic.".$ext;
		move_uploaded_file($_FILES['student_pass']['tmp_name'], $_SERVER['DOCUMENT_ROOT']."/SINCE/student/".$filename1);
            $update = array(
                "student_pic" => $filename1
            );
            $bind = array(
                ":search1" => $registrant_id
            );
            $db->update("tbl_registrant", $update, "registrant_id = :search1", $bind);        
        }        
        
        
        $update = array(
            "paypal_invoice" => $invoice
        );
        $bind = array(
            ":search1" => $registrant_id
        );
        $db->update("tbl_registrant", $update, "registrant_id = :search1", $bind);        
        
        if ($_REQUEST['total_registration']==0){
            $action = "complimentary.php";
        }else{
            switch ($_REQUEST['payment_mode']){
                case "Credit Card":
                    $action = "https://www.paypal.com/cgi-bin/webscr";
                    break;
                case "banktransfer":
                    $action = "banktransfer.php";
                    break;
                case "Other":
                    $action = "others.php";
                    break;
            }
        }
        
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>
Processing
</title>
<!-- Meta Tags -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

</head>

<body>
<?
	if ($code_error==1){
		echo "<script>alert('There is an error with your unique code. Please contact Wendy at secretariat@wccm2019.org for assistance.');window.location='index.php'</script>";
	}else{
?>
<form id="Paypal" name="Paypal" action="<?=$action;?>" method="post">
<!-- testing link -->
<!-- www.sandbox.paypal.com/cgi-bin/webscr-->
<!-- live link -->
<!-- www.paypal.com/cgi-bin/webscr-->

	<center>
		<font face="Arial" size="3">Please wait, transaction processing...</font>
		<?
			if ($_REQUEST['payment_mode']=="Credit Card"){
				echo "<input type='hidden' name='page_style' value='primary'>";
				echo "<input type='hidden' name='first_name' value='".$_REQUEST['registrant_firstname']."'>";
				echo "<input type='hidden' name='address1' value='".$_REQUEST['registrant_mailing']."'>";
				echo "<input type='hidden' name='zip' value='".$_REQUEST['registrant_postal']."'>";
				echo "<input type='hidden' name='email' value='".$_REQUEST['registrant_email']."'>";
				echo "<input type='hidden' name='country' value='SG'>";
				$i=1;
				
				
				echo "<input type='hidden' name='item_name_$i' value=\"SINCE2019 Registration (".strtoupper($_REQUEST['registration_type']).")\">";
				echo "<input type='hidden' name='amount_$i' value=\"".$_REQUEST['total_registration']."\">";
				//echo "<input type='hidden' name='amount_$i' value=\"1\">";
				echo "<input type='hidden' name='quantity_$i' value=\"1\">";
				$i++;

				echo "<input type='hidden' name='invoice' value='$invoice'>";
				echo "<input type='hidden' name='no_shipping' value='1'>";
				echo "<input type='hidden' name='currency_code' value='SGD'>";
				echo "<input type='hidden' name='custom' value='$registrant_id'>";
				echo "<input type='hidden' name='return' value='http://www.events-sp.com/SINCE/register/acknowledge.php'>";
				echo "<input type='hidden' name='cancel_return' value='".$source."'>";
			}else {
				echo "<script>window.location='$action'</script>";
			}
		?>
	</center>
	<input type="hidden" name="business" value="membership@ndtss.org.sg">

	<input type="hidden" name="cmd" value="_cart" />
	<input type="hidden" name="upload" value="1" />
    <input type="hidden" name="page_style" value="SINCE" />
	<?
		if ($_REQUEST['payment_mode']=="Credit Card")
			echo "<script>document.forms.Paypal.submit();</script>";
	}
	?>
</form>

</body>

</html>