<?php 
session_start();
#certpay_htm.php for bidp 18se18 requires certpay_fpdf.php working on bidp.bw 14oc18
#MUST handle apostrophes strtr ' => \' works


#echo '<p>post: ';
#print_r($_POST);
#echo '<p>session: ';
#print_r($_SESSION);


#===============================================
#constants
$pagename='BIDP secure services create certificate payment  page';
$key1="bidp";
if ($_SERVER[SERVER_ADDR]!=$_SERVER[SERVER_NAME])
	{$findserver=$_SERVER[SERVER_NAME]. ':' .$_SERVER[SERVER_PORT];}
else
	{$findserver=$_SERVER[SERVER_ADDR]. ':' .$_SERVER[SERVER_PORT];}
if ($_SERVER[HTTPS]=='on')
	{$thisserver = 'https://' .$findserver;}
else
	{$thisserver = 'http://' .$findserver;}
$sitename=$thisserver. '/php/bidp/';
$_SESSION['sitename']=$sitename;
#echo '<p>this= ' .$thisserver; #ok
$thisfile=$_SERVER[PHP_SELF];
$data_base='bidporg'; #mysql
$dpg='bidp'; #pgsql
#$ptable1='';
$table1='omnilabel'; #bidp.bw product codes
$table2='omni'; #bidp.bw product codes
$connect="pear_connect";
$homefile="https://bidp.bw";
#put in logo
$headwords=strtoupper($key1). " payment certificate and pay sheet creator: ";
#echo '<p>head=' .$headwords; #ok 29se18
#import from userin or posted data
if ($_POST['start']=='')
	{$start=1;}
else 
	{$start=$_POST['start'];}
#echo '<p>start=' .$start; #displays correct start

$prodcode=array('conwq' => 'Form of contract with quantities',
								'connq' => 'Form of contract without quantities',
								'conmw' => 'Minor works form of contract',
								'conmwl' => 'Labour only minor works form of contract'); #available from bidp.bw tables
$certtype=array('interim','final');
$defaults=array('prodcode' => 'conwq',
								'payvat' => 'y',
								'paywt' => 'y',
								'deduct' => 'n',
								'certtype' => 'interim',
								'showretain' => 'n');
$thislogo='<P CLASS="western"><IMG SRC="../../pix/logoalpha.png" NAME="Graphic1" ALIGN=LEFT WIDTH=80 HEIGHT=100 BORDER=0><BR CLEAR=LEFT><BR>';
#constants
#----------------------------------------------------

#include 'test.php.'; #works a bit 29se18
include "bidpall.php"; #works bidp.bw 29se18 14oc18
#include "all.php"; #works for 20.12 30se18 14oc18 
require 'DB.php'; #pear only
include "allhere.php";

$connect()
	or die("<p>Could not connect to mysql");
$link=$connect()
	or die("<p>Could not link to mysql");
$link->setFetchMode(DB_FETCHMODE_ASSOC);
#====================================================


#====================================================
#start html header
echo '<html>';
echo '<head>';
echo "<title>$pagename</title>";
echo $stylebidp; #29se18
?>
	<!-- datepicker javascript insert -->
  <!-- calendar stylesheet -->
  <link rel="stylesheet" type="text/css" media="all" href="/php/js/jscalendar/calendar-win2k-cold-1.css" title="win2k-cold-1" />
  <!-- main calendar program -->
  <script type="text/javascript" src="/php/js/jscalendar/calendar.js"></script>
  <!-- language for the calendar -->
  <script type="text/javascript" src="/php/js/jscalendar/lang/calendar-en.js"></script>
  <!-- the following script defines the Calendar.setup helper function, which makes
       adding a calendar a matter of 1 or 2 lines of code. -->
  <script type="text/javascript" src="/php/js/jscalendar/calendar-setup.js"></script>
	<!-- datepicker javascript insert -->
<?php
echo '</head>';
echo '<body BGCOLOR="#e6e64c" OnLoad="focusform()">'; #29se18
#end html header
#====================================================


#====================================================
#begin start=1
#solicit basic options
if ($start==1) #use bidp style maker 19se18
{
	echo '<p>Start=' .$start;
	echo '<table>';	
	echo '<tr><td>';
	echo '<h3>' .$headwords. ' <font color="red">Define framework</font></h3>';
	echo '</td><td>';
	echo $thislogo;
	echo '</td></tr>';
	echo '</table>';

	echo '<hr>';
	echo '<p>Please make your selections, then click Submit.';

	echo '<hr>';
	echo "<form action=\"$thisfile\" method=\"post\">";
	echo '<table>';
	
	#type of contract
	echo '<tr><th align="left">Select contract</th><td>';
	echo '<select name="prodcode">';	
	foreach ($prodcode as $key => $con)
	{
		echo '<option value="'. $key. '"';
		if ($con==$defaults['prodcode'])
			{echo ' selected="selected">';}
		else
			{echo '>';}	
		echo $con. ' </option>';
	}
	echo '</select>';
	echo '</td></tr>';
	
	echo '<tr><td colspan=2><hr></td></tr>';
	
	#show statement of retention?
	echo '<tr><th align="left">Show statement of retention?</th><td>';
	echo '<input type="radio" name="showretain" value="on"';
	if ($defaults['showretain']=='y')
		{echo ' checked="checked"';}
	echo '> Yes';
	echo '<p>';
	echo '<input type="radio" name="showretain" value="off"';
	if ($defaults['showretain']=='n')
		{echo ' checked="checked"';}
	echo '> No';
	echo '</td></tr>';
	
	echo '<tr><td colspan=2><hr></td></tr>';
	
	#is contractor registered for VAT?	
	echo '<tr><th align="left">Is contractor registered for VAT?</th><td>';
	echo '<input type="radio" name="payvat" value="on"';
	if ($defaults['payvat']=='y')
		{echo ' checked="checked"';}
	echo '> Yes';
	echo '<p>';
	echo '<input type="radio" name="payvat" value="off"';
	if ($defaults['payvat']=='n')
		{echo ' checked="checked"';}
	echo '> No';
	echo '</td></tr>';
	
	echo '<tr><td colspan=2><hr></td></tr>';
	
	#is withholding tax to be deducted?	
	echo '<tr><th align="left">Is withholding tax to be deducted?</th><td>';
	echo '<input type="radio" name="paywt" value="on"';
	if ($defaults['payvat']=='y')
		{echo ' checked="checked"';}
	echo '> Yes';
	echo '<p>';
	echo '<input type="radio" name="paywt" value="off"';
	if ($defaults['payvat']=='n')
		{echo ' checked="checked"';}
	echo '> No';
	echo '</td></tr>';
	
	echo '<tr><td colspan=2><hr></td></tr>';
	
	#are damages deductible?
	echo '<tr><th align="left">Are damages potentially deductible?</th><td>';
	echo '<input type="radio" name="ddeduct" value="on"';
	if ($defaults['deduct']=='y')
		{echo ' checked="checked"';}
	echo '> Yes';
	echo '<p>';
	echo '<input type="radio" name="ddeduct" value="off"';
	if ($defaults['deduct']=='n')
		{echo ' checked="checked"';}
	echo '> No';
	echo '</td></tr>';

	echo '<tr><td colspan=2><hr></td></tr>';
	echo '<tr><td><input type="hidden" name="start" value="2"></td></tr>';
	
	echo '<tr><td><input type="submit" name="button" value="Submit"></td></tr>';
	echo '</table>';
	echo '</form>';
	
	echo '<hr>';
	echo 'Home';
	echo $madehere;
}
#end start=1
#====================================================

 
#====================================================
	#start validation of 2 start=3
	if ($start=='3') #is picked up correctly
	{		
		#if ($start==3)  #&& $save==0
		#{
			$defaultp=$_POST; #re-use values when re-editing
			#print_r($_POST); #ok
			#echo $_POST['architectname'] .' here'; #ok
			$errors=array();
		if ($_POST['architectname']=='')
				{$errors[]='Please enter the architect name.';}
		if ($_POST['architectlocation']=='')
				{$errors[]='Please enter the architect location.';}
		if ($_POST['projectname']=='')
				{$errors[]='Please enter the project name.';}
		if ($_POST['projectlocation']=='')
				{$errors[]='Please enter the project location.';}
		if ($_POST['employername']=='')
				{$errors[]='Please enter the employer name.';}
		if ($_POST['employerlocation']=='')
				{$errors[]='Please enter the employer location.';}
		if ($_POST['contractorname']=='')
				{$errors[]='Please enter the contractor name.';}
		if ($_POST['contractorlocation']=='')
				{$errors[]='Please enter the contractor location.';}
			#if ($_POST['certdate']=='')
			#	{$errors[]='Please enter the certificate date.';}
			#if ($_POST['certtype']=='')
			#	{$errors[]='Please enter the certificate type.';}
		#if ($_POST['certnumber']=='')
		#		{$errors[]='Please enter the certificate number.';}
		#echo '<p>errs='; #ok
		#print_r($errors); #ok
			#if ($settype=='isacc')
			#{
			#	if ($_POST['accno']=='')
			#	{$errors[]='Please enter the account number.';}
			#}
			#print_r($errors); #ok
		if ($errors) #if the array has content
		{
			$errortext ='<p>You need to correct the following error/s:';
			$errortext .= '<tr><td><font color="red"><b><ul><li>';
			$errortext .= implode('</li><li>', $errors); #extract array data, inserting first quote between array fields 24se18
			$errortext .= '</li></ul></b></font></td></tr>';
			$start=2; #edit again using $defaultp as values
		}
		else
		{
				$errortext='';
				$start=4;
		}
		#}
	}
	#end validation of 2 start=3
#====================================================


#====================================================
	#start validation of 4 start=5
	if ($start=='5') #is picked up correctly
	{		
		#if ($start==3)  #&& $save==0
		#{
			$defaultf=$_POST; #re-use values when re-editing
			#print_r($_POST); #ok
			#echo $_POST['architectname'] .' here'; #ok
			$errorsf=array();
		if ($_POST['issuedate']=='')
				{$errorsf[]='Please enter the date of issue.';}
		if ($_POST['valuedate']=='')
				{$errorsf[]='Please enter the date of valuation.';}
		if ($_POST['certnumber']=='')
				{$errorsf[]='Please enter the certificate instalment number.';}
		if ($_POST['congrosamt']=='')
				{$errorsf[]='Please enter the contract gross amount.';}
		if ($_POST['connetretain']=='')
				{$errorsf[]='Please enter the contract nett retention.';}
		if ($_POST['prevcert']=='')
				{$errorsf[]='Please enter the contract previously certified amount.';}
		if ($_SESSION['prodcode']=='conwq' || $_SESSION['prodcode']=='connq')
		{
			echo ''; #not required
		}

		if ($errorsf) #if the array has content
		{
			$errortextf ='<p>You need to correct the following error/s:';
			$errortextf .= '<tr><td><font color="red"><b><ul><li>';
			$errortextf .= implode('</li><li>', $errorsf); #extract array data, inserting first quote between array fields 24se18
			$errortextf .= '</li></ul></b></font></td></tr>';
			$start=4; #edit 4 again using $defaultf as values
		}
		else
		{
				$errortextf='';
				$start=6;
		}
		#}
	}
	#end validation of 4 start=5
#====================================================


#====================================================	
#begin start=2
if ($start=='2') #is picked up correctly
{
	echo '<p>Start=' .$start;
	$showretain=$_POST['showretain'];
	$_SESSION['showretain']=$showretain;
	$prodcode=$_POST['prodcode'];
	$_SESSION['prodcode'] = $_POST['prodcode'];
	$payvat=$_POST['payvat'];
	$_SESSION['payvat'] = $_POST['payvat'];
	$paywt=$_POST['paywt'];
	$_SESSION['paywt'] = $_POST['paywt'];
	#echo '<p>prodcode=' .$prodcode;
	$ddeduct=$_POST['ddeduct'];
	$_SESSION['ddeduct'] = $_POST['ddeduct'];
	?>
	<head><STYLE>
	<!--
	TH { color: #008000; font-family: "Arial", "Helvetica"; font-size: 9pt; text-align: left }
	TD { color: #008000; font-family: "Arial", "Helvetica"; font-size: 9pt }
	H2 { font-family: "Arial", sans-serif }
	P { font-family: "Arial", "Helvetica"; font-size: 9pt }
	H3 { font-family: "Arial", sans-serif }
	H5 { font-family: "Arial", sans-serif }
	-->
	</STYLE></head>
	<?php
	echo '<table>';	
	echo '<tr><td>';
	echo '<h3>' .$headwords. ' <font color="red">Define main options</font></h3>';
	echo '</td><td>';
	echo $thislogo;
	echo '</td></tr>';
	echo '</table>';
	
	echo '<table>';
	#Display framework
	echo '<tr><td>Framework: Contract=' .$_SESSION['prodcode']. '</td><td>Present retention statement=' .$_SESSION['showretain']. '</td><td>Pay VAT=' .$_SESSION['payvat']. '</td><td>Pay withholding tax=' .$_SESSION['paywt']. '</td><td>Damages deductible=' .$_SESSION['ddeduct']. '</td></tr>'; 
	echo '</table>';
	echo '<hr>';
	
	print '<form action="' .$thisfile. '" method="post">';
	if ($errortext>'') #from 3 above
	{
		echo '<p>Validation';
		echo '<table>';
		echo $errortext;
		echo '</table>';
	}
	print '<p>Please enter the project data, then select Submit.';
	echo '<hr>';
	print '<table>';
	
	#get architect name
	echo '<tr><th align="left">Insert architect name</th><td><input type="text" value="' .$defaultp['architectname']. 'df" name="architectname" size="99" maxlength="99"></td></tr>'; #24se18
	#get architect location
	echo '<tr bgcolor="cccccc"><th align="left">Insert architect location</th><td><input type="text" value="x" name="architectlocation" size="99" maxlength="99"></td></tr>';
	
	#get project name
	echo '<tr><th align="left">Insert project name</th><td><input type="text" value="x" name="projectname" size="99" maxlength="99"></td></tr>'; 
	#get project location
	echo '<tr bgcolor="cccccc"><th align="left">Insert project location</th><td><input type="text" value="x" name="projectlocation" size="99" maxlength="99"></td></tr>'; 

	
	#get employer name
	echo '<tr><th align="left">Insert employer name</th><td><input type="text" value="x" name="employername" size="99" maxlength="99"></td></tr>';
	#get employer location
	echo '<tr bgcolor="cccccc"><th align="left">Insert employer location</th><td><input type="text" value="xx" name="employerlocation" size="99" maxlength="99"></td></tr>'; 
	
	#get contractor name
	echo '<tr><th align="left">Insert contractor name</th><td><input type="text" value="xz" name="contractorname" size="99" maxlength="99"></td></tr>'; 
	#get contractor location
	echo '<tr bgcolor="cccccc"><th align="left">Insert contractor location</th><td><input type="text" value="xy" name="contractorlocation" size="99" maxlength="99"></td></tr>'; 
	
	#contract date with datepicker (requires javascript) 
	#this date is serial # a ie f_date_a f_trigger_a
	echo '<tr><th align="left">Contract date</th>';
	echo '<td><input type="text" name="contractdate" id="f_date_a" value="'.$today .'" size="12" maxlength="10" />';
	echo '</td><td><button type="reset" id="f_trigger_a">Pick another date</button>';
	?>
	<script type="text/javascript">
    Calendar.setup({
				<?php #inputField     :    "f_date_dy", ?>
        inputField     :    "f_date_a",      // id of the input field 
        ifFormat       :    "%Y-%m-%d",       // format of the input field
        button         :    "f_trigger_a",   // trigger for the calendar (button ID)
        singleClick    :    false            // double-click mode
    });
	</script>
	<?php
	echo '</td></tr>';

	#enter contract value
	#add control to ensure numberp dot numbert
	echo '<tr bgcolor="cccccc"><th align="left">Insert contract value</th><td><input type="text" value=0.00  name="contractvalue" size="20" maxlength="18"></td></tr>'; 
	#enter contract value
	
	#select certificate type
	echo '<tr><th align="left">Select certificate type</th>';
	echo '<td><select name="certtype">';
	foreach ($certtype as $ctype)
	{
		#echo '<p>=' .$ctype; #ok
		#echo '<p>d=' .$defaults['certtype']; #ok
		echo '<option value="' .$ctype. '"';
		if ($ctype==$defaults['certtype'])
			{echo ' selected="selected">';}
		else
			{echo '>';}
		echo $ctype. '</option>';
	}
	echo '</select>';
	echo '</td></tr>'; 
	#select certificate type
	
	#get certificate number
	#echo '<tr><th align="left">Insert certificate number</th><td><input type="text" value=1 name="certnumber" size=12 maxlength=10></td></tr>'; 
	#get certificate number
	
	print "<tr><td><input type=\"submit\" name=\"button\" value=\"Submit\"></td></tr>";
	print "<tr><td><input type=\"hidden\" name=\"start\" value=\"3\"></td></tr>"; #sets start level for next pass
	
	echo '</table></form>';
	
	echo '<hr>';
	echo '<p>Restart';
	echo '<p>Home';
	echo $madehere;
}
#end start=2
#====================================================


#====================================================
#begin start=4
if ($start==4)
{
	echo '<p>Start=' .$start;
	
	#print_r($defaultp);
	
	#sanitise incoming
	$architectname=htmlentities($_POST['architectname']);
	$architectname=strtr($architectname, array("_"=>"\_","%"=>"\%","'"=>"\'")); #day 25se18
	$_SESSION['architectname'] = $architectname;
	#echo '<p>here: ' .$architectname;	
	
	$architectlocation = htmlentities($_POST['architectlocation']);
	$architectlocation=strtr($architectlocation, array("_"=>"\_","%"=>"\%","'"=>"\'"));
	$_SESSION['architectlocation']=$architectlocation;

	$contractdate = htmlentities($_POST['contractdate']);
	$contractdate=strtr($contractdate, array("_"=>"\_","%"=>"\%","'"=>"\'"));
	$_SESSION['contractdate']=$contractdate;
	
	$projectname = htmlentities($_POST['projectname']);
	$projectname=strtr($projectname, array("_"=>"\_","%"=>"\%","'"=>"\'"));	
	$_SESSION['projectname']=$projectname;

	$projectlocation = htmlentities($_POST['projectlocation']);
	$projectlocation=strtr($projectlocation, array("_"=>"\_","%"=>"\%","'"=>"\'"));
	$_SESSION['projectlocation']=$projectlocation;
	
	$employername = htmlentities($_POST['employername']); 
	$employername=strtr($employername, array("_"=>"\_","%"=>"\%","'"=>"\'"));
	$_SESSION['employername']=$employername;
	
	$employerlocation = htmlentities($_POST['employerlocation']);
	$employerlocation=strtr($employerlocation, array("_"=>"\_","%"=>"\%","'"=>"\'"));
	$_SESSION['employerlocation']=$employerlocation;
	
	$contractorname = htmlentities($_POST['contractorname']);
	$contractorname=strtr($contractorname, array("_"=>"\_","%"=>"\%","'"=>"\'"));
	$_SESSION['contractorname']=$contractorname;
	
	$contractorlocation = htmlentities($_POST['contractorlocation']);
	$contractorlocation=strtr($contractorlocation, array("_"=>"\_","%"=>"\%","'"=>"\'"));
	$_SESSION['contractorlocation']=$contractorlocation;
	
	$certtype = htmlentities($_POST['certtype']);
	$certtype=strtr($certtype, array("_"=>"\_","%"=>"\%","'"=>"\'"));
	$_SESSION['certtype']=$certtype;
	
	$contractvalue = htmlentities($_POST['contractvalue']);
	$contractvalue=strtr($contractvalue, array("_"=>"\_","%"=>"\%","'"=>"\'"));
	$_SESSION['contractvalue']=$contractvalue;
	

	#start form for financial input
	print '<form action="' .$thisfile. '" method="post">';	
	if ($errortextf>'') #from 5 above
	{
		echo '<p>Validation';
		echo '<table>';
		echo $errortextf;
		echo '</table>';
	}
	echo '<table>';	
	echo '<tr><td>';
	echo '<h3>' .$headwords. ' <font color="red">Define financial figures</font></h3>';
	echo '</td><td>';
	echo $thislogo;
	echo '</td></tr>';
	echo '</table>';
	echo '<table>';
	#Display framework
	echo '<tr><td>Framework: Contract=' .$_SESSION['prodcode']. '</td><td>Present retention statement=' .$_SESSION['showretain']. '</td><td>Pay VAT=' .$_SESSION['payvat']. '</td><td>Pay withholding tax=' .$_SESSION['paywt']. '</td><td>Damages deductible=' .$_SESSION['ddeduct']. '</td></tr>'; 
	echo '</table>';
	echo '<hr>';
#---------------------------------------------------
	
	echo '<table width=100%>';
	
	$display_stage2_data='<tr><td colspan=2></td><td colspan=2><font color="gray"><b>Certificate</b><hr>To be presented by the contractor<br> to the employer for payment<hr>ARCHITECT NAME AND ADDRESS</font><br>' .$_SESSION['architectname']. '<br>' .$_SESSION['architectlocation']. '<hr></td></tr><tr><td colspan=2 align="right" valign="top"><font color="grey">I/We certify under the terms of the contract dated</td><td colspan=2>' .$_SESSION['contractdate']. '<hr></td></tr><tr><td valign="top"><font color="grey">INTERIM or FINAL<br>' .$_SESSION['certtype']. '</td><td align="right" valign="top"><font color="grey">For the works known as<br>situated at</font></td><td>' .$_SESSION['projectname']. '<br>' .$_SESSION['projectlocation']. '<hr></td></tr><tr><td colspan=2 align="right" valign="top"><font color="grey">payment as detailed below is due from the employer<br>of</font></td><td valign="top">' .$_SESSION['employername']. '<br>' .$_SESSION['employerlocation']. '<br><hr></td></tr><tr><td valign="top"><font color="grey">CONTRACT SUM</font><br>' .number_format($_SESSION['contractvalue'],2). '</td><td align="right" valign="top"><font color="grey">to the contractor<br>of</font></td><td>' .$_SESSION['contractorname']. '<br>' .$_SESSION['contractorlocation']. '</td></tr><tr><td colspan=4><hr></td></tr>';
	$_SESSION['show2data']=$display_stage2_data;
	
#---------------------------------------------------
	#begin display stage 2 data
	#4 columns for entered data
	echo $display_stage2_data;
	#echo '<tr><td colspan=2></td><td colspan=2><font color="gray"><b>Certificate</b><hr>To be presented by the contractor<br> to the employer for payment<hr>ARCHITECT NAME AND ADDRESS</font><br>' .$_SESSION['architectname']. '<br>' .$_SESSION['architectlocation']. '<hr></td></tr>';
	#echo '<tr><td colspan=2 align="right" valign="top"><font color="grey">I/We certify under the terms of the contract dated</td><td colspan=2>' .$_SESSION['contractdate']. '<hr></td></tr>';
	#echo '<tr><td valign="top"><font color="grey">INTERIM or FINAL<br>' .$_SESSION['certtype']. '</td><td align="right" valign="top"><font color="grey">For the works known as<br>situated at</font></td><td>' .$_SESSION['projectname']. '<br>' .$_SESSION['projectlocation']. '<hr></td></tr>';
	#echo '<tr><td colspan=2 align="right" valign="top"><font color="grey">payment as detailed below is due from the employer<br>of</font></td><td valign="top">' .$_SESSION['employername']. '<br>' .$_SESSION['employerlocation']. '<br><hr></td></tr>';
	#echo '<tr><td valign="top"><font color="grey">CONTRACT SUM</font><br>' .$_SESSION['contractsum']. '</td><td align="right" valign="top"><font color="grey">to the contractor<br>of</font></td><td>' .$_SESSION['contractorname']. '<br>' .$_SESSION['contractorlocation']. '</td></tr>';
	#echo '<tr><td colspan=4><hr></td></tr>';
	#end display stage 2 data
#--------------------------------------------------	

#--------------------------------------------------
	#new fields for financials
	#start form inputs
	#echo '<tr><td colspan=2 align="right" valign="top"><font color="grey">DATE OF ISSUE</font></td><td colspan=2 valign="top"><input type="text" value="20.00" name="issuedate" size="12" maxlength="10"><br><hr></td></tr>';
	
#--------------------------------------------------
	#certificate date with datepicker (requires javascript) 
	#this date is serial # b ie f_date_b f_trigger_b
	echo '<tr><td colspan=2 align="right" valign="top"><font color="grey">DATE OF ISSUE</font></td>';
	echo '<td valign="top"><input type="text" name="issuedate" id="f_date_b" value="' .$today. '" size="12" maxlength="10" />';
	echo '<br><hr></td><td><button type="reset" id="f_trigger_b">Pick another date</button>';
	?>
	<script type="text/javascript">
    Calendar.setup({
				<?php #inputField     :    "f_date_dy", ?>
        inputField     :    "f_date_b",      // id of the input field 
        ifFormat       :    "%Y-%m-%d",       // format of the input field
        button         :    "f_trigger_b",   // trigger for the calendar (button ID)
        singleClick    :    false            // double-click mode
    });
	</script>
	<?php
	echo '<br></td></tr>';
#--------------------------------------------------
	
	#echo '<tr><td colspan=2 align="right" valign="top"><font color="grey">DATE OF VALUATION</font></td><td colspan=2><input type="text" value="20.00" name="valuedate" size="12" maxlength="10"><br><hr></td></tr>';
	
#--------------------------------------------------
	#certificate date with datepicker (requires javascript) 
	#this date is serial # b ie f_date_b f_trigger_b
	echo '<tr><td colspan=2 align="right" valign="top"><font color="grey">DATE OF VALUATION</font></td>';
	echo '<td valign="top"><input type="text" name="valuedate" id="f_date_c" value="' .$today. '" size="12" maxlength="10" />';
	echo '<br><hr></td><td><button type="reset" id="f_trigger_c">Pick another date</button>';
	?>
	<script type="text/javascript">
    Calendar.setup({
				<?php #inputField     :    "f_date_dy", ?>
        inputField     :    "f_date_c",      // id of the input field 
        ifFormat       :    "%Y-%m-%d",       // format of the input field
        button         :    "f_trigger_c",   // trigger for the calendar (button ID)
        singleClick    :    false            // double-click mode
    });
	</script>
	<?php
	echo '<br></td></tr>';
#--------------------------------------------------
	
	echo '<tr><td colspan=2 align="right" valign="top"><font color="grey">CERTIFICATE INSTALMENT NUMBER</font></td><td colspan=2><input type="text" value="2" name="certnumber" size="11" maxlength="9"><br><hr></td></tr>';
	echo '<tr><td colspan=2 align="right" valign="top"><font color="grey">CONTRACT GROSS AMOUNT</td><td><b>P</b></font><input type="text" value="5000001.76" name="congrosamt" size="22" maxlength="20"><br><hr></td><td></td></tr>';
	echo '<tr><td colspan=2 align="right" valign="top"><font color="grey">CONTRACT NETT RETENTION</td><td colspan=2><b>P</b></font><input type="text" value="25000" name="connetretain" size="20" maxlength="18"><br><hr></td><td></td></tr>';
	echo '<tr><td colspan=2 align="right" valign="top"><font color="grey">CONTRACT NETT AMOUNT</td><td colspan=2><b>P</b></font>automatic<br><hr></td><td>automatic</td></tr>';
	echo '<tr><td colspan=2 align="right" valign="top"><font color="grey">PREVIOUSLY CERTIFIED</td><td colspan=2><b>P</b></font><input type="text" value="22123" name="prevcert" size="22" maxlength="20"><br><hr></td><td></td></tr>';
	echo '<tr><td colspan=2 align="right" valign="top"><font color="grey"><b>Amount of this certificate</td><td colspan=2><font -color="grey"><b>P</b></font>automatic<br><hr></td><td>automatic</td></tr>';
	echo '<tr><td><font color="grey">IN WORDS</font></td><td colspan=3>automatic</td></tr>';
	
	if ($_SESSION['ddeduct']=="on")
	{
		echo '<tr><th align="left">Damages are potentially deductible:</th></tr>';
		echo '<tr><th align="left">Contract rate in pula and thebe per time unit</th><td><input type="text" name="drate" value="200" width=15 maxlength=13></td></tr>'; #insert rate for damages
		echo '<tr><th align="left">Contract time units on which damages are applicable</th><td><input type="text" name="dunits" value="20" width=15 maxlegnth=13></td></tr>'; #insert time units for damages
	}
	
	echo '<tr><td colspan=4><hr></td></tr>';
	echo '<tr><td><br></td></tr>'; #blank line
	echo '<tr><td><br></td></tr>'; #blank line
	echo '<tr><td></td><td align="right"><font color="grey">SIGNATURE OF ARCHITECT</font></td><td></td></tr>';
	echo '<tr><td colspan=4><hr></td></tr>';
	
	#if not minor
	#statement of retention
	if ($_SESSION['showretain']=='on')
	{
		echo '<tr><td colspan=2 align="right" valign="top"><font color="grey"><b>Statement of retention</b></font></td></tr>';
		echo '<tr><td colspan=2 align="right" valign="top"><font color="grey">Gross retention under the terms of the contract</td><td></td><td><font color="grey"><b>P</b></font>automatic<br><hr></td><td></td></tr>'; #repeat from above
		echo '<tr><td colspan=2 align="right" valign="top"><font color="grey">Previous releases to nominated sub-contractors</td><td><font color="grey">P</font><input type="text" name="prevtonomsubcons" value="2000.00" width=15 maxlength=13><br><hr></td><td></td><td></td></tr>';
		echo '<tr><td colspan=2 align="right" valign="top"><font color="grey">The contractor having confirmed receipt of satisfactory indemnity, final payments to be made to the nominated sub-contractors listed below, the retention being adjusted accordingly</td><td></td><td></td><td></td></tr>';
		echo '<tr><td colspan=2 align="right" valign="top"><font color="grey">Sub-contractor</td><td><font color="grey">P</font><input type="text" name="tonomsubcon1" value="2000.00" width=15 maxlength=13></td><td></td><td></td></tr>';
		echo '<tr><td colspan=2 align="right" valign="top"><font color="grey">Sub-contractor</td><td><font color="grey">P</font><input type="text" name="tonomsubcon2" value="2000.00" width=15 maxlength=13></td><td></td><td></td></tr>';
		echo '<tr><td colspan=2 align="right" valign="top"><font color="grey">Sub-contractor</td><td><font color="grey">P</font><input type="text" name="tonomsubcon3" value="" width=15 maxlength=13><br></td><td></td><td></td></tr>';
		echo '<tr><td colspan=2></td><td colspan=2><hr></td></tr>';
		echo '<tr><td colspan=2 align="right" valign="top"><font color="grey">Balance of retention</td><td></td><td><font color="grey"><b>P</b></font>automatic</td><td></td></tr>';
		echo '<tr><td colspan=2 align="right" valign="top"><font color="grey">Partial release of  retention</td><td></td><td><font color="grey"><b>P</b></font>automatic<br><hr></td><td></td></tr>'; #add together tonomsubcon1, 2 & 3
		echo '<tr><td colspan=2 align="right" valign="top"><font color="grey">Nett retention as shown in certificate above</td><td></td><td><font color="grey"><b>P</b></font>automatic</td><td></td></tr>';
	}
	
	#if payvat=on
	#
	if ($_SESSION['paywt']=='on')
	{
		echo '<tr><td>Pay VAT to contractor</td></tr>';
	}
	
	#if paywt=on
	#
	if ($_SESSION['paywt']=='on')
	{
		echo '<tr><td>Pay withholding tax to BURS</td></tr>';
	}
	
	print "<tr><td><input type=\"submit\" name=\"button\" value=\"Submit\"></td></tr>";
	print "<tr><td><input type=\"hidden\" name=\"start\" value=\"5\"></td></tr>"; #sets start level for next pass	
	
	#end form
	echo '</table></form>';
	
	echo '<hr>';
	echo '<p>Restart';
	echo '<p>Home';
	echo $madehere;
}
#end start=4
#===============================================


#===============================================
#begin start=6 view html form and select next action
if ($start==6)
{
	echo '<p>Start=' .$start;

#-----------------------------------------------
	#sanitise new input data from stage 4
	$issuedate=htmlentities($_POST['issuedate']);
	$issuedate=strtr($issuedate, array("_"=>"\_","%"=>"\%","'"=>"\'"));
	$_SESSION['issuedate'] = $issuedate;
	
	$valuedate=htmlentities($_POST['valuedate']);
	$valuedate=strtr($valuedate, array("_"=>"\_","%"=>"\%","'"=>"\'"));
	$_SESSION['valuedate'] = $valuedate;
	
	$certnumber=htmlentities($_POST['certnumber']);
	$certnumber=strtr($certnumber, array("_"=>"\_","%"=>"\%","'"=>"\'"));
	$_SESSION['certnumber'] = $certnumber;
	
	$congrosamt=htmlentities($_POST['congrosamt']);
	$congrosamt=strtr($congrosamt, array("_"=>"\_","%"=>"\%","'"=>"\'"));
	$_SESSION['congrosamt'] = $congrosamt;
	
	$connetretain=htmlentities($_POST['connetretain']);
	$connetretain=strtr($connetretain, array("_"=>"\_","%"=>"\%","'"=>"\'"));
	$_SESSION['connetretain'] = $connetretain;
	
	$prevcert=htmlentities($_POST['prevcert']);
	$prevcert=strtr($prevcert, array("_"=>"\_","%"=>"\%","'"=>"\'"));
	$_SESSION['prevcert'] = $prevcert;
	
	if ($_SESSION['showretain']=='on')
	{
		$prevtonomsubcons=htmlentities($_POST['prevtonomsubcons']);
		$prevtonomsubcons=strtr($prevtonomsubcons, array("_"=>"\_","%"=>"\%","'"=>"\'"));
		$_SESSION['prevtonomsubcons'] = $prevtonomsubcons;
		$tonomsubcon1=htmlentities($_POST['tonomsubcon1']);
		$tonomsubcon1=strtr($tonomsubcon1, array("_"=>"\_","%"=>"\%","'"=>"\'"));
		$_SESSION['tonomsubcon1'] = $tonomsubcon1;
		$tonomsubcon1=htmlentities($_POST['tonomsubcon1']);
		$tonomsubcon1=strtr($tonomsubcon1, array("_"=>"\_","%"=>"\%","'"=>"\'"));
		$_SESSION['tonomsubcon2'] = $tonomsubcon1;
		$tonomsubcon1=htmlentities($_POST['tonomsubcon1']);
		$tonomsubcon1=strtr($tonomsubcon1, array("_"=>"\_","%"=>"\%","'"=>"\'"));
		$_SESSION['tonomsubcon3'] = $tonomsubcon1;
	}
	
	if ($_SESSION['ddeduct']=="on")
	{
		$drate=htmlentities($_POST['drate']);
		$drate=strtr($drate, array("_"=>"\_","%"=>"\%","'"=>"\'"));
		$_SESSION['drate'] = $drate;
		
		$dunits=htmlentities($_POST['dunits']);
		$dunits=strtr($dunits, array("_"=>"\_","%"=>"\%","'"=>"\'"));
		$_SESSION['dunits'] = $dunits;
	}
#-----------------------------------------------
	
#-----------------------------------------------	
	echo '<table>';	
	echo '<tr><td>';
	echo '<h3>' .$headwords. ' <font color="red">View html form and select next action</font></h3>';
	echo '</td><td>';
	echo $thislogo;
	echo '</td></tr>';
	echo '</table>';
	echo '<table>';
	#Display framework
	echo '<tr><td>Framework: Contract=' .$_SESSION['prodcode']. '</td><td></td>Present retention statement=' .$_SESSION['showretain']. '<td>Pay VAT=' .$_SESSION['payvat']. '</td><td>Pay withholding tax=' .$_SESSION['paywt']. '</td><td>Damages deductible=' .$_SESSION['ddeduct']. '</td></tr>'; 
	echo '</table>';
	echo '<hr>';
#-----------------------------------------------

	if ($_SESSION['ddeduct']=="on")
	{
		$ddeductamt=$_SESSION['drate']*$_SESSION['dunits'];
		$ddeducttext='<tr><hr></tr><tr><td></td><td colspan=3>Damages are potentially deductible on this certificate payment in the nett sum of P' .number_format($ddeductamt,2). '</td></tr>';
	}
	
	if ($_SESSION['showretain']=='on')
	{
		#prepare calculations for display below
	}
	
	$connetamt=$_SESSION['congrosamt']-$_SESSION['connetretain'];
	$_SESSION['connetamt']=$connetamt;
	$certamount=$connetamt-$_SESSION['prevcert'];
	$_SESSION['certamount']=$certamount;
	
	#set up for number in words
	require_once('Numbers/Words.php');
	$nw = new Numbers_Words();
	#integer pula
	$pulas=intval($_SESSION['certamount']);
	#thebe
	$thebe=number_format($_SESSION['certamount']-$pulas,2);
	#echo '<p>thebe=' .$thebe; #ok
	if ($thebe==0)
		{$thebewords=' only.';}
	else
	{
		$thebe=($thebe*100);
		$thebewords=$nw->toWords($thebe);
		$thebewords=$thebewords . ' thebe.';
	}
	$num2words= $nw->toWords($pulas);
	$amtwords=ucfirst($num2words). ' Pula ' .$thebewords;
	$_SESSION['amtwords']=$amtwords;
	
	echo '<table width=100%>';
	
	$display_stage4_data='<tr><td colspan=2 align="right" valign="top"><font color="grey">DATE OF ISSUE</font></td><td valign="top">' .$_SESSION['issuedate']. '<hr></td</tr>
	<tr><td colspan=2 align="right" valign="top"><font color="grey">DATE OF VALUATION</font></td><td valign="top">' .$_SESSION['valuedate']. '<hr></td></tr>
	<tr><td colspan=2 align="right" valign="top"><font color="grey">CERTIFICATE INSTALMENT NUMBER</font></td><td valign="top">' .$_SESSION['certnumber']. '<hr></td><td>COPY</td></tr>
	<tr><td colspan=2 align="right" valign="top"><font color="grey">CONTRACT GROSS AMOUNT</font></td><td  align="right" valign="top">P' .number_format($_SESSION['congrosamt'],2). '<hr></td></tr>
	<tr><td colspan=2 align="right" valign="top"><font color="grey">CONTRACT NETT RETENTION</font></td><td  align="right" valign="top">P' .number_format($_SESSION['connetretain'],2). '<hr></td></tr>
	<tr><td colspan=2 align="right" valign="top"><font color="grey">CONTRACT NET AMOUNT</font></td><td align="right" valign="top">P' .number_format($connetamt,2). '<hr></td></tr>
	<tr><td colspan=2 align="right" valign="top"><font color="grey">CONTRACT PREVIOUSLY CERTIFIED</font></td><td align="right" valign="top">P' .number_format($_SESSION['prevcert'],2). '<hr></td></tr>
	<tr><td colspan=2 align="right"><font color="grey"><b>Amount of this certificate</b></font></td><td align="right" valign="top"></font>P' .number_format($certamount,2). '</td></tr>
	<tr><td align="right"><font color="grey">IN WORDS</font></td><td>' .ucfirst($num2words). ' Pula ' .$thebewords. '</td></tr>
	<tr><td colspan=4>' .$ddeducttext. '<hr></td></tr>
	<tr><td colspan=2 align="right">SIGNATURE OF ARCHITECT<br><br></td></tr>
	<tr><td colspan=4><hr></td></tr>
	';
	
#-----------------------------------------------	
	$display_retentionstate='<tr><td colspan=2 align="right" valign="top"><font color="grey"><b>Statement of retention</b></font></td></tr><tr><td colspan=2 align="right" valign="top"><font color="grey">Gross retention under the terms of the contract</td><td></td><td><font color="grey"><b>P</b></font>' .number_format($_SESSION['connetretain'],2). '<br><hr></td><td></td></tr><tr><td colspan=2 align="right" valign="top"><font color="grey">Previous releases to nominated sub-contractors</td><td><font color="grey">P</font>' .number_format($_SESSION['prevtonomsubcons'],2). '<br><hr></td><td></td><td></td></tr><tr><td colspan=2 align="right" valign="top"><font color="grey">The contractor having confirmed receipt of satisfactory indemnity, final payments to be made to the nominated sub-contractors listed below, the retention being adjusted accordingly</td><td></td><td></td><td></td></tr><tr><td colspan=2 align="right" valign="top"><font color="grey">Sub-contractor</td><td><font color="grey">P</font>' .number_format($_SESSION['tonomsubcon1'],2). '</td><td></td><td></td></tr><tr><td colspan=2 align="right" valign="top"><font color="grey">Sub-contractor</td><td><font color="grey">P</font>' .number_format($_SESSION['tonomsubcon2'],2).  '</td><td></td><td></td></tr><tr><td colspan=2 align="right" valign="top"><font color="grey">Sub-contractor</td><td><font color="grey">P</font>' .number_format($_SESSION['tonomsubcon3'],2). '<br></td><td></td><td></td></tr><tr><td colspan=2></td><td colspan=2><hr></td></tr><tr><td colspan=2 align="right" valign="top"><font color="grey">Balance of retention</td><td></td><td><font color="grey"><b>P</b></font>' .number_format($_SESSION[''],2). '</td><td></td></tr><tr><td colspan=2 align="right" valign="top"><font color="grey">Partial release of  retention</td><td></td><td><font color="grey"><b>P</b></font>' .number_format($_SESSION[''],2). '<br><hr></td><td></td></tr><tr><td colspan=2 align="right" valign="top"><font color="grey">Nett retention as shown in certificate above</td><td></td><td><font color="grey"><b>P</b></font>' .number_format($_SESSION['connetretain'],2). '</td><td></td></tr>';
#-----------------------------------------------
	
	$vatamount=$certamount*0.12;
	$_SESSION['vatamount']=$vatamount;
	$amountwithvat=$certamount*1.12;
	$_SESSION['amountwithvat']=$amountwithvat;
	$display_payvat='<tr><td align="right" colspan=2><font color="grey">ADD 12% VAT</font></td><td align="right">' .number_format($vatamount,2). '</td><td></td></tr><tr><br></tr><tr><td align="right" valign="top" colspan=2><font color="grey">AMOUNT INCLUDING VAT</font></td><td align="right">' .number_format($amountwithvat,2). '</td><td></td></tr>';
	
	$wtamount=$certamount*.03;
	$_SESSION['wtamount']=$wtamount;
	$display_paywt='<tr><td colspan=2><font color="grey"><b>Pay 3% withholding tax on certificate amount to BURS & supply ITW form to contractor</font></td><td align="right"><b>' .number_format($wtamount,2). '</b></td><td></td></tr>';
	
	#remainder calculations
	if ($_SESSION['payvat']== 'on' && $_SESSION['paywt']== 'on')
	{			
		$payrem = ($_SESSION['amountwithvat']-$_SESSION['wtamount']);
		$_SESSION['payrem']=$payrem;
	}
	elseif ($_SESSION['payvat']== 'off' && $_SESSION['paywt']== 'on')
	{
		$payrem = ($_SESSION['certamount']-$_SESSION['wtamount']);
		$_SESSION['payrem']=$payrem;
	}
	


#-----------------------------------------------
	#display stage 2 data
	echo $_SESSION['show2data'];
	
#-----------------------------------------------

#-----------------------------------------------
	#display stage 4 data
	echo $display_stage4_data;
	
	if ($_SESSION['showretain']=='on')
	{
		echo $display_retentionstate;
	}
	
	
	if (($_SESSION['payvat']=='on' || $_SESSION['paywt']=='on') || ($_SESSION['payvat']=='on' && $_SESSION['paywt']=='on'))
	{
		echo '<tr><td align="right" colspan=2><font color="grey"><b>PAYSHEET INCLUDING TAXES</b></font></td><td align="right"></td><td></td></tr>';
		echo '<tr><td align="right" colspan=2><font color="grey">AMOUNT OF THIS CERTIFICATE</font></td><td align="right">' .number_format($certamount,2). '</td><td></td></tr>';
	}
	
	if ($_SESSION['payvat']=='on')
		{echo $display_payvat;}
		
	if (($_SESSION['payvat']=='on' || $_SESSION['paywt']=='on') || ($_SESSION['payvat']=='on' && $_SESSION['paywt']=='on'))
		{echo '<tr><td colspan=4><hr></td></tr>';}
		
	if ($_SESSION['paywt']=='on')
		{echo $display_paywt;}
		
	if ($_SESSION['payvat']=='on' &&  $_SESSION['paywt']!='on')
		{echo '<tr><td align="right" colspan=2><font color="grey"><b>Pay amount including VAT direct to contractor</font></td><td align="right"><b>' .number_format($amountwithvat,2). '</b></td><td></td></tr>';} #26se18
	elseif ($_SESSION['payvat']!='on' &&  $_SESSION['paywt']=='on')
	{
		$paythis=$certamount-$wtamount;
		echo '<tr><td colspan=2><font color="grey"><b>Pay remainder direct to contractor</font></td><td align="right"><b>' .number_format($paythis,2). '</b></td><td></td></tr>';
	}
	elseif ($_SESSION['payvat']=='on' || $_SESSION['paywt']=='on')
	{
		$paythis=$amountwithvat-$wtamount;
		echo '<tr><td colspan=2><font color="grey"><b>Pay remainder direct to contractor</b></font></td><td align="right"><b>' .number_format($paythis,2). '</b></td><td></td></tr>';
	}
	
#-----------------------------------------------

	echo '</table>';
	echo '<hr>';
	
#-----------------------------------------------
	#allow selection for next action
	echo '<h4>Options for action:</h4>';
	
	echo '<h4>Select proceed to .pdf options if wanted, then click Submit</h4>';
	echo '<form action="' .$thisfile. '" method="post">';
	echo '<table width=100%>';
	echo '<tr><td>Clicking Submit opens the .pdf view for viewing, printing and/or saving and defining copies wanted</td></tr>';
	echo '<tr><td><input type="submit" name="button" value="Submit"></td></tr>';
	echo '<tr><td><input type="hidden" name="start" value="7"></td></tr>';
	echo '</table></form>';
	
	#give option to download .csv file from /tmp
	#save file to /tmp with current datetime as name & use same in calling download of that file
	echo '<p>Click on <a href="#/tmp/that_datetime_filename.csv">this link</a> to download a .csv file to your machine. This can be imported into a spreadsheet on your system.'; 
	
#-----------------------------------------------


	echo '<hr>';
	echo '<p>Restart';
	echo '<p>Home';	
	echo $madehere;
}
#end start=6 view html form and select next action
#===============================================


#===============================================
	#begin start=7
	if ($start==7)
	{
		echo '<p>Start=' .$start;
		if ($_SERVER[HTTPS]=='on')
			{$thisfpdf = $thisserver. '/php/bidp/certpay_fpdf.php';}
		else
			{$thisfpdf = $thisserver. '/php/bidp/certpay_fpdf.php';}
		#link title: title="Go to chapter two"
		#link in new window: target="_blank"
		
#-----------------------------------------------	
		echo '<table>';	
		echo '<tr><td>';
		echo '<h3>' .$headwords. ' <font color="red">Select .pdf options</font></h3>';
		echo '</td><td>';
		echo $thislogo;
		echo '</td></tr>';
		echo '</table>';
		echo '<table>';
		#Display framework
		echo '<tr><td>Framework: Contract=' .$_SESSION['prodcode']. '</td><td>Present retention statement=' .$_SESSION['showretain']. '</td><td>Pay VAT=' .$_SESSION['payvat']. '</td><td>Pay withholding tax=' .$_SESSION['paywt']. '</td><td>Damages deductible=' .$_SESSION['ddeduct']. '</td></tr>'; 
		echo '</table>';
		echo '<hr>';
#-----------------------------------------------
		
		echo '<p>Define copies wanted, then click Submit to proceed to .fpdf file view for viewing, printing and/or saving';
		echo '<form action="' .$thisfpdf. '" method="post">';
		echo '<table>';
		echo '<tr><th align="left">Original (recommended paper colour white)</th><td><input type="checkbox" name="oprint" value="y" checked="checked"></td></tr>'; #checkbox, original
		echo '<tr><th align="left">QS copy (recommended paper colour blue)</th><td><input type="checkbox" name="qprint" value="y" checked="checked"></td></tr>'; #checkbox, qs copy
		echo '<tr><th align="left">Architect copy (recommended paper colour pink)</th><td><input type="checkbox" name="aprint" value="y" checked="checked"></td></tr>'; #checkbox, architect copy
		echo '<tr><th align="left">Contractor copy (recommended paper colour yellow)</th><td><input type="checkbox" name="cprint" value="y" checked="checked"></td></tr>'; #checkbox, contractor copy
		echo '<tr><th align="left">Employer copy (recommended paper colour green)</th><td><input type="checkbox" name="eprint" value="y" checked="checked"></td></tr>'; #checkbox, employer copy
		#echo '<tr><td><input type="hidden" name="start" value="9"></td></tr>';
		echo '<tr><td><input type="submit" name="button" value="Submit"></td></tr>';
		echo '</table></form>';
		echo '<hr>';
		echo $madehere;
	}
	#end start=7
#===============================================


#===============================================
#begin start=8
#produce pdf
if ($start==8)
{
#print_r($_POST); #ok
#	print $jobref.' 
#nb pdml=no png graphics
	if ($_POST['reference']>'')
		{$reference=$_POST['reference'];}
	elseif ($_GET['reference']>'')
		{$reference=$_GET['reference'];}
	$cert_no=$_POST['cert_no'];
	#echo $cert_no; #ok
	#$contracdat=$link->getOne("SELECT contracdat FROM $table4 WHERE job_nmbr='$reference'");
	$cdate=mktime(0,0,0,substr($_SESSION['contractdate'],5,2),substr($_SESSION['contractdate'],8,2),substr($_SESSION['contractdate'],0,4));


#----------------------------------------------
	include 'pdml.php';
	
	?>
	<pdml>
	<body>
	<font face=arial size=11>
	<div top=55mm left=95mm height=60mm>
	</div>
	<?php
	#locate logo top left
	echo '<img src="/x/bidp/logo/logoplain.jpg" top=12mm left=12mm>'; #local logo
	echo '<cell left=12mm top=12mm width=100mm></cell><br height=120%>';
	echo '<cell left=96mm top=12mm width=100mm><font size=5mm><b>Certificate</b></font></cell><br>';
	echo '<line from="93,20" to="198,20">';
	echo '<cell left=12mm top=40mm width=100mm></cell><br height=120%>';
	echo '<cell left=96mm top=40mm width=100mm>' .$_SESSION['architectname']. '</cell><br>'; #works
	echo '<cell left=96mm top=45mm width=100mm>' .$_SESSION['architectlocation']. '</cell><br>';
	echo '<cell left=95mm top=60mm width=30mm>'. date('jS', $cdate) .'</cell><br>';
	echo '<cell left=135mm top=60mm width=30mm>'. date('F', $cdate) .'</cell><br>';
	echo '<cell left=165mm top=60mm width=30mm>XX '. date('Y', $cdate) .'</cell><br>';
	echo '<cell left=96mm top=71mm width=100mm>' .$_SESSION['projectname']. '</cell><br>';
	echo '<cell left=96mm top=78mm width=100mm>' . $_SESSION['projectlocation']. '</cell><br>';
	if ($getcert['cert_type']=='i')
		{$cert_type='Interim';}
	else
		{$cert_type='Final';}
	echo '<cell left=15mm top=79mm width=30mm>'. $cert_type .'</cell><br>';
	$empadd=$empdata['adline1'] .', '. $empdata['adline2'];
	if ($empdata['adline3']!=$empdata['adline2'] && $empdata['adline3']>'')
		{$empadd=$empadd. ', '. $empdata['adline3'];}
	echo '<cell left=96mm top=90mm width=100mm>' .$_SESSION['employername']. '</cell><br>';
	echo '<cell left=96mm top=95mm width=100mm>' .$_SESSION['employerlocation']. '</cell><br>';
	echo '<cell left=96mm top=106mm width=100mm>'. $_SESSION['contractorname']. '</cell><br>';
	echo '<cell left=15mm top=110mm width=30mm>'. number_format($contracsum,2) .'</cell><br>';
	echo '<cell left=96mm top=113mm width=100mm>'. $_SESSION['contractorlocation']. '</cell><br>';	
	echo '<cell left=105mm top=122mm width=100mm>'. $getcert['cert_date'] .'</cell><br>';
	echo '<cell left=105mm top=128mm width=100mm>' .$_SESSION['valuedate']. '</cell><br>';
	echo '<cell left=105mm top=134mm width=40mm>' .$_SESSION['certnumber']. '</cell><br>';
	echo '<cell left=105mm top=141mm width=40mm align=right>' .number_format($_SESSION['congrosamt'],2). '</cell><br>';
	echo '<cell left=105mm top=148mm width=40mm align=right>'. number_format($getcert['retention'],2) .'</cell><br>';
	$nett=$getcert['grossamt']-$getcert['retention'];
	echo '<cell left=105mm top=155mm width=40mm align=right>'. number_format($nett,2) .'</cell><br>';
	echo '<cell left=105mm top=162mm width=40mm align=right>'. number_format($getcert['prevcert'],2) .'</cell><br>';
	echo '<b>';
	$payable=$nett-$getcert['prevcert'];
	echo '<cell left=105mm top=171mm width=40mm align=right>'. number_format($payable,2) .'</cell>';
	echo '<font face=arial size=11></b>';
	
	#numbers to words start
	$nums=array('1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '30', '40', '50', '60', '70', '80', '90');
	$numwords=array(	'1' => 'one',
										'2' => 'two',
										'3' => 'three',
										'4' => 'four',
										'5' => 'five',
										'6' => 'six',
										'7' => 'seven',
										'8' => 'eight',
										'9' => 'nine',
										'10' => 'ten',
										'11' => 'eleven',
										'12' => 'twelve',
										'13' => 'thirteen',
										'14' => 'fourteen',
										'15' => 'fifteen',
										'16' => 'sixteen',
										'17' => 'seventeen',
										'18' => 'eighteen',
										'19' => 'nineteen',
										'20' => 'twenty',
										'30' => 'thirty',
										'40' => 'forty',
										'50' => 'fifty',
										'60' => 'sixty',
										'70' => 'seventy',
										'80' => 'eighty',
										'90' => 'ninety');
	#decimals
	$theben=substr($payable, (strpos($payable, '.')+1), 2);
	#array_search($numwords, $theben); #ok
	#substr_count($payable, '.'); #ok
	#strlen($payable); #ok
	if ($theben<1)
	{
	 	$thebew='zero';
	}
	elseif (array_search($theben, $nums)) #error warning switched off 19ap07
		{$thebew=$numwords["$theben"];}
	else
	{ 
		$thebetens=substr($theben,0,1) .'0';
		$thebeunits=substr($theben,1,1);
		$ttenwords=$numwords[$thebetens];
		$tunitwords=$numwords[$thebeunits];
		$thebew=$ttenwords .' '. $tunitwords;
	}
	#pulas
	$pulastrlen=strlen($payable)-3;
	#$test1=$pulastrlen;
	if ($pulastrlen==0)
		{$pulaw='zero';}
	elseif ($pulastrlen>0)
	{
		$phtens=substr($payable,-5,2);
		if ($pulastrlen>2) 
			{$phhundreds=substr($payable,-6,1);}
		if ($pulastrlen>3)
			{$pttens=substr($payable,-8,2);}
		if ($pulastrlen>5)
			{$pthundreds=substr($payable,-9,1);}
		if ($pulastrlen>6)
			{$pmtens=substr($payable,-11,2);}
		if ($pulastrlen>8)
			{$pmhundreds=substr($payable,-12,1);}
	}
	#phundreds
	if (array_search($phtens, $nums))
		{$pulaw=$numwords["$phtens"];} #poss add leading space?
	else
	{ 
		$phttens=substr($phtens,0,1) .'0';
		$phtunits=substr($phtens,1,1);
		$phtenwords=$numwords[$phttens];
		$phunitwords=$numwords[$phtunits];
		$pulaw=' '. $phtenwords .' '. $phunitwords;
	}
	if ($phhundreds>0)
		{$pulaw=' '. $numwords["$phhundreds"] .' hundred and '. $pulaw;}
	#pthousands
	if ($pulastrlen>3)
	{
	  if (array_search($pttens, $nums))
			{$pulaw=$numwords["$pttens"] .' '. $pulaw;}
		else
		{ 
			$ptttens=substr($pttens,0,1) .'0';
			$pttunits=substr($pttens,1,1);
			$pttenwords=$numwords[$ptttens];
			$ptunitwords=$numwords[$pttunits];
			$pulaw=' '. $pttenwords .' '. $ptunitwords .' thousand,'. $pulaw;
		}
		if ($pthundreds>0)
			{$pulaw= $numwords["$pthundreds"] .' hundred and'. $pulaw;}
	}
	#pmillions
	if ($pulastrlen>6)
	{
		if (array_search($pmtens, $nums))
			{$pulaw=$numwords["$pmtens"] .' '. $pulaw;}
		else
		{ 
			$pmttens=substr($pmtens,0,1) .'0';
			$pmtunits=substr($pmtens,1,1);
			$pmtenwords=$numwords[$pmttens];
			$pmunitwords=$numwords[$pmtunits];
			$pulaw=' '. $pmtenwords .' '. $pmunitwords .' million,'. $pulaw;
		}
		if ($pmhundreds>0)
			{$pulaw= $numwords["$pmhundreds"] .' hundred and'. $pulaw;}
	}
	#negative amount
	if ($payable<0)
		{$pulaw='minus '.$pulaw;}
	#numbers to words end
	$thebeamount=$thebew .' thebe';
	$pulaamount=$pulaw .' pula';
	$wordamount=$pulaamount .' '. $thebeamount;
	echo '<font face=arial size=9>';
	echo '<cell left=45mm top=176mm width=150mm align=right>'. ucfirst(trim($wordamount)) .'</cell><br>';
	echo '<font face=arial size=11>';
	?>	
	</pdml>
	<?php
} #end start=8
#===============================================


#===============================================
if ($start==9) #start of pdf from certificate
{

#-------------------------------------------------
	if ($yes=='no')
	{#begin yes
		function Footer()
		{
			global $key1, $key2, $key3, $f, $id, $bag, $connect, $link, $today, $firm, $thisserver, $thisfile;
			if ($bag=='fax' || $bag=='let')
			{
				$this->SetTextColor(128,128,128);
				$this->SetX(0);
				$this->SetY(-20); #adjust from bottom
				$this->SetFont ('Arial', '', 6.8);
			#$this->Cell (0,10,"page $section-".$this->PageNo().'/{nb}',0,0,'C');
				if ($firm=='y')
				{
					$this->Cell(165,5,"Director: David Alexander Young B.Arch(Natal) MBIDP  (British). Associated with Kevin Macgarry, Architect, Durban. VAT registration # C067 1380 1112",0,1,'c');
					#$this->Cell(42,0,'',0,0);
					#$this->Cell(60,0,"Architect, Durban. VAT registration # C067 1380 1112",0,1,'c');
				}
				elseif ($firm=='d')
				{
					$this->Cell(155,5,"Sole partner: David Alexander Young B.Arch(Natal) MBIDP (British). VAT registration # C067 1380 1112",0,1,'C');
				}
				elseif ($firm=='i')
				{
				 	$this->Cell(155,5,"Director: David Alexander Young B.Arch(Natal) MBIDP  (British)",0,0,'C');
				}
				$this->SetTextColor(0,0,0);
			}
		}#end footer
	}#end yes
#-------------------------------------------------


	require 'fpdf.php';
	$pdf = new FPDF();
	$pdf->AddPage();
	// Insert a logo in the top-left corner at 300 dpi
	#$pdf->Image('../../pix/yalogosm.jpg',10,10,-300); #displays f not k
	// Insert a dynamic image from a URL
	$pdf->Image('../../pix/logoplain.jpg',10,10,-300); #displays f not k
	// Insert a dynamic image from a URL
	$pdf->Image('http://192.168.20.12/pix/logoalpha.png',60,30,90,0,'PNG'); #displays f not k
	$pdf->SetFont('Arial','B',16);
	$pdf->Cell(40,10,'Hello World!'); #displays f not k
	$pdf->Output();
}# end start=9 function print pdf to screen
#===============================================


#===============================================
	#begin ros ezpdf start=10
	if ($start==10)
	{
		error_reporting(E_ALL);
		set_include_path('../../pdfphp01248/src/'.PATH_SEPARATOR.get_include_path());
		date_default_timezone_set('UTC');

		include 'Cezpdf.php';

		class Creport extends Cezpdf
		{
			public function __construct($p, $o)
			{
				parent::__construct($p, $o, 'color', array(0.8, 0.8, 0.8));
			}
		}	
		$pdf = new Creport('a4', 'portrait');

		$pdf->ezSetMargins(20, 20, 20, 20);

		$mainFont = 'Times-Roman';
		// select a font
		$pdf->selectFont($mainFont);
		$size = 12;

		$height = $pdf->getFontHeight($size);
		// modified to use the local file if it can
		$pdf->openHere('Fit');

		$pdf->ezText("ROS PDF Image Example\n", 18);
		
		$pdf->ezText('PNG true color plus alpha channel #1');
		$pdf->ezImage('../../pix/yalogosm.jpg', 0, 0, 'none', 'right'); #displays f & k
		#$pdf->ezImage('../../pix/logoplain.jpg', 0, 0, 'none', 'right'); #inop f
		#$pdf->ezImage('../../pix/logoalpha.png', 0, 0, 'none', 'right'); #inop f
		#$pdf->ezImage('../../pix/test_alpha.png', 0, 0, 'none', 'right');	#inop	f
		
		
		if (isset($_GET['d']) && $_GET['d']) 
			{echo $pdf->ezOutput(true);} 
		else 
		{$pdf->ezStream(array('compress' => 0));}
		
		//error_log($pdf->messages);		
	}
	#end ros ezpdf start=10
#===============================================


echo '</body></html>';
#end
?>