Location: PHPKode > projects > HuMo-gen > relations.php
<?php

//=================================================================
// relations.php - checks relationships between person X and person Y
//
// written by Yossi Beck - August 2010 for HuMo-gen
//
// contains the following functions:
// create_rel_array      - creates $rel_array with gedcom nr and generation nr of ancestors of person X and Y
// compare_rel_array     - compares the $rel_array arrays of X and Y to find common ancestor (can be the persons themselves)
// calculate_rel         - if found, determines the nature of the relation (siblings, ancestors, nephews etc.)
// calculate_ancestor    - calculates the degree of relations (2nd great-grandfather)
// calculate_descendant  - calculates the degree of relations (3rd great-grandson)
// calculate_nephews     - calculates the degree of relations (grand-niece)
// calculate_uncles      - calculates the degree of relations (4th great-grand-uncle)
// calculate_cousins     - calculates the degree of relations (2nd cousin twice removed)
// search_marital        - if no direct blood relation found, searches for relation between spouses of person X and Y
// search_bloodrel       - searches for blood relationship between X and Y
// display               - displays the result of comparison checks
// display_table         - displays simple chart showing the found relationship
// unset_var             - unsets the vital variables before searching marital relations
// getperson             - retrieves person from MySQL database by gedcom nr
// dutch_ancestor        - special algorithm to process complicated dutch terminology for distant ancestors
//
// the meaning of the value of the $table variable (for displaying table with lineage if a match is found):
// 1 = parent - child
// 2 = child - parent
// 3 = uncle - nephew
// 4 = nephew - uncle
// 5 = cousin
// 6 = siblings
// 7 = spouses or self
//
// the meaning of the value of the $spouse variable (flagging type of relationship check):
// 0 = checks relation X vs Y
// 1 = checks relation spouse of X versus person Y
// 2 = checks relation person X versus spouse of Y
// 3 = checks relation spouse of X versus spouse of Y
//
// values in the genarray:
// the genarray is an array of the ancestors of a base person (one of the two persons entered in the search or their spouses)
// genarray[][0] = gedcom number of the person
// genarray[][1] = number of generations (counted from base person)
// genarray[][2] = array number of child
//
// meaning of some other global variables:
// $doublespouse - flags situation where searched persons X and Y are both spouses of a third person
// $special_spouseX (and Y) - flags situation where the regular text "spouse of" has to be changed:
// ----- for example: "X is spouse of brother of Y" should become "X is sister-in-law of Y"
// $sexe, $sexe2 - the sex of persons X and Y
// person, $person2 - gedcom nr of the searched persons X and Y
//
//
//
//============================================================================================

function create_rel_array ($gednr)  {
// creates array of ancestors of person with gedcom nr. $gednr
global $db;

	$gezin_id=$gednr;

	$kwartier2[]	= $gezin_id;
	$kwartiernummer2[]=1;
	$huwelijksnummer2[]=0;
	$generatie      = 1;
	$genarray_count=0;

	// *** Loop gehele kwartierstaat ***
	while (isset($kwartier2[0])){
		unset($kwartier);
		$kwartier=$kwartier2;
		unset($kwartier2);

		unset($kwartiernummer);
		$kwartiernummer=$kwartiernummer2;
		unset($kwartiernummer2);

		unset($huwelijksnummer);
		$huwelijksnummer=$huwelijksnummer2;
		unset($huwelijksnummer2);

		// *** Loop per generatie ***
		$kwcount=count($kwartier);
		for ($i=0; $i<$kwcount; $i++) {

			if ($kwartier[$i]!='0'){
				$persoonmn=mysql_query("SELECT * FROM ".veilig($_SESSION['tree_prefix'])."person
					WHERE pers_gedcomnumber='".veilig($kwartier[$i])."'",$db);
				@$persoonmnDb=mysql_fetch_object($persoonmn);
				$man_cls = New persoon_cls;
				$man_cls->construct($persoonmnDb);
				$levendmn=$man_cls->privacy;

				if (strtolower($persoonmnDb->pers_sexe)=='m' AND $kwartiernummer[$i]>1){
					$gezin=mysql_query("SELECT * FROM ".veilig($_SESSION['tree_prefix'])."family
						WHERE fam_gedcomnumber='".veilig($huwelijksnummer[$i])."'",$db);
					@$gezinDb=mysql_fetch_object($gezin);

					// *** Privacy filter van de vrouw mee berekenen ***
					$persoonvr=mysql_query("SELECT * FROM ".veilig($_SESSION['tree_prefix'])."person
						WHERE pers_gedcomnumber='".veilig($gezinDb->fam_woman)."'",$db);
					@$persoonvrDb=mysql_fetch_object($persoonvr);
					$vrouw_cls = New persoon_cls;
					$vrouw_cls->construct($persoonvrDb);
					$levendvr=$vrouw_cls->privacy;

					//$gezinlevend=privacyhuwelijk($gezinDb, $levendmn, $levendvr);
					// *** Huwelijk verwerken met een class ***
					$huw_cls = New huwelijk_cls;
					$huw_cls->construct($gezinDb, $levendmn, $levendvr);
					$gezinlevend=$huw_cls->privacy;
				}

				//*** Gegevens person weergeven ***

				$genarray[$genarray_count][0]= $kwartier[$i];
				$genarray[$genarray_count][1]= $generatie-1;
				$genarray_count++; // increase by one

				// ==	ga na of er ouders zijn
				if ($persoonmnDb->pers_famc){
					$query_gezin	= "SELECT * FROM ".veilig($_SESSION['tree_prefix'])."family WHERE fam_gedcomnumber = '".$persoonmnDb->pers_famc."'";
					$result_gezin = mysql_query($query_gezin,$db);
					@$record_gezin = mysql_fetch_object($result_gezin);
					if ($record_gezin->fam_man){
						$kwartier2[] = $record_gezin->fam_man;
						$kwartiernummer2[]=(2*$kwartiernummer[$i]);
						$huwelijksnummer2[]=$persoonmnDb->pers_famc;
						$genarray[][2]= $genarray_count-1;
						// save array nr of child in parent array so we can build up ancestral line later
					}

					if ($record_gezin->fam_woman){
						$kwartier2[]= $record_gezin->fam_woman;
						$kwartiernummer2[]=(2*$kwartiernummer[$i]+1);
						$huwelijksnummer2[]=$persoonmnDb->pers_famc;
						$genarray[][2]= $genarray_count-1;
						// save array nr of child in parent array so we can build up ancestral line later
					}
				}
			}
		}	// loop per generatie
		$generatie++;
	}	// loop kwartierstaat

	return $genarray;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function compare_rel_array($arrX, $arrY, $spouce_flag) {

global $foundX_nr, $foundY_nr, $foundX_gen, $foundY_gen, $foundX_match, $foundY_match, $spouse;

	foreach($arrX as $keyx=>$valx) {
		foreach($arrY as $keyy=>$valy) {
			if($arrX[$keyx][0]==$arrY[$keyy][0]) {
					$foundX_match=$keyx;  // saves the array nr of common ancestor in ancestor array of X
					$foundY_match=$keyy;  // saves the array nr of common ancestor in ancestor array of Y
					if(isset($arrX[$keyx][2])) { $foundX_nr=$arrX[$keyx][2]; } // saves the array nr of the child leading to X
					if(isset($arrY[$keyy][2])) { $foundY_nr=$arrY[$keyy][2]; } // saves the array nr of the child leading to Y
					if(isset($arrX[$keyx][1])) { $foundX_gen=$arrX[$keyx][1]; }// saves the nr of generations common ancestor is removed from X
					if(isset($arrY[$keyy][1])) { $foundY_gen=$arrY[$keyy][1]; }// saves the nr of generations common ancestor is removed from Y
					$spouse=$spouce_flag; // saves global variable flagging if we're comparing X - Y or spouse combination
					return;
			}
		}
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function calculate_rel ($arr_x, $arr_y, $genX, $genY) {
// calculates the relationship found: "X is 2nd cousin once removed of Y"
global $reltext, $sexe, $spouse, $special_spouseY, $special_spouseX, $doublespouse, $table, $language;

	$doublespouse=0;
	if ( $arr_x == 0 AND $arr_y == 0 ) {  // self
		$reltext = $language["rel_identical"];
		if($spouse==1 OR $spouse==2) { $reltext = " "; }
		if($spouse==3) {$doublespouse=1; }
		// it's the spouse itself so text should be "X is spouse of Y", not "X is spouse of is identical to Y" !!
		$table=7;
	}
	elseif ( $arr_x == 0 AND $arr_y > 0 ) {  // x is ancestor of y
		$table=1;
		calculate_ancestor ($genY);
	}
	elseif ( $arr_y == 0 AND $arr_x > 0 ) {  // x is descendant of y
		$table=2;
		calculate_descendant ($genX);
	}
	elseif ( $genX == 1 AND $genY == 1 ) {  // x is brother of y

		$table=6;
		if($sexe=='m') {
				$reltext = $language["rel_brother"];
				if($spouse==1) { $reltext = $language["rel_sisinlaw"]; $special_spouseX=1;}  //comparing spouse of X with Y
				if($spouse==2 OR $spouse==3) { $reltext =  $language["rel_brothinlaw"]; $special_spouseY=1;} //comparing X with spouse of Y or comparing 2 spouses
				//$special_spouseX flags not to enter "spouse of" for X in display function
				//$special_spouseY flags not to enter "spouse of" for Y in display function
		}
		else {
				$reltext = $language["rel_sister"];
				if($spouse==1) { $reltext =  $language["rel_brothinlaw"]; $special_spouseX=1;}  //comparing spouse of X with Y
				if($spouse== 2 OR $spouse==3) { $reltext =  $language["rel_sisinlaw"]; $special_spouseY=1;}  //comparing X with spouse of Y or comparing 2 spouses
				//$special_spouseX flags not to enter "spouse of" for X in display function
				//$special_spouseY flags not to enter "spouse of" for Y in display function
		}
	}
	elseif ( $genX == 1 AND $genY > 1 ) {  // x is uncle, great-uncle etc of y
		$table=3;
		calculate_uncles ($genY);
	}
	elseif ( $genX > 1 AND $genY == 1 ) {  // x is nephew, great-nephew etc of y
		$table=4;
		calculate_nephews ($genX);
	}
	else {  // x and y are cousins of any number (2nd, 3rd etc) and any distance removed (once removed, twice removed etc)
		$table=5;
		calculate_cousins ($genX, $genY);
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function spanish_degrees ($pers, $text) {
	   global $spantext, $language;
		if ($pers == 2) { $spantext = $text; }	
		if ($pers == 3) { $spantext = $language["rel_es_2"].$text; }				
		if ($pers == 4) { $spantext = $language["rel_es_3"].$text; }					
		if ($pers == 5) { $spantext = $language["rel_es_4"].$text; }	
		if ($pers == 6) { $spantext = $language["rel_es_5"].$text; }	
		if ($pers == 7) { $spantext = $language["rel_es_6"].$text; }
		if ($pers == 8) { $spantext = $language["rel_es_7"].$text; }
		if ($pers == 9) { $spantext = $language["rel_es_8"].$text; }
		if ($pers == 10) { $spantext = $language["rel_es_9"].$text; }
		if ($pers == 11) { $spantext = $language["rel_es_10"].$text; }
		if ($pers == 12) { $spantext = $language["rel_es_11"].$text; }
		if ($pers == 13) { $spantext = $language["rel_es_12"].$text; }
		if ($pers == 14) { $spantext = $language["rel_es_13"].$text; }
		if ($pers == 15) { $spantext = $language["rel_es_14"].$text; }
		if ($pers == 16) { $spantext = $language["rel_es_15"].$text; }
		if ($pers == 17) { $spantext = $language["rel_es_16"].$text; }
		if ($pers == 18) { $spantext = $language["rel_es_17"].$text; }
		if ($pers == 19) { $spantext = $language["rel_es_18"].$text; }
		if ($pers == 20) { $spantext = $language["rel_es_19"].$text; }
		if ($pers == 21) { $spantext = $language["rel_es_20"].$text; }
		if ($pers == 22) { $spantext = $language["rel_es_21"].$text; }
		if ($pers == 23) { $spantext = $language["rel_es_22"].$text; }
		if ($pers == 24) { $spantext = $language["rel_es_23"].$text; }
		if ($pers == 25) { $spantext = $language["rel_es_24"].$text; }
		if ($pers == 26) { $spantext = $language["rel_es_25"].$text; }	
}	
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function calculate_ancestor($pers) {
global $reltext, $sexe, $spouse, $special_spouseY, $language, $ancestortext, $dutchtext, $Taalkeuze, $spantext;

	$anscestortext='';
	if($sexe=='m') {
		$parent = $language["rel_father"];
	}
	else {
		$parent = $language["rel_mother"];
	}

	if ($pers == 1) {
		if($spouse==2 OR $spouse==3) {
			$special_spouseY=1; // prevents "spouse of Y" in output
			if($parent==$language["rel_father"]) { $parent=$language["rel_fathinlaw"]; }
			else { $parent=$language["rel_mothinlaw"];}
		}
		$reltext = $parent.$language["rel_of"];
	}
	else {
		if($Taalkeuze=="nl") {
			dutch_ancestors($pers);
			$reltext= $ancestortext.$parent.$language["rel_of"];
			if ($pers >4 ) {
					$gennr=$pers-2;
					$dutchtext =  "(".$ancestortext.$parent." = ".$gennr.$language["rel_th"].$language["rel_gr_grandpar"].$parent.")";
			}

		}
		elseif($Taalkeuze=="es") {
			if($parent==$language["rel_father"]) { $grparent=$language["rel_grandfather"]; $spanishnumber="o"; }
			else {$grparent=$language["rel_grandmother"]; $spanishnumber="a";}
			$gennr=$pers-1;
			$degree=$gennr.$spanishnumber." ".$grparent;
			if ($pers == 2) { $reltext = $grparent.$language["rel_of"]; }
			elseif($pers >2 AND $pers <27) {
				spanish_degrees($pers,$grparent); // sets spanish "bis", "tris" etc prefix
				$reltext = $spantext." (".$degree.")".$language["rel_of"];
			}
			else { $reltext = $degree.$language["rel_of"]; }
		}
		elseif($Taalkeuze=="he") {
			if($parent==$language["rel_father"]) { $grparent=$language["rel_grandpar"]; $grgrparent=$language["rel_gr_grandpar"]; }
			else {$grparent=$language["rel_grand"];  $grgrparent=$language["rel_great-grand"];  }
			$gennr=$pers-2;			 
			if ($pers == 2) { $reltext = $grparent.$language["rel_of"]; }
			elseif ($pers > 2) { 
				$degree='';
				if($pers >3) { $degree=$language["rel_degree"].$gennr; }
				$reltext = $grgrparent.$degree.$language["rel_of"];			
			}			 
		}
		else { // andere talen
			if ($pers == 2) { $reltext = $language["rel_grandpar"].$parent.$language["rel_of"]; }
			if ($pers == 3) { $reltext = $language["rel_gr_grandpar"].$parent.$language["rel_of"]; }
			if ($pers == 4) { $reltext = $language["rel_2nd"].$language["rel_gr_grandpar"].$parent.$language["rel_of"]; }
			if ($pers == 5) { $reltext = $language["rel_3rd"].$language["rel_gr_grandpar"].$parent.$language["rel_of"]; }
			$gennr=$pers-2;
			if ($pers >  5) { $reltext = $gennr.$language["rel_th"].$language["rel_gr_grandpar"].$parent.$language["rel_of"]; }
		}
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function dutch_ancestors($gennr) {
		global $ancestortext;
		global $hoog, $opper, $aarts, $voor, $edel, $stam, $oud, $rest;

		if ($gennr > 512){
				$text = " Neanthertaler ancestor of ";
		}
		else {
				if ($gennr > 256) {
						$hoog="hoog-";
						$gennr-=256;
						dutch_ancestors($gennr);
				}
				elseif ($gennr > 128) {
						$opper="opper-";
						$gennr-=128;
						dutch_ancestors($gennr);
				}
				elseif ($gennr > 64) {
						$aarts="aarts-";
						$gennr-=64;
						dutch_ancestors($gennr);
				}
				elseif ($gennr > 32) {
						$voor="voor-";
						$gennr-=32;
						dutch_ancestors($gennr);
				}
				elseif ($gennr > 16) {
						$edel="edel-";
						$gennr-=16;
						dutch_ancestors($gennr);
				}
				elseif ($gennr > 8) {
						$stam="stam-";
						$gennr-=8;
						dutch_ancestors($gennr);
				}
				elseif ($gennr > 4) {
						$oud="oud";
						$gennr-=4;
						dutch_ancestors($gennr);
				}
				else {
						if ($gennr==4) { $rest='betovergroot'; }
						if ($gennr==3) { $rest='overgroot'; }
						if ($gennr==2) { $rest='groot'; }
						if ($gennr==1) { $rest=''; }
				}
		}
		$ancestortext= $hoog.$opper.$aarts.$voor.$edel.$stam.$oud.$rest;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function calculate_descendant($pers) {
global $reltext,  $sexe, $spouse, $special_spouseX, $language, $Taalkeuze, $spantext;

	if($sexe=='m') {
		$child = $language["rel_son"];
	}
	else {
		$child = $language["rel_daughter"];
	}
	if ($pers == 1) {
		if($spouse==1) {
			if($child==$language["rel_son"]) { $child=$language["rel_daughtinlaw"]; }
			else { $child=$language["rel_soninlaw"];}
			$special_spouseX=1;
		}
		$reltext = $child.$language["rel_of"];
	}
	elseif($Taalkeuze=="es") {
		if($child == $language["rel_son"]) { $grchild=$language["rel_grandson"]; $spanishnumber="o"; }
		else {$grchild=$language["rel_granddaughter"]; $spanishnumber="a";}
		$gennr=$pers-1;
		$degree=$gennr.$spanishnumber." ".$grchild;
		if ($pers == 2) { $reltext = $grchild.$language["rel_of"]; }
		elseif($pers >2 AND $pers <27) {
			spanish_degrees($pers,$grchild); // sets spanish "bis", "tris" etc prefix
			$reltext = $spantext." (".$degree.")".$language["rel_of"];
		}
		else { $reltext = $degree.$language["rel_of"]; }
	}
	elseif($Taalkeuze=="he") {
		if($child==$language["rel_son"]) { $grchild=$language["rel_grandson"]; $grgrchild=$language["rel_gr_grandson"]; }
		else {$grchild=$language["rel_granddaughter"];  $grgrchild=$language["rel_gr_granddaughter"]; }
		$gennr=$pers-2;			 
		if ($pers == 2) { $reltext = $grchild.$language["rel_of"]; }
		elseif ($pers > 2) { 
			$degree='';
			if($pers >3) { $degree=$language["rel_degree"].$gennr; }
			$reltext = $grgrchild.$degree.$language["rel_of"];			
		}			 
	}
	else {
		if ($pers == 2) { $reltext = $language["rel_grand"].$child.$language["rel_of"]; }
		if ($pers == 3) { $reltext = $language["rel_great-grand"].$child.$language["rel_of"]; }
		if ($pers == 4) { $reltext = $language["rel_2nd"].$language["rel_great-grand"].$child.$language["rel_of"]; }
		if ($pers == 5) { $reltext = $language["rel_3rd"].$language["rel_great-grand"].$child.$language["rel_of"]; }
		$gennr=$pers-2;
		if ($pers >  5) { $reltext = $gennr.$language["rel_th"].$language["rel_great-grand"].$child.$language["rel_of"]; }
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function calculate_nephews($generX) { // handed generations x is removed from common ancestor
global $reltext,  $sexe, $language, $spantext, $Taalkeuze;


	if($Taalkeuze=="es"){
		if($sexe=="m") { $neph=$language["rel_nephew"]; $span_postfix="o "; $grson=$language["rel_grandson"]; }
		else { $neph=$language["rel_niece"]; $span_postfix="a "; $grson=$language["rel_granddaughter"]; }	
		$gendiff = abs($generX - $generY);
		$gennr=$gendiff-1;
		$degree=$grson." ".$gennr.$span_postfix;
		if($gendiff ==1) { $reltext=$neph.$language["rel_of"];}	
		elseif($gendiff > 1 AND $gendiff < 27) { 
			spanish_degrees($gendiff,$grson);
			$reltext=$neph." ".$spantext.$language["rel_of"];				
		}
 		else { $reltext=$neph." ".$degree; }
	}
	elseif($Taalkeuze=="he"){
		if($sexe=='m') { $nephniece = $language["rel_nephew"]; }
		else { $nephniece = $language["rel_niece"]; }
		$gendiff = abs($generX - $generY);
		$gennr=$gendiff-1;
		if($gendiff ==1) { $reltext=$nephniece.$language["rel_of"];}	
		elseif($gendiff > 1 ) {  	
			if($gendiff >2) { $degree=$language["rel_degree"].$gennr; }		
			$reltext=$nephniece.$degree.$language["rel_of"]; 				
		}		 
	}
	else {
		if($sexe=='m') {
			$nephniece = $language["rel_nephew"];
		}
		else {
			$nephniece = $language["rel_niece"];
		}
		if ($generX == 2) { $reltext = $nephniece.$language["rel_of"]; }
		if ($generX == 3) { $reltext = $language["rel_grandnef"].$nephniece.$language["rel_of"]; }
		if ($generX == 4) { $reltext = $language["rel_gr_grandnef"].$nephniece.$language["rel_of"]; }
		if ($generX == 5) { $reltext = $language["rel_2nd"].$language["rel_gr_grandnef"].$nephniece.$language["rel_of"]; }
		if ($generX == 6) { $reltext = $language["rel_3nd"].$language["rel_gr_grandnef"].$nephniece.$language["rel_of"]; }
		$gennr=$generX-3;
		if ($generX >  6) { $reltext = $gennr.$language["rel_th"].$language["rel_gr_grandnef"].$nephniece.$language["rel_of"]; }
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function calculate_uncles($generY) { // handed generations y is removed from common ancestor
global $reltext,  $sexe, $language, $ancestortext, $dutchtext, $Taalkeuze, $spantext;
	$ancestortext='';
	if($sexe=='m') {
		$uncleaunt = $language["rel_uncle"];
		}
	else {
		$uncleaunt = $language["rel_aunt"];
	}

	if($Taalkeuze=="nl") {
		dutch_ancestors($generY-1);
		$reltext= $ancestortext.$uncleaunt.$language["rel_of"];
		if ($generY >4 ) {
			$gennr = $generY -3;
			$dutchtext =  "(".$ancestortext.$uncleaunt." = ".$gennr.$language["rel_th"].$language["rel_gr_grandpar"].$uncleaunt.")";
		}

	}
	elseif($Taalkeuze=="es"){
		if($sexe=="m") { $uncle=$language["rel_uncle"]; $span_postfix="o "; $gran=$language["rel_grandfather"]; }
		else { $uncle=$language["rel_aunt"]; $span_postfix="a "; $gran=$language["rel_grandmother"]; }	
		$gendiff = abs($generX - $generY);
		$gennr=$gendiff-1;
 		$degree=$gran." ".$gennr.$span_postfix;
		if($gendiff ==1) { $reltext=$uncle.$language["rel_of"];}	
		elseif($gendiff > 1 AND $gendiff < 27) {  
			spanish_degrees($gendiff,$gran);
			$reltext=$uncle." ".$spantext.$language["rel_of"];				
		}
		else {$reltext=$uncle." ".$degree; }
	}
	elseif($Taalkeuze=="he"){
		$gendiff = abs($generX - $generY);
		$gennr=$gendiff-1;
		if($gendiff ==1) { $reltext=$uncleaunt.$language["rel_of"];}	
		elseif($gendiff > 1 ) {  	
			if($gendiff >2) { $degree=$language["rel_degree"].$gennr; }		
			$reltext=$uncleaunt.$degree.$language["rel_of"]; 				
		}		 
	}
	else {
		if ($generY == 2) { $reltext = $uncleaunt." of "; }
		if ($generY == 3) { $reltext = $language["rel_grandpar"].$uncleaunt." of "; }
		if ($generY == 4) { $reltext = $language["rel_gr_grandpar"].$uncleaunt." of "; }
		if ($generY == 5) { $reltext = $language["rel_2nd"].$language["rel_gr_grandpar"].$uncleaunt." of "; }
		if ($generY == 6) { $reltext = $language["rel_3rd"].$language["rel_gr_grandpar"].$uncleaunt." of "; }
		$gennr=$generY-2;
		if ($generY >  6) { $reltext = $gennr.$language["rel_th"].$language["rel_gr_grandpar"].$uncleaunt." of "; }
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function calculate_cousins ($generX, $generY) {
global $reltext, $famsX, $famsY, $language, $sexe, $Taalkeuze, $spantext;

	if($Taalkeuze=="es") {
		$gendiff = abs($generX - $generY);
		
		if ($gendiff == 0) { 
			if($sexe=="m") { $cousin=$language["rel_cousinmale"]; $span_postfix="o "; $sibling=$language["rel_1stcousin"]; }
			else { $cousin=$language["rel_cousinfemale"]; $span_postfix="a "; $sibling=$language["rel_1stcousinfemale"];}
			if($generX==2) { $reltext=$cousin." ".$sibling.$language["rel_of"];}
			elseif($generX > 2) { $degree=$generX-1; $reltext = $cousin." ".$degree.$span_postfix.$language["rel_of"]; }
		}
		elseif($generX < $generY) { 
			if($sexe=="m") { $uncle=$language["rel_uncle"]; $span_postfix="o "; $gran=$language["rel_grandfather"]; }
			else { $uncle=$language["rel_aunt"]; $span_postfix="a "; $gran=$language["rel_grandmother"]; }
			
			if($gendiff ==1) { $relname=$uncle;}			
			elseif($gendiff>1 AND $gendiff <27) { 
				spanish_degrees($gendiff,$gran);
				$relname=$uncle." ".$spantext;				
			}
			else { }
			$reltext= $relname." ".$generX.$span_postfix.$language["rel_of"];
		}
		else {
			if($sexe=="m") { $nephew=$language["rel_nephew"]; $span_postfix="o ";$grson=$language["rel_grandson"]; }
			else { $nephew=$language["rel_niece"]; $span_postfix="a "; $grson=$language["rel_granddaughter"]; }
			
			if($gendiff ==1) { $relname=$nephew;}
			else { 
				spanish_degrees($gendiff,$grson);
				$relname=$nephew." ".$spantext;				
			}			
			$reltext=$relname." ".$generY.$span_postfix.$language["rel_of"];			
		}
	}
	elseif($Taalkeuze=="he") {
		if($sexe=='m') { $cousin=$language["rel_cousinmale"]; }
		else { $cousin=$language["rel_cousinfemale"];}
		$gendiff = abs($generX - $generY);
		if ($gendiff == 0) { $removenr = ""; }
		elseif ($gendiff==1) { $removenr = $language["rel_removed"].$language["rel_onceremoved"]; }
		else {
			$removenr=$language["rel_removed"].$gendiff." ".$language["rel_timesremoved"];
		}
		$degree='';
		$degreediff = min($generX,$generY);
		if($degreediff > 2) {
			$degree=$language["rel_degree"].($degreediff-1)." ";
		}
		$reltext=$cousin.$degree.$removenr.$language["rel_of"];
	} 
	else {	
		$gendiff = abs($generX - $generY);
		if ($gendiff == 0) { $removenr = ""; }
		elseif ($gendiff == 1 ) { $removenr = $language["rel_onceremoved"]; }
		elseif ($gendiff == 2 ) { $removenr = $language["rel_twiceremoved"]; }
		elseif ($gendiff > 2 ) { $removenr = $gendiff.$language["rel_timesremoved"]; }
	
		$degreediff = min($generX,$generY);
		if($degreediff == 2) { $degree = $language["rel_1stcousin"]; }
		if($degreediff == 3) { $degree = $language["rel_2ndcousin"]; }
		if($degreediff == 4) { $degree = $language["rel_3rdcousin"]; }
	
		if($sexe=='m') {
			$cousin=$language["rel_cousinmale"];
			if($Taalkeuze=="nl" AND $degreediff==2 AND $gendiff==0) { $cousin=$language["rel_nephew"]; }
			if($Taalkeuze=="nl" AND $generY==2 AND $gendiff!=0) { $cousin="volle ".$language["rel_nephew"]; }
			if($Taalkeuze=="nl" AND $generX==2 AND $gendiff!=0) { $cousin=$language["rel_nephew"]; }
		}
		else {
			$cousin=$language["rel_cousinfemale"];
			if($Taalkeuze=="nl" AND $degreediff==2 AND $gendiff==0) { $cousin=$language["rel_niece"]; }
			if($Taalkeuze=="nl" AND $generY==2 AND $gendiff!=0) { $cousin="volle ".$language["rel_niece"]; }
			if($Taalkeuze=="nl" AND $generX==2 AND $gendiff!=0) { $cousin=$language["rel_niece"]; }
		}

		if($degreediff > 4)  {
			$degreediff-=1;
			if($Taalkeuze=="nl") { $degreediff--;} // 5th cousin is in dutch "4de achterneef"
			$degree = $degreediff.$language["rel_th"];
		}
	
		$reltext = $degree.' '.$cousin.' '.$removenr.$language["rel_of"];
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function search_marital() {
global $db, $famsX, $famsY, $famspouseX, $famspouseY, $rel_arrayX, $rel_arrayY, $foundX_nr, $foundY_nr, $foundX_gen, $foundY_gen;
global $sexe, $sexe2, $spousenameX, $spousenameY, $foundX_match, $foundY_match;
global $rel_arrayspouseX, $rel_arrayspouseY, $spouse;

	$marrX = explode(";",$famsX);
	$marrY = explode(";",$famsY);
	if($famsX!='') {
					$marrcount=count($marrX);
		for($x=0; $x<$marrcount; $x++) {

			$gezin=mysql_query("SELECT fam_man, fam_woman FROM ".veilig($_SESSION['tree_prefix'])."family WHERE fam_gedcomnumber='".$marrX[$x]."'",$db);
			@$gezinDb=mysql_fetch_object($gezin) or die("Geen geldig marital 1a gezinsnummer.");

			if($sexe=='f') {
				$thespouse=$gezinDb->fam_man;
			}
			else {
				$thespouse=$gezinDb->fam_woman;
			}

			$rel_arrayspouseX = create_rel_array($thespouse);

				if(isset($rel_arrayspouseX)) {
					compare_rel_array ($rel_arrayspouseX, $rel_arrayY, 1); // "1" flags comparison with "spouse of X"
				}

			if($foundX_match !=='') {

				$famspouseX=$marrX[$x];

				if($sexe=='m') {$sexe="f";} else {$sexe="m";} // we have to switch sex since the spouse is the relative!
					calculate_rel ($foundX_match, $foundY_match, $foundX_gen, $foundY_gen);

				$spouseidDb=getperson($thespouse);
				$spousenameX=$spouseidDb->pers_firstname." ".$spouseidDb->pers_lastname;

				break;
			}
		}
	}

	if($foundX_match==='' AND $famsY!='') {  // no match found between "spouse of X" and "Y", let's try "X" with "spouse of "Y"
					$ymarrcount=count($marrY);
		for($x=0; $x<$ymarrcount; $x++) {

			$gezin=mysql_query("SELECT fam_man, fam_woman FROM ".veilig($_SESSION['tree_prefix'])."family WHERE fam_gedcomnumber='".$marrY[$x]."'",$db);
			@$gezinDb=mysql_fetch_object($gezin) or die("Geen geldig marital 2a gezinsnummer.");

			if($sexe2=='f') {
				$thespouse2=$gezinDb->fam_man;
			}
			else {
				$thespouse2=$gezinDb->fam_woman;
			}

			$rel_arrayspouseY = create_rel_array($thespouse2);

			if(isset($rel_arrayspouseY)) {
				compare_rel_array ($rel_arrayX, $rel_arrayspouseY, 2); // "2" flags comparison with "spouse of Y"
			}
			if($foundX_match !=='') {

				$famspouseY=$marrY[$x];

				calculate_rel ($foundX_match, $foundY_match, $foundX_gen, $foundY_gen);

				$spouseidDb=getperson($thespouse2);
				$spousenameY=$spouseidDb->pers_firstname." ".$spouseidDb->pers_lastname;

				break;
			}
		}
	}

	if($foundX_match==='' AND $famsX!='' AND $famsY!='') { // still no matches, let's try comparison of "spouse of X" with "spouse of Y"
		$xmarrcount=count($marrX);
		$ymarrcount=count($marrY);
		for($x=0; $x<$xmarrcount; $x++) {
			for($y=0; $y<$ymarrcount; $y++) {

				$gezin=mysql_query("SELECT fam_man, fam_woman FROM ".veilig($_SESSION['tree_prefix'])."family WHERE fam_gedcomnumber='".$marrX[$x]."'",$db);
				@$gezinDb=mysql_fetch_object($gezin) or die("Geen geldig newmarital 1a gezinsnummer.");

			if($sexe=='f') {
					$thespouse=$gezinDb->fam_man;
				}
				else {
					$thespouse=$gezinDb->fam_woman;
				}

				$rel_arrayspouseX = create_rel_array($thespouse);

				$gezin=mysql_query("SELECT fam_man, fam_woman FROM ".veilig($_SESSION['tree_prefix'])."family WHERE fam_gedcomnumber='".$marrY[$y]."'",$db);
				@$gezinDb=mysql_fetch_object($gezin) or die("Geen geldig newmarital 2a gezinsnummer.");

				if($sexe2=='f') {
					$thespouse2=$gezinDb->fam_man;
				}
				else {
					$thespouse2=$gezinDb->fam_woman;
				}

				$rel_arrayspouseY = create_rel_array($thespouse2);

				if(isset($rel_arrayspouseX) AND isset($rel_arrayspouseY)) {
					compare_rel_array ($rel_arrayspouseX, $rel_arrayspouseY, 3); //"3" flags comparison "spouse of X" with "spouse of Y"
				}
				if($foundX_match !=='') {

					if($sexe=='m') {$sexe="f";} else {$sexe="m";} // we have to switch sex since the spouse is the relative!
					calculate_rel ($foundX_match, $foundY_match, $foundX_gen, $foundY_gen);

					$spouseidDb=getperson($thespouse);
					$spousenameX=$spouseidDb->pers_firstname." ".$spouseidDb->pers_lastname;

					$spouseidDb=getperson($thespouse2);
					$spousenameY=$spouseidDb->pers_firstname." ".$spouseidDb->pers_lastname;

					$famspouseX=$marrX[$x];
					$famspouseY=$marrY[$y];

					break;
				} //end if foundmatch !=''
			} // for y
				if($foundX_match !=='') {
					break;
				} // end if
		} // for x
	} // end if not found match

} //end function

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function search_bloodrel() {
global $rel_arrayX, $rel_arrayY, $person, $person2, $foundX_match, $foundY_match, $reltext, $foundX_gen, $foundY_gen;
			unset_vars();
	$rel_arrayX = create_rel_array( $person ); // === gedcom nr of person X ===
	$rel_arrayY = create_rel_array( $person2 ); // === gedcom nr of person Y ===
	if(isset($rel_arrayX) AND isset($rel_arrayY)) {
		compare_rel_array ($rel_arrayX, $rel_arrayY, 0);
	}

	if ($foundX_match !=='') {
		calculate_rel ($foundX_match, $foundY_match, $foundX_gen, $foundY_gen);
	}
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function unset_vars() {
global $foundX_nr, $foundY_nr, $foundX_gen, $foundY_gen, $foundX_match, $foundY_match, $reltext, $spouse, $table;

	$foundX_nr='';
	$foundY_nr='';
	$foundX_gen='';
	$foundY_gen='';
	$foundX_match='';
	$foundY_match='';
	$table='';
	$reltext='';
	$spouse='';
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function getperson($gednr) {
global $db;

	$person=mysql_query("SELECT * FROM ".veilig($_SESSION['tree_prefix'])."person WHERE pers_gedcomnumber='".$gednr."'",$db);
	@$personDb=mysql_fetch_object($person) or die("Geen geldig getperson gezinsnummer.");

	return $personDb;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function display () {
global $foundX_match, $reltext, $bloodreltext, $name1, $name2, $spouse, $rel_arrayspouseX;
global $special_spouseY, $special_spouseX, $spousenameX, $spousenameY, $table, $doublespouse, $db;
global $rel_arrayX, $rel_arrayY, $famX, $famY, $language, $dutchtext, $persoon, $persoon2;
global $sexe, $Taalkeuze, $dirmark1;

	if($Taalkeuze=="he") {
		if($sexe=="m") { $language["rel_is"]=$language["rel_heb_is_male"]; }
		else { $language["rel_is"]=$language["rel_heb_is_female"]; }
	}

	$bloodrel='';
	search_bloodrel();

	if($reltext) {
		print '<table class="humo container"><tr><td>';
					$bloodrel=1;
		print $language["rel_bloodrel"]."<br><br>";
		print "&nbsp;&nbsp;<a class='relsearch' href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$famX."&amp;hoofdpersoon=".$rel_arrayX[0][0]."'>";
		print $name1."</a>".$language["rel_is"].$reltext;
		print "<a class='relsearch' href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$famY."&amp;hoofdpersoon=".$rel_arrayY[0][0]."'>".$name2."</a><p>";
		print $dutchtext;
		print '<hr style="width:100%;height:0.25em;color:darkblue;background-color:darkblue;"  /><br>';
		$bloodreltext=$reltext;
		display_table();
	}

	if($table!=1 AND $table!=2 AND $table!=7) {
			unset_vars();
			search_marital();

		if($reltext) {

			if($bloodrel==1) { print '</td><td>'; }
			else { echo '<table class="humo container"<tr><td>'; }

			print $language["rel_marrel"]."<br><br>";
			$spousetext1=''; $spousetext2='';

			if($doublespouse==1) { // X and Y are both spouses of Z
				$spouseidDb=getperson($rel_arrayspouseX[$foundX_match][0]);
				$spousename=$spouseidDb->pers_firstname." ".$spouseidDb->pers_lastname;
				print "<span>&nbsp;&nbsp;<a class='relsearch' href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$famX."&amp;hoofdpersoon=".$rel_arrayX[0][0]."'>";
				//print $name1."</a> and ";
				print $name1."</a>".$language["rel_and"];
				print "<a class='relsearch' href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$famY."&amp;hoofdpersoon=".$rel_arrayY[0][0]."'>".$name2."</a>";


				if($persoon->pers_sexe == "M") {
					print $language["rel_bothhusbands"];
				}
				else {
					print $language["rel_bothwifes"];
				}

				print "<a href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$famY."&amp;hoofdpersoon=".$rel_arrayspouseX[$foundX_match][0]."'>".$spousename."</a></span><br>";
			}
			else {
				if(($spouse==1 AND $special_spouseX!==1) OR $spouse==3) {
					if($persoon->pers_sexe=='M') {
						$spousetext1 = $language["rel_husbandof"];
					}
					else {
						$spousetext1 = $language["rel_wifeof"];
					}
				}
				if(($spouse==2 OR $spouse==3) AND $special_spouseY!==1) {
					if($persoon2->pers_sexe=='M') {
						$spousetext2 = $language["rel_wifeof"];
					}
					else {
						$spousetext2 = $language["rel_husbandof"];
					}
										}
				print "<span>&nbsp;&nbsp;<a class='relsearch' href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$famX."&amp;hoofdpersoon=".$rel_arrayX[0][0]."'>";
				//print $name1."</a> is ".$spousetext1.$reltext.$spousetext2;
				print $name1."</a>".$language["rel_is"].$spousetext1.$reltext.$spousetext2;
				print "<a class='relsearch' href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$famY."&amp;hoofdpersoon=".$rel_arrayY[0][0]."'>".$name2."</a></span><br>";

					// ==================================================
					// this is additional text to explain marriage relations. maybe unnecessary since we got a chart.....

				if(($spouse==1 AND $reltext!=' ') OR $spouse==3) {
					if($persoon->pers_sexe=='M') {
						print "&nbsp;&nbsp;&nbsp;".$dirmark1."(".$name1.$language["rel_is"].$language["rel_husbandof"].$spousenameX.")<br>";
					}
					else {
						print "&nbsp;&nbsp;&nbsp;".$dirmark1."(".$name1.$language["rel_is"].$language["rel_wifeof"].$spousenameX.")<br>";
					}
				}
				if($spouse==2) {
					$rel=$reltext;
					if($reltext==$language["rel_sisinlaw"]) { $rel=$language["rel_sister"];}
					if($reltext==$language["rel_brothinlaw"]) { $rel=$language["rel_brother"];}
					if($reltext==$language["rel_fathinlaw"].$language["rel_of"]) { $rel=$language["rel_father"].$language["rel_of"];}
					if($reltext==$language["rel_mothinlaw"].$language["rel_of"]) { $rel=$language["rel_mother"].$language["rel_of"];}
					if($reltext==$language["rel_soninlaw"]) { $rel=$language["rel_son"];}
					if($reltext==$language["rel_daughtinlaw"]) { $rel=$language["rel_daughter"];}
					print "&nbsp;&nbsp;&nbsp;".$dirmark1."(".$name1.$language["rel_is"].$rel.$spousenameY.")<br>";
				}
				if($spouse==3) {
					if($Taalkeuze=="he") { // switch gender
						if($language["rel_is"]==$language["rel_heb_is_female"]) { $language["rel_is"]=$language["rel_heb_is_male"]; }
						else { $language["rel_is"]=$language["rel_heb_is_female"]; }
					}
					$rel=$reltext;
					if($reltext==$language["rel_sisinlaw"]) { $rel=$language["rel_sister"];}
					if($reltext==$language["rel_brothinlaw"]) { $rel=$language["rel_brother"];}
					if($reltext==$language["rel_fathinlaw"]) { $rel=$language["rel_fathinlaw"];}
					if($reltext==$language["rel_mothinlaw"]) { $rel=$language["rel_mothinlaw"];}
					if($reltext==$language["rel_soninlaw"]) { $rel=$language["rel_son"];}
					if($reltext==$language["rel_daughtinlaw"]) { $rel=$language["rel_daughter"];}
					print "&nbsp;&nbsp;&nbsp;".$dirmark1."(".$spousenameX.$language["rel_is"].$rel.$spousenameY.")<br>";
				}
				//======================= end additional text
			}

			print '<hr style="width:100%;height:0.25em;color:darkblue;background-color:darkblue;" /><br>';
			display_table();
		}
	}

	if($bloodreltext=='' AND $reltext=='') {
		print "<br>&nbsp;&nbsp;<span style='font-size:120%'>".$language["rel_norelsfound"]."</span><br>";
	}
	else { print '</td></tr></table>'; }
				print '<br><br>';

}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

function display_table() {
global $db, $foundX_nr, $foundY_nr, $foundX_gen, $foundY_gen, $foundX_match, $foundY_match;
global $table, $name1, $name2, $rel_arrayX, $rel_arrayY, $spouse, $rel_arrayspouseX, $rel_arrayspouseY, $famspouseX, $famspouseY;
global $famX, $famY, $gednr, $gednr2, $dirmark1, $dirmark2;

	if($table==1 OR $table==2) {
		if($table==1 AND $foundY_gen==1 AND $spouse=='') {
			// father-son - no need for table
		}
		else if($table==2 AND $foundX_gen==1 AND $spouse=='') {
			// son-father - no need for table
		}
		else {
			if($spouse==1) { $rel_arrayX = $rel_arrayspouseX;}
			if($spouse==2) { $rel_arrayY = $rel_arrayspouseY;}
			if($spouse==3) { $rel_arrayX = $rel_arrayspouseX; $rel_arrayY = $rel_arrayspouseY;}

			if($table==2) {
				$tempfound=$foundY_nr; $foundY_nr=$foundX_nr; $foundX_nr=$tempfound;
				$temprel=$rel_arrayY; $rel_arrayY=$rel_arrayX; $rel_arrayX=$temprel;
				$tempname=$name1;  $name1=$name2; $name2=$tempname;
				$tempfam=$famspouseX;  $famspouseX=$famspouseY; $famspouseY=$tempfam;
				$tempfamily=$famX;  $famX=$famY; $famY=$tempfamily;
										$tempged=$gednr2;  $gednr2=$gednr; $gednr=$tempged;
			}
			print "<table id=\"reltable\" class=\"humo reltable\">";
			print "<tr>";

			if(($spouse==1 AND $table==1) OR ($spouse==2 AND $table==2) OR $spouse==3) {

				$persidDb=getperson($rel_arrayX[0][0]);
				$personname=$persidDb->pers_firstname." ".$persidDb->pers_lastname;

				print "<td><br><a href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$famspouseX."&amp;hoofdpersoon=".$rel_arrayX[0][0]."'>".$personname."</a></td>";

				print "<td>&nbsp;<br>&nbsp;".$dirmark1."x&nbsp;&nbsp;<a class='search' href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$famX."&amp;hoofdpersoon=".$gednr."'>".$name1."</a></td>";
			}
			else {
				print "<td><a class='search' href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$famX."&amp;hoofdpersoon=".$rel_arrayX[0][0]."'>".$name1."</a></td>";
				if(($spouse==1 AND $table==2) OR ($spouse==2 AND $table==1)) {
					print "<td>&nbsp;</td>";
				}
			}
			print "</tr>";
			$count=$foundY_nr;
			while($count!=0) {

				$persidDb=getperson($rel_arrayY[$count][0]);
				$personname=$persidDb->pers_firstname." ".$persidDb->pers_lastname;
				if($persidDb->pers_fams) {
					$fams=$persidDb->pers_fams;
					$tempfam=explode(";",$fams);
					$fam=$tempfam[0];
				}
				else {
					$fam=$persidDb->pers_famc;
				}
				print "<tr>";
				print "<td>|<br><a href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$fam."&amp;hoofdpersoon=".$persidDb->pers_gedcomnumber."'>".$personname."</a></td>";
				if($spouse==1 OR $spouse==2 OR $spouse==3) { print "<td>&nbsp;</td>"; }
				$count=$rel_arrayY[$count][2];
			}

			print "<tr>";

			if(($spouse==1 AND $table==2) OR ($spouse==2 AND $table==1) OR $spouse==3) {

				$persidDb=getperson($rel_arrayY[0][0]);
				$personname=$persidDb->pers_firstname." ".$persidDb->pers_lastname;
				print "<td>|<br><a href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$famspouseY."&amp;hoofdpersoon=".$rel_arrayY[0][0]."'>".$personname."</a></td>";
				print "<td>&nbsp;<br>&nbsp;x&nbsp;&nbsp;".$dirmark1."<a class='search' href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$famY."&amp;hoofdpersoon=".$gednr2."'>".$name2."</a></td>";
			}
			else {
				print "<td>|<br><a class='search' href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$famY."&amp;hoofdpersoon=".$rel_arrayY[0][0]."'>".$name2."</a></td>";
				if(($spouse==1 AND $table==1) OR ($spouse==2 AND $table==2)) {
					print "<td>&nbsp;</td>";
				}
			}
			print "</tr></table>";
		}
	}
	if($table==3 OR $table==4 OR $table==5 OR $table==6 ) {
		$rowcount=max($foundX_gen,$foundY_gen);
		$countX=$foundX_nr;
		$countY=$foundY_nr;
		$name1_done=0;
		$name2_done=0;

		$colspan=3;
		if($spouse==1) { $rel_arrayX = $rel_arrayspouseX;}
		if($spouse==2) { $rel_arrayY = $rel_arrayspouseY;}
		if($spouse==3) { $rel_arrayX = $rel_arrayspouseX; $rel_arrayY = $rel_arrayspouseY;}

		print "<table id=\"reltable\" class=\"humo reltable\">";

		$persidDb=getperson($rel_arrayX[$foundX_match][0]);
		$personname=$persidDb->pers_firstname." ".$persidDb->pers_lastname;
		print "<tr>";
		if($spouse==1 OR $spouse==3) {print "<td>&nbsp;</td>"; }
		if($persidDb->pers_fams) {
			$fams=$persidDb->pers_fams;
			$tempfam=explode(";",$fams);
			$fam=$tempfam[0];
		}
		else {
			$fam=$persidDb->pers_famc;
		}
		print "<td colspan=".$colspan."><a href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$fam."&amp;hoofdpersoon=".$persidDb->pers_gedcomnumber."'>".$personname."</a><br>";
					for($i=1;$i<16;$i++) { print "&#9472;"; }
					print "&#9524;";
					for($i=1;$i<16;$i++) { print "&#9472;"; }
					print "</td>";
		if($spouse==2 OR $spouse==3) {print "<td>&nbsp;</td>"; }
		print "</tr>";
		for($e=1; $e <= $rowcount; $e++) {
			if($countX!=0) {
				$persidDb=getperson($rel_arrayX[$countX][0]);
				$personname=$persidDb->pers_firstname." ".$persidDb->pers_lastname;
				print "<tr>";
				if($spouse==1 OR $spouse==3) {print "<td>&nbsp;</td>";  }
				if($persidDb->pers_fams) {
					$fams=$persidDb->pers_fams;
					$tempfam=explode(";",$fams);
					$fam=$tempfam[0];
				}
				else {
					$fam=$persidDb->pers_famc;
				}
				print "<td>|<br><a href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$fam."&amp;hoofdpersoon=".$persidDb->pers_gedcomnumber."'>".$personname."</a></td>";
				$countX=$rel_arrayX[$countX][2];
			}
			elseif($name1_done==0) {
				print "<tr>";
				if($spouse==1 OR $spouse==3) {
					$persidDb=getperson($rel_arrayX[0][0]);
					$personname=$persidDb->pers_firstname." ".$persidDb->pers_lastname;
					if($persidDb->pers_fams) {
						$fams=$persidDb->pers_fams;
						$tempfam=explode(";",$fams);
						$fam=$tempfam[0];
					}
					else {
						$fam=$persidDb->pers_famc;
					}
					print "<td>&nbsp;<br><a class='search' href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$famX."&amp;hoofdpersoon=".$gednr."'>".$name1."</a>&nbsp;&nbsp;".$dirmark1."x&nbsp;</td>";
					print "<td>|<br><a href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$fam."&amp;hoofdpersoon=".$persidDb->pers_gedcomnumber."'>".$personname."</a></td>";
				}
				else {
					print "<td>|<br><a class='search' href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$famX."&amp;hoofdpersoon=".$gednr."'>".$name1."</a></td>";
				}
				$name1_done=1;
			}
			else {
				print "<tr>";
				if($spouse==1 OR $spouse==3) {print "<td>&nbsp;</td>"; }
				print "<td>&nbsp;</td>";
			}
			if($countY!=0) {
				$persidDb=getperson($rel_arrayY[$countY][0]);
				$personname=$persidDb->pers_firstname." ".$persidDb->pers_lastname;
				print "<td width=30px>&nbsp;</td>";

				if($persidDb->pers_fams) {
					$fams=$persidDb->pers_fams;
					$tempfam=explode(";",$fams);
					$fam=$tempfam[0];
				}
				else {
					$fam=$persidDb->pers_famc;
				}
				print "<td>|<br><a href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$fam."&amp;hoofdpersoon=".$persidDb->pers_gedcomnumber."'>".$personname."</a></td>";
				if($spouse==2 OR $spouse==3) {print "<td>&nbsp;</td>";  }
				print "</tr>";
				$countY=$rel_arrayY[$countY][2];
			}
			elseif($name2_done==0) {
				if($spouse==2 OR $spouse==3) {
					$persidDb=getperson($rel_arrayY[0][0]);
					$personname=$persidDb->pers_firstname." ".$persidDb->pers_lastname;
					print "<td width=30px>&nbsp;</td>";

					if($persidDb->pers_fams) {
						$fams=$persidDb->pers_fams;
						$tempfam=explode(";",$fams);
						$fam=$tempfam[0];
					}
					else {
						$fam=$persidDb->pers_famc;
					}
					print "<td>|<br><a href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$fam."&amp;hoofdpersoon=".$persidDb->pers_gedcomnumber."'>".$personname."</a></td>";
					print "<td>&nbsp;<br>&nbsp;x&nbsp;&nbsp;".$dirmark1."<a class='search' href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$famY."&amp;hoofdpersoon=".$gednr2."'>".$name2."</a></td>";
				}
				else {
					print "<td width=30px>&nbsp;</td>";
					print "<td>|<br><a class='search' href='gezin.php?database=".veilig($_SESSION['tree_prefix'])."&amp;id=".$famY."&amp;hoofdpersoon=".$gednr2."'>".$name2."</a></td>";
				}
				print "</tr>";
				$name2_done=1;
			}
			else {
				print "<td width=30px>&nbsp;</td>";
				print "<td>&nbsp;</td>";
				if($spouse==2 OR $spouse==3) {print "<td>&nbsp;</td>"; }
				print "</tr>";
			}
		}
		print "</table>";
	}
}
//-----------------------------------------------------------------------------------------------------
$kop='database_naam';
include("header.php");

include("menu.php");

require_once('include/person_cls.php');
include("include/marriage_cls.php");

$foundX_nr='';
$foundY_nr='';
$foundX_gen='';
$foundY_gen='';
$foundX_match='';
$foundY_match='';
$spouse='';
$reltext='';
$special_spouseX='';
$special_spouseY='';
$table='';
$name1= '';
$name2= '';

//======== HELP POPUP ========================

echo '<div class="fonts weergave '.$rtlmarker.'sddm" style="display:inline;">';
echo '<a href="#"';
echo ' style="display:inline" ';
echo 'onmouseover="mopen(event,\'hulpmenu\',10,150)"';
echo 'onmouseout="mclosetime()">';
echo '&nbsp;&nbsp;&nbsp;<strong>'.$language["rel_helppopup"].'</strong>';
echo '</a>&nbsp;';
echo '<div class="sddm_fixed" style="z-index:40; text-align:'.$alignmarker.'; padding:4px; direction:'.$rtlmarker.'" id="hulpmenu" onmouseover="mcancelclosetime()" onmouseout="mclosetime()">';

print $language["rel_help1"].'<br>';
print '<ul><li>'.$language["rel_help2"].'</li>';
print '<li>'.$language["rel_help3"].'</li>';
print '<li>'.$language["rel_help4"].'</li>';
print '<li>'.$language["rel_help5"].'</li>';
print '<li>'.$language["rel_help6"].'</li></ul>';
print $language["rel_help7"].'<br>';
print '<ul><li>'.$language["rel_help8"].'</li>';
print '<li>'.$language["rel_help9"].'</li>';
print '<li>'.$language["rel_help10"].'</li>';
print '<li>'.$language["rel_help12"].'</li></ul>';
echo '</div>';
echo '</div>';

//=================================

if (isset($_SESSION['tree_prefix'])) { $tree_prefix=$_SESSION['tree_prefix'];}

if(!isset($_POST["search1"]) AND !isset($_POST["search2"]) AND !isset($_POST["calculator"]) AND !isset($_POST["switch"])) {
	// no button pressed: this is a fresh entry from humogen's frontpage link: start clean search form
	$_SESSION["search1"]='';
	$_SESSION["search2"]='';
	$_SESSION['rel_search_firstname']='';
	$_SESSION['rel_search_lastname']='';
	$_SESSION['rel_search_firstname2']='';
	$_SESSION['rel_search_lastname2']='';
}

$person=''; if (isset($_POST["person"])){	$person=$_POST['person']; }
$person2=''; if (isset($_POST["person2"])){ $person2=$_POST['person2']; }
if (isset($_POST["search1"])){ $_SESSION["search1"]=1; }
if (isset($_POST["search2"])){ $_SESSION["search2"]=1; }

if(isset($_POST["switch"])) {
	$temp=$_SESSION['rel_search_firstname']; $_SESSION['rel_search_firstname']=$_SESSION['rel_search_firstname2']; $_SESSION['rel_search_firstname2']=$temp;
	$temp=$_SESSION['rel_search_lastname'];  $_SESSION['rel_search_lastname']=$_SESSION['rel_search_lastname2'];   $_SESSION['rel_search_lastname2']=$temp;
	$temp=$person; $person=$person2; $person2=$temp;
	$temp=$_SESSION["search1"]; $_SESSION["search1"]=$_SESSION["search2"]; $_SESSION["search2"]=$temp;
}

// ===== BEGIN SEARCH BOX SYSTEM
print '<span class="fonts weergave"><br><br>&nbsp;&nbsp;&nbsp;'.$language["rel_help11"].'<br>';
print '&nbsp;&nbsp;&nbsp;'.$language["rel_help12"].'</span><br>';

echo '<form method="POST" action="'.$_SERVER['PHP_SELF'].'" style="display : inline;">';

echo '<table class="humo relmenu weergave">';
echo '<tr><td>';
echo '&nbsp;';
echo '</td><td>';
print $language["main_firstname"].':';
echo '</td><td>';
print $language["main_lastname"].':';
echo '</td><td>';
echo $language["main_search"];
echo '</td><td colspan=2>'.$language["rel_pickname"].'</td><td>';
echo $language["rel_calculate"];
echo '</td></tr><tr><td>';
echo $language["rel_person"].'1:';
echo '</td><td>';

$search_firstname='';
if (isset($_POST["search_firstname"]) AND !isset($_POST["switch"])){
	$search_firstname=veilig($_POST['search_firstname']);
	$_SESSION['rel_search_firstname']=$search_firstname;
}
if (isset($_SESSION['rel_search_firstname'])){ $search_firstname=$_SESSION['rel_search_firstname']; }

$search_lastname='';
if (isset($_POST["search_lastname"]) AND !isset($_POST["switch"])){
	$search_lastname=veilig($_POST['search_lastname']);
	$_SESSION['rel_search_lastname']=$search_lastname;
}
if (isset($_SESSION['rel_search_lastname'])){ $search_lastname=$_SESSION['rel_search_lastname']; }

print ' <input type="text" class="fonts relboxes" name="search_firstname" value="'.$search_firstname.'" size="15"> ';
echo '</td><td>';

print '&nbsp; <input class="fonts relboxes" type="text" name="search_lastname" value="'.$search_lastname.'" size="15">';
echo ' <input type="hidden" name="tree_prefix" value="'.$tree_prefix.'">';
echo '</td><td>';
print '&nbsp; <input class="fonts" type="submit" name="search1" value="'.$language["main_search"].'">';
echo '</td><td>';

if(isset($_SESSION["search1"]) AND $_SESSION["search1"]==1) {
	$persoon_qry= "SELECT * FROM ".$tree_prefix."person WHERE CONCAT(REPLACE(pers_prefix,'_',' '),pers_lastname)";
	$persoon_qry.= " LIKE '%".$search_lastname."%' AND pers_firstname LIKE '%".$search_firstname."%' ORDER BY pers_lastname, pers_firstname";
	$persoon_result = mysql_query($persoon_qry,$db);
	if ($persoon_result){
		if(mysql_num_rows($persoon_result)>0) {
			print '<select class="fonts" size="1" name="person"  style="width: 230px">';
				while ($persoon=mysql_fetch_object($persoon_result)){
					echo '<option';
					if(isset($person)) {
						if ($persoon->pers_gedcomnumber==$person AND !(isset($_POST["search1"]) AND $search_lastname=='' AND $search_firstname=='')){
							echo ' SELECTED';
						}
					}
					$prefix1=''; $prefix2='';
					if($user['soortindex']=="j") {
						$prefix1=strtolower(str_replace("_"," ",$persoon->pers_prefix));
					}
					else {
						$prefix2=" ".strtolower(str_replace("_"," ",$persoon->pers_prefix));
					}
					echo ' value="'.$persoon->pers_gedcomnumber.'">'.$prefix1.$persoon->pers_lastname.', '.$persoon->pers_firstname.$prefix2.' ['.$persoon->pers_gedcomnumber.']</option>';
				}				
				echo '</select>';
			}
		else {print '<select size="1" name="notfound" value="1" style="width: 230px"><option>'.$language["rel_notfound"].'</option></select>'; }
	}
}
else {  print '<select size="1" name="person" style="width: 230px"><option></option></select>'; }
echo '</td><td rowspan=2>';
//echo '<input type="image" src="images/turn_around.gif" alt="'.$language["rel_switch"].'" title="'.$language["rel_switch"].'" value="Submit" name="switch" >';
echo '<input type="submit" alt="'.$language["rel_switch"].'" title="'.$language["rel_switch"].'" value=" " name="switch" style="background: #fff url(\'images/turn_around.gif\') top no-repeat;width:25px;height:25px">';
echo '</td><td rowspan=2>';
echo '<input type="submit" name="calculator" value="'.$language["rel_calculate"].'" style="font-size:115%;">';
echo '</td></tr><tr><td>';

// SECOND PERSON
echo $language["rel_person"].'2:';
echo '</td><td>';

$search_firstname2='';
if (isset($_POST["search_firstname2"]) AND !isset($_POST["switch"])){
	$search_firstname2=veilig($_POST['search_firstname2']);
	$_SESSION['rel_search_firstname2']=$search_firstname2;
}
if (isset($_SESSION['rel_search_firstname2'])){ $search_firstname2=$_SESSION['rel_search_firstname2']; }

$search_lastname2='';
if (isset($_POST["search_lastname2"]) AND !isset($_POST["switch"])){
	$search_lastname2=veilig($_POST['search_lastname2']);
	$_SESSION['rel_search_lastname2']=$search_lastname2;
}
if (isset($_SESSION['rel_search_lastname2'])){ $search_lastname2=$_SESSION['rel_search_lastname2']; }

print ' <input type="text" class="fonts relboxes" name="search_firstname2" value="'.$search_firstname2.'" size="15"> ';
echo '</td><td>';
print '&nbsp; <input class="fonts relboxes" type="text" name="search_lastname2" value="'.$search_lastname2.'" size="15">';
echo ' <input type="hidden" name="tree_prefix" value="'.$tree_prefix.'">';
echo '</td><td>';
print '&nbsp; <input class="fonts" type="submit" name="search2" value="'.$language["main_search"].'">';
echo '</td><td>';

if(isset($_SESSION["search2"]) AND $_SESSION["search2"]==1) {
	$persoon_qry= "SELECT * FROM ".$tree_prefix."person WHERE CONCAT(REPLACE(pers_prefix,'_',' '),pers_lastname)";
				$persoon_qry.= " LIKE '%".$search_lastname2."%' AND pers_firstname LIKE '%".$search_firstname2."%' ORDER BY pers_lastname, pers_firstname";
	$persoon_result2 = mysql_query($persoon_qry,$db);
	if ($persoon_result2){
		if(mysql_num_rows($persoon_result2)>0) {
			print '<select class="fonts" size="1" name="person2" style="width: 230px">';
			while ($persoon2=mysql_fetch_object($persoon_result2)){
				echo '<option';
				if(isset($person2)) {
					if ($persoon2->pers_gedcomnumber==$person2 AND !(isset($_POST["search2"]) AND $search_lastname2=='' AND $search_firstname2=='')){
						echo ' SELECTED';
					}
				}
				$prefix1=''; $prefix2='';
				if($user['soortindex']=="j") {
					$prefix1=strtolower(str_replace("_"," ",$persoon2->pers_prefix));
				}
				else {
					$prefix2=" ".strtolower(str_replace("_"," ",$persoon2->pers_prefix));
				}
				echo ' value="'.$persoon2->pers_gedcomnumber.'">'.$prefix1.$persoon2->pers_lastname.', '.$persoon2->pers_firstname.$prefix2.' ['.$persoon2->pers_gedcomnumber.']</option>';
			}
			echo '</select>';
		}
			else { print '<select size="1" name="notfound" value="1" style="width: 230px"><option>'.$language["rel_notfound"].'</option></select>'; }
	}
}
else {  print '<select size="1" name="person2" style="width: 230px"><option></option></select>'; }
echo '</td></tr></table>';
echo '</form>';

// ===== END SEARCH BOX SYSTEM

if(isset($_POST["calculator"]) OR isset($_POST["switch"])) { // calculate or switch button is pressed
	if(isset($person) AND $person!='' AND isset($person2) AND $person2!='') { // 2 persons have been selected
		$persoon=getperson($person);
			$persoon2=getperson($person2);
		if (isset($persoon)){
			$gednr=$persoon->pers_gedcomnumber;
			$name1 = $persoon->pers_firstname.' '.strtolower(trim($persoon->pers_prefix,'_')).$persoon->pers_lastname;
			$sexe=''; if($persoon->pers_sexe =='M') { $sexe='m'; } else { $sexe='f'; }
		}
		if($persoon->pers_fams) {
			$famsX=$persoon->pers_fams;
			$tempfam=explode(";",$famsX);
			$famX=$tempfam[0];
		}
		else {
			$famX=$persoon->pers_famc;
		}
			if (isset($persoon2)){
			$gednr2=$persoon2->pers_gedcomnumber;
			$name2 = $persoon2->pers_firstname.' '.strtolower(trim($persoon2->pers_prefix,'_')).$persoon2->pers_lastname;
			$sexe2=''; if($persoon2->pers_sexe =='M') { $sexe2='m'; } else { $sexe2='f'; }
		}
		if($persoon2->pers_fams) {
			$famsY=$persoon2->pers_fams;
			$tempfam=explode(";",$famsY);
			$famY=$tempfam[0];
		}
		else {
			$famY=$persoon2->pers_famc;
		}

		display(); // initiates all the comparison and calculation functions and writes result
	}
	else {  // "calculate" or "switch" button pressed with one or two names not selected: write warning to first choose two names
		print "<br><h3>&nbsp;&nbsp;&nbsp;".$language["rel_mustchoose"]."</h3>";
	}
}
include("footer.php");
?> 
Return current item: HuMo-gen