function check_updated_facts($i, &$famrec, $TAGS, $prefix)
 {
     global $typefacts, $pid, $pgv_lang, $factarray;
     $famrec = trim($famrec);
     $famupdate = false;
     $repeat_tags = array();
     $var = $prefix . $i . "DESCS";
     if (!empty($_POST[$var])) {
         $DESCS = $_POST[$var];
     } else {
         $DESCS = array();
     }
     $var = $prefix . $i . "DATES";
     if (!empty($_POST[$var])) {
         $DATES = $_POST[$var];
     } else {
         $DATES = array();
     }
     $var = $prefix . $i . "PLACS";
     if (!empty($_POST[$var])) {
         $PLACS = $_POST[$var];
     } else {
         $PLACS = array();
     }
     $var = $prefix . $i . "TEMPS";
     if (!empty($_POST[$var])) {
         $TEMPS = $_POST[$var];
     } else {
         $TEMPS = array();
     }
     $var = $prefix . $i . "RESNS";
     if (!empty($_POST[$var])) {
         $RESNS = $_POST[$var];
     } else {
         $RESNS = array();
     }
     $var = $prefix . $i . "REMS";
     if (!empty($_POST[$var])) {
         $REMS = $_POST[$var];
     } else {
         $REMS = array();
     }
     $var = "F" . $i . "MARRY";
     if (!empty($_REQUEST[$var])) {
         $FMARRY = $_REQUEST[$var];
     } else {
         $FMARRY = "";
     }
     $var = "F" . $i . "DIVY";
     if (!empty($_REQUEST[$var])) {
         $FDIVY = $_REQUEST[$var];
     } else {
         $FDIVY = "";
     }
     for ($j = 0; $j < count($TAGS); $j++) {
         if (!empty($TAGS[$j])) {
             $fact = $TAGS[$j];
             if (!isset($repeat_tags[$fact])) {
                 $repeat_tags[$fact] = 1;
             } else {
                 $repeat_tags[$fact]++;
             }
             $DATES[$j] = check_input_date($DATES[$j]);
             if (!isset($REMS[$j])) {
                 $REMS[$j] = 0;
             }
             if ($REMS[$j] == 1) {
                 $DESCS[$j] = "";
                 $DATES[$j] = "";
                 $PLACS[$j] = "";
                 $TEMPS[$j] = "";
                 $RESNS[$j] = "";
             }
             if (empty($DATES[$j]) && empty($PLACS[$j]) && empty($TEMPS[$j]) && empty($RESNS[$j])) {
                 if (!in_array($fact, $typefacts)) {
                     $factrec = "1 {$fact}";
                 } else {
                     $factrec = "1 EVEN\n2 TYPE {$fact}";
                 }
                 if (!empty($DESCS[$j])) {
                     $factrec .= " {$DESCS[$j]}\n";
                 } elseif (!empty($FMARRY)) {
                     $factrec = "1 MARR Y\n";
                 } elseif (!empty($FDIVY)) {
                     $factrec = "1 DIV Y\n";
                 } else {
                     $factrec = "";
                 }
             } else {
                 if (!in_array($fact, $typefacts)) {
                     $factrec = "1 {$fact}";
                 } else {
                     $factrec = "1 EVEN\n2 TYPE {$fact}";
                 }
                 if (!empty($DESCS[$j])) {
                     $factrec .= " {$DESCS[$j]}\n";
                 } else {
                     $factrec .= "\n";
                 }
                 if (!empty($DATES[$j])) {
                     $factrec .= "2 DATE {$DATES[$j]}\n";
                 }
                 if (!empty($PLACS[$j])) {
                     $factrec .= "2 PLAC {$PLACS[$j]}\n";
                 }
                 if (!empty($TEMPS[$j])) {
                     $factrec .= "2 TEMP {$TEMPS[$j]}\n";
                 }
                 if (!empty($RESNS[$j])) {
                     $factrec .= "2 RESN {$RESNS[$j]}\n";
                 }
             }
             if (!in_array($fact, $typefacts)) {
                 $lookup = "1 {$fact}";
             } else {
                 $lookup = "1 EVEN\n2 TYPE {$fact}\n";
             }
             $pos1 = strpos($famrec, $lookup);
             $k = 1;
             //-- make sure we are working with the correct fact
             while ($k < $repeat_tags[$fact]) {
                 $pos1 = strpos($famrec, $lookup, $pos1 + 5);
                 $k++;
                 if ($pos1 === false) {
                     break;
                 }
             }
             $noupdfact = false;
             if ($pos1 !== false) {
                 $pos2 = strpos($famrec, "\n1 ", $pos1 + 5);
                 if ($pos2 === false) {
                     $pos2 = strlen($famrec);
                 }
                 $oldfac = trim(substr($famrec, $pos1, $pos2 - $pos1));
                 $noupdfact = FactEditRestricted($pid, $oldfac);
                 if ($noupdfact) {
                     echo "<br />", $pgv_lang["update_fact_restricted"], " ", $factarray[$fact], "<br /><br />";
                 } else {
                     //-- delete the fact
                     if ($REMS[$j] == 1) {
                         $famupdate = true;
                         $famrec = substr($famrec, 0, $pos1) . "\n" . substr($famrec, $pos2);
                     } else {
                         if (!empty($oldfac) && !empty($factrec)) {
                             $factrec = $oldfac;
                             if (!empty($DESCS[$j])) {
                                 if (strstr($factrec, "1 {$fact}")) {
                                     $factrec = preg_replace("/1 {$fact}.*/", "1 {$fact} {$DESCS[$j]}", $factrec);
                                 } else {
                                     $factrec = $factrec . "\n1 {$fact} {$DESCS[$j]}";
                                 }
                             }
                             if (!empty($DATES[$j])) {
                                 if (strstr($factrec, "\n2 DATE")) {
                                     $factrec = preg_replace("/2 DATE.*/", "2 DATE {$DATES[$j]}", $factrec);
                                 } else {
                                     $factrec = $factrec . "\n2 DATE {$DATES[$j]}";
                                 }
                             }
                             if (!empty($PLACS[$j])) {
                                 if (strstr($factrec, "\n2 PLAC")) {
                                     $factrec = preg_replace("/2 PLAC.*/", "2 PLAC {$PLACS[$j]}", $factrec);
                                 } else {
                                     $factrec = $factrec . "\n2 PLAC {$PLACS[$j]}";
                                 }
                             }
                             if (!empty($TEMPS[$j])) {
                                 if (strstr($factrec, "\n2 TEMP")) {
                                     $factrec = preg_replace("/2 TEMP.*/", "2 TEMP {$TEMPS[$j]}", $factrec);
                                 } else {
                                     $factrec = $factrec . "\n2 TEMP {$TEMPS[$j]}";
                                 }
                             }
                             if (!empty($RESNS[$j])) {
                                 if (strstr($factrec, "\n2 RESN")) {
                                     $factrec = preg_replace("/2 RESN.*/", "2 RESN {$RESNS[$j]}", $factrec);
                                 } else {
                                     $factrec = $factrec . "\n2 RESN {$RESNS[$j]}";
                                 }
                             }
                             $factrec = preg_replace("/[\r\n]+/", "\n", $factrec);
                             $oldfac = preg_replace("/[\r\n]+/", "\n", $oldfac);
                             if (trim($factrec) != trim($oldfac)) {
                                 $famupdate = true;
                                 $famrec = substr($famrec, 0, $pos1) . trim($factrec) . "\n" . substr($famrec, $pos2);
                             }
                         }
                     }
                 }
             } else {
                 if (!empty($factrec)) {
                     $famrec .= "\n" . $factrec;
                     $famupdate = true;
                 }
             }
         }
     }
     return $famupdate;
 }
/**
* Add new gedcom lines from interface update arrays
* The edit_interface and add_simple_tag function produce the following
* arrays incoming from the $_POST form
* - $glevels[] - an array of the gedcom level for each line that was edited
* - $tag[] - an array of the tags for each gedcom line that was edited
* - $islink[] - an array of 1 or 0 values to tell whether the text is a link element and should be surrounded by @@
* - $text[] - an array of the text data for each line
* With these arrays you can recreate the gedcom lines like this
* <code>$glevel[0]." ".$tag[0]." ".$text[0]</code>
* There will be an index in each of these arrays for each line of the gedcom
* fact that is being edited.
* If the $text[] array is empty for the given line, then it means that the
* user removed that line during editing or that the line is supposed to be
* empty (1 DEAT, 1 BIRT) for example.  To know if the line should be removed
* there is a section of code that looks ahead to the next lines to see if there
* are sub lines.  For example we don't want to remove the 1 DEAT line if it has
* a 2 PLAC or 2 DATE line following it.  If there are no sub lines, then the line
* can be safely removed.
* @param string $newged the new gedcom record to add the lines to
* @param int $levelOverride Override GEDCOM level specified in $glevels[0]
* @return string The updated gedcom record
*/
function handle_updates($newged, $levelOverride = "no")
{
    global $glevels, $islink, $tag, $uploaded_files, $text, $NOTE, $WORD_WRAPPED_NOTES;
    if ($levelOverride == "no" || count($glevels) == 0) {
        $levelAdjust = 0;
    } else {
        $levelAdjust = $levelOverride - $glevels[0];
    }
    for ($j = 0; $j < count($glevels); $j++) {
        // BH These following lines destroyed the Shared Note
        // Therefore they have been removed for now
        /*
        		//-- update external note records first
        		if (($islink[$j])&&($tag[$j]=="NOTE")) {
        			if (empty($NOTE[$text[$j]])) {
        				delete_gedrec($text[$j]);
        				$text[$j] = "";
        			} else {
        				$noterec = find_gedcom_record($text[$j]);
        				$newnote = "0 @$text[$j]@ NOTE\n";
        				$newline = "1 CONC ".rtrim(stripLRMRLM($NOTE[$text[$j]]));
        				$newnote .= breakConts($newline);
        				if (PGV_DEBUG) {
        					echo "<pre>$newnote</pre>";
        				}
        				replace_gedrec($text[$j], $newnote);
        			}
        		} //-- end of external note handling code
        */
        //echo $glevels[$j]." ".$tag[$j];
        // Look for empty SOUR reference with non-empty sub-records.
        // This can happen when the SOUR entry is deleted but its sub-records
        // were incorrectly left intact.
        // The sub-records should be deleted.
        if ($tag[$j] == "SOUR" && ($text[$j] == "@@" || $text[$j] == "")) {
            $text[$j] = "";
            $k = $j + 1;
            while ($k < count($glevels) && $glevels[$k] > $glevels[$j]) {
                $text[$k] = "";
                $k++;
            }
        }
        if (trim($text[$j]) != '') {
            $pass = true;
        } else {
            //-- for facts with empty values they must have sub records
            //-- this section checks if they have subrecords
            $k = $j + 1;
            $pass = false;
            while ($k < count($glevels) && $glevels[$k] > $glevels[$j]) {
                if ($text[$k] != '') {
                    if ($tag[$j] != "OBJE" || $tag[$k] == "FILE") {
                        $pass = true;
                        break;
                    }
                }
                if ($tag[$k] == "FILE" && count($uploaded_files) > 0) {
                    $filename = array_shift($uploaded_files);
                    if (!empty($filename)) {
                        $text[$k] = $filename;
                        $pass = true;
                        break;
                    }
                }
                $k++;
            }
        }
        //-- if the value is not empty or it has sub lines
        //--- then write the line to the gedcom record
        //if ((($text[trim($j)]!="")||($pass==true)) && (strlen($text[$j]) > 0)) {
        //-- we have to let some emtpy text lines pass through... (DEAT, BIRT, etc)
        if ($pass == true) {
            $newline = $glevels[$j] + $levelAdjust . " " . $tag[$j];
            //-- check and translate the incoming dates
            if ($tag[$j] == "DATE" && $text[$j] != '') {
                $text[$j] = check_input_date($text[$j]);
            }
            // echo $newline;
            if ($text[$j] != "") {
                if ($islink[$j]) {
                    $newline .= " @" . $text[$j] . "@";
                } else {
                    $newline .= " " . $text[$j];
                }
            }
            $newged .= breakConts($newline);
        }
    }
    return $newged;
}
     //-- create a shared note record from the incoming variables
 //------------------------------------------------------------------------------
 //-- create a shared note record from the incoming variables
 case 'addnoteaction':
     if (PGV_DEBUG) {
         phpinfo(INFO_VARIABLES);
     }
     $newgedrec = "0 @XREF@ NOTE\n";
     if (isset($_REQUEST['EVEN'])) {
         $EVEN = $_REQUEST['EVEN'];
     }
     if (!empty($EVEN) && count($EVEN) > 0) {
         $newgedrec .= "1 DATA\n";
         $newgedrec .= "2 EVEN " . implode(",", $EVEN) . "\n";
         if (!empty($EVEN_DATE)) {
             $newgedrec .= "3 DATE " . check_input_date($EVEN_DATE) . "\n";
         }
         if (!empty($EVEN_PLAC)) {
             $newgedrec .= "3 PLAC " . $EVEN_PLAC . "\n";
         }
         if (!empty($AGNC)) {
             $newgedrec .= "2 AGNC " . $AGNC . "\n";
         }
     }
     if (isset($_REQUEST['ABBR'])) {
         $ABBR = $_REQUEST['ABBR'];
     }
     if (isset($_REQUEST['TITL'])) {
         $TITL = $_REQUEST['TITL'];
     }
     if (isset($_REQUEST['DATE'])) {