<?PHP
$enrollee=mysql_getRow("select * from enrollee where enrollee_id=$enrollee_id");
$address=mysql_getRow("select * from address where owner_id=$enrollee_id and address_type_id='ehom'");
$license=mysql_getRow("select * from credential where credential_type_code='dl' and enrollee_id=$enrollee_id");
$phone=mysql_getRow("select * from phone where enrollee_id=$enrollee_id and phone_type_code='ehom'");
$ss=mysql_getRow("select * from credential where credential_type_code='ss' and enrollee_id=$enrollee_id");

$args["strSocialSecurity"]=$ss["number"];
$args["strOwnNameLast"]=$enrollee["last_name"];
$args["strCurAddrCity"]=mysql_getOne("select city_name from city where city_id=".$address["city_id"]);
$args["strCurAddrState"]=mysql_getOne("select state_code from state where state_id=".$address["state_id"]);
$args["strCurAddrZip"]=$address["zipCode"];
$args["strDrivingLicense"]=$license["number"];
$args["strTelephone"]=$phone["phone_number"];
$licst=$license["issuing_agency"];
if (strlen($licst) == 2)
  $args["strDrivingLicenseState"]=$licst;
else
{
  $dlst=mysql_getOne("select state_code from state where state_name = '$licst'");
  if (is_null($dlst))
    $dlst=mysql_getOne("select state_code from state where state_name like '%$licst%'");
  if (is_null($dlst))
    $dlst=substr($licst,0,2);
  $args["strDrivingLicenseState"]=$dlst;
}
$args["strCustRefNumber"]=NCMSUSER;
$args["strCustPassword"]=NCMSPASSWORD;
$args["strOwnNameFirst"]=$enrollee["first_name"];
$args["strAddress"]=$address["street_line_01"];
$args["strEmail"]=$_SERVER["SSL_CLIENT_S_DN_Email"];
$args["strIPAddress"]=$_SERVER["REMOTE_ADDR"];
$args["strLanguage"]="en";

$url=NCMSURL."?".http_build_query($args);
loggywoggy("Going to NCMS with $url.");
$res=file_get_contents($url);
if (strlen($res) == 0)
{
  reporterror("getting the PII from NCMS with $url");
}
else
{
  file_put_contents($snoopyfile,$res);

  $questionnum=0;

  function startElement($parser, $name, $attrs)
  {
    global $depth;
    global $questionnum;
    /*
    echo "\n";
    for ($i = 0; $i < $depth[$parser]; $i++)
    {
      echo "  ";
    }
      echo "$name";
      $depth[$parser]++;
      if (count($attrs)>0) echo "\n".print_r($attrs,TRUE);
    */
      
      if ($name == "Question")
      {
        global $enrollee_id;
        $question=$attrs["Question"];
        $query="insert into questions set "
          ."enrollee_id=$enrollee_id"
          .",question='".mysql_escape_string($question)."'";
        if (mysql_query($query))
        {
          $questionnum=mysql_insert_id();
        }
        else
        {
          reporterror("inserting a question into the database",$query);
          $questionnum=0;
        }
      }
      if ($name == "Answer")
      {
        if ($questionnum == 0)
        {
          reporterror ("parsing an answer when we don't have a question");
        }
        else
        {
          $num=$attrs["AnswerNumber"];
          $answer=$attrs["Answer"];
          $query="insert into answers set "
            ."question_id=$questionnum"
            .",answernumber=$num"
            .",answer='".mysql_escape_string($answer)."'";
          if (!mysql_query($query))
          {
            reporterror("recording answer $num to question $questionnum, $answer",$query);
          }
          if ($attrs["IsCorrect"] == "True")
          {
            $query="update questions set correct=$num where question_id=$questionnum";
            if (!mysql_query($query))
            {
              reporterror("designating answer $num as the correct one for question $questionnum");
            }
          }
        }
      }
  }

  function endElement($parser, $name) 
  {
      global $depth;
      $depth[$parser]--;
  }

  function characterData($parser, $data) 
  {
      if (strlen(trim($data)) > 0) echo "=<b>$data</b>";
  }

  function defaultData($parser,$data)
  {
  if (strlen($data)>0) echo "<FONT color=\"red\">$data</FONT>";
  }

  $par = xml_parser_create();
  xml_set_element_handler($par, "startElement", "endElement");
  ##xml_set_character_data_handler($par, "characterData");
  ##xml_set_default_handler($par, "defaultData");
  xml_parser_set_option($par,XML_OPTION_CASE_FOLDING,0);
  xml_parse($par,$res,TRUE);
  xml_parser_free($par);
}
?>
