<?php
/*=================================================
	Project: phpGedView
	File: gdbi_functions.php
	Author:
		John Finlay		(yalnifj)
	Comments:
		Various functions used by the GDBI interface

	Change Log:
		4/29/03 - File Created

    phpGedView: Genealogy Viewer
    Copyright (C) 2002 to 2003  John Finlay and Others

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

===================================================*/
# $Id: gdbi_functions.php,v 1.60 2004/11/05 22:35:30 yalnifj Exp $
if (strstr($_SERVER["PHP_SELF"],"functions")) {
	print "Now, why would you want to do that.  You're not hacking are you?";
	exit;
}

/**
 * read the contents of a gedcom file
 *
 * opens a gedcom file and reads the contents into the <var>$fcontents</var> global string
 */
function read_gedcom_file() {
	global $fcontents;
	global $GEDCOM, $GEDCOMS;
	global $pgv_lang;
	$fcontents = "";
	if (isset($GEDCOMS[$GEDCOM])) {
		$fp = fopen($GEDCOMS[$GEDCOM]["path"], "r");
		$fcontents = fread($fp, filesize($GEDCOMS[$GEDCOM]["path"]));
		fclose($fp);
	}
}

//-- read the file onto the stack
read_gedcom_file();

//-------------------------------------------- newConnection
//-- this function creates a new unique connection
//-- and adds it to the connections file
//-- it returns the connection identifier
function newConnection() {
	return session_name()."\t".session_id()."\n";
}

//-------------------------------------------- get_next_record
//-- gets the next person in the gedcom, if we reach the end then
//-- returns false
function get_next_xref($gid, $type='INDI') {
	global $GEDCOM, $myindilist, $pgv_changes;

	if (!isset($myindilist[$gid])) {
		print "ERROR 4: Could not find gedcom record with xref:$gid\n";
		return false;
	}
	$found = false;
	foreach($myindilist as $key=>$value) {
		if ($found) {
			return $key;
		}
		if ($key==$gid) $found=true;
	}
	//print "ERROR 14: Reached the end of the list\n";
	return "";
}

//-------------------------------------------- get_prev_record
//-- gets the previous person in the gedcom, if we reach the start then
//-- returns the last record
function get_prev_xref($gid, $type='INDI') {
	global $GEDCOM, $myindilist, $pgv_changes;

	if (!isset($myindilist[$gid])) {
		print "ERROR 4: Could not find gedcom record with xref:$gid\n";
		return false;
	}
	$found = false;
	foreach($myindilist as $key=>$value) {
		if ($key==$gid) $found=true;
		if ($found) {
			if (isset($prev)) {
				return $prevkey;
			}
			else {
				//print "ERROR 15: Reached the beginning of the list\n";
				return "";
			}
		}
		$prev = $value;
		$prevkey = $key;
	}
	//print "ERROR 14: Reached the end of the list\n";
	return "";
}

//-------------------------------------------- replace_gedrec
//-- This function will replace a gedcom record with
//-- a the id $gid with the $gedrec
function replace_gedrec($gid, $gedrec) {
	global $fcontents, $GEDCOM, $pgv_changes, $manual_save;
	$pos1 = strpos($fcontents, "0 @$gid@");
	if ($pos1===false) {
		print "ERROR 4: Could not find gedcom record with xref:$gid\n";
		return false;
	}
	if (($gedrec = check_gedcom($gedrec))!==false) {
		$pos2 = strpos($fcontents, "0 @", $pos1+1);
		if ($pos2===false) $pos2=strlen($fcontents);
		$undo = substr($fcontents, $pos1, $pos2-$pos1);
		$fcontents = substr($fcontents, 0,$pos1).trim($gedrec)."\r\n".substr($fcontents, $pos2);
		$change = array();
		$change["gid"] = $gid;
		$change["gedcom"] = $GEDCOM;
		$change["type"] = "replace";
		$change["status"] = "submitted";
		$change["user"] = getUserName();
		$change["time"] = time();
		$change["undo"] = $undo;
		if (!isset($pgv_changes[$gid."_".$GEDCOM])) $pgv_changes[$gid."_".$GEDCOM] = array();
		$pgv_changes[$gid."_".$GEDCOM][] = $change;
		AddToLog("Replacing gedcom record $gid ->" . getUserName() ."<-");
		if (!isset($manual_save)) return write_file();
		else return true;
	}
	return false;
}

//-------------------------------------------- append_gedrec
//-- this function will append a new gedcom record at
//-- the end of the gedcom file.
function append_gedrec($gedrec) {
	global $fcontents, $GEDCOM, $pgv_changes;

	if (($gedrec = check_gedcom($gedrec))!==false) {
		$ct = preg_match("/0 @(.*)@ (.*)/", $gedrec, $match);
		$gid = $match[1];
		$type = trim($match[2]);
		$xref = get_new_xref($type);
		$gedrec = preg_replace("/0 @$gid@/", "0 @$xref@", $gedrec);
		$pos1 = strrpos($fcontents, "0");
		$fcontents = substr($fcontents, 0, $pos1).trim($gedrec)."\r\n".substr($fcontents, $pos1);
		$change = array();
		$change["gid"] = $xref;
		$change["gedcom"] = $GEDCOM;
		$change["type"] = "append";
		$change["status"] = "submitted";
		$change["user"] = getUserName();
		$change["time"] = time();
		$change["undo"] = "";
		if (!isset($pgv_changes[$xref."_".$GEDCOM])) $pgv_changes[$xref."_".$GEDCOM] = array();
		$pgv_changes[$xref."_".$GEDCOM][] = $change;
		AddToLog("Appending new $type record $gid ->" . getUserName() ."<-");
		if (write_file()) return $xref;
		else return false;
	}
	return false;
}

//-------------------------------------------- get_new_xref
//-- get the next available xref
function get_new_xref($type='INDI') {
	global $fcontents;

	$ct = preg_match_all("/0 @(.*)@ $type/", $fcontents, $match, PREG_SET_ORDER);
	$num = 0;
	for($i=0; $i<$ct; $i++) {
		$ckey = $match[$i][1];
		$bt = preg_match("/(\d+)/", $ckey, $bmatch);
		if ($bt>0) {
			$bnum = trim($bmatch[1]);
			if ($num < $bnum) $num = $bnum;
		}
	}
	$num++;
	$key = $type{0}."$num";
	return $key;
}

//-------------------------------------------- delete_gedrec
//-- this function will delete the gedcom record with
//-- the given $gid
function delete_gedrec($gid) {
	global $fcontents, $GEDCOM, $pgv_changes, $manual_save;
	$pos1 = strpos($fcontents, "0 @$gid@");
	if ($pos1===false) {
		print "ERROR 4: Could not find gedcom record with xref:$gid\n";
		return false;
	}
	$pos2 = strpos($fcontents, "0 @", $pos1+1);
	if ($pos2===false) $pos2=strpos($fcontents, "0 TRLR", $pos1+1);
	$undo = substr($fcontents, $pos1, $pos2-$pos1);
	$fcontents = substr($fcontents, 0,$pos1).substr($fcontents, $pos2);
	$change = array();
	$change["gid"] = $gid;
	$change["gedcom"] = $GEDCOM;
	$change["type"] = "delete";
	$change["status"] = "submitted";
	$change["user"] = getUserName();
	$change["time"] = time();
	$change["undo"] = $undo;
	if (!isset($pgv_changes[$gid."_".$GEDCOM])) $pgv_changes[$gid."_".$GEDCOM] = array();
	$pgv_changes[$gid."_".$GEDCOM][] = $change;
	AddToLog("Deleting gedcom record $gid ->" . getUserName() ."<-");
	if (!isset($manual_save)) return write_file();
	else return true;
}

//-------------------------------------------- check_gedcom
//-- this function will check a GEDCOM record for valid gedcom format
function check_gedcom($gedrec) {
	global $pgv_lang;

	$gedrec = stripslashes($gedrec);
	$ct = preg_match("/0 @(.*)@ (.*)/", $gedrec, $match);
	if ($ct==0) {
		print "ERROR 20: Invalid GEDCOM 5.5 format.\n";
		if ($GLOBALS["DEBUG"]) print "<pre>$gedrec</pre>\n";
		return false;
	}
	$gedrec = trim($gedrec);
	$pos1 = strpos($gedrec, "1 CHAN");
	if ($pos1!==false) {
		$pos2 = strpos($gedrec, "\n1", $pos1+4);
		if ($pos2===false) $pos2 = strlen($gedrec);
		$newgedrec = substr($gedrec, 0, $pos1);
		$newgedrec .= "1 CHAN\n2 DATE ".date("d M Y")."\n";
		$newgedrec .= "3 TIME ".date("H:i:s")."\n";
		$newgedrec .= "2 _PGVU ".getUserName()."\n";
		$newgedrec .= substr($gedrec, $pos2);
		$gedrec = $newgedrec;
	}
	else {
		$newgedrec = "\n1 CHAN\n2 DATE ".date("d M Y")."\n";
		$newgedrec .= "3 TIME ".date("H:i:s")."\n";
		$newgedrec .= "2 _PGVU ".getUserName();
		$gedrec .= $newgedrec;
	}
	$gedrec = preg_replace(array("/(\r\n)+/", "/\r+/", "/\n+/"), array("\r\n", "\r", "\n"), $gedrec);
	return $gedrec;
}

//-------------------------------------------- undo_change
//-- this function will undo a change in the gedcom file
function undo_change($cid, $index) {
	global $fcontents, $pgv_changes, $GEDCOMS, $GEDCOM;

	if (isset($pgv_changes[$cid])) {
		$changes = $pgv_changes[$cid];
		$change = $changes[$index];
		$change["undo"] = $change["undo"];
		if ($GEDCOM != $change["gedcom"]) {
			$GEDCOM = $change["gedcom"];
			read_gedcom_file();
		}
		if ($change["type"]=="delete") {
			$pos1 = strrpos($fcontents, "0");
			$fcontents = substr($fcontents, 0, $pos1).trim($change["undo"])."\r\n".substr($fcontents, $pos1);
		}
		else if ($change["type"]=="append") {
			$pos1 = strpos($fcontents, "0 @".$change["gid"]."@");
			if ($pos1===false) {
				print "ERROR 4: Could not find gedcom record with gid:".$change["gid"]."\n";
				return false;
			}
			$pos2 = strpos($fcontents, "0 @", $pos1+1);
			if ($pos2===false) $pos2=strpos($fcontents, "0 TRLR", $pos1+1);
			$fcontents = substr($fcontents, 0,$pos1).substr($fcontents, $pos2);
		}
		else if ($change["type"]=="replace") {
			$pos1 = strpos($fcontents, "0 @".$change["gid"]."@");
			if ($pos1===false) {
				print "ERROR 4: Could not find gedcom record with gid:".$change["gid"]."\n";
				return false;
			}
			$pos2 = strpos($fcontents, "0 @", $pos1+1);
			if ($pos2===false) $pos2=strlen($fcontents);
			$fcontents = substr($fcontents, 0,$pos1).trim($change["undo"])."\r\n".substr($fcontents, $pos2);
		}
		unset($changes[$index]);
		unset($pgv_changes[$cid][$index]);
		if (count($pgv_changes[$cid])==0) unset($pgv_changes[$cid]);
		else $pgv_changes[$cid] = $changes;
		AddToLog("Undoing change $cid - $index ".$change["type"]." ->" . getUserName() ."<-");
		return write_file();
	}
	return false;
}

//-------------------------------------------- write_file
//-- this function writes the $fcontents back to the
//-- gedcom file
function write_file() {
	global $fcontents, $GEDCOMS, $GEDCOM, $pgv_changes, $INDEX_DIRECTORY;

	if (preg_match("/win/i", PHP_OS)==0) {
		$fcontents = preg_replace('/\r/', "\n", $fcontents);
		$fcontents = preg_replace('/\n+/', "\n", $fcontents);
	}
	else {
		$fcontents = preg_replace('/([^\r])\n/', "$1\r\n", $fcontents);
	}
	$fcontents = preg_replace('/\\\+/', "\\", $fcontents);
	if (preg_match("/0 TRLR/", $fcontents)==0) $fcontents.="0 TRLR\n";
	//-- write the gedcom file
	if (!is_writable($GEDCOMS[$GEDCOM]["path"])) {
		print "ERROR 5: GEDCOM file is not writable.  Unable to complete request.\n";
		return false;
	}
	$fp = fopen($GEDCOMS[$GEDCOM]["path"], "wb");
	if ($fp===false) {
		print "ERROR 6: Unable to open GEDCOM file resource.  Unable to complete request.\n";
		return false;
	}
	$fw = fwrite($fp, $fcontents);
	if ($fw===false) {
		print "ERROR 7: Unable to write to GEDCOM file.\n";
		fclose($fp);
		return false;
	}
	fclose($fp);

	return write_changes();
}

/**
 * prints a form to add an individual or edit an individual's name
 *
 * @param string $nextaction	the next action the gdbi_interface.php file should take after the form is submitted
 * @param string $famid			the family that the new person should be added to
 * @param string $namerec		the name subrecord when editing a name
 * @param string $tag			how the new person is added to the family
 */
function print_indi_form($nextaction, $famid, $linenum="", $namerec="", $tag="CHIL") {
	global $pgv_lang, $factarray, $pid, $PGV_IMAGE_DIR, $PGV_IMAGES, $monthtonum;
	?>
	<script language="JavaScript" type="text/javascript">
	function updatewholename() {
		frm = document.addchildform;
		spfx = "";
		if (document.getElementById("SPFX")) spfx = document.getElementById("SPFX").value+" ";
		npfx = "";
		if (frm.NPFX.value!="") npfx = frm.NPFX.value + " ";
		frm.NAME.value = npfx + frm.GIVN.value + " /" + spfx + frm.SURN.value + "/ " + frm.NSFX.value;
	}
	function checkform() {
		frm = document.addchildform;
		/* if (frm.GIVN.value=="") {
			alert('<?php print $pgv_lang["must_provide"].$pgv_lang["given_name"]; ?>');
			frm.GIVN.focus();
			return false;
		}
		if (frm.SURN.value=="") {
			alert('<?php print $pgv_lang["must_provide"].$pgv_lang["surname"]; ?>');
			frm.SURN.focus();
			return false;
		} */
		if (frm.NAME.value=="") {
			alert('<?php print $pgv_lang["must_provide"].$factarray["NAME"]; ?>');
			frm.NAME.focus();
			return false;
		}
		return true;
	}
	</script>
	<?php
	print '<script language="JavaScript" src="CalendarPopup.js"></script>';
	print '<script language="JavaScript">document.write(getCalendarStyles());</script>';
	print "<form method=\"post\" name=\"addchildform\" onsubmit=\"return checkform();\">\n";
	print "<input type=\"hidden\" name=\"action\" value=\"$nextaction\" />\n";
	print "<input type=\"hidden\" name=\"linenum\" value=\"$linenum\" />\n";
	print "<input type=\"hidden\" name=\"famid\" value=\"$famid\" />\n";
	print "<input type=\"hidden\" name=\"pid\" value=\"$pid\" />\n";
	print "<input type=\"hidden\" name=\"tag\" value=\"$tag\" />\n";
	print "<table class=\"facts_table\">";
	//-- prefix
	$name = "";
	$nt = preg_match("/\d NPFX (.*)/", $namerec, $nmatch);
	if ($nt>0) $name = trim($nmatch[1]);
	print "<tr><td class=\"facts_label\">".$factarray["NAME"]." ".$factarray["NPFX"].":</td>\n";
	print "<td class=\"facts_value\"><input type=\"text\" name=\"NPFX\" value=\"$name\" onchange=\"updatewholename();\" /></td></tr>\n";
	//-- given name
	$name = "";
	$nt = preg_match("/\d GIVN (.*)/", $namerec, $nmatch);
	if ($nt>0) $name = trim($nmatch[1]);
	print "<tr><td class=\"facts_label\">".$pgv_lang["given_name"]."</td>";
	print "<td class=\"facts_value\"><input type=\"text\" name=\"GIVN\" value=\"$name\" onchange=\"updatewholename();\" /></td></tr>\n";
	//-- nickname
	$name = "";
	$nt = preg_match("/\d NICK (.*)/", $namerec, $nmatch);
	if ($nt>0) $name = trim($nmatch[1]);
	print "<tr><td class=\"facts_label\">".$factarray["NICK"].":</td>";
	print "<td class=\"facts_value\"><input type=\"text\" name=\"NICK\" value=\"$name\" onchange=\"updatewholename();\" /></td></tr>\n";
	//-- surname
	$name = "";
	$nt = preg_match("/\d SURN (.*)/", $namerec, $nmatch);
	if ($nt>0) $name = trim($nmatch[1]);
	print "<tr><td class=\"facts_label\">".$pgv_lang["surname"]."</td>";
	print "<td class=\"facts_value\"><input type=\"text\" name=\"SURN\" value=\"".$name."\" onchange=\"updatewholename();\" /></td></tr>\n";
	//-- suffix
	$name = "";
	$nt = preg_match("/\d NSFX (.*)/", $namerec, $nmatch);
	if ($nt>0) $name = trim($nmatch[1]);
	print "<tr><td class=\"facts_label\">".$pgv_lang["suffix"]."</td>";
	print "<td class=\"facts_value\"><input type=\"text\" name=\"NSFX\" value=\"$name\" onchange=\"updatewholename();\" /></td></tr>\n";
	//-- whole name
	$name = "";
	$nt = preg_match("/\d NAME (.*)/", $namerec, $nmatch);
	if ($nt>0) $name = trim($nmatch[1]);
	print "<tr><td class=\"facts_label\">".$pgv_lang["name"]."</td>";
	print "<td class=\"facts_value\"><input type=\"text\" name=\"NAME\" value=\"".$name."\" size=\"35\" /></td></tr>\n";
	if (empty($namerec)) {
		//-- sex
		print "<tr><td class=\"facts_label\">".$pgv_lang["sex"]."</td>";
		print "<td class=\"facts_value\"><select name=\"SEX\"><option value=\"M\"";
		if ($tag=="HUSB") print " selected=\"selected\"";
		print ">".$pgv_lang["male"]."</option><option value=\"F\"";
		if ($tag=="WIFE") print " selected=\"selected\"";
		print ">".$pgv_lang["female"]."</option><option value=\"U\">".$pgv_lang["unknown"]."</option></select></td></tr>\n";
		//-- birth
		print "<tr><td class=\"facts_label\">".$pgv_lang["birth"]."</td>";
		print "<td class=\"facts_value\"></td></tr>";
		print "<tr><td class=\"facts_label\">".$pgv_lang["date"]."</td>";
		print "<td class=\"facts_value\"><input type=\"text\" name=\"birthdate\" id=\"birthdate\" />";
		?>
		<script language="JavaScript">
			var cal1x = new CalendarPopup("caldiv1");
			cal1x.showYearNavigation();
			cal1x.showYearNavigationInput();
			cal1x.setMonthNames(<?php foreach($monthtonum as $mon=>$num) {
				if (isset($pgv_lang[$mon])) {
					if ($num>1) print ",";
					print "\"".$pgv_lang[$mon]."\"";
				}
			}?>);

		</script>
		<a href="#" onclick="cal1x.select(document.getElementById('birthdate'),'anchor1x','d NNN yyyy'); return false;"><img src="<?php print $PGV_IMAGE_DIR."/".$PGV_IMAGES["calendar"]["small"]; ?>" border="0" name="anchor1x" id="anchor1x" alt="<?php print $pgv_lang["select_date"];?>"></a>
		<div id="caldiv1" style="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></div>
		<?php
		print "</td></tr>\n";
		print "<tr><td class=\"facts_label\">".$factarray["PLAC"]."</td>";
		print "<td class=\"facts_value\"><input type=\"text\" name=\"birthplace\" id=\"birthplace\" />";
		print " <a href=\"#\" onclick=\"return findPlace(document.getElementById('birthplace'));\">".$pgv_lang["find_place"]."</a>\n";
		print "</td></tr>";
		//-- death
		print "<tr><td class=\"facts_label\">".$pgv_lang["death"]."</td>";
		print "<td class=\"facts_value\"></td></tr>";
		print "<tr><td class=\"facts_label\">".$pgv_lang["date"]."</td>";
		print "<td class=\"facts_value\"><input type=\"text\" name=\"deathdate\" id=\"deathdate\" />";
		?>
		<script language="JavaScript">
			var cal2x = new CalendarPopup("caldiv2");
			cal2x.showYearNavigation();
			cal2x.showYearNavigationInput();
			cal2x.setMonthNames(<?php foreach($monthtonum as $mon=>$num) {
				if (isset($pgv_lang[$mon])) {
					if ($num>1) print ",";
					print "\"".$pgv_lang[$mon]."\"";
				}
			}?>);

		</script>
		<a href="#" onclick="cal2x.select(document.getElementById('deathdate'),'anchor2x','d NNN yyyy'); return false;"><img src="<?php print $PGV_IMAGE_DIR."/".$PGV_IMAGES["calendar"]["small"]; ?>" border="0" name="anchor2x" id="anchor2x" alt="<?php print $pgv_lang["select_date"];?>"></a>
		<div id="caldiv2" style="position:absolute;visibility:hidden;background-color:white;layer-background-color:white;"></div>
		<?php
		print "</td></tr>\n";
		print "<tr><td class=\"facts_label\">".$factarray["PLAC"]."</td>";
		print "<td class=\"facts_value\"><input type=\"text\" name=\"deathplace\" id=\"deathplace\" />";
		print " <a href=\"#\" onclick=\"return findPlace(document.getElementById('deathplace'));\">".$pgv_lang["find_place"]."</a>\n";
		print "</td></tr>";
		print "</table>\n";
		print "<input type=\"submit\" value=\"".$pgv_lang["save"]."\" /><br />\n";
	}
	else {
		if ($namerec!="NEW") {
			$gedlines = split("\n", $namerec);	// -- find the number of lines in the record
			$fields = preg_split("/\s/", $gedlines[0]);
			$glevel = $fields[0];
			$level = $glevel;
			$type = trim($fields[1]);
			$level1type = $type;
			$tags=array();
			$i = 0;
			$namefacts = array("NPFX", "GIVN", "NICK", "SURN", "NSFX", "NAME");
			do {
				if (!in_array($type, $namefacts)) {
					$text = "";
					for($j=2; $j<count($fields); $j++) {
						if ($j>2) $text .= " ";
						$text .= $fields[$j];
					}
					$iscont = false;
					while(($i+1<count($gedlines))&&(preg_match("/".($level+1)." (CON[CT])\s?(.*)/", $gedlines[$i+1], $cmatch)>0)) {
						$iscont=true;
						if ($cmatch[1]=="CONT") $text.="\r\n";
						if ($WORD_WRAPPED_NOTES) $text .= " ";
						$text .= $cmatch[2];
						$i++;
					}
					$islink = 0;
					if (preg_match("/^@(.*)@/", $text)>0) $islink=1;
					print "<tr><td class=\"facts_label\"><input type=\"hidden\" name=\"glevels[]\" value=\"$level\" /><input type=\"hidden\" name=\"islink[]\" value=\"$islink\" /><input type=\"hidden\" name=\"tag[]\" value=\"$type\" />";
					if (isset($factarray["$type"])) print $factarray["$type"];
					else print $type;
					print "</td>";
					$tags[]=$type;
					print "<td class=\"facts_value\">";
					if (!empty($text)) {
						if ($type=="TEMP") {
							print "<select name=\"text[]\">\n";
							print "<option value=\"\">".$pgv_lang["no_temple"]."</option>\n";
							foreach($TEMPLE_CODES as $code=>$temple) {
								print "<option value=\"$code\"";
								if ($code==trim($text)) print " selected=\"selected\"";
								print ">$temple</option>\n";
							}
							print "</select>\n";
						}
						else if ($type=="SOUR") {
							$ct = preg_match("/@(.*)@/", $text, $smatch);
							if ($ct>0) {
								$sname = get_source_descriptor($smatch[1]);
								print "<input type=\"text\" id=\"text$i\" class=\"pedigree_form\" size=\"3\" name=\"text[]\" value=\"$smatch[1]\" />\n ";
								print "<a href=\"#\" onclick=\"return findSource(document.getElementById('text$i'));\">".$pgv_lang["find_id"]."</a> ";
								print $sname;
							}
						}
						else if (($type=="NOTE")&&($islink)) {
							$ct = preg_match("/@(.*)@/", $text, $smatch);
							if ($ct>0) {
								print "<input type=\"hidden\" name=\"text[]\" value=\"$smatch[1]\" />\n";
								$noterec = find_gedcom_record($smatch[1]);
								$nt = preg_match("/0 @$smatch[1]@ NOTE (.*)/", $noterec, $n1match);
								print "<textarea name=\"NOTE[$smatch[1]]\" rows=\"8\" cols=\"50\">";
								if ($nt>0) print trim($n1match[1]);
								print preg_replace("/<br \/>/", "", get_cont(1, $noterec));
								print "\n</textarea>\n";
							}
						}
						else {
							if ((!$iscont)&&($type!="NOTE")){
								$setDirection = "";
								if($type == "DATE") {
									$setDirection = " dir=\"ltr\"";
								}
								print "<input id=\"$type\" type=\"text\" name=\"text[]\" size=\"40\" value=\"$text\"$setDirection />\n";
							}
							else print "<textarea id=\"$type\" name=\"text[]\" rows=\"4\" cols=\"50\">$text</textarea>\n";
						}
						if ($type=="PLAC") {
							print " <a href=\"#\" onclick=\"return findPlace(document.getElementById('text$i'));\">".$pgv_lang["find_place"]."</a>\n";
						}
					}
					else {
						print "<input type=\"hidden\" name=\"text[]\" value=\"$text\" />\n";
					}
					print "</td></tr>\n";
				}
				$i++;
				if (isset($gedlines[$i])) {
					$fields = preg_split("/\s/", $gedlines[$i]);
					$level = $fields[0];
					if (isset($fields[1])) $type = trim($fields[1]);
				}
			} while (($level>$glevel)&&($i<count($gedlines)));
		}
		print "</td></tr>\n";
		print "</table>\n";
		print "<input type=\"submit\" value=\"".$pgv_lang["save"]."\" /><br />\n";
	}
	print "</form>\n";
}

?>
