<?php
	// strip mac address to 12 char string
	function strip_mac($mac) {
		$mac = str_replace('-', '', $mac);
		$mac = str_replace(':', '', $mac);
		$mac = str_replace('.', '', $mac);
		$mac = str_replace(',', '', $mac);
		$mac = str_replace(' ', '', $mac);
		$mac = strtoupper($mac);
		
		return ($mac);
	}
	
	// rebuild mac address
	function write_mac($mac) {
		// check for invalid mac
		if (strlen($mac)!=12) {
			return $mac;
		} else {			
			$mac1 = substr($mac, 0, 2);
			$mac2 = substr($mac, 2, 2);
			$mac3 = substr($mac, 4, 2);
			$mac4 = substr($mac, 6, 2);
			$mac5 = substr($mac, 8, 2);
			$mac6 = substr($mac, 10, 2);
			$mac = $mac1 . '-' . $mac2 . '-' . $mac3 . '-' . $mac4 . '-' . $mac5 . '-' . $mac6;
			
			return $mac;
		}
	}
	
	// redirect page
	function header_location($location) {
		return header("location: " . $location);
		exit;
	}
	
	// get location name and that of its parents and return with links to the locations
	function location_name($location_id, $seperator) {
		// create an array
		$location_name = array();
		
		// get location name(s)
		$result = mysql_query("SELECT location_name, parent FROM location WHERE location_id='$location_id'");
		while ($row = mysql_fetch_object($result)) {
			// put this parent before any children
			array_unshift($location_name, $row->location_name);
			
			// repeat
			location_name($row->parent, '.');
		}
		
		// count total no. of found locations
		$location_count = count($location_name); 
		
		// display location for every array value
		for ($i = 0; $i < $location_count; $i++ ) {
 			echo '<a href="locationview.php?location_id=' . $location_id . '">' . $location_name[$i] . '</a>' . $seperator;
		} 
	}
	
	// calculate page for pagination (pagination is used in subnetview.php)
	function page($ip) {
		$iprange = explode('.', $ip);
		$iprange3 = $iprange[2];
		
		return $iprange3;
	}
?>