Index: /trunk/www/db_po/edit_po.php
===================================================================
--- /trunk/www/db_po/edit_po.php	(revision 11504)
+++ /trunk/www/db_po/edit_po.php	(revision 11505)
@@ -6,36 +6,18 @@
     echo (file_get_contents("index-header.html"));
 
+    //call get_progs() at startup
+    printf("<script type=\"text/javascript\" >\n");
+    printf("window.onload = function () { get_progs(\"\",\"10\",\"edit\"); }\n");
+    printf("</script>\n");
+
     include("db.php");
-    $db_id = mysql_pconnect($host, $user, $pw);
-    if ($db_id==FALSE)
-    {
-        printf("mysql_connect returned the following error: %s\n", mysql_error());
-        die("");
-    }
-    mysql_select_db($db);
-    echo "Hi, this is a test interface for the DB <b>" . $db . "</b>.<br><br>";
-
-    $query0="SELECT fProgram from ProgramOption GROUP BY fProgram";
-
-    $result0=mysql_query($query0, $db_id);
-
+    printf("With this page, you can <u>edit</u> the DB <b>%s</b>.\n", $db);
+    printf("For view, please go <a href='view_po.php'>here</a>.<br><br>\n");
     printf("<table border='0'><tbody>\n");
-    printf("<tr>\n<td>Please select a program:</td>\n");
-    printf("<td>\n<select name='prog' size='1' onchange=\"get_keys(this.value)\"class='Width'>\n");
-    printf("<option value='empty'> ---select program--- </option>\n");
-    printf("<option value='all'> all programs </option>\n");
-    while ($row0 = mysql_fetch_row($result0))
-        printf("<option value='%s'>%s </option>\n", $row0[0], $row0[0]);
-    printf("</select>\n</td>\n</tr>\n");
-    mysql_free_result($result0);
-
-    echo "<tr id='keyfield'></tr>";
-    echo "<tr id='valuefield'></tr>";
-    printf("</tbody></table>\n");
-
-    mysql_close($db_id);
-
-
-
+    printf("<tr>\n");
+    printf("<td valign='top' id='progfield'></td>\n");
+    printf("<td valign='top' id='keyfield'></td>\n");
+    printf("<td valign='top' id='valuefield1'></td>\n");
+    printf("</tr>\n</tbody></table>\n");
 
     echo (file_get_contents("index-footer.html"));
Index: /trunk/www/db_po/get_keys.php
===================================================================
--- /trunk/www/db_po/get_keys.php	(revision 11504)
+++ /trunk/www/db_po/get_keys.php	(revision 11505)
@@ -9,4 +9,10 @@
         return;
     }
+    if (empty($_GET["fKey"]))
+        $_GET["fKey"]="";
+    if (empty($_GET["fSelectSize"]))
+        $_GET["fSelectSize"]="10";
+    if (empty($_GET["fMode"]))
+        $_GET["fMode"]="view";
 
     include("db.php");
@@ -19,18 +25,36 @@
     mysql_select_db($db);
 
-    $query0="SELECT fKey from ProgramOption ";
+    $query1 = "SELECT Max(fCounter) FROM History";
+    $result1=mysql_query($query1, $db_id);
+    $row1 = mysql_fetch_row($result1);
+    $maxcounter=$row1[0];
+    mysql_free_result($result1);
+
+    $query0="SELECT Concat(fKey1, fKey2) as ourkeys from ProgramOption ";
+    $query0.="WHERE fCounter=".$maxcounter." ";
     if ($_GET["fProgram"]!="all")
-        $query0.="WHERE fProgram='".$_GET["fProgram"]."' ";
-    $query0.="GROUP BY fKey";
+        $query0.="AND fProgram='".$_GET["fProgram"]."' ";
+    $query0.="GROUP BY ourkeys";
 
     $result0=mysql_query($query0, $db_id);
 
-    printf("<td>Please select a key:</td>\n");
-    printf("<td><select name='key' size='1' onchange=\"get_values('%s',this.value)\"class='Width'>\n", $_GET["fProgram"]);
-    printf("<option value='empty'> ---select key--- </option>\n");
-    printf("<option value='newkey'> add new key </option>\n");
+    if (strcmp($_GET["fMode"], "edit")==0)
+        printf("select key: <br>\n");
+    else
+        printf("select key: \n");
+    printf("<select style='width:200px' id='key' size='%s' onchange=\"get_values(1,'%s',this.value,'%s')\"class='Width'>\n",
+           $_GET["fSelectSize"], $_GET["fProgram"], $_GET["fMode"]);
+    if (!empty($_GET["fKey"]) && strcmp("all", $_GET["fKey"])==0)
+        printf("<option value='all' selected> --- all keys --- </option>\n");
+    else
+        printf("<option value='all'> --- all keys --- </option>\n");
     while ($row0 = mysql_fetch_row($result0))
-        printf("<option value='%s'>%s </option>\n", $row0[0], $row0[0]);
-    printf("</select>\n</td>\n");
+        if (!empty($_GET["fKey"]) && strcmp($row0[0], $_GET["fKey"])==0)
+            printf("<option value='%s' selected>%s </option>\n", $row0[0], $row0[0]);
+        else
+            printf("<option value='%s'>%s </option>\n", $row0[0], $row0[0]);
+    printf("</select>\n<br>\n");
+    if (strcmp($_GET["fMode"], "edit")==0)
+        printf("<button onClick='addRow(2,\"%s\",\"\",\"\",\"\",\"\");' id='addrowkey' style='display:inline'>Add Key</button>\n", $_GET["fProgram"]);
     mysql_free_result($result0);
 
Index: /trunk/www/db_po/get_values.php
===================================================================
--- /trunk/www/db_po/get_values.php	(revision 11504)
+++ /trunk/www/db_po/get_values.php	(revision 11505)
@@ -4,15 +4,24 @@
     ini_set("mysql.trace_mode", "On");
 
+    if (empty($_GET["fGetValCase"]))
+    {
+        echo "Please specify how you want to display the table (for view or for insert).";
+        //1: insert
+        //2: view
+        //3: view compare
+        return;
+    }
     if (empty($_GET["fKey"]))
     {
-        echo "Please select a key.<br>";
+        echo "Please select a key.";
         return;
     }
     if (empty($_GET["fProgram"]))
     {
-        echo "Please select a program.<br>";
+        echo "Please select a program.";
         return;
     }
-
+    if (empty($_GET["fStatusDate"]))
+        $_GET["fStatusDate"]="";
 
     include("db.php");
@@ -26,47 +35,129 @@
 
     $query1 = "SELECT Max(fCounter) FROM History";
+    // get counter of fStatusDate or current status
+    if ($_GET["fGetValCase"]!=1 && $_GET["fStatusDate"]!="")
+        $query1.=" WHERE (fValidFrom < '".$_GET["fStatusDate"]."' OR fValidFrom like '".$_GET["fStatusDate"]."%')";
+
     $result1=mysql_query($query1, $db_id);
     $row1 = mysql_fetch_row($result1);
-    $maxcount=$row1[0];
-    //echo $maxcount;
+    $counter=$row1[0];
+    mysql_free_result($result1);
 
-    $query0 ="SELECT fIndex, fProgram, fKey, fValue, fDescription, fCounter FROM ProgramOption ";
-    $query0.="WHERE fKey='".$_GET["fKey"]."'";
-    if ($_GET["fProgram"]!="all")
+    //get key1 and key2 separately from DB (for function AddRow())
+    $query2 = "SELECT fKey1, SUBSTRING(fKey2,2), fType FROM ProgramOption WHERE Concat(fKey1,fKey2)='".$_GET["fKey"]."'";
+    $result2=mysql_query($query2, $db_id);
+    $row2 = mysql_fetch_row($result2);
+    $key1=$row2[0];
+    $key2=$row2[1];
+    $type=$row2[2];
+    mysql_free_result($result2);
+
+    $query0 ="SELECT fIndex, fValidFrom, fProgram, Concat(fKey1,fKey2), fValue, fDescription, fCounter, fType, fMin, fMax, fKey1, SUBSTRING(fKey2,2), if(fOriginalIndex=0, fIndex, fOriginalIndex) FROM ProgramOption ";
+    $query0.="WHERE 1=1 ";
+    if (strcmp($_GET["fKey"],"all")!=0)
+        $query0.="AND Concat(fKey1,fKey2)='".$_GET["fKey"]."' ";
+    if (strcmp($_GET["fProgram"],"all")!=0)
         $query0.="AND fProgram='".$_GET["fProgram"]."' ";
+    if ($_GET["fGetValCase"]!=1)
+    {
+        $query0.="AND NOT isnull(fValue) ";
+        $query0.="AND fCounter>=".$counter;
+        if ($_GET["fStatusDate"]!="")
+            $query0.=" AND (fValidFrom < '".$_GET["fStatusDate"]."' OR fValidFrom like '".$_GET["fStatusDate"]."%') ";
+    }
+    $query0.=" ORDER BY fOriginalIndex, fIndex";
 
     $result0=mysql_query($query0, $db_id);
 
-    printf("<td valign='top'>Please edit:</td>\n");
-    printf("<td>\n");
-    printf("<button onClick='ShowCurrent();' id='showcurrent' style='display:none'>Show Current</button>\n");
-    printf("<button onClick='ShowAll();' id='showall' style='display:inline'>Show All</button>\n");
-    printf("<button onClick='addRow(\"%s\",\"%s\");' id='addrow' style='display:inline'>Add Row</button>\n", $_GET["fProgram"], $_GET["fKey"]);
-    printf("<table border='1' width='100px' height='100px'><tbody id='valtable'>\n");
-    printf("<tr><th>Index / Counter</th><th>Program</th><th>Key</th><th>Value</th><th>Description</th>");
-    printf("<th>Action</th></tr>\n");
-    while ($row0 = mysql_fetch_row($result0))
+    if ($_GET["fGetValCase"]==1)
     {
-        if ($row0[5]==$maxcount)
-            printf("<tr id='new%s' style='display:'>\n", $row0[0]);
-        else
-            printf("<tr id='old%s' style='display:none'>\n", $row0[0]);
-        printf("<td> %s / %s</td>\n", $row0[0], $row0[5]);
-        printf("<td id='prog%s'>%s</td>\n", $row0[0], $row0[1]);
-        printf("<td id='key%s'>%s</td>\n", $row0[0], $row0[2]);
-        printf("<td align='right' id='val%s'>%s</td>\n", $row0[0], $row0[3]);
-        printf("<td id='descr%s'>%s</td>\n", $row0[0], $row0[4]);
-        if ($row0[5]==$maxcount)
-            printf("<td id='action%s'><input type='button' value='Edit' onclick='EditRow(\"%s\")'></td>\n</tr>\n", $row0[0], $row0[0]);
-        else
-            printf("<td id='action%s'></td>\n</tr>\n", $row0[0]);
-        $maxindex=$row0[0];
+        printf("<button onClick='ShowCurrent();' id='showcurrent' style='display:none'>Show Current</button>\n");
+        printf("<button onClick='ShowAll();' id='showall' style='display:inline'>Show History</button>\n");
+        printf("<button onClick='addRow(3, \"%s\",\"%s\",\"%s\",\"%s\",\"%s\");' id='addrowvals' style='display:inline'>Add Value</button>\n",
+               $_GET["fProgram"], $_GET["fKey"], $key1, $key2, $type);
     }
-    printf("<div id='addline'></div>");
-    printf("</tbody></table>\n<br>\n");
-    printf("<div id='maxindex' style='display:none'>%s</div>\n", $maxindex);
-    printf("</td>\n");
+    printf("<table font-size='-1' border='1' width='100px' height='100px'><tbody id='valtable%s'>\n", $_GET["fGetValCase"]);
+    printf("<tr>");
+    printf("<th>Index / Orig / Counter</th>\n");
+    printf("<th>Valid since</th>\n");
+    printf("<th>Program</th>\n");
+    //printf("<th>Key</th>\n");
+    printf("<th>Key1</th>\n");
+    printf("<th>Key2</th>\n");
+    printf("<th>Value</th>\n");
+    printf("<th>Type</th>\n");
+    if ($_GET["fGetValCase"]==1)//show only in edit mode
+        printf("<th class='description%d'>Description</th>\n", $_GET["fGetValCase"]);
+    else
+        printf("<th class='description%d' style='display:none'>Description</th>\n", $_GET["fGetValCase"]);
+    if ($_GET["fGetValCase"]==1)//show only in edit mode
+        printf("<th>Action</th>\n");
+    printf("<th>Min</th>\n");
+    printf("<th>Max</th>\n");
+    printf("</tr>\n");
+    $maxindex=0;
+    if ($result0)
+        while ($row0 = mysql_fetch_row($result0))
+        {
+            if ($_GET["fGetValCase"]==1)
+            {
+                if ($row0[6]==$counter)
+                    printf("<tr id='new%s' style='display:'>\n", $row0[0]);
+                else
+                    printf("<tr id='old%s' style='display:none'>\n", $row0[0]);
+            }
+            else
+                printf("<tr id='%scompare%s' style='display:'>\n", $_GET["fGetValCase"], $row0[12]);
 
-    mysql_free_result($result0);
+            printf("<td> %s / %s / %s <div id='%dorigindex%s' style='display:none'>%s</div> </td>\n",
+                   $row0[0], $row0[12], $row0[6], $_GET["fGetValCase"], $row0[0], $row0[12]);
+            if (strcmp('0000-00-00 00:00:00', $row0[1])==0)
+                printf("<td id='notyetvalid%s'> %s </td>\n", $row0[0], $row0[1]);
+            else
+                printf("<td> %s </td>\n", $row0[1]);
+            printf("<td id='prog%s'>%s</td>\n", $row0[0], $row0[2]);
+            //printf("<td id='key%s'>%s</td>\n", $row0[0], $row0[3]);
+            printf("<td id='1key%s'>%s</td>\n", $row0[0], $row0[10]);
+            printf("<td id='2key%s'>%s</td>\n", $row0[0], $row0[11]);
+            printf("<td align='right' id='val%s'>%s</td>\n", $row0[0], $row0[4]);
+            printf("<td id='type%s'>%s</td>\n", $row0[0], $row0[7]);
+            if ($_GET["fGetValCase"]==1)//show only in edit mode
+                printf("<td class='description%d' id='descr%s'>%s</td>\n", $_GET["fGetValCase"], $row0[0], $row0[5]);
+            else
+                printf("<td class='description%d' style='display:none' id='descr%s'>%s</td>\n", $_GET["fGetValCase"], $row0[0], $row0[5]);
+            if ($_GET["fGetValCase"]==1)//show only in edit mode
+            {
+                if ($row0[6]==$counter)
+                    printf("<td id='action%s'><input type='button' value='Edit' onclick='EditRow(\"%s\")'></td>\n", $row0[0], $row0[0]);
+                else
+                    printf("<td id='action%s'></td>\n", $row0[0]);
+            }
+            printf("<td id='min%s'>%s</td>\n", $row0[0], $row0[8]);
+            printf("<td id='max%s'>%s</td>\n", $row0[0], $row0[9]);
+            printf("</tr>\n");
+            if ($row0[0]>$maxindex)
+                $maxindex=$row0[0];
+        }
+    printf("</tbody></table>\n");
+    if ($_GET["fGetValCase"]==1)
+    {
+        printf("<button onClick='ShowCurrent();' id='showcurrent2' style='display:none'>Show Current</button>\n");
+        printf("<button onClick='ShowAll();' id='showall2' style='display:inline'>Show History</button>\n");
+        printf("<button onClick='addRow(3,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\");' id='addrowvals2' style='display:inline'>Add Value</button>\n",
+               $_GET["fProgram"], $_GET["fKey"], $key1, $key2, $type);
+    }
+    else
+    {
+        printf("<button onClick='HideDescription(%d);' id='hidedescr%d' style='display:none'>Hide Description</button>\n", $_GET["fGetValCase"], $_GET["fGetValCase"]);
+        printf("<button onClick='ShowDescription(%d);' id='showdescr%d' style='display:'>Show Description</button>\n", $_GET["fGetValCase"], $_GET["fGetValCase"]);
+    }
+
+    if ($_GET["fGetValCase"]==3)
+        printf("<div id='maxindex3' style='display:none'>%s</div>\n", $maxindex);
+    else
+        printf("<div id='maxindex' style='display:none'>%s</div>\n", $maxindex);
+
+    if ($result0)
+        mysql_free_result($result0);
 
     mysql_close($db_id);
Index: /trunk/www/db_po/insert_po.php
===================================================================
--- /trunk/www/db_po/insert_po.php	(revision 11504)
+++ /trunk/www/db_po/insert_po.php	(revision 11505)
@@ -11,5 +11,5 @@
     if (empty($_GET["fIndex"]))
     {
-        echo "Please giv an index.<br>";
+        echo "Please give an index.<br>";
         return;
     }
@@ -19,7 +19,12 @@
         return;
     }
-    if (empty($_GET["fKey"]))
-    {
-        echo "Please select a key.<br>";
+    if (empty($_GET["fKey1"]))
+    {
+        echo "Please select a key1.<br>";
+        return;
+    }
+    if (empty($_GET["fKey2"]))
+    {
+        echo "Please select a key2.<br>";
         return;
     }
@@ -31,13 +36,17 @@
     if (empty($_GET["fDescription"]))
     {
-        echo "Please insert a Description.<br>";
-        return;
-    }
-
-
+        echo "Please insert a description.<br>";
+        return;
+    }
+    if (empty($_GET["fType"]))
+    {
+        echo "Please select a type.<br>";
+        return;
+    }
+    if (empty($_GET["fOriginalIndex"]))
+        $_GET["fOriginalIndex"]="";
 
     include("db.php");
     $db_id = mysqli_connect($host, $user, $pw, $db);
-    //if ($db_id==FALSE)
     if (mysqli_connect_errno())
     {
@@ -45,32 +54,138 @@
         die("");
     }
-    //mysql_select_db($db);
+
+    //query old entry
+    $query1="SELECT fValue, fProgram, fKey1, fDescription, fType, fMin, fMax, fKey2 from ProgramOption WHERE fIndex='".$_GET["fIndex"]."';";
+    //compare new values to old values
+    //insert only, if at least one values is new
+    //  update counter only if value is updated
+    if (!$result1=mysqli_query($db_id, $query1))
+    {
+        echo "Error sending query: ".$query1;
+        return;
+    }
+    $row1 = mysqli_fetch_row($result1);
+
+    if ($_GET["fUpdCase"]!=4
+        && strcmp($row1[0],$_GET["fValue"])==0
+        && strcmp($row1[1],$_GET["fProgram"])==0
+        && strcmp($row1[2],$_GET["fKey1"])==0
+        && strcmp($row1[7],$_GET["fKey2"])==0
+        && strcmp($row1[3],$_GET["fDescription"])==0
+        && strcmp($row1[4],$_GET["fType"])==0
+        //&& strcmp($row1[5],$_GET["fMin"])==0
+        //&& strcmp($row1[6],$_GET["fMax"])==0
+       )
+    {
+        // be careful, this output is evaluated by UpdateRow() in po.js
+        echo "no change";
+        mysqli_free_result($result1);
+        return;
+    }
+    else
+        mysqli_free_result($result1);
+
+    //check if key alread exists
+    $query2="SELECT Count(*) from ProgramOption WHERE fKey1='".$_GET["fKey1"]."' AND fKey2='.".$_GET["fKey2"]."'";
+    $query2.=" AND fCounter=(Select Max(fCounter) from History)";
+    if (!$result2=mysqli_query($db_id, $query2))
+    {
+        echo "Error sending query: ".$query2;
+        return;
+    }
+    $row2 = mysqli_fetch_row($result2);
+    if (($_GET["fUpdCase"]==1 || $_GET["fUpdCase"]==5) && $row2[0]>0)
+    {
+        // be careful, this output is evaluated by UpdateRow() in po.js
+        echo "key exists";
+        mysqli_free_result($result2);
+        return;
+    }
+    else
+        mysqli_free_result($result2);
+
+    //check if program alread exists
+    $query3="SELECT Count(*) from ProgramOption WHERE fProgram='".$_GET["fProgram"]."'";
+    $query3.=" AND fCounter=(Select Max(fCounter) from History)";
+    if (!$result3=mysqli_query($db_id, $query3))
+    {
+        echo "Error sending query: ".$query3;
+        return;
+    }
+    $row3 = mysqli_fetch_row($result3);
+    if ($_GET["fUpdCase"]==5 && $row3[0]>0)
+    {
+        // be careful, this output is evaluated by UpdateRow() in po.js
+        echo "prog exists";
+        mysqli_free_result($result3);
+        return;
+    }
+    else
+        mysqli_free_result($result3);
+
+    //note for fType:
+    //SHOW COLUMNS FROM ProgramOption LIKE 'fType';
 
     //new entry in table ProgramOption
-    $query0="INSERT ProgramOption SET ";
-    $query0.="fProgram='".$_GET["fProgram"]."'";
-    $query0.=", fKey='".$_GET["fKey"]."'";
-    if ($_GET["fUpdCase"]==2 || $_GET["fUpdCase"]==3)
-        $query0.=", fValue='".$_GET["fValue"]."'";
-    if ($_GET["fUpdCase"]==1 || $_GET["fUpdCase"]==4)
-        $query0.=", fValue=NULL";
-    $query0.=", fDescription='".$_GET["fDescription"]."'";
-    $query0.=", fValidFrom=Now() ";
-    if ($_GET["fUpdCase"]==1)
-        $query0.=", fCounter= (Select Max(fCounter) from History) ;";
-    else
-        $query0.=", fCounter= (Select Max(fCounter) from History)+1 ;";
-
-    //update counter for other valid entries in ProgramOption
-    $query0.=" UPDATE ProgramOption SET ";
-    $query0.=" fCounter= (Select Max(fCounter) from History)+1 ";
-    $query0.=" WHERE fCounter= (Select Max(fCounter) from History) ";
-    if ($_GET["fUpdCase"]==3 || $_GET["fUpdCase"]==4)
-        $query0.=" AND NOT fIndex=".$_GET["fIndex"];
-    $query0.=" AND NOT ISNULL(fValue); ";
-
-    //insert new counter to history
-    if ($_GET["fUpdCase"]!=1)
-    {
+    //update only in case 3 if value is not changed
+    if ($_GET["fUpdCase"]==6)
+        $query0="DELETE FROM ProgramOption ";
+    else
+    {
+        if (strcmp($row1[0],$_GET["fValue"])==0 && $_GET["fUpdCase"]==3)
+            $query0="UPDATE ";
+        else
+            $query0="INSERT ";
+        $query0.=" ProgramOption SET ";
+        // set program, key, description, type
+        $query0.="fProgram='".$_GET["fProgram"]."'";
+        if (!empty($_GET["fOriginalIndex"]))
+            $query0.=", fOriginalIndex='".$_GET["fOriginalIndex"]."'";
+        $query0.=", fKey1='".$_GET["fKey1"]."'";
+        $query0.=", fKey2='.".$_GET["fKey2"]."'";
+        $query0.=", fDescription='".$_GET["fDescription"]."'";
+        $query0.=", fType='".$_GET["fType"]."'";
+        // set value
+        // change value in case is was
+        if ($_GET["fUpdCase"]==2 //inserted
+            || ($_GET["fUpdCase"]==3 && strcmp($row1[0],$_GET["fValue"])!=0))// changed
+            $query0.=", fValue='".$_GET["fValue"]."'";
+        // set to NULL in case it was
+        if ($_GET["fUpdCase"]==1 || $_GET["fUpdCase"]==5 //new insert
+            || $_GET["fUpdCase"]==4) //delete
+            $query0.=", fValue=NULL";
+        //increase counter in case value was
+        if ($_GET["fUpdCase"]==2//created
+            || ($_GET["fUpdCase"]==3 && strcmp($row1[0],$_GET["fValue"])!=0)//changed
+            || $_GET["fUpdCase"]==4)//deleted
+        {
+            $query0.=", fValidFrom=Now() ";
+            $query0.=", fCounter= (Select Max(fCounter) from History)+1 ";
+        }
+        else//else set current counter
+            $query0.=", fCounter= (Select Max(fCounter) from History) ";
+    }
+    //do update only for current row
+    if ((strcmp($row1[0],$_GET["fValue"])==0 && $_GET["fUpdCase"]==3) || $_GET["fUpdCase"]==6)
+        $query0.=" WHERE fIndex=".$_GET["fIndex"];
+    if ($_GET["fUpdCase"]==6)
+        $query0.=" AND fValidFrom='0000-00-00 00:00:00'";
+    $query0.=";";
+
+    //update counter in case value was 
+    if ($_GET["fUpdCase"]==2 //created
+        || ($_GET["fUpdCase"]==3 && strcmp($row1[0],$_GET["fValue"])!=0)//changed
+       || $_GET["fUpdCase"]==4)//deleted
+    {
+        //update counter for other valid entries in ProgramOption
+        $query0.=" UPDATE ProgramOption SET ";
+        $query0.=" fCounter= (Select Max(fCounter) from History)+1 ";
+        $query0.=" WHERE fCounter= (Select Max(fCounter) from History) ";
+        if ($_GET["fUpdCase"]==3 || $_GET["fUpdCase"]==4)
+            $query0.=" AND NOT fIndex=".$_GET["fIndex"];
+        $query0.=" AND ((NOT ISNULL(fValue) AND NOT fValidFrom='0000-00-00 00:00:00') ";//normal entries, but not deleted ones
+        $query0.=" OR (ISNULL(fValue) AND fValidFrom='0000-00-00 00:00:00')); ";//new entries
+
+        //insert new counter to history
         $query0.=" INSERT History SET ";
         $query0.=" fCounter= (Select Max(fCounter) from ProgramOption) ";
@@ -78,36 +193,4 @@
     }
 
-
-    /*
-    if ($_GET["fUpdCase"]==1 || $_GET["fUpdCase"]==2)
-        $query0 ="INSERT ProgramOption SET ";//insert key or insert value
-    else
-        $query0 ="UPDATE ProgramOption SET ";//update value (includes 'delete')
-
-    if ($_GET["fUpdCase"]==4)//i.e. delete
-        $query0.=" fValue=NULL";
-    else
-    {
-        $query0.="fProgram='".$_GET["fProgram"]."'";
-        $query0.=", fKey='".$_GET["fKey"]."'";
-        $query0.=", fValue='".$_GET["fValue"]."'";
-        $query0.=", fDescription='".$_GET["fDescription"]."'";
-        $query0.=", fValidFrom=Now() ";
-    }
-
-    if ($_GET["fUpdCase"]==1 || $_GET["fUpdCase"]==2) //only in case of insert
-        $query0.=", fValidFrom=Now() ";
-
-    if ($_GET["fUpdCase"]==3 || $_GET["fUpdCase"]==4)// only in case of update
-        $query0.="WHERE fIndex=".$_GET["fIndex"]."; ";
-
-    if ($_GET["fUpdCase"]==2 || $_GET["fUpdCase"]==3)// only when value is changed (includes insert)
-    {
-        $query0.="UPDATE ProgramOption SET fCounter=(Select Max(fCounter) from History)+1 WHERE NOT ISNULL(fValue); ";
-        $query0.="INSERT History SET fCounter=(Select Max(fCounter) from ProgramOption), fValidFrom=Now(); ";
-    }
-    */
-
-    //echo $query0;
     $result0=mysqli_multi_query($db_id, $query0);
     if ($result0)
Index: /trunk/www/db_po/po.js
===================================================================
--- /trunk/www/db_po/po.js	(revision 11504)
+++ /trunk/www/db_po/po.js	(revision 11505)
@@ -1,57 +1,181 @@
-
-//function
-function get_keys(prog)
-{
-    //alert(prog);
-
-    var xmlhttp;
-    if (window.XMLHttpRequest)
-    {// code for IE7+, Firefox, Chrome, Opera, Safari
-        xmlhttp=new XMLHttpRequest();
-    }
-    else
-    {// code for IE6, IE5
-        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
-    }
-    xmlhttp.open("GET","get_keys.php?fProgram="+prog,false);
+var xmlhttp;
+if (window.XMLHttpRequest)
+{// code for IE7+, Firefox, Chrome, Opera, Safari
+    xmlhttp=new XMLHttpRequest();
+}
+else
+{// code for IE6, IE5
+    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
+}
+
+//function to get programs from the db
+function get_progs(prog, selectsize, mode)
+{
+    xmlhttp.open("GET","get_progs.php?fProgram="+prog+"&fSelectSize="+selectsize+"&fMode="+mode,false);
     xmlhttp.send();
     if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
-        document.getElementById('keyfield').innerHTML = xmlhttp.responseText;
-        document.getElementById('valuefield').innerHTML = "";
-    }
-    else
-        document.getElementById('keyfield').innerHTML = "argh";
-
-}
-
-//function
-function get_values(prog, key)
-{
-    //alert(prog+"---"+key);
-    var xmlhttp;
-    if (window.XMLHttpRequest)
-    {// code for IE7+, Firefox, Chrome, Opera, Safari
-        xmlhttp=new XMLHttpRequest();
-    }
-    else
-    {// code for IE6, IE5
-        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
-    }
-    xmlhttp.open("GET","get_values.php?fKey="+key+"&fProgram="+prog,false);
+        document.getElementById('progfield').innerHTML = xmlhttp.responseText;
+        document.getElementById('keyfield').innerHTML = "";
+        if (document.getElementById('valuefield1'))
+            document.getElementById('valuefield1').innerHTML = "";
+    }
+    else
+        document.getElementById('progfield').innerHTML = "argh";
+}
+
+//function to get keys from the db
+function get_keys(prog, key, selectsize, mode)
+{
+    xmlhttp.open("GET","get_keys.php?fProgram="+prog+"&fKey="+key+"&fSelectSize="+selectsize+"&fMode="+mode,false);
     xmlhttp.send();
     if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
-        document.getElementById('valuefield').innerHTML = xmlhttp.responseText;
-        if (key=='newkey')
-            addRow(prog, key);
-    }
-    else
-        document.getElementById('valuefield').innerHTML = "argh";
-}
-
-function addRow(prog, key)
-{
-    tabBody=document.getElementById("valtable");
+        document.getElementById('keyfield').innerHTML = xmlhttp.responseText;
+        if (document.getElementById('valuefield1'))
+            document.getElementById('valuefield1').innerHTML = "";
+    }
+    else
+        document.getElementById('keyfield').innerHTML = "argh";
+
+    if(document.getElementById('addrowprog'))
+        document.getElementById('addrowprog').style.display = "";
+}
+
+function get_statusdate(getvalcase, val)
+{
+    if (!val)
+    {
+        radio=document.getElementsByName('dateformat'+getvalcase);
+        for (i=0; i< radio.length; i++)
+            if (radio[i].checked)
+                val=radio[i].value;
+    }
+    if (val=="pulldown")
+    {
+        year=document.getElementById('year'+getvalcase).value;
+        month=document.getElementById('month'+getvalcase).value;
+        day=document.getElementById('day'+getvalcase).value;
+        hour=document.getElementById('hour'+getvalcase).value;
+        min=document.getElementById('min'+getvalcase).value;
+        sec=document.getElementById('sec'+getvalcase).value;
+        if (year=="empty")
+            statusdate="";
+        else
+        {
+            statusdate=year;
+            if (month!="empty")
+            {
+                statusdate=statusdate+"-"+month;
+                if (day!="empty")
+                {
+                    statusdate=statusdate+"-"+day;
+                    if (hour!="empty")
+                    {
+                        statusdate=statusdate+" "+hour;
+                        if (min!="empty")
+                        {
+                            statusdate=statusdate+":"+min;
+                            if (sec!="empty")
+                                statusdate=statusdate+":"+sec;
+                        }
+                    }
+                }
+            }
+        }
+    }
+    if (val=="textinput")
+        statusdate=document.getElementById('statusdate'+getvalcase).value;
+    return statusdate;
+}
+
+//function to get values from the db
+function get_values(getvalcase, prog, key, mode)
+{
+    if (parseInt(getvalcase)>1)
+    {
+        //get prog and key
+        progselect=document.getElementById("prog");
+        if (!progselect)
+            prog="all";
+        else
+            if (progselect.options[progselect.selectedIndex])
+                prog=progselect.options[progselect.selectedIndex].value;
+            else
+                prog="all";
+        keyselect=document.getElementById("key");
+        if (!keyselect)
+            key="all";
+        else
+            if (keyselect.options[keyselect.selectedIndex])
+                key=keyselect.options[keyselect.selectedIndex].value;
+            else
+                key="all";
+    }
+    if (mode=="view" && getvalcase==1)
+        return;
+    //if (getvalcase==3)
+    //    document.getElementById('comparevals').style.display = "";
+    if ((getvalcase==2 || getvalcase==3) && document.getElementById('statusdate'+getvalcase))
+    {
+        statusdate=get_statusdate(getvalcase);
+        //alert(statusdate);
+        phpcall="get_values.php?fStatusDate="+statusdate+"&fGetValCase="+getvalcase+"&fKey="+key+"&fProgram="+prog;
+    }
+    else
+        phpcall="get_values.php?fGetValCase="+getvalcase+"&fKey="+key+"&fProgram="+prog;
+
+    xmlhttp.open("GET",phpcall,false);
+    xmlhttp.send();
+    if (xmlhttp.readyState==4 && xmlhttp.status==200)
+        document.getElementById('valuefield'+getvalcase).innerHTML = xmlhttp.responseText;
+    else
+        document.getElementById('valuefield'+getvalcase).innerHTML = "argh";
+
+    if(document.getElementById('addrowkey'))
+        document.getElementById('addrowkey').style.display = "";
+}
+
+function addRow(addcase, prog, key, key1, key2, type)
+{
+    if (!prog)
+    {
+        progselect=document.getElementById("prog");
+        if (progselect.options[progselect.selectedIndex])
+            prog=progselect.options[progselect.selectedIndex].value;
+        else
+            prog='all';
+    }
+    if (!key)
+    {
+        keyselect=document.getElementById("key");
+        if (!keyselect)
+            key='all';
+        else
+            if (keyselect.options[keyselect.selectedIndex])
+                key=keyselect.options[keyselect.selectedIndex].value;
+            else
+                key='all';
+    }
+    switch(addcase)
+    {
+    case 1:
+        get_keys(prog,key,"10", "edit");
+    case 2:
+        get_values(1, prog, key);
+    }
+
+    if (prog=='all' && (addcase==3 || addcase==2))
+        addcase=1;
+    if (key=='all' && addcase==3)
+        addcase=2;
+
+
+    for (var i = 1 ; i <= document.getElementById('maxindex').innerHTML ; i++)
+        if(document.getElementById('action'+i))
+            //document.getElementById('action'+i).innerHTML = "";
+            document.getElementById('action'+i).style.display = "none";
+
+    tabBody=document.getElementById("valtable1");
     row=document.createElement("TR");
     row.id='val0';
@@ -62,148 +186,437 @@
     cell4 = document.createElement("TD");
     cell5 = document.createElement("TD");
-    //case add new key (value NULL)
-    if (key=='newkey')
-    {
+    cell6 = document.createElement("TD");
+    cell7 = document.createElement("TD");
+    cell8 = document.createElement("TD");
+    cell9 = document.createElement("TD");
+    cell10 = document.createElement("TD");
+    var updcase;
+
+    // addcase: three cases
+    // 1: add new prog+key, val=NULL
+    // 2: add new key, val=NULL, prog=selected
+    // 3: add new val, prog=selected, key=selected, type fixed
+    switch(addcase)
+    {
+    case 1:
+        //field for prog
         input1 = document.createElement("INPUT");
-        input1.value = prog;
         input1.id = 'prog0';
+        cell1.appendChild(input1);
+        //fields for keys
         input2 = document.createElement("INPUT");
-        input2.value = 'newkey';
-        input2.id = 'key0';
+        input2.id = '1key0';
+        cell2.appendChild(input2);
+        input10 = document.createElement("INPUT");
+        input10.id = '2key0';
+        cell10.appendChild(input10);
+        //field for val
         cell3.id = 'newval0';
         textnode3=document.createTextNode("NULL");
-        input4 = document.createElement("INPUT");
-        input4.id = 'newdescr0';
-        input5 = document.createElement("input");
-        input5.value = 'Add';
-        input5.type = "button";
-        input5.onclick = function () { UpdateRow(1, 0); onclick() }
-        cell1.appendChild(input1);
+        cell3.appendChild(textnode3);
+        updcase=5;
+        //field type
+        select7 = document.createElement("select");
+        select7.id = 'newtype0';
+        select7.option = 'string';
+        option7a = document.createElement("option");
+        option7a.text = '-';
+        option7a.value = '';
+        select7.appendChild(option7a);
+        option7b = document.createElement("option");
+        option7b.id = 'int';
+        option7b.text = 'int';
+        option7b.value = 'int';
+        select7.appendChild(option7b);
+        option7c = document.createElement("option");
+        option7c.id = 'float';
+        option7c.text = 'float';
+        option7c.value = 'float';
+        select7.appendChild(option7c);
+        option7d = document.createElement("option");
+        option7d.id = 'string';
+        option7d.text = 'string';
+        option7d.value = 'string';
+        select7.appendChild(option7d);
+        option7e = document.createElement("option");
+        option7e.id = 'bool';
+        option7e.text = 'bool';
+        option7e.value = 'bool';
+        select7.appendChild(option7e);
+        cell7.appendChild(select7);
+        break;
+    case 2:
+        //field for prog
+        cell1.id = 'prog0';
+        textnode1=document.createTextNode(prog);
+        cell1.appendChild(textnode1);
+        //field for key
+        input2 = document.createElement("INPUT");
+        input2.id = '1key0';
         cell2.appendChild(input2);
+        input10 = document.createElement("INPUT");
+        input10.id = '2key0';
+        cell10.appendChild(input10);
+        //field for val
+        cell3.id = 'newval0';
+        textnode3=document.createTextNode("NULL");
         cell3.appendChild(textnode3);
-        cell4.appendChild(input4);
-        cell5.appendChild(input5);
-        //cell5.appendChild(button5);
-    }
-    //case add new val for same key
-    else
-    {
+        updcase=1;
+        //field type
+        select7 = document.createElement("select");
+        select7.id = 'newtype0';
+        select7.option = 'string';
+        option7a = document.createElement("option");
+        option7a.text = '-';
+        option7a.value = '';
+        select7.appendChild(option7a);
+        option7b = document.createElement("option");
+        option7b.id = 'int';
+        option7b.text = 'int';
+        option7b.value = 'int';
+        select7.appendChild(option7b);
+        option7c = document.createElement("option");
+        option7c.id = 'float';
+        option7c.text = 'float';
+        option7c.value = 'float';
+        select7.appendChild(option7c);
+        option7d = document.createElement("option");
+        option7d.id = 'string';
+        option7d.text = 'string';
+        option7d.value = 'string';
+        select7.appendChild(option7d);
+        option7e = document.createElement("option");
+        option7e.id = 'bool';
+        option7e.text = 'bool';
+        option7e.value = 'bool';
+        select7.appendChild(option7e);
+        cell7.appendChild(select7);
+        break;
+    case 3:
+        //field for prog
         cell1.id = 'prog0';
-        cell2.id = 'key0';
         textnode1=document.createTextNode(prog);
-        textnode2=document.createTextNode(key);
+        cell1.appendChild(textnode1);
+        //field for key
+        cell2.id = '1key0';
+        textnode2=document.createTextNode(key1);
+        cell2.appendChild(textnode2);
+        cell10.id = '2key0';
+        textnode10=document.createTextNode(key2);
+        cell10.appendChild(textnode10);
+        //field for val
         input3 = document.createElement("INPUT");
         input3.id = 'newval0';
-        input4 = document.createElement("INPUT");
-        input4.id = 'newdescr0';
-        input5 = document.createElement("input");
-        input5.value = 'Add';
-        input5.type = "button";
-        input5.onclick = function () { UpdateRow(2, 0); onclick() }
-        cell1.appendChild(textnode1);
-        cell2.appendChild(textnode2);
         cell3.appendChild(input3);
-        cell4.appendChild(input4);
-        cell5.appendChild(input5);
-    }
-    row.appendChild(cell0);
-    row.appendChild(cell1);
-    row.appendChild(cell2);
-    row.appendChild(cell3);
-    row.appendChild(cell4);
-    row.appendChild(cell5);
+        updcase=2;
+        //field type
+        cell7.id = 'type0';
+        textnode7 = document.createTextNode(type);
+        cell7.appendChild(textnode7);
+        break;
+    default:
+        break;
+    }
+
+    //field descr
+    input4 = document.createElement("INPUT");
+    input4.id = 'newdescr0';
+    cell4.appendChild(input4);
+    //field action
+    input5a = document.createElement("input");
+    input5a.value = 'Add';
+    input5a.type = "button";
+    input5a.onclick = function () { UpdateRow(updcase, 0); onclick() }
+    cell5.appendChild(input5a);
+    input5b = document.createElement("input");
+    input5b.value = 'Cancel';
+    input5b.type = "button";
+    input5b.onclick = function () { CancelAddRow(prog, key); onclick() }
+    cell5.appendChild(input5b);
+
+    row.appendChild(cell0);//index, counter
+    row.appendChild(cell6);//valid from
+    row.appendChild(cell1);//prog
+    row.appendChild(cell2);//key1
+    row.appendChild(cell10);//key2
+    row.appendChild(cell3);//value
+    row.appendChild(cell7);//type
+    row.appendChild(cell4);//descr
+    row.appendChild(cell5);//action
+    row.appendChild(cell8);//min
+    row.appendChild(cell9);//max
     tabBody.appendChild(row);
 
     // to be removed later when solution for adding multiple new rows is found
-    document.getElementById('addrow').style.display = "none";
+    document.getElementById('addrowvals').style.display = "none";
+    document.getElementById('addrowvals2').style.display = "none";
+    document.getElementById('addrowkey').style.display = "none";
+    document.getElementById('addrowprog').style.display = "none";
+}
+
+function CancelAddRow(prog, key)
+{
+    //get select program and key
+    if (!prog)
+    {
+        progselect=document.getElementById("prog");
+        prog=progselect.options[progselect.selectedIndex].value;
+    }
+    if (!key)
+    {
+        keyselect=document.getElementById("key");
+        key=keyselect.options[keyselect.selectedIndex].value;
+    }
+    //reload the original content in case of cancel
+    get_values(1, prog, key);
+
+    // to be removed later when solution for adding multiple new rows is found
+    document.getElementById('addrowvals').style.display = "";
+    document.getElementById('addrowvals2').style.display = "";
+    document.getElementById('addrowkey').style.display = "";
+    document.getElementById('addrowprog').style.display = "";
+}
+
+function CancelUpdate()
+{
+    //get select program and key
+    progselect=document.getElementById("prog");
+    prog=progselect.options[progselect.selectedIndex].value;
+    keyselect=document.getElementById("key");
+    key=keyselect.options[keyselect.selectedIndex].value;
+    //reload the original table in case of cancel
+    get_values(1, prog, key);
 }
 
 function EditRow(index)
 {
+    for (var i = 1 ; i <= document.getElementById('maxindex').innerHTML ; i++)
+        if(document.getElementById('action'+i) && i!=index)
+            //document.getElementById('action'+i).innerHTML = "";
+            document.getElementById('action'+i).style.display = "none";
+
+    //get current values
     oldval=document.getElementById('val'+index).innerHTML;
-    document.getElementById('val'+index).innerHTML = "<input align='right' id='newval"+index+"' value='"+oldval+"'>";
     olddescr=document.getElementById('descr'+index).innerHTML;
+    oldtype=document.getElementById('type'+index).innerHTML;
+    if (!oldval)
+        delcase=6;
+    else
+        delcase=4;
+    //set field for value
+    if (oldtype=='bool')
+    {
+        if (oldval=='yes')
+            textval="<input name='boolbox' type='radio' id='newval"+index+"' value='yes' checked> yes";
+        else
+            textval="<input name='boolbox' type='radio' id='newval"+index+"' value='yes'> yes";
+        if (oldval=='no')
+            textval=textval+"<input name='boolbox' type='radio' id='newval"+index+"' value='no' checked> no"
+        else
+            textval=textval+"<input name='boolbox' type='radio' id='newval"+index+"' value='no'> no"
+    }
+    else
+        textval="<input align='right' id='newval"+index+"' value='"+oldval+"'>";
+    document.getElementById('val'+index).innerHTML = textval;
+    //set field for description
     document.getElementById('descr'+index).innerHTML = "<input align='right' id='newdescr"+index+"' value='"+olddescr+"'>";
-    document.getElementById('action'+index).innerHTML =
-        "<input type='button' value='Update' onclick='UpdateRow(3,"+index+")'>"
-        +"<input type='button' value='Delete' onclick='UpdateRow(4,"+index+")'>";
+    /*
+    //set field for type
+    typeselecttext="<select id='newtype"+index+"' value='"+oldtype+"'>"+"<option>-</option>";
+    if (oldtype=='int')
+        typeselecttext=typeselecttext+"<option selected>int</option>";
+    else
+        typeselecttext=typeselecttext+"<option>int</option>";
+    if (oldtype=='float')
+        typeselecttext=typeselecttext+"<option selected>float</option>";
+    else
+        typeselecttext=typeselecttext+"<option>float</option>";
+    if (oldtype=='string')
+        typeselecttext=typeselecttext+"<option selected>string</option>";
+    else
+        typeselecttext=typeselecttext+"<option>string</option>";
+    if (oldtype=='bool')
+        typeselecttext=typeselecttext+"<option selected>bool</option>";
+    else
+        typeselecttext=typeselecttext+"<option>bool</option>";
+    typeselecttext=typeselecttext+"</select>";
+    document.getElementById('type'+index).innerHTML = typeselecttext;
+    */
+
+    //set field for action
+    actiontext="<input type='button' value='Update' onclick='UpdateRow(3,"+index+")'>";
+    notyetvalid=document.getElementById('notyetvalid'+index)
+    if (delcase!=6 || notyetvalid)
+        actiontext=actiontext+"<input type='button' value='Delete' onclick='UpdateRow("+delcase+","+index+")'>";
+    actiontext=actiontext+"<input type='button' value='Cancel' onclick='CancelUpdate()'>";
+    document.getElementById('action'+index).innerHTML =actiontext;
 }
 
 function UpdateRow(updcase,index)
 {
-    // three cases of update
-    // 1: add new key
-    // 2: add new val (for same key)
-    // 3: edit val
-    // 4: delete val (ie set to NULL)
-    if (updcase==1)
-    {
+    // five cases of update              corresponds to
+    // 5: add new prog + key             addcase 1
+    // 1: add new key                    addcase 2
+    // 2: add new val (for same key)     addcase 3        -> same type
+    // 3: edit val                                        -> same type
+    // 4: delete val (ie set to NULL)                     -> same type
+    // 6: delete new key (ie val still NULL) -> real delete
+    descr=document.getElementById('newdescr'+index).value;
+    origindex=0;
+    switch(updcase)
+    {
+    case 5:
         prog=document.getElementById('prog'+index).value;
-        key=document.getElementById('key'+index).value;
-    }
-    else
-    {
+        key1=document.getElementById('1key'+index).value;
+        key2=document.getElementById('2key'+index).value;
+        val=document.getElementById('newval'+index).innerHTML;
+        type=document.getElementById('newtype'+index).value;
+        break;
+    case 1:
+        //prog=document.getElementById('prog'+index).value;
         prog=document.getElementById('prog'+index).innerHTML;
-        key=document.getElementById('key'+index).innerHTML;
-    }
-    if (updcase==1)
+        key1=document.getElementById('1key'+index).value;
+        key2=document.getElementById('2key'+index).value;
         val=document.getElementById('newval'+index).innerHTML;
-    else
+        type=document.getElementById('newtype'+index).value;
+        break;
+    case 2:
+        //prog=document.getElementById('prog'+index).value;
+        prog=document.getElementById('prog'+index).innerHTML;
+        key1=document.getElementById('1key'+index).innerHTML;
+        key2=document.getElementById('2key'+index).innerHTML;
         val=document.getElementById('newval'+index).value;
-    descr=document.getElementById('newdescr'+index).value;
+        type=document.getElementById('type'+index).innerHTML;
+        break;
+    case 3:
+        prog=document.getElementById('prog'+index).innerHTML;
+        key1=document.getElementById('1key'+index).innerHTML;
+        key2=document.getElementById('2key'+index).innerHTML;
+        type=document.getElementById('type'+index).innerHTML;
+        val="hallo";
+        if (type=="bool")
+        {
+            radio=document.getElementsByName('boolbox');
+            for (i=0; i< radio.length; i++)
+                if (radio[i].checked)
+                    val=radio[i].value;
+        }
+        else
+            val=document.getElementById('newval'+index).value;
+        origindex=document.getElementById('1origindex'+index).innerHTML;
+        break;
+    case 4:
+    case 6:
+        answer = confirm("Do you really want to delete the value?");
+        if (!answer)
+            return;
+        prog=document.getElementById('prog'+index).innerHTML;
+        key1=document.getElementById('1key'+index).innerHTML;
+        key2=document.getElementById('2key'+index).innerHTML;
+        val=document.getElementById('newval'+index).value;
+        if (!val)
+            val="NULL";
+        type=document.getElementById('type'+index).innerHTML;
+        origindex=document.getElementById('1origindex'+index).innerHTML;
+        break;
+    }
     if (!descr)
         alert("Please insert description!");
-    if (!key)
-        alert("Please insert key!");
+    if (!key1)
+        alert("Please insert key1!");
+    if (!key2)
+        alert("Please insert key2!");
     if (!val)
         alert("Please insert value!");
-    if (!val || !descr || !key)
+    if (!val || !descr || !key1 || !key2)
         return;
     if (index==0)
         index=-1;
 
-    phpcall="insert_po.php?fUpdCase="+updcase+"&fIndex="+index+"&fProgram="+prog+"&fKey="+key+"&fValue="+val+"&fDescription="+descr;
-    //alert("u/i --- "+index+" --- "+prog+" --- "+key+" --- "+val+" --- "+descr);
+    //check if value has correct type
+    //  for string no check needed
+    if (updcase==3)
+    {
+        if (type=='int' && parseInt(val)!=val)
+        {
+            alert("Value is not a int.");
+            return;
+        }
+        if (type=='float' && parseFloat(val)!=val)
+        {
+            alert("Value is not a float.");
+            return;
+        }
+        if (type=='bool' && val!='yes' && val!='no')
+        {
+            alert("Value is neither 'yes' not 'no'.");
+            return;
+        }
+    }
+    if (!type && (updcase==2 || updcase==3 || updcase==4))
+    {
+        alert("Please select type!");
+        return;
+    }
+
+    phpcall="insert_po.php?fUpdCase="+updcase+"&fIndex="+index+"&fOriginalIndex="+origindex+"&fProgram="+prog
+        +"&fKey1="+key1+"&fKey2="+key2+"&fValue="+val+"&fDescription="+descr+"&fType="+type;
     //alert(phpcall);
 
-    var xmlhttp;
-    if (window.XMLHttpRequest)
-    {// code for IE7+, Firefox, Chrome, Opera, Safari
-        xmlhttp=new XMLHttpRequest();
-    }
-    else
-    {// code for IE6, IE5
-        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
-    }
     xmlhttp.open("GET", phpcall, false);
     xmlhttp.send();
+
+    // get somehow return value of inser_po.php
+    //   to catch cases where insert was rejected due to no update
     if (xmlhttp.readyState==4 && xmlhttp.status==200)
+    {
+        if (xmlhttp.responseText=="no change")
+        {
+            alert("You didn't change anything.");
+            return;
+        }
+        if (xmlhttp.responseText=="key exists")
+        {
+            alert("This key already exists. You cannot insert it a second time.");
+            return;
+        }
+        if (xmlhttp.responseText=="prog exists")
+        {
+            alert("This program already exists. You cannot insert it a second time.");
+            return;
+        }
         alert(xmlhttp.responseText);
+    }
     else
         alert("Mist! "+xmlhttp.responseText);
 
-    var xmlhttp2;
-    if (window.XMLHttpRequest)
-    {// code for IE7+, Firefox, Chrome, Opera, Safari
-        xmlhttp2=new XMLHttpRequest();
-    }
-    else
-    {// code for IE6, IE5
-        xmlhttp2=new ActiveXObject("Microsoft.XMLHTTP");
-    }
-    xmlhttp2.open("GET","get_values.php?fKey="+key+"&fProgram="+prog,false);
-    xmlhttp2.send();
-    if (xmlhttp2.readyState==4 && xmlhttp2.status==200)
-    {
-        document.getElementById('valuefield').innerHTML = xmlhttp2.responseText;
-        if (key=='newkey')
-            addRow(prog, key);
-    }
-    else
-        document.getElementById('valuefield').innerHTML = "argh";
-}
-
-
-//    printf("<div id='showcurrent'>\n<button onClick='ShowCurrent();'>Show Current</button>\n</div>\n");
+    //reload the content of the page
+    progselect=document.getElementById("prog");
+    if (!progselect)
+        oldprog=prog;
+    else
+        if (progselect.options[progselect.selectedIndex])
+            oldprog=progselect.options[progselect.selectedIndex].value;
+        else
+            oldprog=prog;
+    keyselect=document.getElementById("key");
+    if (!keyselect)
+        oldkey=key1+"."+key2;
+    else
+        if (keyselect.options[keyselect.selectedIndex])
+            oldkey=keyselect.options[keyselect.selectedIndex].value;
+        else
+            oldkey=key1+"."+key2;
+    //reload progs and keys
+    get_progs(oldprog, "10", "edit");
+    get_keys(oldprog, oldkey, "10", "edit");
+    //reload values
+    xmlhttp.open("GET","get_values.php?fGetValCase=1&fKey="+oldkey+"&fProgram="+oldprog,false);
+    xmlhttp.send();
+    if (xmlhttp.readyState==4 && xmlhttp.status==200)
+        document.getElementById('valuefield1').innerHTML = xmlhttp.responseText;
+    else
+        document.getElementById('valuefield1').innerHTML = "argh";
+}
 
 function ShowCurrent()
@@ -213,5 +626,7 @@
             document.getElementById('old'+i).style.display = "none";
     document.getElementById('showcurrent').style.display = "none";
+    document.getElementById('showcurrent2').style.display = "none";
     document.getElementById('showall').style.display = "";
+    document.getElementById('showall2').style.display = "";
 }
 
@@ -220,8 +635,258 @@
     for (var i = 1 ; i <= document.getElementById('maxindex').innerHTML ; i++)
         if(document.getElementById('old'+i))
+        {
             document.getElementById('old'+i).style.display = "";
+            document.getElementById('old'+i).style.color = "grey";
+        }
     document.getElementById('showcurrent').style.display = "";
+    document.getElementById('showcurrent2').style.display = "";
     document.getElementById('showall').style.display = "none";
-}
-
-
+    document.getElementById('showall2').style.display = "none";
+}
+
+function InsertRow(table, row)
+{
+    var x=document.getElementById('valtable'+table).insertRow(row);
+    for (var i = 0 ; i < 9 ; i++)
+    {
+        y=x.insertCell(i);
+        y.innerHTML="&nbsp;";
+    }
+}
+
+function CompareValues()
+{
+    counter=0;
+    maxindex2=document.getElementById('maxindex').innerHTML;
+    maxindex3=document.getElementById('maxindex3').innerHTML;
+    //alert(maxindex2);
+    //alert(maxindex3);
+    if (parseInt(maxindex2)>parseInt(maxindex3))
+        maxindex=maxindex2;
+    else
+        maxindex=maxindex3;
+
+    for (var i = 1 ; i <= maxindex ; i++)
+    {
+        if (document.getElementById('2compare'+i))
+        {
+            counter++;
+            if (!document.getElementById('3compare'+i))
+            {
+                InsertRow(3,counter);
+                document.getElementById('2compare'+i).style.color='red';
+            }
+            else
+            {
+                val2=document.getElementById('2compare'+i).cells[5].innerHTML;
+                val3=document.getElementById('3compare'+i).cells[5].innerHTML;
+                if (val2!=val3)
+                {
+                    document.getElementById('2compare'+i).style.color='blue';
+                    document.getElementById('3compare'+i).style.color='blue';
+                }
+            }
+        }
+        else
+            if (document.getElementById('3compare'+i))
+            {
+                counter++;
+                InsertRow(2,counter);
+                document.getElementById('3compare'+i).style.color='green';
+            }
+    }
+    //document.getElementById('comparevals').style.display = "none";
+}
+
+function HideDescription(getvalcase)
+{
+    for(var i=0;i<document.getElementsByTagName('*').length;i++)
+        if(document.getElementsByTagName('*')[i].className == 'description'+getvalcase)
+            document.getElementsByTagName('*')[i].style.display = 'none';
+    document.getElementById('hidedescr'+getvalcase).style.display = "none";
+    document.getElementById('showdescr'+getvalcase).style.display = "";
+}
+
+function ShowDescription(getvalcase)
+{
+    for(var i=0;i<document.getElementsByTagName('*').length;i++)
+        if(document.getElementsByTagName('*')[i].className == 'description'+getvalcase)
+            document.getElementsByTagName('*')[i].style.display = '';
+    document.getElementById('showdescr'+getvalcase).style.display = "none";
+    document.getElementById('hidedescr'+getvalcase).style.display = "";
+}
+
+function ShowDateInput(idx)
+{
+    document.getElementById('datepulldown'+idx).style.display = "none";
+    document.getElementById('dateinput'+idx).style.display = "";
+    set_statusdate(idx, get_statusdate(idx, 'pulldown'));
+}
+
+function ShowDatePulldown(idx)
+{
+    document.getElementById('datepulldown'+idx).style.display = "";
+    document.getElementById('dateinput'+idx).style.display = "none";
+    set_statusdate(idx, get_statusdate(idx, 'textinput'));
+}
+
+function reset_dates(datename, dateval, dateval2, idx)
+{
+    radio=document.getElementsByName('dateformat'+idx);
+    for (i=0; i< radio.length; i++)
+        if (radio[i].checked)
+            val=radio[i].value;
+    if (val=="pulldown")
+    {
+        //alert("reset:"+datename+"-"+dateval+"-"+dateval2+"-"+idx);
+        if (datename=="year")
+        {
+            get_dates("year", dateval, dateval2, idx);
+            get_dates("month", dateval, dateval2, idx);
+        }
+
+        if (datename=="year" || datename=="month")
+            if (datename=="month")
+                get_dates("day", dateval, dateval2, idx);
+            else
+                get_dates("day", "empty", "empty", idx);
+
+        if (datename=="year" || datename=="month" || datename=="day")
+            if (datename=="day")
+                get_dates("hour", dateval, dateval2, idx);
+            else
+                get_dates("hour", "empty", "empty", idx);
+
+        if (datename=="year" || datename=="month" || datename=="day" || datename=="hour")
+            if (datename=="hour")
+                get_dates("min", dateval, dateval2, idx);
+            else
+                get_dates("min", "empty", "empty", idx);
+
+        if (datename=="year" || datename=="month" || datename=="day" || datename=="hour" || datename=="min")
+            if (datename=="min")
+                get_dates("sec", dateval, dateval2, idx);
+            else
+                get_dates("sec", "empty", "empty", idx);
+    }
+    if (val=="textinput")
+        document.getElementById('statusdate'+idx).value="";
+}
+function get_dates(datename, dateval, datevalprev, idx, reset)
+{
+    //alert(datename+"_"+dateval+"_"+datevalprev+"_"+idx);
+    phpcall="get_date.php?fDateValue="+dateval+"&fDateValuePrev="+datevalprev+"&fDateName="+datename+"&fIdx="+idx;
+    //alert(phpcall);
+    xmlhttp.open("GET",phpcall,false);
+    xmlhttp.send();
+    if (xmlhttp.readyState==4 && xmlhttp.status==200)
+        document.getElementById(datename+'select'+idx).innerHTML = xmlhttp.responseText;
+    else
+        document.getElementById(datename+'select'+idx).innerHTML = "argh";
+
+    //to be able to do no reset in case when the function is called from reset_dates()
+    if (reset)
+        reset_dates(datename, "empty", "empty", idx);
+}
+
+function set_statusdate(getvalcase, statusdate)
+{
+    //alert(statusdate);
+
+    radio=document.getElementsByName('dateformat'+getvalcase);
+    for (i=0; i< radio.length; i++)
+        if (radio[i].checked)
+            val=radio[i].value;
+    if (val=="pulldown")
+    {
+        //check whole string
+        if (statusdate=="")
+        {
+            //set all values to 'empty'
+            get_dates("year", "empty", "empty", getvalcase, "yes");
+            return;
+        }
+        dates=statusdate.split("-",3);
+        //check year
+        if (!dates[0])
+        {
+            get_dates("year", "empty", "empty", getvalcase, "yes");
+            return;
+        }
+        else
+            get_dates("year", dates[0], dates[0], getvalcase);
+        //check month
+        if (!dates[1])
+        {
+            get_dates("month", "empty", "empty", getvalcase, "yes");
+            return;
+        }
+        else
+            get_dates("month", dates[1], dates[0], getvalcase);
+        //check rest of string
+        if (!dates[2])
+        {
+            get_dates("day", "empty", "empty", getvalcase, "yes");
+            return;
+        }
+        rests=dates[2].split(" ",2);
+        //check day
+        if (!rests[0])
+        {
+            get_dates("day", "empty", "empty", getvalcase, "yes");
+            return;
+        }
+        else
+            get_dates("day", rests[0], dates[1], getvalcase);
+        //check rest of string
+        if (!rests[1])
+        {
+            get_dates("hour", "empty", "empty", getvalcase, "yes");
+            return;
+        }
+        times=rests[1].split(":",3);
+        //check hour
+        if (!times[0])
+        {
+            get_dates("hour", "empty", "empty", getvalcase, "yes");
+            return;
+        }
+        else
+            get_dates("hour", times[0], rests[0], getvalcase);
+        //check minutes
+        if (!times[1])
+        {
+            get_dates("min", "empty", "empty", getvalcase, "yes");
+            return;
+        }
+        else
+            get_dates("min", times[1], times[0], getvalcase);
+        //check seconds
+        if (!times[2])
+        {
+            get_dates("sec", "empty", "empty", getvalcase, "yes");
+            return;
+        }
+        else
+            get_dates("sec", times[2], times[1], getvalcase);
+
+    }
+    if (val=="textinput")
+        document.getElementById('statusdate'+getvalcase).value=statusdate;
+
+}
+
+
+function get_timestamp(getvalcase, direction)
+{
+    statusdate=get_statusdate(getvalcase);
+
+    xmlhttp.open("GET","get_timestamp.php?fStatusDate="+statusdate+"&fDirection="+direction,false);
+    xmlhttp.send();
+    if (xmlhttp.readyState==4 && xmlhttp.status==200)
+        newstatusdate = xmlhttp.responseText;
+    else
+        newstatusdate = "argh";
+
+    set_statusdate(getvalcase, newstatusdate);
+}
