var levels;
var loaded = false;
var selectedRB;
var isnew;
var islocked;
var updateparentId = 'ctl00_ctl00_c_c_M_wu_';
var addparentId = 'ctl00_ctl00_c_c_M_w_';

var errorNotPopulated;

function SetNotPopulatedError(in_Msg) {
    errorNotPopulated = in_Msg;
}

function SetIsNew(in_New) {
    isnew = in_New;
}

function SetIsLocked(in_Islocked) {
    islocked = in_Islocked;
}

function RegisterLevel(in_Level) {
    if (!levels)
        levels = new Array(in_Level.toString());
    else
        levels = levels.concat(new Array(in_Level.toString()));
}

function Clear() {
    if (!lst_TD)
        return;

    for (var i = 0; i < lst_TD.length; i++) {
        document.getElementById(lst_TD[i].Name).className = "mtx_empty";
    }
}

function PalletSelect(in_Value) {
    if (loaded == false) {
        loaded = true;
        if (isnew == undefined) { InitTable(); }

    }
    if (islocked) return;
    if (!in_Value)
        return;

    selectedRB = in_Value;
    SelectRB(in_Value);
}

function SelectRB(in_Value) {
    if (isnew == true) {
        var Element = document.getElementById(in_Value.replace('mtx_', addparentId + 'rb_'));
    }
    else {
        var Element = document.getElementById(in_Value.replace('mtx_', updateparentId + 'rb_'));
    }

    if (Element)
        Element.checked = true;
}

function PostForm() {
    if (PopulateForm()) {
        // Submit
        document.results.submit();
    }
}

function RegisterTDs(in_MaxX, in_MaxY) {
    for (var x = 0; x <= in_MaxX; x++) {
        for (var y = 0; y <= in_MaxY; y++) {
            if (isnew == true) {
                RegisterTD(addparentId + 'cell' + x + '_' + y);
            }
            else {
                RegisterTD(updateparentId + 'cell' + x + '_' + y);
            }

        }
    }
}

var lst_TD;

function RegisterTD(in_ID) {
    if (!lst_TD) {
        lst_TD = new Array(new TD(in_ID));
    }
    else {
        lst_TD = lst_TD.concat(new Array(new TD(in_ID)));
    }
}

function TD(name) {
    this.Name = name;
    if (islocked) return;
    var Element = document.getElementById(name);

    Element.onclick = function () {
        Element.className = selectedRB;
    };

    Element.onmousemove = function (e) {
        var evt = e;
        if (!evt)
            evt = window.event;
        if (evt.button & 1)
            Element.className = selectedRB;
    };
}

function InitCellsFromList(in_List) {
    var currNum = GetHead(in_List);
    var tList = GetTail(in_List);

    var i = 0;

    while (currNum != null) {
        if (i >= lst_TD.length)
            return;

        document.getElementById(lst_TD[i++].Name).className = 'mtx_' + currNum;

        currNum = GetHead(tList);
        tList = GetTail(tList);
    }
}

function GetHead(in_List) {
    if (!in_List)
        return null;

    var i = in_List.indexOf(';');

    if (i == 0)
        return "";
    else if (i == -1)
        return in_List;
    else
        return in_List.substring(0, i);
}

function GetTail(in_List) {
    if (!in_List)
        return null;

    var i = in_List.indexOf(';');
    if (i == in_List.length)
        return null;
    else if (i == 0)
        return in_List.substring(1);
    else if (i == -1)
        return null;
    else
        return in_List.substring(i + 1);
}

function disableLoadButton() {
    document.getElementById(updateparentId + "btn_LoadCurrentGrid").disabled = true;

}
function PopulateForm() {
    // Clear all fields
    if (isnew == undefined) { InitTable(); };
    if (isnew == true) {
        var count = document.getElementById("ctl00_ctl00_c_c_M_pnl_Levels").children.length;
    }
    else {
        var count = document.getElementById(updateparentId + "pnl_ULevels").children.length;
    }

    if (count > 0) {
        count = count / 2;
    }
    for (var l = 0; l < count; l++) {
        if (isnew == true) {
            var currElement = document.getElementById("ctl00_ctl00_c_c_M_pnl_Levels").children[l];
        }
        else {
            var currElement = document.getElementById(updateparentId + "pnl_ULevels").children[l];
        }

        currElement.value = "";
    }

    // Repopulate all fields
    if (lst_TD == undefined) {
        alert("Please select a value for each cell in the matrix.");
        return false;
    }
    for (var i = 0; i < lst_TD.length; i++) {
        var currElement = lst_TD[i].Name;
        var tEmt = document.getElementById(currElement);
        if (tEmt.className == 'mtx_empty') {
            if (errorNotPopulated) {
                alert(errorNotPopulated);
            }
            else {
                alert("Please select a value for each cell in the matrix.");
            }
            return false;
        }
        if (isnew == true) {
            var currLevel = tEmt.className.replace('mtx_', 'ctl00_ctl00_c_c_M_level');
        }
        else {
            var currLevel = tEmt.className.replace('mtx_', updateparentId + 'level');
        }

        var outElement = document.getElementById(currLevel);
        if (outElement) {
            if (isnew == true) {
                outElement.value += currElement.replace(addparentId + 'cell', '') + ";"
            }
            else {
                outElement.value += currElement.replace(updateparentId + 'cell', '') + ";"
            }

        }
    }

    if (isnew == true) {
        document.getElementById(addparentId + "FinishNavigationTemplateContainerID_FinishGrid").disabled = false;
        document.getElementById(addparentId + "btn_CheckGrid").disabled = true;
    }
    else {
        document.getElementById(updateparentId + "FinishNavigationTemplateContainerID_UFinishGrid").disabled = false;
        document.getElementById(updateparentId + "btn_UpdatecheckGrid").disabled = true;
    }

    return false;
}
