<?php
//=================================================================
// relations.php - checks relationships between person X and person Y
//
// written by Yossi Beck - August 2010 for HuMo-gen
// adjusted for several languages 2011-2015 by Yossi Beck
// extended marital calculator added by Yossi Beck - February 2014
//
// 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
// get_person            - 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 sexe of persons X and Y
// person, $person2 - gedcom nr of the searched persons X and Y
//============================================================================================
ini_set('max_execution_time', 600);

//global declarations for Joomla
global $foundX_nr, $foundY_nr, $foundX_gen, $foundY_gen, $foundX_match, $foundY_match, $spouse;
global $reltext, $sexe, $sexe2, $special_spouseY, $special_spouseX, $doublespouse, $table;
global $ancestortext, $dutchtext, $selected_language, $spantext;
global $famsX, $famsY, $famX, $famY, $famspouseX, $famspouseY, $rel_arrayX, $rel_arrayY, $spousenameX, $spousenameY;
global $rel_arrayspouseX, $rel_arrayspouseY;
global $person, $person2, $searchDb, $searchDb2;
global $bloodreltext, $name1, $name2, $gednr, $gednr2;
global $fampath;
// *** Only needed for dutch relation names ***
global $hoog, $opper, $aarts, $voor, $edel, $stam, $oud, $rest;

// include_once("../header.php"); // returns CMS_ROOTPATH constant
if (!defined("CMS_ROOTPATH")) define("CMS_ROOTPATH", "");
if (!defined("CMS_ROOTPATH_ADMIN")) define("CMS_ROOTPATH_ADMIN", "admin/");
// include_once(CMS_ROOTPATH."menu.php");
require_once(CMS_ROOTPATH."include/person_cls.php");
include_once(CMS_ROOTPATH."include/marriage_cls.php");
include_once(CMS_ROOTPATH."include/language_date.php");
include_once(CMS_ROOTPATH."include/date_place.php");

// *** Function to show family tree texts ***
include_once (CMS_ROOTPATH.'include/show_tree_text.php');
include_once(CMS_ROOTPATH."include/db_functions_cls.php");
$db_functions = New db_functions();

// if(CMS_SPECIFIC == "Joomla") {
// 	$fampath = "index.php?option=com_humo-gen&amp;task=family&amp"; // path to family.php for joomla (used some 20 times in this code
// }
// else {
// 	$fampath = CMS_ROOTPATH."family.php?";
// }

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

	$family_id=$gednr;
	$ancestor_id2[] = $family_id;
	$ancestor_number2[] = 1;
	$marriage_number2[] = 0;
	$generation = 1;
	$genarray_count = 0;

	// *** Loop ancestor report ***
	while (isset($ancestor_id2[0])){
		unset($ancestor_id);
		$ancestor_id=$ancestor_id2;
		unset($ancestor_id2);

		unset($ancestor_number);
		$ancestor_number=$ancestor_number2;
		unset($ancestor_number2);

		unset($marriage_number);
		$marriage_number=$marriage_number2;
		unset($marriage_number2);

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

			if ($ancestor_id[$i]!='0'){
				$person_manDb=$db_functions->get_person($ancestor_id[$i]);
				$man_cls = New person_cls;
				$man_cls->construct($person_manDb);
				$man_privacy=$man_cls->privacy;

				if (strtolower($person_manDb->pers_sexe)=='m' AND $ancestor_number[$i]>1){
					@$familyDb=$db_functions->get_family($marriage_number[$i]);

					// *** Use privacy filter of woman ***
					$person_womanDb=$db_functions->get_person($familyDb->fam_woman);
					$woman_cls = New person_cls;
					$woman_cls->construct($person_womanDb);
					$woman_privacy=$woman_cls->privacy;

					// *** Use class for marriage ***
					$marriage_cls = New marriage_cls;
					$marriage_cls->construct($familyDb, $man_privacy, $woman_privacy);
					$family_privacy=$marriage_cls->privacy;
				}

				//*** Show person data ***
				$genarray[$genarray_count][0]= $ancestor_id[$i];
				$genarray[$genarray_count][1]= $generation-1;
				$genarray_count++; // increase by one

				// *** Check for parents ***
				if ($person_manDb->pers_famc){
					@$familyDb = $db_functions->get_family($person_manDb->pers_famc);
					if ($familyDb->fam_man){
						$ancestor_id2[] = $familyDb->fam_man;
						$ancestor_number2[]=(2*$ancestor_number[$i]);
						$marriage_number2[]=$person_manDb->pers_famc;
						$genarray[][2]= $genarray_count-1;
						// save array nr of child in parent array so we can build up ancestral line later
					}

					if ($familyDb->fam_woman){
						$ancestor_id2[]= $familyDb->fam_woman;
						$ancestor_number2[]=(2*$ancestor_number[$i]+1);
						$marriage_number2[]=$person_manDb->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 generation
		$generation++;
	}	// loop ancestors

	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, $sexe2, $spouse, $special_spouseY, $special_spouseX, $doublespouse, $table, $language;
global $selected_language;

	$doublespouse=0;
	if ( $arr_x == 0 AND $arr_y == 0 ) {  // self
		$reltext = __(' identical to ');
		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

/*	
elder brother's wife 嫂
younger brother's wife 弟妇
elder sister's husband 姊夫
younger sister's husband 妹夫
*/	
	
		$table=6;
		if($sexe=='m') {
			$reltext = __('brother of ');
			if($spouse==1) { 
				$reltext = __('sister-in-law of '); $special_spouseX=1;  //comparing spouse of X with Y
			}
			if($spouse==2 OR $spouse==3) { 
				$reltext =  __('brother-in-law of '); $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 = __('sister of ');
			if($spouse==1) { 
				$reltext =  __('brother-in-law of '); 
				$special_spouseX=1;  //comparing spouse of X with Y
			}
			if($spouse== 2 OR $spouse==3) { 
				$reltext =  __('sister-in-law of '); 
				$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 calculate_ancestor($pers) {
global $db_functions, $reltext, $sexe, $sexe2, $spouse, $special_spouseY, $language, $ancestortext, $dutchtext, $selected_language, $spantext, $generY, $foundY_nr, $rel_arrayY;
global $rel_arrayspouseY;

	$anscestortext='';
	if($sexe=='m') {
		$parent = __('father');
	}
	else {
		$parent = __('mother');
	}
	

	if ($pers == 1) {
		if($spouse==2 OR $spouse==3) {
			$special_spouseY=1; // prevents "spouse of Y" in output
			if($parent==__('father')) { $parent=__('father-in-law'); }
			else { $parent=__('mother-in-law');}
		}
		$reltext = $parent.__(' of ');
	}
	else {
		 // *** Other languages ***
			if ($pers == 2) { $reltext = __('grand').$parent.__(' of '); }
			if ($pers == 3) { $reltext = __('great-grand').$parent.__(' of '); }
			if ($pers == 4) { $reltext = __('2nd').' '.__('great-grand').$parent.__(' of '); }
			if ($pers == 5) { $reltext = __('3rd').' '.__('great-grand').$parent.__(' of '); }
			$gennr=$pers-2;
			if ($pers >  5) { $reltext = $gennr.__('th').' '.__('great-grand').$parent.__(' of '); } 
	}
}


function calculate_descendant($pers) {
global $db_functions, $reltext, $sexe, $sexe2, $spouse, $special_spouseX, $language, $selected_language, $spantext, $foundX_nr, $rel_arrayX, $rel_arrayspouseX;

	if($sexe=='m') {
		$child = __('son');
	}
	else {
		$child = __('daughter');
	}
		
	if ($pers == 1) {
		if($spouse==1) {
			if($child==__('son')) { $child=__('daughter-in-law'); }
			else { $child=__('son-in-law');}
			$special_spouseX=1;
			
		}  
		$reltext = $child.__(' of ');
	}
	else {
		if ($pers == 2) { $reltext = __('grand').$child.__(' of '); }
		if ($pers == 3) { $reltext = __('great-grand').$child.__(' of '); }
		if ($pers == 4) { $reltext = __('2nd').' '.__('great-grand').$child.__(' of '); }
		if ($pers == 5) { $reltext = __('3rd').' '.__('great-grand').$child.__(' of '); }
		$gennr=$pers-2;
		if ($pers >  5) { $reltext = $gennr.__('th').' '.__('great-grand').$child.__(' of '); }
	}
}

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

function calculate_nephews($generX) { // handed generations x is removed from common ancestor
global $db_functions, $reltext, $sexe, $sexe2, $language, $spantext, $selected_language, $foundX_nr, $rel_arrayX, $rel_arrayspouseX, $spouse;
global $reltext_nor, $reltext_nor2; // for Norwegian and Danish

	
		if($sexe=='m') {
			$nephniece = __('nephew');
		}
		else {
			$nephniece = __('niece');
		}
		if ($generX == 2) { $reltext = $nephniece.__(' of '); }
		if ($generX == 3) { $reltext = __('grand').$nephniece.__(' of '); }
		if ($generX == 4) { $reltext = __('great-grand').$nephniece.__(' of '); }
		if ($generX == 5) { $reltext = __('2nd').' '.__('great-grand').$nephniece.__(' of '); }
		if ($generX == 6) { $reltext = __('3rd').' '.__('great-grand').$nephniece.__(' of '); }
		$gennr=$generX-3;
		if ($generX >  6) { $reltext = $gennr.__('th ').__('great-grand').$nephniece.__(' of '); }
	
}

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

function calculate_uncles($generY) { // handed generations y is removed from common ancestor
global $db_functions, $reltext,  $sexe, $sexe2, $language, $ancestortext, $dutchtext, $selected_language, $spantext, $rel_arrayspouseY, $spouse;
global $foundY_nr, $rel_arrayY, $fampath;  // only for Finnish paragraph
global $reltext_nor, $reltext_nor2; // for Norwegian and Danish
global  $rel_arrayspouseX, $rel_arrayX, $foundX_nr; // for Chinese

	$ancestortext='';
	if($sexe=='m') {
		$uncleaunt = __('uncle');
	}
	else {
		$uncleaunt = __('aunt');
	}


		if ($generY == 2) { $reltext = $uncleaunt.__(' of '); }
		if ($generY == 3) { $reltext = __('grand').$uncleaunt.__(' of '); }
		if ($generY == 4) { $reltext = __('great-grand').$uncleaunt.__(' of '); }
		if ($generY == 5) { $reltext = __('2nd').' '.__('great-grand').$uncleaunt.__(' of '); }
		if ($generY == 6) { $reltext = __('3rd').' '.__('great-grand').$uncleaunt.__(' of '); }
		$gennr=$generY-2;
		if ($generY >  6) { $reltext = $gennr.__('th').' '.__('great-grand').$uncleaunt.__(' of '); }
	
}

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

function calculate_cousins ($generX, $generY) {
global $db_functions, $reltext, $famsX, $famsY, $language, $sexe, $sexe2, $selected_language, $spantext, $foundY_nr, $rel_arrayX, $rel_arrayspouseX, $rel_arrayY, $rel_arrayspouseY,$spouse, $foundX_nr, $foundY_nr;
global $reltext_nor, $reltext_nor2; // for Norwegian


		$gendiff = abs($generX - $generY);
		if ($gendiff == 0) { $removenr = ""; }
		elseif ($gendiff == 1 ) { $removenr = ' '.__('once removed'); }
		elseif ($gendiff == 2 ) { $removenr = ' '.__('twice removed'); }
		elseif ($gendiff > 2 ) { $removenr = $gendiff.' '.__('times removed'); }

		$degreediff = min($generX,$generY);
		if($degreediff == 2) { $degree = __('1st [COUSIN]'); }  
		if($degreediff == 3) { $degree = __('2nd [COUSIN]'); }  
		if($degreediff == 4) { $degree = __('3rd [COUSIN]'); }  
		
		if($sexe=='m') {
			$cousin=__('COUSIN_MALE');
		}
		else {
			$cousin=__('COUSIN_FEMALE');
		}

		if($degreediff > 4)  {
			$degreediff-=1;
			$degree = $degreediff.__('th').' ';
		}
		
			$reltext = $degree.' '.$cousin.' '.$removenr.__(' of ');
		
	
}

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

function search_marital() {
global $dbh, $db_functions, $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, $tree_prefix_quoted, $tree_id;

	$pers_cls = New person_cls;
	$marrX = explode(";",$famsX);
	$marrY = explode(";",$famsY);

	if($famsX!='') {
		$marrcount=count($marrX);
		for($x=0; $x<$marrcount; $x++) {
			@$familyDb=$db_functions->get_family($marrX[$x],'man-woman');
			if($sexe=='f') {
				$thespouse=$familyDb->fam_man;
			}
			else {
				$thespouse=$familyDb->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=$db_functions->get_person($thespouse);
				$name=$pers_cls->person_name($spouseidDb);
				$spousenameX=$name["name"];

				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++) {
			@$familyDb=$db_functions->get_family($marrY[$x],'man-woman');
			if($sexe2=='f') {
				$thespouse2=$familyDb->fam_man;
			}
			else {
				$thespouse2=$familyDb->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=$db_functions->get_person($thespouse2);
				$name=$pers_cls->person_name($spouseidDb);
				$spousenameY=$name["name"];
				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++) {
				@$familyDb=$db_functions->get_family($marrX[$x],'man-woman');
				if($sexe=='f') {
					$thespouse=$familyDb->fam_man;
				}
				else {
					$thespouse=$familyDb->fam_woman;
				}

				$rel_arrayspouseX = create_rel_array($thespouse);
				@$familyDb=$db_functions->get_family($marrY[$y],'man-woman');
				if($sexe2=='f') {
					$thespouse2=$familyDb->fam_man;
				}
				else {
					$thespouse2=$familyDb->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=$db_functions->get_person($thespouse);
					$name=$pers_cls->person_name($spouseidDb);
					$spousenameX=$name["name"];

					$spouseidDb=$db_functions->get_person($thespouse2);
					$name=$pers_cls->person_name($spouseidDb);
					$spousenameY=$name["name"];

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

					break;
				} //end if foundmatch !=''
			} // for y
			if($foundX_match !=='') { break; }
		} // 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);
		error_log($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='';
}



//-----------------------------------------------------------------------------------------------------

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

$pers_cls = New person_cls;

//======== HELP POPUP ========================
// if(CMS_SPECIFIC=="Joomla") {
// 	echo '<div class="fonts '.$rtlmarker.'sddm" style="z-index:400; position:absolute; top:20px; left:10px;">';
// 	$popwidth="width:700px;";
// }
// else {
// 	echo '<div class="fonts '.$rtlmarker.'sddm" style="margin-right:10px; float:right; display:inline">';
// 	$popwidth="";
// }

// echo '<a href="#"';
// echo ' style="display:inline;" ';
// if(CMS_SPECIFIC=="Joomla") {
// 	echo 'onmouseover="mopen(event,\'help_menu\',0,0)"';
// }
// else {
// 	echo 'onmouseover="mopen(event,\'help_menu\',10,150)"';
// }
// echo 'onmouseout="mclosetime()">';
// echo '&nbsp;&nbsp;&nbsp;<strong><span class="print_version">'.__('Information about the Relationship Calculator').'</span></strong>';
// echo '</a>&nbsp;';
// echo '<div class="sddm_fixed" style="'.$popwidth.' z-index:400; text-align:'.$alignmarker.'; padding:4px; direction:'.$rtlmarker.'" id="help_menu" onmouseover="mcancelclosetime()" onmouseout="mclosetime()">';
// print '<br>';

// echo __('This calculator will find the following relationships:<br>
// <ul><li>Any blood relationship between X and Y ("X is great-grandfather of Y", "X is 3rd cousin once removed of Y" etc.)</li>
// <li>Blood relationship between the spouse of X and person Y ("X is spouse of 2nd cousin of Y", "X is son-in-law of Y")</li>
// <li>Blood relationship between person X and the spouse of Y ("X is 2nd cousin of spouse of Y", "X is father-in-law of Y")</li>
// <li>Blood relationship between spouse of X and spouse of Y ("X spouse of sister-in-law of Y" etc.)</li>
// <li>Direct marital relation ("X is spouse of Y")</li></ul>
// Directions for use:<br>
// <ul><li>Enter first and/or last name (or part of names) in the search boxes and press "Search". Repeat this for person 1 and 2.</li>
// <li>If more than 1 person is found, select the one you want from the search result pulldown box. Repeat this for person 1 and 2.</li>
// <li>Now press the "Calculate relationships" button on the right.</li>
// <li><b>TIP: when you click "search" with empty first <u>and</u> last name boxes you will get a list with all persons in the database. (May take a few seconds)</b></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"]) AND !isset($_POST["extended"]) AND !isset($_POST["next_path"]) AND !isset($_GET['pers_id'])) {
	// 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']='';
	$_SESSION['rel_search_gednr']=''; $_SESSION['rel_search_gednr2']='';
	unset($_SESSION["search_pers_id"]);
}

$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; }
// *** Link from person pop-up menu ***
if (isset($_GET['pers_id'])){
	$_SESSION["search1"]=1;
	$_SESSION["search_pers_id"]=safe_text($_GET['pers_id']);
	$_SESSION['rel_search_firstname']=''; $_SESSION['rel_search_lastname']='';
}



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=$_SESSION['rel_search_gednr'];  $_SESSION['rel_search_gednr']=$_SESSION['rel_search_gednr2'];   $_SESSION['rel_search_gednr2']=$temp;	
	$temp=$person; $person=$person2; $person2=$temp;
	$temp=$_SESSION["search1"]; $_SESSION["search1"]=$_SESSION["search2"]; $_SESSION["search2"]=$temp;
}

?>