<?php
	//gotta verify the input is what we expect
	$validInput = true;
	$error = '';
	
	//validate existing sites
	if($existingSites != 'x')
		if(strlen($existingSites)) {
			$existingSites = explode("\n", $existingSites);
			foreach($existingSites as &$existingSite) {
				$existingSite = filter_var($existingSite, FILTER_SANITIZE_NUMBER_INT);
				if(!$existingSite) {
					$error .= 'Please ensure the <b>Existing Site ZIP Codes</b> are entered correctly.<br />';
					$validInput = false;
					break;
				}
			}
			unset($existingSite); //must destroy the reference to the last element
		} else
			$existingSites = array(); //make it an empty array
			
	//validate candidate sites
	if(strlen($candidateSites)) {
		$candidateSites = explode("\n", $candidateSites);
		foreach($candidateSites as &$candidateSite) {
			$candidateSite = filter_var($candidateSite, FILTER_SANITIZE_NUMBER_INT);
			if(!$candidateSite) {
				$error .= 'Please ensure the <b>Candidate Site ZIP Codes</b> are entered correctly.<br />';
				$validInput = false;
				break;
			}
		}
		unset($candidateSite); //must destroy the reference to the last element
		$candidateSites = array_unique($candidateSites); //we don't need or want duplicates
	} else
		$candidateSites = array(); //make it an empty array
		
	//validate state
	$state = filter_var($state, FILTER_SANITIZE_STRING);
	if(is_null($state) || strlen($state) == 0 || $state === 'MULTI-STATE') {
		$error .= 'Please ensure the <b>State</b> is chosen correctly.<br />';
		$validInput = false;
	}
	
	//validate algorithm
	$algorithm = filter_var($algorithm, FILTER_SANITIZE_STRING);
	if(is_null($algorithm) || strlen($algorithm) == 0 || !($algorithm === 'MAXIMAL_COVERAGE_MODEL' || $algorithm === 'KMEDIAN')) {
		$error .= 'Please ensure the <b>Algorithm</b> is chosen correctly.<br />';
		$validInput = false;
	}
	
	//validate number of sites
	$numSites = filter_var($numSites, FILTER_SANITIZE_NUMBER_INT);
	if(is_null($numSites) || (is_bool($numSites) && !$numSites)) {
		$error .= 'Please ensure the <b>Number of Candidate Sites to Find</b> is entered correctly.<br />';
		$validInput = false;
	} else if($numSites < 0 || $numSites > count($candidateSites)) {
		$error .= 'The <b>Number of Candidate Sites to Find</b> must be between 0 and the number of candidate sites entered in the <b>Candidate Site ZIP Codes</b> box.<br />';
		$error .= '&rarr; You entered ' . count($candidateSites) . ' <i>unique</i> candidate site ZIP codes.<br />';
		$error .= "&rarr; You entered $numSites for the number of candidate sites to find.<br />";
		$error .= "&rarr; This algorithm only considers <i>unique</i> ZIP codes.<br />";
		$validInput = false;
	}
	
	//validate radius
	$radius = filter_var($radius, FILTER_SANITIZE_NUMBER_INT);
	if(is_null($radius) || (is_bool($radius) && !$radius)) {
		$error .= 'Please ensure the <b>Radius of Coverage</b> is entered correctly.<br />';
		$validInput = false;
	} else if($radius <= 0) {
		$error .= 'The <b>Radius of Coverage</b> must be greater than 0.<br />';
		$error .= "&rarr; You entered a radius of coverage of $radius miles.<br />";
		$validInput = false;
	}
?>
