$apexname="HIMB Tanks";
$aurl="http://166.122.79.29"; // Enter your APEX URL : port
////////////////////////////////////////
/// uncomment ONE of the below lines ///
//date_default_timezone_set("America/Los_Angeles");
// date_default_timezone_set("America/Phoenix");
// date_default_timezone_set("America/Denver");
// date_default_timezone_set("America/Chicago");
// date_default_timezone_set("America/Indianapolis");
date_default_timezone_set("America/New_York");
////////////////////////////////////////
$range = $_GET['r'];
if (intval($range) < 1) $range = '1';
if (intval($range) > 14) $range = '14';
$range--;
$width = $_GET['w'];
$height = $_GET['h'];
if ($width == '' or intval($width) < 150) $width = '150';
if ($height == ''or intval($height)< 30) $height = '30';
$fontsize = $_GET['f'];
if ($fontsize == ''or intval($fontsize)< 11) $fontsize = '11';
$today=date(DATE_RFC822);
$xml_probename_key  =  "*DATALOG*RECORD*PROBE*NAME";
$xml_probevalue_key  =  "*DATALOG*RECORD*PROBE*VALUE";
$apex_array = array();
$counter = 0;
class xml_apex{
    var $probename, $probevalue;
}
function startTag($parser, $data){
    global $current_tag;
    $current_tag .= "*$data";
}
function endTag($parser, $data){
    global $current_tag;
    $tag_key = strrpos($current_tag, '*');
    $current_tag = substr($current_tag, 0, $tag_key);
}
function contents($parser, $data){
    global $current_tag, $curdate, $xml_date, $xml_probename_key, $xml_probevalue_key, $counter, $apex_array;
    switch($current_tag){
        case $xml_probename_key:
            $apex_array[$counter] = new xml_apex();
            $apex_array[$counter]->recdate = $curdate;
            $apex_array[$counter]->probename = $data;
            break;
        case $xml_probevalue_key:
            $apex_array[$counter]->probevalue = $data;
            $counter++;
            break;        
    }
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$url=$aurl.'/cgi-bin/datalog.xml?sdate='.date('ymd',mktime(0, 0, 0, date("m"), date("d")-$range, date("Y"))).'&days='.$range;
$data = file_get_contents($url, false);
xml_parse($xml_parser, $data);
xml_parser_free($xml_parser);
$probecount = count($apex_array);
$pHCount = -1; $pHList = ''; $pHLast = 0; $pHPos = 50;
$tempCount = -1; $tempList = ''; $tempLast = 0; $tempPos = 50;
$ampCount = -1; $ampList = ''; $ampLast = 0; $ampPos = 50;
for($x=0;$x<=$probecount;$x++){
    $apex_array[$x]->probevalue = floatval($apex_array[$x]->probevalue);
    switch($apex_array[$x]->probename){
        case 'pH':
            $pHCount ++;
            $pHLast = $apex_array[$x]->probevalue;
            if ($pHList == '') {
                $pHLow = $apex_array[$x]->probevalue;
                $pHHigh = $apex_array[$x]->probevalue;
                $pHList = strval($apex_array[$x]->probevalue);
            } else {
                if ($apex_array[$x]->probevalue < $pHLow) $pHLow = $apex_array[$x]->probevalue;
                if ($apex_array[$x]->probevalue > $pHHigh) $pHHigh = $apex_array[$x]->probevalue;
                $pHList = $pHList . ',' . strval($apex_array[$x]->probevalue);
            };
            break;
        case 'Temp':
            $tempCount ++;
            $tempLast = $apex_array[$x]->probevalue;
            if ($tempList == '') {
                $tempLow = $apex_array[$x]->probevalue;
                $tempHigh = $apex_array[$x]->probevalue;
                $tempList = strval($apex_array[$x]->probevalue);
            } else {
                if ($apex_array[$x]->probevalue < $tempLow) $tempLow = $apex_array[$x]->probevalue;
                if ($apex_array[$x]->probevalue > $tempHigh) $tempHigh = $apex_array[$x]->probevalue;
                $tempList = $tempList . ',' . strval($apex_array[$x]->probevalue);
            };
            break;
        case 'Amp_3':
            $ampCount ++;
            $ampLast = $apex_array[$x]->probevalue;
            if ($ampList == '') {
                $ampLow = $apex_array[$x]->probevalue;
                $ampHigh = $apex_array[$x]->probevalue;
                $ampList = strval($apex_array[$x]->probevalue);
            } else {
                if ($apex_array[$x]->probevalue < $ampLow) $ampLow = $apex_array[$x]->probevalue;
                if ($apex_array[$x]->probevalue > $ampHigh) $ampHigh = $apex_array[$x]->probevalue;
                $ampList = $ampList . ',' .strval($apex_array[$x]->probevalue);
            };
            break;                    
    }
}
$pHPos = 0;
$tempPos = 0;
$ampPos = 0;
if ($pHHigh - $pHLow > 0) $pHPos = intval((($pHLast - $pHLow)/($pHHigh - $pHLow)) * 100);
if ($tempHigh - $tempLow > 0) $tempPos = intval((($tempLast - $tempLow)/($tempHigh - $tempLow)) * 100);
if ($ampHigh - $ampLow > 0) $ampPos = intval((($ampLast - $ampLow)/($ampHigh - $ampLow)) * 100);

// DISPLAY GRAPH
echo '
' . $apexname . '

';
echo '';
echo '
';
echo '';
echo '
';
echo '';
echo '
';
echo '
' . $today . '
';
?>