<?php 
$locations=urlencode($_GET['locations']);
$locations=stripslashes($locations);
$locations=str_replace(',', '', $locations);

$file = 'http://a4570108784.api.wxbug.net/getLocationsXML.aspx?ACode=a4570108784&SearchString=' . $locations . '&OutputType=1';

$fh = Loader::helper('file');
$xml = $fh->getContents($file, $timeout = 5);

xml2list($xml);

function xml2list($file) {
if (strtolower(substr($file, -4, 4)) == '.xml') {
	$xml = @file_get_contents($file);
} else {
	$xml = $file;
}

if ($xml) {
	//$data = preg_replace(">"."[[:space:]]+"."<","><",$xml);
	$data = preg_replace("#>[[:space:]]+<#","><",$xml);
	$p = xml_parser_create();
	xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
	xml_parse_into_struct($p, $data, $vals, $index);
	xml_parser_free($p);
	$i = 0;
	$tree   = array();
	$c		= 0;
	$size_of_vals = sizeof($vals);

	while (++$i < $size_of_vals) {

	switch ($vals[$i]['level']) {
		case '3':
		switch ($vals[$i]['type']) {
			case 'cdata':
				break;
			case 'complete':
				$tree[$c] = array(
					'cityname' => $vals[$i]['attributes']['cityname'],
					'statename' => $vals[$i]['attributes']['statename'],
					'countryname' => $vals[$i]['attributes']['countryname'],
					'zipcode' => $vals[$i]['attributes']['zipcode'],
					'citycode' => $vals[$i]['attributes']['citycode'],
					'citytype' => $vals[$i]['attributes']['citytype'],
				);
				$c++;
				break;
			case 'open':
				break;
			case 'close':
				break;
		}
		break;
	}

}

		$results_count = $c;
		$c = 0;
		$selectString = "<select id=\"locList\" onChange=\"listSet(this.value)\">\n<option> - Select City - </option>";

		while ($c < $results_count) {
		$selectString .= "\n<option value=" . $tree[$c]['zipcode'] . $tree[$c]['citycode'] .  $tree[$c]['citytype'] . ">" . $tree[$c]['cityname'] . " " . $tree[$c]['statename'] . " " . $tree[$c]['countryname'] . " " . $tree[$c]['zipcode'] . " " . $tree[$c]['citycode'] . "</option>";
		$c++;
		}

		$selectString .= "\n</select>";

            echo $selectString;

        } else {
        	echo "ERROR: Location service unavailable.";
        }
    }



?>
