<?PHP
#This makes an affidavit pdf stored in the variable $pdf.
#
#

$fi=mysql_getRow("select * from foundational_info where enrollee_id=$enrollee_id");
$query="select * from address where address_id=".$enrollee["address_id"];
$residence=mysql_getRow($query);
if (!$residence) echo reporterror("finding the residence for $enrollee_id",$query);

$query="select * from address where address_id=".$officer["address_id"];
$office=mysql_getRow($query);
if (!$office) echo reporterror("finding the office for $enrollee_id",$query);

##  We assume that the enrollment happens at the officer's address
$officername=makeName($officer);
$query="select city_name from city where city_id=".$office["city_id"];
$venuecity=mysql_getOne($query);
if (!$venuecity) echo reporterror("getting the officer's city",$query);
$query="select state_name from state where state_id=".$office["state_id"];
$venuestate=mysql_getOne($query);
if (!$venuestate) echo reporterror("getting the officer's state",$query);
$query="select county_name from county where county_id=".$office["county_id"];
$venuecounty=mysql_getOne($query);
if (!$venuecounty) echo reporterror("getting the officer's county",$query);
$query="select country_name from country where country_id=".$office["country_id"];
$venuecountry=mysql_getOne($query);
if (!$venuecountry) echo reporterror("getting the officer's country",$query);

$name=makeName($enrollee);
$address=makeAddress($residence,0);
$query="select city_name from city where city_id=".$residence["city_id"];
$addresscity=mysql_getOne($query);
$query="select state_name from state where state_id=".$residence["state_id"];
$addressstate=mysql_getOne($query);
$query="select country_name from country where country_id=".$address["country_id"];
$addresscountry=mysql_getOne($query);
if (!$addresscountry) echo reporterror("getting the enrollee's country",$query);
if (is_null($addresscity)) $addresscity=$venuecity;
if (is_null($addressstate)) $addressstate=$venuestate;

$birthname=$fi["name_born"];
$mom=$fi["mothers_m_name"];
$dad=$fi["fathers_name"];
$birthdate=$fi["date_of_birth"];
$birthcity=mysql_getRow("select * from city where city_id=".$fi["city_born"]);
$birthplace=$birthcity["city_name"];
if (!is_null($birthcity["state_id"]) && $birthcity["state_id"] > 0) $birthplace.=", ".mysql_getOne("select state_name from state where state_id=".$birthcity["state_id"]);
if (!is_null($birthcity["country_code"]) && $birthcity["country_code"] > 0) $birthplace.=", ".mysql_getOne("select country_name from country where country_id=".$birthcity["country_code"]);

$pdfname=UPLOADDIRECTORY."/$enrollee_id/affidavit.pdf";

include_once('includes/fpdf.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Times','B',16);
$pdf->Cell(0,10,'AFFIDAVIT OF IDENTITY',0,0,'C');
$pdf->Ln(10);
$pdf->SetFont('Times','',10);
$pdf->Cell(0,10,'State of '.$venuestate);
$pdf->Ln(5);
$pdf->Cell(0,10,'County of '.$venuecounty);
$pdf->Ln(10);
$pdf->Cell(0,10,'I, '.$name.', being duly sworn, and under penalties of perjury, state that:');
$pdf->Ln(5);
$pdf->Cell(0,10,'I reside at '.$address.', '.$addresscity.', '.$addressstate.'.');
$pdf->Ln(5);
$pdf->Cell(0,10,'I was born on the date '.$birthdate.' at '.$birthplace.'.');
$pdf->Ln(5);
if($birthaddress)
{
	$pdf->Ln(5);
	$pdf->Cell(0,10,'My address at the time of my birth was '.$birthaddress.'.');
}
$pdf->Cell(0,10,'The names of my parents are '.$mom.' and '.$dad.'.');
$pdf->Ln(5);
$pdf->Cell(0,10,'The name given to me at my birth was '.$birthname.'.');
$credquery="select c.*,t.type_name from credential c,credential_type t where c.credential_type_code=t.credential_type_code and enrollee_id=$enrollee_id order by issue_date";
$res=mysql_query($credquery);
if (!$res) echo reporterror("getting credentials",$query);
else
{
  $row=mysql_fetch_array($res);
  if ($row["credential_type_code"] == "bc")
  {
    $pdf->Ln(5);
    $pdf->Cell(0,10,"This is shown on my birth certificate presented at this time.");
    $row=mysql_fetch_array($res);
  }
  $nids=0;
  do
  {
    if ($row["credential_type_code"] == "bc") continue;
    if (is_null($row["credential_type_code"])) continue;
    if (is_null($row["prior_name"])){$nids++;continue;}
    $pdf->Ln(5);
    $pdf->Cell(0,10,"My name was changed from ".$row["prior_name"]." to ".$row["name"]. " by ".$row["type_name"]." on ".prettyDate($row["issue_date"]).".");
  }while ($row=mysql_fetch_array($res));
}
if ($nids > 0)
{
  $pdf->Ln(5);
  $pdf->Cell(0,10,"As further proof of my identity, I also present ".(($nids>0)?("these $nids credentials:"):("this credential:")));
  $res=mysql_query($credquery);
  if (!$res) echo reporterror("redoing the credentials",$query);
  while ($row=mysql_fetch_array($res))
  {
    if ($row["credential_type_code"] == "bc") continue;
    if (is_null($row["credential_type_code"])) continue;
    if (is_null($row["prior_name"]))
    {
      $pdf->Ln(5);
      $pdf->Cell(10);
      $pdf->Cell(0,10,$row["type_name"]." issued by ".$row["issuing_agency"]." on ".prettyDate($row["issue_date"]));
    }
  }
}
if(!is_null($nickname))
{
	$pdf->Ln(5);
	$pdf->Cell(0,10,'I am also known by others as '.$nickname.'.');
}
if(!is_null($social))
{
	$pdf->Ln(5);
	$pdf->Cell(0,10,'My social security number is '.$social.'.');
}
if(!is_null($license))
{
	$pdf->Ln(5);
	$pdf->Cell(0,10,'My driver\'s license number is '.$license.' issued by the state of '.$licensestate.'.');
}
if(!is_null($military))
{
	$pdf->Ln(5);
	$pdf->Cell(0,10,'My military ID number is '.$military.'.');
}
if(!is_null($school))
{
	$pdf->Ln(5);
	$pdf->Cell(0,10,'My school ID number is '.$school.'.');
}
if(!is_null($passport))
{
	$pdf->Ln(5);
	$pdf->Cell(0,10,'My passport number is '.$passport.'.');
}

$pdf->Ln(5);
$pdf->Cell(0,10,'The biometric information which is obtained today by '.makeName($officer).' and which further identifies me is');
$pdf->Ln(5);
$pdf->Cell(0,10,'to be made part of this record.');

$pdf->Ln(5);
$pdf->Cell(0,10,'I am responsible for determining under what circumstances the information in this record is to be disclosed, and to');
$pdf->Ln(5);
$pdf->Cell(0,10,'whom it will be disclosed, as specified in my Personal Non-Disclosure Agreement.');
$pdf->Ln(5);
$pdf->Cell(0,10,'I swear that the identity information given above is true, under penalties of perjury.');
$pdf->Ln(15);
$pdf->Cell(100,10,'_______________________________________');
$pdf->Cell(0,10,'______________');
$pdf->Ln(5);
$pdf->Cell(100,10,'Signature');
$pdf->Cell(0,10,'Date');
$pdf->Ln(10);

//JURAT

$pdf->SetFont('Times','B',16);
$pdf->Cell(0,10,'Jurat',0,0,'C');
	//	$pdf->Cell(0,10,'Notarial Certificate',0,0,'C');
$pdf->Ln(15);
$pdf->SetFont('Times','',10);
$pdf->Cell(0,10,'State of '.$venuestate);
$pdf->Ln(5);
$pdf->Cell(0,10,'County of '.$venuecounty);
$pdf->Ln(10);
$pdf->Cell(0,10,'Sworn to (or affirmed) before me, a notary public in the state of '.$venuestate.',');
$pdf->Ln(5);
$pdf->Cell(0,10,'this ________ day of _____________, 20___ by '.$name.',');
$pdf->Ln(5);
$pdf->Cell(0,10,'who deposes and says that this affidavit is made for the purpose of establishing his/her');
$pdf->Ln(5);
$pdf->Cell(0,10,'identity for the purpose to be determined by himself/herself.');
$pdf->Ln(10);
$pdf->Cell(0,10,'Signer ____is known to me (or) ____produced identification: _________________________');
$pdf->Ln(15);
$pdf->Cell(100,10,'_________________________');
$pdf->Cell(0,10,'(Notary Seal)');
$pdf->Ln(5);
$pdf->Cell(0,10,'Signature of Notary');
$pdf->Ln(15);
##$pdf->Cell(0,10,'_________________________');
$pdf->Cell(0,10,$officername);
$pdf->Ln(5);
$pdf->Cell(0,10,'Printed Name of Notary');
$pdf->Ln(10);
$pdf->Cell(0,10,'NOTARY PUBLIC, State of '.$venuestate);
$pdf->Ln(15);
$pdf->Cell(0,10,'Copyright 2001-2009 '.OURLEGALNAME.' All rights reserved.',0,0,'C');
$pdf->Output($pdfname);

$pdfmade = true;
?>