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

	#--------------------------------------------------------------------
	# $Id: contacts.php3,v 1.33 2000/12/27 21:59:10 shevett Exp $
	#--------------------------------------------------------------------
	
	include "./contutils.php3";

 	$scquery=db_query("select * from status");
 	while ($row=db_fetch_array($scquery)) {
		$code = $row[flag];
		$value = $row[label];
		$scarray[$code] = $value;
	}


	if ("$state[contzoom]") {
		$wclause = "where tag='$state[contzoom]' ";
	} else {
		# Are filters turned on?  If so, grab the current slip filter out of statelist.
		if ($state[filters] <> 0) {
			$fq = db_query("select sl_column from statelist where sl_login='$curruser' and sl_table='contacts' and sl_flags='FILT'");
			if (db_numrows($fq)) {
				$filtid = db_result($fq,0,0);
				$filtq = db_query("select fselect from filters where _rowid=$filtid");
				$wclause=stripslashes(db_result($filtq,0,0));
			}
		}
	}

	$oclause=(ereg('order',$wclause)) ? "" : GenOrder("contacts");
	$qstring = "select ".
							"tag, name, company, email, passwd, ctype, comment ".
							"from contacts ".
							"$wclause ".
							"$oclause";
	
	LogIt("DEBUG","contacts.php3","Select query is: $qstring");
	$result = db_query("$qstring");
	if (db_numrows($result) == 0) {
		Message("No contacts available. <br>Either table is empty or<br>current Filter is too restrictive.");
	} else {
		LoadColinfo();
		if (! $state[contzoom]) {
			echo "<tr bgcolor=$sys_col[bgmed]>";
			echo "<td>&nbsp;</td>$Colheader";
			echo "</tr>";
			echo "<tr></tr>";
		}
	}
	
	#
	# This is the hack to get around not having a left join in POSTGRES.  Load the current
	# expansion list from the statelist table into 'stinfo'.  Use that as a reference to see
	# if a certain contact needs to be expanded or not.
	#

	$stq = db_query("select * from statelist where sl_login='$curruser' and sl_table='contacts' and (sl_flags is NULL OR (sl_flags='M'))");
	while ($stdata = db_fetch_array($stq)) {
		$stinfo[$stdata[sl_column]] = '1';
	}

	$ShowContCounter = 0;
	$ShowContMax = ($prefs[opt_mxconts]) ? $prefs[opt_mxconts] : $sys_numshownconts ;
	$ShowContAll = db_numrows($result);
	$ShowContBar = 0;
	$flipper = 0;

	while ($row=db_fetch_array($result)) {

		if (! $state[contzoom]) {
			# If we've gotten to this point, then we're really going to display this
			# slip.  start counting rows.

			$ShowContCounter++;
			LogIt("DEBUG","contacts.php3","Contacts counter is $ShowContCounter");

			# Are we running through slips we shouldn't display?

			if ($ShowContCounter < $state[cbgn]) {
				LogIt("DEBUG","contacts.php3","Skipping because ShowContCounter ($ShowContCounter) is less than state[cbgn] ($state[cbgn])");
				continue;
			}

			# Have we reached the max we can display?
	
			if ($ShowContCounter > $ShowContMax+$state[cbgn]) {
				LogIt("DEBUG","contacts.php3","Skipping because ShowContCounter ($ShowContCounter) is greater than ShowContMax+state[cbgn] ($ShowContMax+$state[cbgn])");
				continue;
			}
		}

		if ($state[contzoom]) {
			$bgcol = $sys_col[zoomborderbg]; 
		} else {
			$bgcol = ($flipper) ? $sys_col[flip1] : $sys_col[flip2] ;
		}
		$flipper = (! $flipper) ;

		$tag_cgi = rawurlencode($row[tag]);
		echo "<tr valign=top>";
		echo "<td width=10% bgcolor=$bgcol nowrap>\n";
		if ($stinfo[$row[tag]]) {
			if ($row[tag] == $state[contzoom]) {
				echo "<a href=\"nbrowse.php3?f_collapse=1&f_contact=$tag_cgi&f_tag=$$tag_cgi&f_contzoom=$tag_cgi&f_stflag=M\">";
				echo "<img src=\"$sys_graphics/active-left.gif\" border=0></a>\n";
			} else {
				echo "<a href=\"nbrowse.php3?f_collapse=1&f_contact=$tag_cgi&f_stflag=M\">";
				echo "<img src=\"$sys_graphics/active-down.gif\" border=0></a>\n";
			}
		} else {
			if ($row[tag] == $state[contzoom]) {
				echo "<a href=\"nbrowse.php3?f_expand=1&f_tag=$row[tag]&f_contzoom=$row[tag]&f_stflag=M\">";
				echo "<img src=\"$sys_graphics/active-up.gif\" border=0></a>\n";
			} else {
				echo "<a href=\"nbrowse.php3?f_expand=1&f_contact=$row[tag]&f_stflag=M\">";
				echo "<img src=\"$sys_graphics/active-right.gif\" border=0></a>\n";
			}
		}
		echo "<a href=\"nbrowse.php3?f_contact=$tag_cgi&f_contzoom=$tag_cgi&f_expand=1&f_stflag=M\">$row[tag]</a>\n";
		if ($stinfo[$row[tag]]) {
			echo "<font size=2>\n";
			echo "<p align=right>\n";
			echo "<a href=\"nbrowse.php3?f_contact=$tag_cgi&f_func=addcont\">New Contact</a><br>\n";
			echo "<a href=\"nbrowse.php3?f_contact=$tag_cgi&f_func=modcont\">Modify</a><br>\n";
			echo "<a href=\"nbrowse.php3?f_contact=$tag_cgi&f_func=delcont\">Delete</a>\n";
			echo "<p align=right>\n";
			echo "<a href=\"nbrowse.php3?f_contact=$tag_cgi&f_func=addradd\">New Address</a><br>\n";
			echo "<a href=\"nbrowse.php3?f_contact=$tag_cgi&f_func=addphone\">New Phone number</a><br>\n";
			echo "</p>";
		}
		echo "</td>\n";
		
		if ($stinfo[$row[tag]]) {
			echo "<td bgcolor=$sys_col[detailbg] colspan=".SizeOf($cscol).">";
			echo "<table bgcolor=$sys_col[detailbg] cellpadding=0>";
			echo "<tr>";
			echo "<td><font color=\"$sys_col[detailfg]\">Name:</td>";
			echo "<td><font color=\"$sys_col[detailfg]\"><b>".$row[name]."</b></td>";
			echo "</tr>";
			echo "<tr>";
			echo "<td><font color=\"$sys_col[detailfg]\">Company:</td>";
			echo "<td><font color=\"$sys_col[detailfg]\"><b>".$row[company]."</b></td>";
			echo "</tr>";
			echo "<tr>";
			echo "<td><font color=\"$sys_col[detailfg]\">Email:</td>";
			echo "<td><font color=\"$sys_col[detailfg]\"><b><a href=\"mailto:$row[email]\">".$row[email]."</a></b></td>";
			echo "</tr>";
			echo "<tr>";
			echo "<td><font color=\"$sys_col[detailfg]\">Type:</td>";
			echo "<td><font color=\"$sys_col[detailfg]\"><b>";
			if ($row[ctype] == "C") {
				echo "Contact";
			} else if ($row[ctype] == "M") {
				echo "Manufacturer";
			}
			echo "</b></td>";
			echo "</tr>";
			echo "<tr>";
			echo "<td valign=top><font color=\"$sys_col[detailfg]\">Comments:</td>";
			echo "<td><b>";
			echo ereg_replace("\n","<br>",htmlspecialchars(stripslashes($row[comment]))); 
			echo "</b></td>";
			echo "</tr>";
			echo "<td colspan=2>";
			echo "<hr>";
			$aq = db_query("select * from statelist where sl_login='$curruser' and sl_table='contacts' and sl_flags='A' and sl_column='$row[tag]'");
			$pq = db_query("select * from addresses where tag='$row[tag]' order by label");
			$addrcount = db_numrows($pq) ;
			if (db_numrows($aq)) {
				echo "<a href=\"nbrowse.php3?f_contact=$tag_cgi&f_process=collapseaddr\">";
				echo "<img src=\"$sys_graphics/active-down.gif\" border=0>Addresses</a> ($addrcount)<br>";
				echo "<table bgcolor=$sys_col[bgmed]>";
				echo "<tr bgcolor=$sys_col[bgmed]>";
				echo "<th bgcolor=\"#808080\">&nbsp;</th>\n";
				echo "<th><font size=2>Main</th>";
				echo "<th><font size=2>Label</th>";
				echo "<th><font size=2>Address</th>";
				echo "<th><font size=2>City</th>";
				echo "<th><font size=2>State</th>";
				echo "<th><font size=2>PostCode</th>";
				echo "<th><font size=2>Function</th>";
				echo "</tr>";
				while ($adata = db_fetch_array($pq)) {
					$adbg = ($adflip) ? $sys_col[flip1] : $sys_col[flip2] ;
					$adflip = (! $adflip) ;
					echo "<tr valign=top bgcolor=$adbg>";
					$prefix= ($adata[main]) ? "active-" : "" ;
					echo "<td bgcolor=\"#808080\">&nbsp;</td>";
					echo "<td><a href=\"nbrowse.php3?f_label=$adata[label]&f_process=contaddrsel&f_contact=$tag_cgi\">";
					echo "<img src=\"$sys_graphics/${prefix}circle.gif\" border=0>";
					echo "</a>";
					echo "</td>";
					echo "<td><font size=2>$adata[label]</td>";
					echo "<td><font size=2>$adata[address1]";
					if ($adata[address2]) {
						echo "<br>$adata[address2]";
					}
					echo "</td>";
					echo "<td><font size=2>$adata[city]</td>";
					echo "<td><font size=2>$adata[state]</td>";
					echo "<td><font size=2>$adata[pcode]</td>";
					echo "<td><font size=2>";
					$safelabel = urlencode($adata[labl]);
					echo "<a href=\"nbrowse.php3?f_label=$safelabel&f_func=addredit&f_contact=$tag_cgi\">Edit</a>";
					echo " | ";
					echo "<a href=\"nbrowse.php3?f_label=$safelabel&f_process=addrpost&f_flag=delete&f_contact=$tag_cgi\">Dlte</a>";
					echo "</td>";
					echo "</tr>";
				}
				echo "</table>";
			} else {
				echo "<a href=\"nbrowse.php3?f_contact=$tag_cgi&f_process=expaddr\">";
				echo "<img src=\"$sys_graphics/active-right.gif\" border=0>Addresses</a> ($addrcount)<br>";
			}
			$aq = db_query("select * from statelist where sl_login='$curruser' and sl_table='contacts' and sl_flags='P' and sl_column='$row[tag]'");
			$pq = db_query("select * from phones where tag='$row[tag]' order by label");
			$phonecount = db_numrows($pq);
			if (db_numrows($aq)) {
				echo "<a href=\"nbrowse.php3?f_contact=$tag_cgi&f_process=collapsephones\">";
				echo "<img src=\"$sys_graphics/active-down.gif\" border=0>Phone numbers</a> ($phonecount)<br>";
				echo "<table>";
				echo "<tr bgcolor=$sys_col[bgmed]>";
				echo "<th bgcolor=\"#808080\">&nbsp;</th>\n";
				echo "<th><font size=2>Main</th>";
				echo "<th><font size=2>Label</th>";
				echo "<th><font size=2>Number</th>";
				echo "<th><font size=2>Function</th>";
				echo "</tr>";
				$adflip = 1;
				while ($adata = db_fetch_array($pq)) {
					$adbg = ($adflip) ? $sys_col[flip1] : $sys_col[flip2] ;
					$adflip = (! $adflip) ;
					echo "<tr valign=top bgcolor=$adbg>";
					$prefix= ($adata[main]) ? "active-" : "" ;
					echo "<td bgcolor=\"#808080\">&nbsp;</td>";
					echo "<td><a href=\"nbrowse.php3?f_label=$adata[label]&f_process=contphonesel&f_contact=$tag_cgi\">";
					echo "<img src=\"$sys_graphics/${prefix}circle.gif\" border=0>";
					echo "</a>";
					echo "</td>";
					echo "<td><font size=2>";
					echo "$adata[label]</td>";
					echo "<td><font size=2>$adata[number]</td>";
					echo "<td><font size=2>";
					echo "<a href=\"nbrowse.php3?f_label=$adata[label]&f_func=phonedit&f_contact=$tag_cgi\">Edit</a>";
					echo " | ";
					echo "<a href=\"nbrowse.php3?f_label=$adata[label]&f_process=phonepost&f_flag=delete&f_contact=$tag_cgi\">Dlte</a>";
					echo "</td>";
					echo "</tr>";
				}
				echo "</table>";
			} else {
				echo "<a href=\"nbrowse.php3?f_contact=$tag_cgi&f_process=expphones\">";
				echo "<img src=\"$sys_graphics/active-right.gif\" border=0>Phone numbers</a> ($phonecount)<br>";
			}
			$aq = db_query("select * from statelist where sl_login='$curruser' and sl_table='contacts' and sl_flags='S' and sl_column='$row[tag]'");
			$sq = db_query("select sid,status,o_date,currtech,problem from slips where ctag='$row[tag]' order by sid");
			$slipcount = db_numrows($sq);
			if (db_numrows($aq)) {	
				echo "<a href=\"nbrowse.php3?f_contact=$tag_cgi&f_process=collapsecslips\">";
				echo "<img src=\"$sys_graphics/active-down.gif\" border=0>Slips ($slipcount)</a>";
				echo "<table bgcolor=$sys_col[bghigh]>";
				echo "<tr align=left bgcolor=\"$sys_col[bgmed]\">";
				echo "<th bgcolor=\"#808080\">&nbsp;</th>";
				echo "<th><font size=2>Slip</th>";
				echo "<th><font size=2>Status</th>";
				echo "<th><font size=2>Opened</th>";
				echo "<th><font size=2>Current Tech</th>";
				echo "<th><font size=2>Problem</th>";
				echo "</tr>\n";
				$sflipper=1;
				while ($sdata = db_fetch_array($sq)) {
					$sbgcol = ($sflipper) ? $sys_col[flip1] : $sys_col[flip2] ;
					$sflipper = (! $sflipper) ;
					echo "<tr valign=top bgcolor=\"$sbgcol\">";
					echo "<td bgcolor=\"#808080\">&nbsp;</td>";
					echo "<td><font size=2>";
					echo "<a href=\"nbrowse.php3?f_mdataset=slips&f_slipzoom=$sdata[sid]&f_expand=1&f_sid=$sdata[sid]\">";
					echo "$sdata[sid]</a></td>";
					echo "<td><font size=2>" . $scarray[$sdata[status]] . "</td>";
					echo "<td><font size=2>".date("$sys_datefmt",$sdata[o_date])."</td>";
					echo "<td><font size=2>";
					echo $sdata[currtech] ? $sdata[currtech] : "None";
					echo "</td>";
					echo "<td><font size=2>$sdata[problem]</td>";
					echo "</tr>\n";
				}
				echo "</table>";
			} else {
				echo "<a href=\"nbrowse.php3?f_contact=$tag_cgi&f_process=expcslips\">";
				echo "<img src=\"$sys_graphics/active-right.gif\" border=0>Slips ($slipcount)</a>";
			}
			echo "</td></tr>";
			echo "</table>";
		} else {
			for ($l = 0; $l < SizeOf($cscol) ; $l++) {
				echo "<td bgcolor=$bgcol>";
				echo "<font color=#000000 size=2>";
				$z = $row[$cscol[$l]] ;
				if (ereg("D",$csflags[$l])) {
					$z = ($z > 0) ? date("$sys_datefmt",$z) : "";
				}
				if ($cscol[$l] == "status") {
					$z = $scarray[$z];
				}
				if ("$z") {
					echo "$z";
				} else {
					echo "&nbsp;";
				}
				echo "</td>\n";
			}
		}
		
		echo "</tr>\n";

		if ((($ShowContCounter == $ShowContMax + $state[cbgn]) || ($ShowContCounter == $ShowContAll)) && (! $ShowContBar)) {
			$ShowContBar=1;
			$ccount = SizeOf($cscol) + 2;
			$prevset = $ShowContCounter - (2 * $ShowContMax);
			echo "<tr></tr>\n";
 			echo "<tr valign=top>\n";
			echo "<td colspan=$ccount bgcolor=$sys_col[bghigh] align=center>";
			echo "<table width=100% bgcolor=$sys_col[bghigh]><tr align=top>";
			echo "<td width=33% align=left><font size=2>";
			if ($prevset < 0) { 
				$prevset = 0; 
			}
			if ($state[cbgn] > 0) {
				echo "<a href=\"nbrowse.php3?f_topcont=$prevset\">Prev ";
				echo "<img src=\"$sys_graphics/active-left.gif\" border=0 alt=\"Previous Set\"></a>";
			}
			echo "</td>";
			echo "<td width=33% align=center><font size=2>$state[cbgn] - $ShowContCounter displayed out of $ShowContAll</td>";
			$nextset = $ShowContCounter;
			echo "<td width=33% align=right><font size=2>";
			if ($ShowContCounter <> $ShowContAll) {
				echo "<a href=\"nbrowse.php3?f_topcont=$nextset\">";
				echo "<img src=\"$sys_graphics/active-right.gif\" border=0 alt=\"Next Set\"> Next</a> ";
			}
			echo "</td>";
			echo "</tr>";
			echo "</table></td></tr>";
			continue;
		}

		
	}

?>
