<?php

ini_set("display_errors", "On");

include("plotinclude.php");
printf("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n");
printf("<html>\n<head>\n");
printf("<meta http-equiv='content-type' content='text/html; charset=ISO-8859-1'>\n");
printf("<title>show plots</title>\n");
printf("<link rel='stylesheet' type='text/css' href='lamouette.css'>\n");
printf("</head>\n");
printf("<body>\n");

$ds=0;
if (!empty($_GET["ds"]))
    $ds=$_GET["ds"];
$tabnum=1;
if (!empty($_GET["tabnum"]))
    $tabnum=$_GET["tabnum"];
$type2="calib";
if (!empty($_GET["type2"]))
    $type2=$_GET["type2"];
$plot="";
$nextseq=0;
$prevseq=0;
$seq=0;
if (!empty($_GET["prevseq"]) && !empty($_GET["prev"]))
    $seq=$_GET["prevseq"];
if (!empty($_GET["nextseq"]) && !empty($_GET["next"]))
    $seq=$_GET["nextseq"];
$next=0;
$prev=0;
$numseq=0;

if (!empty($ds))
{
    $num=sprintf("%08d",$ds);
    $num2=substr($num,0,5);
    $datasetfile="http://datacenter.astro.uni-wuerzburg.de/datasets/" . $num2 . "/dataset" . $num . ".txt";
    $dataset=file_get_contents($datasetfile);
    $onpos=strpos($dataset, "SequencesOn:");
    $offpos=strpos($dataset, "SequencesOff:");
    $possource=strpos($dataset, "SourceName:");
    $sequoff="";
    if (!empty($offpos))
        $sequoff=substr($dataset, $offpos+13, $possource-($offpos+13));
    if (empty($offpos))
        $offpos=$possource;
    $sequon=substr($dataset, $onpos+12, $offpos-($onpos+12));
    $sequences=split(" ", trim($sequon));
    if ($seq!=0 && !in_array($seq, $sequences))
        $seq=0;
    foreach($sequences as $key => $sequ)
    {
        if ($seq==0)
            $seq=$sequ;
        if ($next==1)
        {
            $nextseq=$sequ;
            if ($prevseq!=0)
                break;
            $next=0;
        }
        if ($sequ==$seq)
        {
            $next=1;
            continue;
        }
        $prevseq=$sequ;
    }
    $numseq=count($sequences);
    if ($numseq==2)
        $nextseq=$prevseq;
}

$type=gettypename($type2);
$plot=getplotname($seq, $tabnum, $type, $type2);

printf("<form action='showplots-ds.php' method='GET'>\n");
printf("<table width='100%%' border='0'>\n");
printf("<tr><td align='center'>\n");
if (!empty($prevseq))
    printf("%d &nbsp;", $prevseq);
if ($numseq>1)
    printf("<input type='submit' value='<< Prev Plot' name='prev'>\n");
printf("<input type='text' name='ds' size='5' maxlength='5' value='%s'>\n", $ds);
printf("<input type='text' name='type2' size='6' maxlength='6' value='%s'>\n", $type2);
printf("<input type='text' name='tabnum' size='2' maxlenght='2' value='%s'>\n", $tabnum);
printf("<input type='hidden' name='prevseq' size='2' maxlenght='2' value='%s'>\n", $prevseq);
printf("<input type='hidden' name='nextseq' size='2' maxlenght='2' value='%s'>\n", $nextseq);
if ($numseq>1)
    printf("<input type='submit' value='Next Plot >>' name='next'>\n");
else
    printf("<input type='submit' value='Plot' name='next'>\n");
if (!empty($nextseq))
    printf("&nbsp;%d ", $nextseq);
if ($numseq>0)
{
    printf("&nbsp; [ %d sequence", $numseq);
    if ($numseq>1)
        printf("s");
    print(" ] \n");
}
printf("</td>\n</tr>\n<tr>\n<td align='center'>\n");
if (!empty($seq))
    printf("%s <img src='%s'>", $plot, $plot);
else
    printf("You have to insert a dataset number in the first field.");
printf("</td>\n</tr>\n</table>\n");
printf("</form>\n");
printf("</body>\n");
printf("</html>\n");

ini_set("display_errors", "Off");

?>
