<?php
	##This is the foundational information or borth record.  This information should never change for any individual.
	##Although sex is listed as birth_sex in the database, I present it to the user as sex to avoid cuteness, disgust, and commendation for sensitivity.
	##We do not have an option for undecided or indeterminate.
	require("includes/.htconfig.php");
	
	
	if( !empty($_POST) )
	{	
		$father_name=trim($_REQUEST['father_name']);
		$mother_name=trim($_REQUEST['mother_name']);
		$birth_name=trim($_REQUEST['birth_name']);
		$birth_date=trim($_REQUEST['dob']);
		$birth_sex=fromRequest("birth_sex");
		$city_born=trim($_REQUEST['city_born']);
		$state_born=trim($_REQUEST['state_born']);
		$country_id=fromRequest("country_id");
		
		if (is_null($birth_name)) $errmsgs.="<BR>We need to know the name on your birth certificate.";
		if (is_null($father_name)) $errmsgs.="<BR>We need to know your father's name on your birth certificate.";
		if (is_null($mother_name)) $errmsgs.="<BR>We need to know your mother's name on your birth certificate.";
		if (is_null($birth_date)) $errmsgs.="<BR>We need to know your date of birth as recorded on your birth certificate.";
		if (is_null($city_born)) $errmsgs.="<BR>We need to know the city you were born in.";
		if (is_null($state_born)) $errmsgs.="<BR>We need to know the state you were born in.";
		if (is_null($country_id)) $errmsgs.="<BR>We need to know what country you were born in.";
		
		$state_id=getStateId($state_born,$country_id);
		$city_id=getCityId($city_born,$state_id,$country_id);
		
		$n=mysql_getOne("select count(city_id) from birth_record where enrollee_id=$enrollee_id");
		$query=(($n>0)?"update":"insert into");
		$query.=" birth_record set "
		."birth_name='".mysql_escape_string($birth_name)."'"
		.",father_name='".mysql_escape_string($father_name)."'"
		.",mother_name='".mysql_escape_string($mother_name)."'"
		.",birth_sex='$birth_sex'"
		.",birth_date='".mysql_escape_string($birth_date)."'"
		.",city_id=$city_id"
		.",state_id=$state_id"
		.",country_id=$country_id";
		if($n>0) $query.=" where enrollee_id=$enrollee_id";
		else  $query.=",enrollee_id=$enrollee_id";
		
		if (mysql_query($query))
		{
			if (strlen($errmsgs) == 0) advancePage($enrollee_id,$newby);
		}
		else
		{
			reporterror("inserting birth record for enrollee $enrollee_id",$query);
			$errmsgs.=addMessage("We couldn't record your data.  Could you look it over and try again, please?");
		}
	}
	?>
<!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>Birth Record</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
	is_valid = blank_check( document.frm.father_name.value, "PLEASE INPUT YOUR FATHER'S NAME" )
	if( is_valid == false ) { document.frm.father_name.focus(); return is_valid }
	
	is_valid = blank_check( document.frm.mother_name.value, "PLEASE INPUT YOUR MOTHER'S NAME AT BIRTH" )
	if( is_valid == false ) { document.frm.mother_name.focus(); return is_valid }
	
	is_valid = blank_check( document.frm.birth_name.value, "PLEASE INPUT YOUR NAME AT BIRTH" )
	if( is_valid == false ) { document.frm.birth_name.focus(); return is_valid }
	
	is_valid = blank_check( document.frm.birth_date.value, "PLEASE INPUT DATE OF BIRTH" )
	if( is_valid == false ) { document.frm.birth_date.focus(); return is_valid }
	
	is_valid = chosen_check( document.frm.country_id.value, "PLEASE SELECT THE COUNTRY WHERE YOU WERE BORN" )
	if( is_valid == false ) { document.frm.country_id.focus(); return is_valid }
	
	is_valid = chosen_check( document.frm.city_born.value, "PLEASE SELECT THE CITY WHERE YOU WERE BORN" )
	if( is_valid == false ) { document.frm.city_born.focus(); return is_valid }
	
	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");
	if (strlen($errmsgs) > 0)
	{
	?>
<table width="80%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td height="30" align="center" valign="top">
<strong>
<?PHP
	echo $errmsgs;
?>
</strong>
</td>
</tr>
</table>
<?PHP
	}
	$spacer="<td height=\"30\" align=\"left\" valign=\"top\" width=\"1%\">&nbsp;";
	?>
<form name="frm" method="post" action="" enctype="multipart/form-data" onSubmit="return validate()">
<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">	 
<tr>
<td width="30%" valign="top">
<div class="bodySubHeader">
<span class="bsht03">Step 2: Birth Record</span>		
</div>
</td>
</tr> 
</table>		
<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0" class="DarkLightBox">
<tr>
<?PHP
	echo inputCells("Father's Name$redstar","father_name");
	echo $spacer.inputCells("Mother's Birth Name$redstar","mother_name");
	echo $spacer.inputCells("Your Name at Birth$redstar","birth_name");
	echo "</tr><tr>";
	?>
<td height="30" align="left" valign="top" class="browseEventContents">Sex:</td>
<td height="30" align="left" valign="top">
<select name="birth_sex">
<option value="m" Selected="">Male</option>
<option value="f">Female</option>
</select>
</td>
<td height="30" align="left" valign="top">&nbsp;</td>
<td height="30" align="left" valign="top" class="browseEventContents">
Date of birth:
<?PHP
	echo $redstar;
?>
</td>
<td height="30" align="left" valign="top">
<input name="dob" id="dob" type="text" class="textField"/>&nbsp;<img src="cal/img.gif" id="f_trigger_c" 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">Country of Birth:<font color="red"><strong>*</strong></font></td>	   
<td height="30" align="left" valign="top" colspan="7">
<select name='country_id' id='country_id'>
<option value='0'>--- Select Country ---</option>
<?php
	if (!is_null($enrollee["city_born"]))
	{
        $city=mysql_getRow("select * from city where city_id=".$enrollee["city_born"]);
        $state_born=mysql_getOne("select state_name from state where state_id=".$city["state_id"]);
	}
	if (!is_null($_REQUEST["country_id"])) $country_id=$_REQUEST["country_id"];
	else if (!is_null($city["country_code"])) $country_id=$city["country_code"];
	else $country_id=0;
	echo spew_options($link,"select country_id, country_name from country order by country_name asc","country_id","country_name",$country_id);
	?>
</select>
</td>	   
</tr>
<tr>
<?PHP
	echo inputCells("State of Birth$redstar","state_born");
	?>
</tr>
<tr>
<?PHP
	echo inputCells("City of Birth$redstar","city_born");
	?>
</td>
</tr>
<tr>
<td height="35" align="left" valign="bottom" colspan="7">&nbsp;</td>	   
<td height="35" align="left" valign="bottom"><input name="Submit" type="submit" value="Next" /></td>
</tr>
</table>
</form>
<script type="text/javascript">
Calendar.setup({
			   inputField     :    "dob",     // id of the input field
			   ifFormat       :    "%Y-%m-%e",      // format of the input field
			   button         :    "f_trigger_c",  // trigger for the calendar (button ID)
			   align          :    "Tl",           // alignment (defaults to "Bl")
			   singleClick    :    true
			   });
</script>
<?php
	include_once("includes/footer.php");
	?>
</td>
</tr> 	 
</table>
</body>
</html>