Location: PHPKode > projects > Myphpim > MyPhPim-01.05/addresses.php3
<?php

//****************************************************************************************
//                Copyright (C) 2000 Koen de Boeve
//
//        This program is free software; you can redistribute it and/or
//        modify it under the terms of the GNU General Public License
//        as published by the Free Software Foundation; either version 2
//        of the License, or (at your option) any later version.
//
//        This program is distributed in the hope that it will be useful,
//        but WITHOUT ANY WARRANTY; without even the implied warranty of
//        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//        GNU General Public License for more details.
//
//        You should have received a copy of the GNU General Public License
//        along with this program; if not, write to the Free Software
//        Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
//
//        Version : MyPhPim-01.05
//        Author : Koen de Boeve
//        Contact: hide@address.com
//**************************************************************************************** 

include "conf/global_conf.inc";

session_start();

$html = new HtmlCl;
$db = new DB;

function is_upload_file($filename) {
        if (!$tmp_file = get_cfg_var('upload_tmp_dir')) {
                $tmp_file = dirname(tempnam('', ''));
        }
        $tmp_file .= '/' . basename($filename);
        /* User might have trailing slash in php.ini... */
        return (ereg_replace('/+', '/', $tmp_file) == $filename);
} 


if ( $menu ) {
	switch ( $menu ) {
		

		case "main":
				echo "<html>\n";
                                echo "<head>\n";
                                echo "<style type=\"text/css\">\n";
                                echo "<!--\n";
                                echo "body { margin: 0 }\n";
                                echo "A{text-decoration:none}\n";
                                echo "-->\n";
                                echo "</style>\n";
				echo "</head>\n";
				echo "<body bgcolor=darkseagreen text=blue vlink=blue alink=blue link=blue marginheight=0>\n";
				echo "<table border=0 width=100%><tr><td align=left><font size=-1>
					<a href=addresses.php3?menu=create target=bodyframe>| New | </a>
					<a href=addresses.php3?menu=pilot target=bodyframe>| Pilot | </a>
					</font></td><td align=right><font size=-1>
					<a href=addresses.php3?menu=sharing target=bodyframe>| Sharing | </a>
					</font></td></tr></table>\n";
				echo "</body>\n";
				echo "</html>\n";
				break;;
		case "pilot" : 
				$form = new FormCl;
				$pdb = new PilotCl;
				if ( $pdbfile ) {
                                        if ( is_upload_file ( $pdbfile ) ) {
                                                copy($pdbfile,$uploadpath . $user . "_" . $pdbfile_name);
						$filename = $uploadpath . $user . "_" . $pdbfile_name;
                                        } else {
                                                echo "Could not load file...<p>\n";
                                        } 
				}
				if ( $addpdb == "ADD" ) {
					$categories = $pdb->GetCategoriesPDB ( $filename );
					$lst = $pdb->GetAddressPDB ( $filename ); 
					//unlink ( $uploadpath . $pdbfile_name );
				}
				if ( !$lst && ! $categories ) {
					$html->htmlheader ( "Palm Pilot",0 );	
					echo "<form enctype=multipart/form-data action=addresses.php3?menu=pilot method=post name=pilot>\n";
					echo "<h3>Load Pilot File ( .pdb )</h3>";
					$form->hidden ( "MAX_FILE_SIZE", "5000000" );
					//$form->hidden ( "menu", "pilot" );
					$form->file ( "pdbfile", 40 );
					//$form->hidden ( "categories", $categories );
                                	$form->submit ( "addpdb", "ADD" );
					$form->close();
					echo "<hr noshade>";
					$html->htmlfooter ( 0 );
				} else {
					$pdb->ShowListPDB ( $filename );
				}	
				break;;
		case "list":
				$form = new FormCl;
				$query = "SELECT user_id FROM users WHERE user='$user'";
				$result = $db->query ( $query );
				$user_id = $db->result ( $result, 0, "user_id" );

				// Create basic query
				
				$querystr = "SELECT *  FROM addresses WHERE owner=$user_id";
				
				// Lets see if we enabled public sharing for addresses
				
				$query = "SELECT sharetype FROM addresses_subscribed WHERE user_id=$user_id AND sharetype='public'";
				$result = $db->query ( $query );
				$numrow = $db->numrows ( $result );
				if ( $numrow != 0 ) {
					$query = "SELECT address_id FROM addresses_shares WHERE sharetype='public'";
					$result = $db->query ( $query );
					$numrows = $db->numrows ( $result );
					for ( $i = 0; $i < $numrows; $i++ ) {
						$addressid = $db->result ( $result , $i, "address_id" );
						$querystr = $querystr . " OR address_id=$addressid";
					}
				}
		
				// From which users do we wish to see addresses?

				$query = "SELECT sharedfrom FROM addresses_subscribed WHERE user_id=$user_id AND sharetype='user'";
				$result = $db->query ( $query );
				$numrows = $db->numrows ( $result );
				for ( $i = 0; $i < $numrows; $i++ ) {
					$sharedfrom = $db->result ( $result, $i, "sharedfrom" );
					$sub_query = "SELECT address_id FROM addresses WHERE owner=$sharedfrom";
					$sub_result = $db->query ( $sub_query );
					$numsub_rows = $db->numrows ( $sub_result );
					for ( $j = 0; $j < $numsub_rows; $j++ ) {
						$address_id = $db->result ( $sub_result, $j, "address_id" );
						$sub_sub_query = "SELECT shareusers FROM addresses_shares WHERE address_id=$address_id";
						$sub_sub_result = $db->query ( $sub_sub_query );
						$numsub_sub_rows = $db->numrows ( $sub_sub_result );
						for ( $k = 0; $k < $numsub_sub_rows; $k++ ) {
							$shareusers = $db->result ( $sub_sub_result, $k, "shareusers" );
							if ( $shareusers == $user_id ) {
								$querystr = $querystr . " OR address_id=$address_id";
							}
						}
					}
				}				
				

				// To which Groups do we belong?
				$query = "SELECT sharedfrom FROM addresses_subscribed WHERE user_id=$user_id AND sharetype='group'";
				$result = $db->query ( $query );
				$numrows = $db->numrows ( $result );
				for ( $i = 0; $i < $numrows; $i++ ) {
					$sharedfrom = $db->result ( $result, $i, "sharedfrom" );
					$sub_query = "SELECT address_id FROM addresses_shares WHERE sharegroups=$sharedfrom";	
					$sub_result = $db->query ( $sub_query );
					$numsub_rows = $db->numrows ( $sub_result );
					for ( $j = 0; $j < $numsub_rows; $j++ ) {
						$address_id = $db->result ( $sub_result, $j, "address_id" );
						$querystr = $querystr . " OR address_id=$address_id";
					}
				}
				if ( !$sort ) {
					$srtstr = "name, firstname, email, company";
				} else {
					switch ( $sort ) {
						case "1" : $srtstr = "name, firstname, email, company";
							   break;;
						case "2" : $srtstr = "email, name, firstname, company";
							   break;;
						case "3" : $srtstr = "company, name, firstname, email";
							   break;;
					}
				}
				if ( $letter ) {
					$uletter = strtoupper ( $letter );
					$lletter = strtolower ( $letter );
					$completter = $uletter . $lletter;
					$querystr = $querystr . " AND name REGEXP \"^[$completter]\"";
				}
				if ( $search ) {
					$querystr = $querystr . " AND $field LIKE \"%$value%\"";
				}
				$querystr = $querystr . " ORDER BY $srtstr";
				if ( $desc == 1 ) {
					$querystr = $querystr . " ASC";
					$desc = 0;
				} else {
					$querystr = $querystr . " DESC";
					$desc = 1;
				}	
				$html->htmlheader ( "Addresses For $user",0 );	
				$result = $db->query ( $querystr );
				$numrows = $db->numrows ( $result );
				echo "<center><table border=0>\n";
				$form->init ( "addresses.php3", "post","addresses" ); 
				$count = 1;
				echo "<tr bgcolor=dimgray><td>&nbsp</td>";
				echo "<td align=center><font size=-1><a CLASS=white href=addresses.php3?menu=list&start=$start&sort=1&desc=$desc>Name, Firstname </a></font></td>";
				echo "<td align=center><font size=-1><a CLASS=white href=addresses.php3?menu=list&start=$start&sort=2&desc=$desc>E-mail</a></font></td>";
				echo "<td align=center><font size=-1><a CLASS=white href=addresses.php3?menu=list&start=$start&sort=3&desc=$desc>Company </a></font></td>";
				echo "<td align=center><font size=-1 color=white>Owner</font></td></tr>";
				if ( !$max ) {
					$max = 10;
				}
				if ( !$start ) {
					$start = 0;
				}
				$end = $start + $max;
				if ( $end > $numrows ) {
					$end = $numrows;
				}
				$prev = $start - $max;
				for ( $i = $start; $i < $end; $i++ ) {
					$addressid = $db->result ( $result, $i, "address_id" );
					$name = $db->result ( $result, $i, "name" );
					$firstname = $db->result ( $result, $i, "firstname" );
					$owner = $db->result ( $result, $i, "owner" );
					$email = $db->result ( $result, $i, "email" );
					$company = $db->result ( $result, $i, "company" );
					if ( !$name ) { $name="&nbsp"; }
					if ( !$firstname ) { $firstname = "&nbsp"; }
					if ( !$email ) { $email = "&nbsp"; }
					if ( !$company ) { $company = "&nbsp"; }
					$query = "SELECT user FROM users WHERE user_id=$owner";
					$res = $db->query ( $query );
					$username = $db->result ( $res, 0, "user" );
					echo "<tr bgcolor=lightgrey><td align=center width=20><font size=-1>";
					echo "<input type=checkbox name=select[$i] value=$addressid>\n";
					echo "</font></td>\n";
					echo "<td width=200><font size=-1><a CLASS=blue href=addresses.php3?menu=detail&address_id=$addressid>$name $firstname</a></font></td>\n";
					echo "<td><font size=-1><a CLASS=blue href=main.php3?menu=compose&to=$email>$email</a></font></td>";
					echo "<td width=200><font size=-1><a CLASS=blue href=addresses.php3?menu=detail&address_id=$addressid>$company</a></font></td>\n";
					echo "<td width=50 align=center><font size=-1 color=dimgray>$username</font></td></tr>";
					$count++;
				}
				$form->hidden ( "numaddresses", $i );
				if ( $end != $numrows ) {
					echo "<tr><td ><font size=-1><a CLASS=blue href=addresses.php3?menu=list&start=$end&sort=$sort&letter=$letter>Next 10</a></td>";
				} else {
					echo "<tr><td>&nbsp;</td>";
				} 
				echo "<td colspan=3 align=center><font size=-1>";
				$form->submit ("deleteaddresses", "Delete" );
				echo "</font></td>\n";
				if ( $start != 0 ) {
					echo "<td ><font size=-1><a CLASS=blue href=addresses.php3?menu=list&start=$prev&sort=$sort&letter=$letter>Previous 10</a></td></tr>";
				} else {
					echo "<tr><td>&nbsp;</td></tr>";
				}		
				echo "<tr><td colspan=4 align=center>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=A>A </a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=B> B</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=C> C</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=D> D</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=E> E</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=F> F</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=G> G</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=H> H</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=I> I</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=J> J</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=K> K</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=L> L</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=M> M</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=N> N</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=O> O</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=P> P</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=Q> Q</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=R> R</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=S> S</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=T> T</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=U> U</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=V> V</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=W> W</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=X> X</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=Y> Y</a>";
				echo "<a CLASS=blue href=addresses.php3?menu=list&sort=$sort&letter=Z> Z</a>";
				echo "</td></tr>";
				$form->close ();
				echo "</table></center>\n";
				// Search widget
				$fields = array ( name, firstname, title, address, city, zip, state, country,
						telhome, telwork, faxwork, telother, mobile, pager, main, email,
						note, birthday, custom1, custom2, custom3, custom4 );
				echo "<center><table border=0 cellspacing=0 cellpadding=0>";
				$form->init ( "addresses.php3", "post","addresses" );
				echo "<tr bgcolor=lightgrey><td><font size=-1><b>SEARCH</b></font></td><td>";
				$form->SimpleSelect ( "field" , $fields, $fields, 0 );
				echo "</td><td>";
				$form->text ( "value", "", 80, 40, 0 );
				echo "</td><td>";
				$form->submit ( "search", "Go" );
				echo "</td></tr>"; 
				$form->hidden ( "menu", "list" );
				$form->close ( );
				echo "</table></center>";
				$html->htmlfooter();
				break;;
		case "create" :
				$query = "SELECT user_id FROM users WHERE user='$user'";
				$result = $db->query ( $query );
				$user_id = $db->result ( $result, 0, "user_id" );
				$form = new FormCl;
				$html->htmlheader ( "Create New Address For $user", 0 );
				echo "<table border=0> \n";
				echo "<tr><td>";
				echo "<table border=0 cellspacing=0 cellpadding=0>\n";
				$form->init ( "addresses.php3", "get", "addresses" );
				echo "<tr><td valign=top><font size=-1>\n";
				$sharetypes = array ( "private", "public" );
				$select[0] = "distribute";
				$form->radio ( "VER", "sharedtype", $sharetypes, NULL );
				echo "<br>";
				$form->radio ( "VER", "sharedtype", $select, NULL ); 
					$mainquery = "SELECT group_id, groupname FROM groups WHERE visible='public' ";
					$query = "SELECT group_id FROM groupmembers WHERE user_id=$user_id";
					$result = $db->query ( $query );
					$numrows = $db->numrows ( $result );
					for ( $i = 0; $i < $numrows; $i++ ) {
						$group_id = $db->result ( $result, $i, "group_id" );
						$mainquery = $mainquery . "OR group_id=$group_id ";
					}
					$mainquery = $mainquery . " ORDER BY groupname";
					$result = $db->query ( $mainquery );
					$numrows = $db->numrows ( $result );
					for  ( $i = 0; $i < $numrows; $i++ ) {
						$groupid[$i] = $db->result ( $result, $i, "group_id" );
						$groupnames[$i] = $db->result ( $result, $i, "groupname" );
					}
					echo "</font></td></tr><tr><td valign=bottom colspan=2><font size=-1><b>groups: </b><br>\n";
					$form->multiselect ( "groupids", $groupid, $groupnames, "NO-ONE", 2);
						
				echo "<br><b>Users:</b><br>\n";
				$query = "SELECT user_id, user FROM users where user!='$user' ORDER BY user";
				$result = $db->query ( $query );
				$numrows = $db->numrows ( $result );
				for ( $i = 0; $i < $numrows; $i++ ) {
					$userid[$i] = $db->result ( $result, $i, "user_id" );
					$username[$i] = $db->result ( $result, $i, "user" );
				}
				$form->multiselect ( "userids", $userid, $username, "NO-ONE", 2 );
				echo "<tr bgcolor=lightgrey><td valign=top><table border=0 cellspacing=0 cellpadding=0>";
					echo "<tr bgcolor=lightgrey><td><font size=-1>Title: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "title", "$title", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Name: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "name", "$name", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Firstname: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "firstname", "$firstname", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Company: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "company", "$company", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Note: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "note", "$note", 80, 20, 0 );
					echo "</small></font></td></tr><tr bgcolor=lightgrey><td><br></td><td>&nbsp;</tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><br></td><td>&nbsp;</tr>\n";
				echo "</table></td>";
				echo "<td valign=top><table border=0 cellspacing=0 cellpadding=0>";
					echo "<tr bgcolor=lightgrey><td><font size=-1>Address: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "address", "$address", 120, 56, 0 );
					echo "</small></font></td>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>City: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "city", "$city", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Zip: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "zip", "$zip", 16, 16, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>State: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "state", "$state", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Country: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "country", "$country", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
				echo "</td></tr></table></td></tr>";
				echo "<tr bgcolor=lightgrey><td><table border=0 cellspacing=0 cellpadding=0>";
					echo "<tr bgcolor=lightgrey><td><font size=-1>Tel Home: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "telhome", "$telhome", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Tel Work: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "telwork", "$telwork", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Fax Work: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "faxwork", "$faxwork", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Mobile: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "mobile", "$mobile", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Other: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "telother", "$telother", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Other 2: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "main", "$main", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Pager: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "pager", "$pager", 80, 20, 0 );
					echo "</small></font>\n"; 
				echo "</td><td>&nbsp;</td></table></td>";
				echo "<td valign=top><table border=0 cellspacing=0 cellpadding=0>";
					echo "<tr bgcolor=lightgrey><td><font size=-1>E-Mail: </font></td><td colspan=2><font size=+1 face=clean><small>\n";
					$form->text ( "email", "$email", 80, 40, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>custom 1: </font></td><td colspan=2><font size=+1 face=clean><small>\n";
					$form->text ( "custom1", "$custom1", 80, 40, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>custom 2: </font></td><td colspan=2><font size=+1 face=clean><small>\n";
					$form->text ( "custom2", "$custom2", 80, 40, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>custom 3: </font></td><td colspan=2><font size=+1 face=clean><small>\n";
					$form->text ( "custom3", "$custom3", 80, 40, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>custom 4: </font></td><td colspan=2><font size=+1 face=clean><small>\n";
					$form->text ( "custom4", "$custom4", 80, 40, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Birthday: </font></td><td><font size=-1>Day: </font><font size=+1 face=clean>";
					$form->text ( "day", $day, 2, 2, 0 );
					echo "</font><font size=-1> Month: </font><font size=+1 face=clean>";
					$form->text ( "month", $month, 2, 2, 0 );
					echo "</font><font size=-1> Year: </font><font size=+1 face=clean>";
					$form->text ( "year", $year, 4, 4, 0 );
					echo "</font></td><td><font size=-1>Notify Birthday: </font></td><td><font size=-1>";
					if ( $notify_birthday == 'Y' ) {
						$form->checkbox ( "notify_birthday", 1, 0 );
					} else {
						$form->checkbox ( "notify_birthday", 0, 0 );
					}	
					echo "</font></td></tr>\n";
				echo "</table></td></tr>";
                                echo "<tr><td><font size=-1>Create this Address <br>For another User:</font></td><td><font size=-1>";
                                $form->checkbox ( "otheruser", 0, 0 );
                                echo "</font></td></tr><tr><td><font size=-1>Users:</font></td><td><font size=-1>";
                                $form->multiselect ( "foruser", $userid, $username, "NO-ONE", 2 );
                                echo "</font></td></tr><tr><td><font size=-1>groups:</font></td><td><font size=-1>";
                                $form->multiselect ( "forgroup", $groupid, $groupnames, "NO-ONE", 2);
                                echo "</font></td></tr><tr><td><font size=-1>comments:</font></td><td colspan=3><font size=-1>";
                                $form->textarea('comments','',60,5 );
				echo "<tr><td colspan=8 align=center><font size=-1>\n";
				$form->submit ( "addaddresses", "Save" );
				echo "</font></td></tr>\n";
				$form->close();
				echo "</table>\n";
				echo "</td></tr></table>";
				$html->htmlfooter();
				break;;
		case "detail" :
				$query = "SELECT user_id FROM users WHERE user='$user'";
				$result = $db->query ( $query );
				$user_id = $db->result ( $result ,0 , "user_id" );
				$query = "SELECT * FROM addresses WHERE address_id=$address_id";
				$result = $db->query ( $query );
				$addressid = $db->result ( $result, $i, "address_id" );
				$name = $db->result ( $result, $i, "name" );
				$firstname = $db->result ( $result, $i, "firstname" );
				$owner = $db->result ( $result, $i, "owner" );
				$address = $db->result ( $result, $i, "address" );
				$city = $db->result ( $result, $i, "city" );
				$zip = $db->result ( $result, $i, "zip" );
				$state = $db->result ( $result, $i, "state" );
				$country = $db->result ( $result, $i, "country" );
				$telhome = $db->result ( $result, $i, "telhome" );
				$telother = $db->result ( $result, $i, "telother" );
				$telwork = $db->result ( $result, $i, "telwork" );
				$faxwork = $db->result ( $result, $i, "faxwork" );
				$main = $db->result ( $result, $i, "main" );
				$pager = $db->result ( $result, $i, "pager" );
				$custom1 = $db->result ( $result, $i, "custom1" );
				$custom2 = $db->result ( $result, $i, "custom2" );
				$custom3 = $db->result ( $result, $i, "custom3" );
				$custom4 = $db->result ( $result, $i, "custom4" );
				$mobile = $db->result ( $result, $i, "mobile" );
				$email = $db->result ( $result, $i, "email" );
				$birthday = $db->result ( $result, $i, "birthday" );
				$notify_birthday = $db->result ( $result, $i, "notify_birthday" );
				$company = $db->result ( $result, $i, "company" );
				$note = $db->result ( $result, $i, "note" );
				$title = $db->result ( $result, $i, "title" );
				$addrupdate = $db->result ( $result, $i, "addrupdate" );
				$addrdelete = $db->result ( $result, $i, "addrdelete" );
                                list ($year, $month, $day) = split ('[-]', $birthday );
				$query = "SELECT sharetype FROM addresses_shares WHERE address_id=$address_id";
				$result = $db->query ( $query ); 
				$numrows = $db->numrows ( $result );
				if ( $numrows != 0 ) {
					if (!$sharedtype ) {
						$sharedtype = $db->result ( $result, 0, "sharetype" );
					}
					$query = "SELECT sharegroups FROM addresses_shares WHERE address_id=$address_id AND sharegroups!=0";
					$result = $db->query ( $query );
					$numrows = $db->numrows ( $result );
					for ( $i = 0; $i < $numrows; $i++ ) {
						$sharedgroup = $db->result ( $result, $i, "sharegroups" );
						$sub_query = "SELECT groupname FROM groups WHERE group_id=$sharedgroup";
						$sub_result = $db->query ( $sub_query );
						$sharedgroupnames[$i] = $db->result ( $sub_result, 0, "groupname" );
					} 
					$query = "SELECT shareusers FROM addresses_shares WHERE address_id=$address_id AND shareusers!=0";
					$result = $db->query ( $query );
					$numrows = $db->numrows ( $result );
					for ( $i = 0; $i < $numrows; $i++ ) {
						$shareduser = $db->result ( $result, $i, "shareusers" );
						$sub_query = "SELECT user FROM users WHERE user_id=$shareduser";
						$sub_result = $db->query ( $sub_query ) ;
						$sharedusernames[$i] = $db->result ( $sub_result, 0, "user" );
					}
				} else {
					$sharedusernames[0]="fake";
					$sharedgroupnames[0]="alsofake";
				}
				$form = new FormCl;
				$html->htmlheader ( "Address For $user", 0 );
				echo "<table border=0 cellpadding=0 cellspacing=0>\n";
				$form->init ( "addresses.php3", "get", "addresses" );
				echo "<tr><td colspan=2 align=left><font size=-1>\n";
				$sharetypes = array ( "private", "public" );
                                $select[0] = "distribute";
				if ( ! $sharedtype ) { $sharedtype = 'private'; }
                                $form->radio ( "VER", "sharedtype", $sharetypes, $sharedtype );
                                echo "<br>";
                                $form->radio ( "VER", "sharedtype", $select, $sharedtype );
				$mainquery = "SELECT group_id, groupname FROM groups WHERE visible='public' ";
                                $query = "SELECT group_id FROM groupmembers WHERE user_id=$user_id";
                                $result = $db->query ( $query );
                                $numrows = $db->numrows ( $result );
                                for ( $i = 0; $i < $numrows; $i++ ) {
                                	$group_id = $db->result ( $result, $i, "group_id" );
                                	$mainquery = $mainquery . "OR group_id=$group_id ";
                                }
                                $mainquery = $mainquery . " ORDER BY groupname";
                                $result = $db->query ( $mainquery );
                                $numrows = $db->numrows ( $result );
                                for  ( $i = 0; $i < $numrows; $i++ ) {
                                	$groupid[$i] = $db->result ( $result, $i, "group_id" );
                                	$groupnames[$i] = $db->result ( $result, $i, "groupname" );
                                }
                                echo "</font></td></tr><tr><td>&nbsp</td><td valign=bottom><font size=-1><b>groups: </b><br>\n";
                                $form->multiselect ( "groupids", $groupid, $groupnames, $sharedgroupnames, 2);

                                echo "<br><b>Users:</b><br>\n";
                                $query = "SELECT user_id, user FROM users ORDER BY user";
                                $result = $db->query ( $query );
                                $numrows = $db->numrows ( $result );
                                for ( $i = 0; $i < $numrows; $i++ ) {
                                        $userid[$i] = $db->result ( $result, $i, "user_id" );
                                        $username[$i] = $db->result ( $result, $i, "user" );
                                }
                                $form->multiselect ( "userids", $userid, $username, $sharedusernames, 2 );

				echo "<tr bgcolor=lightgrey><td valign=top><table border=0 cellspacing=0 cellpadding=0>";
					echo "<tr bgcolor=lightgrey><td><font size=-1>Title: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "title", "$title", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Name: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "name", "$name", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Firstname: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "firstname", "$firstname", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Company: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "company", "$company", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Note: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "note", "$note", 80, 20, 0 );
					echo "</small></font></td></tr><tr bgcolor=lightgrey><td><br></td><td>&nbsp;</tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><br></td><td>&nbsp;</tr>\n";
				echo "</table></td>";
				echo "<td valign=top><table border=0 cellspacing=0 cellpadding=0>";
					echo "<tr bgcolor=lightgrey><td><font size=-1>Address: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "address", "$address", 120, 56, 0 );
					echo "</small></font></td>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>City: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "city", "$city", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Zip: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "zip", "$zip", 16, 16, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>State: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "state", "$state", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Country: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "country", "$country", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
				echo "</td></tr></table></td></tr>";
				echo "<tr bgcolor=lightgrey><td><table border=0 cellspacing=0 cellpadding=0>";
					echo "<tr bgcolor=lightgrey><td><font size=-1>Tel Home: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "telhome", "$telhome", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Tel Work: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "telwork", "$telwork", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Fax Work: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "faxwork", "$faxwork", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Mobile: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "mobile", "$mobile", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Other: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "telother", "$telother", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Other 2: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "main", "$main", 80, 20, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Pager: </font></td><td><font size=+1 face=clean><small>\n";
					$form->text ( "pager", "$pager", 80, 20, 0 );
					echo "</small></font>\n"; 
				echo "</td><td>&nbsp;</td></table></td>";
				echo "<td valign=top><table border=0 cellspacing=0 cellpadding=0>";
					echo "<tr bgcolor=lightgrey><td><font size=-1>E-Mail: </font></td><td colspan=2><font size=+1 face=clean><small>\n";
					$form->text ( "email", "$email", 80, 40, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>custom 1: </font></td><td colspan=2><font size=+1 face=clean><small>\n";
					$form->text ( "custom1", "$custom1", 80, 40, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>custom 2: </font></td><td colspan=2><font size=+1 face=clean><small>\n";
					$form->text ( "custom2", "$custom2", 80, 40, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>custom 3: </font></td><td colspan=2><font size=+1 face=clean><small>\n";
					$form->text ( "custom3", "$custom3", 80, 40, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>custom 4: </font></td><td colspan=2><font size=+1 face=clean><small>\n";
					$form->text ( "custom4", "$custom4", 80, 40, 0 );
					echo "</small></font></td></tr>\n"; 
					echo "<tr bgcolor=lightgrey><td><font size=-1>Birthday: </font></td><td><font size=-1>Day: </font><font size=+1 face=clean>";
					$form->text ( "day", $day, 2, 2, 0 );
					echo "</font><font size=-1> Month: </font><font size=+1 face=clean>";
					$form->text ( "month", $month, 2, 2, 0 );
					echo "</font><font size=-1> Year: </font><font size=+1 face=clean>";
					$form->text ( "year", $year, 4, 4, 0 );
					echo "</font></td><td><font size=-1>Notify Birthday: </font></td><td><font size=-1>";
					if ( $notify_birthday == 'Y' ) {
						$form->checkbox ( "notify_birthday", 1, 0 );
					} else {
						$form->checkbox ( "notify_birthday", 0, 0 );
					}	
					echo "</font></td></tr>\n";
				echo "</table></td></tr>";
				echo "<tr><td colspan=2><fontsize=-1>Allow Others To Update This Item:</font></td></tr>\n";
                                echo "<tr><td>&nbsp</td><td><font size=-1>";
                                if ( $addrupdate == "Y" ) {
                                        $form->checkbox ( "addrupdate", 1, 0 );
                                } else {
                                        $form->checkbox ( "addrupdate", 0, 0 );
                                }
                                echo "</font></td></tr>\n";
                                echo "<tr><td colspan=2><fontsize=-1>Allow Others To Delete This Item:</font></td></tr>\n";
                                echo "<tr><td>&nbsp</td><td><font size=-1>";
                                if ( $addrdelete == "Y" ) {
                                        $form->checkbox ( "addrdelete", 1, 0 );
                                } else {
                                        $form->checkbox ( "addrdelete", 0, 0 );
                                }
                                echo "</font></td></tr>\n";
				echo "<tr><td colspan=8 align=center><font size=-1>\n";
				$form->hidden ( "address_id", $address_id );
				$form->submit ( "updateaddresses", "Save" );
				echo "</font></td></tr>\n";
				$form->close();
				echo "</table>\n";
				$html->htmlfooter();
				break;;
		case "emaillist" :
				$form = new FormCl;
				$html->htmlheader ( "", 0 );
				$html->footer();
				break;;
		case "sharing" :
				$form = new FormCl;
				$html->htmlheader ( "Address Sharings For $user", 0 );
				echo "<b>Active Shares</b><p>\n";
				echo "<table border=0>\n";
				$query = "SELECT user_id FROM users WHERE user='$user'";
				$result = $db->query ( $query );
				$user_id = $db->result ( $result, 0, "user_id" );
				$query = "SELECT * FROM addresses_subscribed WHERE user_id=$user_id";
				$result = $db->query ( $query );
				$numrows = $db->numrows ( $result );
				for ( $i = 0; $i < $numrows; $i++ ) {
					$subscribe_id = $db->result ( $result, $i, "subscrib_id" );
					$sharetype = $db->result ( $result, $i, "sharetype" );
					if ( $sharetype == "group" ) {
						$sharedfrom = $db->result ( $result, $i, "sharedfrom" );
						$query = "SELECT groupname from groups WHERE group_id=$sharedfrom";
						$result1 = $db->query ( $query );
						$sharename = $db->result ( $result1, 0, "groupname" );
						echo "<tr bgcolor=lightgrey><td width=45><font size=-1>$sharetype</font></td>";
						echo "<td width=150><font size=-1>$sharename</font></td>\n";
					} else if ( $sharetype == "user" ) {
						$sharedfrom = $db->result ( $result, $i, "sharedfrom" );
						$query = "SELECT user from users WHERE user_id=$sharedfrom";
						$result1 = $db->query ( $query );
						$sharename = $db->result ( $result1, 0, "user" );
						echo "<tr bgcolor=lightgrey><td width=45><font size=-1>$sharetype</font></td>";
						echo "<td width=150><font size=-1>$sharename</font></td>\n";
					} else {
						echo "<tr bgcolor=lightgrey><td width=45><font size=-1>$sharetype</font></td>";
						echo "<td width=150><font size=-1>&nbsp</font></td>\n";
					}
					echo "<td><font size=-1><a CLASS=blue href=addresses.php3?deletesharing=1&subscrib_id=$subscribe_id> | Remove Share | </a></font></td></tr>\n";
					
						
				}
				echo "</table><hr noshade>";
				echo "<b>Add A Share</b><p>\n";	
						
				$form->init ( "addresses.php3", "post", "addresses" );
				$sharetypes = array ( "group", "user", "public" );
				echo "<font size=-1>Share Type: ";
				if ( !$share_type ) { $share_type = "public" ; }
				$form->SimpleSelect ( "share_type onChange=window.location.href=\"addresses.php3?menu=sharing&share_type=\"+(options[selectedIndex].value)", $sharetypes, $sharetypes, $share_type );
				echo "</font><br>\n";
				if ( $share_type == "group" ) {
					echo "<font size=-1><b>Groups: </b>";
					echo "<select name=sharedfrom size=5>";
					$query = "SELECT group_id, groupname FROM groups WHERE owner=$user_id OR visible='public'";
                                        $tmpquery = "SELECT group_id FROM groupmembers WHERE user_id=$user_id";
                                        $tmpresult  = $db->query ( $tmpquery );
                                        $numrows = $db->numrows ( $tmpresult );
                                        for ( $i = 0; $i < $numrows; $i++ ) {
                                                $group_id = $db->result ( $tmpresult, $i, "group_id" );
                                                $query = $query . " OR group_id=$group_id";
                                        }
                                        $query = $query . " ORDER BY groupname";
				
					//$query = "SELECT group_id, groupname FROM groups WHERE owner=$user_id OR visible='public' ORDER BY groupname";
					$result = $db->query ( $query );
					$numrows = $db->numrows ( $result );
					for ( $i = 0; $i < $numrows; $i++ ) {
						$groupids[$i] = $db->result ( $result, $i, "group_id" );
						$groupnames[$i] = $db->result ( $result, $i, "groupname" );
						echo "<option value=$groupids[$i]>$groupnames[$i]</option>";
					}
					echo "</select></font><br>\n";
				} else if ( $share_type == "user" ) {
					echo "<font size=-1><b>Users: </b>";
					echo "<select name=sharedfrom size=5>";
				
					$query = "SELECT user_id, user FROM users WHERE user!='$user' ORDER BY user";
					$result = $db->query ( $query );
					$numrows = $db->numrows ( $result );
					for ( $i = 0; $i < $numrows; $i++ ) {
						$userids[$i] = $db->result ( $result, $i, "user_id" );
						$usernames[$i] = $db->result ( $result, $i, "user" );
						echo "<option value=$userids[$i]>$usernames[$i]</option>";
					}
					echo "</select></font><br>\n";
				}
				$form->hidden ( "user_id", $user_id ); 
				$form->submit ( "addshare", "Add" );			
				$form->close ();	
				$html->htmlfooter ();
				break;;
	}
}

if  ( $deletesharing ) {
			$query = "DELETE FROM addresses_subscribed WHERE subscrib_id=$subscrib_id";
			$result = $db->query ( $query );
			header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/addresses.php3?menu=sharing" );
}

if ( $addshare ) {
			$query = "SELECT sharetype, sharedfrom FROM addresses_subscribed WHERE user_id=$user_id";
			$result = $db->query ( $query );
			$numrows = $db->numrows ( $result );
			$flag = 0;
			for ( $i = 0; $i < $numrows; $i++ ) {
				$shr = $db->result ( $result, $i, "sharetype" );
				$shrfrm = $db->result ( $result, $i, "sharedfrom" );
				if ( $shr == $share_type && $shrfrm == $sharedfrom ) {
					$flag = 1;
				}
				if ( $shr == "public" && $share_type == "public") {	
					$flag = 1 ;
				}
			}
			if ( $flag == 0 ) {
				$query = "INSERT INTO addresses_subscribed VALUES ( NULL, $user_id, '$share_type', 'Y', '$sharedfrom')";
				$result = $db->query ( $query );
			}
			header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/addresses.php3?menu=sharing" );
			
}

if ( $addaddresses ) {
		if ( $sharedtype == "distribute" ) {
			$splitted = explode ( "&", $QUERY_STRING );
                	$numsplitted = count ( $splitted ) ;
			$j = 0;
			$l = 0;
                	for ( $i = 0 ; $i < $numsplitted; $i++ ) {
                        	if ( ereg ( "^groupids", $splitted[$i] ) ) {
                                	$parts = explode ( "=", $splitted[$i] );
					$end = $numparts - 1 ;
					$groups[$j] = $parts[1];
					$j++;
				} else if ( ereg ( "^userids", $splitted[$i] ) ) {
					$parts = explode ( "=", $splitted[$i] );
					$users[$l] = $parts[1];
					$l++;
				}
			}
		}
		if ( $year ) {
			$birthday = $year . "-" . $month . "-" . $day;
		} else {
			$birthday = 'NULL';
		}
		$vars->date = date ( 'Y-m-d' );
		$query = " SELECT user_id FROM users WHERE user='$user'";
		$result = $db->query ( $query );
		$user_id = $db->result ( $result, 0, "user_id" );
		$vars->name = $name;
		$vars->firstname = $firstname;
		$vars->address = $address;
		$vars->city = $city;
		$vars->zip = $zip;
		$vars->state = $state;
		$vars->country = $country;
		$vars->telhome = $telhome;
		$vars->telother = $telother;
		$vars->telwork = $telwork;
		$vars->faxwork = $faxwork;
		$vars->main = $main;
		$vars->pager = $pager;
		$vars->mobile = $mobile;
		$vars->custom1 = $custom1;
		$vars->custom2 = $custom2;
		$vars->custom3 = $custom3;
		$vars->custom4 = $custom4;
		$vars->email = $email;
		$vars->birthday = $birthday;
		$vars->notify_birthday = $notify_birthday;
		$vars->company = $company;
		$vars->note = $note;
		$vars->title = $title;
                $vars->table = "addresses";
                $vars->numgroups = count ( $groups );
                $vars->numusers = count ( $users );
		$vars->users = $users;
		$vars->groups = $groups;
                $vars->sharedtype = $sharedtype;
		$vars->id_field = "address_id";

		// ####################################################################################
 
                // now lets see if this record should be created for another user
                $vars->otheruser = $otheruser;
                $vars->type='address';
                if ( $otheruser == 'Y' ) {
                        $vars->owner_id = 0; // if we create this for another user, noone owns this record
                                           // until targetted user accepted it
                        $splitted = explode ( "&", $QUERY_STRING );
                        $numsplitted = count ( $splitted ) ;
                        $j = 0;
                        $l = 0;
                        for ( $i = 0 ; $i < $numsplitted; $i++ ) {
                                if ( ereg ( "^forgroup", $splitted[$i] ) ) {
                                        $parts = explode ( "=", $splitted[$i] );
                                        $end = $numparts - 1 ;
                                        $forgroups[$j] = $parts[1];
                                        $j++;
                                } else if ( ereg ( "^foruser", $splitted[$i] ) ) {
                                        $parts = explode ( "=", $splitted[$i] );
                                        $forusers[$l] = $parts[1];
                                        $l++;
                                }
                        }
                        $vars->forusers = $forusers;
                        $vars->forgroups = $forgroups;
                        $vars->comment = $comments;
                        $vars->byuser = $user_id;
                        $db->InsertForUser ( $vars );
                } else {
			$vars->owner_id = $user_id;
                        $last_id = $db->InsertNewRecord ( $vars );
                }
 
                // #################################################################################### 
			
			 
		header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/addresses.php3?menu=list" );  
}
if ( $updateaddresses ) {
		if ( $year != '0000' ) {
			$birthday = $year . "-" . $month . "-" . $day;
		} else {
			$birthday = '';
		}
		if ( $notify_birthday != 'Y' ) {
			$notify_birthday = 'N'; 
		}
		$updatedate = date ( 'Y-m-d' );
		$query = " SELECT user_id FROM users WHERE user='$user'";
		$result = $db->query ( $query );
		$user_id = $db->result ( $result, 0, "user_id" );
		if ( $sharedtype == "distribute" ) {
                       	$splitted = explode ( "&", $QUERY_STRING );
                       	$numsplitted = count ( $splitted ) ;
                       	$j = 0;
                       	$l = 0;
                       	for ( $i = 0 ; $i < $numsplitted; $i++ ) {
                               	if ( ereg ( "^groupids", $splitted[$i] ) ) {
                                       	$parts = explode ( "=", $splitted[$i] );
                                       	$end = $numparts - 1 ;
                                       	$groups[$j] = $parts[1];
                                       	$j++;
                               	} else if ( ereg ( "^userids", $splitted[$i] ) ) {
                                       	$parts = explode ( "=", $splitted[$i] );
                                       	$users[$l] = $parts[1];
                                       	$l++;
                               	}
                       	}
		}
		$query = "UPDATE addresses SET name='$name', firstname='$firstname', address='$address', 
			city='$city', zip='$zip', state='$state', country='$country', telhome='$telhome', 
			telother='$telother', telwork='$telwork', faxwork='$faxwork', mobile='$mobile', 
			title='$title', pager='$pager', main='$main', email='$email', birthday='$birthday', 
			notify_birthday='$notify_birthday', company='$company', note='$note', custom1='$custom1', 
			custom2='$custom2', custom3='$custom3', custom4='$custom4', addrdelete='$addrdelete', 
			addrupdate='$addrupdate' WHERE address_id=$address_id";
			$result = $db->query ( $query );
			$query = "DELETE FROM addresses_shares WHERE address_id=$address_id";
			$result = $db->query ( $query );
			$numgroups = count ( $groups );
			for ( $i = 0; $i < $numgroups; $i++ ) {
				$query = "INSERT INTO addresses_shares VALUES ( NULL, '$sharedtype', '', $groups[$i], $address_id ) ";
				$result = $db->query ( $query );
			}
			$numusers = count ( $users );
			for  ( $i = 0; $i < $numusers; $i++ ) {
				$query = "INSERT INTO addresses_shares VALUES ( NULL, '$sharedtype', $users[$i], '', $address_id ) ";
				$result = $db->query ( $query );
			}
			if ( $sharedtype == "public" ) {
				$query = "INSERT INTO addresses_shares VALUES ( NULL, '$sharedtype', '', '', $address_id ) ";
				$result = $db->query ( $query );
			}
			header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/addresses.php3?menu=detail&address_id=$address_id" );  
}
if ( $deleteaddresses ) {
		$query = "SELECT user_id FROM users WHERE user='$user'";
		$result = $db->query ( $query );
		$user_id = $db->result ( $result, 0, "user_id" );
		//$numselect = count ( $select );
		$numselect = $numaddresses - 1;
		for ( $i = 0; $i <= $numselect; $i++ ) {
			$address_id = $select[$i];
			if ( $address_id ) {
				$query = "SELECT owner FROM addresses WHERE address_id=$address_id";
				$result = $db->query ( $query );
				$owner = $db->result( $result, 0, "owner" );
				if ( $owner == $user_id ) {
					$query = "DELETE FROM addresses WHERE address_id=$address_id";
					$result = $db->query ( $query );
					$query = "DELETE FROM addresses_shares WHERE address_id=$address_id";
					$result = $db->query ( $query );
				}	
			}
		}
		header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/addresses.php3?menu=list" );  
		
}		

if ( $return ) {
	header ( "Location: http://$HTTP_SERVER_VARS[HTTP_HOST]/$basepath/addresses.php3?menu=detail&address_id=$address_id" );  
}
	
?>
Return current item: Myphpim