<?php
switch ($data['whattodo'])
{
	case "Get Record":
	{
    // ------- we have the name of an organisation... show data
	// -------- so open up the database...

    //*connecting, selecting database*/
    $link=database_open();
	// -------- get all the desired entries...
	if (empty($data['namecontains']) && empty($data['idis']))
	{						
     print("<br>You will need to give us more clues than that.<br>");
	 $line == false;
	}
	if (!empty($data['namecontains']) && empty($data['idis']))
	{
	 $query = "SELECT * FROM directory WHERE name LIKE '{$data['namecontains']}%' ORDER BY status DESC";
	 $result = mysql_query($query) or clean_die("Could not execute query.");
	 $line = mysql_fetch_row($result);
	 if ($line[17]<1) $line=false;			
	}			
	if (!empty($data['idis']))
	{
	 $query = "SELECT * FROM directory WHERE id = '{$data['idis']}' ORDER BY status DESC";
	 $result = mysql_query($query) or clean_die("Could not execute query");
	 $line = mysql_fetch_row($result);
	 if ($line[17]<1) $line=false;
	}					

	mysql_close($link);	
	if ($line==false)
	{
?>

<p>No record matched.</p>
<form name="update_another" method="post"  action="<?echo $PHP_SELF?>">
<input type=submit class=go name="whattodo" value="Find another record"><br>
</form>

<?php }
	else
	{
	// ------ say if there's an update already waiting

	if ($line[17] == 5)	print("<br>There is already an update pending for this entry. The updated entry is shown below.<br>");

	// -------- display the first in a form & give options

	$c_list =  array_merge( $countries['Europe'], $countries['The Carribean'], $countries['South America'], $countries['Africa'], $countries['Asia'], $countries['Middle East'], $countries['North/Central America'], $countries['Oceania']);
	sort ($c_list);
			
?>
<form name="update_submit" method="post" action="<?echo $PHP_SELF?>">

<table>
<tr>
<td width=30% align=right><I>Name of the group</I></td>
<td width = 70%>
<input type=text class=text name="name" size="43"<?php print(" value = \"{$line[1]}\">\n");
print("<input type = hidden name = \"status\" value = \"{$line[17]}\">");
print("<input type = hidden name = \"id\" value = \"{$line[0]}\">");
print("<input type = hidden name = \"comments\" value = \"{$line[19]}\">");
?>
</td></tr>
	
<tr><td width=30% align=right><I>Address</I></td>
<td width = 70%><textarea name="location" cols=40 rows=3><?php
print($line[4]);
?></textarea></td> </tr>

<tr><td width=30% align=right>Country</td>
<td width = 70%><select name="country">

<?php

$found = false;
foreach ($c_list as $land)
{
 print ("<option name = country ");
 if ($land==$line[6]) 
 {
  print("selected ");
  $found = true;
 }
 print ("value=\"{$land}\">{$land}</option>\n");
}
 if ($found == false)
 {
 print ("<option name = country selected value = \"\">Invalid Country: {$line[6]}</option>\n");
			}
			?>

</select>
</td> </tr>

<tr> <td align = right><I>Region</I></td>
<td>
			<?php
			if (in_array($line[6], array_keys($regions)))
			{
			print("<table><tr><td>");
				$numregions = sizeof($regions[$line[6]]);
				$colleng = ceil($numregions/3);
				for ($i=0; $i<$colleng; $i++)
				{
					$region = $regions[$line[6]][$i];
					print("<input type = radio name = \"region\" value = \"{$region}\"");
					if ($region == $line[5])
					{
						print(" checked");
					}
					print("> {$region}<br>\n");
				}
				print("</td>\n<td>\n");
				for ($i=$colleng; $i<(2*$colleng); $i++)
				{
					$region = $regions[$line[6]][$i];
					print("<input type = radio name = \"region\" value = \"{$region}\"");
					if ($region == $line[5])
					{
						print(" checked");
					}
					print("> {$region}<br>\n");
	
				}
				print("</td>\n<td>\n");
				for ($i=(2*$colleng); $i<$numregions; $i++)
				{
					$region = $regions[$line[6]][$i];
					print("<input type = radio name = \"region\" value = \"{$region}\"");
					if (region == $line[5])
					{
						print(" checked");
					}
					print("> {$region}<br>\n");					
				}

				print("</td></tr></table>\n");	
			}
			else
			print("Not applicable for this country");
			?>
</td></tr>

<tr> <td width=30% align=right><I>Contact Name</I></td>
<td width = 70%><input type=text class=text name="contact" size = "43"
			<?php
			print(" value = \"{$line[2]}\"");
			?>
></td> </tr>

<tr> <td width=30% align=right><I>Telephone no</I></td>
<td width = 70%><input type=text class=text name="tel" size="43"
			<?php
			print(" value = \"{$line[7]}\"");
			?>
></td> </tr>

<tr> <td width=30% align=right><I>Fax no</I></td>
<td width = 70%><input type=text class=text name="fax" size="43"
			<?php
			print(" value = \"{$line[8]}\"");
			?>
></td> </tr>

<tr> <td width=30% align=right><I>E-mail</I></td>
<td width = 70%><input type=text class=text name="email" size="43"
			<?php
			print(" value = \"{$line[9]}\"");
			?>
></td> </tr>

<tr> <td width=30% align=right><I>Web address</I></td>
<td width = 70%><input type=text class=text name="web" size="43"
			<?php
			if (substr($line[10],0,4) == "www.") $line[10] = "http://".$line[10];
			print(" value = \"{$line[10]}\">");
			if ((!empty($line[10])) && (substr($line[10],0,7)!="http://")) print("<br> Warning -- web addresses should begin with \"http://\"");
			?>
</td> </tr>

<tr> <td width=30% align=right valign=top><I>Year founded<br></I></td>
<td width = 70%><input type=text class=text name="founded" size = "43"
			<?php
			if (!empty($line[11])) print(" value = \"{$line[11]}\"");
			?>
></td> </tr>

<tr> <td width=30% align=right valign=top><I>Area(s) of focus:</I></td>
<td width = 70%>
<table>
<tr><td align = left>
<!--
<input type=checkbox name="focus[]" value="AIRT"
			<?php
			if (stristr($line[12],"AIRT")) print(" checked");
			?>
> Air Transport<br>
-->
<input type=checkbox name="focus[]" value="TECH"
			<?php
			if (stristr($line[12],"TECH")) print(" checked");
			?>
> Alternative Technologies<BR>
<input type=checkbox name="focus[]" value="CORP"
			<?php
			if (stristr($line[12],"CORP")) print(" checked");
			?>
> Anti-Corporate/Globalisation<BR>
<input type=checkbox name="focus[]" value="CARF"
			<?php
			if (stristr($line[12],"CARF")) print(" checked");
			?>
> Car-Free/Anti-Car<BR>
<input type=checkbox name="focus[]" value="CITI"
			<?php
			if (stristr($line[12],"CITI")) print(" checked");
			?>
> Cities & Public Space<BR>
<input type=checkbox name="focus[]" value="CLIM"
			<?php
			if (stristr($line[12],"CLIM")) print(" checked");
			?>
> Climate Change<BR>
<input type=checkbox name="focus[]" value="CYCL"
			<?php
			if (stristr($line[12],"CYCL")) print(" checked");
			?>
> Cycling<BR>
<!--
<input type=checkbox name="focus[]" value="INDU"
			<?php
			if (stristr($line[12],"INDU")) print(" checked");
			?>
> Oil/Auto Industries<BR>
-->
</td><td align=left>
<input type=checkbox name="focus[]" value="PEDE"
			<?php
			if (stristr($line[12],"PEDE")) print(" checked");
			?>
> Pedestrians/Walking<BR>
<!--<input type=checkbox name="focus[]" value="POLL"
			<?php
			if (stristr($line[12],"POLL")) print(" checked");
			?>
> Pollution/Heath<BR>
-->
<input type=checkbox name="focus[]" value="PUBT"
			<?php
			if (stristr($line[12],"PUBT")) print(" checked");
			?>
> Public Transport<BR>
<!--
<input type=checkbox name="focus[]" value="RESD"
			<?php
			if (stristr($line[12],"RESD")) print(" checked");
			?>
> Resource Depletion<BR>
-->
<input type=checkbox name="focus[]" value="RDBL"
			<?php
			if (stristr($line[12],"RDBL")) print(" checked");
			?>
> Road Building/Habitat Protection<BR>
<input type=checkbox name="focus[]" value="RDSF"
			<?php
			if (stristr($line[12],"RDSF")) print(" checked");
			?>
> Road Safety/Road Deaths<BR>
<input type=checkbox name="focus[]" value="TRAF"
			<?php
			if (stristr($line[12],"TRAF")) print(" checked");
			?>
> Traffic Reduction/Calming<BR>
</td></tr></table>
</td> </tr>

<tr> <td width=30% align=right valign=top><I>Group Description:<br></I></td>
<td width = 70%><textarea name="description" wrap=virtual cols=40 rows=5><?php
			print($line[3]);
			?></textarea></td> </tr>

<tr></tr>

<tr> <td width=30% align=right valign=top><I>Language(s):</I></td>
<td width = 70%><input type=text class=text name="languages" size = "43"
			<?php
			print(" value = \"{$line[13]}\"");
			?>
></td> </tr>

<tr></tr>

<tr> <td width=30% align=right  valign=top><I>Activities:</I></td>
<td width = 70% align=left>
<input type=checkbox name="method[]" value="PLAN"
			<?php
			if (stristr($line[14],"PLAN")) print(" checked");
			?>
> Consultancy/Urban Planning<BR>
<input type=checkbox name="method[]" value="DIRA"
			<?php
			if (stristr($line[14],"DIRA")) print(" checked");
			?>
> Direct Action (Protests)<br>
<input type=checkbox name="method[]" value="EDUC"
			<?php
			if (stristr($line[14],"EDUC")) print(" checked");
			?>
> Education/Training<BR>
<input type=checkbox name="method[]" value="MONI"
			<?php
			if (stristr($line[14],"MONI")) print(" checked");
			?>
> Industry Monitoring<BR>
<input type=checkbox name="method[]" value="LOBB"
			<?php
			if (stristr($line[14],"LOBB")) print(" checked");
			?>
> Lobbying/Political Advocacy<BR>
<input type=checkbox name="method[]" value="MEDI"
			<?php
			if (stristr($line[14],"MEDI")) print(" checked");
			?>
> Information (Publications, websites)<BR>
<input type=checkbox name="method[]" value="INFO"
			<?php
			if (stristr($line[14],"INFO")) print(" checked");
			?>
> Public Media Campaigns<BR>
<input type=checkbox name="method[]" value="RESE"
			<?php
			if (stristr($line[14],"RESE")) print(" checked");
			?>
> Research/Scientific Studies<BR>
</td> </tr>

<tr></tr>

<tr> <td width=30% align=right valign=top><I>Geographic Scope:</I></td>
<td width = 70% align=left>
<input type=radio name="extent" value = "L"
			<?php
			if (stristr($line[15],"L")) print(" checked");
			?>
> Local/Regional<BR>
<input type=radio name="extent" value = "N"
			<?php
			if (stristr($line[15],"N")) print(" checked");
			?>
> National<BR>
<input type=radio name="extent" value = "I"
			<?php
			if (stristr($line[15],"I")) print(" checked");
			?>
> International<BR>
</td></tr>

<tr></tr>

<tr> <td width=30% align=right valign=top><I>Type of Organisation:</I></td>
<td width = 70% align=left>
<input type=radio name="org is" value = "N"
			<?php
			if (stristr($line[16],"N")) print(" checked");
			?>
> Non-profit<BR>
<input type=radio name="org is" value = "G"
			<?php
			if (stristr($line[16],"G")) print(" checked");
			?>
> Governmental<BR>
<input type=radio name="org is" value = "C"
			<?php
			if (stristr($line[16],"C")) print(" checked");
			?>
> Commercial<BR>
<input type=radio name="org is" value = "A"
			<?php
			if (stristr($line[16],"A")) print(" checked");
			?>
> Academic<BR>
<input type=radio name="org is" value = "W"
			<?php
			if (stristr($line[16],"W")) print(" checked");
			?>
> Website (blog, etc.)<BR>
<input type=radio name="org is" value = "O"
			<?php
			if (stristr($line[16],"O")) print(" checked");
			?>
> Other<BR>
</td></tr>

<tr></tr>
<tr>
<td align= center><input type=submit class=go name="whattodo" value="Submit Changes"></td>
<td align = center><input type=submit class=go name="whattodo" value="Forget About It"></td>
</tr>
</table>
</form>
<?php
		}
	}
	break;
	case "Submit Changes":
	{		
			/*connecting, selecting database*/
		$link=database_open();

				// -------- load data back into the database and acknowledge.
		if ($data['status']==5)
		{
					// --------- if we're updating an update, delete old update...
			$query = "DELETE FROM directory WHERE id = {$data['id']} AND status = '5'";
			$result = mysql_query($query) or clean_die("Could not execute query");
		}

				// -------- now load the whole thing in as an update pending...

		$focus = "";
		if (array_key_exists('focus', $data))
		{
			foreach($data['focus'] as $temp) {$focus = $focus . $temp . " ";}
		}

		$methods = "";
		if (array_key_exists('method', $data))
		{
			foreach($data['method'] as $temp) {$methods = $methods . $temp . " ";}
		}

		$today=date('Y-m-d');
		$data['comments'] = $data['comments'] . "Update submitted {$today}. ";

		if (trim($data['founded']) == "") $data['founded'] = "NULL";
		else $data['founded'] = "'" . $data['founded'] . "'";

				// ------- build query, set status and save	
		$query = "INSERT into directory values ('{$data['id']}', '{$data['name']}', '{$data['contact']}', '{$data['description']}', '{$data['location']}', '{$data['region']}', '{$data['country']}', '{$data['tel']}', '{$data['fax']}', '{$data['email']}', '{$data['web']}', {$data['founded']}, '{$focus}', '{$data['languages']}', '{$methods}', '{$data['extent']}', '{$data['org_is']}', '5', '{$today}', '{$data['comments']}', '{$data['webcomments']}')";
		$result = mysql_query($query) or clean_die("Could not execute query");

		/* Free resultset & close connection*/
		mysql_close($link);?>
<br>
Your update is now awaiting review. Thanks for taking care of your entry. We love you for it.<br>
<br>
<form name="update_another" method="post" action="<?echo $PHP_SELF?>">
<input type=submit class=go name="whattodo" value="Find another record"><br>
</form>

<?php
	}
	break;
	default:
	{
     // -------- give entry dialogue - select organisation
	?>
<p>To request a change be made to your record, enter the number of your organisation's entry in this database. Changes will be reviewed by World Carfree Network staff, and will not be visible immediately. To request an entry be deleted entirely, please e-mail <SCRIPT TYPE="text/javascript">
  email1=('web'); email2=('worldcarfree.net')	
  document.write(
    '<A href="mailto:' + email1 + '&#64;' + email2 + '">' 
    + email1 + '&#64;' + email2 + '</a>'
  )
</SCRIPT>.
</p>
<p>If you do not know your record number, first do a search. Your number is in the bottom left corner of your listing.</p>

<form name="update_get" method="post" action="<?echo $PHP_SELF?>">
<p>Registration number:
<input type=hidden name="whattodo" value="Get Record">
<input type=text class=text name="idis" size="6">
<br>
<br><input type=submit class=go name="whattodo" value="Get Record">
</form>
	<?php
	}
	break;
}
?>
