<?php

// areacode_chooser.php

function areacode_chooser($csv_loc, $nc_name)
{
	// $csv_loc is the filesystem location fo the CSV file of areacodes and areas
	// $nc_name is the name of the numberchooser to target
	
	// -------------------------------------------------------------------------------------------------

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

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

	// READ IN AREA CODES FROM areacodes.csv
	$ac_data = file($csv_loc);
	foreach ($ac_data as $line)
	{
		$data = explode(',', $line);
		$ac_arr[] = array('code' => $data[0], 'area' => $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']);
	
	// -------------------------------------------------------------------------------------------------
	
	?>
	
	<!-- AREACODE xdriver -->
	<style>
	<!--
	
	#<?=$nc_name;?>_areacode_chooser td.cell1f_thin:hover, #<?=$nc_name;?>_areacode_chooser td.cell2f_thin:hover
	{
		color: #009;
		background-color: #FC6;
	}
	
	#<?=$nc_name;?>_areacode_chooser .left_big, #<?=$nc_name;?>_areacode_chooser .left_small
	{
		display: block;
		width: 270px;
		float: left;
	}
	
	#<?=$nc_name;?>_areacode_chooser .left_small
	{
		width: 100px;
	}
	
	#<?=$nc_name;?>_areacode_chooser
	{
		display: none;
		z-index: 90909;
		position: absolute;
		top: 0px;
		left: 0px;
		background-color: #FFF;
		border: 1px solid;
		padding: 10px;
		margin: 20px;
		overflow: scroll;
		height: 400px;
	}
	
	
	#<?=$nc_name;?>_areacode_chooser #by_code
	{
		display: none;
	}
	
	-->
	</style>
	
	<script language="javascript" type="text/javascript">
	<!--
	// This script is used to populate the NumberChooser specialsequence fields, and trigger a search
	//
	if (typeof xdriver == 'undefined') xdriver = new Object(); // CREATE IN initialise trolley <--- !!!
	xdriver.<?=$nc_name;?> = new Object();
	
	xdriver.<?=$nc_name;?>.areacode_search = function(area, code)
	{
		xdriver.<?=$nc_name;?>.areacode_chooser.setStyle('display', 'none');
		
		var int_suffix = code.substring(1);
		
		var p_arr = new Array();
		p_arr.push([code + ' (' + area + ') numbers only', 	'!44' + int_suffix, 	0, 1]);
		p_arr.push(['None (all UK local numbers)',  		'!44',					0, 0]);
		dmx.<?=$nc_name;?>.set_prefix(p_arr);
	
		dmx.<?=$nc_name;?>.search('Searching for ' + code + ' (' + area + ') numbers');
	}
	
	xdriver.<?=$nc_name;?>.init_areacode = function()
	{
		xdriver.<?=$nc_name;?>.areacode_chooser = $('<?=$nc_name;?>_areacode_chooser');
		xdriver.<?=$nc_name;?>.barea = $('<?=$nc_name;?>_by_area');
		xdriver.<?=$nc_name;?>.bcode = $('<?=$nc_name;?>_by_code');
	}
	
	// Set up page initialisation event handler
	if (window.addEventListener)
	{
		// W3C
		window.addEventListener('load', xdriver.<?=$nc_name;?>.init_areacode, true);
	}
	else if (window.attachEvent)
	{
		// IE  - Quirks Mode
		window.attachEvent('onload', xdriver.<?=$nc_name;?>.init_areacode);
	}
	
	// -->
	</script>
	
	<div id="<?=$nc_name;?>_areacode_chooser">
	
		<p>
		To find numbers for a local area just click on it. This window will close and a number search will be automatically triggered.
		[ <a href="#" onclick="this.blur(); xdriver.<?=$nc_name;?>.barea.setStyle('display', 'block'); xdriver.<?=$nc_name;?>.bcode.setStyle('display', 'none'); return false;">Sort by Location</a> |
		  <a href="#" onclick="this.blur(); xdriver.<?=$nc_name;?>.bcode.setStyle('display', 'block'); xdriver.<?=$nc_name;?>.barea.setStyle('display', 'none'); return false;">Sort by Area Code</a> ]
		</p>
					
		<div id="<?=$nc_name;?>_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 ? 'cell1f_thin' : 'cell2f_thin'); ?>" onclick="xdriver.<?=$nc_name;?>.areacode_search('<?=(trim($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'];?>&Submit=Submit" target="_blank" class="popup">Find adjacent areas using UK Phone Info</a>
				</td>
			</tr>
		<?php $i++; endforeach; ?>
		</table>
		</div>
		
		<div id="<?=$nc_name;?>_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 ? 'cell1f_thin' : 'cell2f_thin'); ?>" onclick="xdriver.<?=$nc_name;?>.areacode_search('<?=(trim($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'];?>&Submit=Submit" target="_blank" class="popup">Find adjacent areas using UK Phone Info</a>
				</td>
			</tr>
		<?php $i++; endforeach; ?>
		</table>
		</div>
		
	</div>
	<!-- END AREACODE xdriver -->
	
	<?php

}

?>

