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, mode)
{
    xmlhttp.open("GET","get_progs.php?fProgram="+prog+"&fMode="+mode,false);
    xmlhttp.send();
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        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, mode)
{
    xmlhttp.open("GET","get_keys.php?fProgram="+prog+"&fKey="+key+"&fMode="+mode,false);
    xmlhttp.send();
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
        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;
        statusdate=(year =='empty'? '':year);
        statusdate=statusdate+(month=='empty'? '':'-'+month);
        statusdate=statusdate+(day  =='empty'? '':'-'+day);
        statusdate=statusdate+(hour =='empty'? '':' '+hour);
        statusdate=statusdate+(min  =='empty'? '':':'+min);
        statusdate=statusdate+(sec  =='empty'? '':':'+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)
{
    //in this case the given 'prog' and 'key' are ignored
    if (parseInt(getvalcase)>1)
    {
        //get prog and key
        progselect=document.getElementById("prog");
        if (!progselect)
            prog="all";
        else
            if (progselect.selectedIndex>=0)
                prog=progselect.options[progselect.selectedIndex].value;
            else
                prog="all";
        keyselect=document.getElementById("key");
        if (!keyselect)
            key="all";
        else
            if (keyselect.selectedIndex>=0)
                key=keyselect.options[keyselect.selectedIndex].value;
            else
                key="all";
    }
    //if (mode=="view" && getvalcase==1)
    //    return;
    if ((getvalcase==2 || getvalcase==3) && document.getElementById('statusdate'+getvalcase))
    {
        statusdate=get_statusdate(getvalcase);
        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, min, max, descr)
{
    if (!prog)
    {
        progselect=document.getElementById("prog");
        if (progselect.selectedIndex>=0)
            prog=progselect.options[progselect.selectedIndex].value;
        else
            prog='all';
    }
    if (!key)
    {
        keyselect=document.getElementById("key");
        if (!keyselect)
            key='all';
        else
            if (keyselect.selectedIndex>=0)
                key=keyselect.options[keyselect.selectedIndex].value;
            else
                key='all';
    }
    switch(addcase)
    {
    case 1:
        get_keys(prog, key, "edit");
    case 2:
        get_values(1, prog, key, "edit");
    }
    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';
    row.bgColor='#C8C8C8';
    cell0 = document.createElement("TD");
    cell1 = document.createElement("TD");
    cell2 = document.createElement("TD");
    cell3 = document.createElement("TD");
    cell4 = document.createElement("TD");
    cell5 = document.createElement("TD");
    cell6 = document.createElement("TD");
    cell7 = document.createElement("TD");
    cell8 = document.createElement("TD");
    cell9 = document.createElement("TD");
    cell10 = document.createElement("TD");
    cell11 = document.createElement("TD");
    cell0.className="indices1";
    cell6.className="validsince1";
    cell11.className="user1";
    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/min/max fixed
    //field descr
    input4 = document.createElement("INPUT");
    input4.style.width = 300;
    input4.id = 'newdescr0';
    switch(addcase)
    {
    case 1:
        updcase=5;
        //field for prog
        input1 = document.createElement("INPUT");
        input1.style.width = 80;
        input1.id = 'prog0';
        cell1.appendChild(input1);
        //fields for keys
        input2 = document.createElement("INPUT");
        input2.style.width = 200;
        input2.id = '1key0';
        cell2.appendChild(input2);
        input10 = document.createElement("INPUT");
        input10.id = '2key0';
        input10.style.width = 100;
        cell10.appendChild(input10);
        //field for val
        cell3.id = 'newval0';
        textnode3=document.createTextNode("NULL");
        cell3.appendChild(textnode3);
        //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);
        //field for min
        input8 = document.createElement("INPUT");
        input8.id = 'newmin0';
        input8.style.width = 50;
        cell8.appendChild(input8);
        //field for max
        input9 = document.createElement("INPUT");
        input9.id = 'newmax0';
        input9.style.width = 50;
        cell9.appendChild(input9);
        break;
    case 2:
        updcase=1;
        //field for prog
        cell1.id = 'prog0';
        textnode1=document.createTextNode(prog);
        cell1.appendChild(textnode1);
        //field for key
        input2 = document.createElement("INPUT");
        input2.id = '1key0';
        input2.style.width = 200;
        if (key1)
            input2.value = key1;
        cell2.appendChild(input2);
        input10 = document.createElement("INPUT");
        input10.id = '2key0';
        input10.style.width = 100;
        if (key2)
            input10.value = key2;
        cell10.appendChild(input10);
        //field for val
        cell3.id = 'newval0';
        textnode3=document.createTextNode("NULL");
        cell3.appendChild(textnode3);
        //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);
        //field for min
        input8 = document.createElement("INPUT");
        input8.id = 'newmin0';
        input8.style.width = 50;
        if (min)
            input8.value = min;
        cell8.appendChild(input8);
        //field for max
        input9 = document.createElement("INPUT");
        input9.id = 'newmax0';
        input9.style.width = 50;
        if (max)
            input9.value = max;
        cell9.appendChild(input9);
        //field for descr
        if (descr)
            input4.value = descr;
        break;
    case 3:
        updcase=2;
        //field for prog
        cell1.id = 'prog0';
        textnode1=document.createTextNode(prog);
        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.style.width = 200;
        input3.id = 'newval0';
        cell3.appendChild(input3);
        //field type
        cell7.id = 'type0';
        textnode7 = document.createTextNode(type);
        cell7.appendChild(textnode7);
        //field min
        cell8.id = 'min0';
        textnode8 = document.createTextNode(min);
        cell8.appendChild(textnode8);
        //field max
        cell9.id = 'max0';
        textnode9 = document.createTextNode(max);
        cell9.appendChild(textnode9);
        break;
    default:
        break;
    }
    //rest of field descr
    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(cell8);//min
    row.appendChild(cell9);//max
    row.appendChild(cell4);//descr
    row.appendChild(cell5);//action
    row.appendChild(cell11);//user
    tabBody.appendChild(row);
    ShowHide('indices', 'sh_indices', 1)
    ShowHide('validsince', 'sh_validsince', 1)
    ShowHide('user', 'sh_user', 1)
    //disabeling all other edit/add buttons
    // (to be removed later when solution for adding multiple new rows is found)
    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, "edit");
    //disabeling all other edit/add buttons
    // (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
    document.getElementById('addrowprog').style.display = "";
    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, "edit");
}
function EditRow(index)
{
    //disabeling all other edit/add buttons
    // (to be removed later when solution for adding multiple new rows is found)
    for (var i = 1 ; i <= document.getElementById('maxindex').innerHTML ; i++)
        if(document.getElementById('action'+i) && i!=index)
            //document.getElementById('action'+i).style.display = "none";
            document.getElementById('action'+i).innerHTML = "";
    document.getElementById('addrowvals').style.display = "none";
    document.getElementById('addrowvals2').style.display = "none";
    document.getElementById('addrowkey').style.display = "none";
    document.getElementById('addrowprog').style.display = "none";
    //get current values
    oldval=document.getElementById('val'+index).innerHTML;
    olddescr=document.getElementById('descr'+index).innerHTML;
    oldtype=document.getElementById('type'+index).innerHTML;
    // added to make min/max editable
    oldmin=document.getElementById('min'+index).innerHTML;
    oldmax=document.getElementById('max'+index).innerHTML;
    if (!oldval)
        delcase=6;
    else
        delcase=4;
    //set field for value
    if (oldtype=='bool')
    {
        if (oldval=='yes')
            textval=" yes";
        else
            textval=" yes";
        if (oldval=='no')
            textval=textval+" no"
        else
            textval=textval+" no"
    }
    else
        textval="";
    document.getElementById('val'+index).innerHTML = textval;
    //set field for description
    document.getElementById('descr'+index).innerHTML = "";
    if (oldtype=='int' || oldtype=='float')
    {
        document.getElementById('min'+index).innerHTML = "";
        document.getElementById('max'+index).innerHTML = "";
    }
    /*
    //set field for type
    typeselecttext="";
    document.getElementById('type'+index).innerHTML = typeselecttext;
    */
    //set field for action
    actiontext="";
    notyetvalid=document.getElementById('notyetvalid'+index)
    if (delcase!=6 || notyetvalid)
        actiontext=actiontext+"";
    actiontext=actiontext+"";
    document.getElementById('action'+index).innerHTML =actiontext;
}
function UpdateRow(updcase,index)
{
    // 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,min,max
    // 3: edit val                                        -> same type,min,max
    // 4: delete val (ie set to NULL)                     -> same type,min,max
    // 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;
        key1=document.getElementById('1key'+index).value;
        key2=document.getElementById('2key'+index).value;
        val=document.getElementById('newval'+index).innerHTML;
        type=document.getElementById('newtype'+index).value;
        min=document.getElementById('newmin'+index).value;
        max=document.getElementById('newmax'+index).value;
        break;
    case 1:
        //prog=document.getElementById('prog'+index).value;
        prog=document.getElementById('prog'+index).innerHTML;
        key1=document.getElementById('1key'+index).value;
        key2=document.getElementById('2key'+index).value;
        val=document.getElementById('newval'+index).innerHTML;
        type=document.getElementById('newtype'+index).value;
        min=document.getElementById('newmin'+index).value;
        max=document.getElementById('newmax'+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;
        type=document.getElementById('type'+index).innerHTML;
        min=document.getElementById('min'+index).innerHTML;
        max=document.getElementById('max'+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;
        // changed to make min/max editable
        if (type=='int' || type=='float')
        {
            min=document.getElementById('newmin'+index).value;
            max=document.getElementById('newmax'+index).value;
        }
        else
        {
            min=document.getElementById('min'+index).innerHTML;
            max=document.getElementById('max'+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;
        min=document.getElementById('min'+index).innerHTML;
        max=document.getElementById('max'+index).innerHTML;
        origindex=document.getElementById('1origindex'+index).innerHTML;
        break;
    }
    if (!descr)
        alert("Please insert description!");
    if (!key1)
        alert("Please insert key1!");
    if (!val)
        alert("Please insert value!");
    if (!val || !descr || !key1)
        return;
    if (index==0)
        index=-1;
    if (!type && (updcase==2 || updcase==3 || updcase==4))
    {
        alert("Please select type!");
        return;
    }
    //check if value has correct type and is within ranges
    //  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;
        }
        // check ranges for int
        if (max && type=='int' && parseInt(val)>parseInt(max))
        {
            alert("You cannot enter a value larger than "+max+".");
            return;
        }
        if (min && type=='int' && parseInt(val)parseFloat(max))
        {
            alert("You cannot enter a value larger than "+max+".");
            return;
        }
        if (min && type=='float' && parseFloat(val)=0)
            oldprog=progselect.options[progselect.selectedIndex].value;
        else
            oldprog=prog;
    keyselect=document.getElementById("key");
    if (!keyselect)
        oldkey=key1+"."+key2;
    else
        if (keyselect.selectedIndex>=0)
            oldkey=keyselect.options[keyselect.selectedIndex].value;
        else
            oldkey=key1+"."+key2;
    */
    //reload content of page for the values of prog and key1.key2
    //  which were just edited/added
    oldprog=prog;
    if (key2)
        oldkey=key1+"."+key2;
    else
        oldkey=key1;
    //reload progs and keys
    get_progs(oldprog, "edit");
    get_keys(oldprog, oldkey, "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()
{
    for (var i = 1 ; i <= document.getElementById('maxindex').innerHTML ; i++)
        if(document.getElementById('old'+i))
            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 = "";
}
function ShowAll()
{
    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);
    y=x.insertCell(0);
    y.colSpan='5';
    y.innerHTML=" ";
}
function CompareValues()
{
    counter=0;
    maxindex2=document.getElementById('maxindex').innerHTML;
    maxindex3=document.getElementById('maxindex3').innerHTML;
    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";
    //reload all setting for show-menu
    ShowHide('indices', 'sh_indices', 2)
    ShowHide('indices', 'sh_indices', 3)
    ShowHide('description', 'sh_descr', 2)
    ShowHide('description', 'sh_descr', 3)
    ShowHide('validsince', 'sh_validsince', 2)
    ShowHide('validsince', 'sh_validsince', 3)
    ShowHide('minimum', 'sh_min', 2)
    ShowHide('minimum', 'sh_min', 3)
    ShowHide('maximum', 'sh_max', 2)
    ShowHide('maximum', 'sh_max', 3)
    ShowHide('user', 'sh_user', 2)
    ShowHide('user', 'sh_user', 3)
}
function ShowHide(column, idname, getvalcase)
{
    box=document.getElementById(idname+getvalcase);
    text=box.checked? '':'none';
    for(var i=0;i