Location: PHPKode > projects > Generic Genetic Studies Database > ggsd-2.19/cgi-bin/delete_phenotype_values.php
<?php

#Copyright 2008 Aaron Day-Williams <hide@address.com>
#
#This file is part of Generic Genetic Studies Database (GGSD).
#
#GGSD 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 3 of the License, or
#(at your option) any later version.
#
#GGSD 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 GGSD.  If not, see <http://www.gnu.org/licenses/>.


header('Content-type: text/html');
session_cache_limiter('private');
session_start();
require 'sql_lib.inc';
require 'html_lib.inc';
require 'file_system_lib.inc';

#NAME	delete_phenotype_values.php

#TYPE	PHP

#USES	sql_lib.inc(authenticate deletePhenotypeValue tableQuery distinctTableQuery compoundTableQuery)
#	html_lib.inc(printPhenotypeValueCheckList)

#PURPOSE	To give the user a form to delete genotypes from the database.

#USAGE	delete_phenotype_values.php

#PARAMETERS
#	authorized user		auth_user	string	optional
#	want list		want_list	int	optional
#	indvidual id		indiv_id	int	optional
#	pedigree id		ped_id		int	optional
#	phenotype name		phenotype_name	string	optional
#	phenotype value ids	pheno_val_ids	list	optional

#RETURNS
#	success message					if success
#	errror message					if error

#DATASTRUCTURES No special datastructures

#FUNCTIONAL DESCRIPTION First get the args and authenticate the user.  Then determine whether
#		       the user knows which phenotype value they want to delete or whether they want
#		       a list of all the phenotype values.  If they know give them a web form, else give
#		       them a list of all the phenotype values in the database.  Then get data and delete
#                       phenotype values.

#ASSUMPTIONS  That the phenotype value exists

#LIMITATIONS

#COMMENTS

#PRELIMINARY DESIGN
#	    1	Get Args
#	    2	Authenticate User
#	    3	Determine whether know phenotype value or want list
#	    4	If know phenotype value give a form
#	    5	Get web form data, and delete
#	    6	If want list, give a list
#	    7	Get list data, and delete

echo "<html>\n";
echo "<head>\n";
if (isset($_POST['end_session'])){echo "  <META HTTP-EQUIV=Refresh CONTENT=\"$logout_refresh_time;URL=../right.html\">\n";}
else                             {echo "  <META HTTP-EQUIV=Refresh CONTENT=\"$normal_refresh_time; URL=$_SERVER[SCRIPT_NAME]\">\n";}
echo "  <title>Delete Phenotype Values</title>\n";
echo "</head>\n";
echo "<body bgcolor=\"white\">\n";
echo "<center><h3>Delete Phenotype Values</h3></center>\n";
echo "<hr>\n";
printHomeNavBar();
echo "<br>\n";


#1     Get Args
$access_type_required  = 'administrative';
$want_list             = $_POST['want_list'];
$upload_file           = $_POST['upload_file'];
$file_on_server        = $_POST['file_name'];
$individual_id         = $_POST['individual_id'];#value from known value to delete, select list
$indiv_ids             = $_POST['indiv_id'];     #value from check list of individuals
$indiv_multiple_pages  = $_POST['indiv_multiple_pages'];
$ped_id                = $_POST['pedigree_id'];
$phenotype_id          = $_POST['phenotype_id']; #value from know value to delete, select list
$pheno_ids             = $_POST['pheno_id'];     #value from check list of phenotypes
$pheno_multiple_pages  = $_POST['pheno_multiple_pages'];
$function_to_call      = $_POST['function_to_call'];
$table_name            = $_POST['table_name'];
$field_list            = $_POST['field_list'];
$term                  = $_POST['term'];
$mode                  = $_POST['mode'];
$join                  = $_POST['join'];
$order_by              = $_POST['order_by'];
$last_record_retrieved = $_POST['last_record_retrieved'];
#$pheno_val_ids        = $_POST['pheno_val_id'];

$ret_val = connect();
if ($ret_val != '1')
  {
    $date = date('Y-m-d H:i:s');
    echo "Error #1: $date<br>\n";
    echo "Error in call to connect from delete_phenotype_values.php<br>\n";
    echo "$ret_val<br><br>\n";
    echo "Please email the above error using the Comments/Help/Support link \n";
    echo "in the left navigation bar.<br>\n";
    echo "</body></html>\n";
    exit;
  }


#2     Authenticate User
if (isset($_POST['end_session']))#if want to end session
  {
    #if (key_exists('files_on_server',$_SESSION))
    #  {
    #    $files_to_delete_array = $_SESSION['files_on_server'];
    #    $ret_val = deleteFiles($files_to_delete_array);
    #    if ($ret_val != '1')
    #      {
    #        $date = date('Y-m-d H:i:s');
    #        echo "Error #2: $date<br>\n";
    #        echo "Error in call to deleteFiles from delete_phenotype_values.php<br>\n";
    #        echo "$ret_val<br><br>\n";
    #        echo "Please email the above error using Comments/Help/Support link \n";
#	    echo "in the left navigation bar.<br>\n";
#	    echo "</body></html>\n";
#	    session_unset();
#	    session_destroy();
#	    $ret_val = disconnect();
#	    exit;
#	  }#end error in call to deleteFiles
#      }#end if files that need to be deleted

    if (is_dir("../data/download_data/$_SESSION[user_name]"))
      {
        $ret_val = deleteExpiredFiles($_SESSION['user_name']);
        if ($ret_val != '1')
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #3: $date<br>\n";
            echo "Error in call to deleteExpiredFiles from delete_phenotype_values.php<br>\n";
            echo "$ret_val<br><br>\n";
            echo "Please email the above error using Comments/Help/Support link \n";
	    echo "in the left navigation bar.<br>\n";
	    echo "</body></html>\n";
	    session_unset();
	    session_destroy();
	    $ret_val = disconnect();
	    exit;
	  }#end error in call to deleteExpiredFiles
      }#end if user has own directory in download_data directory
    session_unset();
    session_destroy();
    echo "You have successfully logged out of the system.<br>\n";
    #echo "To log back in click on any of the links on the left navigation bar.<br>\n";
    $ret_val = disconnect();
    exit;
  }#end if want to end session
else#don't want to end session
  {
    if ((!session_is_registered('user_name')) and (!session_is_registered('user_passwd')))
      {
	if ((!preg_match('/\w+/',$_POST['user_name'])) and (!preg_match('/\w+/',$_POST['user_passwd'])))
	  {
	    $ret_val = printLoginPage('delete_phenotype_values.php');
            if ($ret_val != '1')
              {
                $date = date('Y-m-d H:i:s');
		echo "Error #4: $date<br>\n";
		echo "Error in call to printLoginPage from delete_phenotype_values.php<br>\n";
		echo "$ret_val<br><br>\n";
		echo "Please email the above error using Comments/Help/Support link \n";
		echo "in the left navigation bar.<br>\n";
		echo "</body></html>\n";
		session_unset();
		session_destroy();
		$ret_val = disconnect();
		exit;
	      }#end if error in call to printLoginPage
	  }#end if haven't started a session yet
        else #have started a session
	  {
	    $auth_user = authenticate($_POST['user_name'],$_POST['user_passwd'],$_POST['rand'],$access_type_required,$_POST['database_id']);
	    if (($auth_user != '1') and ($auth_user != '0'))
	      {
		$date = date('Y-m-d H:i:s');
		echo "Error #5: $date<br>\n";
		echo "Error in call to authenticate from delete_phenotype_values.php<br>\n";
		echo "$auth_user<br>\n";
		echo "Please email the above error using Comments/Help/Support link \n";
		echo "in the left navigation bar.<br>\n";
		echo "</body></html>\n";
		session_unset();
		session_destroy();
		$ret_val = disconnect();
		exit;
	      }
            if ($auth_user == '0')
	      {
		echo "You are not an authorized user of this website!!<br><br>\n";
		echo "You must be an authorized user to access the data contained in this site.\n";
		echo "If you feel like you should be an authorized user, or want to see if you can\n";
		echo "become an authorized user please contact $contact_person at \n";
                echo "<a href=\"mailto:$contact_email\">$contact_email</a>\n";
		echo "</body></html>\n";
		session_unset();
		session_destroy();
		$ret_val = disconnect();
		exit;
	      }
            if ($auth_user == '1')
	      {
		$_SESSION['user_name']   = $_POST['user_name'];
		$_SESSION['user_passwd'] = $_POST['user_passwd'];
                $_SESSION['database_id'] = $_POST['database_id'];
		$_SESSION['rand']        = $_POST['rand'];
                $_SESSION['entry_time']  = time();
		session_write_close();
	      }
	  }#end else have user name an passwd
      }#end if session variables not registered
    else #session variable are registered
      {
        if ((time() - $_SESSION['entry_time']) > $session_expiration_time)#if session has expired
          {
            if (key_exists('files_on_server',$_SESSION))
              {
                $files_to_delete_array = $_SESSION['files_on_server'];
                $ret_val = deleteFiles($files_to_delete_array);
                if ($ret_val != 1)
                  {
                    $date = date('Y-m-d H:i:s');
                    echo "Error #6: $date<br>\n";
                    echo "Error in call to deleteFiles from delete_phenotype_values.php<br>\n";
                    echo "$ret_val<br><br>\n";
                    echo "Please email the above error using Comments/Help/Support link \n";
		    echo "in the left navigation bar.<br>\n";
                    session_unset();
                    session_destroy();
                    $ret_val = disconnect();
                    exit;
                  }#end if error in call to deleteFiles
              }#end if files that need to be deleted

            if (is_dir("../data/download_data/$_SESSION[user_name]"))
              {
                $ret_val = deleteExpiredFiles($_SESSION['user_name']);
                if ($ret_val != 1)
                  {
                    $date = date('Y-m-d H:i:s');
                    echo "Error #7: $date<br>\n";
                    echo "Error in call to deleteExpiredFiles from delete_phenotype_values.php<br>\n";
                    echo "$ret_val<br><br>\n";
                    echo "Please email the above error using Comments/Help/Support link \n";
		    echo "in the left navigation bar.<br>\n";
                    session_unset();
                    session_destroy();
                    $ret_val = disconnect();
                    exit;
                  }#end if error in call to deleteExpiredFiles
              }#end if user has a data directory
            session_unset();
            session_destroy();

            echo "<b>Your Session has expired.  You must log back into the system.</b><br><br><br>\n";
            $ret_val = printLoginPage('delete_phenotype_values.php');
            if ($ret_val != '1')
              {
                $date = date('Y-m-d H:i:s');
		echo "Error #8: $date<br>\n";
		echo "Error in call to printLoginPage from delete_phenotype_values.php<br>\n";
		echo "$ret_val<br><br>\n";
		echo "Please email the above error using Comments/Help/Support link \n";
		echo "in the left navigation bar.<br>\n";
		echo "</body></html>\n";
		session_unset();
		session_destroy();
		$ret_val = disconnect();
		exit;
	      }#end if error in call to printLoginPage
          }#end if session has expired 
        else #session hasn't expired
          {
	    if ((!session_is_registered('database_id')) and (!isset($_POST['database_id'])))
	      {
		$ret_val = printDatabaseSelectPage('delete_phenotype_values.php');
		if ($ret_val != '1')
		  {
		    $date = date('Y-m-d H:i:s');
		    echo "Error #9: $date<br>\n";
		    echo "Error in call to printDatabaseSelectPage from delete_phenotype_values.php<br>\n";
		    echo "$ret_val<br><br>\n";
		    echo "Please email the above error using Comments/Help/Support link \n";
		    echo "in the left navigation bar.<br>\n";
		    echo "</body></html>\n";
		    session_unset();
		    session_destroy();
		    $ret_val = disconnect();
		    exit;
		  }#end if error in call to printDatabaseSelectPage
	      }#end if the database id session variable isn't set and haven't selected
	    else #all the session variables are set
	      {
		$user_name   = $_SESSION['user_name'];
		$user_passwd = $_SESSION['user_passwd'];
		if (session_is_registered('database_id')){$database_id = $_SESSION['database_id'];}
		else #set database id session variable
		  {
		    $database_id = $_POST['database_id']; 
		    $_SESSION['database_id'] = $_POST['database_id'];
		  }#end else set database id session variable
		$rand        = $_SESSION['rand'];
		$_SESSION['entry_time'] = time();
		$auth_user = authenticate($user_name,$user_passwd,$rand,$access_type_required,$database_id);
		if (($auth_user != '1') and ($auth_user != '0'))
		  {
		    $date = date('Y-m-d H:i:s');
		    echo "Error #10: $date<br>\n";
		    echo "Error in call to authenticate from delete_phenotype_values.php<br>\n";
		    echo "$auth_user<br>\n";
		    echo "Please email the above error using Comments/Help/Support link \n";
		    echo "in the left navigation bar.<br>\n";
		    echo "</body></html>\n";
		    session_unset();
		    session_destroy();
		    $ret_val = disconnect();
		    exit;
		  }
		if ($auth_user == '0')
		  {
		    echo "You are not an authorized user of this website!!<br><br>\n";
		    echo "You must be an authorized user to access the data contained in this site.\n";
		    echo "If you feel like you should be an authorized user, or want to see if you can\n";
		    echo "become an authorized user please contact $contact_person \n";
		    echo "at <a href=\"mailto:$contact_email\">$contact_email</a>\n";
		    echo "</body></html>\n";
		    session_unset();
		    session_destroy();
		    $ret_val = disconnect();
		    exit;
		  }
	      }#end else all the session variables are set
          }#end else session hasn't expired
      }#end else registered values
  }#end else not ending session

if ($auth_user == '1')
  {
    $database_id = $_SESSION['database_id'];
    #3     Determine if know phenotype value or want list
    if (!preg_match('/^\d$/',$want_list))
      {
	echo "<form action=\"delete_phenotype_values.php\" name=\"form\" method=\"POST\" target=\"main\">\n";
	echo "This from will allow you to delete any phenotype value record in the database.<br>\n";
        echo "If you only want to delete one phenotype value at \n";
	echo "a time and you know the individual and phenotype name of the phenotype value, \n";
        echo "select the 'I Know Which Phenotype Value I Want to Delete' button.  \n";
	echo "If you want to delete multiple individuals' phenotype values for a specific phenotype or \n";
        echo "you don't know which phenotype values you want to delete, select the \n";
	echo "'Want a List of All Phenotype Values' button.  If you want to delete multiple phenotype values and \n";
        echo "you know the individual(s) and phenotype(s) names you can upload a file with the data to delete by selecting \n";
        echo " the 'Upload a File of Phenotype Values' button.  Please select how you would like to proceed:<br><br>\n";

        echo "<table>\n";
	echo "<tr><td><input type=\"radio\" name=\"want_list\" value=\"0\" checked></td><td>I Know Which Phenotype Value I Want to Delete</td></tr>\n";
	echo "<tr><td><input type=\"radio\" name=\"want_list\" value=\"1\"></td><td>I Want a List of All the Phenotype Values</td></tr>\n";
        echo "<tr><td><input type=\"radio\" name=\"want_list\" value=\"2\"></td><td>Upload a File of Phenotype Values</td></tr>\n";
        echo "</table>\n";
        echo "<br>\n";

	echo "<table align=\"center\">\n";
        echo "<tr>\n";
	echo "<td><input type=\"submit\" name=\"submit\" value=\"Select Phenotype Value(s) to Delete\"></td>\n";
        echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selection Mode\"></td>\n";
        echo "</tr>\n";
        echo "</form>\n";
        echo "</table>\n";
        echo "<br><br>\n\n";

        $ret_val = printLogOutButton('delete_phenotype_values.php');
      }#end determine whether web form or list

    #4     If know phenotype value, give web form
    if (($want_list == '0') and
	(!preg_match('/\d+/',$individual_id)) and
	(!preg_match('/\d+/',$ped_id)) and
	(!preg_match('/\w+/',$phenotype_id))
       )
      {
	echo "<form action=\"delete_phenotype_values.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(this))\">\n";
	echo "Please select the Pedigree ID of the Individual whose phenotype value you would like to delete, \n";
        echo "and Phenotype Name of the value to delete:<br><br>\n";
	echo "<table>\n";
	echo "<tr><td>Pedigree ID:</td><td>\n";
        $jscript_1 = printPedigreeSelectList($database_id);
        if ($jscript_1 == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #11: $date<br>\n";
            echo "Error in call to printPedigreeSelectList from delete_phenotype_values.php<br>\n";
            echo "See error message from printPedigreeSelectList above for an explanation.<br><br>\n";
            echo "Please email the above error using the Comments/Help/Support link on the \n";
	    echo "left navigation bar.\n";
            echo "</form>\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
          }
        if ($jscript_1 == 'no_defined_pedigrees')
          {
            $date = date('Y-m-d H:i:s');
            echo "This message was generated at $date<br>\n";
            echo "</form>\n";
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }
        echo "</td></tr>\n";
        echo "<tr><td>&nbsp;</td></tr>\n";
	echo "<tr><td>Phenotype Name:</td><td>\n";
        $jscript_2 = printPhenotypeSelectList($database_id,'0','all');
        if ($jscript_2 == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #12: $date<br>\n";
            echo "Error in call to printPhenotypeSelectList from delete_phenotype_values.php<br>\n";
            echo "See error message from printPhenotypeSelectList above for an explanation.<br><br>\n";
            echo "Please email the above error using the Comments/Help/Support link on the \n";
	    echo "left navigation bar.\n";
            echo "</form>\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
          }
        if ($jscript_2 == 'no_defined_phenotypes')
          {
            $date = date('Y-m-d H:i:s');
            echo "This message was generated at $date<br>\n";
            echo "</form>\n";
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }
        echo "</td></tr>\n";
	echo "</table>\n";
        echo "<br><br>\n";

	echo "<input type=\"hidden\" name=\"want_list\" value=\"0\">\n";
	echo "<table align=\"center\">\n";
        echo "<tr>\n";
	echo "<td><input type=\"submit\" name=\"submit\" value=\"Select Individual Whose Phenotype Value To Delete\"></td>\n";
	echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Pedigree ID and Phenotype Name\"></td>\n";
        echo "</tr>\n";
        echo "</form>\n";
        echo "</table>\n";
        echo "<br><br>\n";
        
        $ret_val = printLogOutButton('delete_phenotype_values.php');
	echo "\n\n";

        $jscript_1 = preg_replace('/
 else {return true;}
}
$/','',$jscript_1);
        $jscript_2 = preg_replace('/^\{
/','',$jscript_2);
        $jscript_2 = preg_replace('/ else {return true;}
}$/','',$jscript_2);
        $jscript = "$jscript_1\n$jscript_2\nreturn true;";
	echo "<script>\n";
	echo "function Validate(form)\n";
        echo "$jscript\n";
	echo "</script>\n";
      }#end if know phenotype value to delete

    #5 Have pedigree and phenotype, but not individual
    if (($want_list == '0') and
	(!preg_match('/\d+/',$individual_id)) and
	(preg_match('/\d+/',$ped_id)) and
	(preg_match('/\w+/',$phenotype_id))
       )
      {
        $field_list = array('pedigree_table_id');
        $ped_rec_list = tableQuery('pedigree_table',$database_id,$field_list,$ped_id,'equal','all');
        if (!is_array($ped_rec_list))
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #13: $date<br>\n";
            echo "Error in call to tableQuery from delete_phenotype_values.php<br>\n";
            echo "$ped_rec_list<br><br>\n";
            echo "Please email the above error using the Comments/Help/Support link on the \n";
	    echo "left navigation bar.\n";
            echo "</form>\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
          }#end error in call to tableQuery
        if (sizeof($ped_rec_list) == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "There is no pedigree in the database with pedigree table id $ped_id.  It appears that the pedigree, and all the associated individuals ";
            echo "and their values, were deleted from the database before you submitted your request to delete a phenotype value of someone in that pedigree.<br>\n";
            echo "This message was generated at $date.\n";
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end if no pedigree
        elseif (sizeof($ped_rec_list) == '1') {$ped_rec = $ped_rec_list[0];}
        else
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #14: $date<br>\n";
            echo "The user wanted to delete a phenotype value for an individual with pedigree table id $ped_id.  But when queried the pedigree_table with ";
            echo "that id multiple records were returned.<br><br>\n";
            echo "Please email the above error using the Comments/Help/Support link on the \n";
	    echo "left navigation bar.\n";
            echo "</form>\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
          }#end error in data integrity

        $field_list = array('phenotype_id');
        $phenotype_rec_list = tableQuery('phenotype_table',$database_id,$field_list,$phenotype_id,'equal','all');
        if (!is_array($phenotype_rec_list))
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #15: $date<br>\n";
            echo "Error in call to tableQuery from delete_phenotype_values.php<br>\n";
            echo "$phenotype_rec_list<br><br>\n";
            echo "Please email the above error using the Comments/Help/Support link on the \n";
	    echo "left navigation bar.\n";
            echo "</form>\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
          }#end error in call to tableQuery
        if (sizeof($phenotype_rec_list) == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "There is no phenotype in the database with phenotype id $phenotype_id.  It appears that the phenotype, and all the associated ";
            echo "values, were deleted from the database before you submitted your request to delete a phenotype value for that phenotype.<br>\n";
            echo "This message was generated at $date.\n";
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end if no pedigree
        elseif (sizeof($phenotype_rec_list) == '1') {$phenotype_rec = $phenotype_rec_list[0];}
        else
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #16: $date<br>\n";
            echo "The user wanted to delete a phenotype value for phenotype id $phenotype_id.  But when queried the phenotype_table with ";
            echo "that id multiple records were returned.<br><br>\n";
            echo "Please email the above error using the Comments/Help/Support link on the \n";
	    echo "left navigation bar.\n";
            echo "</form>\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
          }#end error in data integrity

	echo "<form action=\"delete_phenotype_values.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(this))\">\n";
	echo "Please select the Individual ID from Pedigree ID $ped_rec[pedigree_id] whose phenotype value for Phenotype Name $phenotype_rec[phenotype_name] \n";
        echo "you would like to delete:<br><br>\n";
	echo "<table>\n";
	echo "<tr><td>Individual ID:</td><td>\n";
        $jscript = printIndividualSelectList($database_id,$ped_id);
        if ($jscript_1 == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #17: $date<br>\n";
            echo "Error in call to printIndividualSelectList from delete_phenotype_values.php<br>\n";
            echo "See error message from printIndividualSelectList above for an explanation.<br><br>\n";
            echo "Please email the above error using the Comments/Help/Support link on the \n";
	    echo "left navigation bar.\n";
            echo "</form>\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
          }
        if ($jscript == 'no_defined_individuals')
          {
            $date = date('Y-m-d H:i:s');
            echo "This message was generated at $date<br>\n";
            echo "</form>\n";
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }
        echo "</td></tr>\n";
	echo "</table>\n";
        echo "<br><br>\n";

	echo "<input type=\"hidden\" name=\"want_list\" value=\"0\">\n";
        echo "<input type=\"hidden\" name=\"pedigree_id\" value=\"$ped_id\">\n";
        echo "<input type=\"hidden\" name=\"phenotype_id\" value=\"$phenotype_id\">\n";
	echo "<table align=\"center\">\n";
        echo "<tr>\n";
	echo "<td><input type=\"submit\" name=\"submit\" value=\"Delete Phenotype Value\"></td>\n";
	echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Individual ID\"></td>\n";
        echo "</tr>\n";
        echo "</form>\n";
        echo "</table>\n";
        echo "<br><br>\n\n";
        
        $ret_val = printLogOutButton('delete_phenotype_values.php');
	echo "\n\n";

        echo "<script>\n";
	echo "function Validate(form)\n";
        echo "$jscript\n";
	echo "</script>\n";
      }#end if know phenotype value to delete

    #6     Get form data, and delete
    if (($want_list == '0') and
	(preg_match('/\d+/',$individual_id)) and
	(preg_match('/\d+/',$ped_id)) and
	(preg_match('/\w+/',$phenotype_id))
       )
      {
        $rand = rand();
        $tmp_log_file = "log_file_"."$rand".".txt";
        $log_file_handle = fopen("../data/$tmp_log_file","w");
        if (!$log_file_handle)
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #18: $date<br>\n";
            echo "Error in call to fopen from delete_phenotype_values.php<br>\n";
            echo "Unable to open log file to keep track of when, what, and by whom data is altered in the database<br><br>\n";
	    echo "Please email the above error using the Comments/Help/Support link \n";
	    echo "on the left navigation bar.\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
	  }#end error in call to fopen
        $log_file_statement = "";

	$field_list_array = array('pedigree_table_id','individual_table_id');
	$term_array       = array($ped_id,$individual_id);
	$mode_array       = array('equal','equal');
	$indiv_rec_list   = compoundTableQuery('individual_table',$database_id,$field_list_array,$term_array,$mode_array,'and','all');
	if (!is_array($indiv_rec_list))
	  {
            $date = date('Y-m-d H:i:s');
	    echo "Error #19: $date<br>\n";
	    echo "Error in call to compoundTableQuery from delete_phenotype_values.php<br>\n";
	    echo "$indiv_rec_list<br><br>\n";
	    echo "Please email the above error using the Comments/Help/Support link on the \n";
	    echo "left navigation bar.\n";
	    echo "</body></html>\n";
            fclose($log_file_handle);
            chmod("../data/$tmp_log_file",0777);
            $command = "rm ../data/$tmp_log_file";
            exec($command,$output_array,$return_code);
	    $ret_val = disconnect();
	    exit;
	  }#end error in call to compoundTableQuery
	if (sizeof($indiv_rec_list) == '0')
          {
            $date = date('Y-m-d H:i:s');
	    echo "The individual you selected no longer exists in the database<br>\n";
            echo "This message was generated at $date<br>\n";
            fclose($log_file_handle);
            chmod("../data/$tmp_log_file",0777);
            $command = "rm ../data/$tmp_log_file";
            exec($command,$output_array,$return_code);
            $ret_val = disconnect();
            exit;
	  }#end if no individual
	$indiv_rec = $indiv_rec_list[0];
	$indiv_table_id = $indiv_rec['individual_table_id'];

	$field_list_array = array('phenotype_id');
	$phenotype_rec_list = tableQuery('phenotype_table',$database_id,$field_list_array,$phenotype_id,'equal','all');
	if (!is_array($phenotype_rec_list))
	  {
            $date = date('Y-m-d H:i:s');
	    echo "Error #20: $date<br>\n";
	    echo "Error in call to tableQuery from delete_phenotype_values.php<br>\n";
	    echo "$phenotype_rec_list<br><br>\n";
	    echo "Please email the above error using the Comments/Help/Support link on the<br>\n";
	    echo "left navigation bar.<br>\n";
	    echo "</body></html>\n";
            fclose($log_file_handle);
            chmod("../data/$tmp_log_file",0777);
            $command = "rm ../data/$tmp_log_file";
            exec($command,$output_array,$return_code);
	    $ret_val = disconnect();
	    exit;
	  }#end error in call to distinctTableQuery
	if (sizeof($phenotype_rec_list) == '0')
	  {
            $date = date('Y-m-d H:i:s');
	    echo "The phenotype with phenotype id $phenotype_id has been deleted from the database.<br> \n";
            echo "This message was generated at $date<br>\n";
            fclose($log_file_handle);
            chmod("../data/$tmp_log_file",0777);
            $command = "rm ../data/$tmp_log_file";
            exec($command,$output_array,$return_code);
            $ret_val = disconnect();
            exit;
	  }#end if no marker
	$phenotype_rec     = $phenotype_rec_list[0];
        $type_of_phenotype = $phenotype_rec['type_of_phenotype'];

	$field_list_array = array('individual_table_id','phenotype_id');
	$term_array       = array($indiv_table_id,$phenotype_id);
	$mode_array       = array('equal','equal');
        if ($type_of_phenotype == 'quantitative')
	  {$pheno_value_rec_list = compoundTableQuery('quantitative_phenotype_value_table',$database_id,$field_list_array,$term_array,$mode_array,'and','all');}
        if ($type_of_phenotype == 'qualitative')
	  {$pheno_value_rec_list = compoundTableQuery('qualitative_phenotype_value_table',$database_id,$field_list_array,$term_array,$mode_array,'and','all');}
	if (!is_array($pheno_value_rec_list))
	  {
            $date = date('Y-m-d H:i:s');
	    echo "Error #21: $date<br>\n";
	    echo "Error in call to compoundTableQuery from delete_phenotype_values.php<br>\n";
	    echo "$pheno_value_rec_list<br><br>\n";
	    echo "Please email the above error using the Comments/Help/Support link on the \n";
	    echo "left navigation bar.\n";
	    echo "</body></html>\n";
            fclose($log_file_handle);
            chmod("../data/$tmp_log_file",0777);
            $command = "rm ../data/$tmp_log_file";
            exec($command,$output_array,$return_code);
	    $ret_val = disconnect();
	    exit;
	  }#end error in call to compoundTableQuery
	if (sizeof($pheno_value_rec_list) == '0')
          {
            echo "Individual $indiv_rec[individual_id] in Pedigree $indiv_rec[pedigree_id] does not have a phenotype value \n";
            echo "for Phenotype $phenotype_rec[phenotype_name]<br>\n";
	    echo "</body></html>\n";
            fclose($log_file_handle);
            chmod("../data/$tmp_log_file",0777);
            $command = "rm ../data/$tmp_log_file";
            exec($command,$output_array,$return_code);
	    $ret_val = disconnect();
	    exit;
          }
        else
          {
	    $pheno_value_rec = $pheno_value_rec_list[0];
	    if ($type_of_phenotype == 'quantitative'){$pheno_value_id  = $pheno_value_rec['quantitative_phenotype_value_id'];}
            if ($type_of_phenotype == 'qualitative') {$pheno_value_id  = $pheno_value_rec['qualitative_phenotype_value_id'];}
          }

	$ret_val = deletePhenotypeValue($pheno_value_id,$type_of_phenotype);
	if ($ret_val != '1')
	  {
            $date = date('Y-m-d H:i:s');
	    echo "Error #22: $date<br>\n";
	    echo "Error in call to deletePhenotypeValue from delete_phenotype_values.php<br>\n";
	    echo "$ret_val<br><br>\n";
	    echo "Please email the above error using the Comments/Help/Support link on the \n";
	    echo "left navigation bar.\n";
	    echo "</body></html>\n";
            fclose($log_file_handle);
            chmod("../data/$tmp_log_file",0777);
            $command = "rm ../data/$tmp_log_file";
            exec($command,$output_array,$return_code);
	    $ret_val = disconnect();
	    exit;
	  }#end error in call to deletePhenotypeValue
        $date = date('Y-m-d H:i:s');
        $log_file_statement.= "$date:\t$_SESSION[user_name] Deleted $type_of_phenotype Phenotype Value $pheno_value_id ";
        $log_file_statement.= "from Database ID $database_id, for Individual Table ID $indiv_table_id and Phenotype ID $phenotype_id.\n";
        fwrite($log_file_handle,$log_file_statement);
        fclose($log_file_handle);
        chmod("../data/$tmp_log_file",0777);
        $command = "cat ../data/$tmp_log_file >> ../data/log_file.txt";
        exec($command,$output_array,$return_code);
        $command = "rm ../data/$tmp_log_file";
        exec($command,$output_array,$return_code);
	echo "Successfully deleted the Phenotype Value for Phenotype $phenotype_rec[phenotype_name] for ";
        echo "Individual $indiv_rec[individual_id] in Pedigree $indiv_rec[pedigree_id]<br><br>\n";

        echo "Now emailing all the users of the database to inform them of the new information in the database.<br>\n";

        $database_rec_list = getDatabase($database_id);
        if (!is_array($database_rec_list))
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #23: $date<br>\n";
            echo "Error in call to getDatabase from delete_phenotype_values.php<br>\n";
            echo "$database_rec_list<br><br>\n";
            echo "Please email the above error using the Comments/Help/Support link on the \n";
            echo "left navigation bar.\n";
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end error in call to getDatabase
        $database_rec = $database_rec_list[0];

        $user_rec_list = getAllUsers();
        if (!is_array($user_rec_list))
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #24: $date<br>\n";
            echo "Error in call to getAllUsers() from delete_phenotype_values.php<br>\n";
            echo "$user_rec_list<br><br>\n";
            echo "Please email the above error using the Comments/Help/Support link on the \n";
            echo "left navigation bar.\n";
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end error in call to getAllUsers
	foreach ($user_rec_list as $user_rec)
          {
            if ($user_rec['user_name'] == 'root') {$root_email = $user_rec['user_email'];}
          }#end foreach user

        $subject = "Phenotype Value Deleted from your Genetic Studies Database";
        $message = "The Phenotype Value for Phenotype $phenotype_rec[phenotype_name] for Individual $indiv_rec[individual_id] in ";
        $message.= "Pedigree $indiv_rec[pedigree_id] was deleted from the database $database_rec[database_name].";
        $header  = "From: $root_email";


        $field_list = array('user_database_link_id');
        $user_database_link_rec_list = tableQuery('user_database_link_table',$database_id,$field_list,'_','wildcard','all');
        if (!is_array($user_database_link_rec_list))
          {
            $date = date('Y-m-d H:i:s');
            echo"Error #25: $date<br>\n";
            echo "Error in call to tableQuery from delete_phenotype_values.php<br>\n";
            echo "$user_database_link_rec_list<br><br>\n";
            echo "Please email the above error using the Comments/Help/Support link on the \n";
            echo "left navigation bar.\n";
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end error in call to tableQuery
	if (sizeof($user_database_link_rec_list) > '0')
          {
            foreach ($user_database_link_rec_list as $user_database_link_rec)
              {
                $user_id = $user_database_link_rec['user_id'];
                $field_list = array('user_id');
                $user_rec_list = getUser($user_id);
                if (!is_array($user_rec_list))
                  {
                    $date = date('Y-m-d H:i:s');
                    echo "Error #26: $date<br>\n";
                    echo "Error in call to getUser from delete_phenotype_values.php<br>\n";
                    echo "$user_rec_list<br><br>\n";
                    echo "Please email the above error using the Comments/Help/Support link on the \n";
                    echo "left navigation bar.\n";
                    echo "</body></html>\n";
                    $ret_val = disconnect();
                    exit;
                  }#end error in call to getUser
		if (sizeof($user_rec_list) == '0') {continue;}
                elseif (sizeof($user_rec_list) == '1') {$user_rec = $user_rec_list[0];}
                else
                  {
                    $date = date('Y-m-d H:i:s');
                    echo "Error #27: $date<br>\n";
                    echo "Tried to send an update email to user id $user_id, but when queried the user table \n";
                    echo "with that id multiple records were returned.<br><br>\n";
                    echo "Please email the above error using the Comments/Help/Support link on the \n";
                    echo "left navigation bar.\n";
                    echo "</body></html>\n";
                    $ret_val = disconnect();
                    exit;
                  }#end error in data integrity
		$user_email_address = $user_rec['user_email'];

                $ret_val = mail($user_email_address,$subject,$message,$header);
                #if (!$ret_val) {echo "Unable to send an update email to user $user_rec[user_name]<br>\n";}
                #else           {echo "Sent an update email to user $user_name<br>\n";}
              }#end foreach user associated with database
	  }#end if have users associated with database
	echo "Finished sending update emails to all the users<br>\n";
      }#end get form data and delete

    #7  Want a list, but haven't selected phenotypes
    if (($want_list == '1') and
	(!preg_match('/^\d+$/',$pheno_multiple_pages)) and
        (sizeof($pheno_ids) == '0') and
        (!preg_match('/^\d+$/',$indiv_multiple_pages)) and
        (sizeof($indivs_id) == '0')
       )
      {
        $field_list = array('phenotype_id');
        $num_phenos = countTableQuery('phenotype_table',$database_id,$field_list,'_','wildcard');
        if (!preg_match('/^\d+$/',$num_phenos))
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #28: $date<br>\n";
            echo "Error in call to countTableQuery from delete_phenotype_values.php<br>\n";
            echo "$num_phenos<br><br>\n";
            echo "Please email the above error using the Comments/Help/Support link on the \n";
	    echo "left navigation bar.\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
	  }#end error in call to countTableQuery
        if ($num_phenos == 0)
          {
            $date = date('Y-m-d H:i:s');
            echo "Currently there are no Phenotypes in the database, therefore there are no phenotype values to delete.<br>\n";
            echo "This message was generated at $date.\n";
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end if no phenotypes

        if ($num_phenos > $max_records_to_print_per_page)
          {
            $pheno_multiple_pages = 1;
            $from                 = "0";
            $to                   = $max_records_to_print_per_page - 1;
            $range                = "$from"."-"."$to";
            $pheno_rec_list = tableQuery('phenotype_table',$database_id,$field_list,'_','wildcard',$range,'phenotype_name');
            if (!is_array($pheno_rec_list))
              {
                $date = date('Y-m-d H:i:s');
                echo "Error #29: $date<br>\n";
                echo "Error in call to tableQuery from delete_phenotype_values.php<br>\n";
                echo "$pheno_rec_list<br><br>\n";
                echo "Please email the above error using the Comments/Help/Support link on the \n";
	        echo "left navigation bar.\n";
	        echo "</body></html>\n";
	        $ret_val = disconnect();
	        exit;
	      }#end error in call to tableQuery
            if (sizeof($pheno_rec_list) == 0)
              {
                $date = date('Y-m-d H:i:s');
                echo "Currently there are no Phenotypes in the database, therefore there are no phenotype values to delete.<br>\n";
                echo "This message was generated at $date.\n";
                echo "</body></html>\n";
                $ret_val = disconnect();
                exit;
              }#end if no phenotypes
            $function_to_call      = 'tableQuery';
            $table_name            = 'phenotype_table';
            $term                  = '_';
            $mode                  = 'wildcard';
            $order_by              = 'phenotype_name';
            $last_record_retrieved = $max_records_to_print_per_page;
          }#end if multiple pages
        else #only a single page
          {
            $pheno_multiple_pages = 0;
            $range                = "all";
            $pheno_rec_list = tableQuery('phenotype_table',$database_id,$field_list,'_','wildcard',$range,'phenotype_name');
            if (!is_array($pheno_rec_list))
              {
                $date = date('Y-m-d H:i:s');
                echo "Error #30: $date<br>\n";
                echo "Error in call to tableQuery from delete_phenotype_values.php<br>\n";
                echo "$pheno_rec_list<br><br>\n";
                echo "Please email the above error using the Comments/Help/Support link on the \n";
	        echo "left navigation bar.\n";
	        echo "</body></html>\n";
	        $ret_val = disconnect();
	        exit;
	      }#end error in call to tableQuery
            if (sizeof($pheno_rec_list) == 0)
              {
                $date = date('Y-m-d H:i:s');
                echo "Currently there are no Phenotypes in the database, therefore there are no phenotype values to delete.<br>\n";
                echo "This message was generated at $date.\n";
                echo "</body></html>\n";
                $ret_val = disconnect();
                exit;
              }#end if no phenotypes
          }#end else only a single page

	echo "Please select the Phenotype(s) you would like to delete Individuals' values for:<br><br>\n";

        $ret_val = printLogOutButton('delete_phenotype_values.php');
        echo "<br><br>\n\n";

        if ($pheno_multiple_pages)
          {
            echo "<table align=\"center\">\n";
            echo "<form action=\"delete_phenotype_values.php\" name=\"skip_form1\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
            echo "<input type=\"hidden\" name=\"want_list\" value=\"$want_list\">\n";
            echo "<input type=\"hidden\" name=\"pheno_multiple_pages\" value=\"0\">\n";
            echo "<input type=\"hidden\" name=\"pheno_id\" value=\"\">\n";
            echo "<tr>\n";
            echo "<td><input type=\"submit\" name=\"submit\" value=\"Select Individuals to Delete Values For\" onClick=\"copyData('skip_form1')\"></td>\n";
            echo "</form>\n\n";

            echo "<form action=\"delete_phenotype_values.php\" name=\"form\" method=\"POST\" target=\"main\">\n";
            $num_records_left = $num_phenos - $max_records_to_print_per_page;
            if ($num_records_left > $max_records_to_print_per_page)
              {echo "<td><input type=\"submit\" name=\"submit\" value=\"Get Next $max_records_to_print_per_page Phenotypes\"></td>\n";}
            else
              {echo "<td><input type=\"submit\" name=\"submit\" value=\"Get Next $num_records_left Phenotypes\"></td>\n";}
            echo "</tr>\n";
            echo "</table>\n";
            echo "<br>\n";
            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            echo "<td>&nbsp;</td>\n";
            echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selected Phenotype\"></td>\n";
            echo "</tr>\n";
            echo "</table>\n";
          }#end if multiple pages
        else #only a single page
          {
            echo "<form action=\"delete_phenotype_values.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(this))\">\n";
            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            echo "<td><input type=\"submit\" name=\"submit\" value=\"Select Individual(s)\"></td>\n";
	    echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selected Phenotype\"></td>\n";
            echo "</tr>\n";
            echo "</table>\n";
          }#end else only a single page
        echo "<br><br>\n";

        $jscript = printPhenotypeCheckList($pheno_rec_list);
        if ($jscript == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #31: $date<br>\n";
            echo "Error in call to printPhenotypeCheckList from delete_phenotype_values.php<br>\n";
            echo "See the error message above from printPhenotypeCheckList for an explanation.<br><br>\n";
            echo "Please email the above error using the Comments/Help/Support link on the \n";
	    echo "left navigation bar.\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
	  }#end error in call to printPhenotypeCheckList
        echo "<br><br>\n";

	echo "<input type=\"hidden\" name=\"want_list\" value=\"1\">\n";
        echo "<input type=\"hidden\" name=\"pheno_multiple_pages\" value=\"$pheno_multiple_pages\">\n";
        if ($pheno_multiple_pages)
          {
            echo "<input type=\"hidden\" name=\"function_to_call\" value=\"$function_to_call\">\n";
	    if ($function_to_call == 'tableJoin') 
	      {
		foreach ($table_name as $name) {echo "<input type=\"hidden\" name=\"table_name[]\" value=\"$name\">\n";}
	      }
	    else{echo "<input type=\"hidden\" name=\"table_name\" value=\"$table_name\">\n";}

	    foreach ($field_list as $field){echo "<input type=\"hidden\" name=\"field_list[]\" value=\"$field\">\n";}

	    if (is_array($term)){foreach ($term as $sub_term){echo "<input type=\"hidden\" name=\"term[]\" value=\"$sub_term\">\n";}}
	    else {echo "<input type=\"hidden\" name=\"term\" value=\"$term\">\n";}

	    if (is_array($mode)){foreach ($mode as $sub_mode){echo "<input type=\"hidden\" name=\"mode[]\" value=\"$sub_mode\">\n";}}
	    else {echo "<input type=\"hidden\" name=\"mode\" value=\"$mode\">\n";}

	    if ($function_to_call == 'compoundTableQuery') {echo "<input type=\"hidden\" name=\"join\" value=\"$join\">\n";}

	    echo "<input type=\"hidden\" name=\"order_by\" value=\"$order_by\">\n";
            echo "<input type=\"hidden\" name=\"last_record_retrieved\" value=\"$last_record_retrieved\">\n";

            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            echo "<td>&nbsp;</td>\n";
            echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selected Phenotype\"></td>\n";
            echo "</tr>\n";
            echo "</table>\n";
            echo "<br>\n";
            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            if ($num_records_left > $max_records_to_print_per_page)
              {echo "<td><input type=\"submit\" name=\"submit\" value=\"Get Next $max_records_to_print_per_page Phenotypes\"></td>\n";}
            else
              {echo "<td><input type=\"submit\" name=\"submit\" value=\"Get Next $num_records_left Phenotypes\"></td>\n";}
            echo "</form>\n\n";

            echo "<form action=\"delete_phenotype_values.php\" name=\"skip_form2\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
            echo "<input type=\"hidden\" name=\"want_list\" value=\"$want_list\">\n";
            echo "<input type=\"hidden\" name=\"pheno_multiple_pages\" value=\"0\">\n";
            echo "<input type=\"hidden\" name=\"pheno_id\" value=\"\">\n";
            echo "<td><input type=\"submit\" name=\"submit\" value=\"Select Individuals to Delete Values For\" onClick=\"copyData('skip_form2')\"></td>\n";
            echo "</form>\n";
            echo "</tr>\n";
            echo "</table>\n";
          }#end if multiple pages
        else #only a single page
          {
	    echo "<table align=\"center\">\n";
            echo "<tr>\n";
	    echo "<td><input type=\"submit\" name=\"submit\" value=\"Select Individual(s)\"></td>\n";
	    echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selected Phenotype\"></td>\n";
            echo "</tr>\n";
            echo "</form>\n";
            echo "</table>\n";
          }#end else only a single gene
        echo "<br><br>\n";

        $ret_val = printLogOutButton('delete_phenotype_values.php');
	echo "\n\n";

	echo "<script>\n";
	echo "function Validate(form)\n";
	echo "$jscript\n";
        if ($pheno_multiple_pages)
          {
            echo "\n";
            echo "function copyData(formname)\n";
            echo "{\n";
            echo "  var pheno_string = new Array();\n";
            echo "  var length = 0;\n";
            echo "  if (document.form.elements['pheno_id[]'].length > 1){\n";
            echo "    for (var i=0; i<document.form.elements['pheno_id[]'].length; i++){\n";
            echo "      if (document.form.elements['pheno_id[]'][i].type == \"hidden\"){\n";
            echo "        if (document.form.elements['pheno_id[]'][i].value != \"\"){\n";
            echo "          length = pheno_string.length;\n";
            echo "          pheno_string[length] = document.form.elements['pheno_id[]'][i].value;}}\n";
            echo "      else{\n";
            echo "        if (document.form.elements['pheno_id[]'][i].checked){\n";
            echo "          length = pheno_string.length;\n";
            echo "          pheno_string[length] = document.form.elements['pheno_id[]'][i].value;}}}}\n";
            echo "  else{\n";
            echo "    if (document.form.elements['pheno_id[]'].type == \"hidden\"){\n";
            echo "      length = pheno_string.length;\n";
            echo "      pheno_string[length] = document.form.elements['pheno_id[]'].value;}\n";
            echo "    else{\n";
            echo "      if (document.form.elements['pheno_id[]'].checked){\n";
            echo "        length = pheno_string.length;\n";
            echo "        pheno_string[length] = document.form.elements['pheno_id[]'].value;}}}\n";
            echo "  if (formname == 'skip_form1'){\n";
            echo "    document.skip_form1.pheno_id.value = pheno_string;}\n";
            echo "  if (formname == 'skip_form2'){\n";
            echo "    document.skip_form2.pheno_id.value = pheno_string;}\n";
            echo "}\n";
          }#end if multiple pages
	echo "</script>\n";
        unset($pheno_multiple_pages);
      }#end if want list

    #8
    if (($want_list == '1') and
	($pheno_multiple_pages == '1') and
        (!preg_match('/^\d+$/',$indiv_multiple_pages)) and
        (sizeof($indivs_id) == '0')
       )
      {
        if ($function_to_call == 'tableQuery')
          {
            $num_phenos = countTableQuery($table_name,$database_id,$field_list,$term,$mode);
	    if (!preg_match('/^\d+$/',$num_phenos))
	      {
		$date = date('Y-m-d H:i:s');
		echo "Error #32: $date<br>\n";
		echo "Error in call to countTableQuery from delete_phenotype_values.php<br>\n";
		echo "$num_phenos<br><br>\n";
		echo "Please email the above error using The Comments/Help/Support link on the \n";
		echo "left navigation bar.\n";
		echo "</form>\n";
		echo "</body></html>\n";
		$ret_val = disconnect();
		exit;
	      }#end error in call to countTableQuery
	    $records_yet_to_be_retrieved = $num_phenos - $last_record_retrieved;

            if ($records_yet_to_be_retrieved > $max_records_to_print_per_page)
              {
                $pheno_multiple_pages = 1;
		$from = $last_record_retrieved;
		$to   = ($last_record_retrieved-1) + $max_records_to_print_per_page;
		$range = "$from"."-"."$to";
		$pheno_rec_list = tableQuery($table_name,$database_id,$field_list,$term,$mode,$range,$order_by);
		if (!is_array($pheno_rec_list))
		  {
		    $date = date('Y-m-d H:i:s');
		    echo "Error #33: $date<br>\n";
		    echo "Error in call to tableQuery from delete_phenotype_values.php<br>\n";
		    echo "$pheno_rec_list<br><br>\n";
		    echo "Please email the above error using The Comments/Help/Support link on the \n";
		    echo "left navigation bar.\n";
		    echo "</form>\n";
		    echo "</body></html>\n";
		    $ret_val = disconnect();
		    exit;
		  }#end if error in call to tableQuery
                $last_record_retrieved = $to + 1;
	      }#end if multiple pages
	    else #num records fit on one page
	      {
                $pheno_multiple_pages = 0;
		$from = $last_record_retrieved;
		$to   = $num_indivs - 1;
		$range = "$from"."-"."$to";
		$pheno_rec_list = tableQuery($table_name,$database_id,$field_list,$term,$mode,$range,$order_by);
		if (!is_array($pheno_rec_list))
		  {
		    $date = date('Y-m-d H:i:s');
		    echo "Error #34: $date<br>\n";
		    echo "Error in call to tableQuery from delete_phenotype_values.php<br>\n";
		    echo "$pheno_rec_list<br><br>\n";
		    echo "Please email the above error using The Comments/Help/Support link on the \n";
		    echo "left navigation bar.\n";
		    echo "</form>\n";
		    echo "</body></html>\n";
		    $ret_val = disconnect();
		    exit;
		  }#end if error in call to tableQuery
              }#end else only a single page
          }#end if function_to_call == tableQuery

        echo "Please select the Phenotype(s) you would like to delete Individuals' values for:<br><br>\n";

        $ret_val = printLogOutButton('delete_phenotype_values.php');
        echo "<br><br>\n\n";

        if ($pheno_multiple_pages)
          {
            echo "<table align=\"center\">\n";
            echo "<form action=\"delete_phenotype_values.php\" name=\"skip_form1\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
            echo "<input type=\"hidden\" name=\"want_list\" value=\"$want_list\">\n";
            echo "<input type=\"hidden\" name=\"pheno_multiple_pages\" value=\"0\">\n";
            echo "<input type=\"hidden\" name=\"pheno_id\" value=\"\">\n";
            echo "<tr>\n";
            echo "<td><input type=\"submit\" name=\"submit\" value=\"Select Individuals to Delete Values For\" onClick=\"copyData('skip_form1')\"></td>\n";
            echo "</form>\n\n";

            echo "<form action=\"delete_phenotype_values.php\" name=\"form\" method=\"POST\" target=\"main\">\n";
            $num_records_left = $num_phenos - $last_record_retrieved;
            if ($num_records_left > $max_records_to_print_per_page)
              {echo "<td><input type=\"submit\" name=\"submit\" value=\"Get Next $max_records_to_print_per_page Phenotypes\"></td>\n";}
            else
              {echo "<td><input type=\"submit\" name=\"submit\" value=\"Get Next $num_records_left Phenotypes\"></td>\n";}
            echo "</tr>\n";
            echo "</table>\n";
            echo "<br>\n";
            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            echo "<td>&nbsp;</td>\n";
            echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selected Phenotype\"></td>\n";
            echo "</tr>\n";
            echo "</table>\n";
          }#end if multiple pages
        else #only a single page
          {
            echo "<form action=\"delete_phenotype_values.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(this))\">\n";
            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            echo "<td><input type=\"submit\" name=\"submit\" value=\"Select Individual(s)\"></td>\n";
	    echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selected Phenotype\"></td>\n";
            echo "</tr>\n";
            echo "</table>\n";
          }#end else only a single page
        echo "<br><br>\n";

        $jscript = printPhenotypeCheckList($pheno_rec_list);
        if ($jscript == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #35: $date<br>\n";
            echo "Error in call to printPhenotypeCheckList from delete_phenotype_values.php<br>\n";
            echo "See the error message above from printPhenotypeCheckList for an explanation.<br><br>\n";
            echo "Please email the above error using the Comments/Help/Support link on the \n";
	    echo "left navigation bar.\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
	  }#end error in call to printPhenotypeCheckList
        echo "<br><br>\n";

	echo "<input type=\"hidden\" name=\"want_list\" value=\"1\">\n";
        echo "<input type=\"hidden\" name=\"pheno_multiple_pages\" value=\"$pheno_multiple_pages\">\n";
        if ($pheno_multiple_pages)
          {
            echo "<input type=\"hidden\" name=\"function_to_call\" value=\"$function_to_call\">\n";
	    if ($function_to_call == 'tableJoin') 
	      {
		foreach ($table_name as $name) {echo "<input type=\"hidden\" name=\"table_name[]\" value=\"$name\">\n";}
	      }
	    else{echo "<input type=\"hidden\" name=\"table_name\" value=\"$table_name\">\n";}

	    foreach ($field_list as $field){echo "<input type=\"hidden\" name=\"field_list[]\" value=\"$field\">\n";}

	    if (is_array($term)){foreach ($term as $sub_term){echo "<input type=\"hidden\" name=\"term[]\" value=\"$sub_term\">\n";}}
	    else {echo "<input type=\"hidden\" name=\"term\" value=\"$term\">\n";}

	    if (is_array($mode)){foreach ($mode as $sub_mode){echo "<input type=\"hidden\" name=\"mode[]\" value=\"$sub_mode\">\n";}}
	    else {echo "<input type=\"hidden\" name=\"mode\" value=\"$mode\">\n";}

	    if ($function_to_call == 'compoundTableQuery') {echo "<input type=\"hidden\" name=\"join\" value=\"$join\">\n";}

	    echo "<input type=\"hidden\" name=\"order_by\" value=\"$order_by\">\n";
            echo "<input type=\"hidden\" name=\"last_record_retrieved\" value=\"$last_record_retrieved\">\n";

            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            echo "<td>&nbsp;</td>\n";
            echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selected Phenotype\"></td>\n";
            echo "</tr>\n";
            echo "</table>\n";
            echo "<br>\n";
            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            if ($num_records_left > $max_records_to_print_per_page)
              {echo "<td><input type=\"submit\" name=\"submit\" value=\"Get Next $max_records_to_print_per_page Phenotypes\"></td>\n";}
            else
              {echo "<td><input type=\"submit\" name=\"submit\" value=\"Get Next $num_records_left Phenotypes\"></td>\n";}
            echo "</form>\n\n";

            echo "<form action=\"delete_phenotype_values.php\" name=\"skip_form2\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
            echo "<input type=\"hidden\" name=\"want_list\" value=\"$want_list\">\n";
            echo "<input type=\"hidden\" name=\"pheno_multiple_pages\" value=\"0\">\n";
            echo "<input type=\"hidden\" name=\"pheno_id\" value=\"\">\n";
            echo "<td><input type=\"submit\" name=\"submit\" value=\"Select Individuals to Delete Values For\" onClick=\"copyData('skip_form2')\"></td>\n";
            echo "</form>\n";
            echo "</tr>\n";
            echo "</table>\n";
          }#end if multiple pages
        else #only a single page
          {
	    echo "<table align=\"center\">\n";
            echo "<tr>\n";
	    echo "<td><input type=\"submit\" name=\"submit\" value=\"Select Individual(s)\"></td>\n";
	    echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selected Phenotype\"></td>\n";
            echo "</tr>\n";
            echo "</form>\n";
            echo "</table>\n";
          }#end else only a single gene
        echo "<br><br>\n";

        $ret_val = printLogOutButton('delete_phenotype_values.php');
	echo "\n\n";

	echo "<script>\n";
	echo "function Validate(form)\n";
	echo "$jscript\n";
        if ($pheno_multiple_pages)
          {
            echo "\n";
            echo "function copyData(formname)\n";
            echo "{\n";
            echo "  var pheno_string = new Array();\n";
            echo "  var length = 0;\n";
            echo "  if (document.form.elements['pheno_id[]'].length > 1){\n";
            echo "    for (var i=0; i<document.form.elements['pheno_id[]'].length; i++){\n";
            echo "      if (document.form.elements['pheno_id[]'][i].type == \"hidden\"){\n";
            echo "        if (document.form.elements['pheno_id[]'][i].value != \"\"){\n";
            echo "          length = pheno_string.length;\n";
            echo "          pheno_string[length] = document.form.elements['pheno_id[]'][i].value;}}\n";
            echo "      else{\n";
            echo "        if (document.form.elements['pheno_id[]'][i].checked){\n";
            echo "          length = pheno_string.length;\n";
            echo "          pheno_string[length] = document.form.elements['pheno_id[]'][i].value;}}}}\n";
            echo "  else{\n";
            echo "    if (document.form.elements['pheno_id[]'].type == \"hidden\"){\n";
            echo "      length = pheno_string.length;\n";
            echo "      pheno_string[length] = document.form.elements['pheno_id[]'].value;}\n";
            echo "    else{\n";
            echo "      if (document.form.elements['pheno_id[]'].checked){\n";
            echo "        length = pheno_string.length;\n";
            echo "        pheno_string[length] = document.form.elements['pheno_id[]'].value;}}}\n";
            echo "  if (formname == 'skip_form1'){\n";
            echo "    document.skip_form1.pheno_id.value = pheno_string;}\n";
            echo "  if (formname == 'skip_form2'){\n";
            echo "    document.skip_form2.pheno_id.value = pheno_string;}\n";
            echo "}\n";
          }#end if multiple pages
	echo "</script>\n";
        unset($pheno_multiple_pages);
      }#end if multiple pages

    #9
    if (($want_list == '1') and
	($pheno_multiple_pages == '0') and
        (sizeof($pheno_ids) > '0') and
        (!preg_match('/^\d+$/',$indiv_multiple_pages)) and
        (sizeof($indivs_id) == '0')
       )
      {
         if (!is_array($pheno_ids)){$pheno_id_term = $pheno_ids;}
         else                      {$pheno_id_term = join($pheno_ids,',');}

         $field_list_array = array('phenotype_id');
         $quant_indiv_table_id_array = distinctTableQuery('individual_table_id','quantitative_phenotype_value_table',$database_id,$field_list_array,$pheno_id_term,'equal','all');
         if (!is_array($quant_indiv_table_id_array))
           {
             $date = date('Y-m-d H:i:s');
	     echo "Error #36: $date<br>\n";
	     echo "Error in call to distinctTableQuery from delete_phenotype_values.php<br>\n";
	     echo "$quant_indiv_table_id_array<br><br>\n";
	     echo "Please email the above error using the Comments/Help/Support link on the \n";
	     echo "left navigation bar.\n";
	     echo "</body></html>\n";
	     $ret_val = disconnect();
	     exit;
	   }#end error in call to distinctTableQuery

         $qual_indiv_table_id_array = distinctTableQuery('individual_table_id','qualitative_phenotype_value_table',$database_id,$field_list_array,$pheno_id_term,'equal','all');
         if (!is_array($qual_indiv_table_id_array))
           {
             $date = date('Y-m-d H:i:s');
	     echo "Error #37: $date<br>\n";
	     echo "Error in call to distinctTableQuery from delete_phenotype_values.php<br>\n";
	     echo "$qual_indiv_table_id_array<br><br>\n";
	     echo "Please email the above error using the Comments/Help/Support link on the \n";
	     echo "left navigation bar.\n";
	     echo "</body></html>\n";
	     $ret_val = disconnect();
	     exit;
	   }#end error in call to distinctTableQuery
         
         if ((sizeof($quant_indiv_table_id_array) == '0') and (sizeof($quant_indiv_table_id_array) == '0'))
           {
             $date = date('Y-m-d H:i:s');
             echo "There are no Individuals in the database that have phenotype values for the selected Phenotypes<br>\n";
             echo "This message was generated at $date<br>\n";
             echo "</body></html>\n";
             $ret_val = disconnect();
             exit;
           }
         $quant_id_term = join($quant_indiv_table_id_array,',');
         $qual_id_term  = join($qual_indiv_table_id_array,',');
         $indiv_table_id_term = "$quant_id_term".","."$qual_id_term";
         unset($quant_indiv_table_id_array);
         unset($qual_indiv_table_id_array);

         $field_list_array = array('individual_table_id');
         $num_indivs = countTableQuery('individual_table',$database_id,$field_list_array,$indiv_table_id_term,'in');
         if (!preg_match('/^\d+$/',$num_indivs))
           {
             $date = date('Y-m-d H:i:s');
	     echo "Error #38: $date<br>\n";
	     echo "Error in call to countTableQuery from delete_phenotype_values.php<br>\n";
	     echo "$num_indivs<br><br>\n";
	     echo "Please email the above error using the Comments/Help/Support link on the \n";
	     echo "left navigation bar.\n";
	     echo "</body></html>\n";
	     $ret_val = disconnect();
	     exit;
	   }#end error in call to countTableQuery
         if ($num_indivs == '0')
           {
             $date = date('Y-m-d H:i:s');
             echo "There are no Individuals in the database that have phenotype values for the selected Phenotypes<br>\n";
             echo "This message was generated at $date<br>\n";
             echo "</body></html>\n";
             $ret_val = disconnect();
             exit;
           }#end if no individuals

         if ($num_indivs > $max_records_to_print_per_page)
           {
             $indiv_multiple_pages = 1;
             $from                 = "0";
             $to                   = $max_records_to_print_per_page - 1;
             $range                = "$from"."-"."$to";
             if ($db_type == 'mysql'){$order_by = 'pedigree_id+0,individual_id+0';}
             if ($db_type == 'pgsql'){$order_by = "pedigree_id::text::integer,individual_id::text::integer";}
             $indiv_rec_list = tableQuery('individual_table',$database_id,$field_list_array,$indiv_table_id_term,'in','all',$order_by);
             if (!is_array($indiv_rec_list))
               {
                 $date = date('Y-m-d H:i:s');
	         echo "Error #39: $date<br>\n";
	         echo "Error in call to tableQuery from delete_phenotype_values.php<br>\n";
	         echo "$indiv_rec_list<br><br>\n";
	         echo "Please email the above error using the Comments/Help/Support link on the \n";
	         echo "left navigation bar.\n";
	         echo "</body></html>\n";
	         $ret_val = disconnect();
	         exit;
	       }#end error in call to tableQuery
             $final_function_call   = 'tableQuery';
             $final_table_name      = 'individual_table';
             $final_field_list      = $field_list_array;
             $final_term            = $indiv_table_id_term;
             $final_mode            = 'in';
             $final_order_by        = $order_by;
             $last_record_retrieved = $max_records_to_print_per_page;
           }#end if multiple pages
         else #only a single page
           {
             $indiv_multiple_pages = 0;
             $range                = "all";
             if ($db_type == 'mysql'){$order_by = 'pedigree_id+0,individual_id+0';}
             if ($db_type == 'pgsql'){$order_by = "pedigree_id::text::integer,individual_id::text::integer";}
             $indiv_rec_list = tableQuery('individual_table',$database_id,$field_list_array,$indiv_table_id_term,'in','all',$order_by);
             if (!is_array($indiv_rec_list))
               {
                 $date = date('Y-m-d H:i:s');
	         echo "Error #40: $date<br>\n";
	         echo "Error in call to tableQuery from delete_phenotype_values.php<br>\n";
	         echo "$indiv_rec_list<br><br>\n";
	         echo "Please email the above error using the Comments/Help/Support link on the \n";
	         echo "left navigation bar.\n";
	         echo "</body></html>\n";
	         $ret_val = disconnect();
	         exit;
	       }#end error in call to tableQuery
           }#end else only a single page

         echo "Select which Individuals for which you want to delete Phenotype Values for the selected Phenotypes<br><br>\n";

         $ret_val = printLogOutButton('delete_phenotype_values.php');
         echo "<br><br>\n";

         if ($indiv_multiple_pages)
           {
             echo "<table align=\"center\">\n";
             echo "<form action=\"delete_phenotype_values.php\" name=\"skip_form1\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
             echo "<input type=\"hidden\" name=\"want_list\" value=\"$want_list\">\n";
             echo "<input type=\"hidden\" name=\"pheno_multiple_pages\" value=\"$pheno_multiple_pages\">\n";
             echo "<input type=\"hidden\" name=\"pheno_id\" value=\"$pheno_id_term\">\n";
             echo "<input type=\"hidden\" name=\"indiv_multiple_pages\" value=\"0\">\n";
             echo "<input type=\"hidden\" name=\"indiv_id\" value=\"\">\n";
             echo "<tr>\n";
             echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Delete Individuals' Phenotype Values\" onClick=\"copyData('skip_form1')\"></td>\n";
             echo "</form>\n\n";

             echo "<form action=\"delete_phenotype_values.php\" name=\"form\" method=\"POST\" target=\"main\">\n";
             $records_left = $num_indivs - $last_record_retrieved;
             if ($records_left > $max_records_to_print_per_page)
               {echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Get Next $max_records_to_print_per_page Individuals\"></td>\n";}
             else
               {echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Get Next $records_left Individuals\"></td>\n";}
             echo "</tr>\n";
             echo "</table>\n";
             echo "<br>\n";
             echo "<table align=\"center\">\n";
             echo "<tr>\n";
             echo "<td align=\"center\">&nbsp;</td>\n";
             echo "<td align=\"center\"><input type=\"reset\" name=\"reset\" value=\"Reset Selected Individuals\"></td>\n";
             echo "</tr>\n";
             echo "</table>\n";
           }#end if multiple pages
         else #only a single page
           {
             echo "<table align=\"center\">\n";
             echo "<form action=\"delete_phenotype_values.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
             echo "<tr>\n";
             echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Delete Individuals' Phenotype Values\" onClick=\"copyData('skip_form1')\"></td>\n";
             echo "<td align=\"center\"><input type=\"reset\" name=\"reset\" value=\"Reset Selected Individuals\"></td>\n";
             echo "</tr>\n";
             echo "</table>\n";
           }#end else only a single page
         echo "<br><br>\n";

         $jscript = printIndividualCheckList($indiv_rec_list,$database_id);
         if ($jscript == '0')
           {
             $date = date('Y-m-d H:i:s');
             echo "Error #41: $date<br>\n";
             echo "Error in call to printIndividualCheckList from delete_phenotype_values.php<br>\n";
             echo "See error message from printIndividualCheckList above for an explanation.<br><br>\n";
             echo "Please email the above error using the Comments/Help/Support link on the \n";
	     echo "left navigation bar.\n";
             echo "</form>\n";
	     echo "</body></html>\n";
	     $ret_val = disconnect();
	     exit;
           }#end if error in call to printIndividualCheckList
         echo "<br><br>\n";

         echo "<input type=\"hidden\" name=\"want_list\" value=\"$want_list\">\n";
         echo "<input type=\"hidden\" name=\"pheno_multiple_pages\" value=\"$pheno_multiple_pages\">\n";
         echo "<input type=\"hidden\" name=\"pheno_id\" value=\"$pheno_id_term\">\n";
         echo "<input type=\"hidden\" name=\"indiv_multiple_pages\" value=\"$indiv_multiple_pages\">\n";

         if ($indiv_multiple_pages)
           {
             echo "<input type=\"hidden\" name=\"function_to_call\" value=\"$final_function_call\">\n";
	     if ($final_function_call == 'tableJoin') 
               {
                 foreach ($final_table_name as $name) {echo "<input type=\"hidden\" name=\"table_name[]\" value=\"$name\">\n";}
               }
             else{echo "<input type=\"hidden\" name=\"table_name\" value=\"$final_table_name\">\n";}

	     foreach ($final_field_list as $field){echo "<input type=\"hidden\" name=\"field_list[]\" value=\"$field\">\n";}

             if (is_array($final_term)){foreach ($final_term as $term){echo "<input type=\"hidden\" name=\"term[]\" value=\"$term\">\n";}}
             else {echo "<input type=\"hidden\" name=\"term\" value=\"$final_term\">\n";}

             if (is_array($final_mode)){foreach ($final_mode as $mode){echo "<input type=\"hidden\" name=\"mode[]\" value=\"$mode\">\n";}}
             else {echo "<input type=\"hidden\" name=\"mode\" value=\"$final_mode\">\n";}

             if ($final_function_call == 'compoundTableQuery') {echo "<input type=\"hidden\" name=\"join\" value=\"$final_join\">\n";}

             echo "<input type=\"hidden\" name=\"order_by\" value=\"$final_order_by\">\n";
             echo "<input type=\"hidden\" name=\"last_record_retrieved\" value=\"$last_record_retrieved\">\n";

             echo "<table align=\"center\">\n";
             echo "<tr>\n";
             echo "<td align=\"center\">&nbsp;</td>\n";
             echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selected Individuals\"></td>\n";
             echo "</tr>\n";
             echo "</table>\n";
             echo "<br>\n";
             echo "<table align=\"center\">\n";
             echo "<tr>\n";
             if ($records_left > $max_records_to_print_per_page)
               {echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Get Next $max_records_to_print_per_page Individuals\"></td>\n";}
             else
               {echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Get Next $records_left Individuals\"></td>\n";}
             echo "</form>\n\n";

             echo "<form action=\"delete_phenotype_values.php\" name=\"skip_form2\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
             echo "<input type=\"hidden\" name=\"want_list\" value=\"$want_list\">\n";
             echo "<input type=\"hidden\" name=\"pheno_multiple_pages\" value=\"$pheno_multiple_pages\">\n";
             echo "<input type=\"hidden\" name=\"pheno_id\" value=\"$pheno_id_term\">\n";
             echo "<input type=\"hidden\" name=\"indiv_multiple_pages\" value=\"0\">\n";
             echo "<input type=\"hidden\" name=\"indiv_id\" value=\"\">\n";
             echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Delete Individuals' Phenotype Values\" onClick=\"copyData('skip_form2')\"></td>\n";
             echo "</form>\n";
             echo "</tr>\n";
             echo "</table>\n";
           }#end if multiple_pages
         else #only a single page
           {
             echo "<table align=\"center\">\n";
             echo "<tr>\n";
             echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Delete Individuals' Phenotype Values\" onClick=\"copyData('skip_form1')\"></td>\n";
             echo "<td align=\"center\"><input type=\"reset\" name=\"reset\" value=\"Reset Selected Individuals\"></td>\n";
             echo "</tr>\n";
             echo "</table>\n";
             echo "</form>\n";
           }#end else only a single page
         echo "<br><br>\n";

         $ret_val = printLogOutButton('delete_phenotype_values.php');
         echo "\n\n";

         echo "<script>\n";
	 echo "function Validate(form)\n";
	 echo "$jscript\n";
         if ($indiv_multiple_pages)
           {
             echo "\n";
             echo "function copyData(formname)\n";
             echo "{\n";
             echo "  var indiv_string = new Array();\n";
             echo "  var length = 0;\n";
             echo "  if (document.form.elements['indiv_id[]'].length > 1){\n";
             echo "    for (var i=0; i<document.form.elements['indiv_id[]'].length; i++){\n";
             echo "      if (document.form.elements['indiv_id[]'][i].checked){\n";
             echo "        length = indiv_string.length;\n";
             echo "        indiv_string[length] = document.form.elements['indiv_id[]'][i].value;}}}\n";
             echo "  else{\n";
             echo "    if (document.form.elements['indiv_id[]'].checked){\n";
             echo "      length = indiv_string.length;\n";
             echo "      indiv_string[length] = document.form.elements['indiv_id[]'].value;}}\n";
             echo "  if (formname == 'skip_form1'){\n";
             echo "    document.skip_form1.indiv_id.value = indiv_string;}\n";
             echo "  if (formname == 'skip_form2'){\n";
             echo "    document.skip_form2.indiv_id.value = indiv_string;}\n";
             echo "}\n";
           }#end if multiple pages
	 echo "</script>\n";
         unset($indiv_multiple_pages);
       }#end if have phenotype id but not individuals

    #10
    if (($want_list == '1') and
        ($pheno_multiple_pages == '0') and
	(sizeof($pheno_ids) > '0') and
        ($indiv_multiple_pages == '1')
       )
      {
        if ($function_to_call == 'tableQuery')
          {
            $num_indivs = countTableQuery($table_name,$database_id,$field_list,$term,$mode);
	    if (!preg_match('/^\d+$/',$num_indivs))
	      {
		$date = date('Y-m-d H:i:s');
		echo "Error #42: $date<br>\n";
		echo "Error in call to countTableQuery from delete_phenotype_values.php<br>\n";
		echo "$num_indivs<br><br>\n";
		echo "Please email the above error using The Comments/Help/Support link on the \n";
		echo "left navigation bar.\n";
		echo "</form>\n";
		echo "</body></html>\n";
		$ret_val = disconnect();
		exit;
	      }#end error in call to countTableQuery
	    $records_yet_to_be_retrieved = $num_indivs - $last_record_retrieved;

	    if ($records_yet_to_be_retrieved > $max_records_to_print_per_page)
	      {
                $indiv_multiple_pages = 1;
		$from = $last_record_retrieved;
		$to   = ($last_record_retrieved-1) + $max_records_to_print_per_page;
		$range = "$from"."-"."$to";
		$indiv_rec_list = tableQuery($table_name,$database_id,$field_list,$term,$mode,$range,$order_by);
		if (!is_array($indiv_rec_list))
		  {
		    $date = date('Y-m-d H:i:s');
		    echo "Error #43: $date<br>\n";
		    echo "Error in call to tableQuery from delete_phenotype_values.php<br>\n";
		    echo "$indiv_rec_list<br><br>\n";
		    echo "Please email the above error using The Comments/Help/Support link on the \n";
		    echo "left navigation bar.\n";
		    echo "</form>\n";
		    echo "</body></html>\n";
		    $ret_val = disconnect();
		    exit;
		  }#end if error in call to tableQuery
		if (sizeof($indiv_rec_list) == 0)
		  {
		    $date = date('Y-m-d H:i:s');
		    echo "Error #44: $date<br>\n";
		    echo "Error in call to delete_phenotype_values.php<br>\n";
		    echo "There should be records returned for tableQuery search with parameters:<br>\n";
		    echo "table_name = $table_name<br>\n";
		    echo "field_list = ".join(',',$field_list)."<br>\n";
		    echo "term       = $term<br>\n";
		    echo "mode       = $mode<br>\n";
		    echo "range      = $range<br>\n";
		    echo "order_by   = $order_by<br>\n";
		    echo "But no records were returned<br><br>\n";
		    echo "Please email the above error using The Comments/Help/Support link on the \n";
		    echo "left navigation bar.\n";
		    echo "</form>\n";
		    echo "</body></html>\n";
		    $ret_val = disconnect();
		    exit;
		  }#end if no records returned
                $last_record_retrieved = $to + 1;
	      }#end if multiple pages
	    else #num records fit on one page
	      {
                $indiv_multiple_pages = 0;
		$from = $last_record_retrieved;
		$to   = $num_indivs - 1;
		$range = "$from"."-"."$to";
		$indiv_rec_list = tableQuery($table_name,$database_id,$field_list,$term,$mode,$range,$order_by);
		if (!is_array($indiv_rec_list))
		  {
		    $date = date('Y-m-d H:i:s');
		    echo "Error #45: $date<br>\n";
		    echo "Error in call to tableQuery from delete_phenotype_values.php<br>\n";
		    echo "$indiv_rec_list<br><br>\n";
		    echo "Please email the above error using The Comments/Help/Support link on the \n";
		    echo "left navigation bar.\n";
		    echo "</form>\n";
		    echo "</body></html>\n";
		    $ret_val = disconnect();
		    exit;
		  }#end if error in call to tableQuery
		if (sizeof($indiv_rec_list) == 0)
		  {
		    $date = date('Y-m-d H:i:s');
		    echo "Error #46: $date<br>\n";
		    echo "Error in call to delete_phenotype_values.php<br>\n";
		    echo "There should be records returned for tableQuery search with parameters:<br>\n";
		    echo "table_name = $table_name<br>\n";
		    echo "field_list = ".join(',',$field_list)."<br>\n";
		    echo "term       = $term<br>\n";
		    echo "mode       = $mode<br>\n";
		    echo "range      = $range<br>\n";
		    echo "order_by   = $order_by<br>\n";
		    echo "But no records were returned<br><br>\n";
		    echo "Please email the above error using The Comments/Help/Support link on the \n";
		    echo "left navigation bar.\n";
		    echo "</form>\n";
		    echo "</body></html>\n";
		    $ret_val = disconnect();
		    exit;
		  }#end if no records returned
	      }#end else less than max per page left
	  }#end if tableQuery

        echo "Select which Individuals for which you want to delete Phenotype Values for the selected Phenotypes<br><br>\n";

        $ret_val = printLogOutButton('delete_phenotype_values.php');
        echo "<br><br>\n";

        if ($indiv_multiple_pages)
          {
            echo "<table align=\"center\">\n";
            echo "<form action=\"delete_phenotype_values.php\" name=\"skip_form1\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
            echo "<input type=\"hidden\" name=\"want_list\" value=\"$want_list\">\n";
            echo "<input type=\"hidden\" name=\"pheno_multiple_pages\" value=\"$pheno_multiple_pages\">\n";
            echo "<input type=\"hidden\" name=\"pheno_id\" value=\"$pheno_ids\">\n";
            echo "<input type=\"hidden\" name=\"indiv_multiple_pages\" value=\"0\">\n";
            echo "<input type=\"hidden\" name=\"indiv_id\" value=\"\">\n";
            echo "<tr>\n";
            echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Delete Individuals' Phenotype Values\" onClick=\"copyData('skip_form1')\"></td>\n";
            echo "</form>\n\n";

            echo "<form action=\"delete_phenotype_values.php\" name=\"form\" method=\"POST\" target=\"main\">\n";
            $records_left = $num_indivs - $last_record_retrieved;
            if ($records_left > $max_records_to_print_per_page)
              {echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Get Next $max_records_to_print_per_page Individuals\"></td>\n";}
            else
              {echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Get Next $records_left Individuals\"></td>\n";}
            echo "</tr>\n";
            echo "</table>\n";
            echo "<br>\n";
            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            echo "<td align=\"center\">&nbsp;</td>\n";
            echo "<td align=\"center\"><input type=\"reset\" name=\"reset\" value=\"Reset Selected Individuals\"></td>\n";
            echo "</tr>\n";
            echo "</table>\n";
          }#end if multiple pages
        else #only a single page
          {
            echo "<table align=\"center\">\n";
            echo "<form action=\"delete_phenotype_values.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
            echo "<tr>\n";
            echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Delete Individuals' Phenotype Values\" onClick=\"copyData('skip_form1')\"></td>\n";
            echo "<td align=\"center\"><input type=\"reset\" name=\"reset\" value=\"Reset Selected Individuals\"></td>\n";
            echo "</tr>\n";
            echo "</table>\n";
          }#end else only a single page
        echo "<br><br>\n";

        $jscript = printIndividualCheckList($indiv_rec_list,$database_id);
        if ($jscript == '0')
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #47: $date<br>\n";
            echo "Error in call to printIndividualCheckList from delete_phenotype_values.php<br>\n";
            echo "See error message from printIndividualCheckList above for an explanation.<br><br>\n";
            echo "Please email the above error using the Comments/Help/Support link on the \n";
	    echo "left navigation bar.\n";
            echo "</form>\n";
	    echo "</body></html>\n";
	    $ret_val = disconnect();
	    exit;
          }#end if error in call to printIndividualCheckList
        echo "<br><br>\n";

        echo "<input type=\"hidden\" name=\"want_list\" value=\"$want_list\">\n";
        echo "<input type=\"hidden\" name=\"pheno_multiple_pages\" value=\"$pheno_multiple_pages\">\n";
        echo "<input type=\"hidden\" name=\"pheno_id\" value=\"$pheno_ids\">\n";
        echo "<input type=\"hidden\" name=\"indiv_multiple_pages\" value=\"$indiv_multiple_pages\">\n";
        if (sizeof($indiv_ids) > 0)
          {
            if (!is_array($indiv_ids)){echo "<input type=\"hidden\" name=\"indiv_id[]\" value=\"$indiv_ids\">\n";}
            else
              {
                $id_term = join($indiv_ids,',');
                echo "<input type=\"hidden\" name=\"indiv_id[]\" value=\"$id_term\">\n";
                unset($id_term);
              }
          }#end if have ids from previous page(s)

        if ($indiv_multiple_pages)
          {
            echo "<input type=\"hidden\" name=\"function_to_call\" value=\"$function_to_call\">\n";
	    if ($function_to_call == 'tableJoin') 
              {
                foreach ($table_name as $name) {echo "<input type=\"hidden\" name=\"table_name[]\" value=\"$name\">\n";}
              }
            else{echo "<input type=\"hidden\" name=\"table_name\" value=\"$table_name\">\n";}

	    foreach ($field_list as $field){echo "<input type=\"hidden\" name=\"field_list[]\" value=\"$field\">\n";}

            if (is_array($term)){foreach ($term as $sub_term){echo "<input type=\"hidden\" name=\"term[]\" value=\"$sub_term\">\n";}}
            else {echo "<input type=\"hidden\" name=\"term\" value=\"$term\">\n";}

            if (is_array($mode)){foreach ($mode as $sub_mode){echo "<input type=\"hidden\" name=\"mode[]\" value=\"$sub_mode\">\n";}}
            else {echo "<input type=\"hidden\" name=\"mode\" value=\"$mode\">\n";}

            if ($function_to_call == 'compoundTableQuery') {echo "<input type=\"hidden\" name=\"join\" value=\"$join\">\n";}

            echo "<input type=\"hidden\" name=\"order_by\" value=\"$order_by\">\n";
            echo "<input type=\"hidden\" name=\"last_record_retrieved\" value=\"$last_record_retrieved\">\n";

            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            echo "<td align=\"center\">&nbsp;</td>\n";
            echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selected Individuals\"></td>\n";
            echo "</tr>\n";
            echo "</table>\n";
            echo "<br>\n";
            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            if ($records_left > $max_records_to_print_per_page)
              {echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Get Next $max_records_to_print_per_page Individuals\"></td>\n";}
            else
              {echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Get Next $records_left Individuals\"></td>\n";}
            echo "</form>\n\n";

            echo "<form action=\"delete_phenotype_values.php\" name=\"skip_form2\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
            echo "<input type=\"hidden\" name=\"want_list\" value=\"$want_list\">\n";
            echo "<input type=\"hidden\" name=\"pheno_multiple_pages\" value=\"$pheno_multiple_pages\">\n";
            echo "<input type=\"hidden\" name=\"pheno_id\" value=\"$pheno_ids\">\n";
            echo "<input type=\"hidden\" name=\"indiv_multiple_pages\" value=\"0\">\n";
            echo "<input type=\"hidden\" name=\"indiv_id\" value=\"\">\n";
            echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Delete Individuals' Phenotype Values\" onClick=\"copyData('skip_form2')\"></td>\n";
            echo "</form>\n";
            echo "</tr>\n";
            echo "</table>\n";
          }#end if multiple_pages
        else #only a single page
          {
            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            echo "<td align=\"center\"><input type=\"submit\" name=\"submit\" value=\"Delete Individuals' Phenotype Values\" onClick=\"copyData('skip_form1')\"></td>\n";
            echo "<td align=\"center\"><input type=\"reset\" name=\"reset\" value=\"Reset Selected Individuals\"></td>\n";
            echo "</tr>\n";
            echo "</table>\n";
            echo "</form>\n";
          }#end else only a single page
        echo "<br><br>\n";

        $ret_val = printLogOutButton('delete_phenotype_values.php');
        echo "\n\n";

        echo "<script>\n";
	echo "function Validate(form)\n";
	echo "$jscript\n";
        if ($indiv_multiple_pages)
          {
            echo "\n";
            echo "function copyData(formname)\n";
            echo "{\n";
            echo "  var indiv_string = new Array();\n";
            echo "  var length = 0;\n";
            echo "  if (document.form.elements['indiv_id[]'].length > 1){\n";
            echo "    for (var i=0; i<document.form.elements['indiv_id[]'].length; i++){\n";
            echo "      if (document.form.elements['indiv_id[]'][i].type == \"hidden\"){\n";
            echo "        length = indiv_string.length;\n";
            echo "        indiv_string[length] = document.form.elements['indiv_id[]'][i].value;}\n";
            echo "      else{\n";
            echo "        if (document.form.elements['indiv_id[]'][i].checked){\n";
            echo "          length = indiv_string.length;\n";
            echo "          indiv_string[length] = document.form.elements['indiv_id[]'][i].value;}}}}\n";
            echo "  else{\n";
            echo "    if (document.form.elements['indiv_id[]'].type == \"hidden\"){\n";
            echo "      length = indiv_string.lenght;\n";
            echo "      indiv_string[length] = document.form.elements['indiv_id[]'].value;}\n";
            echo "    else{\n";
            echo "      if (document.form.elements['indiv_id[]'].checked){\n";
            echo "        length = indiv_string.length;\n";
            echo "        indiv_string[length] = document.form.elements['indiv_id[]'].value;}}}\n";
            echo "  if (formname == 'skip_form1'){\n";
            echo "    document.skip_form1.indiv_id.value = indiv_string;}\n";
            echo "  if (formname == 'skip_form2'){\n";
            echo "    document.skip_form2.indiv_id.value = indiv_string;}\n";
            echo "}\n";
          }#end if multiple pages
	echo "</script>\n";
        unset($indiv_multiple_pages);
      }#end if have phenotype id and have seen indivs, but more to see 

    #11     Get List data, and delete
    if (($want_list == '1') and
        ($pheno_multiple_pages == '0') and
        (sizeof($pheno_ids) > '0') and
        ($indiv_multiple_pages == '0') and
	(sizeof($indiv_ids) > '0')
       )
      {
        $rand_num = rand();
        $file_name = "delete_pheno_value_data_upload_".$rand_num.".txt";
        $file_handle = fopen("../data/uploaded_data/$_SESSION[user_name]/$file_name","w");
        if (!$file_handle)
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #48: $date<br>\n";
            echo "Error in call to fopen from delete_phenotype_values.php<br>\n";
            echo "Unable to open file to write ids to delete.<br><br>\n";
            echo "Please email the above error using Comments/Help/Support link \n";
	    echo "in the left navigation bar.<br>\n";
	    echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end error in call to fopen

        if (!is_array($pheno_ids))
          {
            if (preg_match('/,/',$pheno_ids))
              {
                $pheno_delete_array = explode(',',$pheno_ids);
                if (!is_array($indiv_ids))
                  {
                    if (preg_match('/,/',$indiv_ids))
                      {
                        $indiv_delete_array = explode(',',$indiv_ids);
                        foreach ($pheno_delete_array as $delete_pheno)
                          {
                            foreach ($indiv_delete_array as $delete_indiv){fwrite($file_handle,"$delete_pheno,$delete_indiv\n");}
                          }#end foreach phenotype to delete value for
                        unset($indiv_delete_array);
                      }#end if indiv_ids is comma-separated list
                    else #only a single indiv_id
                      {
                        foreach ($pheno_delete_array as $delete_pheno){fwrite($file_handle,"$delete_pheno,$indiv_id\n");}
                      }#end else only a single indiv_id
                  }#end if indiv_ids is not an array
                else #indiv_ids is an array
                  {
                    $delete_indiv_id_term = join($indiv_ids,',');
                    $indiv_delete_array   = explode(',',$delete_indiv_id_term);
                    foreach ($pheno_delete_array as $delete_pheno)
                      {
                        foreach ($indiv_delete_array as $delete_indiv){fwrite($file_handle,"$delete_pheno,$delete_indiv\n");}
                      }#end foreach phenotype to delete value for
                    unset($indiv_delete_array);
                  }#end else indiv_ids is an array
                unset($pheno_delete_array);
              }#end if comma-separated list
            else #just a single phenotype id
              {
                if (!is_array($indiv_ids))
                  {
                    if (preg_match('/,/',$indiv_ids))
                      {
                        $indiv_delete_array = explode(',',$indiv_ids);
                        foreach ($indiv_delete_array as $delete_indiv){fwrite($file_handle,"$pheno_ids,$delete_indiv\n");}
                        unset($indiv_delete_array);
                      }#end if indiv_ids is comma-separated list
                    else #only a single indiv_id
                      { fwrite($file_handle,"$pheno_ids,$indiv_ids\n");}
                  }#end if indiv_ids is not an array
                else #indiv_ids is an array
                  {
                    $delete_indiv_id_term = join($indiv_ids,',');
                    $indiv_delete_array   = explode(',',$delete_indiv_id_term);
                    foreach ($indiv_delete_array as $delete_indiv){fwrite($file_handle,"$pheno_ids,$delete_indiv\n");}
                    unset($indiv_delete_array);
                  }#end else indiv_ids is an array
              }#end else just a single phenotype id
          }#end if isn't an array
        else #else is an array
          {
            $delete_pheno_id_term = join($pheno_ids,',');
            $pheno_delete_array   = explode(',',$delete_pheno_id_term);
            if (!is_array($indiv_ids))
              {
                if (preg_match('/,/',$indiv_ids))
                  {
                    $indiv_delete_array = explode(',',$indiv_ids);
                    foreach ($pheno_delete_array as $delete_pheno)
                      {
                        foreach ($indiv_delete_array as $delete_indiv){fwrite($file_handle,"$delete_pheno,$delete_indiv\n");}
                      }#end foreach phenotype to delete value for
                    unset($indiv_delete_array);
                  }#end if indiv_ids is comma-separated list
                else #only a single indiv_id
                  {
                     foreach ($pheno_delete_array as $delete_pheno){fwrite($file_handle,"$delete_pheno,$indiv_id\n");}
                  }#end else only a single indiv_id
              }#end if indiv_ids is not an array
            else #indiv_ids is an array
              {
                $delete_indiv_id_term = join($indiv_ids,',');
                $indiv_delete_array   = explode(',',$delete_indiv_id_term);
                foreach ($pheno_delete_array as $delete_pheno)
                  {
                    foreach ($indiv_delete_array as $delete_indiv){fwrite($file_handle,"$delete_pheno,$delete_indiv\n");}
                  }#end foreach phenotype to delete value for
                unset($indiv_delete_array);
              }#end else indiv_ids is an array
            unset($pheno_delete_array);
            fwrite($file_handle,"$delete_ped_table_id_term\n");
          }#end else is an array
        fclose($file_handle);

        $file_size = @filesize("../data/uploaded_data/$_SESSION[user_name]/$file_name");
        if (!$file_size)
          {
            $command = "stat -c %s ../data/uploaded_data/$_SESSION[user_name]/$file_name";
            exec($command,$output_array);
            $file_size = $output_array[0];
          }#end if file too big for php
        $file_name = "../../data/uploaded_data/$_SESSION[user_name]/$file_name";
        $user_file = 0;

        echo "Deleting the selected Phenotype Values from the database.<br>\n";

        $ret_val = chdir("../bin/database_query_scripts/");
        if (!$ret_val)
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #49: $date<br>\n";
            echo "Error in call to chdir from delete_phenotype_values.php<br>\n";
            echo "Unable to change directory to database query scripts directory<br><br>\n";
            echo "Please email the above error using Comments/Help/Support link \n";
	    echo "in the left navigation bar.<br>\n";
	    echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end error in call to chdir

	if ($file_size > $max_file_size_to_wait_to_run)
          {
            $background_flag = 1;
            if ($file_size >= $max_file_size_not_to_split)
              {
                $found = preg_match('/^(.+\/)([^\/]+)$/',$file_name,$captured);
                $path          = $captured[1];
                $name          = $captured[2];
                $sub_file_name = "sub_"."$name";
                $prefix        = "$path"."$sub_file_name";
                $prefix        = preg_replace('/\.\w+$/','_',$prefix);

                $command = "split -a5 --lines=$num_lines_to_split_files $file_name $prefix";
                if (sizeof($output_array) > '0'){$output_array = array();}
                exec($command,$output_array,$return_code);
                if ($return_code)
                  {
                    $err_msg = implode("\n",$output_array);
                    $date = date('Y-m-d H:i:s');
                    echo "Error #50: $date<br>\n";
                    echo "Error in call to exec to split file from delete_phenotype_values.php<br>\n";
                    echo "$err_msg<br><br>\n";
                    echo "Please email the above error using the Comments/Help/Support link on the \n";
	            echo "left navigation bar.\n";
	            echo "</form>\n";
	            echo "</body></html>\n";
	            $ret_val = disconnect();
	            exit;
	          }#end if error in exec call to split file
                $file_array = glob("$prefix*");
                $num_files_split = sizeof($file_array);
                sort($file_array);
                foreach ($file_array as $key=>$file)
                  {
                    $command = "./delete_data_from_database.php '$user_file' '$file' '$database_id' 'Delete Phenotype Values' ";
                    $command.= "'$_SESSION[user_name]' '$background_flag' >> dummy_delete_data_background_output.txt &";

                    $last_line = system($command,$sys_ret_val);
                    if ($sys_ret_val != '0')
	              {
	                $date = date('Y-m-d H:i:s');
	                echo "Error #51: $date<br>\n";
	                echo "Error in system call to delete_data_from_database.php from delete_phenotype_values.php<br>\n";
	                echo "The return value of the sytem call was $sys_ret_val<br><br>\n";
                        echo "Please email the above error using the Comments/Help/Support link on the \n";
	                echo "left navigation bar.\n";
	                echo "</form>\n";
	                echo "</body></html>\n";
	                $ret_val = disconnect();
	                exit;
	              }#end if error in system call
                  }#end foreach sub file
              }#end if file too big for php
            else #don't need to split file
              {
                $command = "./delete_data_from_database.php '$user_file' '$file_name' '$database_id' 'Delete Phenotype Values' ";
                $command.= "'$_SESSION[user_name]' '$background_flag' >> dummy_delete_data_background_output.txt &";

                $last_line = system($command,$sys_ret_val);
                if ($sys_ret_val != '0')
	          {
	            $date = date('Y-m-d H:i:s');
	            echo "Error #52: $date<br>\n";
	            echo "Error in system call to delete_data_from_database.php from delete_phenotype_values.php<br>\n";
	            echo "The return value of the sytem call was $sys_ret_val<br><br>\n";
                    echo "Please email the above error using the Comments/Help/Support link on the \n";
	            echo "left navigation bar.\n";
	            echo "</form>\n";
	            echo "</body></html>\n";
	            $ret_val = disconnect();
	            exit;
	          }#end if error in system call
              }#end else don't need to split file
          }#end if run in background
        else #run in foreground
          {
            $background_flag = 0;
            $command = "./delete_data_from_database.php '$user_file' '$file_name' '$database_id' 'Delete Phenotype Values' '$_SESSION[user_name]' '$background_flag' ";

            passthru($command,$sys_ret_val);
            if ($sys_ret_val != '0')
	      {
                echo "<br><br>\n";
	        $date = date('Y-m-d H:i:s');
	        echo "Error #53: $date<br>\n";
	        echo "Error in system call to delete_data_from_database.php from delete_phenotype_values.php<br>\n";
	        echo "See the error message above for an explanation of the error<br><br>\n";
                echo "Please email the above error using the Comments/Help/Support link on the \n";
	        echo "left navigation bar.\n";
	        echo "</form>\n";
	        echo "</body></html>\n";
	        $ret_val = disconnect();
	        exit;
	      }#end if error in system call
          }#end else run in foreground

        $ret_val = chdir("../../cgi-bin/");
        if (!$ret_val)
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #54: $date<br>\n";
            echo "Error in call to chdir from delete_phenotype_values.php<br>\n";
            echo "Unable to change directory to cgi-bin directory<br><br>\n";
            echo "Please email the above error using Comments/Help/Support link \n";
	    echo "in the left navigation bar.<br>\n";
	    echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end error in call to chdir

        if ($background_flag)
          {
            echo "<center>\n";
	    echo "<b>The data you uploaded is being inserted into the database.<br>\n";
            if ($file_size >= $max_file_size_not_to_split)
              {
                echo "The file was too large to handle, so it was split into $num_files_split sub files, and each \n";
                echo "sub file is being processed separately.  So you will recieve an email when the data in each sub file \n";
                echo "has been deleted from the database \n";
              }#end if split file
	    else {echo "You will receive an email when all the data has been deleted from the database \n";}
	    echo "detailing the results of the file upload and any errors.</b><br><br>\n";
	    echo "</center>\n";
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end if running in background

	if (!$background_flag){echo "Finished sending update emails to all the users<br>\n";}
      }#end if have phenotype value ids

    #12 If want to upload a file, but haven't selected whether on server or to upload
    if (($want_list == '2') and
        (!preg_match('/^\d$/',$upload_file))
       )
      {
        echo "Please select whether you would like to upload a file from your computer, or \n";
        echo "select a file that is already in your 'Uploaded Data' directory on the server.<br><br>\n";

        echo "<form action=\"delete_phenotype_values.php\" name=\"form\" method=\"POST\" target=\"main\">\n";
        echo "<input type=\"hidden\" name=\"want_list\" value=\"$want_list\">\n";
        echo "<table align=\"center\">\n";
        echo "<tr><td><input type=\"radio\" name=\"upload_file\" value=\"0\" checked></td><td>Select File in 'Uploaded Data' directory</td></tr>\n";
        echo "<tr><td><input type=\"radio\" name=\"upload_file\" value=\"1\"></td><td>Upload File from Your Computer</td></tr>\n";
        echo "</table>\n";
        echo "<br>\n";

        echo "<table align=\"center\">\n";
        echo "<tr>\n";
        echo "<td><input type=\"submit\" name=\"submit\" value=\"Select File to Delete From\"></td>\n";
        echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Method to Select File\"></td>\n";
        echo "</tr>\n";
        echo "</table>\n";
        echo "<br><br>\n";

        printLogOutButton('delete_phenotype_values.php');
      }#end if want to upload file but haven't select way to select

    #13 If want to delete from file, but haven't selected it yet
    if ((($want_list == '2') and
         ($upload_file == '0') and
         (!preg_match('/\w+/',$file_on_server))) or
        (($want_list == '2') and
         ($upload_file == '1') and
         (!preg_match('/\w+/',$_FILES['pheno_val_file']['tmp_name'])))
       )
      {
        echo "Please select the file that contains the information to Delete from the Phenotype Value Table.<br>\n";
        echo "The file must contain three columns of data separated by a comma for the deletion to work correctly.  The file format should be:<br>\n";
        echo "<table>\n";
        echo "<tr><td>&nbsp;</td><td><b>Column 1:</b></td><td>Phenotype Name</td></tr>\n";
        echo "<tr><td>&nbsp;</td><td><b>Column 2:</b></td><td>Pedigree ID</td></tr>\n";
        echo "<tr><td>&nbsp;</td><td><b>Column 3:</b></td><td>Individual ID</td></tr>\n";
        echo "</table>\n";
        echo "<br>\n";
        echo "Each line describes a single phenotype value for a specific individual to be deleted from the database.  \n";
        echo "Each phenotype value needs to be on its own line.<br>\n";
        echo "(Click <a href=\"../data/test_data/phenotype_value_delete_test_data.txt\" target=\"_blank\">here</a> for an example file.)<br>\n";
        echo "<br><br>\n\n";

        if ($upload_file == '0') #if want to select file from server
          {
            echo "<form action=\"delete_phenotype_values.php\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
            echo "<table align=\"center\">\n";
            echo "<tr>\n";
            echo "<td>File Name:</td><td>\n";
            $jscript = printFileSelectList($_SESSION[user_name],$database_id,'upload');
            if ($jscript == '0')
              {
                $date = date('Y-m-d H:i:s');
                echo "Error #55: $date<br>\n";
                echo "Error in call to printFileSelectList from delete_phenotype_values.php.<br>\n";
                echo "Please see the error from printFileSelectList above for an explanation.<br><br>\n";
                echo "Please email the above error message using the Comments/Help/Suggestions link \n";
                echo "on the left navigation bar.\n";
                echo "</form>\n";
                echo "</body></html>\n";
                $ret_val = disconnect();
                exit;
              }#end error in call to printFileSelectList
            if ($jscript == '1')
              {
                $date = date('Y-m-d H:i:s');
                echo "</td>\n";
                echo "</tr>\n";
                echo "</table>\n";
                echo "</form>\n";
                echo "<br><br>\n";

                echo "<center>\n";
                echo "<b>There are no files stored for user $_SESSION[user_name] in their uploaded_data directory.</b><br>\n";
                echo "This means either you have not transfered the file to the server, or the file has expired and has been \n";
                echo "deleted from the server.<br> So you need to either transfer the file you are looking for to the server and put it \n";
                echo "in your uploaded_data directory, or upload the file directly from your computer in order to insert the data.<br><br>\n";
                echo "</center>\n\n";

                $ret_val = printLogOutButton('delete_phenotype_values.php');
                echo "</body></html>\n";
                $ret_val = disconnect();
                exit;
              }#end if no files in users directory to upload
            echo "</td>\n";
            echo "</tr>\n";
            echo "</table>\n";
          }#end if want to select file from server
        if ($upload_file == '1') #if want to upload file from users computer
          {
            echo "<form action=\"delete_phenotype_values.php\" enctype=\"multipart/form-data\" name=\"form\" method=\"POST\" target=\"main\" onSubmit=\"return(Validate(form))\">\n";
            echo "<table align=\"center\">\n";
	    echo "<tr><td>File Name:</td><td><input type=\"file\" name=\"pheno_val_file\" size=\"20\"></td></tr>\n";
            echo "</table>\n";
          }
        echo "<br><br>\n";

        echo "<input type=\"hidden\" name=\"want_list\" value=\"$want_list\">\n";
        echo "<input type=\"hidden\" name=\"upload_file\" value=\"$upload_file\">\n";
        echo "<table align=\"center\">\n";
        echo "<tr>\n";
        echo "<td><input type=\"submit\" name=\"submit\" value=\"Delete Data in File from Database\"></td>\n";
        echo "<td><input type=\"reset\" name=\"reset\" value=\"Reset Selected File\"></td>\n";
        echo "</tr>\n";
        echo "</table>\n";
        echo "</form>\n\n";

        printLogOutButton('delete_phenotype_values.php');
        echo "\n\n";

        echo "<script>\n";
        echo "function Validate(form)\n";
        if ($upload_file == '0'){echo "$jscript\n";}
        if ($upload_file == '1')
          {
            echo "{\n";
            echo " if (form.pheno_val_file.value == \"\"){\n";
            echo "   alert(\"You Must Upload a File\");\n";
            echo "   form.pheno_val_file.focus();\n";
            echo "   return false;}\n";
            echo " else{return true;}\n";
            echo "}\n";
          }
        echo "</script>\n";
      }#end if want to delete from file, but haven't selected it yet

    #14 Have file, now delete data
    if ((($want_list == '2') and 
         ($upload_file == '0') and 
         (preg_match('/\w+/',$file_on_server))) or
        (($want_list == '2') and
         ($upload_file == '1') and
         (preg_match('/\w+/',$_FILES['pheno_val_file']['tmp_name'])))
       )
      {
        if ($upload_file == '0')
          {
            $uploaded_file_name = "../"."$file_on_server";
            $uploaded_file_size = @filesize("$file_on_server");
            if (!$uploaded_file_size)
              {
                $command = "stat -c %s $file_on_server";
                exec($command,$output_array);
                $uploaded_file_size = $output_array[0];
              }#end if file too big for php
          }#end else file already on server
        if ($upload_file == '1')
          {
            $rand_num = rand();
            $file_name = "delete_pheno_val_data_upload_".$rand_num.".txt";
            $ret_val = move_uploaded_file($_FILES['pheno_val_file']['tmp_name'],"../data/uploaded_data/$_SESSION[user_name]/$file_name");
	    if (!$ret_val)
              {
                $date = date('Y-m-d H:i:s');
	        echo "Error #56: $date<br>\n";
	        echo "Error in call to delete_phenotype_values.php<br>\n";
	        echo "Unable to move uploaded file in order to read it.<br><br>\n";
	        echo "Please email the above error message using the Comments/Help/Support link on \n";
	        echo "the left navigation bar.\n";
	        echo "</body></html>\n";
                fwrite($log_file_handle,$log_file_statement);
                fclose($log_file_handle);
                chmod("../data/$tmp_log_file",0777);
	        $ret_val = disconnect();
	        exit;
	      }#end if (!ret_val)
            $uploaded_file_name = "../../data/uploaded_data/$_SESSION[user_name]/$file_name";
            $uploaded_file_size = @filesize("../data/uploaded_data/$_SESSION[user_name]/$file_name");
            if (!$uploaded_file_size)
              {
                $command = "stat -c %s ../data/uploaded_data/$_SESSION[user_name]/$file_name";
                exec($command,$output_array);
                $uploaded_file_size = $output_array[0];
              }#end if file too big for php
          }#end if file uploaded from user's computer
        $user_file = 1;

        echo "Now Updating the Information in the Database and Deleting the Phenotype Values in the file.<br><br>\n";
        
        $ret_val = chdir("../bin/database_query_scripts/");
        if (!$ret_val)
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #57: $date<br>\n";
            echo "Error in call to chdir from delete_phenotype_values.php<br>\n";
            echo "Unable to change directory to database query scripts directory<br><br>\n";
            echo "Please email the above error using Comments/Help/Support link \n";
	    echo "in the left navigation bar.<br>\n";
	    echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end error in call to chdir

	if ($uploaded_file_size > $max_file_size_to_wait_to_run)
          {
            $background_flag = 1;
            if ($uploaded_file_size >= $max_file_size_not_to_split)
              {
                $found = preg_match('/^(.+\/)([^\/]+)$/',$uploaded_file_name,$captured);
                $path          = $captured[1];
                $file_name     = $captured[2];
                $sub_file_name = "sub_"."$file_name";
                $prefix        = "$path"."$sub_file_name";
                $prefix        = preg_replace('/\.\w+$/','_',$prefix);

                $command = "split -a5 --lines=$num_lines_to_split_files $uploaded_file_name $prefix";
                if (sizeof($output_array) > '0'){$output_array = array();}
                exec($command,$output_array,$return_code);
                if ($return_code)
                  {
                    $err_msg = implode("\n",$output_array);
                    $date = date('Y-m-d H:i:s');
                    echo "Error #58: $date<br>\n";
                    echo "Error in call to exec to split file from delete_phenotype_values.php<br>\n";
                    echo "$err_msg<br><br>\n";
                    echo "Please email the above error using the Comments/Help/Support link on the \n";
	            echo "left navigation bar.\n";
	            echo "</form>\n";
	            echo "</body></html>\n";
	            $ret_val = disconnect();
	            exit;
	          }#end if error in exec call to split file
                $file_array = glob("$prefix*");
                $num_files_split = sizeof($file_array);
                sort($file_array);
                foreach ($file_array as $key=>$file)
                  {
                    $command = "./delete_data_from_database.php '$user_file' '$file' '$database_id' 'Delete Phenotype Values' ";
                    $command.= "'$_SESSION[user_name]' '$background_flag' >> dummy_delete_data_background_output.txt &";

                    $last_line = system($command,$sys_ret_val);
                    if ($sys_ret_val != '0')
	              {
	                $date = date('Y-m-d H:i:s');
	                echo "Error #59: $date<br>\n";
	                echo "Error in system call to delete_data_from_database.php from delete_phenotype_values.php<br>\n";
	                echo "The return value of the sytem call was $sys_ret_val<br><br>\n";
                        echo "Please email the above error using the Comments/Help/Support link on the \n";
	                echo "left navigation bar.\n";
	                echo "</form>\n";
	                echo "</body></html>\n";
	                $ret_val = disconnect();
	                exit;
	              }#end if error in system call
                  }#end foreach sub file
              }#end if file too big for php
            else #don't need to split file
              {
                $command = "./delete_data_from_database.php '$user_file' '$uploaded_file_name' '$database_id' 'Delete Phenotype Values' ";
                $command.= "'$_SESSION[user_name]' '$background_flag' >> dummy_delete_data_background_output.txt &";

                $last_line = system($command,$sys_ret_val);
                if ($sys_ret_val != '0')
	          {
	            $date = date('Y-m-d H:i:s');
	            echo "Error #60: $date<br>\n";
	            echo "Error in system call to delete_data_from_database.php from delete_phenotype_values.php<br>\n";
	            echo "The return value of the sytem call was $sys_ret_val<br><br>\n";
                    echo "Please email the above error using the Comments/Help/Support link on the \n";
	            echo "left navigation bar.\n";
	            echo "</form>\n";
	            echo "</body></html>\n";
	            $ret_val = disconnect();
	            exit;
	          }#end if error in system call
              }#end else don't need to split file
          }#end if run in background
        else #run in foreground
          {
            $background_flag = 0;
            $command = "./delete_data_from_database.php '$user_file' '$uploaded_file_name' '$database_id' 'Delete Phenotype Values' '$_SESSION[user_name]' '$background_flag' ";

            passthru($command,$sys_ret_val);
            if ($sys_ret_val != '0')
	      {
                echo "<br><br>\n";
	        $date = date('Y-m-d H:i:s');
	        echo "Error #61: $date<br>\n";
	        echo "Error in system call to delete_data_from_database.php from delete_phenotype_values.php<br>\n";
	        echo "See the error message above for an explanation of the error<br><br>\n";
                echo "Please email the above error using the Comments/Help/Support link on the \n";
	        echo "left navigation bar.\n";
	        echo "</form>\n";
	        echo "</body></html>\n";
	        $ret_val = disconnect();
	        exit;
	      }#end if error in system call
          }#end else run in foreground

	$ret_val = chdir("../../cgi-bin/");
        if (!$ret_val)
          {
            $date = date('Y-m-d H:i:s');
            echo "Error #62: $date<br>\n";
            echo "Error in call to chdir from delete_phenotype_values.php<br>\n";
            echo "Unable to change directory to cgi-bin directory<br><br>\n";
            echo "Please email the above error using Comments/Help/Support link \n";
	    echo "in the left navigation bar.<br>\n";
	    echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end error in call to chdir

        if ($background_flag)
          {
            echo "<center>\n";
	    echo "<b>The data you uploaded is being inserted into the database.<br>\n";
            if ($uploaded_file_size >= $max_file_size_not_to_split)
              {
                echo "The file was too large to handle, so it was split into $num_files_split sub files, and each \n";
                echo "sub file is being processed separately.  So you will recieve an email when the data in each sub file \n";
                echo "has been deleted from the database \n";
              }#end if split file
	    else {echo "You will receive an email when all the data has been deleted from the database \n";}
	    echo "detailing the results of the file upload and any errors.</b><br><br>\n";
	    echo "</center>\n";
            echo "</body></html>\n";
            $ret_val = disconnect();
            exit;
          }#end if running in background

	if (!$background_flag){echo "Finished sending update emails to all the users<br>\n";}
      }#end if have file to delete from
  }#end if (auth_user == '1')	   
$ret_val = disconnect();
echo "</body></html>\n";
?>
Return current item: Generic Genetic Studies Database