﻿
function FixCheckBoxList(Control, ControlAll) {
    //debugger;
    Control = document.getElementById(Control);
    ControlAll = document.getElementById(ControlAll);
    var i = 0;
    var s = "document.forms[0]." + Control.id;
    var FirstClick = false;
    var changed;
    var changedAll;

    if (Control.Boxes == undefined) {
        Control.Box = new Array();
        Control.CheckedBoxes = new Array();
        while (eval(s + "_" + i) != undefined) {
            Control.Box[i] = eval(s + "_" + i);
            Control.CheckedBoxes[i] = Control.Box[i].checked;
            i = i + 1;
        }
        Control.Boxes = i;
        FirstClick = true;
    }

    changed = WhatHasChanged(Control, FirstClick);
    FirstClick = false;

    if (ControlAll.Box == undefined) {
        if (ControlAll != undefined) {
            ControlAll.Box = ControlAll;
            ControlAll.CheckedBox = ControlAll.Box.checked;
        }
    }

    if (HasItChanged(ControlAll)) {
        //Om "Alla" kryssrutan blev ikryssad
        if (ControlAll.checked) {
            CheckAll(Control, false);
        }
    }

    if (changed != undefined) {
        //"Alla" rutan ska inte vara kryssad om någon annan är kryssad.
        if (ControlAll.checked) {
            var OneChecked = false;
            for (i = 0; i < Control.Boxes; i++) {
                if (Control.CheckedBoxes[i] == true) {
                    OneChecked = true;
                }
            }
            if (OneChecked) {
                ControlAll.checked = false;
                ControlAll.CheckedBox = false;
            }
        }
    }
}

function WhatHasChanged(Control, FirstClick) {
    if (FirstClick) {
        for (i = 0; i < Control.Boxes; i++) {
            if (Control.CheckedBoxes[i]) {
                return i;
            }
        }
    }
    else {
        for (i = 0; i < Control.Boxes; i++) {
            if (Control.CheckedBoxes[i] != Control.Box[i].checked) {
                Control.CheckedBoxes[i] = Control.Box[i].checked;
                return i;
            }
        }
    }
    return undefined;
}

function HasItChanged(Control) {
    if (Control.CheckedBox != Control.checked) {
        Control.CheckedBox = Control.checked;
        return true;
    }
    return false;
}


function CheckAll(Control, Value) {
    var i = 0;
    for (i = 0; i < Control.Boxes; i++) {
        Control.Box[i].checked = Value;
        Control.CheckedBoxes[i] = Value;
    }
}
