<?

$ProjectId=$_SESSION[`ProjectId`];
if ($ProjectId==``)$ProjectId=$_REQUEST[`projectid`];

global $rID;
global $rstring;
global $ffn2;

$ffn2=~../upload/NLPC/contacts.vcf~;

//$_REQUEST[`clean`]=1;

$mSubj=`NLPC Contacts`;

$rID=$_REQUEST[~RolesAvailable~];
if ($rID == ~~)
	$rID=999;

if ($rID==999)
	$rstring=~ ~;
else
	$rstring=~ and RoleId=~ . $rID . ~ ~;

//echo ~RolesAvailable=~ , $rID;

$NOSORT=1;

$sql=~select LastName as Last, Firstname as First, TelePhone as Phone,~ .
		~concat(`<a href=mailto:`,email,`>`,email,`</a>`) as Email ,~ .
                ~Street as Street_or_PO_Box_No, City, Region as State, PostalCode as Zip ~.
		~from Users where isDeleted=0 and UserID in ~ .
		~(select UserID from ProjectMembers ~ .
		~where projectid=`~ . $ProjectId . ~`) ~ .
		$rstring .
		~order by LastName, FirstName~;

//echo ~sql:~ , $sql , ~~;

function ToAll()
{
	global $rstring;

	$sql = ~select email from Users where isDeleted=0 and UserID in ~ .
		~(select UserID from ProjectMembers ~ .
		~where projectid=`~ . $ProjectId . ~`) ~ .
		$rstring .
		~order by LastName, FirstName~;

	$res=GetResultset($sql);

    if(!$row = mysql_fetch_assoc($res))
    {
        return false;
    }

	$tComma=~~;
    do
    {
 		$tList= $tList . $tComma . $row[~email~];
 		$tComma=~;~;
    }
    while ($row = mysql_fetch_assoc($res));

	DisConnect();

	return $tList;
}

function loadRoles()
{
	global $rID;

	echo ~<option value=999>All Roles</option>~;

	$sql = ~Select RoleId,RoleName from Roles where isDisabled=0 order by RoleName~;

	$res=GetResultset($sql);

    if(!$row = mysql_fetch_assoc($res))
    {
        return false;
    }

    do
    {
		if ($row[~RoleId~] == $rID)
			echo `<option selected=~yes~ value=~` , $row[~RoleId~] , `~>` ,   $row[~RoleName~], `</option>`;
		else
			echo `<option value=~` , $row[~RoleId~] , `~>` ,  $row[~RoleName~], `</option>`;
    }
    while ($row = mysql_fetch_assoc($res));

	DisConnect();
}
echo `<table class=tdblueS width=100%><tr><td>`;
echo `<table border=0 width=100%><tr><td align=middle>`;

echo `<a href=mailto:`, ToAll(), `><font size=3>Email to Contacts</font></a>`;
echo `</td>`;
echo `<td><a href=~`,$ffn2,`~><font size=3>Export Contacts</font></a></td>`;
echo `<td align=left>`;
echo `<select name=~RolesAvailable~ class=~NormalTextBox~ style=~width:180px;~ onchange=~ReLoadPage();~>`;

loadRoles();

echo `</select>`;
echo `</td>`;
echo `</tr></table>`;
$CELLPADDING=10;
$CELLSPACING=0;
ShowQueryResult($sql);
echo `<br><font size=1>(Only portal account holders appear in the above list)</font>`;
echo `</td></tr></table>`;
echo `<p>`;

echo `</center>`;

?>