<?php
session_start();
ob_start();

require("includes/config.php");
require("includes/dbcon.php");
if (!$link) reporterror("connecting to the database");

if($_SESSION['uid']==""){	
	header("location:index.php");
	exit();
}
$enrollee_id=$_SESSION['uid'];

$activeQuery = mysql_query("select * from enrollee where enrollee_id=".$enrollee_id);
$activeValue = mysql_fetch_array($activeQuery);

if( !empty($_POST) )
{	
	$fisrt_name=trim($_REQUEST['fisrt_name']);
	$middle_name=trim($_REQUEST['middle_name']);
	$last_name=trim($_REQUEST['last_name']);
	$sex=trim($_REQUEST['sex']);
	$dob=trim($_REQUEST['dob']);
	$city_born=trim($_REQUEST['city_born']);
	$country=trim($_REQUEST['country']);
	$state=trim($_REQUEST['state']);
	$city=trim($_REQUEST['city']);
	##$street_number=trim($_REQUEST['street_number']);
	$street_line_01=trim($_REQUEST['street_line_01']);
	$street_line_02=trim($_REQUEST['street_line_02']);
	$street_line_03=trim($_REQUEST['street_line_03']);
	$zipCode=trim($_REQUEST['zipCode']);
	$landPhone=trim($_REQUEST['landPhone']);
	$mobilePhone=trim($_REQUEST['mobilePhone']);	

  $state_id=getStateId($state,$country);
  $city_id=getCityId($city,$state_id,$country);
	
	if(!empty($landPhone))
	{
		$insert_landPhone_sql="insert into phone set	 
		phone_type='Land Phone',
		phone_number=".$landPhone;
		mysql_query($insert_landPhone_sql);
		$landPhoneId= mysql_insert_id();

		$insert_enrolleePhone2_sql="insert into enrollee_phone set	 
		enrollee_id=".$enrollee_id.",
		phone_id=".$landPhoneId;
		mysql_query($insert_enrolleePhone2_sql);
	}
	if(!empty($mobilePhone))
	{
		$insert_mobilePhone_sql="insert into phone set	 
		phone_type='Mobile Phone',
		phone_number=".$mobilePhone;
		mysql_query($insert_mobilePhone_sql);
		$mobilePhoneId= mysql_insert_id();

		$insert_enrolleePhone3_sql="insert into enrollee_phone set	 
		enrollee_id=".$enrollee_id.",
		phone_id=".$mobilePhoneId;
		mysql_query($insert_enrolleePhone3_sql);
	}
	   
	$zipCode_sql="select * from zip_code where zip_code=".$zipCode;
	$latLonValue=mysql_getRow($zipCode_sql);
  if (is_null($latLonValue))
  {
    $lat=0;
    $lon=0;
  }
  else
  {
  	$lat=$latLonValue['lat'];
	  $lon=$latLonValue['lon'];
  }

	$insert_address_sql="insert into address set 
	city_id=".$city_id.",
	state_id=".$state_id.", 
	country_id=".$country.", 
	street_line_01='".mysql_escape_string($street_line_01)."',
	street_line_02='".mysql_escape_string($street_line_02)."',
	street_line_03='".mysql_escape_string($street_line_03)."',
	zipCode='".mysql_escape_string($zipCode)."',
	lat=".mysql_escape_string($lat).",
	lng=".mysql_escape_string($lon);	
	mysql_query($insert_address_sql);
	$insrt_address_id= mysql_insert_id();
	//$insrt_address_id= 1;
	if($insrt_address_id)
	{		
		if($_FILES["userImage"]['name']!='')
		{
			$type=substr($_FILES["userImage"]['type'],0,strpos($_FILES["userImage"]['type'],"/"));
			if($type!="image")
			{	
				$err_msg="Invalid Format of Image File";				
				//header("location:userDetails.php");
			}
			else    ##$type!="image"
			{
				$file_db=$enrollee_id."_".$_FILES["userImage"]['name'];
				$dirname="./upload/".$enrollee_id."/";				
				$filename=$dirname.$file_db;				
				$src=$_FILES["userImage"]['tmp_name'];
				$tag="width";
				$thwidth="90";
				$thheight="90";					
				thumbnail($filename,$src,$thwidth,$thheight,$tag);
				

				##$insert_docType_sql="insert into document_type set	 
				##document_type_name='Photo'";	
				##mysql_query($insert_docType_sql);
				##$docTypeId= mysql_insert_id();
        $docTypeId=1;

				if($docTypeId)
				{
					$insert_documents_sql="insert into documents set	 
					document_type_id=".$docTypeId.",
					document_name='".$file_db."',					
					document_link='".$filename."'";	
					mysql_query($insert_documents_sql);
					$documentsId= mysql_insert_id();
				}
				if($documentsId)
				{
					$insert_enrolleeDoc_sql="insert into enrollee_documents set	 
					enrollee_id=".$enrollee_id.",
					document_id=".$documentsId.",	
					date_created=now(),
					date_updated=now()";	
					mysql_query($insert_enrolleeDoc_sql);					
				}
			}     ##$type!="image"
		}       ##$_FILES["userImage"]['name']!=''
		$update_sql="update enrollee SET 
      address_id=".$insrt_address_id.",
		  city_born=".$cityId.",			 
		  fisrt_name='".mysql_escape_string($fisrt_name)."',
		  middle_name='".mysql_escape_string($middle_name)."',
		  last_name='".mysql_escape_string($last_name)."',
		  gender='".mysql_escape_string($sex)."',	
		  date_of_birth='".mysql_escape_string($dob)."'";
		  $update_sql .= " WHERE enrollee_id=".$enrollee_id;		
		  $rs=mysql_query($update_sql) or die(mysql_error());   ##Seems like a programmer's expediency.
		//header("location:selectOfficer.php");
		header("location:userDetails2.php");
	}   ##insrt_address_id
  else
    $err_msg=reporterror("inserting the address",$insert_address_sql);
}
ob_end_flush();

$redstar="<font color=\"red\"><strong>*</strong></font>";
?>
<!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>
<!--script language='javascript' src='datepicker/popcalendar.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.fisrt_name.value, "PLEASE INPUT FIRST NAME" )
		if( is_valid == false ) { document.frm.fisrt_name.focus(); return is_valid }

	is_valid = blank_check( document.frm.last_name.value, "PLEASE INPUT LAST NAME" )
		if( is_valid == false ) { document.frm.last_name.focus(); return is_valid }
	
	is_valid = blank_check( document.frm.dob.value, "PLEASE INPUT DATE OF BIRTH" )
		if( is_valid == false ) { document.frm.dob.focus(); return is_valid }
		
	is_valid = blank_check( document.frm.city_born.value, "PLEASE INPUT CITY OF BIRTH" )
		if( is_valid == false ) { document.frm.city_born.focus(); return is_valid }	

	is_valid = blank_check( document.frm.state.value, "PLEASE INPUT STATE" )
		if( is_valid == false ) { document.frm.state.focus(); return is_valid }

	is_valid = blank_check( document.frm.city.value, "PLEASE INPUT CITY" )
		if( is_valid == false ) { document.frm.city.focus(); return is_valid }

	is_valid = blank_check( document.frm.zipCode.value, "PLEASE INPUT ZIP" )
		if( is_valid == false ) { document.frm.zipCode.focus(); return is_valid }

	is_valid = chosen_check( document.frm.country.value, "PLEASE SELECT COUNTRY" )
		if( is_valid == false ) { document.frm.country.focus(); return is_valid }
		
	##is_valid = blank_check( document.frm.street_number.value, "PLEASE INPUT STREET NUMBER" )
	##	if( is_valid == false ) { document.frm.street_number.focus(); return is_valid }
	
		
	is_valid = blank_check( document.frm.street_line_01.value, "PLEASE INPUT A STREET ADDRESS" )
		if( is_valid == false ) { document.frm.street_line_01.focus(); return is_valid }
	
		
	is_valid = blank_check( document.frm.landPhone.value, "PLEASE INPUT A PHONE NUMBER" )
		if( is_valid == false ) { document.frm.landPhone.focus(); return is_valid }	
		
	##is_valid = blank_check( document.frm.mobilePhone.value, "PLEASE INPUT MOBILE NUMBER" )
	##	if( is_valid == false ) { document.frm.mobilePhone.focus(); return is_valid }
		
	##is_valid = blank_check( document.frm.userImage.value, "PLEASE UPLOAD YOUR IMAGE" )
	##	if( is_valid == false ) { document.frm.userImage.focus(); 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");
?>
      <?php
if( $activeValue['activFlag']== 0 )   ##We should let activFlag count so that the enrollee would be redirected to the next page to be done.
{
?>
      <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 1: Current Data</span>
              </div>
            </td>
          </tr>
        </table>
        <table width="90%" border="0" align="center" cellpadding="0" cellspacing="0" class="DarkLightBox">
          <?php
	if($err_msg)
	{
	?>
          <tr>
            <td height="30" align="center" valign="top" colspan="8">
              <strong>
                <?=$err_msg?>
              </strong>
            </td>
          </tr>
          <?php
	}
	?>
          <tr>
            <td width="65%" align="left" valign="top" colspan="8" class="TopMenu2">Personal Details</td>
          </tr>
          <tr>
            <td align="left" valign="top" colspan="8">&nbsp;</td>
          </tr>
          <tr>
            <td height="30" align="left" valign="top" class="browseEventContents">First Name:<?=$redstar?>
            </td>
            <td height="30" align="left" valign="top">
              <input name="fisrt_name" value="<?=$activeValue['fisrt_name']?>" type="text" class="textField" readonly/>
            </td>
            <td height="30" align="left" valign="top" width="1%">&nbsp;</td>
            <td height="30" align="left" valign="top" class="browseEventContents">Middle Name:</td>
            <td height="30" align="left" valign="top">
              <input name="middle_name" value="<?=$activeValue['middle_name']?>" type="text" class="textField" readonly/>
            </td>
            <td height="30" align="left" valign="top" width="1%">&nbsp;</td>
            <td height="30" align="left" valign="top" class="browseEventContents">
              Surname:<?=$redstar?>
            </td>
            <td height="30" align="left" valign="top">
              <input name="last_name" value="<?=$activeValue['last_name']?>" type="text" class="textField" readonly/>
            </td>
          </tr>
          <tr>
            <td height="30" align="left" valign="top" class="browseEventContents">Sex:</td>
            <td height="30" align="left" valign="top">
              <select name="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:<?=$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>
            <td height="30" align="left" valign="top">&nbsp;</td>
            <td height="30" align="left" valign="top" class="browseEventContents">City of Birth:<?=$redstar?>
            </td>
            <td height="30" align="left" valign="top">
              <input name="city_born" type="text" class="textField"/>
            </td>
          </tr>
          <tr>
            <td height="30" align="left" valign="top" class="browseEventContents">Upload Your Photo:<?=$redstar?>
            </td>
            <td height="30" align="left" valign="top" colspan="7">
              <input type="file" name="userImage" value=""/>
            </td>
          </tr>
          <tr>

          </tr>
          <tr>
            <td align="left" valign="top" colspan="8">&nbsp;</td>
          </tr>


          <TR valign="top">
            <td colspan="6">
              <TABLE>
                <TR>
                  <td width="65%" align="left" valign="top" colspan="8" class="TopMenu2">Residential Address</td>
                </TR>
                <TR>
                  <td height="30" align="left" valign="top" class="browseEventContents">Street Address:<?=$redstar?>
                  </td>
                  <td height="30" align="left" valign="top">
                    <input name="street_line_01" type="text" class="textField"/>
                  </td>
                </TR>
                <TR>
                  <td height="30" align="left" valign="top" class="browseEventContents"></td>
                  <td height="30" align="left" valign="top">
                    <input name="street_line_02" type="text" class="textField"/>
                  </td>
                </TR>
                <TR>
                  <td height="30" align="left" valign="top" class="browseEventContents"></td>
                  <td height="30" align="left" valign="top">
                    <input name="street_line_03" type="text" class="textField"/>
                  </td>
                </TR>
                <tr>
                  <td height="30" align="left" valign="top" class="browseEventContents">City:<?=$redstar?>
                  </td>
                  <td height="30" align="left" valign="top">
                    <input name="city" type="text" class="textField"/>
                  </td>
                  <td height="30" align="left" valign="top">&nbsp;</td>
                  <td height="30" align="left" valign="top" class="browseEventContents">
                    State:<?=$redstar?>
                  </td>
                  <td height="30" align="left" valign="top">
                    <input name="state" type="text" class="textField"/>
                  </td>
                  <td height="30" align="left" valign="top">&nbsp;</td>
                  <td height="30" align="left" valign="top" class="browseEventContents">
                    Zip Code:<?=$redstar?>
                  </td>
                  <td height="30" align="left" valign="top">
                    <input name="zipCode" type="text" class="textField"/>
                  </td>
                </tr>
                <tr>
                  <td height="30" align="left" valign="top" class="browseEventContents">
                    Country:<?=$redstar?>
                  </td>
                  <td height="30" align="left" valign="top" colspan="7">
                    <select name='country' id='country'>
                      <option value='0' Selected='' >--- Select Country ---</option>
                      <?php					
					$countryQuery = mysql_query("select country_id, country_name from country order by country_name asc");
					while($countryValue = mysql_fetch_array($countryQuery))
					{
						if($countryValue['country_id'] == $_REQUEST['country_id'])
						{
							echo"<option value='".$countryValue['country_id']."' Selected >".$countryValue['country_name']."</option>";
						}
						else
						{
							echo"<option value='".$countryValue['country_id']."' >".$countryValue['country_name']."</option>";
						}
					}
			?>
                    </select>
                  </td>
                </tr>
              </TABLE>
              </td>
              <td colspan='4'>
                <TABLE>
                  <TR>
              <td width="65%" align="left" valign="top" colspan="8" class="TopMenu2">Phone Numbers</td>
            </TR>
                  <tr>
                    <td height="30" align="left" valign="top" class="browseEventContents">
                      Primary Phone Number:<?=$redstar?>
                    </td>
                    <td height="30" align="left" valign="top">
                      <input name="landPhone" type="text" class="textField"/>
                    </td>
                    <td height="30" align="left" valign="top">&nbsp;</td>
                  </tr>
                  <tr>
                    <td height="30" align="left" valign="top" class="browseEventContents">
                      Cell Phone Number:<?=$redstar?>
                    <td height="30" align="left" valign="top">
                      <input name="mobilePhone" type="text" class="textField"/>
                    </td>
                    <td height="30" align="left" valign="top">&nbsp;</td>
                    <td height="30" align="left" valign="top" class="browseEventContents">&nbsp;</td>
                    <td height="30" align="left" valign="top">&nbsp;</td>
                  </tr>
                </TABLE>
        </td>
      </TR>
        
      </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
}
else    ##activFlag == 0
{
	$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>
<?php
function thumbnail($filethumb,$file,$Twidth,$Theight,$tag)
{	
	list($width,$height,$type,$attr)=getimagesize($file);
	switch($type)
	{
		case 1:
			$img = ImageCreateFromGIF($file);
		break;
		case 2:
			$img=ImageCreateFromJPEG($file);
		break;
		case 3:
			$img=ImageCreateFromPNG($file);
		break;
	}
	if($tag == "width") //width contraint
	{
		$Theight=round(($height/$width)*$Twidth);
	}
	elseif($tag == "height") //height constraint
	{
		$Twidth=round(($width/$height)*$Theight);
	}
	else
	{
		if($width > $height)
			$Theight=round(($height/$width)*$Twidth);
		else
			$Twidth=round(($width/$height)*$Theight);
	}
	$thumb=imagecreatetruecolor($Twidth,$Theight);
	if(imagecopyresampled($thumb,$img,0,0,0,0,$Twidth,$Theight,$width,$height))
	{		
		switch($type)
		{
			case 1:
				ImageGIF($thumb,$filethumb);
			break;
			case 2:
				ImageJPEG($thumb,$filethumb);
			break;
			case 3:
				ImagePNG($thumb,$filethumb);
			break;
		}
		chmod($filethumb,0666);
		return true;
	}
}
?>