<?php
# ---------------------------------------------------------------------
# truc is a tool for requirement and use case tracking
# Copyright (C) 2006 ASDIS - http://sf.net/projects/truc
#
# (rth) Initial truc version based on rth
#       Copyright (C) 2005 George Holbrook - rth@lists.sourceforge.net
#
# This program is distributed under the terms and conditions of the GPL
# See the README and LICENSE files for details
#----------------------------------------------------------------------

# ----------------------------------------------------------------------4/13/2006
# Returns array of all test statuses with optional blank value at end.
#
# OUTPUT:
#   array of all unique test statuses.
# ----------------------------------------------------------------------
function test_get_status( $blank=false ) {

	$test_status =  array( 	"New",
                                        'Completed',
					"Reviewed",
					"Approved",
					"Rejected",
					"Released",
					"Implemented");


	# add a blank value to the array.  Useful when populating a listbox when you want ' ' as an option
	if( $blank ) {
		$test_status[] = "";
	}

	return $test_status;
}

# ----------------------------------------------------------------------
# Create and run query for displaying test records. Display table header.
# OUTPUT:
#   array of test records.
# ----------------------------------------------------------------------
function &test_apply_filter($project_id, $where_clause, $per_page, $order_by, $order_dir, $page_number, $csv_name=null) {

    global $db;
	# WE SHOULD REWRITE TEST_GET_DETAIL SO YOU CAN INPUT A WHERE CLAUSE.
	$test_tbl				= TEST_TBL;
	$f_project_id			= $test_tbl .".". TEST_PROJ_ID;
	$f_test_id				= $test_tbl .".". TEST_ID;
	$f_test_name			= $test_tbl .".". TEST_NAME;
	$f_manual_test			= $test_tbl .".". TEST_MANUAL;
	$f_automated_test		= $test_tbl .".". TEST_AUTOMATED;
	$f_ba_owner				= $test_tbl .".". TEST_BA_OWNER;
	$f_qa_owner				= $test_tbl .".". TEST_QA_OWNER;
	$f_tester				= $test_tbl .".". TEST_TESTER;
	$f_test_type			= $test_tbl .".". TEST_TESTTYPE;
    $f_area_tested			= $test_tbl .".". TEST_AREA_TESTED;
	$f_test_priority		= $test_tbl .".". TEST_PRIORITY;
	$f_test_status			= $test_tbl .".". TEST_STATUS;
    $f_deleted				= $test_tbl .".". TEST_DELETED;
    $f_archived				= $test_tbl .".". TEST_ARCHIVED;
	$f_autopass				= $test_tbl .".". TEST_AUTO_PASS;
	$f_dateassigned			= $test_tbl .".". TEST_DATE_ASSIGNED;
	$f_dateexpcomplete		= $test_tbl .".". TEST_DATE_EXPECTED;
	$f_dateactcomplete		= $test_tbl .".". TEST_DATE_COMPLETE;
	$f_datebasignoff		= $test_tbl .".". TEST_BA_SIGNOFF;
	$f_test_comments		= $test_tbl .".". TEST_COMMENTS;
	/*
	$f_project_id		  = $test_tbl .".". PROJECT_ID;
	$f_test_type          = $test_tbl .".". TEST_TESTTYPE;
    $f_area_tested        = $test_tbl .".". TEST_AREA_TESTED;
	$f_autopass           = $test_tbl .".". TEST_AUTO_PASS;
	$f_test_deleted       = $test_tbl .".". TEST_DELETED;
    $f_test_archived      = $test_tbl .".". TEST_ARCHIVED;
	$f_test_purpose       = $test_tbl .".". TEST_PURPOSE;
	$f_test_comments      = $test_tbl .".". TEST_COMMENTS;
	$f_manual_tests       = $test_tbl .".". TEST_MANUAL;
	$f_manual_test        = $test_tbl .".". TEST_MANUAL;
	$f_performance        = $test_tbl .".". TEST_LR;
	$f_automated_test     = $test_tbl .".". TEST_AUTOMATED;
	$f_ba_owner           = $test_tbl .".". TEST_BA_OWNER;
	$f_qa_owner           = $test_tbl .".". TEST_QA_OWNER;
	$f_test_priority      = $test_tbl .".". TEST_PRIORITY;
	$f_assigned_to        = $test_tbl .".". TEST_ASSIGNED_TO;
	$f_assigned_by        = $test_tbl .".". TEST_ASSIGNED_BY;
	$f_dateassigned       = $test_tbl .".". TEST_DATE_ASSIGNED;
	$f_dateexpcomplete    = $test_tbl .".". TEST_DATE_EXPECTED;
	$f_dateactcomplete    = $test_tbl .".". TEST_DATE_COMPLETE;
	$f_datebasignoff      = $test_tbl .".". TEST_BA_SIGNOFF;
	$f_duration			  = $test_tbl .".". TEST_DURATION;
	*/

	//$test_ver_tbl		= TEST_VERS_TBL;
	//$f_vers_test_id		= $test_ver_tbl .".". TEST_VERS_TEST_ID;
	//$f_vers_id			= $test_ver_tbl .".". TEST_VERS_ID;

	$q = "SELECT
			$f_test_id,
			$f_test_name,
			$f_manual_test,
			$f_automated_test,
			$f_ba_owner,
			$f_qa_owner,
			$f_tester,
			$f_test_type,
			$f_area_tested,
			$f_test_priority,
			$f_test_status,
			$f_autopass,
			$f_dateassigned,
			$f_dateexpcomplete,
			$f_dateactcomplete,
			$f_datebasignoff,
			$f_test_comments
		 FROM $test_tbl
		 WHERE $f_project_id = '$project_id'
		 AND $f_deleted = 'N'
		 AND $f_archived = 'N'";
	/*
    $q = "SELECT $test_tbl.*, $f_vers_id
		 FROM $test_tbl, $test_ver_tbl
		 WHERE $f_test_id = $f_vers_test_id
		 AND $f_project_id = '$project_id'
		 AND $f_deleted = 'N'
		 AND $f_archived = 'N'";
	*/

    $order_clause 	= " ORDER BY $order_by $order_dir";
	$where_clause 	= $where_clause." GROUP BY $f_test_name";
    $q 				.= $where_clause.$order_clause;

	if( $per_page!=0 && $page_number!=0 ) {

		$row_count = db_num_rows( $db, db_query($db, $q) );

		$page_number = util_page_number($page_number, $row_count, $per_page);

		# Add the limit clause to the query so that we only show n number of records per page
		$offset = ( ( $page_number - 1 ) * $per_page );
		html_table_offset( 	$row_count,
							$per_page,
							$page_number,
							$order_by,
							$order_dir,
							$csv_name );

		$q .= " LIMIT $offset, ".$per_page;

	}

	$rs =& db_query($db, $q);
	return db_fetch_array($db, $rs);
}

function test_get_all_ids($project_id) {

    global $db;
	$test_tbl			= TEST_TBL;
	$f_test_id			= $test_tbl .".". TEST_ID;
	$f_test_name        = $test_tbl .".". TEST_NAME;
	$f_project_id		= $test_tbl .".". TEST_PROJ_ID;
    $f_deleted			= $test_tbl .".". TEST_DELETED;
    $f_archived			= $test_tbl .".". TEST_ARCHIVED;

	$q = "SELECT $f_test_id
			 FROM $test_tbl
			 WHERE $f_project_id = '$project_id'
			 AND $f_deleted = 'N'
			 AND $f_archived = 'N'";

	$rs =& db_query($db, $q);
	return db_fetch_array($db, $rs);
}


# ----------------------------------------------------------------------
# Create where clause for tests and run query to extract test data
# OUTPUT:
#   array of test records.
# ----------------------------------------------------------------------
function &test_filter_rows( $project_id, 
							$manauto, 
							$baowner,
							$qaowner, 
							$tester, 
							$testtype, 
							$test_area,
							$test_status,
							$per_page,
							$test_search,
							$orderby,
							$order_dir,
							$page_number,
							$csv_name=null) {

    $where_clause = test_filter_generate_where_clause($manauto, $baowner, $qaowner,$tester, $testtype, $test_area, $test_status, $test_search);
    $row =& test_apply_filter($project_id, $where_clause, $per_page, $orderby, $order_dir, $page_number, $csv_name);

    return $row;
}

# ----------------------------------------------------------------------
# Create where clause for tests and run query to extract test data
# OUTPUT:
#   array of test records.
# ----------------------------------------------------------------------
function &test_copy_filter_rows(	$project_id,
									$release_id,
									$build_id,
									$testset_id,
									$filter_man_auto,
									$filter_ba_owner,
									$filter_qa_owner,
									$filter_tester,
									$filter_test_type,
									$filter_area_tested,
									$per_page,
									$test_search,
									$order_by,
									$order_dir,
									$page_number) {

    $where_clause = test_filter_generate_where_clause(	$filter_man_auto,
														$filter_ba_owner,
														$filter_qa_owner,
														$filter_tester,
														$filter_test_type,
														$filter_area_tested,
														$test_status="",
														$test_search);

    $row =& test_copy_apply_filter(	$project_id,
									$release_id,
									$build_id,
									$testset_id,
									$per_page,
									$order_by,
									$order_dir,
									$page_number,
									$where_clause );

    return $row;
}

# ----------------------------------------------------------------------
# Create where clause for test query
# OUTPUT:
#   Where clause string
# ----------------------------------------------------------------------
function test_filter_generate_where_clause($manauto, $baowner, $qaowner,$tester, $testtype, $test_area, $test_status, $test_search) {

    $f_test_name          = TEST_TBL. "." .TEST_NAME;
    $f_manual_tests       = TEST_TBL. "." .TEST_MANUAL;
    $f_automated_tests    = TEST_TBL. "." .TEST_AUTOMATED;
    $f_ba_owner           = TEST_TBL. "." .TEST_BA_OWNER;
    $f_qa_owner           = TEST_TBL. "." .TEST_QA_OWNER;
    $f_tester			  =	TEST_TBL. "." .TEST_TESTER;
    $f_test_type          = TEST_TBL. "." .TEST_TESTTYPE;
    $f_area_tested        = TEST_TBL. "." .TEST_AREA_TESTED;
    $f_test_status        = TEST_TBL. "." .TEST_STATUS;


    $where_clause = '';

    # MANUAL AUTOMATED
    if ( !empty($manauto) && $manauto != 'all') {
        if( $manauto == 'Manual' ) {
            $where_clause = $where_clause. " AND $f_manual_tests = 'YES' AND $f_automated_tests = ''";
        }
        elseif( $manauto == 'Automated' ) {
            $where_clause = $where_clause. " AND $f_manual_tests = '' AND $f_automated_tests = 'YES'";
        }
        else {
            $where_clause = $where_clause. " AND $f_manual_tests = 'YES' AND $f_automated_tests = 'YES'";
        }
    }
    # BA OWNER
    if ( !empty($baowner)  && $baowner != 'all') {

        $where_clause = $where_clause." AND $f_ba_owner = '$baowner'";
    }
    # QA OWNER
    if ( !empty($qaowner) && $qaowner != 'all') {

        $where_clause = $where_clause." AND $f_qa_owner = '$qaowner'";
    }
    # TESTER
	if ( !empty($tester) && $tester != 'all') {

	    $where_clause = $where_clause." AND $f_tester = '$tester'";
    }
    # TEST TYPE
    if ( !empty($testtype) && $testtype != 'all') {

        $where_clause = $where_clause." AND $f_test_type = '$testtype'";
    }
    # AREA TESTED
    if ( !empty($test_area) && $test_area != 'all') {

        $where_clause = $where_clause." AND $f_area_tested = '$test_area'";
    }
    # TEST STATUS
    if ( !empty($test_status) && $test_status != 'all') {

        $where_clause = $where_clause." AND $f_test_status = '$test_status'";
    }

	# SEARCH
	if ( !empty($test_search) ) {

		$where_clause = $where_clause." AND  ($f_test_name  LIKE '%$test_search%')";
    }

    return $where_clause;
}



# ----------------------------------------------------------------------
# Create where clause for test workflow and run query to extract test
# workflow data
# OUTPUT:
#   array of test workflow records.
# ----------------------------------------------------------------------
function test_workflow_filter_rows($project_id, $manauto, $test_type, $baowner, $qaowner,$tester, $area_tested, $test_status, $per_page,$test_search, $orderby, $order_dir, $page_number, $csv_name=null) {

	$where_clause = test_workflow_filter_generate_where_clause($manauto, $test_type, $baowner, $qaowner,$tester, $area_tested, $test_status, $test_search);
    $row = test_apply_filter($project_id, $where_clause, $per_page, $orderby, $order_dir, $page_number, $csv_name);
    return $row;

}


# ----------------------------------------------------------------------
# Create where clause for test workflow query
# OUTPUT:
#   Where clause string
# ----------------------------------------------------------------------
function test_workflow_filter_generate_where_clause($manauto, $test_type, $baowner, $qaowner,$tester, $test_area, $test_status, $test_search) {

    $f_test_name			= TEST_TBL. "." .TEST_NAME;
    $f_manual_tests			= TEST_TBL. "." .TEST_MANUAL;
    $f_automated_tests		= TEST_TBL. "." .TEST_AUTOMATED;
    $f_ba_owner				= TEST_TBL. "." .TEST_BA_OWNER;
    $f_qa_owner				= TEST_TBL. "." .TEST_QA_OWNER;
    $f_tester				= TEST_TBL. "." .TEST_TESTER;
    $f_test_status			= TEST_TBL. "." .TEST_STATUS;
	$f_test_type			= TEST_TBL. "." .TEST_TESTTYPE;
	$f_area_tested        	= TEST_TBL. "." .TEST_AREA_TESTED;

    $where_clause = '';

    # MANUAL AUTOMATED
    if ( !empty($manauto) && $manauto != 'all') {
        if( $manauto == 'Manual' ) {
            $where_clause = $where_clause. " AND $f_manual_tests = 'YES' AND $f_automated_tests = ''";
        }
        elseif( $manauto == 'Automated' ) {
            $where_clause = $where_clause. " AND $f_manual_tests = '' AND $f_automated_tests = 'YES'";
        }
        else {
            $where_clause = $where_clause. " AND $f_manual_tests = 'YES' AND $f_automated_tests = 'YES'";
        }
    }
    # TEST TYPE
    if ( !empty($test_type) && $test_type != 'all') {

        $where_clause = $where_clause." AND $f_test_type = '$test_type'";
    }
    # BA OWNER
    if ( !empty($baowner)  && $baowner != 'all') {

        $where_clause = $where_clause." AND $f_ba_owner = '$baowner'";
    }
    # QA OWNER
    if ( !empty($qaowner) && $qaowner != 'all') {

        $where_clause = $where_clause." AND $f_qa_owner = '$qaowner'";
    }
    # TESTER
    if ( !empty($tester) && $tester != 'all') {

        $where_clause = $where_clause." AND $f_tester = '$tester'";
    }
    # AREA TESTED
    if ( !empty($test_area) && $test_area != 'all') {

        $where_clause = $where_clause." AND $f_area_tested = '$test_area'";
    }
    # TEST STATUS
    if ( !empty( $test_status ) && $test_status != 'all') {

        $where_clause = $where_clause." AND $f_test_status = '$test_status'";
    }
	# SEARCH
	if ( !empty($test_search) ) {

		$where_clause = $where_clause." AND  ($f_test_name  LIKE '%$test_search%')";
    }

    return $where_clause;
}


# ----------------------------------------------------------------------
# Get filename corresponding to a test
#
# INPUT:
#   Test id
# OUTPUT:
#   Corresponding filename
# ----------------------------------------------------------------------
function test_get_filename($test_id) {
    global $db;

	$td_tbl					= MAN_TD_TBL;
	$f_td_id				= $td_tbl .".". MAN_TD_MANUAL_TEST_ID;
	$f_test_id				= $td_tbl .".". MAN_TD_TEST_ID;
	$f_display_name			= $td_tbl .".". MAN_TD_DISPLAY_NAME;

	$td_vers_tbl			= MAN_TD_VER_TBL;
	$f_td_vers_id			= MAN_TD_VER_TBL .".". MAN_TD_VER_MANUAL_TEST_ID;
	$f_timestamp			= MAN_TD_VER_TBL .".". MAN_TD_VER_TIME_STAMP;


    $q = "SELECT
				MAX($f_timestamp), $f_display_name
		  FROM $td_tbl
		  INNER JOIN $td_vers_tbl ON $f_td_vers_id = $f_td_id
		  WHERE $f_test_id = '$test_id'
		  GROUP BY $f_timestamp ASC";
    $rs = $db->Execute( $q );
    $row = db_fetch_row( $db, $rs ) ;

    return ($row[MAN_TD_DISPLAY_NAME]);

}






# ----------------------------------------------------------------------
# Print Test submenu
# INPUT:
#   Current Page (so that it will not be shown as a hyperlink)
# ----------------------------------------------------------------------
function test_menu_print($page) {

	$screen_link = lang_get('screen_link');
	$field_link = lang_get('field_link');
	$add_field_link = lang_get('add_field');
	$function_link = lang_get('function_link');
    $test_link = lang_get('test_link');
    $test_add_link = lang_get('test_add_link');
    $test_workflow_link = lang_get('test_workflow_link');

	/*
    $menu_items = array(
			$function_link => 'function_page.php',
            $test_link => 'test_page.php',
            $test_add_link => 'test_add_page.php',
            $test_workflow_link => 'test_workflow_page.php',
			$screen_link => 'screen_page.php',
			$field_link => 'field_page.php',
			$add_field_link => 'field_add_page.php',
            );
	*/

	$menu_items = array(
			$test_link => 'test_page.php',
            $test_add_link => 'test_add_page.php',
//            $test_workflow_link => 'test_workflow_page.php',
            );

    html_print_sub_menu( $page, $menu_items );
}

# ----------------------------------------------------------------------
# Print Test Menu that displays Test Steps, File Upload, and Requirement Associaton
# INPUT:
#   TestID
# ----------------------------------------------------------------------
function test_sub_menu_print( $test_id, $page, $tab,$version_control_time=0  ) {

	$normal_url = $page ."?test_id=". $test_id;


	$style_enabled = 'page-numbers';
	$style_disabled = 'page-numbers-disabled';

	print"<br>";
	print"<div class='sub_menu' style='border-bottom:1px solid; padding-bottom:10px;margin-bottom:20px'>\n";
	print "<form name='test_detail_tab' method=post action='$normal_url'>\n";
	print"<ul>\n";

	$url = $normal_url . "&amp;tab=1";
        if ($version_control_time!=0) $url.="&amp;time=".$version_control_time;
        print"<li><a";
        if ($tab == '1') echo ' class="selected"';
        print" href='$url'>". lang_get('test_steps') ."</a></li>";

	$url = $normal_url . "&amp;tab=5";
        if ($version_control_time!=0) $url.="&amp;time=".$version_control_time;
        print"<li><a";
        if ($tab == '5') echo ' class="selected"';
        print" href='$url'>Steps Summary</a></li>";


	$url = $normal_url . "&amp;tab=3";
        if ($version_control_time!=0) $url.="&amp;time=".$version_control_time;
        print"<li><a";
        if ($tab == '3') echo ' class="selected"';
        print" href='$url'>". lang_get('req_assoc_link') ."</a></li>";

	$url = $normal_url . "&amp;tab=2";
        if ($version_control_time!=0) $url.="&amp;time=".$version_control_time;
        print"<li><a";
        if ($tab == '2') echo ' class="selected"';
        print" href='$url'>". lang_get( 'test_docs' ) ."</a></li>";

	$url = $normal_url . "&amp;tab=4";
        if ($version_control_time!=0) $url.="&amp;time=".$version_control_time;
        print"<li><a";
        if ($tab == '4') echo ' class="selected"';
        print" href='$url'>". lang_get( 'usecase_history' ) ."</a></li>";

	print"</ul>\n";
	print"</form>\n";
	print"</div>\n";

}


function usecase_unlock($usecase_id) {

	global $db;

	$tbl_usecase 				= TEST_TBL;
	$f_usecase_id 				= $tbl_usecase .".". TEST_ID;
	$f_usecase_locked_by			= $tbl_usecase .".". USECASE_LOCKED_BY;
	$f_usecase_locked_date			= $tbl_usecase .".". USECASE_LOCKED_DATE;

	$q = "	UPDATE $tbl_usecase
			SET
				$f_usecase_locked_by = '',
				$f_usecase_locked_date = ''
			WHERE $f_usecase_id = '$usecase_id'";

	db_query($db, $q);
}

function usecase_lock($usecase_id, $user) {

	global $db;

	$tbl_usecase 				= TEST_TBL;
	$f_usecase_id 				= $tbl_usecase .".". TEST_ID;
	$f_usecase_locked_by			= $tbl_usecase .".". USECASE_LOCKED_BY;
	$f_usecase_locked_date			= $tbl_usecase .".". USECASE_LOCKED_DATE;

	$q = "	UPDATE $tbl_usecase
			SET
				$f_usecase_locked_by = '$user',
				$f_usecase_locked_date = '".date("Y-m-d H:i:s")."'
			WHERE $f_usecase_id = '$usecase_id'";

	db_query($db, $q);
}

function usecase_get_locks($project_id,$username) {
        // IGNORES: project_id!
  	global $db;

	$tbl_usecase 				= TEST_TBL;
	$f_usecase_id 				= $tbl_usecase .".". TEST_ID;
	$f_locked_by			= $tbl_usecase .".". USECASE_LOCKED_BY;
	$f_locked_date			= $tbl_usecase .".". USECASE_LOCKED_DATE;

	$q = "	SELECT
				*
			FROM $tbl_usecase
			WHERE $f_locked_by = '".$username."'
			ORDER BY $f_locked_date ASC";
	//print"$q<br>";



	$rows = db_fetch_array( $db, db_query($db, $q) );

	return $rows;
}
# ----------------------------------------------------------------------
# Get list of test priorities
# OUTPUT:
#   Array of test available priorities
# ----------------------------------------------------------------------
function test_get_priorities() {
    $low = lang_get('priority_low');
    $medium = lang_get('priority_medium');
    $high = lang_get('priority_high');
    $priorities_arr = array($low, $medium, $high, '');
    return $priorities_arr;
}


# ----------------------------------------------------------------------
# Add Test record to database
# ----------------------------------------------------------------------
function test_add_test($project_id, $testname, $testpurpose, $testcomments, $testpriority, $teststatus, $testareatested, $testtype,  $ba_owner, $qa_owner, $assigned_to, $assigned_by, $dateassigned, $dateexpcomplete, $dateactcomplete, $datebasignoff, $duration, $autopass, $steps, $auto, $performance,$test_pre,$test_post) {

    global $db;

	$s_project_properties = session_get_project_properties();
	$s_test_versions = $s_project_properties['test_versions'];

	$test_tbl             = TEST_TBL;
	$f_test_id			  = $test_tbl .".". TEST_ID;
	$f_test_name          = TEST_NAME;
	$f_project_id		  = PROJECT_ID;
	$f_test_type          = TEST_TESTTYPE;
    $f_area_tested        = TEST_AREA_TESTED;
	$f_autopass           = TEST_AUTO_PASS;
	$f_test_deleted       = TEST_DELETED;
    $f_test_archived      = TEST_ARCHIVED;
	$f_test_purpose       = TEST_PURPOSE;
	$f_usecase_pre        = USECASE_PRE_CONDITION;
	$f_usecase_post        = USECASE_POST_CONDITION;
	$f_test_purpose       = TEST_PURPOSE;
	$f_test_comments      = TEST_COMMENTS;
	$f_manual_tests       = TEST_MANUAL;
	$f_manual_test        = TEST_MANUAL;
	$f_performance        = TEST_LR;
	$f_automated_test     = TEST_AUTOMATED;
	$f_ba_owner           = TEST_BA_OWNER;
	$f_qa_owner           = TEST_QA_OWNER;
	$f_test_priority      = TEST_PRIORITY;
	$f_test_status        = TEST_STATUS;
	$f_assigned_to        = TEST_ASSIGNED_TO;
	$f_assigned_by        = TEST_ASSIGNED_BY;
	$f_dateassigned       = TEST_DATE_ASSIGNED;
	$f_dateexpcomplete    = TEST_DATE_EXPECTED;
	$f_dateactcomplete    = TEST_DATE_COMPLETE;
	$f_datebasignoff      = TEST_BA_SIGNOFF;
	$f_duration			  = TEST_DURATION;

	$q  = "INSERT INTO $test_tbl
		  ($f_test_purpose, $f_test_name, $f_project_id, $f_test_comments, $f_area_tested, $f_test_type,
		   $f_test_priority, $f_test_status, $f_ba_owner, $f_qa_owner, $f_assigned_to,
		   $f_assigned_by, $f_dateassigned, $f_dateexpcomplete, $f_dateactcomplete,
		   $f_datebasignoff, $f_duration, $f_manual_test, $f_automated_test, $f_performance,
		   $f_autopass, $f_test_deleted, $f_test_archived,$f_usecase_pre,$f_usecase_post)
		  VALUES
		  ('$testpurpose', '$testname', '$project_id', '$testcomments', '$testareatested',
		   '$testtype', '$testpriority', '$teststatus', '$ba_owner', '$qa_owner',
		   '$assigned_to', '$assigned_by', '$dateassigned', '$dateexpcomplete',
		   '$dateactcomplete', '$datebasignoff', '$duration', '$steps', '$auto', '$performance',
		   '$autopass', 'N', 'N','$test_pre','$test_post')";

	db_query( $db, $query );


}


# ----------------------------------------------------------------------
# Add Test record to database
# ----------------------------------------------------------------------
function test_add_test_version($project_id, $test_name, $purpose, $comments, $priority, $status, $area_tested, $test_type,  $ba_owner, $qa_owner, $tester, $assigned_to, $assigned_by, $date_assigned, $date_expected, $date_completed, $date_signoff, $duration, $autopass, $manual, $automated, $performance, $assoc_req,$test_pre,$test_post) {

    global $db;

	$test_req_assoc_tbl	 		= TEST_REQ_ASSOC_TBL;
	$f_test_req_assoc_id		= TEST_REQ_ASSOC_ID;
	$f_test_req_assoc_test_id	= TEST_REQ_ASSOC_TEMPEST_TEST_ID;
	$f_test_req_assoc_req_id	= TEST_REQ_ASSOC_REQ_ID;

	$test_tbl             = TEST_TBL;
	$f_test_id			  = TEST_ID;
	$f_test_name          = TEST_NAME;
	$f_project_id		  = PROJECT_ID;
	$f_purpose			  = TEST_PURPOSE;
	$f_priority			  = TEST_PRIORITY;
	$f_status			  = TEST_STATUS;
	$f_test_type          = TEST_TESTTYPE;
    $f_area_tested        = TEST_AREA_TESTED;
	$f_autopass           = TEST_AUTO_PASS;
	$f_test_deleted       = TEST_DELETED;
    $f_test_archived      = TEST_ARCHIVED;
	$f_ba_owner           = TEST_BA_OWNER;
	$f_qa_owner           = TEST_QA_OWNER;
	$f_tester			  = TEST_TESTER;
	$f_manual_test        = TEST_MANUAL;
	$f_automated_tests    = TEST_AUTOMATED;
	$f_performance        = TEST_LR;
	$f_assigned_to		  = TEST_ASSIGNED_TO;
	$f_assigned_by		  = TEST_ASSIGNED_BY;
	$f_date_assigned	  = TEST_DATE_ASSIGNED;
	$f_date_expected	  = TEST_DATE_EXPECTED;
	$f_date_completed	  = TEST_DATE_COMPLETE;
	$f_duration			  = TEST_DURATION;
	$f_last_updated		  = TEST_LAST_UPDATED;
	$f_last_updated_by	  = TEST_LAST_UPDATED_BY;

	$last_updated		  = date_get_short_dt();
	$s_user_properties 	  = session_get_user_properties();
	$s_user_name		  = $s_user_properties['username'];
	$last_updated_by	  = $s_user_name;

	$test_version_tbl	  = TEST_VERS_TBL;
	$f_vers_test_id		  = TEST_VERS_TEST_ID;
	$f_version_no		  = TEST_VERS_NUMBER;
	$f_latest_vers		  = TEST_VERS_LATEST;
	$f_active_version	  = TEST_VERS_ACTIVE;
	$f_test_comments      = TEST_VERS_COMMENTS;
	$f_test_status        = TEST_VERS_STATUS;
	$f_version_author	  = TEST_VERS_AUTHOR;
	$f_date_created		  = TEST_VERS_DATE_CREATED;
	$f_usecase_pre        = USECASE_PRE_CONDITION;
	$f_usecase_post        = USECASE_POST_CONDITION;

	$q = "INSERT INTO $test_tbl
		  ( $f_test_name, $f_project_id, $f_purpose, $f_area_tested, $f_test_type,
		   $f_autopass, $f_test_deleted, $f_test_archived, $f_ba_owner, $f_qa_owner, $f_tester,
		   $f_manual_test, $f_automated_tests, $f_performance, $f_priority, $f_status,
		   $f_assigned_to, $f_assigned_by, $f_date_assigned, $f_date_expected,
		   $f_date_completed, $f_duration, $f_last_updated, $f_last_updated_by,$f_usecase_pre,$f_usecase_post)
		  VALUES
		  ('$test_name', '$project_id', '$purpose', '$area_tested', '$test_type',
		   '$autopass', 'N', 'N', '$ba_owner', '$qa_owner', '$tester', '$manual', '$automated',
		   '$performance', '$priority', '$status', '$assigned_to', '$assigned_by', '$date_assigned',
		   '$date_expected', '$date_completed', '$duration', '$last_updated', '$last_updated_by','$test_pre','$test_post')";

	#print"$q<br>";
	db_query( $db, $q );


	$q_id = "SELECT $f_test_id FROM $test_tbl WHERE $f_test_name = '$test_name'";
	$test_id = db_get_one( $db, $q_id );

	$created_date = date_get_short_dt();
	$author = session_get_username();

	$q_version = "INSERT INTO $test_version_tbl
			($f_vers_test_id, $f_version_no, $f_latest_vers, $f_test_comments, $f_test_status,
			 $f_version_author, $f_date_created,  $f_active_version )
			VALUES
			('$test_id', '1.0', 'Y', '$comments', '$status',
			'$author', '$created_date', 'Y' )";

	#print"$q_version<br>";
	db_query( $db, $q_version );

	# associate with requirement
	if( $assoc_req ) {

		$q = "	INSERT INTO	$test_req_assoc_tbl
					($f_test_req_assoc_test_id, $f_test_req_assoc_req_id)
				VALUES
					($test_id, $assoc_req)";

		db_query($db, $q);
	}

}



# ----------------------------------------------------------------------
# Update Test record
# ----------------------------------------------------------------------

function test_update_test($test_id, $test_version_id, $test_name, $test_purpose, $testcomments, $priority,
                          $test_status, $area_tested, $test_type,  $ba_owner, $qa_owner, $tester,
                          $assigned_to, $assigned_by, $date_assigned, $date_expected,
                          $date_completed, $duration, $autopass, $manual_test, $automated_test, $performance,$test_pre,$test_post) {

    global $db;

	$test_tbl             = TEST_TBL;
	$f_test_id			  = TEST_ID;
	$f_test_name          = TEST_NAME;
	$f_project_id		  = PROJECT_ID;
	$f_test_purpose		  = TEST_PURPOSE;
	$f_priority			  = TEST_PRIORITY;
	$f_comments   		  = TEST_COMMENTS;
	$f_test_type          = TEST_TESTTYPE;
    $f_area_tested        = TEST_AREA_TESTED;
	$f_autopass           = TEST_AUTO_PASS;
	$f_test_deleted       = TEST_DELETED;
    $f_test_archived      = TEST_ARCHIVED;
	$f_ba_owner           = TEST_BA_OWNER;
	$f_qa_owner           = TEST_QA_OWNER;

	$f_tester			  = TEST_TESTER;
	$f_manual_test        = TEST_MANUAL;
	$f_automated_test     = TEST_AUTOMATED;
	$f_performance        = TEST_LR;
	$f_assigned_to		  = TEST_ASSIGNED_TO;
	$f_assigned_by		  = TEST_ASSIGNED_BY;
	$f_date_assigned	  = TEST_DATE_ASSIGNED;
	$f_date_expected	  = TEST_DATE_EXPECTED;
	$f_date_completed	  = TEST_DATE_COMPLETE;
	$f_duration			  = TEST_DURATION;
	$f_test_status		  = TEST_STATUS;

	$f_usecase_pre        = USECASE_PRE_CONDITION;
	$f_usecase_post        = USECASE_POST_CONDITION;

	/*
	$f_signoff_by		  = TEST_SIGNOFF_BY;
	$f_signoff_date		  = TEST_SIGNOFF_DATE;
	$test_version_tbl	  = TEST_VERS_TBL;
	$f_vers_id			  = TEST_VERS_ID;
	$f_vers_test_id		  = TEST_VERS_TEST_ID;
	$f_version_no		  = TEST_VERS_NUMBER;
	$f_latest_vers		  = TEST_VERS_LATEST;
	$f_test_status        = TEST_VERS_STATUS;
	$f_signoff_date       = TEST_VERS_SIGNOFF_DATE;
	$f_signoff_by		  = TEST_VERS_SIGNOFF_BY;
	*/

    $q = "UPDATE $test_tbl SET
              $f_test_purpose = '$test_purpose',
              $f_comments = '$testcomments',
              $f_test_name = '$test_name',
              $f_area_tested = '$area_tested',
              $f_test_type = '$test_type',
              $f_priority = '$priority',
              $f_ba_owner = '$ba_owner',
              $f_qa_owner = '$qa_owner',
              $f_tester   = '$tester',
              $f_assigned_to = '$assigned_to',
              $f_assigned_by = '$assigned_by',
              $f_date_assigned = '$date_assigned',
              $f_date_expected = '$date_expected',
              $f_date_completed = '$date_completed',
			  $f_duration = '$duration',
              $f_manual_test = '$manual_test',
              $f_automated_test = '$automated_test',
              $f_performance = '$performance',
              $f_autopass = '$autopass',
              $f_usecase_pre= '$test_pre',
              $f_usecase_post='$test_post',
			  $f_test_status = '$test_status'
              WHERE
              $f_test_id = '$test_id'";

	db_query( $db, $q );

	# update last updated by and date
	test_change_last_update( $test_id );


	/*
	# UPDATE VERSION
	$q_ver = "UPDATE $test_version_tbl SET
			 $f_test_status = '$test_status',
			 $f_signoff_date = '$signoff_date',
			 $f_signoff_by = '$signoff_by'
			 WHERE
			 $f_vers_id = '$test_version_id'";


	db_query( $db, $q_ver );
	*/

}


# ----------------------------------------------------------------------
# Upgrade Test record
# ----------------------------------------------------------------------
function test_upgrade_test($test_id, $test_version_id, $test_name, $test_purpose, $testcomments, $priority,
                           $test_status, $area_tested, $test_type,  $ba_owner, $qa_owner, $tester,
                           $assigned_to, $assigned_by, $date_assigned, $date_expected,
                           $date_completed, $duration, $autopass, $manual_test, $automated_test, $performance,$test_pre,$test_post) {

    global $db;

	$test_tbl             = TEST_TBL;
	$test_version_tbl             = TEST_VERSIONCONTROL_TBL;
	$f_versiondate			  = TEST_VERSIONCONTROL_VERSIONDATE;
	$f_test_id			  = TEST_ID;
	$f_test_name          = TEST_NAME;
	$f_project_id		  = PROJECT_ID;
	$f_test_purpose		  = TEST_PURPOSE;
	$f_priority			  = TEST_PRIORITY;
	$f_comments   		  = TEST_COMMENTS;
	$f_test_type          = TEST_TESTTYPE;
    $f_area_tested        = TEST_AREA_TESTED;
	$f_autopass           = TEST_AUTO_PASS;
	$f_test_deleted       = TEST_DELETED;
    $f_test_archived      = TEST_ARCHIVED;
	$f_ba_owner           = TEST_BA_OWNER;
	$f_qa_owner           = TEST_QA_OWNER;

	$f_tester			  = TEST_TESTER;
	$f_manual_test        = TEST_MANUAL;
	$f_automated_test     = TEST_AUTOMATED;
	$f_performance        = TEST_LR;
	$f_assigned_to		  = TEST_ASSIGNED_TO;
	$f_assigned_by		  = TEST_ASSIGNED_BY;
	$f_date_assigned	  = TEST_DATE_ASSIGNED;
	$f_date_expected	  = TEST_DATE_EXPECTED;
	$f_date_completed	  = TEST_DATE_COMPLETE;
	$f_duration			  = TEST_DURATION;
	$f_test_status		  = TEST_STATUS;

	$f_usecase_pre        = USECASE_PRE_CONDITION;
	$f_usecase_post        = USECASE_POST_CONDITION;
    $q = "SELECT * FROM $test_tbl WHERE $f_test_id = '$test_id'";
    $rs = & db_query( $db, $q);
    if ($row = db_fetch_row( $db, $rs )) {
      $versioncounter=$row[TEST_VERSIONCOUNTER];
//      print_r($row);
      $tmp_keys=array();
      $tmp_values=array();
      $q=") VALUE (";
      foreach($row AS $tmp_key => $tmp_value) {
        $tmp_keys[]= $tmp_key;
        $tmp_values[]=$tmp_value;
      }
      $timestamp=time();
      $q = "INSERT INTO $test_version_tbl ($f_versiondate,".implode(",",$tmp_keys).") VALUES (".$timestamp.",'".implode("','",$tmp_values)."');";
      db_query( $db, $q);
        } else {
           // Invalid, error handler missing.
        }

    $q = "SELECT * FROM ".TEST_STEP_TBL." WHERE $f_test_id = '$test_id'";
    $rs = & db_query( $db, $q);
    while ($row = db_fetch_row( $db, $rs )) {
//      print_r($row);
      $tmp_keys=array();
      $tmp_values=array();
      $q=") VALUE (";
      foreach($row AS $tmp_key => $tmp_value) {
        $tmp_keys[]= $tmp_key;
        $tmp_values[]=$tmp_value;
      }
      $q = "INSERT INTO ".TEST_VERSIONCONTROL_STEP_TBL." (".TEST_VERSIONCOUNTER.",".implode(",",$tmp_keys).") VALUES (".$versioncounter.",'".implode("','",$tmp_values)."');";
      db_query( $db, $q);
   }
   
    $q = "SELECT * FROM ".TEST_REQ_ASSOC_TBL." WHERE $f_test_id = '$test_id'";
    $rs = & db_query( $db, $q);
    while ($row = db_fetch_row( $db, $rs )) {
//      print_r($row);
      $tmp_keys=array();
      $tmp_values=array();
      $q=") VALUE (";
      foreach($row AS $tmp_key => $tmp_value) {
        $tmp_keys[]= $tmp_key;
        $tmp_values[]=$tmp_value;
      }
      $q = "INSERT INTO ".TEST_VERSIONCONTROL_REQ_ASSOC_TBL." (".TEST_VERSIONCOUNTER.",".implode(",",$tmp_keys).") VALUES (".$versioncounter.",'".implode("','",$tmp_values)."');";
      db_query( $db, $q);
   }


    $q = "UPDATE $test_tbl SET
              $f_test_purpose = '$test_purpose',
              $f_comments = '$testcomments',
              $f_test_name = '$test_name',
              $f_area_tested = '$area_tested',
              $f_test_type = '$test_type',
              $f_priority = '$priority',
              $f_ba_owner = '$ba_owner',
              $f_qa_owner = '$qa_owner',
              $f_tester   = '$tester',
              $f_assigned_to = '$assigned_to',
              $f_assigned_by = '$assigned_by',
              $f_date_assigned = '$date_assigned',
              $f_date_expected = '$date_expected',
              $f_date_completed = '$date_completed',
			  $f_duration = '$duration',
              $f_manual_test = '$manual_test',
              $f_automated_test = '$automated_test',
              $f_performance = '$performance',
              $f_autopass = '$autopass',
              $f_usecase_pre = '$test_pre',
              $f_usecase_post = '$test_post',
              ".TEST_VERSIONCOUNTER."=".TEST_VERSIONCOUNTER."+1,
			  $f_test_status = '$test_status'
              WHERE
              $f_test_id = '$test_id'";

	db_query( $db, $q );

	# update last updated by and date
	test_change_last_update( $test_id );


	/*
	# UPDATE VERSION
	$q_ver = "UPDATE $test_version_tbl SET
			 $f_test_status = '$test_status',
			 $f_signoff_date = '$signoff_date',
			 $f_signoff_by = '$signoff_by'
			 WHERE
			 $f_vers_id = '$test_version_id'";


	db_query( $db, $q_ver );
	*/

}


function test_update_field( $project_id, $test_id, $field_name, $value ) {

	global $db;
	$test_tbl		= TEST_TBL;
	$f_project_id	= TEST_PROJ_ID;
	$f_test_id		= TEST_ID;

	$q = "UPDATE $test_tbl
		 SET $field_name = '$value'
		 WHERE $f_project_id = '$project_id'
		 AND $f_test_id IN ( $test_id )";

	/*  I thought of trying to make this query less expensive by removing the
	/*  in clause when it's not needed.  It works the way it is but
	/*  we may want to change it if it becomes too expensive
	if( $in_clause ) {
		$q .= " AND $f_test_id IN ( $test_id )";
	}
	else {
		$q .= " AND $f_test_id = '$test_id'";
	}
	*/

	db_query( $db, $q );

}

function test_update_field_man_auto( $project_id, $test_id, $performed_by) {

	global $db;
	$test_tbl		= TEST_TBL;
	$f_project_id	= TEST_PROJ_ID;
	$f_test_id		= TEST_ID;
	$f_manual		= TEST_MANUAL;
	$f_auto			= TEST_AUTOMATED;

	$q = "UPDATE $test_tbl
		 SET
		 	$f_manual = '$performed_by'
		 WHERE $f_project_id = '$project_id'
		 	AND $f_test_id IN ( $test_id )";

	db_query( $db, $q );
}

# ----------------------------------------------------------------------
# Set the deleted flag for a test
# ----------------------------------------------------------------------
function test_delete($test_id){

	global $db;
    $test_tbl             = TEST_TBL;
    $f_test_id            = TEST_ID;
    $f_test_deleted       = TEST_DELETED;

    $query = "UPDATE $test_tbl
              SET $f_test_deleted = 'Y'
              WHERE $f_test_id  = '$test_id'";
    db_query( $db, $query );
}


# ----------------------------------------------------------------------
# Check if Test name already exists
# INPUT:
#   Test Name to Check
# OUTPUT:
#   True if Test with Test Name already exists, otherwise false.
# ----------------------------------------------------------------------
function test_name_exists( $project_id, $test_name ) {

    global $db;
    $test_tbl			= TEST_TBL;
    $f_test_id			= TEST_ID;
    $f_test_name		= TEST_NAME;
    $f_test_project_id	= TEST_PROJ_ID;

    $query = "	SELECT COUNT($f_test_name)
				FROM $test_tbl
				WHERE $f_test_name='$test_name'
					AND $f_test_project_id=$project_id";

    $result = db_get_one( $db, $query );

    if ( 0 == $result) {
        return false;
    } else {
        return true;
    }
}


# ----------------------------------------------------------------------
# Get the details for a test
# INPUT:
#   test id
# OUTPUT:
#   test details
# ----------------------------------------------------------------------
function test_get_detail( $test_id, $versiontime=0 ) {

    global $db;
    $extra="";


    if ($versiontime==0) $test_tbl             = TEST_TBL;
       else {
         $test_tbl             = TEST_VERSIONCONTROL_TBL;
         $extra.=" AND ".TEST_VERSIONCONTROL_VERSIONDATE."=".$versiontime;
       }

    $f_test_id            = $test_tbl .".". TEST_ID;
    $f_test_name          = $test_tbl .".". TEST_NAME;
    $f_purpose			  = $test_tbl .".". TEST_PURPOSE;
	$f_comments			  = $test_tbl .".". TEST_COMMENTS;
    $f_ba_owner           = $test_tbl .".". TEST_BA_OWNER;
    $f_qa_owner           = $test_tbl .".". TEST_QA_OWNER;
    $f_tester			  = $test_tbl .".". TEST_TESTER;
    $f_test_type          = $test_tbl .".". TEST_TESTTYPE;
    $f_area_tested        = $test_tbl .".". TEST_AREA_TESTED;
	$f_status			  = $test_tbl .".". TEST_STATUS;
    $f_test_priority      = $test_tbl .".". TEST_PRIORITY;
	$f_signoff_by		  = $test_tbl .".". TEST_SIGNOFF_BY;
	$f_signoff_date		  = $test_tbl .".". TEST_SIGNOFF_DATE;
	//$f_creator			  = $test_tbl .".". TEST_AUTHOR;
	$f_date_created		  = $test_tbl .".". TEST_DATE_CREATED;
    $f_steps              = $test_tbl .".". TEST_MANUAL;
    $f_script             = $test_tbl .".". TEST_AUTOMATED;
    $f_autopass           = $test_tbl .".". TEST_AUTO_PASS;
    $f_performance        = $test_tbl .".". TEST_LR;
    $f_assigned_to        = $test_tbl .".". TEST_ASSIGNED_TO;
    $f_assigned_by        = $test_tbl .".". TEST_ASSIGNED_BY;
    $f_date_assigned      = $test_tbl .".". TEST_DATE_ASSIGNED;
    $f_date_expected	  = $test_tbl .".". TEST_DATE_EXPECTED;
    $f_date_completed     = $test_tbl .".". TEST_DATE_COMPLETE;
	$f_duration		      = $test_tbl .".". TEST_DURATION;
	$f_last_updated		  = $test_tbl .".". TEST_LAST_UPDATED;
	$f_last_updated_by	  = $test_tbl .".". TEST_LAST_UPDATED_BY;
	$f_usecase_versioncounter	  = $test_tbl .".". TEST_VERSIONCOUNTER;
	$f_usecase_lockeddate	  = $test_tbl .".". USECASE_LOCKED_DATE;
	$f_usecase_lockedby	  = $test_tbl .".". USECASE_LOCKED_BY;
	$f_usecase_pre        = $test_tbl .".". USECASE_PRE_CONDITION;
	$f_usecase_post        = $test_tbl .".". USECASE_POST_CONDITION;



    $q = "SELECT $f_test_id, $f_usecase_pre, $f_usecase_post, $f_usecase_lockeddate, $f_usecase_lockedby, $f_comments, $f_status, $f_test_name, $f_test_type,
				 $f_area_tested, $f_test_priority, $f_purpose, $f_qa_owner, $f_ba_owner, $f_tester,
				 $f_steps, $f_script, $f_performance, $f_autopass, $f_assigned_to ,
				 $f_assigned_by, $f_date_assigned, $f_date_expected,  $f_date_completed,
				 $f_duration, $f_signoff_by, $f_signoff_date, $f_date_created,
				 $f_last_updated, $f_last_updated_by,$f_usecase_versioncounter
              FROM $test_tbl
              WHERE $f_test_id = '$test_id'".$extra;

	//print"$q<br>";

    $rs = & db_query( $db, $q);
    $row = db_fetch_row( $db, $rs ) ;

    return $row;

}



# ----------------------------------------------------------------------
# Get test name
# INPUT:
#   Test id
# OUTPUT:
#   Corresponding test name
# ----------------------------------------------------------------------
function test_get_name( $testid ) {

    global $db;
    $test_tbl             = TEST_TBL;
    $f_test_id            = TEST_ID;
    $f_test_name          = TEST_NAME;

    $query = "SELECT $f_test_name
              FROM $test_tbl
              WHERE $f_test_id = '$testid'";

     $result = db_get_one( $db, $query );

     return $result;
}


# ----------------------------------------------------------------------
# Get uploaded documents for a test
# INPUT:
#   Test id
# OUTPUT:
#   Array of uploaded document records
# ----------------------------------------------------------------------
function test_get_uploaded_documents($testid) {

    global $db;
    $f_test_doc_tbl       = MAN_TD_TBL;
    $f_test_display_name  = MAN_TD_DISPLAY_NAME;
    $f_test_id            = MAN_TD_TEST_ID;
    $f_man_test_id        = MAN_TD_MANUAL_TEST_ID;

    $query = "SELECT DISTINCT $f_man_test_id, $f_test_display_name
              FROM $f_test_doc_tbl
              WHERE $f_test_id = '$testid'";

    $rs = &db_query( $db, $query );
    $num = db_num_rows( $db, $rs );

    $row = array();

    for ( $i=0 ; $i < $num ; $i++ ) {
        array_push( $row, db_fetch_row( $db, $rs ) );
    }

    return $row;
}


# ----------------------------------------------------------------------
# Get uploaded document details for a manual test
# INPUT:
#   Manual Test id
# OUTPUT:
#   document details
# ----------------------------------------------------------------------
function test_get_uploaded_document_detail($man_test_id) {

    global $db;

	$td_tbl					= MAN_TD_TBL;
	$f_td_test_id			= MAN_TD_TBL .".". MAN_TD_MANUAL_TEST_ID;
	$f_display_name			= MAN_TD_TBL .".". MAN_TD_DISPLAY_NAME;

    $test_docs_ver_tbl		= MAN_TD_VER_TBL;
    $f_man_test_id			= MAN_TD_VER_TBL .".". MAN_TD_VER_MANUAL_TEST_ID;
    $f_filename				= MAN_TD_VER_TBL .".". MAN_TD_VER_FILENAME;
    $f_comments				= MAN_TD_VER_TBL .".". MAN_TEST_DOCS_VERS_COMMENTS;
    $f_time_stamp			= MAN_TD_VER_TBL .".". MAN_TD_VER_TIME_STAMP;
    $f_uploaded_by			= MAN_TD_VER_TBL .".". MAN_TD_VER_UPLOADED_BY;
    $f_version				= MAN_TD_VER_TBL .".". MAN_TD_VER_VERSION;
    $f_doc_type				= MAN_TD_VER_TBL .".". MAN_TEST_DOCS_VERS_MANUAL_DOC_TYPE_NAME;



    $query = "SELECT $f_man_test_id, $f_filename, $f_comments, $f_time_stamp, $f_uploaded_by,
             $f_version, $f_doc_type, $f_display_name
             FROM $test_docs_ver_tbl, $td_tbl
			 WHERE $f_td_test_id = $f_man_test_id
             AND $f_man_test_id = '$man_test_id'
             ORDER BY $f_version DESC
             LIMIT 1";

    $rs = & db_query( $db, $query );
    $row = db_fetch_row( $db, $rs ) ;
    return $row;
}

# ----------------------------------------------------------------------
# Get historyfor a test
# INPUT:
#   Test id
# OUTPUT:
#   Array of version records
# ----------------------------------------------------------------------
function test_get_history($test_id) {

	$tbl_req 					= REQ_TBL;
	$f_req_proj_id				= $tbl_req .".". REQ_PROJECT_ID;
	$f_req_id 					= $tbl_req .".". REQ_ID;
	$f_req_filename 			= $tbl_req .".". REQ_FILENAME;

        $tbl_test_req_assoc			= TEST_REQ_ASSOC_TBL;
        $f_test_req_assoc_id		= $tbl_test_req_assoc .".". TEST_REQ_ASSOC_ID;
        $f_test_req_assoc_req_id	= $tbl_test_req_assoc .".". TEST_REQ_ASSOC_REQ_ID;
        $f_test_req_assoc_test_id	= $tbl_test_req_assoc .".". TEST_REQ_ASSOC_TEMPEST_TEST_ID;
	$f_test_req_assoc_covered	= $tbl_test_req_assoc .".". TEST_REQ_ASSOC_PERCENT_COVERED;

	$tbl_versioncontrol             = TEST_VERSIONCONTROL_TBL;

	$f_version_test_id 		= $tbl_versioncontrol .".". TEST_ID;
	$f_versiondate                  = $tbl_versioncontrol .".". TEST_VERSIONCONTROL_VERSIONDATE;
	$f_test_name                  = $tbl_versioncontrol .".". TEST_NAME;
	$f_test_purpose                  = $tbl_versioncontrol .".". TEST_PURPOSE;
	$f_usecase_pre        = $tbl_versioncontrol .".". USECASE_PRE_CONDITION;
	$f_usecase_post        = $tbl_versioncontrol .".". USECASE_POST_CONDITION;




	$q = "	SELECT	$f_versiondate, $f_test_name, $f_test_purpose, $f_usecase_pre, $f_usecase_post
			FROM $tbl_versioncontrol
			WHERE $test_id = $f_version_test_id ORDER BY $f_versiondate DESC";

	global $db;

	$rs =& db_query($db, $q);
	$rows =& db_fetch_array($db, $rs);
	
//	print_r($rows);

	return $rows;
}

# ----------------------------------------------------------------------
# Get associated requirements for a test
# INPUT:
#   Test id
# OUTPUT:
#   Array of requirement records
# ----------------------------------------------------------------------
function test_get_associated_requirements($test_id,$test_versioncounter=null) {

	$tbl_req 					= REQ_TBL;
	$f_req_proj_id				= $tbl_req .".". REQ_PROJECT_ID;
	$f_req_id 					= $tbl_req .".". REQ_ID;
	$f_req_filename 			= $tbl_req .".". REQ_FILENAME;

         $versioncontrol="";
        if ($test_versioncounter!=null) {
          $versioncontrol=" AND ".TEST_VERSIONCOUNTER."='".$test_versioncounter."'";
           $tbl_test_req_assoc	= TEST_VERSIONCONTROL_REQ_ASSOC_TBL;
        } else  $tbl_test_req_assoc			= TEST_REQ_ASSOC_TBL;
    $f_test_req_assoc_id		= $tbl_test_req_assoc .".". TEST_REQ_ASSOC_ID;
    $f_test_req_assoc_req_id	= $tbl_test_req_assoc .".". TEST_REQ_ASSOC_REQ_ID;
    $f_test_req_assoc_test_id	= $tbl_test_req_assoc .".". TEST_REQ_ASSOC_TEMPEST_TEST_ID;
	$f_test_req_assoc_covered	= $tbl_test_req_assoc .".". TEST_REQ_ASSOC_PERCENT_COVERED;
	$f_test_req_assoc_usecase_versioncounter	= $tbl_test_req_assoc .".". TEST_REQ_ASSOC_USECASE_VERSIONCOUNTER;

	$tbl_test			= TEST_TBL;
	$f_test_name 		= $tbl_test .".". TEST_NAME;
	$f_test_id 			= $tbl_test .".". TEST_ID;

	$q = "	SELECT
				$f_test_req_assoc_id,
				$f_req_id,
				$f_req_filename,
				$f_test_req_assoc_covered,
				$f_test_req_assoc_usecase_versioncounter
			FROM $tbl_test_req_assoc
			INNER JOIN $tbl_req ON $f_test_req_assoc_req_id = $f_req_id
			WHERE $f_test_req_assoc_test_id = $test_id ".$versioncontrol;

	global $db;

	$rs =& db_query($db, $q);
	$rows =& db_fetch_array($db, $rs);

	return $rows;
}

function test_edit_assoc_requirements( $test_id, $session_records_name, $pc_covered_text_input_name) {

	global $db;

	$tbl_req 					= REQ_TBL;
	$f_req_proj_id				= $tbl_req .".". REQ_PROJECT_ID;
	$f_req_id 					= $tbl_req .".". REQ_ID;
	$f_req_filename 			= $tbl_req .".". REQ_FILENAME;

    $tbl_test_req_assoc			= TEST_REQ_ASSOC_TBL;
    $f_test_req_assoc_req_id	= $tbl_test_req_assoc .".". TEST_REQ_ASSOC_REQ_ID;
    $f_test_req_assoc_test_id	= $tbl_test_req_assoc .".". TEST_REQ_ASSOC_TEMPEST_TEST_ID;
	$f_test_req_assoc_covered	= $tbl_test_req_assoc .".". TEST_REQ_ASSOC_PERCENT_COVERED;
	$f_test_req_assoc_usecase_versioncounter	= $tbl_test_req_assoc .".". TEST_REQ_ASSOC_USECASE_VERSIONCOUNTER;

	$tbl_test			= TEST_TBL;
	$f_test_name 		= $tbl_test .".". TEST_NAME;
	$f_test_id 			= $tbl_test .".". TEST_ID;
	$f_test_versioncounter		= $tbl_test .".". TEST_VERSIONCOUNTER;

	$s_project_properties   = session_get_project_properties();
	$project_id				= $s_project_properties['project_id'];

	$req_ids = requirement_get_all_ids($project_id);

	$q = "SELECT $f_test_versioncounter
			FROM $tbl_test
			WHERE $f_test_id = $test_id";
	$rs = db_query($db, $q);
	if (db_num_rows($db, $rs)==0) {
          echo 'Can\'t find this Use Case';
          exit;
        }

	$row =db_fetch_row($db, $rs);
        $usecase_version=$row[TEST_VERSIONCOUNTER];




	foreach($req_ids as $row) {

		$req_id = $row[REQ_ID];

		$q = "	SELECT $f_test_req_assoc_req_id
				FROM $tbl_test_req_assoc
				WHERE $f_test_req_assoc_req_id = $req_id
					AND	$f_test_req_assoc_test_id = $test_id";

		$rs = db_query($db, $q);
		$record_exists = db_num_rows($db, $rs);

		if( session_records_ischecked($session_records_name, $req_id) ) {

			$pc_covered = session_validate_form_get_field($pc_covered_text_input_name.$req_id);
			if( $pc_covered == '' ) {
				$pc_covered = 0;
			}

			if(!$record_exists) {

				# Add new record
				$q = "	INSERT INTO $tbl_test_req_assoc
							($f_test_req_assoc_req_id, $f_test_req_assoc_test_id, $f_test_req_assoc_covered,$f_test_req_assoc_usecase_versioncounter)
						VALUES
							($req_id, $test_id, '$pc_covered',$usecase_version)";
			} else {

				# Update current record
				$q = "	UPDATE $tbl_test_req_assoc
						SET
							$f_test_req_assoc_covered = '$pc_covered',
							$f_test_req_assoc_usecase_versioncounter = $usecase_version
						WHERE
							$f_test_req_assoc_req_id = $req_id
							AND $f_test_req_assoc_test_id = $test_id";
			}
		} else {

			if($record_exists) {
				$q = "	DELETE FROM $tbl_test_req_assoc
						WHERE $f_test_req_assoc_req_id = $req_id
							AND	$f_test_req_assoc_test_id = $test_id";
			}
		}

		db_query($db, $q);
	}
}


# ----------------------------------------------------------------------
# Get associated requirement for a test
# INPUT:
#   Test ID, Requirement ID
# OUTPUT:
#   Percent of covered
# ----------------------------------------------------------------------
function test_get_percent_req_coverage( $test_id, $req_id ) {

    global $db;
    $f_assoc_tbl			= TEST_REQ_ASSOC_TBL;
    $f_assoc_id				= TEST_REQ_ASSOC_ID;
    $f_percent_covered	    = TEST_REQ_ASSOC_PERCENT_COVERED;
    $f_test_id				= TEST_REQ_ASSOC_TEMPEST_TEST_ID;
	$f_req_id				= TEST_REQ_ASSOC_REQ_ID;
	$row					= array();

    $q = "SELECT $f_assoc_id, $f_percent_covered
          FROM $f_assoc_tbl
          WHERE $f_test_id = '$test_id'
		  AND $f_req_id = '$req_id'";

    $rs = &db_query( $db, $q );
    $num = db_num_rows( $db, $rs );

	if( $num > 0 ) {
		$row = db_fetch_row( $db, $rs );
	}
	else {
		//$row[TEST_REQ_ASSOC_PERCENT_COVERED] = "0";
		$row = "";
	}

    return $row;
}

function test_set_percent_req_coverage( $assoc_id, $percent_covered ) {

	global $db;
    $f_assoc_tbl			= TEST_REQ_ASSOC_TBL;
    $f_assoc_id				= TEST_REQ_ASSOC_ID;
    $f_percent_covered	    = TEST_REQ_ASSOC_PERCENT_COVERED;

    $q = "UPDATE $f_assoc_tbl
		 SET $f_percent_covered = '$percent_covered'
		 WHERE $f_assoc_id = '$assoc_id'";
	//print"$q<br>";
    db_query( $db, $q );

}


function test_get_req_assoc_ids( $test_id ) {

	global $db;
    $f_assoc_tbl	= TEST_REQ_ASSOC_TBL;
    $f_req_id		= TEST_REQ_ASSOC_REQ_ID;
    $f_test_id		= TEST_REQ_ASSOC_TEMPEST_TEST_ID;
	$rows			= array();

    $q = "SELECT $f_req_id
          FROM $f_assoc_tbl
          WHERE $f_test_id = '$test_id'";

    $rs =& db_query( $db, $q );

    while( $row=db_fetch_row($db, $rs) ) {

		$rows[$row[TEST_REQ_ASSOC_REQ_ID]] = "";
    }

    return $rows;
}

# ----------------------------------------------------------------------
# Get field value from test table
# Useful for getting qa_owners, ba_owners, etc from tests
# INPUT:
#   $project_id:
#   $field = field in test table you want to query
#   $blank: set equal to true if you want a blank added to the end of return value
# OUTPUT:
#   array containing array containing field value
# ----------------------------------------------------------------------
function test_get_test_value($project_id, $field, $blank=false) {

    global $db;
    $test_tbl    		= TEST_TBL;
    $f_test_id			= TEST_ID;
    $f_project_id		= TEST_PROJ_ID;
    $f_deleted			= TEST_DELETED;
    $f_archive			= TEST_ARCHIVED;
    $arr_value 			= array();

    $q = "SELECT DISTINCT($field)
          FROM $test_tbl
          WHERE $f_project_id = '$project_id'
          AND $f_deleted = 'N'
          AND $f_archive = 'N'
          AND $field != ''
          ORDER BY $field ASC";
    //print"$q<br>";

    $rs = & db_query( $db, $q );
    while($row = db_fetch_row( $db, $rs ) ) {
		array_push($arr_value, $row[$field]);
    }

    if( $blank == true ) {
    	$arr_value[] = "";
    }

    return $arr_value;

}


# ----------------------------------------------------------------------
# Return values for manaul and automated tests
# OUTPUT:
#   array containing array manual / auto values
# ----------------------------------------------------------------------
function test_get_man_auto_values() {

    $arr = array("System", "User");

    return $arr;
}


# ----------------------------------------------------------------------
# Get TestTypeName from test type table
# INPUT:
#   $project_id:
# OUTPUT:
#   array containing array containing field value
# ----------------------------------------------------------------------
function test_get_test_type( $project_id, $blank=false ) {

	global $db;
	$test_type_tbl		  	= TEST_TYPE_TBL;
	$f_test_type_name		= TEST_TYPE_TYPE;
	$f_project_id			= TEST_TYPE_PROJ_ID;
	$test_types				= Array();

	$q = "SELECT DISTINCT $f_test_type_name
		 FROM $test_type_tbl
		 WHERE $f_project_id = '$project_id'
		 ORDER BY $f_test_type_name";

	$rs = & db_query( $db, $q );

	while($row = db_fetch_row( $db, $rs ) ) { ;
		array_push($test_types, $row[$f_test_type_name]);
	}

	if( $blank == true ) {
	   	$test_types[] = "";
	}

    return $test_types;

}

# ----------------------------------------------------------------------
# Get TestTypeName from test type table
# INPUT:
#   $project_id:
# OUTPUT:
#   array containing array containing field value
# ----------------------------------------------------------------------
function test_get_areas_tested( $project_id, $blank=false ) {

	global $db;

	$tbl_test_area		  	= AREA_TESTED_TBL;
	$f_test_area_id			= AREA_TESTED_ID;
	$f_test_area_project_id	= AREA_TESTED_PROJ_ID;
	$f_test_area_name		= AREA_TESTED_NAME;

	$test_areas				= Array();

	$q = "	SELECT DISTINCT $f_test_area_name
			FROM $tbl_test_area
			WHERE $f_test_area_project_id = '$project_id'
			ORDER BY $f_test_area_name";

	$rs =& db_query( $db, $q );

	while($row = db_fetch_row( $db, $rs ) ) {
		array_push($test_areas, $row[$f_test_area_name]);
	}

	if( $blank == true ) {
	   	$test_areas[] = "";
	}

    return $test_areas;
}

# ---------------------------------------------------------------------------------
# Return an array with the values for auto pass ( Enabled, Disabled )
# ---------------------------------------------------------------------------------
function test_get_autopass_values() {

	$auto_pass = array('Enabled', 'Disabled');

	return $auto_pass;
}


# ----------------------------------------------------------------------
# Query for returning test records.
#
# OUTPUT:
#	html_table_offset
#	tests
# ----------------------------------------------------------------------
function test_get(	$page,
					$project_id,
					$per_page,
					$order_by,
					$order_dir,
					$page_number ) {

	global $db;

	$tbl_test		= TEST_TBL;
	$f_name 		= TEST_NAME;
	$f_type 		= TEST_TESTTYPE;
	$f_priority 	= TEST_PRIORITY;
	$f_id 			= TEST_ID;
	$f_steps 		= TEST_MANUAL;
	$f_script 		= TEST_AUTOMATED;
	$f_status 		= TEST_STATUS;
	$f_area 		= TEST_AREA_TESTED;
	$f_deleted 		= TEST_DELETED;
	$f_archive 		= TEST_ARCHIVED;
	$f_area_tested 	= TEST_AREA_TESTED;
	$f_project_id 	= TEST_PROJ_ID;

	$q = "	SELECT $f_id,
			$f_name,
			$f_type,
			$f_priority,
			$f_script,
			$f_steps,
			$f_status,
			$f_area_tested,
			$f_archive
		FROM $tbl_test
		WHERE $f_deleted = 'N'
			AND $f_archive = 'N'
			AND $f_project_id = $project_id
		GROUP BY $f_id
		ORDER BY '$order_by' $order_dir";

	if( $per_page!=0 ) {

		$row_count = db_num_rows( $db, db_query($db, $q) );

		$page_number = util_page_number($page_number, $row_count, $per_page);

		# Add the limit clause to the query so that we only show n number of records per page
		$offset = ( ( $page_number - 1 ) * $per_page );

		html_table_offset( 	$row_count,
							$per_page,
							$page_number,
							$order_by,
							$order_dir );

		$q .= " LIMIT $offset, $per_page";
	}
//print$q;
	return db_fetch_array($db, db_query($db, $q) );
}

# ----------------------------------------------------------------------
# Create and run query for displaying test result records.
# Display table header.
#
# OUTPUT:
#   array of test records to copy
# ----------------------------------------------------------------------
function test_copy_apply_filter(	$project_id,
									$release_id,
									$build_id,
									$testset_id,
									$per_page,
									$order_by,
									$order_dir,
									$page_number,
									$where_clause ) {

    global $db;
    $test_tbl          		= TEST_TBL;
	$f_test_id				= TEST_TBL. "." .TEST_ID;
	$f_project_id			= TEST_TBL. "." .PROJECT_ID;
	$f_test_name            = TEST_TBL. "." .TEST_NAME;
	$f_manual_tests         = TEST_TBL. "." .TEST_MANUAL;
	$f_automated_tests      = TEST_TBL. "." .TEST_AUTOMATED;
	$f_ba_owner             = TEST_TBL. "." .TEST_BA_OWNER;
	$f_qa_owner             = TEST_TBL. "." .TEST_QA_OWNER;
	$f_tester				= TEST_TBL. "." .TEST_TESTER;
	$f_test_load            = TEST_TBL. "." .TEST_LR;
	$f_test_type            = TEST_TBL. "." .TEST_TESTTYPE;
	$f_area_tested          = TEST_TBL. "." .TEST_AREA_TESTED;
	$f_deleted              = TEST_TBL. "." .TEST_DELETED;
	$f_archived             = TEST_TBL. "." .TEST_ARCHIVED;
	$f_test_priority        = TEST_TBL. "." .TEST_PRIORITY;
	$f_auto_pass            = TEST_TBL. "." .TEST_AUTO_PASS;

	$ts_assoc_tbl           = TEST_TS_ASSOC_TBL;
	$f_ts_assoc_id          = TEST_TS_ASSOC_TBL. "." .TEST_TS_ASSOC_ID;
	$f_ts_assoc_ts_id       = TEST_TS_ASSOC_TBL. "." .TEST_TS_ASSOC_TS_ID;
	$f_ts_assoc_test_id     = TEST_TS_ASSOC_TBL. "." .TEST_TS_ASSOC_TEST_ID;
	$f_ts_assoc_test_status = TEST_TS_ASSOC_TBL. "." .TEST_TS_ASSOC_STATUS;
	$f_ts_assoc_assigned_to = TEST_TS_ASSOC_TBL. "." .TEST_TS_ASSOC_ASSIGNED_TO;
	$f_ts_assoc_comments    = TEST_TS_ASSOC_TBL. "." .TEST_TS_ASSOC_COMMENTS;


	$results_tbl			= TEST_RESULTS_TBL;
	$f_results_ts_id		= $results_tbl .".". TEST_RESULTS_TEST_SET_ID;
	$f_status				= $results_tbl .".". TEST_RESULTS_TEST_STATUS;
	$f_assigned_to			= $results_tbl .".". TEST_RESULTS_ASSIGNED_TO;
	$f_comments				= $results_tbl .".". TEST_RESULTS_COMMENTS;

	$limit_clause       	= '';

	$q = "SELECT $f_test_name,
			$f_test_id,
			$f_ts_assoc_id,
			$f_ts_assoc_test_status,
			$f_ts_assoc_comments,
			$f_ts_assoc_assigned_to,
			$f_manual_tests,
			$f_automated_tests,
			$f_qa_owner,
			$f_test_load,
			$f_ba_owner,
			$f_tester,
			$f_test_priority,
			$f_area_tested,
			$f_auto_pass,
			$f_test_type
		FROM $ts_assoc_tbl
		INNER JOIN $test_tbl ON $f_ts_assoc_test_id = $f_test_id
		WHERE $f_project_id = '$project_id'
			AND $f_ts_assoc_ts_id = '$testset_id'
			AND $f_deleted = 'N'
			AND $f_archived = 'N'
			$where_clause
		GROUP BY $f_test_id
		ORDER BY $order_by $order_dir";


	if( $per_page!=0 ) {

		# Add the limit clause to the query so that we only show n number of records per page
		$offset = ( ( $page_number - 1 ) * $per_page );

		$row_count = db_num_rows( $db, db_query($db, $q) );

		$page_number = util_page_number($page_number, $row_count, $per_page);

		html_table_offset(	$row_count,
							$per_page,
							$page_number,
							$order_by,
							$order_dir);

		$q .= " LIMIT $offset, $per_page";
	}

    return db_fetch_array($db, db_query($db, $q));
}

# ----------------------------------------------------------------------
# Get field value from test table that is associated in the  test set assoc table
# Useful for getting qa_owners, ba_owners, etc from tests that are in a test set
# INPUT:
#   $project_id:
#   $testset_id
#   $field = field in test table you want to query
#   $blank: set equal to true if you want a blank added to the end of return value
# OUTPUT:
#   array containing usernames of qa_owners
# ----------------------------------------------------------------------
function test_get_types( $project_id ) {

    global $db;

	$test_tbl		= TEST_TBL;
	$f_test_type	= TEST_TESTTYPE;
    $arr_value		= array();

    $q = "SELECT DISTINCT($f_test_type)
          FROM $test_tbl
          ORDER BY $f_test_type ASC";

    $rs = & db_query( $db, $q );
    while($row = db_fetch_row( $db, $rs ) ) { ;
		$arr_value[] = $row[TEST_TESTTYPE];
    }

    return $arr_value;
}


# ----------------------------------------------------------------------
# Create and run query for displaying test step records.
# INPUT:
#	test_id
#   version control time is optional.  If not passed the live version is taken
# OUTPUT:
#   array of test step records
# ----------------------------------------------------------------------
function test_get_test_steps( $test_id, $page_number, $csv_name,$version_control_time=0,$root=0) {

	global $db;

        $ts_tbl				= TEST_STEP_TBL;
	$ts_tbl				= TEST_STEP_TBL;
	$f_ts_id			= TEST_STEP_ID;
	$f_ts_test_id		= TEST_STEP_TEST_ID;
	$f_ts_step_no		= TEST_STEP_NO;
	$f_ts_action		= TEST_STEP_ACTION;
	$f_ts_test_inputs	= TEST_STEP_TEST_INPUTS;
	$f_ts_expected		= TEST_STEP_EXPECTED;
	$f_info_step		= TEST_STEP_INFO_STEP;
	$f_parent_step		= TEST_STEP_PARENT;

        if ($version_control_time!=0) {
        $ts_tbl	= TEST_VERSIONCONTROL_STEP_TBL;
	$f_versiondate		= TEST_VERSIONCONTROL_STEP_TBL.".".TEST_VERSIONCONTROL_VERSIONDATE;
	$q = "SELECT
		  $ts_tbl.$f_ts_id,
		  $ts_tbl.$f_ts_test_id,
		  $f_ts_step_no,
		  $f_ts_action,
		  $f_ts_test_inputs,
		  $f_ts_expected,
		  $f_info_step

		  FROM $ts_tbl,".TEST_VERSIONCONTROL_TBL."
		  WHERE $ts_tbl.$f_ts_test_id = '$test_id'";
		  $q.=" AND $f_parent_step='$root' ";
		    $q.=" AND ".TEST_VERSIONCONTROL_TBL.".".TEST_VERSIONCONTROL_VERSIONDATE." = '$version_control_time'
		    AND ".TEST_VERSIONCONTROL_TBL.".".TEST_VERSIONCOUNTER."=".TEST_VERSIONCONTROL_STEP_TBL.".".TEST_VERSIONCOUNTER."
		  ORDER BY $f_ts_step_no";

        } else {
	$q = "SELECT
		  $f_ts_id,
		  $f_ts_test_id,
		  $f_ts_step_no,
		  $f_ts_action,
		  $f_ts_test_inputs,
		  $f_ts_expected,
		  $f_info_step

		  FROM $ts_tbl
		  WHERE $f_ts_test_id = '$test_id'";

		  $q.=" AND $f_parent_step=$root ";

		  $q.="ORDER BY $f_ts_step_no";

        }

	if( RECORDS_PER_PAGE_TEST_STEPS != 0 && isset($page_number) ) {

		# Add the limit clause to the query so that we only show n number of records per page
		$offset = ( ( $page_number - 1 ) * RECORDS_PER_PAGE_TEST_STEPS );

		html_table_offset( 	db_num_rows( $db, db_query($db, $q) ),
							RECORDS_PER_PAGE_TEST_STEPS,
							$page_number,
							$order_by=null,
							$order_dir=null,
							$csv_name);

		$q .= " LIMIT $offset, ". RECORDS_PER_PAGE_TEST_STEPS;

	}


	return db_fetch_array( $db, db_query($db, $q) );

}






function test_get_test_step( $test_id,$version_control_time=0,$stepid=0) {

	global $db;

        $ts_tbl				= TEST_STEP_TBL;
	$ts_tbl				= TEST_STEP_TBL;
	$f_ts_id			= TEST_STEP_ID;
	$f_ts_test_id		= TEST_STEP_TEST_ID;
	$f_ts_step_no		= TEST_STEP_NO;
	$f_ts_action		= TEST_STEP_ACTION;
	$f_ts_test_inputs	= TEST_STEP_TEST_INPUTS;
	$f_ts_expected		= TEST_STEP_EXPECTED;
	$f_info_step		= TEST_STEP_INFO_STEP;
	$f_parent_step		= TEST_STEP_PARENT;

        if ($version_control_time!=0) {
        $ts_tbl	= TEST_VERSIONCONTROL_STEP_TBL;
	$f_versiondate		= TEST_VERSIONCONTROL_STEP_TBL.".".TEST_VERSIONCONTROL_VERSIONDATE;
	$q = "SELECT
		  $ts_tbl.$f_ts_id,
		  $ts_tbl.$f_ts_test_id,
		  $f_ts_step_no,
		  $f_ts_action,
		  $f_ts_test_inputs,
		  $f_ts_expected,
		  $f_info_step

		  FROM $ts_tbl,".TEST_VERSIONCONTROL_TBL."
		  WHERE $ts_tbl.$f_ts_test_id = '$test_id'";
		  $q.=" AND $f_ts_id=$stepid ";
		    $q.=" AND ".TEST_VERSIONCONTROL_TBL.".".TEST_VERSIONCONTROL_VERSIONDATE." = '$version_control_time'
		    AND ".TEST_VERSIONCONTROL_TBL.".".TEST_VERSIONCOUNTER."=".TEST_VERSIONCONTROL_STEP_TBL.".".TEST_VERSIONCOUNTER."
		  LIMIT 1";

        } else {
	$q = "SELECT
		  $f_ts_id,
		  $f_ts_test_id,
		  $f_ts_step_no,
		  $f_ts_action,
		  $f_ts_test_inputs,
		  $f_ts_expected,
		  $f_info_step

		  FROM $ts_tbl
		  WHERE $f_ts_test_id = '$test_id'";

		  $q.=" AND $f_ts_id=$stepid ";

		  $q.="LIMIT 1";

        }

	if( RECORDS_PER_PAGE_TEST_STEPS != 0 && isset($page_number) ) {

		# Add the limit clause to the query so that we only show n number of records per page
		$offset = ( ( $page_number - 1 ) * RECORDS_PER_PAGE_TEST_STEPS );

		html_table_offset( 	db_num_rows( $db, db_query($db, $q) ),
							RECORDS_PER_PAGE_TEST_STEPS,
							$page_number,
							$order_by=null,
							$order_dir=null,
							$csv_name);

		$q .= " LIMIT $offset, ". RECORDS_PER_PAGE_TEST_STEPS;

	}


        $array=db_fetch_array( $db, db_query($db, $q) );
        if (isset($array[0])) return $array[0];
//        echo $q;
        return array();

}

# ----------------------------------------------------------------------
# Create and run query for displaying test step records in excel or csv format.
# INPUT:
#	test_id
# OUTPUT:
#   array of test step records
# ----------------------------------------------------------------------
function test_get_test_steps_for_export( $test_id ) {

	global $db;

	$ts_tbl				= TEST_STEP_TBL;
	$f_ts_id			= TEST_STEP_ID;
	$f_ts_test_id		= TEST_STEP_TEST_ID;
	$f_ts_step_no		= TEST_STEP_NO;
	$f_ts_action		= TEST_STEP_ACTION;
	$f_ts_test_inputs	= TEST_STEP_TEST_INPUTS;
	$f_ts_expected		= TEST_STEP_EXPECTED;
	$f_info_step		= TEST_STEP_INFO_STEP;

	$q = "SELECT
		  $f_ts_step_no,
		  $f_ts_action,
		  $f_ts_test_inputs,
		  $f_ts_expected,
		  $f_info_step
		  FROM $ts_tbl
		  WHERE $f_ts_test_id = '$test_id'
		  ORDER BY $f_ts_step_no";

	/*
	if( RECORDS_PER_PAGE_TEST_STEPS != 0 && isset($page_number) ) {

		# Add the limit clause to the query so that we only show n number of records per page
		$offset = ( ( $page_number - 1 ) * RECORDS_PER_PAGE_TEST_STEPS );

		html_table_offset( 	db_num_rows( $db, db_query($db, $q) ),
							RECORDS_PER_PAGE_TEST_STEPS,
							$page_number,
							$order_by=null,
							$order_dir=null,
							$csv_name);

		$q .= " LIMIT $offset, ". RECORDS_PER_PAGE_TEST_STEPS;

	}
	*/


	return db_fetch_array( $db, db_query($db, $q) );

}
/*
function test_get_test_steps( $test_version_id, $page_number=null ) {

	global $db;

	$ts_tbl				= TEST_STEP_TBL;
	$f_ts_id			= TEST_STEP_ID;
	$f_ts_version_id	= TEST_STEP_VERSION_ID;
	$f_ts_step_no		= TEST_STEP_NO;
	$f_ts_action		= TEST_STEP_ACTION;
	$f_ts_expected		= TEST_STEP_EXPECTED;

	$q = "SELECT
		  $f_ts_id,
		  $f_ts_version_id,
		  $f_ts_step_no,
		  $f_ts_action,
		  $f_ts_expected
		  FROM $ts_tbl
		  WHERE $f_ts_version_id = '$test_version_id'
		  ORDER BY $f_ts_step_no";

	if( RECORDS_PER_PAGE_TEST_STEPS != 0 && isset($page_number) ) {

		# Add the limit clause to the query so that we only show n number of records per page
		$offset = ( ( $page_number - 1 ) * RECORDS_PER_PAGE_TEST_STEPS );

		html_table_offset( 	db_num_rows( $db, db_query($db, $q) ),
							RECORDS_PER_PAGE_TEST_STEPS,
							$page_number );

		$q .= " LIMIT $offset, ". RECORDS_PER_PAGE_TEST_STEPS;

	}


	return db_fetch_array( $db, db_query($db, $q) );

}
*/

# ----------------------------------------------------------------------
# Get the step numbers from the Test Step table for a given test
# This function is used to populate a list box with "Add Step After Step (1, 2, 3, etc)
# INPUT:
#	test_id
# OUTPUT:
#   array of step numbers
# ----------------------------------------------------------------------
function test_get_test_step_numbers( $test_id ) {

	global $db;

	$ts_tbl				= TEST_STEP_TBL;
	$f_ts_test_id	= TEST_STEP_TEST_ID;
	$f_ts_step_no		= TEST_STEP_NO;
	$row				= array();

	$q = "SELECT
		  $f_ts_step_no
		  FROM $ts_tbl
		  WHERE $f_ts_test_id = '$test_id'
		  ORDER BY $f_ts_step_no";
	$rs = db_query( $db, $q );
	$num = db_num_rows( $db, $rs );

	for ( $i=0 ; $i < $num ; $i++ ) {
        $row[] = $row[$i];
    }
	//$row = db_fetch_row( $db, $rs );

	return $row;

}
/*
function test_get_test_step_numbers( $test_version_id ) {

	global $db;

	$ts_tbl				= TEST_STEP_TBL;
	$f_ts_version_id	= TEST_STEP_VERSION_ID;
	$f_ts_step_no		= TEST_STEP_NO;
	$row				= array();

	$q = "SELECT
		  $f_ts_step_no
		  FROM $ts_tbl
		  WHERE $f_ts_version_id = '$test_version_id'
		  ORDER BY $f_ts_step_no";
	$rs = db_query( $db, $q );
	$num = db_num_rows( $db, $rs );

	for ( $i=0 ; $i < $num ; $i++ ) {
        $row[] = $row[$i];
    }
	//$row = db_fetch_row( $db, $rs );

	return $row;

}

*/

function usecase_locked($project_id,$usecase_id,$user="") {
  $req=test_get_detail($usecase_id);
    if (empty($req[USECASE_LOCKED_BY]) || $req[USECASE_LOCKED_BY]==$user) {
      return false;
    }
    return true;
}

function usecase_locked_by($project_id,$usecase_id) {
  $req=test_get_detail($usecase_id);
    return $req[USECASE_LOCKED_BY];
}


# ----------------------------------------------------------------------
# Add a test step to a test
# INPUT:
#	test_id
#   location - enter the step after another or to the end of the list
#   step_action - the action taken in the test
#   step_expected - the expected result
#   action - used for logging.  Adding a Test Step to test_id ...
#	page_name - used for logging
# OUTPUT:
#   none
# ----------------------------------------------------------------------
function test_add_test_step( $test_id, $location, $action, $expected, $test_inputs, $info_step, $page_name,$parent_step ) {

	global $db;

	$ts_tbl			= TEST_STEP_TBL;
	$f_parent               = TEST_STEP_PARENT;
	$f_id			= TEST_STEP_ID;
	$f_test_id		= TEST_STEP_TEST_ID;
	$f_step_no		= TEST_STEP_NO;
	$f_action		= TEST_STEP_ACTION;
	$f_expected		= TEST_STEP_EXPECTED;
	$f_test_inputs  = TEST_STEP_TEST_INPUTS;
	$f_info_step	= TEST_STEP_INFO_STEP;

	$max_step_no = test_step_get_max_step_number( $test_id );

	if( $location == 'end' ) {
		$step_no = $max_step_no + 1;
	}
	elseif( $location > 0 ) {
		$step_no = $location + 0.1;
	}

	$q = "INSERT INTO $ts_tbl
		($f_parent,$f_test_id, $f_action, $f_expected, $f_test_inputs, $f_info_step)
		VALUES ('$parent_step','$test_id',  '$action', '$expected', '$test_inputs', '$info_step')";
	db_query( $db, $q );

	################################
	# Add entry into the log table for the project

	$deletion = 'N';
	$creation = 'Y';
	$upload = 'N';
	$log_action = "ADDED TEST STEP TO TEST ID $test_id";

	log_activity_log( $page_name, $deletion, $creation, $upload, $log_action );
	test_calculate_step_numbers($test_id);
	#################################

}
/*
function test_add_test_step( $test_version_id, $location, $action, $expected, $page_name ) {

	global $db;

	$ts_tbl			= TEST_STEP_TBL;
	$f_id			= TEST_STEP_ID;
	$f_version_id	= TEST_STEP_VERSION_ID;
	$f_step_no		= TEST_STEP_NO;
	$f_action		= TEST_STEP_ACTION;
	$f_expected		= TEST_STEP_EXPECTED;

	$max_step_no = test_step_get_max_step_number( $test_version_id );

	if( $location == 'end' ) {
		$step_no = $max_step_no + 1;
	}
	elseif( $location > 0 ) {
		$step_no = $location + 0.1;
	}

	$q = "INSERT INTO $ts_tbl
		($f_version_id, $f_step_no, $f_action, $f_expected)
		VALUES ('$test_version_id', '$step_no', '$action', '$expected')";
	db_query( $db, $q );


	################################
	# Add entry into the log table for the project

	$deletion = 'N';
	$creation = 'Y';
	$upload = 'N';
	$log_action = "ADDED TEST STEP TO TEST ID $test_version_id";

	log_activity_log( $page_name, $deletion, $creation, $upload, $log_action );
	#################################

}
*/
# ----------------------------------------------------------------------
# Returns the max step number for a group of test steps.
# Used when a user wants to add a test step after all the others.
# INPUT:
#	test_id
# OUTPUT:
#   the max test step number
# ----------------------------------------------------------------------
function test_step_get_max_step_number( $test_id ) {

	global $db;

	$test_step_tbl	= TEST_STEP_TBL;
	$f_test_id		= TEST_STEP_TEST_ID;
	$f_step_no		= TEST_STEP_NO;

	$q = "SELECT MAX($f_step_no)
		  FROM $test_step_tbl
		  WHERE $f_test_id = '$test_id'";
	$row = db_get_one( $db, $q );

	return $row;

}

/*
function test_step_get_max_step_number( $test_version_id ) {

	global $db;

	$test_step_tbl	= TEST_STEP_TBL;
	$f_version_id	= TEST_STEP_VERSION_ID;
	$f_step_no		= TEST_STEP_NO;

	$q = "SELECT MAX($f_step_no)
		  FROM $test_step_tbl
		  WHERE $f_version_id = '$test_version_id'";
	$row = db_get_one( $db, $q );

	return $row;

}
*/

# ----------------------------------------------------------------------
# Renumber test steps to 1, 2, 3... and remove 1, 1.1, 1.2, etc.
# INPUT:
#	test_id
# OUTPUT:
#   array of test step records
# ----------------------------------------------------------------------
function test_renumber_test_steps( $test_id ) {

	global $db;

	$test_step_tbl	= TEST_STEP_TBL;
	$f_step_id		= TEST_STEP_ID;
	$f_test_id	= TEST_STEP_TEST_ID;
	$f_step_no		= TEST_STEP_NO;
	$i				= 1;

	$q = "SELECT $f_step_id
		  FROM $test_step_tbl
		  WHERE $f_test_id = '$test_id'
		  ORDER BY $f_step_no ASC";
	$rs = db_query( $db, $q );

	while($row = db_fetch_row($db, $rs) ) {

		$step_id = $row[TEST_STEP_ID];

		$q_update = "UPDATE $test_step_tbl
					SET $f_step_no = '$i'
					WHERE $f_step_id = '$step_id'
					AND $f_test_id = '$test_id'";
		#print"$q_update;<br>";
		db_query( $db, $q_update );

		$i = $i + 1;
	}

}
/*
function test_renumber_test_steps( $test_version_id ) {

	global $db;

	$test_step_tbl	= TEST_STEP_TBL;
	$f_step_id		= TEST_STEP_ID;
	$f_version_id	= TEST_STEP_VERSION_ID;
	$f_step_no		= TEST_STEP_NO;
	$i				= 1;

	$q = "SELECT $f_step_id
		  FROM $test_step_tbl
		  WHERE $f_version_id = '$test_version_id'
		  ORDER BY $f_step_no ASC";
	$rs = db_query( $db, $q );

	while($row = db_fetch_row($db, $rs) ) {

		$step_id = $row[TEST_STEP_ID];

		$q_update = "UPDATE $test_step_tbl
					SET $f_step_no = '$i'
					WHERE $f_step_id = '$step_id'
					AND $f_version_id = '$test_version_id'";
		#print"$q_update;<br>";
		db_query( $db, $q_update );

		$i = $i + 1;
	}

}
*/

# ----------------------------------------------------------------------
# Return the detail of a specific test step
# INPUT:
#	test_step_id
# OUTPUT:
#   array containing the test step information
# ----------------------------------------------------------------------
function test_get_test_step_detail( $test_step_id ) {

	global $db;

	$ts_tbl			= TEST_STEP_TBL;
	$f_id			= TEST_STEP_ID;
	$f_version_id	= TEST_STEP_VERSION_ID;
	$f_step_no		= TEST_STEP_NO;
	$f_action		= TEST_STEP_ACTION;
	$f_inputs		= TEST_STEP_TEST_INPUTS;
	$f_expected		= TEST_STEP_EXPECTED;
	$f_info_step	= TEST_STEP_INFO_STEP;
	$f_parent_step	= TEST_STEP_PARENT;

	$q = "SELECT
		  $f_id,
		  $f_version_id,
		  $f_step_no,
		  $f_action,
		  $f_inputs,
		  $f_expected,
		  $f_info_step,
		  $f_parent_step

		  FROM $ts_tbl
		  WHERE $f_id = '$test_step_id'";
	$rs = & db_query( $db, $q );
    $row = db_fetch_row( $db, $rs ) ;

	return $row;

}


function test_get_all_versions( $test_id ) {

	global $db;

	$version_tbl		  = TEST_VERS_TBL;
	$f_test_id			  = TEST_VERS_TEST_ID;
	$f_version_id		  = TEST_VERS_ID;
	$f_version_no		  = TEST_VERS_NUMBER;
	$f_latest			  = TEST_VERS_LATEST;
	$f_active_version	  = TEST_VERS_ACTIVE;
	$f_test_comments      = TEST_VERS_COMMENTS;
	$f_test_status        = TEST_VERS_STATUS;
	$f_assigned_to		  = TEST_VERS_ASSIGNED_TO;
	$f_version_author	  = TEST_VERS_AUTHOR;
	$f_date_created		  = TEST_VERS_DATE_CREATED;
	$f_signoff_by		  = TEST_VERS_SIGNOFF_BY;
	$f_signoff_date		  = TEST_VERS_SIGNOFF_DATE;

	$q = "SELECT
		  $f_test_id,
		  $f_version_id,
		  $f_version_no,
		  $f_latest,
		  $f_active_version,
		  $f_test_comments,
		  $f_test_status,
		  $f_assigned_to,
		  $f_version_author,
		  $f_date_created,
		  $f_signoff_by,
		  $f_signoff_date
		  FROM $version_tbl
		  WHERE $f_test_id = '$test_id'";
	$rs = & db_query( $db, $q );
	$num = db_num_rows( $db, $rs );

    $row = array();

    for ( $i=0 ; $i < $num ; $i++ ) {
        array_push( $row, db_fetch_row( $db, $rs ) );
    }

    return $row;
    //$row = db_fetch_array( $db, $rs ) ;

	//return $row;

}

function test_clean_broken_test_steps() {
        global  $db;
//        db_query($db,"DELETE teststep AS r FROM teststep AS r, (SELECT DISTINCT t.TestStepID, t.TestID FROM `teststep` AS t LEFT JOIN teststep AS s ON (t.TestStepParent = s.TestStepID ) AND (t.TestID = s.TestID) WHERE s.TestStepID IS NULL AND NOT (t.TestStepParent = 0)) AS subselect WHERE (r.TestStepID, r.TestID) = (subselect.TestStepID, subselect.TestID)");
}

# ----------------------------------------------------------------------
# Update the detail of a specific test step
# INPUT:
#	test_id = used to get the max step number for all the steps associated with a test
#   test_step_id = the step in the test_step table to update
#   step_action = field to update (action taken durning test)
#   step_expeced = field to update (expected result)
#	action = used for logging
#   $page = used for logging
# OUTPUT:
#   none
# ----------------------------------------------------------------------
function test_update_test_step( $test_version_id, $test_step_id, $step_no, $step_action, $step_inputs,
								$step_expected=null, $info_step,$parent_step) {

	global $db;
	$test_id=test_id_for_step($test_step_id);
	$ts_tbl			= TEST_STEP_TBL;
	$f_id			= TEST_STEP_ID;
	$f_step_no		= TEST_STEP_NO;
	$f_action		= TEST_STEP_ACTION;
	$f_inputs		= TEST_STEP_TEST_INPUTS;
	$f_expected		= TEST_STEP_EXPECTED;
	$f_info_step	= TEST_STEP_INFO_STEP;
	$f_parent_step	= TEST_STEP_PARENT;

	$update_clause	= "";

	if( $step_no != 'none' ) {

		if( $step_no == 'end' ) {

			$max_step_no = test_step_get_max_step_number( $test_version_id );
			$step_no = $max_step_no + 1;
		}
		else {
			$step_no = $step_no + 0.1;
		}

		$update_clause = ", ". $f_step_no ."='". $step_no ."'";
	}


	$q = "UPDATE $ts_tbl
		  SET $f_action = '$step_action',
		  	  $f_inputs = '$step_inputs',
			  $f_expected = '$step_expected',
			  $f_info_step = '$info_step',
                          $f_parent_step= '$parent_step'";


	$where_clause = " WHERE $f_id = '$test_step_id'";

	$q = $q . $update_clause . $where_clause;
	db_query( $db, $q );
        test_clean_broken_test_steps();
	test_calculate_step_numbers($test_id);
}

function test_calculate_step_numbers($test_id,$test_step_id=null,$path=null) {
  global $db;
  if ($path==null) {
    $path=array(0);
    $test_step_id=0;
    $q = "UPDATE ".TEST_STEP_TBL."
         SET ".TEST_STEP_NO." = '0.0' WHERE ".TEST_ID."=".$test_id."";
   db_query( $db, $q );
  } else {
    $submit_path=$path;
//    if (count($submit_path)>0 && $submit_path[count($submit_path)-1]==0) unset($submit_path[count($submit_path)-1]);
//  echo "test_calculate_step_numbers($test_id,$test_step_id,";print_r($path); echo ") {<br/>";
   $q = "UPDATE ".TEST_STEP_TBL."
         SET ".TEST_STEP_NO." = '".implode('.',$submit_path)."' WHERE ".TEST_STEP_ID."=".$test_step_id."";
   db_query( $db, $q );
  }




  $row_test_steps=test_get_test_steps( $test_id, null, $csv_name=null,$version_control_time=0,$test_step_id);

  if (count($row_test_steps)==0) {
    // We have no substeps, so its a leaf -> done here!
  } else {
    // Its no leaf -> walk through every branch
    $pos=-1;
    foreach($row_test_steps as $tmp_row ) {
      $pos++;

      $sub_path=$path;
      $sub_path[count($sub_path)-1]=$sub_path[count($sub_path)-1]+1;
      if ($pos==0) {
//        echo "ISFIRST:".$test_step_id."|";
        // If its the first, increase the last number.
      } else {
//        echo "ISOTHER:".$test_step_id."|";
        // If its not the first, append the number to the path
        $sub_path[]=$pos;
        $sub_path[]=0;
      }
      test_calculate_step_numbers($test_id,$tmp_row[TEST_STEP_ID],$sub_path);
    }
  }

/*
  global $db;
  if ($path==null) {
    $path=array(0);
    $test_step_id=0;
  }

  $row_test_steps=test_get_test_steps( $test_id, null, $csv_name=null,$version_control_time=0,$test_step_id);

  if (count($path)>0) {
    $path3=$path;
//    if ($path3[count($path3)-1]==0) unset($path3[count($path3)-1]);
     echo "<br/><b>".$test_step_id."</b> "; print_r($path3);
     $q = "UPDATE ".TEST_STEP_TBL."
  	  SET ".TEST_STEP_NO." = '".implode('.',$path3)."' WHERE ".TEST_STEP_ID."=".$test_step_id."";
    db_query( $db, $q );
  }
//  print_r($row_test_steps);
  $path2=$path;
  $path2[count($path2)-1]=$path2[count($path2)-1]+1;
  if (count($row_test_steps)==0) {

  } else {
    $pos=-1;
    if (count($row_test_steps)==1) { foreach($row_test_steps as $tmp_row ) {
        test_calculate_step_numbers($test_id,$tmp_row[TEST_STEP_ID],$path2);
      } } else
    foreach($row_test_steps as $tmp_row ) {
        $pos++;
        $path4=$path2;
        $path4[]=$pos;
        test_calculate_step_numbers($test_id,$tmp_row[TEST_STEP_ID],$path4);

    }
  }
  return true;
   */
}

function test_id_for_step($test_step_id) {
    global $db;
       $q="SELECT ".TEST_ID." FROM ".TEST_STEP_TBL." WHERE ".TEST_STEP_ID."=".$test_step_id."";
       $rs=db_query( $db, $q );;

       if (db_num_rows( $db, $rs )>0) {
         $row=db_fetch_row( $db, $rs );
         return $row[TEST_ID];
       }
       return -1;
}
function test_delete_test_step( $test_step_id ) {

	global $db;
	$test_id=test_id_for_step($test_step_id);

	$test_step_tbl	= TEST_STEP_TBL;
	$f_test_step_id	= TEST_STEP_ID;

	$q = "DELETE
		  FROM $test_step_tbl
		  WHERE $f_test_step_id = '$test_step_id'";
	db_query( $db, $q );
        test_clean_broken_test_steps();
	test_calculate_step_numbers($test_id);

}

# -----------------------------------------------------------
# Set the Latest Flag = 'N' where the Latest Flag = 'Y'
# This is used when creating a new test version
function test_reset_latest( $test_id ) {

	global $db;
	$test_version_tbl	= TEST_VERS_TBL;
	$f_test_id			= TEST_VERS_TEST_ID;
	$f_latest			= TEST_VERS_LATEST;


	$q = "UPDATE
		 $test_version_tbl
		 SET $f_latest = 'N'
		 WHERE $f_latest = 'Y'
		 AND $f_test_id = '$test_id'";
	//print"$q<br>";
	db_query( $db, $q );

}

# --------------------------------------------------------------------------
# Update the TestVersion table making and change the Active Version
# The Active Version is the version of the test that will appear
# when somebody runs a test.  It only applies if they try to run a manual
# test that has test steps.
# INPUT:
#	TestID: The TestID.
#   TestVersionID: The version that will become active
# --------------------------------------------------------------------------
function test_make_active_version( $test_id, $test_version_id ) {

	global $db;

	$version_tbl		= TEST_VERS_TBL;
	$f_test_id			= TEST_VERS_TEST_ID;
	$f_version_id		= TEST_VERS_ID;
	$f_active			= TEST_VERS_ACTIVE;


	$q = "UPDATE $version_tbl SET $f_active = 'N' WHERE $f_test_id = '$test_id'";
	db_query( $db, $q );

	$q2 = "UPDATE $version_tbl SET $f_active = 'Y' WHERE $f_version_id = '$test_version_id'";
	db_query( $db, $q2 );
}

# THIS INS'T FINISHED.  WE CAN:
# REMOVE ASSIGNED_TO FROM THE VERSION TABLE
# ADD INPUTS TO THE VERSION TABLE
# ADD INFO STEP TO THE VERSION TABLE
function test_add_new_version( $test_id, $test_version_id, $comments, $status, $assigned_to ) {

	global $db;

	$version_tbl		  = TEST_VERS_TBL;
	$f_test_id			  = TEST_VERS_TEST_ID;
	$f_version_id		  = TEST_VERS_ID;
	$f_version_no		  = TEST_VERS_NUMBER;
	$f_latest			  = TEST_VERS_LATEST;
	$f_active_version	  = TEST_VERS_ACTIVE;
	$f_test_comments      = TEST_VERS_COMMENTS;
	$f_test_status        = TEST_VERS_STATUS;
	$f_assigned_to		  = TEST_VERS_ASSIGNED_TO;
	$f_version_author	  = TEST_VERS_AUTHOR;
	$f_date_created		  = TEST_VERS_DATE_CREATED;

	$ts_tbl				= TEST_STEP_TBL;
	$f_ts_id			= TEST_STEP_ID;
	$f_ts_version_id	= TEST_STEP_VERSION_ID;
	$f_step_no			= TEST_STEP_NO;
	$f_action			= TEST_STEP_ACTION;
	$f_expected			= TEST_STEP_EXPECTED;



	# SELECT THE MAX VERSION AND ADD ONE
	$q_version = "SELECT $f_version_no
				 FROM $version_tbl
				 WHERE $f_test_id = '$test_id'
				 AND $f_latest = 'Y'";
	#print"$q_version<br>";
	$current_version = db_get_one( $db, $q_version );
	#print"current_version = $current_version<br>";
	$new_version = util_increment_version( $current_version );
	#print"new_version = $new_version<br>";

	# SET THE LATEST FLAG TO 'N' FOR THE LAST VERSION THAT EQUALED 'Y'
	test_reset_latest( $test_id );


	# INSERT INTO VERSION TABLE
	$created_date = date_get_short_dt();
	$author = session_get_username();

	$q = "INSERT INTO $version_tbl
		  ( $f_test_id, $f_version_no, $f_latest, $f_active_version, $f_test_comments,
			$f_test_status, $f_assigned_to, $f_version_author, $f_date_created )
		  VALUES
		  ( '$test_id', '$new_version', 'Y', 'N', '$comments', '$status', '$assigned_to',
			'$author', '$created_date')";
	db_query( $db, $q );

	$new_version_id = test_get_latest_version( $test_id );

	# COPY TEST STEPS INTO NEW VERSION
	$q_steps = "SELECT $f_step_no, $f_action, $f_expected
				FROM $ts_tbl
				WHERE $f_ts_version_id = $test_version_id";
	$rs_steps = db_query( $db, $q_steps );
	$num_steps = db_num_rows( $db, $rs_steps );

	if( $num_steps > 0 ) {

		 while( $row_steps = db_fetch_row($db, $rs_steps) ) {

			$q_insert = "INSERT INTO $ts_tbl
						($f_ts_version_id, $f_step_no, $f_action, $f_expected)
						 VALUES
						('$new_version_id', '$row_steps[$f_step_no]', '$row_steps[$f_action]',
						 '$row_steps[$f_expected]')";
			//print"$q_insert<br>";
			db_query( $db, $q_insert );
		 }
	}

}

function test_get_latest_version( $test_id ) {

	global $db;
	$version_tbl		  = TEST_VERS_TBL;
	$f_test_id			  = TEST_VERS_TEST_ID;
	$f_version_id		  = TEST_VERS_ID;

	$q = "SELECT MAX($f_version_id) FROM $version_tbl WHERE $f_test_id = '$test_id'";
	$version_id = db_get_one( $db, $q );

	return $version_id;
}

function test_get_last_run($test_id, $testset_id) {

	global $db;
	$test_results_tbl		= TEST_RESULTS_TBL;
	$f_results_os			= TEST_RESULTS_OS;
	$f_time_started			= TEST_RESULTS_TIME_STARTED;
	$f_testset_id			= TEST_RESULTS_TEST_SET_ID;
	$f_test_id				= TEST_RESULTS_TEMPEST_TEST_ID;

	$q = "	SELECT
				$f_results_os,
				$f_time_started
			FROM $test_results_tbl
			WHERE
				$f_test_id = $test_id
				AND $f_testset_id = $testset_id
			ORDER BY $f_time_started DESC LIMIT 1";

	$row =& db_fetch_row($db, db_query($db, $q));

	return $row;
}

function test_get_last_modified($project_id) {

    global $db;

	$tbl_test             = TEST_TBL;
	$f_test_id			  = TEST_ID;
	$f_test_name          = TEST_NAME;
	$f_project_id		  = PROJECT_ID;
	$f_purpose			  = TEST_PURPOSE;
	$f_priority			  = TEST_PRIORITY;
	$f_test_type          = TEST_TESTTYPE;
	$f_test_status          = TEST_STATUS;
    $f_area_tested        = TEST_AREA_TESTED;
	$f_autopass           = TEST_AUTO_PASS;
	$f_test_deleted       = TEST_DELETED;
    $f_test_archived      = TEST_ARCHIVED;
	$f_ba_owner           = TEST_BA_OWNER;
	$f_qa_owner           = TEST_QA_OWNER;
	$f_manual_test        = TEST_MANUAL;
	$f_automated_tests    = TEST_AUTOMATED;
	$f_performance        = TEST_LR;
	$f_assigned_to		  = TEST_ASSIGNED_TO;
	$f_assigned_by		  = TEST_ASSIGNED_BY;
	$f_date_assigned	  = TEST_DATE_ASSIGNED;
	$f_date_expected	  = TEST_DATE_EXPECTED;
	$f_date_completed	  = TEST_DATE_COMPLETE;
	$f_duration			  = TEST_DURATION;
	$f_last_updated		  = TEST_LAST_UPDATED;
	$f_last_updated_by	  = TEST_LAST_UPDATED_BY;

	$f_last_updated		  = TEST_LAST_UPDATED;
	$f_last_updated_by	  = TEST_LAST_UPDATED_BY;
	$f_usecase_pre        = USECASE_PRE_CONDITION;
	$f_usecase_post        = USECASE_POST_CONDITION;

	$last_updated		  = date_get_short_dt();
	$s_user_properties 	  = session_get_user_properties();
	$s_user_name		  = $s_user_properties['username'];
	$last_updated_by	  = $s_user_name;

	$q = "	SELECT
				$f_usecase_pre,
				$f_test_status,
				$f_usecase_post,
                                $f_test_id,
				$f_test_name,
				$f_test_type,
				$f_priority,
				$f_automated_tests,
				$f_manual_test,
				$f_area_tested,
				$f_test_archived,
				$f_last_updated,
				$f_last_updated_by
		FROM $tbl_test
		WHERE $f_test_deleted = 'N'
			AND $f_test_archived = 'N'
			AND $f_project_id = $project_id
		GROUP BY $f_test_id
		ORDER BY $f_last_updated DESC
		LIMIT 5";

	$rs = db_query($db, $q);

	return db_fetch_array($db, $rs);

}

# --------------------------------------------------------------------------
# Update the LastUpdatedBy and LastUpdatedDate fields
# Putting this in a function because different people may not want to
# update these fields for every action (test_update, test_step_update, file_upload, etc)
# INPUT:
#	TestID: The TestID.
# --------------------------------------------------------------------------
function test_change_last_update( $test_id ) {

	global $db;

	$test_tbl             = TEST_TBL;
	$f_test_id			  = TEST_ID;
	$f_last_updated		  = TEST_LAST_UPDATED;
	$f_last_updated_by	  = TEST_LAST_UPDATED_BY;

	$last_updated_date	  = date_get_short_dt();
	$s_user_properties 	  = session_get_user_properties();
	$s_user_name		  = $s_user_properties['username'];
	$last_updated_by	  = $s_user_name;

	$q = "UPDATE $test_tbl
		  SET
		  $f_last_updated = '$last_updated_date',
		  $f_last_updated_by = '$last_updated_by'
		  WHERE $f_test_id = '$test_id'";

	db_query( $db, $q );

}

function test_requirement_get_pc_covered($test_id, $req_id) {

	global $db;

    $tbl_test_req_assoc			= TEST_REQ_ASSOC_TBL;
    $f_test_req_assoc_id		= $tbl_test_req_assoc .".". TEST_REQ_ASSOC_ID;
    $f_test_req_assoc_req_id	= $tbl_test_req_assoc .".". TEST_REQ_ASSOC_REQ_ID;
    $f_test_req_assoc_test_id	= $tbl_test_req_assoc .".". TEST_REQ_ASSOC_TEMPEST_TEST_ID;
	$f_test_req_assoc_covered	= $tbl_test_req_assoc .".". TEST_REQ_ASSOC_PERCENT_COVERED;

	$q = "	SELECT
				$f_test_req_assoc_covered
			FROM $tbl_test_req_assoc
			WHERE
				$f_test_req_assoc_req_id = $req_id
				AND $f_test_req_assoc_test_id = $test_id";

	$pc_covered = db_get_one($db, $q);

	return $pc_covered;
}

function test_import_csv($test_id, $name) {

	$ts_tbl				= TEST_STEP_TBL;
	$f_ts_id			= TEST_STEP_ID;
	$f_ts_test_id		= TEST_STEP_TEST_ID;
	$f_ts_step_no		= TEST_STEP_NO;
	$f_ts_action		= TEST_STEP_ACTION;
	$f_ts_input			= TEST_STEP_TEST_INPUTS;
	$f_ts_expected		= TEST_STEP_EXPECTED;
	$f_info_step		= TEST_STEP_INFO_STEP;

	global $db;

	$s_project_properties   = session_get_project_properties();
	$project_name           = $s_project_properties['project_name'];
	$redirect_on_error		= "test_step_import_csv_page.php?test_id=$test_id";

	$uploaded_file = $_FILES[$name];

	if( $uploaded_file['size'] != '0' && is_uploaded_file($uploaded_file['tmp_name']) ) {

		# SCRIPT TO UPDATE RELEASES IN THE SYSTEM
		$debug = false;
		$show_tbl = false;
		$row = 1;

		# READ IN A LINE FROM DATA FILE
		$fhandle 	= fopen($uploaded_file['tmp_name'], "r");
		fgetcsv($fhandle, filesize($uploaded_file['tmp_name']), ",");

		if( !$debug ) {

			$q = "DELETE FROM $ts_tbl WHERE $f_ts_test_id = '$test_id'";
			db_query($db, $q);
		}


		while( $data = fgetcsv($fhandle, filesize($uploaded_file['tmp_name']), ",") ) {
			//print_r($data);exit;
			$num = count($data);
			$step_no = $row;

			$tc_no = '';
			$desc = '';
			$state = '';
			$inputs = '';
			$expected = '';
			$info = '';
			$action = '';
			$jursidiction = '';
			$test_input = '';

			# Get record from CSV file
			$tc_no 		= @util_html_special_chars_string($data[0]);
			$action		= @util_html_special_chars_string($data[1]);
			$test_input	= @util_html_special_chars_string($data[2]);
			$expected 	= @util_html_special_chars_string($data[3]);
			$info 		= @util_html_special_chars_string($data[4]);
		

			# Test if it is just blank data
			$str = $action.$test_input.$expected.$info;

			if( empty($str) ) {

				break;
			}

			$newline = "<br>";

			# Replace single quotes so the SQL works
			$test_input = str_replace( "|", ",", $test_input );
			$action = str_replace( "|", ",", $action );
			$expected = str_replace( "|", ",", $expected );

			$q = "	INSERT INTO $ts_tbl
						( $f_ts_test_id, $f_ts_step_no, $f_ts_action, $f_ts_input, $f_ts_expected, $f_info_step )
					VALUES
						( '$test_id', '$step_no', '$action', '$test_input', '$expected', '$info' )";

			if( $debug ) {
				print"$q;<hr>";
			}
			else {
				//print"$q;<hr>";
				db_query($db, $q);
			}


			$row++;
		}

		fclose ($fhandle);

		# Create a version of the file in Test Doc Version table if the upload was successful
		if( file_name_exists( $test_id, $file_name ) ) {  

			# Get the manual_test_id from the Manual Test Doc table and add a new file version
			$manual_test_id = test_get_manual_test_id( $test_id );
			file_add_supporting_test_doc_version( $file_temp_name, $file_name, $test_id, $manual_test_id, $comments="", $file_type="" );
		}
		else {
			# Add a new file
			file_add_supporting_test_doc( $file_temp_name, $file_name, $test_id, $comments="", $file_type="" );
		}

	}
	else { # The user tried to upload and empty file
		error_report_show( $redirect_on_error, NO_FILE_SPECIFIED );
	}

}

function test_import_excel( $test_id, $name ) {

	global $db;

	$ts_tbl				= TEST_STEP_TBL;
	$f_ts_id			= TEST_STEP_ID;
	$f_ts_test_id		= TEST_STEP_TEST_ID;
	$f_ts_step_no		= TEST_STEP_NO;
	$f_ts_action		= TEST_STEP_ACTION;
	$f_ts_input			= TEST_STEP_TEST_INPUTS;
	$f_ts_expected		= TEST_STEP_EXPECTED;
	$f_info_step		= TEST_STEP_INFO_STEP;

	$s_project_properties   = session_get_project_properties();
	$project_name           = $s_project_properties['project_name'];
	$redirect_on_error		= "test_step_import_csv_page.php?test_id=$test_id";

	$uploaded_file		= $_FILES[$name];
	$file_name			= $_FILES[$name]['name'];
	$file_temp_name		= $_FILES[$name]['tmp_name'];
	$file_size			= $_FILES[$name]['size'];

	/*
	print"file_name = $file_name<br>";
	print"file_temp_name = $uploaded_file[tmp_name]<br>";
	print"file_size = $uploaded_file[size]<br>";
	*/

	if( $file_size != '0' && is_uploaded_file($file_temp_name) ) {


		require_once './Excel/reader.php';
		
		// ExcelFile($filename, $encoding);
		$data = new Spreadsheet_Excel_Reader();


		// Set output Encoding.
		$data->setOutputEncoding('CP1251');

		/***
		* if you want you can change 'iconv' to mb_convert_encoding:
		* $data->setUTFEncoder('mb');
		*
		**/

		/***
		* By default rows & cols indeces start with 1
		* For change initial index use:
		* $data->setRowColOffset(0);
		*
		**/

		/***
		*  Some function for formatting output.
		* $data->setDefaultFormat('%.2f');
		* setDefaultFormat - set format for columns with unknown formatting
		*
		* $data->setColumnFormat(4, '%.3f');
		* setColumnFormat - set format for column (apply only to number fields)
		*
		**/
		$data->read( $uploaded_file['tmp_name'] );

		//error_reporting(E_ALL ^ E_NOTICE);
		$debug = false;

		if( !$debug ) {

			$q = "DELETE FROM $ts_tbl WHERE $f_ts_test_id = '$test_id'";
			db_query($db, $q);
		}

		for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {

			$step_no	= '';
			$action		= '';
			$test_input = '';
			$expected	= '';
			$info		= '';
			
			//for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {

			//echo "\"".$data->sheets[0]['cells'][$i][1]."\",";

			/*
			print"i = $i<br>";
				print"j = $j<br>";
			*/

			# Get record from CSV file
			$step_no	= @util_html_special_chars_string($data->sheets[0]['cells'][$i][1]);
			$action		= @util_html_special_chars_string($data->sheets[0]['cells'][$i][2]);
			$test_input	= @util_html_special_chars_string($data->sheets[0]['cells'][$i][3]);
			$expected 	= @util_html_special_chars_string($data->sheets[0]['cells'][$i][4]);
			$info 		= @util_html_special_chars_string($data->sheets[0]['cells'][$i][5]);
			

			# We don't need to insert the column headers
			if( $i > 1 ) {

				# Insert into the Test Set table
				$q = "	INSERT INTO $ts_tbl
						( $f_ts_test_id, $f_ts_step_no, $f_ts_action, $f_ts_input, $f_ts_expected, $f_info_step )
					VALUES
						( '$test_id', '$step_no', '$action', '$test_input', '$expected', '$info' )";

				if( $debug ) {
					print"$q;<br>";
				}
				else {
					db_query($db, $q);
				}

			}

		}

		

		# Create a version of the file in Test Doc Version table if the upload was successful
		if( file_name_exists( $test_id, $file_name ) ) {  

			# Get the manual_test_id from the Manual Test Doc table and add a new file version
			$manual_test_id = test_get_manual_test_id( $test_id );
			file_add_supporting_test_doc_version( $file_temp_name, $file_name, $test_id, $manual_test_id, $comments="", $file_type="" );
		}
		else {
			# Add a new file
			file_add_supporting_test_doc( $file_temp_name, $file_name, $test_id, $comments="", $file_type="" );
		}

	}
	else { # The user tried to upload and empty file
		error_report_show( $redirect_on_error, NO_FILE_SPECIFIED );
	}

}

# ----------------------------------------------------------------------
# Get the max file version id for an uploaded test
# INPUT:
#   Manual Test id
# OUTPUT:
#   doc version
# ----------------------------------------------------------------------
function test_get_max_file_version( $manual_test_id ) {

	global $db;
    $version_tbl		  = MAN_TD_VER_TBL;
    $f_man_test_id        = MAN_TD_VER_MANUAL_TEST_ID;
    $f_version            = MAN_TD_VER_VERSION;
    

    $q = "SELECT MAX($f_version)
	      FROM $version_tbl
		  WHERE $f_man_test_id = '$manual_test_id'";
    $version = db_get_one( $db, $q );

    return $version;

}

# --------------------------------------------------------------------------
# Return the history related to a specific test id
# INPUT:
#	Manual Test ID
# --------------------------------------------------------------------------
function test_get_document_detail( $manual_test_id ) 
{

	global $db;

	$td_tbl				= MAN_TD_TBL;
	$f_man_test_id		= MAN_TD_TBL .".". MAN_TD_MANUAL_TEST_ID;
	$f_display_name		= MAN_TD_TBL .".". MAN_TD_DISPLAY_NAME;
	
	$td_vers_tbl		= MAN_TD_VER_TBL;
	$f_vers_test_id		= MAN_TD_VER_TBL .".". MAN_TD_VER_MANUAL_TEST_ID;
	$f_filename			= MAN_TD_VER_TBL .".". MAN_TD_VER_FILENAME;
	$f_comments			= MAN_TD_VER_TBL .".". MAN_TEST_DOCS_VERS_COMMENTS;
	$f_time_stamp		= MAN_TD_VER_TBL .".". MAN_TD_VER_TIME_STAMP;
	$f_uploaded_by		= MAN_TD_VER_TBL .".". MAN_TD_VER_UPLOADED_BY;
	$f_version			= MAN_TD_VER_TBL .".". MAN_TD_VER_VERSION;
	$f_doc_type			= MAN_TD_VER_TBL .".". MAN_TEST_DOCS_VERS_MANUAL_DOC_TYPE_NAME;

	$q = "SELECT
			$f_man_test_id, $f_display_name, $f_filename, $f_comments, $f_time_stamp,
			$f_uploaded_by, $f_version, $f_doc_type
		 FROM
			$td_tbl, $td_vers_tbl
		 WHERE
			$f_man_test_id = $f_vers_test_id
		 AND
			$f_man_test_id = '$manual_test_id'";
	
	$rs = db_query( $db, $q );
	$row = db_fetch_array( $db, $rs );

	return $row;

		  

}

# ----------------------------------------------------------------------
# Get manual_test_id from the ManualTestDoc table given a test_id
# INPUT:
#   test_id
# OUTPUT:
#   manual_test_id
# ----------------------------------------------------------------------
function test_get_manual_test_id( $test_id ) {

    global $db;

	$td_tbl					= MAN_TD_TBL;
	$f_manual_test_id		= MAN_TD_MANUAL_TEST_ID;
	$f_test_id				= MAN_TD_TEST_ID;
	$f_display_name			= MAN_TD_DISPLAY_NAME;

	$q = "SELECT 
			$f_manual_test_id
		  FROM
			$td_tbl
		  WHERE 
			$f_test_id = '$test_id'";

	$manual_test_id = db_get_one( $db, $q );

	return $manual_test_id;
}

# ----------------------------------------------------------------------
# Get all screen names associated to a project
# INPUT:
#   project_id
#   order_by  - order the results by this field
#   order_dir - the order in which the results will return ASC or DESC
# OUTPUT:
#   an array of all the screens in the screens_tbl
# ----------------------------------------------------------------------
function test_get_screens( $project_id, $order_by, $order_dir, $page_number=null ) {

	global $db;
	$screen_tbl		= SCREEN_TBL;
	$f_screen_id	= SCREEN_ID;
	$f_project_id	= SCREEN_PROJ_ID;
	$f_screen_name	= SCREEN_NAME;
	$f_screen_desc	= SCREEN_DESC;
	$f_screen_order = SCREEN_ORDER;


	$q = "SELECT
			$f_screen_id, $f_screen_name, $f_screen_desc, $f_screen_order
		 FROM
			$screen_tbl
		 WHERE 
			$f_project_id = '$project_id'
		 ORDER BY
			 $order_by $order_dir";

	if( !is_null($page_number) ) {
		if( RECORDS_PER_PAGE_25 != 0 ) {

			# Add the limit clause to the query so that we only show n number of records per page
			$offset = ( ( $page_number - 1 ) *  RECORDS_PER_PAGE_25 );

			html_table_offset( 	db_num_rows( $db, db_query($db, $q) ),
								RECORDS_PER_PAGE_25,
								$page_number );

			$q .= " LIMIT $offset, ". RECORDS_PER_PAGE_25;

		}
	}
	
	//print"$q<br>";
	$rs = db_query( $db, $q );

	return db_fetch_array( $db, $rs );

}


# ----------------------------------------------------------------------
# Get a specific screen name given the screen_id
# INPUT:
#   screen_id  - order the results by this field
# OUTPUT:
#   an array of the details for a speceific screen
# ----------------------------------------------------------------------
function test_get_screen( $screen_id ) {

	global $db;
	$screen_tbl		= SCREEN_TBL;
	$f_screen_id	= SCREEN_ID;
	$f_project_id	= SCREEN_PROJ_ID;
	$f_screen_name	= SCREEN_NAME;
	$f_screen_desc	= SCREEN_DESC;
	$f_screen_order = SCREEN_ORDER;

	$q = "SELECT
			$f_screen_id, $f_screen_name, $f_screen_desc, $f_screen_order
		 FROM
			$screen_tbl
		 WHERE
			$f_screen_id = '$screen_id'";

	//print"$q<br>";
	$rs = db_query( $db, $q );

	return db_fetch_row( $db, $rs );

}





# ----------------------------------------------------------------------
# Find out if a screen name already exists
# INPUT:
#   project_id
#   screen_name
# OUTPUT:
#   true if the screen name already exists in the system
#   false if the screen name doesn't exist
# ----------------------------------------------------------------------
function test_screen_name_exists( $project_id, $screen_name ) {

	global $db;
	$screen_tbl		= SCREEN_TBL;
	$f_project_id	= SCREEN_PROJ_ID;
	$f_screen_name	= SCREEN_NAME;

	$q = "SELECT
			$f_screen_name
		 FROM
			$screen_tbl
		 WHERE 
			$f_project_id = '$project_id'
		 AND 
			$f_screen_name = '$screen_name'";

	$rs = db_query( $db, $q );
	$num = db_num_rows( $db, $rs );
	
	return $num;

}

# ----------------------------------------------------------------------
# Insert a record into the screen table
# INPUT:
#   project_id
#   screen_name
#   description
# ----------------------------------------------------------------------
function test_add_screen( $project_id, $screen_name, $description, $screen_order ) {

	global $db;
	$screen_tbl		= SCREEN_TBL;
	$f_screen_id	= SCREEN_ID;
	$f_project_id	= SCREEN_PROJ_ID;
	$f_screen_name	= SCREEN_NAME;
	$f_screen_desc	= SCREEN_DESC;
	$f_screen_order = SCREEN_ORDER;

	$max_screen_no = test_get_max_screen_number();

	if( $screen_order == 'end' ) {
		$order_no = $max_screen_no + 1;
	}
	elseif( $screen_order > 0 ) {
		$order_no = $screen_order + 0.1;
	}

	$q = "INSERT INTO $screen_tbl
		  ( $f_project_id, $f_screen_name, $f_screen_desc, $f_screen_order )
		  VALUES( '$project_id', '$screen_name', '$description', '$order_no')";
	
	//print"$q<br>";
	db_query( $db, $q );

}

# ----------------------------------------------------------------------
# Update a record into the screen table
# INPUT:
#   screen_id
#   screen_name
#   description
# ----------------------------------------------------------------------
function test_update_screen( $screen_id, $screen_name, $description, $screen_order ) {

	global $db;
	$screen_tbl		= SCREEN_TBL;
	$f_screen_id	= SCREEN_ID;
	$f_screen_name	= SCREEN_NAME;
	$f_screen_desc	= SCREEN_DESC;
	$f_screen_order = SCREEN_ORDER;

	# Find out the current screen order number
	$current_screen_order = test_get_screen_value( $screen_id, $f_screen_order);

	# Need to update the screen_number if it's changed.  Else leave it the same
	if( $screen_order != $current_screen_order ) {

		# The user has changed the screen number.  Find the max number
		$max_screen_no = test_get_max_screen_number();

		if( $screen_order == 'end' ) {
			$order_no = $max_screen_no + 1;
		}
		elseif( $screen_order > 0 ) {
			$order_no = $screen_order + 0.1;
		}
	}
	else {
		$order_no = $current_screen_order;
	}

	$q = "UPDATE $screen_tbl
		  SET 
			  $f_screen_name = '$screen_name',
			  $f_screen_desc = '$description',
			  $f_screen_order = '$order_no'
		  WHERE
			  $f_screen_id = '$screen_id'";
	
	db_query( $db, $q );

}

# ----------------------------------------------------------------------
# Returns the all the screen order numbers.
# If $order_num is passed in, it will not be included in the list
# of numbers.
# OPTIONAL INPUT:
#	order_num
# OUTPUT:
#   an array of the order number as the appear in the db and the 
#	associated text as it will appear in the list box
# ----------------------------------------------------------------------
function test_get_screen_order_numbers( $order_num=null ) {

	global $db;
	$screen_tbl			= SCREEN_TBL;
	$f_screen_order		= SCREEN_ORDER;
	$order_numbers		= array();
	$i = 0;

	$q = "SELECT $f_screen_order FROM $screen_tbl ORDER BY $f_screen_order";
	$rs = db_query( $db, $q );

	while($row = db_fetch_row( $db, $rs ) ) {
		
		# Don't include order_num in the list if it's passed in
		if( !is_null($order_num) && $row[$f_screen_order] == $order_num ) {
			continue;
		}
		else {

			$order_numbers[$row[$f_screen_order]] = "After ". $row[$f_screen_order];	
		}
	}

	return $order_numbers;
		
}

# ----------------------------------------------------------------------
# Returns the all the screen order numbers.
# If $order_num is passed in, it will not be included in the list
# of numbers.
# OPTIONAL INPUT:
#	order_num
# OUTPUT:
#   an array of the order number as the appear in the db and the 
#	associated text as it will appear in the list box
# ----------------------------------------------------------------------
function test_get_field_order_numbers( $order_num=null ) {

	global $db;
	$field_tbl			= FIELD_TBL;
	$f_field_order		= FIELD_ORDER;
	$order_numbers		= array();
	$i = 0;

	$q = "SELECT $f_field_order FROM $field_tbl ORDER BY $f_field_order";
	$rs = db_query( $db, $q );

	while($row = db_fetch_row( $db, $rs ) ) {
		
		# Don't include order_num in the list if it's passed in
		if( !is_null($order_num) && $row[$f_screen_order] == $order_num ) {
			continue;
		}
		else {

			$order_numbers[$row[$f_field_order]] = "After ". $row[$f_field_order];
		}
	}
	
	return $order_numbers;
		
}

# ----------------------------------------------------------------------
# Returns the max screen number for a group of screen.
# Used when a user wants to add a test step after all the others.
# INPUT:
#	test_id
# OUTPUT:
#   the max test step number
# ----------------------------------------------------------------------
function test_get_max_screen_number() {

	global $db;
	$screen_tbl		= SCREEN_TBL;
	$f_screen_id	= SCREEN_ID;
	$f_screen_order = SCREEN_ORDER;

	$q = "SELECT MAX($f_screen_order)
		  FROM $screen_tbl";
	$row = db_get_one( $db, $q );

	return $row;

}


function test_get_screen_value( $screen_id, $field_name ) {

	global $db;
	$screen_tbl		= SCREEN_TBL;
	$f_screen_id	= SCREEN_ID;

	$q = "SELECT $field_name
	      FROM $screen_tbl
		  WHERE $f_screen_id = '$screen_id'";
	$row = db_get_one( $db, $q );

	return $row;

}

# ----------------------------------------------------------------------
# Delete a screen from the screen table
# INPUT:
#   screen_id
# ----------------------------------------------------------------------
function test_delete_screen( $screen_id ) {

	print"NO CODE YET.  MUST DELETE ALL ASSOCIATED FIELDS TOO";
}



# ----------------------------------------------------------------------
# Find out if a field name already exists
# INPUT:
#   field_name
#   screen_id
# OUTPUT:
#   true if the field name already exists on the screen
#   false if the field name doesn't exist
# ----------------------------------------------------------------------
function test_field_name_exists( $field_name, $screen_id ) {

	global $db;
	$field_tbl		= FIELD_TBL;
	$f_field_name	= FIELD_NAME;
	$f_screen_id	= FIELD_SCREEN_ID;

	$q = "SELECT
			$f_field_name
		 FROM
			$field_tbl
		 WHERE 
			$f_field_name = '$field_name'
		 AND 
			$f_screen_id = '$screen_id'";

	$rs = db_query( $db, $q );
	$num = db_num_rows( $db, $rs );
	
	return $num;

}

# ----------------------------------------------------------------------
# Insert a record into the field_tbl
#
# ----------------------------------------------------------------------
function test_add_field( $field_name, $screen_id, $description, $field_order, $text_box ) {

	global $db;
	$field_tbl		= FIELD_TBL;
	$f_field_name	= FIELD_NAME;
	$f_screen_id	= FIELD_SCREEN_ID;
	$f_description	= FIELD_DESC;
	$f_field_order	= FIELD_ORDER;
	$f_text_only	= FIELD_TEXT_ONLY;

	$max_field_no = test_get_max_field_number( $screen_id );

	if( $field_order == 'end' ) {
		$order_no = $max_field_no + 1;
	}
	elseif( $field_order > 0 ) {
		$order_no = $field_order + 0.1;
	}

	# Set the value for text box to Y or N
	if( $text_box == 'on' ) {
		$text_box = 'Y';
	}
	else {
		$text_box = 'N';
	}

	$q = "INSERT INTO $field_tbl
		  ($f_field_name, $f_screen_id, $f_description, $f_field_order, $f_text_only)
		  VALUES('$field_name', '$screen_id', '$description', '$order_no', '$text_box')";

	//print"$q<br>";
	db_query( $db, $q );

}

# ----------------------------------------------------------------------
# Returns the max field number for all fields on a screen.
# Used when a user wants to add a field after all the others.
# INPUT:
#	screen_id
# OUTPUT:
#   the max test step number
# ----------------------------------------------------------------------
function test_get_max_field_number( $screen_id ) {

	global $db;
	$field_tbl		= FIELD_TBL;
	$f_screen_id	= FIELD_SCREEN_ID;
	$f_field_order	= FIELD_ORDER;

	$q = "SELECT MAX($f_field_order)
		  FROM $field_tbl
		  WHERE $f_screen_id = '$screen_id'";

	$row = db_get_one( $db, $q );

	return $row;

}

# ----------------------------------------------------------------------
# NO LONGER USED
# ----------------------------------------------------------------------
/*
function test_get_fields( $project_id, $order_by, $order_dir ) {

	global $db;
	$field_tbl		= FIELD_TBL;
	$f_field_id		= FIELD_TBL .".". FIELD_ID;
	$f_field_name	= FIELD_TBL .".". FIELD_NAME;
	$f_fscreen_id	= FIELD_TBL .".". FIELD_SCREEN_ID;
	$f_description	= FIELD_TBL .".". FIELD_DESC;
	$f_field_order	= FIELD_TBL .".". FIELD_ORDER;
	$f_text_only	= FIELD_TBL .".". FIELD_TEXT_ONLY;

	$screen_tbl		= SCREEN_TBL;
	$f_screen_id	= SCREEN_TBL .".". SCREEN_ID;
	$f_screen_name	= SCREEN_TBL .".". SCREEN_NAME;

	$q = "SELECT 
			$f_field_id, $f_field_name, $f_description, $f_field_order, $f_text_only, $f_screen_id, $f_screen_id, $f_screen_name
		  FROM 
			$field_tbl, $screen_tbl
		  WHERE
			$f_fscreen_id = $f_screen_id
		  ORDER BY 
			$order_by $order_dir";

	$rs = db_query( $db, $q );
	
	return db_fetch_array( $db, $rs );


}
*/

# ----------------------------------------------------------------------
# Create where clause for tests and run query to extract test data
# OUTPUT:
#   array of test records.
# ----------------------------------------------------------------------
function test_filter_fields( $filter_screen,
							 $filter_search,
							 $order_by,
							 $order_dir,
							 $page_number,
							 $per_page,
							 $csv_name ) {


	$where_clause = test_field_filter_generate_where_clause( $filter_screen, $filter_search );
    $row =& test_field_apply_filter( $where_clause, $per_page, $order_by, $order_dir, $page_number, $csv_name);

    return $row;

}

# ----------------------------------------------------------------------
# Create where clause for fields
# OUTPUT:
#   the WHERE clause of the sql statement
# ----------------------------------------------------------------------
function test_field_filter_generate_where_clause( $filter_screen, $filter_search ) {

	$filter_tbl			= FIELD_TBL;
	$f_screen_id		= FIELD_TBL .".". FIELD_SCREEN_ID;
	$f_field_name		= FIELD_TBL .".". FIELD_NAME;


    $where_clause = '';

    # FIELD_NAME
    if ( !empty($filter_screen)  && $filter_screen != 'all') {

        $where_clause = $where_clause." AND $f_screen_id = '$filter_screen'";
    }
	# SEARCH
	if ( !empty($filter_search) ) {

		$where_clause = $where_clause." AND  ($f_field_name  LIKE '%$filter_search%')";
    }

    return $where_clause;
}
# ----------------------------------------------------------------------
# Show a nice tree for all substeps of one step
# OUTPUT:
#   HTML-Table
# ----------------------------------------------------------------------
function test_print_steptree($test_id,$way,$root=0,$version_control_time=0,$new_way="",$can_edit=false) {
  global $redirect_url;
  $delete_page				= 'delete_page.php';
  $row_test_step_add_action_page  = 'test_step_add_action.php';
  $row_test_step_edit_page		= 'test_step_edit_page.php';
  $alternatives="";
  $count=0;
  $cur_step=-1;
  if (true) { //$root==0) {
    $rows_test_steps = test_get_test_steps( $test_id, null, $csv_name=null,$version_control_time,$root);
    foreach($rows_test_steps as $cur_row_test_step ) {
//      $cur_step=$cur_row_test_step[TEST_STEP_ID];
      break;
    }
  }

  $possible_steps = test_get_test_steps( $test_id, null, $csv_name=null,$version_control_time,$root);

  $thetop=array_top($way);
  foreach($possible_steps as $step ) {
    if ($thetop==$step[TEST_STEP_ID] || count($way)==0) {
      $count++;
      if ((count($way)!=0)&&($thetop==$step[TEST_STEP_ID])) {
        $way=RemoveFirstFromArray($way);
      }
      $cur_step=$step[TEST_STEP_ID];
      break;
    }
  }



/*  if (true) {
                if ((count($way)==0)&&($root!=0)) {
                  $row_test_step=test_get_test_step( $test_id, $version_control_time,$root);
                  print_r($row_test_step);
                  $cur_step=$row_test_step[TEST_STEP_ID];
                } else {
		$rows_test_steps = test_get_test_steps( $test_id, null, $csv_name=null,$version_control_time,$root);
			foreach($rows_test_steps as $cur_row_test_step ) {
                        }
                }
*/



     // This is NOT our first step.
//   echo $cur_step;
   $row_test_step=test_get_test_step( $test_id, $version_control_time,$cur_step);

   if (empty($row_test_step[TEST_STEP_ID])) return 0;

				$row_test_step_id	= $row_test_step[TEST_STEP_ID];
				$step_number     	= $row_test_step[TEST_STEP_NO];
				$step_action     	= $row_test_step[TEST_STEP_ACTION];
				$step_test_inputs  	= $row_test_step[TEST_STEP_TEST_INPUTS];
				$step_expected   	= $row_test_step[TEST_STEP_EXPECTED];
				$info_step 			= $row_test_step[TEST_STEP_INFO_STEP];
				$row_style = html_tbl_alternate_bgcolor( '' );
				$info_step_class = "";
				$step_actor=lang_get('by_user');
				if( $info_step=="Y" ) {
        				$step_actor=lang_get('by_system');
					$info_step_class = "class='test-step-info'";
				}

				

			$alternatives="";
			$tmp_rows=test_get_test_steps( $test_id, null, $csv_name=null,$version_control_time,$root);
			if (count($tmp_rows)>0)
			foreach($tmp_rows as $tmp_row ) if ($tmp_row[TEST_STEP_ID]!=$row_test_step_id) {
                          if ($alternatives!="") $alternatives.="/";
                          $alternatives.="<a href='test_detail_page.php?test_id=".$test_id."&amp;way=".$new_way."-".$tmp_row[TEST_STEP_ID]."'>";
			  $alternatives.=substr(strip_tags($tmp_row[TEST_STEP_EXPECTED]), 0, 25);
                          if (strlen(strip_tags($tmp_row[TEST_STEP_EXPECTED]))>25) $alternatives.='...';
                          $alternatives.="</a>";
                        }


				print"<tr class='$row_style' style='border-top:1px dashed;'>\n";
				print"<td class='tbl-l' align=left><div $info_step_class>$step_number</div></td>\n";
				print"<td  class='tbl-l' align=center><div $info_step_class>$step_actor</div></td>\n";
				print"<td class='tbl-l' align=left><div $info_step_class>";
                                print " $step_action";
                                print"</div></td>\n";
				print"<td class='tbl-l' align=left><div $info_step_class>$step_expected";
                                if ($alternatives!="") print "(".$alternatives.")";
                                print"</div></td>\n";
				if ($version_control_time==0 && $can_edit) {
				print"<td class='tbl-c' style='white-space:nowrap'>";
					print"<a href='$row_test_step_edit_page?test_id=$test_id&amp;test_step_id=$row_test_step_id'>[". lang_get('edit_link') ."]</a>&nbsp\n";
					print"<form name='delete_test_step' method=post action='$delete_page'>\n";
					print"<input type='submit' name='delete' value='[". lang_get( 'delete' ) ."]' class='page-numbers'>";
					print"<input type='hidden' name='r_page' value='$redirect_url'>\n";
					print"<input type='hidden' name='f' value='delete_test_step'>\n";
					print"<input type='hidden' name='id' value='$row_test_step_id'>\n";
					print"<input type='hidden' name='msg' value='100'>\n";
					print"</form>\n";
				print"</td>\n";
				}
				print"</tr>\n";
				

    return 1+test_print_steptree($test_id,$way,$row_test_step_id,$version_control_time,$new_way."-".$row_test_step_id);

/*		$rows_test_steps = test_get_test_steps( $test_id, null, $csv_name=null,$version_control_time,$root);
		# Find out if there are existing test steps
		#$row = test_get_test_steps( $test_id );
		$num_test_steps = sizeof($rows_test_steps);
//		print_r($way);
$alternatives="";
                if ((count($way)==0)&&($root==0)) $way=array("-1");
                $count=0;
		if ($num_test_steps==0) return 0;



			foreach($rows_test_steps as $row_test_step ) {
                          $thetop=array_top($way);
                          if ($thetop==$row_test_step[TEST_STEP_ID] || ($root==0) || ($root!=0 && count($way)==0)) {
                                $count++;
                                if ((count($way)!=0)&&($thetop==$row_test_step[TEST_STEP_ID])) {
                                  $way=RemoveFirstFromArray($way);
                                }


			$tmp_rows=test_get_test_steps( $test_id, null, $csv_name=null,$version_control_time,$row_test_step_id);;
			$alternatives="";
			if (count($tmp_rows)>0)
			foreach($tmp_rows as $tmp_row ) {
                          if ($alternatives!="") $alternatives.="/";
//                          print_r($tmp_row);
                          $alternatives.="<a href='test_detail_page.php?test_id=".$test_id."&amp;way=".$new_way."-".$tmp_row[TEST_STEP_ID]."'>".strip_tags($tmp_row['Action'])."</a>";
                        }


			$row_style = '';
				$row_style = html_tbl_alternate_bgcolor( $row_style );






  				$subcount=test_print_steptree($test_id,$way,$row_test_step_id,$version_control_time,$new_way."-".$row_test_step_id);
				echo $subcount.">";
                                if ($subcount>0) return ($count +$subcount);


                           }
			}
*/
}


function test_get_flows($test_id,$path=null,$version_control_time=0) {
  $total_paths=array();

  if ($path==null) $curstep=0; else $curstep=$path[count($path)-1][TEST_STEP_ID];


  $row_test_steps=test_get_test_steps( $test_id, null, $csv_name=null,$version_control_time,$curstep);
  if (count($row_test_steps)==0) {
    $total_paths=array_merge(array($path),$total_paths);
  } else
    foreach($row_test_steps as $tmp_row ) {
//        $total_paths=array_merge(test_get_flows($test_id,array_merge($path,array($tmp_row[TEST_STEP_ID])),$version_control_time),$total_paths);
        $path2=$path;
        $path2[]=array(
                  TEST_STEP_ID => $tmp_row[TEST_STEP_ID],
                  TEST_STEP_ACTION => $tmp_row[TEST_STEP_ACTION],
                  TEST_STEP_EXPECTED => $tmp_row[TEST_STEP_EXPECTED],
                  TEST_STEP_NO => $tmp_row[TEST_STEP_NO],
                  TEST_STEP_INFO_STEP => $tmp_row[TEST_STEP_INFO_STEP]);
        $total_paths=array_merge($total_paths,test_get_flows($test_id,$path2,$version_control_time));
//        print_r(array_merge($path,array($tmp_row[TEST_STEP_ID])));
//        echo "<hr>";
        /*
 	$row_test_step_id	= $row_test_step[TEST_STEP_ID];
	$step_number     	= $row_test_step[TEST_STEP_NO];
	$step_action     	= $row_test_step[TEST_STEP_ACTION];
	$step_test_inputs  	= $row_test_step[TEST_STEP_TEST_INPUTS];
	$step_expected   	= $row_test_step[TEST_STEP_EXPECTED];
	$info_step 			= $row_test_step[TEST_STEP_INFO_STEP];
	$row_style = html_tbl_alternate_bgcolor( '' );
	$info_step_class = "";
	$step_actor=lang_get('by_user');
	if( $info_step=="Y" ) {
		$step_actor=lang_get('by_system');
		$info_step_class = "class='test-step-info'";
	} */
    }
//  print_r($total_paths);
  return $total_paths;
}

//print_r(test_get_flows(6,null,0));

# ----------------------------------------------------------------------
# Run the query to get the fields
# OUTPUT:
#   array of field records.
# ----------------------------------------------------------------------
function test_field_apply_filter( $where_clause, $per_page, $order_by, $order_dir, $page_number, $csv_name) {

	global $db;
	$field_tbl		= FIELD_TBL;
	$f_field_id		= FIELD_TBL .".". FIELD_ID;
	$f_field_name	= FIELD_TBL .".". FIELD_NAME;
	$f_fscreen_id	= FIELD_TBL .".". FIELD_SCREEN_ID;
	$f_description	= FIELD_TBL .".". FIELD_DESC;
	$f_field_order	= FIELD_TBL .".". FIELD_ORDER;
	$f_text_only	= FIELD_TBL .".". FIELD_TEXT_ONLY;

	$screen_tbl		= SCREEN_TBL;
	$f_screen_id	= SCREEN_TBL .".". SCREEN_ID;
	$f_screen_name	= SCREEN_TBL .".". SCREEN_NAME;

	$q = "SELECT 
			$f_field_id, $f_field_name, $f_description, $f_field_order, $f_text_only, $f_screen_id, $f_screen_id, $f_screen_name
		  FROM 
			$field_tbl, $screen_tbl
		  WHERE
			$f_fscreen_id = $f_screen_id";

    $order_clause 	= " ORDER BY $order_by $order_dir";
	$where_clause 	= $where_clause." GROUP BY $f_field_name";
    $q 				.= $where_clause.$order_clause;

	if( $per_page!=0 && $page_number!=0 ) {

		$row_count = db_num_rows( $db, db_query($db, $q) );

		$page_number = util_page_number($page_number, $row_count, $per_page);

		# Add the limit clause to the query so that we only show n number of records per page
		$offset = ( ( $page_number - 1 ) * $per_page );
		html_table_offset( 	$row_count,
							$per_page,
							$page_number,
							$order_by,
							$order_dir,
							$csv_name );

		$q .= " LIMIT $offset, ".$per_page;

	}

	//print"$q<br>";
	$rs =& db_query($db, $q);
	return db_fetch_array($db, $rs);
}

?>