/*
1. enable/disable all in module except checked
2. enable/disable 4hour if 2 4hour already is selected except checked
3. enable/disable 7hour if 2 7hour already is selected except checked
4. check next periods class if min2period cb is clicked
*/

function classSelected(cb)
{   
    disableModul(cb.getAttribute("czmodul"), cb.checked);
    if(cb.getAttribute("cztwoPeriods") == "true")
    {
        var secondPeriod;
        
        if(cb.getAttribute("czNextPeriod") != undefined) {
            secondPeriod = getClassesByAttributes("czid",cb.getAttribute("czNextPeriod"))[0];
        }
        else if(cb.getAttribute("czPrevPeriod") != undefined)
        {
            secondPeriod = getClassesByAttributes("czid",cb.getAttribute("czPrevPeriod"))[0];
        }
        
        secondPeriod.checked = cb.checked
        disableModul(secondPeriod.getAttribute("czmodul"), cb.checked);
        updatePeriod(secondPeriod);
    }
    
    
    updatePeriod(cb);
	
	var cbLst = new Array();
    cbLst = getClassesByAttributes("czLocked",'true');
    for(i = 0; i < cbLst.length; i++)
    {
        cbLst[i].disabled = true;
    }
}

function updatePeriod(cb)
{
    var FourhoursInPeriod = 0;
    var SevenhoursInPeriod = 0;
    var SelectedModul = new Array();
    
    
    var cbLst = new Array();
    cbLst = getClassesByAttributes("czperiod",cb.getAttribute("czperiod"));
    for (i = 0; i < cbLst.length; i++)
    {
        if(cbLst[i].checked)
        {
            SelectedModul[SelectedModul.length] = cbLst[i].getAttribute("czmodul");
            if(cbLst[i].getAttribute("czhour") == 4)
            {
                FourhoursInPeriod++;
            }
            else
            {
                SevenhoursInPeriod++;
            }
        }
    }
    
    for (i = 0; i < cbLst.length; i++)
    {
        if(!cbLst[i].checked)
        {
            if(((cbLst[i].getAttribute("czhour") == 4) && FourhoursInPeriod >= 2) || ((cbLst[i].getAttribute("czhour") == 7) && SevenhoursInPeriod >= 2))
            {
                cbLst[i].disabled = true;
            }
            else if(!cb.checked)
            {
                if(!valueInArray(SelectedModul, cbLst[i].getAttribute("czmodul")))
                {
                    cbLst[i].disabled = false;
                }
            }
        }
    }
}

function valueInArray(arr, value)
{   
    for (ix = 0; ix < arr.length; ix++) {
        if(arr[ix] == value)
        {
            return true;
        }
    }
    return false;
}

function disableModul(modulid, disable)
{
    var cbLst = new Array();
    cbLst = getClassesByAttributes("czmodul",modulid);
    
    for (i = 0; i < cbLst.length; i++)
    {
        if(!cbLst[i].checked)
        {
            cbLst[i].disabled = disable;
        }
    }
}

function getClassesByAttributes(name, value) {
    var allHTMLTags = new Array();
    var selectedHTMLTags = new Array();
    var counter = 0;

    allHTMLTags = document.getElementsByTagName("input");

    for (i = 0; i < allHTMLTags.length; i++) {
        if (allHTMLTags[i].getAttribute(name) != undefined && allHTMLTags[i].getAttribute(name) == value) {
            selectedHTMLTags[counter] = (allHTMLTags[i]);
            counter++;
        }
    }
    return selectedHTMLTags;
}
