// Functions for "MyRes" download page

function toggleAll(strSection) {
    if (!document.getElementById) return false;
    if (!document.getElementById(strSection + "-all")) return false;
    
    var nKey = strSection.charCodeAt(0) - 97;
    var allCheckbox = document.getElementById(strSection + "-all");
    
    for (i in FileSizes[nKey]) {
        var currentCheckbox = document.getElementById(strSection + i.toString())
        currentCheckbox.checked = allCheckbox.checked;
    }
    updateFilesize();
}
function updateFilesize() {
    if (!document.getElementById) return false;
    if (!document.getElementById("spanSize")) return false;
    
    var spanSize = document.getElementById("spanSize");
    var nFileSize = 0.0;
    for (nCounter = 0; nCounter <= 4; nCounter++) {
        var strSection = String.fromCharCode(nCounter + 97);
        for (nKey in FileSizes[nCounter]) {
            var currentCheckbox = document.getElementById(strSection + nKey.toString());
            if (currentCheckbox.checked) {
                nFileSize = nFileSize + FileSizes[nCounter][nKey];
                if (!currentCheckbox.defaultChecked) {
                    tickRes(strSection + nKey.toString());
                }
            } else {
                var allCheckbox = document.getElementById(strSection + "-all");
                allCheckbox.checked = false;
                if (currentCheckbox.defaultChecked) {
                    untickRes(strSection + nKey.toString());
                }
            }
        }
    }
    nFileSize = nFileSize + FileSizeCovers[0] + FileSizeCovers[1];
    spanSize.innerHTML = nFileSize.toFixed(2) + "mb";
}
function untickRes(strSection) {
    // Get http request object to make ajax call
    xmlHttp = GetXmlHttpObject();
    xmlHttp.open("GET", "/res/addRes.asp?action=remove&section=" + strSection, false);
    xmlHttp.send(null);
}
function tickRes(strSection) {
    // Get http request object to make ajax call
    xmlHttp = GetXmlHttpObject();
    xmlHttp.open("GET", "/res/addRes.asp?action=add&section=" + strSection, false);
    xmlHttp.send(null);
}
function addOnclicks() {
    if (!document.getElementById) return false;
    for (nCounter = 0; nCounter <= 4; nCounter++) {
        var strSection = String.fromCharCode(nCounter + 97);
        for (nKey in FileSizes[nCounter]) {
            var currentCheckbox = document.getElementById(strSection + nKey.toString());
            currentCheckbox.onclick = function () {
                return updateFilesize();
            }
        }
    }
}
addLoadEvent(addOnclicks);
addLoadEvent(updateFilesize);