<?php

include( "menu.inc" );
include( "func.inc" );
include( "view.inc" );
include( "mnum.inc" );

#### reads metatext by proj_id and version ################################
function get_metatext( $pid, $vid, &$ti, &$ta, &$dt, &$st, &$lg, &$rb, &$rbs, &$ml, &$lk, &$kw, &$mn, &$ds ){
	if( !$id = mysql_query( "select * from metatext
		where proj_id = $pid and vers_id = $vid" )) {
		echo "Error No. 100: ".mysql_errno().": ".mysql_error();
		exit;
	}
	if( !$row = mysql_fetch_array( $id )) {
		echo "Error No. 101: no rows selected";
		exit;
	}
	$ti = $row[ "title" ];
	$ta = $row[ "titlabb" ];
	$dt = dt_de( $row[ "submit" ]);
	$st = $row[ "status" ];
	$lg = $row[ "lang" ];
	$ml = $row[ "maillst" ];
	$rb = $row[ "ref_by" ];
	$lk = $row[ "link" ];
	$kw = $row[ "keywds" ];
	$mn = $row[ "membnum" ];
	$ds = $row[ "descr" ];
	$st = st_de( $st );
	if( $rb > 0 ){
		if( !$id = mysql_query( "select title, titlabb from metatext
			where status = 'act' and proj_id = $rb" )) {
			echo "Error No. 102: ".mysql_errno().": ".mysql_error();
			exit;
		}
		if( !$row = mysql_fetch_array( $id )) {
			echo "Error No. 103: no rows selected";
			exit;
		}
		$rbs = "Projekt ".$row[ "titlabb" ].": ".$row[ "title" ];
	}
}

#### inserts all paragraphs of given arrays ###############################
function insert_paragraphs( $pid, $ver, $pa, $hd, $tx ){
	for( $i = 0; !empty( $tx[ $i ]); $i++ ){
		$para = par( $pa[ $i ], 6 );
		$hdln = stripslashes( $hd[ $i ]);
		$text = stripslashes( $tx[ $i ]);
		$hdln = addslashes( $hdln );
		$text = addslashes( $text );
		if( !mysql_query( "insert into par_temp values(
			$pid, $ver, '$para', now(), null, '$hdln', '$text')" )){
			echo "Error No. 110: ".mysql_errno().": ".mysql_error();
			exit;
		}
	}
	if( !$id = mysql_query( "select headln, text 
		from par_temp order by parastr" )){
		echo "Error No. 111: ".mysql_errno().": ".mysql_error();
		exit;
	}
	$i = 0;
	while( $row = mysql_fetch_array( $id )) {
		$i++;
		$para = par( $i, 6 );
		$hdln = addslashes( $row[ "headln" ]);
		$text = addslashes( $row[ "text" ]);
#		if( $flow == 'file' ){
#			$hdln = addslashes( $hdln );
#			$text = addslashes( $text );
#		}
#		else {
#			$hdln = ereg_replace( "'", "\'", $hdln );
#			$text = ereg_replace( "'", "\'", $text );
#		}
		if( !mysql_query( "insert into paragraph values(
			$pid, $ver, '$para', now(), null, '$hdln', '$text')" )){
			$errno = mysql_errno(); $errtxt = mysql_error(); 
			mysql_query( "delete from par_temp" );
			if( $errno == 1062 ) return( FALSE );
			echo "Error No. 112: $errno: $errtxt";
			exit;
		}
	}
	if( !mysql_query( "delete from par_temp" )){
		echo "Error No. 113: ".mysql_errno().": ".mysql_error();
		exit;
	}
	return( TRUE );
}

#### reads all other active projects and returns a select box string ######
function get_all_projects( $pid, $rb ){
	global $t;
	# select all other projects
	if( !$id = mysql_query( "select proj_id, title, titlabb from metatext
		where status = 'act' and proj_id <> $pid 
		and ref_by <> $pid order by titlabb" )) {
		echo "Error No. 120: ".mysql_errno().": ".mysql_error();
		exit;
	}
	$ret = "<select name='rb' size=1>'>\n";
	$ret .= "<option value='0'>#.#.# $t[46] #.#.#\n";
	while( $row = mysql_fetch_array( $id )){
		$ta = "$t[11] ".$row[ "titlabb" ].': '.$row[ "title" ];
		if( strlen( $ta ) > 120 ) $ta = substr( $ta, 0, 117 ).'...';
		$pid = $row[ "proj_id" ];
		$ret .= "<option value='$pid'";
		if( $rb == $pid ) $ret .= " selected";
		$ret .= ">$ta\n";
	}
	$ret .= "</select><input type=hidden name='rb_old' value='$rb'>\n";
	return $ret;
}

#### reads all mailing lists of prjs and returns a select box string ######
function get_all_mlists( $pid, $rb, $curlist, $ownlist ){
#	global $maildom;
	$curlist = htmlentities( $curlist );
	$ownlist = htmlentities( $ownlist );
	$n = 0;
	while( $rb > 0 ){
		# select ref-by project
		if( !$id = mysql_query( "select maillst, ref_by from metatext
			where proj_id = $rb and status = 'act'" )) {
			echo "Error No. 130: ".mysql_errno().": ".mysql_error();
			exit;
		}
		$rb = 0;
		if( $row = mysql_fetch_array( $id )){
			$ml[ $n++ ] = htmlentities( $row[ "maillst" ]);
			$rb = $row[ "ref_by" ];
		}
	}
	$ml[ $n++ ] = $curlist;		# current mailinglist
	if( $ownlist <> $curlist ) $ml[ $n++ ] = $ownlist;
	sort( $ml );
	$ret = "<select name='ml' size=1>'>\n";
	for( $i = 0, $str = ""; $i < $n; $i++ ){
		if( $ml[ $i ] <> $str ){	# avoid doubles
			$str = $ml[ $i ];
			$ret .= "<option";
			if( $str == $curlist ) $ret .= " selected";
			$ret .= ">$str\n";
		}
	}
	$ret .= "</select>\n<input type=hidden name='ml_old' value='$curlist'>\n";
	return $ret;
}

#### reads all members of given project and returns a select box string ######
function get_all_members( $pid, $mid ){
	if( !$id = mysql_query( "select member.memb_id as mid, last_n, first_n, email
		from member, projmemb
		where member.memb_id = projmemb.memb_id
		and proj_id = $pid" )) {
		echo "Error No. 130: ".mysql_errno().": ".mysql_error();
		exit;
	}
	$ret = "<select name='mm' size=1>'>\n";
	while( $row = mysql_fetch_array( $id )){
		$mm = $row[ "mid" ];
		$ln = $row[ "last_n" ];
		$fn = $row[ "first_n" ];
		$em = $row[ "email" ];
		$ret .= "<option value='$mm'";
		if( $mm == $mid ) $ret .= " selected";
		$ret .= ">$fn $ln, $em\n";
	}
	$ret .= "</select>\n";
	return $ret;
}

#### create_file creates the file for the base text #######################
function create_file( $pid, $ver, $ta ){
	$filename = strtolower($ta)."/v".fill($ver,4).".phtml";
	$str = "<?php\ninclude(\"menu.inc\");\n";
	$str .= "include(\"func.inc\");\n";
	$str .= "include(\"view.inc\");\n";
	$str .= "showtext($pid,$ver,TRUE);\nfoot();\n?>";
	# chmod( strtolower($ta), 757);
	$fp = fopen( $filename, "w" );
	fputs( $fp, $str );
	fclose( $fp );
	# chmod( strtolower($ta), 755);
	$lk = "/".$filename;
	if( !mysql_query( "update metatext set link = '$lk'
		where proj_id = $pid and vers_id = $ver" )){
		echo "Error No. 150: ".mysql_errno().": ".mysql_error();
		exit;
	}
}

#### hidden_vars return string with hidden values #########################
function hidden_vars( $mode ){
	global $ver, $pid;
	$ret = "<input type=hidden name='mode' value='$mode'>";
	$ret .= "<input type=hidden name='ver' value='$ver'>";
	$ret .= "<input type=hidden name='pid' value='$pid'>";
	return $ret;
}

#### read text from the uploaded file, return headlines and paragraphs ####
function read_uploaded_file( $userfile, &$hd, &$tx ){
	$i = 0;
	$fp = fopen( $userfile, 'r' );
	while( !feof( $fp )){
		$buf1 = '';
		# read up to first text line
		while( !feof( $fp ) && empty( $buf1 )){
			$buf1 = trim( fgets( $fp, 4096 ));
		}
		# read next line
		$buf2 = trim( fgets( $fp, 4096 ));
		# next line is empty -> headline
		if( empty( $buf2 )){
			$hd[ $i ] = $buf1;
			$buf1 = '';
			# read up to next text line
			while( !feof( $fp ) && empty( $buf1 )){
				$buf1 = trim( fgets( $fp, 4096 ));
			}
			$buf2 = trim( fgets( $fp, 4096 ));
		}
		while( !feof( $fp ) && !empty( $buf2 )){
			$buf1 .= ' '.$buf2;
			$buf2 = trim( fgets( $fp, 4096 ));
		}
		if( !empty( $buf2 )) $buf1 .= ' '.$buf2;
		$tx[ $i ] = $buf1;
		$i++;
	}
	fclose( $fp );
}

#### MAIN #################################################################

#echo "pid: $pid, ver: $ver, mode: $mode";

if( !$pid || !$ver || !$mode || !read_session( $xid, $xpar )){
	echo "<head>\n<meta http-equiv='refresh' content='0; ";
	echo "URL=http://$fqdomain/?logout=1'\n</head>";
#	if( !head( $_x[16], '', $xid, $xpar )) exit;
#	echo "<font $_fontbulk>\n<p>$_x[17]</p></font>\n<br><br>";
#	foot();
	exit;
}
$t = getmsg( 16, $lang );
$mid = $xpar[ 'mid' ];
$e = $xpar[ 'xem' ];
$em = $e;
$fn = $xpar[ 'xfn' ];
$ln = $xpar[ 'xln' ];

switch( $mode ){

	###################
	case "data_change":
	###################

	if( $res ){
		# maintainer changed?
		if( $mid != $mm ){
			if( !mysql_query( "update metatext set memb_id = $mm
				where proj_id = $pid" )){
				echo "Error No. 11: ".mysql_errno().": ".mysql_error();
				exit;
			}
			$mid = $mm;
			$mm = -1;	# flag: maintainer has changed
			if( !$id = mysql_query( "select last_n, first_n, email  
				from member where memb_id = $mid" )) {
				echo "Error No. 12: ".mysql_errno().": ".mysql_error();
				exit;
			}
			if( $row = mysql_fetch_array( $id )) {
				$fn = $row[ 'first_n' ];
				$ln = $row[ 'last_n' ];
				$em = $row[ 'email' ];
			}
		}

		# check checkbox
		if( $usetopml && ( $rb > 0 ) && ( $rb != $rb_old ) && ( $ml == $ml_old )){
			if( !$id = mysql_query( "select maillst
				from metatext where proj_id = $rb" )) {
				echo "Error No. 13: ".mysql_errno().": ".mysql_error();
				exit;
			}
			if( $row = mysql_fetch_array( $id )) {
				$ml = $row[ 'maillst' ];
			}
		}

		# ref_by changed/deleted and mlist not own? Then set to own list
		elseif(( $rb != $rb_old ) && ( $ml != mlst( $ta ))){
			$ml = mlst( $ta );
		}
		# set new values
		if( !mysql_query( "update metatext set
			title = '$ti', maillst = '$ml', ref_by = $rb, keywds = '$kw', descr = '$ds'
			where proj_id = $pid and vers_id = $ver" )){
			echo "Error No. 14: ".mysql_errno().": ".mysql_error();
			exit;
		}

		# check and set change of maillst
		if( $ml != $ml_old ){

			# put all pids and mlists of sub projects into sublist
			$i = 0; $end = 1; #$act = $pid;
			$sublist[ $pid ] = $ml;
			while( list( $act, $ml_old ) = each( $sublist )){
				if( !$id = mysql_query( "select subpj_id, titlabb, maillst 
					from subpjs, metatext
					where subpjs.subpj_id = metatext.proj_id
					and subpjs.proj_id = $act" )){
					echo "Error No. 15: ".mysql_errno().": ".mysql_error();
					exit;
				}
				while( $row = mysql_fetch_array( $id )){
					# not using own mailinglist
					# and not using $ml's mailinglist?
					$ml_own = mlst( $row[ "titlabb" ]);
					if(( $row[ "maillst" ] <> $ml_own )
					and ( $row[ "maillst" ] <> $ml )){
						$sublist[ $row[ "subpj_id" ]] = $ml_own;
					}
				}
				#if( ++$i < $end ) $act = $sublist[ $i ];
			}

			# update all pjs from sublist with root maillist name
			reset( $sublist );	# go to first elem
			while( list( $act, $ml ) = each( $sublist )){
				$qry = "update metatext set maillst = '$ml' ";
				$qry .= "where proj_id = $act ";
				$qry .= "and (status = 'ini' or status = 'act')";
				if( !mysql_query( $qry )){
					echo "Error No. 16: ".mysql_errno().": ".mysql_error();
					exit;
				}
			}
		}

		# check and set change of ref_by
		if( $rb != $rb_old ){
			if ( $rb == 0 ){
				if( !mysql_query( "delete from subpjs 
					where proj_id = $rb_old and subpj_id = $pid" )){
					echo "Error No. 17: ".mysql_errno().": ".mysql_error();
					exit;
				}
			}	
			elseif( $rb_old == 0 ){
				if( !mysql_query( "insert into subpjs values(
					$rb, $pid )" )){
					echo "Error No. 18: ".mysql_errno().": ".mysql_error();
					exit;
				}
			}
			else {
				if( !mysql_query( "update subpjs 
					set proj_id = $rb
					where proj_id = $rb_old 
					and subpj_id = $pid" )){
					echo "Error No. 19: ".mysql_errno().": ".mysql_error();
					exit;
				}
			}
			# set ref_by of act and ini pjs parallel
			if( !mysql_query( "update metatext set ref_by = $rb
				where proj_id = $pid 
				and ( status = 'ini' or status = 'act' )" )){
				echo "Error No. 20: ".mysql_errno().": ".mysql_error();
				exit;
			}
			# calculate new member number
			if( $rb > 0 ) update_member_num( $rb );
			if( $rb_old > 0 ) update_member_num( $rb_old );
		}
		if( !head( $t[1], '', $xid, $xpar )) exit;
		echo "<font $_fontbulk>\n";
		get_metatext( $pid, $ver, $ti, $ta, $dt, $st, $lg, $rb, $rbs, $ml, $lk, $kw, $mn, $ds );
	}
	else{
		if( !head( $t[0], '', $xid, $xpar )) exit;
		echo "<font $_fontbulk>\n";
		get_metatext( $pid, $ver, $ti, $ta, $dt, $st, $lg, $rb, $rbs, $ml, $lk, $kw, $mn, $ds );
		$all_projects = get_all_projects( $pid, $rb );
		$ml_old = mlst( $ta );
		$all_mlists = get_all_mlists( $pid, $rb, $ml, $ml_old );
		$all_members = get_all_members( $pid, $mid );
	}
	break;

	#####################
	case "get_prev_text":
	#####################

	$ver--;
	if( !mysql_query( "insert into par_temp 
		select proj_id, vers_id+1, parastr, 
		now(), send_by, headln, text from paragraph
		where proj_id = $pid and vers_id = $ver
		and length( parastr ) < 8" )){
		echo "Error No. 27: ".mysql_errno().": ".mysql_error();
		exit;
	}
	$ver++;
	if( !mysql_query( "insert into paragraph 
		select * from par_temp" )){
		echo "Error No. 28: ".mysql_errno().": ".mysql_error();
		exit;
	}
	if( !mysql_query( "delete from par_temp" )){
		echo "Error No. 29: ".mysql_errno().": ".mysql_error();
		exit;
	}
	get_metatext( $pid, $ver, $ti, $ta, $dt, $st, $lg, $rb, $rbs, $ml, $lk, $kw, $mn, $ds );
	create_file( $pid, $ver, $ta );
	$mode = "text_change";

	###################
	case "text_change":
	###################

	if( !$id = mysql_query( "select count(*) from paragraph
		where proj_id=$pid and vers_id=$ver" )){
		echo "Error No. 30: ".mysql_errno().": ".mysql_error();
		exit;
	}
	if( !$row = mysql_fetch_array( $id )) {
		echo "Error No. 31: no rows selected";
		exit;
	}
	$count = $row[ 0 ];
	if( $res ){
		if(( $dataflow == 'file' ) && ( $userfile == 'none' )){
			if( !head( $t[2], '', $xid, $xpar )) exit;
			echo "<font $_fontbulk>\n";
			echo "<p><b><font color='$_fg3'>$t[48]</font></b></p>\n";
			$res = '';
		}
		else {
			if( $dataflow == 'file' ) read_uploaded_file( $userfile, $hd, $tx );
			if( !mysql_query( "delete from paragraph
				where proj_id = $pid and vers_id = $ver" )){
				echo "Error No. 32: ".mysql_errno().": ".mysql_error();
				exit;
			}
			insert_paragraphs( $pid, $ver, $pa, $hd, $tx );
			if( !head( $t[4], '', $xid, $xpar )) exit;
			echo "<font $_fontbulk>\n";
		}
	}
	else {
		if( !head( $t[3], '', $xid, $xpar )) exit;
		echo "<font $_fontbulk>\n";
	}
	get_metatext( $pid, $ver, $ti, $ta, $dt, $st, $lg, $rb, $rbs, $ml, $lk, $kw, $mn, $ds );
	break;


	####################
	case "text_version":
	####################

	if( $res ){
		if( !$id = mysql_query( "select proj_id from metatext 
			where proj_id = $pid and vers_id = $ver" )){
			echo "Error No. 40: ".mysql_errno().": ".mysql_error();
			exit;
		}
		if( !mysql_fetch_array( $id )){
			$ver--;	# get language and membnum from prev. version
			get_metatext( $pid, $ver, $x, $x, $x, $x, $lg, $x, $x, $x, $x, $x, $mn, $x );
			$ver++;			
			if( !mysql_query( "insert into metatext values(
				$pid, $ver, $mid, '$ti', '$ta', now(), 'ini',
				'$lg', $rb, '$ml', null, '$kw', $mn, '$ds' )" )){
				echo "Error No. 42: ".mysql_errno().": ".mysql_error();
				exit;
			}
			# next step: submission of a new text
			$res = "";
			$mode = "text_new";
		}
		else {
			if( !head( $t[6], '', $xid, $xpar )) exit;
			echo "<font $_fontbulk>\n";
			get_metatext( $pid, $ver, $ti, $ta, $dt, $st, $lg, $rb, $rbs, $ml, $lk, $kw, $mn, $ds );
		}
	}
	else {
		if( !head( $t[5], '', $xid, $xpar )) exit;
		echo "<font $_fontbulk>\n";
		get_metatext( $pid, $ver, $ti, $ta, $dt, $st, $lg, $rb, $rbs, $ml, $lk, $kw, $mn, $ds );
		$all_projects = get_all_projects( $pid, $rb );
		$ml_old = mlst( $ta );
		$all_mlists = get_all_mlists( $pid, $rb, $ml, $ml_old );
		$all_members = get_all_members( $pid, $mid );
		$ver++;
		$lk = "";
		$dt = date("j").". ".date("M").". ".date("Y");
		$st = $ini;
		echo "<p>$t[49]</p>\n";
	}
	if( $mode == "text_version" ) break;
	
	################
	case "text_new":
	################

	get_metatext( $pid, $ver, $ti, $ta, $dt, $st, $lg, $rb, $rbs, $ml, $lk, $kw, $mn, $ds );
	if( empty( $res )){
		if( !head( $t[2], '', $xid, $xpar )) exit;
		echo "<font $_fontbulk>\n";
	}
	elseif( $dataflow == 'form' ) {
		if( !insert_paragraphs( $pid, $ver, $pa, $hd, $tx )){
			if( !mysql_query( "delete from paragraph
				where proj_id = $pid and vers_id = $ver" )){
				echo "Error No. 43: ".mysql_errno().": ".mysql_error();
				exit;
			}
			insert_paragraphs( $pid, $ver, $pa, $hd, $tx );
		} else {
			# create the file for the base text
			create_file( $pid, $ver, $ta );
		}
		if( !head( $t[7], '', $xid, $xpar )) exit;
		echo "<font $_fontbulk>\n";
	}
	elseif(( $dataflow == 'file' ) && ( $userfile == 'none' )){
		if( !head( $t[2], '', $xid, $xpar )) exit;
		echo "<font $_fontbulk>\n";
		echo "<p><b><font color='$_fg3'>$t[48]</font></b></p>\n";
		$mode = 'text_new';
		$res = '';
	}
	else {
		read_uploaded_file( $userfile, $hd, $tx );
		insert_paragraphs( $pid, $ver, $pa, $hd, $tx );
		# create the file for the base text
		create_file( $pid, $ver, $ta );
		if( !head( $t[7], '', $xid, $xpar )) exit;
		echo "<font $_fontbulk>\n";
	}
	$count = 0;
	break;

	################
	case "activate":
	################

	if( $ver > 1 ){
		$ver--;
		if( !mysql_query( "update metatext 
			set status = 'arc', maillst = null, ref_by = 0
			where proj_id = $pid and vers_id = $ver" )){
			echo "Error No. 50: ".mysql_errno().": ".mysql_error();
			exit;
		}
		$ver++;
	}
	if( !mysql_query( "update metatext set status = 'act'
		where proj_id = $pid and vers_id = $ver" )){
		echo "Error No. 51: ".mysql_errno().": ".mysql_error();
		exit;
	}
	get_metatext( $pid, $ver, $ti, $ta, $dt, $st, $lg, $rb, $rbs, $ml, $lk, $kw, $mn, $ds );
	eval( "\$x = \"$t[8]\";" );
	if( !head( $x, '', $xid, $xpar )) exit;
	echo "<font $_fontbulk>\n";
	eval( "\$x = \"$t[50]\";" );
	echo "<p>$x";
	if( $ver > 1 ) echo " $t[51]";
	echo ". $t[52]</p>\n";
	break;

	################
	case "finish":
	################

	if( !$id = mysql_query( "select titlabb, title from metatext
		where ref_by = $pid and status = 'act'" )){
		echo "Error No. 60: ".mysql_errno().": ".mysql_error();
		exit;
	}
	get_metatext( $pid, $ver, $ti, $ta, $dt, $st, $lg, $rb, $rbs, $ml, $lk, $kw, $mn, $ds );
	if( !$row = mysql_fetch_row( $id )){
		if( !mysql_query( "update metatext set status = 'fin'
			where proj_id = $pid and vers_id = $ver" )){
			echo "Error No. 61: ".mysql_errno().": ".mysql_error();
			exit;
		}
		eval( "\$x = \"$t[9]\";" );
		if( !head( $x, '', $xid, $xpar )) exit;
		eval( "\$x = \"$t[53]\";" );
	} else {
		if( !head( $t[10], '', $xid, $xpar )) exit;
		eval( "\$x = \"$t[54]\";" );
		$x .= ":\n<b><ul><li><a href='/";
		$x .= lnkxid( strtolower( $row[0] ), $xid );
		$x .= "'>Projekt $row[0]: $row[1]</a></li>";
		while( $row = mysql_fetch_row( $id )){
			$x .= "\n<li><a href='/";
			$x .= lnkxid( strtolower( $row[0] ), $xid );
			$x .= "'>Projekt $row[0]: $row[1]</a></li>";
		}
		$x .= "</ul></b>\n<hr size=1 noshade width=100%>\n";
	}
	echo "<font $_fontbulk><p>$x</p></font>\n";
} # end switch

#################
# html output	#	
#################

echo "<table><tr><td>\n";
echo "<font $_fontbulk>\n";
echo "<ul>\n<li><b>$t[12]:</b> $fn $ln </li>\n";
if( $mm < 0 ){	# maintainer has changed?
	echo "<li><b>$t[13]:</b> $em</li>\n";
} else {
	echo "<li><b>$t[13]:</b> $e</li>\n";
}
echo "<li><b>$t[14]:</b> $ti </li>\n";
echo "<li><b>$t[15]:</b> $ta </li>\n";
echo "<li><b>$t[16]:</b> $ver / $st </li>\n";
echo "<li><b>$t[17]:</b> $dt </li>\n";
echo "<li><b>$t[18]:</b> $rbs </li>\n";
echo "<li><b>$t[19]:</b> ".htmlentities( $ml )."</li>\n";
echo "<li><b>$t[20]:</b> <a href='";
echo lnkxid( $lk, $xid )."'>$lk</a></li></font></td>\n";
echo "<td><font $_fontbulk>\n";
echo "<form action='/maintainer.phtml?xid=$xid' method='POST'>";
echo "<input type=submit value='$t[22]'>";
echo "</form></font></td></tr>\n<tr><td colspan=2>";
echo "<font $_fontbulk>\n";
echo "<ul><li><b>$t[21]:</b> ".add_xid( $ds, $xid )."</li></ul>";
echo "</font></td></tr></table></p>\n";

switch( $mode ){

####################
case "data_change":
case "text_version":
####################

	if( $res ){
		if( $mm > 0 ){	# maintainer has not changed
			echo "<center><form action='/modify.phtml?xid=$xid' method='POST'>";
			echo hidden_vars( 'data_change' );
			echo "<input type=submit value='$t[23]'>";
			echo "</form></center>";
		}
		echo "<br><br></font>";
		foot();
		exit;
	}
	eval( "\$x = \"<form>$t[24]</form>\";" );
	echo add_xid( $x, $xid );
	echo "\n<form action='/modify.phtml?res=1&xid=$xid' method='POST'>\n";
	echo "<table>\n<tr>\n<td width='90'>\n";
	echo "<font $_fontbulk>\n";
	echo "$t[25]:</font></td>\n<td>\n";
	echo "<font $_fontbulk>\n";
	echo "$all_members</font></td></tr>\n";
	echo "<tr>\n<td>\n";
	echo "<font $_fontbulk>\n";
	echo "<input type=hidden name='ta' value='$ta'>\n";
	echo "$t[14]:</font></td>\n<td>\n";
	echo "<font $_fontinpt>";
	echo "<input type='text' name='ti' size='40' maxlength='255' ";
	echo "value='$ti'></font></td></tr>\n";
	echo "<tr>\n<td>\n";
	echo "<font $_fontbulk>\n";
	echo "$t[18]:</font></td>\n<td>\n";
	echo "<font $_fontbulk>\n";
	echo "$all_projects<br><input type=checkbox name='usetopml' checked value='1'> $t[47]</font></td></tr>\n";
	echo "<tr>\n<td>\n";
	echo "<font $_fontbulk>\n";
	echo "$t[19]:</font></td>\n<td>\n";
	echo "<font $_fontbulk>\n";
	echo "$all_mlists</font></td></tr>\n";
	echo "<tr>\n<td>\n";
	echo "<font $_fontbulk>\n";
	echo "$t[26]:</font></td>\n<td>\n";
	echo "<font $_fontinpt>";
	echo "<input type='text' name='kw' size='40' maxlength='255' ";
	echo "value='$kw'></font></td></tr>\n";
	echo "<tr>\n<td valign='top'>\n";
	echo "<font $_fontbulk>";
	echo "$t[21]:</font></td>\n<td>\n";
	echo "<font $_fontinpt>\n";
	echo "<textarea type='text' name='ds' rows='4' cols='40' ";
	echo "wrap=virtual>$ds</textarea></font></td></tr>\n";
	echo "<tr>\n<td align=center colspan=2>\n";
	echo "<font $_fontbulk>";
	echo hidden_vars( $mode )."\n";
	if( $mode == "data_change" ){ 
		echo "<input type=submit value='$t[27]'>";
	}else{
		echo "<input type=submit value='$t[28]'>";
	}
	echo "</font></td></tr></table></form>\n";
break;

#######################
case "text_new":
case "text_change":
case "text_upload_new":
case "text_upload_chg":
#######################

	if( $res ){
		echo "<center>\n<form action='/modify.phtml?xid=$xid' method='POST'>";
		echo "<input type=submit value='$t[29]'>";
		echo hidden_vars( 'activate' )."</form></center>\n";
		echo "<form action='/modify.phtml?xid=$xid' method='POST'>\n";
		echo "<table width=100% height=75 bgcolor='$_bg5'><tr><td>";
		echo "<font $_fontbulk><center>";
		$x = "<input type='text' name='offset' size='2' maxlength='6' value='3'>";
		eval( "\$x = \"$t[30]\";" );
		echo "$x<br>\n";
		echo "<img src='/images/pixel.gif' width=1 height=22>\n";
		echo "<input type=submit value='$t[31]'>";
		echo hidden_vars( 'text_change' );
		echo "</center></font></tr></td></table></form>\n";
		eval( "\$x = \"$t[32]\";" );
		echo "<center><h3><font color='$_fg3'>$x</font></h3></center>\n";
		showtext( $pid, $ver, FALSE );
		foot();
		exit;
	}

	# if more than zero rows are selectable
	if( !empty( $count )){
		if( !$id = mysql_query( "select * from paragraph
			where proj_id=$pid and vers_id=$ver
			order by parastr" )){
			echo "Error No. 80: ".mysql_errno().": ".mysql_error();
			exit;
		}
		if( empty( $offset )){
			$offset = 4;
		}
		elseif( $offset > 30 ) {
			$offset = 30;
		}
		elseif( $offset < 0 ) {
			$offset = 0;
		}
	}
	else {
		$offset = 8;	# initial
	}

	echo "<p>$t[33]</p>\n";
	echo "<h3><font color='$_fg3'>$t[34]</font></h3>\n";
	echo "$t[35]\n";

	echo "<form enctype='multipart/form-data' action='/modify.phtml?res=1&xid=$xid' ";
	echo "method='post'>\n<center>".hidden_vars( $mode ); # text_new/text_change
	echo "<input type='hidden' name='dataflow' value='file'>\n";
	echo "<input type='hidden' name='MAX_FILE_SIZE' value='110000'>\n";
	echo "$t[36]: <font size='3'><input type='file' name='userfile' size='40' ";
	echo "maxlength='255'></font><br><br>\n<input type='submit' ";
	echo "value='$t[37]'></center></form>\n";
	echo "<h3><font color='$_fg3'>$t[38]</font></h3>\n";
	echo "$t[39]\n";

	if(( $mode == "text_new" ) && ( $ver > 1 )){
		echo "<form action='/modify.phtml?xid=$xid' method='POST'>\n";
		echo "$t[40]:\n";
		echo "<center>".hidden_vars( "get_prev_text" );
		echo "<input type=submit value='$t[41]'>";
		echo "</center>\n</form>\n";
	}

	echo "<form action='/modify.phtml?res=1&xid=$xid' method='POST'>\n";
	echo "$t[42]\n";
	echo "<br><br>\n<center>".hidden_vars( $mode ); # text_new or text_change
	echo "<input type='hidden' name='dataflow' value='form'>\n";
	echo "<input type=submit value='$t[43]'>\n<br><br>";
	for( $i = 0; $i < $count + $offset; $i++ ){
		$pa[ $i ] = ($i+1)*10; $hd[ $i ] = ""; $tx[ $i ] = "";
		if(( $i < $count ) && (	$row = mysql_fetch_array( $id ))){
			$pa[ $i ] = fmt( substr( $row[ "parastr" ], 0, 6))*10;
			$hd[ $i ] = ereg_replace( "\"", "&quot;", $row[ "headln" ]);
			$tx[ $i ] = $row[ "text" ];
		}
		echo "$t[44] <input type='text' name='pa[]' size='5' ";
		echo "maxlength='6' value='$pa[$i]'> &nbsp;\n";
		echo "$t[45] <input type='text' name='hd[]' "; 
		echo "size='50' maxlength='255' value=\"$hd[$i]\">\n<br><br>";
		echo "<textarea type='text' name='tx[]' rows='8' cols='70' ";
		echo "wrap=virtual>$tx[$i]</textarea>\n<br><br>";
	}
	echo "<input type=submit value='$t[43]'>";
	echo "</center>\n</form></font>\n";
}
foot();
?>
