<?php

	$sPage  = "stats_age";
	require("admin/§lib_inc.php");

	//$sTitle = decode_lng_text("STATS_TITLE");
	$sTitle = decode_lng_text("STATS_AGE_TITLE");
	require("§menu.php");

	//set_time_limit(0);
	
	flush();

	$FAMILY=isset($_REQUEST["FAMILY"])?$_REQUEST["FAMILY"] : -1;
	if($FAMILY!=-1)
	{
		$sReqFam="and id_family=$FAMILY";
	}
	else
	{
		$sReqFam="";
	}

	if($bSessionOk==false)
	{
		$sReqCont="and contemporary=1";
	}
	else
	{
		$sReqCont="";
	}
	$sReqCont="";
	
	echo "<P>";

	$id_type_evnt_marr=0;
	$row_evnt = mysql_fetch_array(mysql_query( "select id_type_event from oxg_events_type WHERE code='MARR'"));
	$id_type_evnt_marr = $row_evnt['id_type_event'];
	$nbMarr = 0;
//	$nbMarr = mysql_num_rows(mysql_query( "select id_event from oxg_events where type='$id_type_evnt_marr' $sReqFam"));
	//$result = mysql_query( "select count(*) as SOMME from oxg_events where type='$id_type_evnt_marr' $sReqFam");
	$wedding_age_total_h = 0;
	$count_h = 0;
	$wedding_age_total_f = 0;
	$count_f = 0;
	$result = mysql_query( "select id_family, id_event, id_father, info2 from oxg_events where type='$id_type_evnt_marr' $sReqFam");
	while ($row = mysql_fetch_array($result))
	{
		//$nbMarr = $row['SOMME'];
		$result_cont = mysql_query( "select * from oxg_individuals where id_individual=$row[id_father] and id_family=$row[id_family] $sReqCont");
		if ($row_cont = mysql_fetch_array($result_cont))
		{
		$nbMarr+=1;
		}
		//recherche date du mariage
		$result_date = mysql_query( "select libelle from oxg_events where type='76' and id_father=$row[id_event] and id_family=$row[id_family] $sReqCont");
		if($row_date = mysql_fetch_array($result_date))
			{
			$weddingdate = substr($row_date['libelle'],strlen($row_date['libelle'])-4);
			}
		//Recherche age du marié
		$result_h = mysql_query( "select date_naissance from oxg_individuals WHERE date_naissance!=\"\" and id_individual=$row[id_father] and id_family=$row[id_family] $sReqCont");
		if ($row_h = mysql_fetch_array($result_h))
			{
			$birthdate_h = substr($row_h['date_naissance'],strlen($row_h['date_naissance'])-4);
			$wedding_age_h = $weddingdate - $birthdate_h;
			$wedding_age_total_h += $wedding_age_h;
			$count_h += 1;
			}
		//Recherche age de la mariée
		$result_f = mysql_query( "select date_naissance from oxg_individuals WHERE date_naissance!=\"\" and id_individual=$row[info2] and id_family=$row[id_family] $sReqCont");
		if ($row_f = mysql_fetch_array($result_f))
			{
			$birthdate_f = substr($row_f['date_naissance'],strlen($row_f['date_naissance'])-4);
			$wedding_age_f = $weddingdate - $birthdate_f;
			$wedding_age_total_f += $wedding_age_f;
			$count_f += 1;
			}
	}
	

	echo "</p>";
	//Affichage de l'age des mariés
	$wedding_age_average_h = $wedding_age_total_h / $count_h;
	$wedding_age_average_f = $wedding_age_total_f / $count_f;
	echo decode_lng_text("STATS_AGE_WM")." : ";
	echo (round($wedding_age_average_h)-1)." ans</br>";
	echo decode_lng_text("STATS_AGE_WW")." : ";
	echo (round($wedding_age_average_f)-1)." ans</br>";	
	echo "<P>";

	
	// Calcul de l'age moyen au moment du décès 
	$count_h = 0;
	$count_f = 0;
	$death_age_total_h = 0;
	$death_age_total_f = 0;
	$result = mysql_query( "select sex, date_naissance, date_deces from oxg_individuals WHERE date_naissance!=\"\" and date_deces!=\"\" $sReqCont $sReqFam");
	while ($row = mysql_fetch_array($result))
	{
	$birthdate = substr($row['date_naissance'],strlen($row['date_naissance'])-4);
	$deathdate = substr($row['date_deces'],strlen($row['date_deces'])-4);
	$death_age = $deathdate - $birthdate;
	if ($row['sex']==0)
		{
		$death_age_total_h += $death_age;
		$count_h += 1;
		}
	if ($row['sex']==1)
		{
		$death_age_total_f += $death_age;
		$count_f += 1;
		}
	}
	$death_age_average_h = $death_age_total_h / $count_h;
	$death_age_average_f = $death_age_total_f / $count_f;
	echo decode_lng_text("STATS_AGE_DM")." : ";
	echo (round($death_age_average_h)-1)." ans</br>";
	echo decode_lng_text("STATS_AGE_DW")." : ";
	echo (round($death_age_average_f)-1)." ans</br>";	
	echo "<P>";
	
	
	
	// Calcul de l'age moyen au moment du décès  des adultes
	$count_h = 0;
	$count_f = 0;
	$death_age_total_h = 0;
	$death_age_total_f = 0;
	$result = mysql_query( "select sex, date_naissance, date_deces from oxg_individuals WHERE date_naissance!=\"\" and date_deces!=\"\" $sReqCont $sReqFam");
	while ($row = mysql_fetch_array($result))
	{
	$birthdate = substr($row['date_naissance'],strlen($row['date_naissance'])-4);
	$deathdate = substr($row['date_deces'],strlen($row['date_deces'])-4);
	$death_age = $deathdate - $birthdate;
	if ($row['sex']==0 and $death_age>19)
		{
		$death_age_total_h += $death_age;
		$count_h += 1;
		}
	if ($row['sex']==1 and $death_age>19)
		{
		$death_age_total_f += $death_age;
		$count_f += 1;
		}
	}
	$death_age_average_h = $death_age_total_h / $count_h;
	$death_age_average_f = $death_age_total_f / $count_f;
	echo decode_lng_text("STATS_AGE_DOM")." : ";
	echo (round($death_age_average_h)-1)." ans</br>";
	echo decode_lng_text("STATS_AGE_DOW")." : ";
	echo (round($death_age_average_f)-1)." ans</br>";	
	echo "<P>";
	
	
	
	
	// Calcul de l'age moyen au moment du décès  des enfants
	$count_h = 0;
	$count_f = 0;
	$death_age_total_h = 0;
	$death_age_total_f = 0;
	$result = mysql_query( "select sex, date_naissance, date_deces from oxg_individuals WHERE date_naissance!=\"\" and date_deces!=\"\" $sReqCont $sReqFam");
	while ($row = mysql_fetch_array($result))
	{
	$birthdate = substr($row['date_naissance'],strlen($row['date_naissance'])-4);
	$deathdate = substr($row['date_deces'],strlen($row['date_deces'])-4);
	$death_age = $deathdate - $birthdate;
	if ($row['sex']==0 and $death_age<20)
		{
		$death_age_total_h += $death_age;
		$count_h += 1;
		}
	if ($row['sex']==1 and $death_age<20)
		{
		$death_age_total_f += $death_age;
		$count_f += 1;
		}
	}
	if ($count_h!=0) {$death_age_average_h = $death_age_total_h / $count_h;} else {$death_age_average_h = 1;}
	if ($count_f!=0) {$death_age_average_f = $death_age_total_f / $count_f;} else {$death_age_average_f = 1;}
	echo decode_lng_text("STATS_AGE_DYM")." : ";
	echo (round($death_age_average_h)-1)." ans</br>";
	echo decode_lng_text("STATS_AGE_DYW")." : ";
	echo (round($death_age_average_f)-1)." ans</br>";	
	echo "<P>";

	$here=decode_lng_text("STATS_HERE");

	echo decode_lng_text("STATS_ARTICLE")." 15 ".decode_lng_text("STATS_TOP15_OLP")." ><a href='§top_older.php?FAMILY=$FAMILY&nbre=15'><b>$here</b></a><<P>";

	echo decode_lng_text("STATS_ARTICLE")." 15 ".decode_lng_text("STATS_TOP15_LML")." ><a href='§top_older2.php?FAMILY=$FAMILY&nbre=15'><b>$here</b></a><<P>";
	
	echo decode_lng_text("STATS_ARTICLE")." 15 ".decode_lng_text("STATS_TOP15_YW")." ><a href='§top_wedding.php?FAMILY=$FAMILY&ordre=asc&nbre=15'><b>$here</b></a><<P>";
	
	echo decode_lng_text("STATS_ARTICLE")." 15 ".decode_lng_text("STATS_TOP15_OW")." ><a href='§top_wedding.php?FAMILY=$FAMILY&ordre=desc&nbre=15'><b>$here</b></a><<P>";

	echo "</TABLE></CENTER>";


	affiche_pied_page(true);

?>
