<?php
session_start();
ob_start();

require("includes/config.php");
require("includes/dbcon.php");
if($_SESSION['uid']==""){	
	header("location:index.php");
	exit();
}
$enrollee_id=$_SESSION['uid'];
$activeValue = mysql_getOne("select activFlag from enrollee where enrollee_id=".$enrollee_id);
$err_msg="";
$msg="";

if( !empty($_POST) )
{
  $ended=FALSE;         ##Stops asking for more credentials
  for ($j=0;$j<3;$j++)
  {
    $credtype=trim($_REQUEST["credential_type_code$j"]);
    if ($credtype == "nm"){$ended=TRUE;continue;}
    if ($credtype == "  ") continue;
    $name=trim($_REQUEST["name$j"]);
    $prior_name=trim($_REQUEST["prior_name$j"]);
    $issuing_agency=trim($_REQUEST["issuing_agency$j"]);
    $issue_date=trim($_REQUEST["issue_date$j"]);
    $valid_until=trim($_REQUEST["valid_until$j"]);
    $query="insert into credential set
      enrollee_id=$enrollee_id,
      issuing_agency='".mysql_escape_string($issuing_agency)."',
      credential_type_code='$credtype',
      name='".mysql_escape_string($name)."',
      issue_date='".mysql_escape_string($issue_date)."'";
    if (strlen($prior_name)>0) $query.=",prior_name='".mysql_escape_string($prior_name)."'";
    if (strlen($valid_until)>0) $query.=",valid_until='".mysql_escape_string($valid_until)."'";
    if (!mysql_query($query)) $err_msg.=reporterror("adding credential $j",$query);
    else $msg.="<BR>Added $credtype to the table.";
  }
  if ($ended  || ($_REQUEST["command"] == "Next"))
  {
    header("location:userDetails4.php");
    exit();
  }
}
ob_end_flush();

######################################################################################################

?>
<!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>User Details Page</title>
<script language="JavaScript" src="includes/js/common.js"></script>
<link rel="stylesheet" type="text/css" media="all" href="cal/calendar-win2k-cold-1.css" title="win2k-cold-1"><!-- main calendar program -->  
  <script type="text/javascript" src="cal/calendar.js"></script><!-- language for the calendar -->  
  <script type="text/javascript" src="cal/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="cal/calendar-setup.js"></script>
<script language="JavaScript">
function validate()
{
	var is_valid=true

	return is_valid
}
</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");
          $redstar='<font color="red"><strong>*</strong></font>';
          $typequery="select * from credential_type";
          $credsquery="select c.*,ct.type_name from credential c,credential_type ct where ct.credential_type_code=c.credential_type_code and enrollee_id=$enrollee_id order by issue_date";
          $creds=mysql_query($credsquery);
          $nCreds=mysql_num_rows($creds);
        ?>
        <?php
        if( $activeValue['activFlag']== 0 )
        {
        ?>
          <table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
            <tr>
              <td height="30" align="center" valign="top" colspan="8">
                <strong>
                  <?=$err_msg?>
                </strong>
              </td>
            </tr>
            <tr>
              <td width="30%" valign="top">
                <div class="bodySubHeader">
                  <span class="bsht03">Step 3: Identity Documentation</span>
                </div>
              </td>
            </tr>
            <tr>
              <td colspan="4">
                <?PHP
                if ($nCreds >0)
                {
                  echo "So far, you have submitted these $nCreds documents:";
                  for ($j=0;$j<$nCreds;$j++)
                  {
                    $cred=mysql_fetch_array($creds);
                    echo "<BR>".$cred["issue_date"]."-".$cred["type_name"]." issued by ".$cred["issuing_agency"]
                      ." gives your name as ".$cred["name"].".";
                    if (!is_null($cred["prior_name"]) && strlen($cred["prior_name"]) > 0)
                      echo "This was a change from ".$cred["prior_name"].".";
                  }
                  echo "<BR><FORM action=userDetails4.php><INPUT type=submit name=command value=\"That's all I have\"></FORM>";
                  echo "<BR>Now you can add more for a complete record.";
                }
                else
                {
                ?>
                We now need to know what documents you will bring to the enrollment session.
                If you change your mind later, we can deal with it at the session.
                We would like to have at least your birth certificate and one government-issued photo ID.
                You should also bring any documents which change your name (court order, marriage license).
                You don't have to get them in order, we can do that.
                Just record them for us now as well as you can and bring them to the enrollment.
                <?PHP
                }
                ?>
                <br/>
                If you make mistakes or don't know exactly what's expected, we can also sort out all that at the enrollment.
                (We have very competent enrollment officers.)
              </td>
            </tr>
          </table>
        <form name="frm" method="post" action="<?=$script?>" enctype="multipart/form-data" onSubmit="return validate()">

          <table>
            <tr>
              <?PHP
              for ($i=0;$i<3;$i++)
              {
                $j=$nCreds+$i;
                echo "<th>Document $j</th>";
              }
              ?>
            </tr>
            <tr>
              <?PHP
              for ($j=0;$j<3;$j++)
              {
              ?>
              <td>
                <table width="90%" border="0" align="center" cellpadding="0" cellspacing="0" class="DarkLightBox">
                  <tr>
                    <td height="30" align="left" valign="top" class="browseEventContents">Document type:</td>
                    <td height="30" align="left" valign="top" colspan="7">
                      <select name='credential_type_code<?=$j?>' id='type_code<?=$j?>'>
                        <?php
                          echo "<option value=\"  \"></option>";
                          if ($nCreds == 0 and $j == 0)
		                        echo spew_options($link,$typequery,"credential_type_code","type_name","bc");
                          else
		                        echo spew_options($link,$typequery,"credential_type_code","type_name");
                          echo "<option value=\"nm\">No more</option>";
                        ?>
                      </select>
                    </td>
                  </tr>

                  <tr>
                    <td height="30" align="left" valign="top" class="browseEventContents">
                      Name:<?$resdtar?>
                    </td>
                    <td height="30" align="left" valign="top">
                      <input name="name<?=$j?>" type="text" class="textField"/>
                    </td>
                  </tr>
                  <tr>
                    <td height="30" align="left" valign="top" class="browseEventContents">Previous name<BR>(if changed):</td>
                    <td height="30" align="left" valign="top">
                      <input name="prior_name<?=$j?>" type="text" class="textField"/>
                    </td>
                  </tr>
                  <tr>
                    <td height="30" align="left" valign="top" class="browseEventContents">
                      Issuing Agency:<?$resdtar?>
                    </td>
                    <td height="30" align="left" valign="top">
                      <input name="issuing_agency<?=$j?>" type="text" class="textField"/>
                    </td>
                  </tr>
                  <tr>
                    <td height="30" align="left" valign="top" class="browseEventContents">
                      Date of issue:<?$resdtar?>
                    </td>
                    <td height="30" align="left" valign="top">
                      <input name="issue_date<?=$j?>" id="issue_date<?=$j?>" type="text" class="textField"/>&nbsp;<img src="cal/img.gif" id="f_trigger_<?=$j?>" style="border: 1px solid red; cursor: pointer;" title="Date selector" onMouseOver="this.style.background='red';" onMouseOut="this.style.background=''">
                    </td>
                  </tr>
                  <tr>
                    <td height="30" align="left" valign="top" class="browseEventContents">Expiration date<BR>(if any):</td>
                    <td height="30" align="left" valign="top">
                      <input name="valid_until<?=$j?>" id="valid_until<?=$j?>" type="text" class="textField"/>&nbsp;<img src="cal/img.gif" id="f_trigger_e<?=$j?>" style="border: 1px solid red; cursor: pointer;" title="Date selector" onMouseOver="this.style.background='red';" onMouseOut="this.style.background=''">
                    </td>
                  </tr>
                </table>
              </td>
              <?PHP
              }
              ?>
            </tr>
            <tr>
              <td height="35" align="left" valign="bottom" colspan="2">&nbsp;</td>
              <td height="35" align="left" valign="bottom">
                <input name="Submit" type="submit" value="Next" />
              </td>
            </tr>
          </table>
        </td>
    </tr>
  </table>
</form>
<?PHP
for($j=0;$j<3;$j++)
{
?>
<script type="text/javascript">
    Calendar.setup({
        inputField     :    "issue_date<?=$j?>",     // id of the input field    
        ifFormat       :    "%Y-%m-%e",      // format of the input field
        button         :    "f_trigger_<?=$j?>",  // trigger for the calendar (button ID)
        align          :    "Tl",           // alignment (defaults to "Bl")
        singleClick    :    true
    });
</script>
<?PHP
}
?>
<?php
}
else
{
	$msg="You are already activated";
?>
	<table width="80%" border="0" align="center" cellpadding="0" cellspacing="0">		 
	 <tr>
	   <td height="30" align="center" valign="top"><strong><?=$msg?></strong></td>	   
	 </tr>
	</table>
<?php
}
?>
<?php
include_once("includes/footer.php");
?>
	   </td>
	 </tr> 	 
</table>
</body>
</html>