<?php

if (isset($data['searchnow']))
{ 
if (!isset($order) || $order=="name")
 { $order="name";
   $order1="country";
 }
if ($order=="country")
 { $order="country, name";
   $order1="name";
 }

// ------compile our query...
$link = database_open();


// ----------- browse query
if(isset($data['letter'])) {

if (ereg("^[A-Z]",$letter)) {
  $query = "SELECT name, country, description, id, region FROM directory WHERE name REGEXP \"^$letter\" AND status=1 ORDER BY $order";} else {
  $query = "SELECT name, country, description, id, region FROM directory WHERE name REGEXP \"^[^A-Z]\" AND status=1 ORDER BY $order";
}
}

//-- else Search query
else {

$query = "SELECT name, country, description, id, region FROM directory WHERE ";

if(isset($data['id'])) $query = $query . "id = {$id} AND ";

if (isset($data['namecontains'])) $query = $query. "name LIKE '%{$data['namecontains']}%' AND ";

if (isset($data['city'])) $query = $query. "address LIKE '%{$data['city']}%' AND ";

if (isset($data['extentis']) && empty($data['extentis']))
{
	$temp="(extent = '";
	foreach($data['extentis'] as $extentis)
	{
		$temp = $temp. $extentis. "' || extent = '";
	}
	$temp=substr($temp,0,-14);
	$query = $query . $temp . ") AND ";
}

if (isset($data['groupis']) && !in_array('blank',$data['groupis']))
{
	$temp="(type = '";
	foreach($data['groupis'] as $groupis)
	{
		$temp = $temp. $groupis. "' || type = '";
	}
	$temp=substr($temp,0,-12);
	$query = $query . $temp . ") AND ";
}

if (!empty($data['descriptioncontains'])) $query = $query. "description LIKE '%{$data['descriptioncontains']}%' AND ";

if (isset($data['fociare']) && !in_array('blank',$data['fociare']))
{
	$temp="(focus LIKE '%";
	foreach($data['fociare'] as $focusis)
	{
		$temp = $temp. $focusis. "%' || focus LIKE '%";
	}
	$temp=substr($temp,0,-17);
	$query = $query . $temp . ") AND ";
}

if (isset($data['methodsare']) && !in_array('blank',$data['methodsare']))
{
	$temp="(methods LIKE '%";
	foreach($data['methodsare'] as $methodsare)
	{
		$temp = $temp. $methodsare. "%' || methods LIKE '%";
	}
	$temp=substr($temp,0,-19);
	$query = $query . $temp . ") AND ";
}

if (empty($data['country']) && !empty($data['superregion']))
{
	$data['country']= array();
	if (in_array("Europe", $data['superregion']))
	{
		$data['country'] = array_merge($data['country'], $countries['Europe']);
	}
	if (in_array("Asia", $data['superregion']))
	{
		$data['country'] = array_merge($data['country'], $countries['Asia']);
	}
	if (in_array("The Carribean", $data['superregion']))
	{
		$data['country'] = array_merge($data['country'], $countries['The Carribean']);
	}
	if (in_array("South America", $data['superregion']))
	{
		$data['country'] = array_merge($data['country'], $countries['South America']);
	}
	if (in_array("North/Central America", $data['superregion']))
	{
		$data['country'] = array_merge($data['country'], $countries['North/Central America']);
	}
	if (in_array("Africa", $data['superregion']))
	{
		$data['country'] = array_merge($data['country'], $countries['Africa']);
	}
	if (in_array("Oceania", $data['superregion']))
	{
		$data['country'] = array_merge($data['country'], $countries['Oceania']);
	}
	if (in_array("Middle East", $data['superregion']))
	{
		$data['country'] = array_merge($data['country'], $countries['Middle East']);
	}
}

if (!empty($data['country']))
{
	$temp="(country = '";
	foreach($data['country'] as $country)
	{
		$temp = $temp. $country. "' || country = '";
	}
	$temp=substr($temp,0,-15);
	$query = $query . $temp . ") AND ";
}

if (!empty($data['region']))
{
	$temp="(region = '";
	foreach($data['region'] as $region)
	{
		$temp = $temp. $region. "' || region = '";
	}
	$temp=substr($temp,0,-14);
	$query = $query . $temp . ") AND ";
}

if (!empty($data['languageis'])) $query = $query. "langs LIKE '%{$data['languageis']}%' AND ";

$query = $query . "status=1 ORDER BY $order";
}
//--end search query


// ------------ execute our query
$result = mysql_query($query) or die(mysql_error());

// ........and print our results
$numgroups = mysql_num_rows($result);

if ($numgroups == 0)
{
	print("<p>No Groups Matched. Try a less restrictive search. There may be search terms left from previous searches. You may wish to <a href=\"Javascript:resetForm()\" onClick=\"changeTabs(1)\">clear</a> the form and start anew.\n");
}
else
{
	print("<!-- Found Data -->\n");
$numgroups = mysql_num_rows($result);

// ------ Excel function
for ($i = 0; $i < $numgroups; $i++)
	{$export .= "id=" . mysql_result($result,$i,3);
	 if ($i < ($numgroups-1)) $export .= " || ";
	}
$result = mysql_query($query) or die(mysql_error());
// ------ end Excel function

if ($order=="country, name") $order="country";
print("<p>{$numgroups} groups matched, ordered by $order &#150; <a href=\"export.php?export=$export\" target=\"_new\">Download</a> contact information as Excel file</p>");

while ($line = mysql_fetch_row($result))
	{
	    print "\t<hr>\n";
    	print ("<a href = \"details.php?{$line[3]}\" target=\"details\" onClick=\"changeTabs(3)\"><font size=-1>{$line[0]}</a>");
        if ($line[4]=="United States" || $line[4]=="Canada") print (", {$line[4]}<BR>\n");
		else {
		if (!empty($line[1])) print (", {$line[1]}<BR>\n");
		else print ("<BR>"); }
		if (strlen($line[2])>100) {
			$descrip = substr($line[2], 0, 100) . "... <a href = \"details.php?{$line[3]}\" target =\"details\" onClick=\"changeTabs(3)\">(more)</a>";
		}
		else
		{
			$descrip = $line[2];
		}
		if (empty($line[2])) $descrip="-";
    	print ("{$descrip}</font></p>\n");
	}
}

/* Free resultset &  close*/
mysql_free_result($result);
mysql_close($link);

printf("<hr><p><font size=-1>This directory is continually being updated and improved. Please help us to improve it by reporting any faults or comments to <SCRIPT TYPE=\"text/javascript\">
  email1=('web'); email2=('worldcarfree.net')	
  document.write(
    '<A href=\"mailto:' + email1 + '&#64;' + email2 + '\">' 
    + email1 + '&#64;' + email2 + '</a>'
  )
</SCRIPT>.</font></p>");

} 

else printf("<p>There is currently no data to display, please do a search first.");
?>