$(document).ready(function() {
    $(".tr-toggle").click( function() {
        var state=$('.tr-toggle').attr('title')=='open' ? 'close' : 'open';
        $('.tr-toggle').text(state=='close' ? "Alles afsluiten" : "Alles openen");
        var trclass = state=='close' ?  'table-row' : 'none';
        $('tr[id*=toggle]').css('display',  trclass);
        $('.tr-toggle').attr('title',state);
    });
});

function checkSerieBoxes(select, form) {
    checkAllBoxes(select, form, 'check_serie,boeklijst,typelijst,modulelijst');
}

function checkAllBoxes(select, form, lijstnaam) {
    var lijsten = lijstnaam.split(',');
    inputfields = form.getElementsByTagName('input');
    for (var i=0; i<inputfields.length; i++) {
        for (var nr=0; nr<lijsten.length; nr++) {
            if (inputfields[i].name.indexOf(lijsten[nr]) == 0) inputfields[i].checked=select;
        }
    }
}

/**
 * Veranderen van het aantal vragen en aantal toetsen behorende bij
 * het gegeven vraagtype
 */
function veranderAantal(vraagtypeId) {
    aantalVragenVeld = document.getElementById('aantalVragen');
    aantalToetsenVeld = document.getElementById('aantalToetsen');
    aantalKerenResettenVeld = document.getElementById('aantalKerenResetten');
    if (aantalVragenVeld!=null && aantalToetsenVeld!=null && aantalKerenResettenVeld!=null) {
        xmlhttp = getXMLHTTP();
        xmlhttp.open("GET", "?action=admin.module.getAantalVragen&vraagtypeId="+vraagtypeId, false);
        xmlhttp.send(null);
        if (xmlhttp.status == 200) {
            aantal = xmlhttp.responseText.split(",");
            aantalVragenVeld.value=aantal[0];
            aantalToetsenVeld.value=aantal[1];
            aantalKerenResettenVeld.value=aantal[2];
        }
    }
}

function veranderHoofdstukken(boekId) {
    aantalHoofdstukken = 0;
    xmlhttp = getXMLHTTP();
    xmlhttp.open("GET", "?action=admin.module.getAantalHoofdstukken&boekId="+boekId, false);
    xmlhttp.send(null);
    if (xmlhttp.status == 200) aantalHoofdstukken = xmlhttp.responseText;

    hoofdstukTabel = document.getElementById('hoofdstukken');
    while (hoofdstukTabel.rows.length>0) hoofdstukTabel.deleteRow(hoofdstukTabel.rows.length-1);
    for (i=0; i<aantalHoofdstukken; i++) {
        row = hoofdstukTabel.insertRow(i);
        cell1 = row.insertCell(0);
        if (i==0) cell1.innerHTML = "Hoofdstukken:";
        cell2 = row.insertCell(1);
        cell2.innerHTML="<input type=\"checkbox\" name=\"hoofdstukken[]\" value=\""+(i+1)+"\">Hoofdstuk "+(i+1)+"</input>";
    }
    document.getElementById('aantalHoofdstukken').value=aantalHoofdstukken;
}

function toggleModuleActief(moduleId) {
    if (moduleId!=null && moduleId!='') {
        xmlhttp = getXMLHTTP();
        xmlhttp.open("GET", "?action=admin.module.toggleActief&moduleId="+moduleId, false);
        xmlhttp.send(null);
        if (xmlhttp.status == 200) {
            if (xmlhttp.responseText!='') alert(xmlhttp.responseText);
        }
    }
}

function toggleVraagActief(vraagId) {
    if (vraagId!=null && vraagId!='') {
        xmlhttp = getXMLHTTP();
        xmlhttp.open("GET", "?action=admin.vragen.toggleActief&vraagId="+vraagId, false);
        xmlhttp.send(null);
        if (xmlhttp.status == 200) {
            if (xmlhttp.responseText!='') alert(xmlhttp.responseText);
        }
    }
}

function toggleRowVisibility(rowId, hiddenValue) {
    row = document.getElementById(rowId);
    if (row!=null) {
        if (row.style.visibility==hiddenValue) row.style.visibility = 'visible';
        else row.style.visibility = hiddenValue;
    }
}

function checkSerie(serie, form) {
    var lijst = form['boeklijst['+serie+'][]'];
    checked=document.getElementById("check_serie_"+serie).checked?"checked":"";
    if (lijst.length) {
        for (var i=0; i<lijst.length; i++) {
            lijst[i].checked = checked;
	
            checkBoek(lijst[i].value, form);
        }
    } else {
        lijst.checked = checked;
        checkBoek(lijst.value, form);
    }

}
function checkBoek(boek, form) {
    var lijst = form['typelijst['+boek+']'];
    checked = document.getElementById("check_boek_"+boek).checked?"checked":"";
    if (lijst.length) {
        for (var i=0; i<lijst.length; i++) {
            lijst[i].checked = checked;
            checkType(boek, lijst[i].value, form);
        }
    } else {
        lijst.checked = checked;
        checkType(boek, lijst.value, form);
    }
}
function checkType(boek, type, form) {
    var lijst = form['modulelijst['+boek+']['+type+'][]'];
    checked = document.getElementById("check_type_"+boek+"_"+type).checked?"checked":"";
    if (lijst.length) {
        for (var i=0; i<lijst.length; i++) {
            lijst[i].checked = checked;
        }
    } else {
        lijst.checked = checked;
    }
}

modules = false;
rows = false;
function veranderModuleVolgorde(moduleId, direction) {
    var previous = null;
    if (modules == false) {
        modules = document.nieuwboek.elements["boekModules[]"];
    }
    for (i=0; i<modules.length; i++) {
        if (modules[i].value == moduleId) {
            var table = document.getElementById("boekModuleTable");
            if (rows==false) rows = table.tBodies[0].getElementsByTagName('tr');
			
            if (direction == 'up' && i>0) {
                previous = modules[i-1].value;
                current = modules[i].value;
				
                modules[i].value = previous;
                modules[i-1].value = current;
				
                previousVolgorde = document.getElementById("volgorde_module_"+previous).value;
                document.getElementById("volgorde_module_"+previous).value = document.getElementById("volgorde_module_"+current).value;
                document.getElementById("volgorde_module_"+current).value = previousVolgorde;

                //Switch de rows van de tabel boekModuleTable
                rowI = rows[i];
                rowJ = rows[i-1];
                table.tBodies[0].insertBefore(rowI, rowJ);
				
                //				var idRowI = rows[i].id;
                //				rows[i].id = rows[i-1].id;
                //				rows[i-1].id = idRowI;

                if (i == 1) {
                    rows[i].cells[2].style.visibility = "visible";
                    rows[i-1].cells[2].style.visibility = "hidden";
                }
                if (i==modules.length-1) {
                    rows[i].cells[1].style.visibility = "hidden";
                    rows[i-1].cells[1].style.visibility = "visible";
                }
				
                break;
            } else if (direction == 'down' && i<modules.length-1) {
                current = modules[i].value;
                next = modules[i+1].value;
				
                modules[i].value = next;
                modules[i+1].value = current;
				
                nextVolgorde = document.getElementById("volgorde_module_"+next).value;
                document.getElementById("volgorde_module_"+next).value = document.getElementById("volgorde_module_"+current).value;
                document.getElementById("volgorde_module_"+current).value = nextVolgorde;
				
                //Switch de rows van de tabel boekModuleTable
                rowI = rows[i];
                rowJ = rows[i+1];
                table.tBodies[0].insertBefore(rowJ, rowI);
				
                if (i == 0) {
                    rows[i].cells[2].style.visibility = "hidden";
                    rows[i+1].cells[2].style.visibility = "visible";
                }
                if (i == modules.length-2) {
                    rows[i].cells[1].style.visibility = "visible";
                    rows[i+1].cells[1].style.visibility = "hidden";
                }
				
                break;
            }
        }
    }
}

function ontkoppenModule(boekId, moduleId, title) {
    if(confirm("Weet u zeker dat u "+ title +" wilt verwijderen?")) {
        window.location = '/?action=admin.boek.ontkoppel&boekId='+boekId+"&moduleId="+moduleId;
    }
}

function veranderApplicatie(id) {
    if (id==4) {
        document.getElementById('applicatieBestand').style.visibility = 'visible';
        document.getElementById('applicatieBestand').style.display = 'block';
        if (document.getElementById('applicatieBestand').value==0) showUploadApplicatieBestand(true);
    } else {
        //Hide the fileselection
        document.getElementById('applicatieBestand').style.visibility = 'hidden';
        document.getElementById('applicatieBestand').style.display = 'none';
        showUploadApplicatieBestand(false);
    }
}
function showUploadApplicatieBestand(show) {
    document.getElementById('nieuwApplicatieBestandDiv').style.visibility = show?'visible':'hidden';
    document.getElementById('nieuwApplicatieBestandDiv').style.display = show?'block':'none';
}

function getXMLHTTP() {
    var xmlhttp=false;
    /*@cc_on @*/
    /*@if (@_jscript_version >= 5)
	 // JScript gives us Conditional compilation, we can cope with old IE versions.
	 // and security blocked creation of the objects.
	 try {
	 	xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	 } catch (e) {
		 try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } catch (E) { xmlhttp = false; }
	 }
	 @end @*/
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp=false;
        }
    }
    if (!xmlhttp && window.createRequest) {
        try {
            xmlhttp = window.createRequest();
        } catch (e) {
            xmlhttp=false;
        }
    }
    return xmlhttp;
}
