<?
	#---------------------------------------------------------------------#
	# This source code is (c)2000 White Pajamas, Inc                      #
	# see http://www.stonekeep.com/license.html for licensing information #
	#---------------------------------------------------------------------#

	#----------------------------------------------------------------------
	# $Id: addlong.php3,v 1.21 2000/12/29 05:46:41 shevett Exp $
	#----------------------------------------------------------------------
	
	#----------------------------------------------------------------------
	#		This module handles a sequenced add of a new slip, prompting the
	#		user as it goes along.
	#----------------------------------------------------------------------

	include "sliputils.php3";

	Function ProgressBar() {
		Global $sys_graphics, $l_ctag, $l_cpick, $l_problem, $l_parent, $l_status, $l_currtech, $seq,$l_priority;
		echo "<table width=100% border=1>\n";
		echo "<tr valign=top>\n";
		$set = "<td width=20% bgcolor=#99cc99 align=center><img src=\"$sys_graphics/active-circle.gif\">\n";
		$unset = "<td width=20% bgcolor=#999999 align=center><img src=\"$sys_graphics/circle.gif\">\n";
		$current = "<td width=20% bgcolor=#cccc00 align=center><img src=\"$sys_graphics/circle.gif\">\n";

		
		if ($seq == 1) {
			echo "$current" ;
		} else {
			echo (Empty($l_ctag) && Empty($l_cpick)) ? "$unset" : "$set";
		}
		echo "<b>Step 1</b><br>";
		echo "Contact Information</td>\n";

		if ($seq == 2) {
			echo "$current" ;
		} else {
			echo Empty($l_problem) ? "$unset" : "$set";
		}
		echo "<b>Step 2</b><br>";
		echo "Problem Description ($l_cpl)</td>\n";

		if ($seq == 3) {
			echo "$current" ;
		} else {
			echo (Empty($l_parent) && Empty($l_status) && Empty($l_currtech)) ? "$unset" : "$set";
		}
		echo "<b>Step 3</b><br>";
		echo "Other Slip Info</td>\n";

		if ($seq == 4) {
			echo "$current" ;
		} else {
			echo ($seq < 4) ? "$unset" : "$set";
		}
		echo "<b>Step 4</b><br>";
		echo "Final Summary</td>\n";

		if ($seq == 5) {
			echo "$current" ;
		} else {
			echo "$unset";
		}
		echo "<b>Step 5</b><br>";
		echo "Committed / Followup</td>\n";

		echo "</tr></table>\n";
	}

	#----------------------------------------------------------------------

	Function ButtonBar() {
		Global $seq, $sys_graphics;
		echo "<table width=100% bgcolor=#808080 cellpadding=2 cellspacing=1 border=0>\n";
		echo "<tr>\n";
		if ($seq != 5) {
			echo "<td width=20% align=center>\n";
			echo "<input type=submit name=prev value=\"<- Prev\">\n";
			echo "</td>\n";
		}

		echo "<td width=20% align=center>";
		$tstring = ($seq == 4) ? "Finish!" : "Next ->";
		$tstring = ($seq == 5) ? "Add another Slip" : $tstring ;
		echo "<input type=submit name=next value=\"$tstring\">";
		echo "</td>";
		echo "</form>";

		echo "<form method=post action=\"nbrowse.php3\">";
		echo "<td width=20% align=center>\n";
		$cstring = ($seq == 5) ? "Return to Slips" : "Cancel";
		echo "<input type=submit name=cancel value=\"$cstring\">\n";
		echo "</td>\n";

		echo "</tr>\n";
		echo "</table>\n";
	}

	#----------------------------------------------------------------------
	Function GetProblem() {
		Global $l_problem,$l_summary,$sys_selfont,$l_priority,$l_currtech;
		echo "<tr bgcolor=#000000><td align=center>\n";
		echo "<table bgcolor=#808080>";
		echo "<tr bgcolor=#bbbbbb>\n";
		echo "<th align=center><h3>Describe the Problem</td>\n";
		echo "</tr>\n";
		echo "<tr bgcolor=#bbbbbb valign=top>\n";
		echo "<td bgcolor=#808080>\n";
		echo "<i>Problem</i><br>\n";
		# FIXME: needs maxsize based on database query
		echo "<input name=\"l_problem\" size=80 value=\"" . stripslashes($l_problem) . "\">\n";
		echo "</td>\n";
		echo "</tr>\n";
		echo "<tr bgcolor=#bbbbbb valign=top>\n";
		echo "<td bgcolor=#808080>\n";
		echo "<i>Summary</i><br>\n";
		echo "<font size=$sys_selfont><textarea name=\"l_summary\" rows=10 cols=60 wrap=\"soft\">";
		echo stripslashes($l_summary);
		echo "</textarea></font>\n";
		echo "</td>\n";
		echo "</tr>\n";
		echo "</table></td></tr>\n";
	}
		

	#----------------------------------------------------------------------
	Function GetOther() {
		Global	$l_status,$l_parent,$l_priority,
						$l_currtech,$sys_selall, $sys_groupstatus,
						$sys_lparent, $sys_lstatus, $sys_ltech, $sys_lusecurr, $curruser;

		$l_status = ("$l_status") ? $l_status : $sys_lstatus;
		$l_parent = ("$l_parent") ? $l_parent : $sys_lparent;

		# pick a default for current tech..

		if (! "$l_currtech") {
			if ($sys_lusecurr) {
				$l_currtech = $curruser;
			} else {
				$l_currtech = $sys_ltech;
			}
		}
	
		echo "<tr bgcolor=#000000><td align=center>\n";
		echo "<table bgcolor=#808080>";
		echo "<tr bgcolor=#bbbbbb height=100%>\n";
		echo "<th align=center colspan=3><b>Enter additional details</b></td>\n";
		echo "</tr>";
		echo "<tr bgcolor=#808080 valign=top>\n";
		echo "<td colspan=3>\n";
		ChooseParent("l_parent",$l_parent);
		echo "</td>";
		echo "</tr>";
		echo "<tr bgcolor=#808080 valign=top>\n";
		echo "<td>\n";
		ChooseStatus("l_status",$l_status);
		echo "</td>";
		echo "<td>\n";
		ChooseCurrtech("Current tech","l_currtech",$l_currtech);
		echo "</td>";
		echo "<td>\n";
		ChoosePriority("l_priority",$l_priority);
		echo "</td>";
		echo "</table></td></tr>";
	}

	#----------------------------------------------------------------------
	Function GetContact() {
		Global $l_ctag;
		echo "<tr bgcolor=#000000><td align=center>\n";

		echo "<table bgcolor=#808080>";
		echo "<tr bgcolor=#bbbbbb>\n";
		echo "<th align=center><h3>Search for a Contact</th>\n";
		echo "<th align=center><h3>Contact Location detail</th>\n";
		echo "</tr>";

		echo "<tr bgcolor=#808080 valign=top>";
		if ("$l_ctag") {
			$cq = db_query("select * from contacts where tag='$l_ctag'");
			$cinfo = db_fetch_array($cq);
		}
		echo "<td align=center>\n";
		?>
			<table bgcolor=#808080>
			<tr bgcolor=#808080 valign=top cellspacing=0>
				<td bgcolor=#808080>
					<i>Tag</i><br>
					<!-- FIXME: needs maxsize based on database query -->
					<input name="l_ctag" value="<? echo $l_ctag ?>" size=10><br>
				</td>
				<td bgcolor=#808080 valign=center>
					<input type=submit name=s_tag value="Find">
				</td>
			</tr>
			<tr bgcolor=#000000><td colspan=2></td></tr>
			<tr bgcolor=#bbbbbb valign=top>
				<td bgcolor=#808080>
					<i>Name</i><br>
					<!-- FIXME: needs maxsize based on database query -->
					<input type=text name=a_name size=40 value="<? echo $cinfo[name] ?>">
				</td>
				<td bgcolor=#808080 valign=center>
					<input type=submit name=s_name value="Find">
				</td>
			</tr>
			<tr bgcolor=#000000><td colspan=2></td></tr>
			<tr bgcolor=#bbbbbb valign=top>
				<td bgcolor=#808080>
					<i>Company</i><br>
					<!-- FIXME: needs maxsize based on database query -->
					<input type=text name=a_company size=50 value="<? echo $cinfo[company] ?>">
				</td>
				<td bgcolor=#808080 valign=center>
					<input type=submit name=s_company value="Find">
				</td>
			</tr>
			<tr bgcolor=#000000><td colspan=2></td></tr>
			<tr bgcolor=#bbbbbb valign=top>
				<td bgcolor=#808080>
					<i>E-Mail</i><br>
					<!-- FIXME: needs maxsize based on database query -->
					<input type=text name="a_email" size=30 value="<? echo $cinfo[email] ?>">
				</td>
				<td bgcolor=#808080 valign=center>
					<input type=submit name=s_email value="Find">
				</td>
			</tr>
			</table>
		</td>
		<? # Here's the detail section to the right ?>
		<td>
			<table bgcolor=#808080>
			<tr bgcolor=#808080 valign=top cellspacing=0>
				<td bgcolor=#808080>
					<i>Phone Number</i><br>
					<select name="l_cpl">
					<option value="default">Use Default
					<? 
						if ("$l_ctag") {
							LogIt("DEBUG","addlong.php3","l_cpl is $l_cpl, row is $cinfo[cphonelabel]");
							$match = ("$l_cpl") ? "$l_cpl" : $cinfo[cphonelabel] ;
							$q = db_query("select * from phones where tag='$l_ctag'");
							LogIt("DEBUG","addlong.php3","trying to match $match in the phonelist.");
							while ($prow = db_fetch_array($q)) {
								$sel = ("$prow[label]" == "$match") ? "selected" : "";
								echo "<option value=\"$prow[label]\" $sel>$prow[label] ($prow[number])\n";
							}
						} else {
							$q = db_query("select label from phones group by label");
							while ($row=db_fetch_array($q)) {
								if ($row[label] == $l_cpl) {
									$sel = "selected";
								} else {
									$sel = "";
								}
								echo "<option value=\"$row[label]\" $sel> $row[label]\n";
							}
						}
					?>
					</select>
					<p>
					<i>Address</i><br>
					<select name="l_caddlabel">
					<option value="default">Use Default
					<?	
						$q = db_query("select label from addresses group by label");
						while ($row=db_fetch_array($q)) {
							if ($row[label] == $l_caddlabel) {
								$sel = "selected";
							} else {
								$sel = "";
							}
							echo "<option value=\"$row[label]\" $sel> $row[label]\n";
						}
					?>
					</select>

				</td>
			</tr>
			</table>
		</td>
		</tr>
		</table>
		<?
		echo "</td></tr>";
	}
		
	#----------------------------------------------------------------------

	Function ShowFinal() {
		Global	$seq, $l_status, 
						$l_currtech, $l_ctag, 
						$l_problem, $l_priority,
						$l_summary, $a_name, $l_status,
						$a_company, $a_email, $a_password, $l_parent;

		echo "<tr bgcolor=#000000><td align=center>\n";

		echo "<table bgcolor=#808080>";
		echo "<tr bgcolor=#bbbbbb>\n";
		echo "<th align=center colspan=2><h3>Final review before Commit</td>\n";
		echo "</tr>";
		echo "<tr bgcolor=#808080 valign=top>";
		echo "<td>Contact will be:</td>";
		echo "<td>";
		echo "$l_ctag";
		echo "</td></tr>";

		echo "<tr bgcolor=#808080 valign=top>";
		echo "<td>Problem:</td>";
		echo "<td>" . stripslashes($l_problem) . "</td></tr>";

		echo "<tr bgcolor=#808080 valign=top>";
		echo "<td>Summary:</td>";
		echo "<td>";
#		echo ereg_replace("\n","<br>",htmlspecialchars($l_summary)); 
		echo nl2br(htmlspecialchars(stripslashes($l_summary)));

		echo "</td></tr>";

		echo "<tr bgcolor=#808080 valign=top>";
		echo "<td>Status is:</td>";
		$squery = db_query("select * from status where flag='$l_status'");
		$sinfo = db_result($squery,0,'label');
		echo "<td>$sinfo</td></tr>";

		echo "<tr bgcolor=#808080 valign=top>";
		echo "<td>Parent is:</td>";
		if ($l_parent) {
			$squery = db_query("select problem from slips where sid=$l_parent");
			$pinfo = db_result($squery,0,'problem');
			echo "<td>$l_parent - $pinfo</td></tr>";
		} else {
			echo "<td>None specified.</td></tr>";
		}

		echo "<tr bgcolor=#808080 valign=top>";
		echo "<td>Assigned tech is:</td>";
		echo "<td>$l_currtech</td></tr>";

		echo "<tr bgcolor=#808080 valign=top>";
		echo "<td>Priority is:</td>";
		echo "<td>$l_priority</td></tr>";

		echo "</table></td></tr>";
 
	}

	#----------------------------------------------------------------------
	Function DumpOther() {
		Global	$seq, $l_status, 
						$l_currtech, $l_ctag, 
						$l_problem, $l_priority,
						$l_summary, $a_name, $l_status, 
						$a_company, $a_email, $a_password, $l_parent;

		echo "<input type=hidden name=f_func value=addlslip>\n";
		if ($seq != 5) {
			echo "<input type=hidden name=seq value=$seq>\n";

			if ($seq <> 1) {
				echo "<input type=hidden name=l_ctag     value=$l_ctag>";
				echo "<input type=hidden name=l_cpl	value=$l_cpl>";
				echo "<input type=hidden name=l_caddlabel value=$l_caddlabel>";
			}
	
			if ($seq <> 2) {
#				echo "<input type=hidden name=l_problem  value=\"$l_problem\">";
#				echo "<input type=hidden name=l_summary  value=\"$l_summary\">";
				echo "<input type=hidden name=l_problem value=\"".stripslashes($l_problem)."\">";
				echo "<input type=hidden name=l_summary value=\"".stripslashes($l_summary)."\">";

			}
	
			if ($seq <> 3) {
				echo "<input type=hidden name=l_parent   value=$l_parent>";
				echo "<input type=hidden name=l_currtech value=$l_currtech>";
				echo "<input type=hidden name=l_status   value=$l_status>";
				echo "<input type=hidden name=l_priority	value=$l_priority>";
			}
		} else {
			echo "<input type=hidden name=seq value=0>";
		}

	}

	#----------------------------------------------------------------------

	Function FindContact($whatcol,$whatval) {
		LogIt("DEBUG","addlong.php3","Searching for the value '$whatval' in the column '$whatcol'");
		$tmpq = db_query("select tag from contacts where $whatcol like '%$whatval%'");
		if (db_numrows($tmpq) > 0) {
			$ntag = db_result($tmpq,0,"tag");
		}
		return $ntag;
	}

	#----------------------------------------------------------------------

	Function CommitInfo() {
		Global	$seq, $l_status, 
						$l_currtech, $l_ctag, 
						$l_problem, 
						$l_summary, $a_name, $l_priority,
						$a_company, $a_email, $a_password, $l_parent,$curruser,$sys_url,
						$sys_lpri,$sys_defpublic;
		$a_spid = ($l_parent) ? "$l_parent" : "0";
#		$a_pri = ($sys_lpri) ? $sys_lpri : 9;
		$i_problem = addslashes($l_problem);
		$i_summary = addslashes($l_summary);
		echo "<tr bgcolor=#000000>\n";
		echo "<td>";
		echo "<table border=0 width=100%><tr>";
		echo "<td bgcolor=#808080>";
		$now = time();
		LogIt("DEBUG","CommitInfo","Updating.  sys_defpublic is '$sys_defpublic'");
		$vlist = "$a_spid,$now,'$curruser','$l_currtech','$l_status',$l_priority,'$l_ctag','$i_problem','$i_summary','$sys_defpublic',$now,'$curruser',";
		# Was the slip both opened and closed in one-shot?
		if ($l_status == "C") {
			$vlist .= "$now,'$curruser'";
		} else {
			$vlist .= "0,''";
		}
		$result = db_query("
			insert into slips 
			(spid,o_date,o_tech,currtech,status,priority,ctag,problem,summary,is_public,l_activity,l_tech,c_date,c_tech) 
			values ($vlist)");
		$newsid = db_insertid($result);
		echo "<p align=center>\n";
		if ($newsid) {
			echo "<font size=5><b>Success!</b></font><p>";
			echo "<p align=center><font size=4>\n";
			echo "Your slip has been added, and assigned slip number <b>$newsid</b><br>";
			echo "</font>";
			echo "<p align=center>\n";
			echo "Information on the slip can be accessed remotely at <b>";
			echo "$sys_url </b>";
		} else {
			echo "<b>FAILED!</b><p>";
			echo "<p align=center>\n";
			echo "There was an error while posting the slip into the<br>";
			echo "system.  Please correct the problem and try again<p>\n";
		}
		echo "</td>";
#		echo "<td bgcolor=#808080>";
#		MailOptions();
#		echo "</td>";
		echo "</tr></table></td>";
		echo "</tr>";
	}

		

	#----------------------------------------------------------------------
	#
	# Okay, figure out what to do with the Sequencing...  Where are we?
	#


	if (! IsSet($seq)) {
		$seq = 1;
	} else {
		if (("$next") && ($seq < 5)) {
			$seq++;
		}
		if (("$prev") && ($seq > 1)) {
			$seq--;
		}
	}
		
	echo "<tr bgcolor=bbbbbb><td align=center height=100%><h3>Add a new slip</td></tr>\n";
	echo "<form method=\"post\" action=\"nbrowse.php3\">\n";

	#
	# In order for the 4 pages to work, we have to preserve the
	# various bits between pages.  Here's where that's done.
	#

	echo "<tr bgcolor=#BBBBBB>\n";
	echo "<td width=100% bgcolor=#202020 align=center>\n";
	ProgressBar();
	echo "</td></tr>";

	#
	# Dump all the 'hidden' fields from the other tables.
	#

	DumpOther();

	if ("$s_tag") {
		$l_ctag = FindContact("tag","$l_ctag");
	}
	if ("$s_name") {
		$l_ctag = FindContact("name","$a_name");
	}
	if ("$s_company") {
		$l_ctag = FindContact("company","$a_company");
	}
	if ("$s_email") {
		$l_ctag = FindContact("email","$a_email");
	}

	switch ($seq) {
		case 1 : {
			GetContact();
			break;;
		}
		case 2 : {
			GetProblem();
			break;;
		}
		case 3 : {
			GetOther();
			break;;
		}
		case 4 : {
			ShowFinal();
			break;;
		}
		case 5 : {
			CommitInfo();
			break;;
		}
	}

	echo "<tr bgcolor=#BBBBBB>\n";
	echo "<td bgcolor=#202020 align=center>\n";
	ButtonBar();
	echo "</td></tr>\n";
	echo "</form>\n";
?>
	

