<?php
session_start();

$newby=12;
require("includes/.htconfig.php");
require("includes/dbcon.php");
if (!$link) echo reporterror("connecting to the database");

##I present a file as an attachment to be added to an email
function addMIMEpart($filename,$type,$boundary,$encode,$dispose="",$otherheaders="",$name="")
{
  if (strlen($filename) > 0)
  {
    if ($name == "") $name=basename($filename);
    $file=fopen($filename,"rb");
    $data=fread($file,filesize($filename));
    fclose($file);
  }
  else
    $data="";
  switch ($type)
  {
    case "pdf":
    case "PDF":
      $ctype="application/octet-stream; name=\"$name\"";
      break;
    case "html":
    case "HTML":
      $ctype="text/html; charset=\"us-ascii\"";
      break;
    case "jpg":
    case "jpeg":
      $ctype="image/jpeg; name=\"$name\"";
      break;
    case "plain":
    default:
      $ctype="text/plain; charset=\"us-ascii\"";
      break;
  }
  switch($encode)
  {
    case "base64":
    case "BASE64":
    case "64":
      $data=chunk_split(base64_encode($data));
      $encoding="base64";
      break;
    case "7bit":
    default:
      $encoding="7bit";
      break;
  }
  switch($dispose)
  {
    case "attachment":
      $disp="attachment; name=$name";
      break;
    case "body":
    case "BODY":
    case "INLINE":
    case "in-line":
    default:
      $disp="in-line";
  }
      

  $att="--$boundary\n";
  $att.="Content-Type: $ctype\n";
  $att.="Content-Transfer-Encoding: $encoding\n";
  $att.="Content-Disposition: $disp\n";
  if (strlen($otherheaders) > 0) $att.=$otherheaders."\n";
  if (strlen($data) > 0) $att.="\n$data\n";
  return $att;
}

function mimeHeaders($boundary)
{
  $head="MIME-Version: 1.0\n";
  $head.="Content-Type: multipart/mixed; boundary=\"$boundary\"\n";
  return $head;
}

$enrollee=mysql_getRow("select * from enrollee where enrollee_id=$enrollee_id");
$officer_id=mysql_getOne("select eo_id from enrollee_eo where enrollee_id=$enrollee_id");
$officer=mysql_getRow("select * from enrollment_officer where officer_id=$officer_id");

include ("includes/fpdf.php");
include ("affidavit.inc.php");

$boundary="==Multipart-Boundary_x".md5(time())."x";
$headers="From: ".EMAILFROM."\n".mimeHeaders($boundary);
$res=mysql_query($credquery);
$creds="";
while ($row=mysql_fetch_array($res))
{
  $creds.="\n\t".$row["type_name"]." issued by ".$row["issuing_agency"]." on ".prettyDate($row["issue_date"]);
}
$att="\n\n".addMIMEpart($pdfname,"pdf",$boundary,"base64");
##$att.=addMIMEpart($ndaname,"pdf",$boundary,"base64");

$enrobody="\n\n".addMIMEpart("","plain",$boundary,"","in-line");
$enrobody.="Howdy!\nAttached to this email should be:\n\t1. your Non-Disclosure agreement (NDA) and \n\t2. your affidavit of identity'";
$enrobody.="\nPlease read them over to make sure that they say what you want to say.";
$enrobody.="If you have a problem with the NDA, notify us so that our lawyers can check out your corrections.  ";
$enrobody.="If there is a problem with the affidavit, please let the officer know when the appointment is made.  ";
$enrobody.="\n\nThe enrollment officer whom you selected, $officername, should be contacting you soon to make an appointment for your enrollment.";
$enrobody.="To this meeting you should take these two documents and the credentials which you told us of, to wit:";
$enrobody.=$creds;
$enrobody.=$att;

$offbody="\n\n".addMIMEpart("","plain",$boundary,"","in-line");
$offbody.="You have a new enrollee by the name of $name.";
$offbody.="\n\nThe victim may be contacted at:\n\t".$enrollee["email"]."\nor\n";
$offbody.="$address\n$addresscity, $addressstate  ".$residence["zipCode"]."\n$addresscountry\n";
$res=mysql_query("select * from phone where phone_id in (select phone_id from enrollee_phone where enrollee_id=$enrollee_id)");
while ($row=mysql_fetch_array($res))
{
  $offbody.="\n\t".$row["phone_number"]." (".$row["phone_type"].")";
}
$offbody.="\n\n".(($enrollee["gender"] == "m")?("He"):("She"))." should bring these credentials:";
$offbody.=$creds;
$offbody.=$att;

$ok=@mail($enrollee["email"],EMAILSUBJECT,$enrobody,$headers);
if ($ok)
{
  $msg="We have sent you an email with further instructions and your NonDisclosure Agreement(NDA) and Affidavit of Identity as attachments.";
  $offok=@mail($officer["email"],OFFICERSUBJECT,$offbody,$headers);
  if ($offok)
    $msg.="<BR>Your enrollment officer, $officername, should contact you soon.";
  else
  {
    echo reporterror("sending email to the officer");
    $msg.="<BR>Contrary to what that message says, we weren't able to contact your enrollment officer so you might want to contact "
        .(($officer["gender"] == "m")?("him."):("her."));
  }
}
else
{
  $msg="We are having some difficulty sending you an email with all your information.  It is available here:";
}

##$msg="Thank You! <br> Here You will get the Docs to Print: ";
?>
<!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>
<link href="./style/design.css" rel="stylesheet" type="text/css" />

<style type="text/css">
<!--
.style1 {font-size: 0.8em}
-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Done</title>
<script type="text/javascript">	
function showPopup(flag,id)
{
	window.open("pop.php?tag="+flag+"&id="+id, "new_win", "width=510,height=300,titlebar,scrollbars,resizable");
}
</script>
</head>
<body>
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="0">
	<tr>
		<td align="center" valign="top">
<?php
include_once("includes/header.php");
?>
			<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">	 
			 <tr>
			   <td width="30%" valign="top">
				<div class="bodySubHeader">
				<span class="bsht03">Wait for us</span>		
				</div>
				</td>
			 </tr> 
			</table>		
			<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0" class="DarkLightBox">	 
			 <tr>
			   <td height="30" align="center" valign="top" colspan="8"><strong><?=$msg?></strong></td>	   
			 </tr>
        <tr>
          <td height="30" align="center" valign="top" colspan="8">
          </td>
        </tr>
			 <tr>
			   <td width="65%" align="left" valign="top" colspan="8" class="TopMenu2">&nbsp;</td>	  
			 </tr> 
			 <tr>
			   <td height="30" align="center" valign="top" colspan="8"><a href="Javascript:void(0)" onclick="javascript:showPopup('NDA','<?=$enrollee_id?>');" class="bodyText2">Open NDA</a></td>	   
			 </tr>
			 <tr>
			   <td height="30" align="center" valign="top" colspan="8"><a href="Javascript:void(0)" onclick="javascript:showPopup('eo','
         <?=$officer_id?>');" class="bodyText2">EO Details</a></td>	   
			 </tr>
			</table>
<?php
include_once("includes/footer.php");
?>
	   </td>
	 </tr> 	 
</table>
</body>
</html>