<?php

// sp_areacode_content.php
// Loaded using AJAX into area code chooser

define('MOD_PHP', substr(PHP_SAPI, 0, 3) != 'cgi'); // Are we running as mod_php?
define('DOCROOT', 
	MOD_PHP && isset($_SERVER['DOCUMENT_ROOT']) ? 
		$_SERVER['DOCUMENT_ROOT'] : 
		(
			MOD_PHP && isset($_SERVER['SCRIPT_FILENAME']) ? 
				substr($_SERVER['SCRIPT_FILENAME'], 0, 0-strlen($_SERVER['PHP_SELF'])) : 
				(
					substr(str_repeat('../', count(explode('/', preg_replace('_/+_', '/', $_SERVER['PHP_SELF'])))-2), 0, -1) != '' ?
						substr(str_repeat('../', count(explode('/', preg_replace('_/+_', '/', $_SERVER['PHP_SELF'])))-2), 0, -1) :
						'.'
				)
		)
	);

// READ IN AREA CODES FROM areacodes.csv
$ac_data = file(DOCROOT . '/dmx/templates/options/numberchooser_areacodes.csv');
foreach ($ac_data as $line)
{
	$data = explode(',', $line);
	$ac_arr[] = array('code' => $data[0], 'area' => trim($data[1]));
}
uasort($ac_arr, 'sort_by_area'); $areacodes['by_area'] = $ac_arr;
uasort($ac_arr, 'sort_by_code'); $areacodes['by_code'] = $ac_arr;
reset($areacodes['by_area']);
reset($areacodes['by_code']);

// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------
// -------------------------------------------------------------------------------------------------

function sort_by_area($a, $b)
{
	return strnatcmp($a['area'], $b['area']);
}

// -------------------------------------------------------------------------------------------------

function sort_by_code($a, $b)
{
	return strnatcmp($a['code'], $b['code']);
}

// -------------------------------------------------------------------------------------------------

?>

<p>
To find numbers for a local area just click on it. This window will close and a number search will be automatically triggered.<br />
[ <a href="#" onclick="this.blur(); barea.style.display = 'block'; bcode.style.display = 'none';  return false;">Sort by Location</a> |
  <a href="#" onclick="this.blur(); barea.style.display = 'none';  bcode.style.display = 'block'; return false;">Sort by Area Code</a> ]
</p>

<div id="scroll">
	<div id="by_area">
	<table class="padded_table_thin" style="width: 100%;">
	<?php $i=0; foreach($areacodes['by_area'] as $areacode) : ?>
		<tr>
			<td class="<?php echo($i % 2 ? 'col1f' : 'col2f'); ?>" onclick="abs_areacode_search('<?=$areacode['area'];?>', '<?=$areacode['code'];?>'); return false;">
				<span class="left_big"><?=$areacode['area'];?></span>
				<?=$areacode['code'];?>
			</td>
			<td>
				<a href="http://www.ukphoneinfo.com/cgi-bin-Phones/nng?GNG=<?=$areacode['code'];?>&amp;Submit=Submit" target="_blank" class="popup">Find adjacent areas using UK Phone Info</a>
			</td>
		</tr>
	<?php $i++; endforeach; ?>
		<tr>
			<td width="60%"></td>
			<td></td>
		</tr>
	</table>
	<br />
	</div>
	
	<div id="by_code">
	<table class="padded_table_thin" style="width: 100%;">
	<?php $i=0; foreach($areacodes['by_code'] as $areacode) : ?>
		<tr>
			<td class="<?php echo($i % 2 ? 'col1f' : 'col2f'); ?>" onclick="abs_areacode_search('<?=$areacode['area'];?>', '<?=$areacode['code'];?>'); return false;">
				<span class="left_small"><?=$areacode['code'];?></span>
				<?=$areacode['area'];?>
			</td>
			<td>
				<a href="http://www.ukphoneinfo.com/cgi-bin-Phones/nng?GNG=<?=$areacode['code'];?>&amp;Submit=Submit" target="_blank" class="popup">Find adjacent areas using UK Phone Info</a>
			</td>
		</tr>
	<?php $i++; endforeach; ?>
		<tr>
			<td width="60%"></td>
			<td></td>
		</tr>
	</table>
	<br />
	</div>
</div>

<script language="javascript">

barea = $('by_area');
bcode = $('by_code');

</script>
