<%@ page import="ro.emid.common.types.TipInstalatie" %>
<%
    //    final boolean printVersion = false;
%>
<script type="text/javascript">
indexTmpStatie = 0;
indexTmpCelula = 1;
indexTmpLinie = 2;
indexTmpPA_PT = 3;
indexTmpTrafo = 4;
indexTmpBare = 5;

function getLineOfDizables() {
    ansamble_selects = new Array(21);
    //                              st, cel, liie, pa, trafo, bare
    ansamble_selects[0] = new Array(13, 1, 0, 1, 1, 0, 0);
    ansamble_selects[1] = new Array(19, 1, 0, 0, 1, 0, 0);
    ansamble_selects[2] = new Array(21, 1, 0, 0, 0, 0, 0);
    ansamble_selects[3] = new Array(22, 1, 0, 0, 1, 0, 0);
    ansamble_selects[4] = new Array(46, 1, 0, 0, 1, 0, 0);
    ansamble_selects[5] = new Array(47, 1, 0, 0, 1, 0, 0);
    ansamble_selects[6] = new Array(48, 1, 0, 0, 1, 0, 0);
    ansamble_selects[7] = new Array(49, 1, 0, 0, 1, 0, 0);
    ansamble_selects[8] = new Array(50, 0, 0, 1, 0, 0, 0);
    ansamble_selects[9] = new Array(51, 1, 0, 1, 1, 0, 0);
    ansamble_selects[10] = new Array(521, 1, 1, 0, 1, 0, 0);
    ansamble_selects[11] = new Array(522, 0, 1, 1, 0, 0, 0);
    ansamble_selects[12] = new Array(53, 1, 0, 0, 1, 0, 1);
    ansamble_selects[13] = new Array(541, 1, 0, 0, 0, 1, 0);
    ansamble_selects[14] = new Array(542, 1, 0, 0, 1, 1, 0);
    ansamble_selects[15] = new Array(57, 1, 0, 0, 1, 0, 0);
    ansamble_selects[16] = new Array(58, 0, 0, 0, 1, 1, 0);
    ansamble_selects[17] = new Array(59, 0, 0, 0, 1, 1, 0);
    ansamble_selects[18] = new Array(60, 1, 0, 0, 0, 0, 0);
    ansamble_selects[19] = new Array(61, 1, 0, 1, 1, 0, 0);
    ansamble_selects[20] = new Array(62, 1, 0, 0, 0, 0, 0);

    value = zaforma.<%=LucrareProgramataConstants.JSP.ANSAMBLU_ID%>x.options[zaforma.<%=LucrareProgramataConstants.JSP.ANSAMBLU_ID%>.selectedIndex].value;
    //alert( value);
    var tmp = new Array(1, 1, 1, 1, 1, 1);
    for (i = 0; i < ansamble_selects.length; i++) {
        if (value == ansamble_selects[i][0]) {
            //alert( tmp);
            tmp[0] = !ansamble_selects[i][1];
            tmp[1] = !ansamble_selects[i][2];
            tmp[2] = !ansamble_selects[i][3];
            tmp[3] = !ansamble_selects[i][4];
            tmp[4] = !ansamble_selects[i][5];
            tmp[5] = !ansamble_selects[i][6];
        }
    }
    //alert( tmp);
    return tmp;
}

function enable_ansamble_selects() {
    var tmp = getLineOfDizables();

    markAsDisabled( $('<%=LucrareProgramataConstants.JSP.STATIE_DEN%>'), tmp[0]);
    markAsDisabled( $('<%=LucrareProgramataConstants.JSP.STATIE_ID%>'), tmp[0]);

    markAsDisabled( $('<%=LucrareProgramataConstants.JSP.CELULA_DEN%>'), tmp[1]);

    if(tmp[1]){
        //resetare
        $('<%=LucrareProgramataConstants.JSP.CELULA_ID%>').value = -1;
        $('<%=LucrareProgramataConstants.JSP.CELULA_COD%>').value = '';
        $('<%=LucrareProgramataConstants.JSP.CELULA_DEN%>').value = '';
    }

    markAsDisabled( $('<%=LucrareProgramataConstants.JSP.LINIE_DEN%>'), tmp[2]);
    markAsDisabled( $('<%=LucrareProgramataConstants.JSP.DERIVATIE_DEN%>'), tmp[2]);

    if(tmp[2] ) {
        //resetare
        $('<%=LucrareProgramataConstants.JSP.LINIE_ID%>').value = -1;
        $('<%=LucrareProgramataConstants.JSP.LINIE_COD%>').value = '';
        $('<%=LucrareProgramataConstants.JSP.LINIE_DEN%>').value = '';
        $('<%=LucrareProgramataConstants.JSP.DERIVATIE_ID%>').value = -1;
        $('<%=LucrareProgramataConstants.JSP.DERIVATIE_COD%>').value = -1;
        $('<%=LucrareProgramataConstants.JSP.DERIVATIE_DEN%>').value = '';
    }

    markAsDisabled( $('<%=LucrareProgramataConstants.JSP.PAPT_DEN%>'), tmp[3]);

    if(tmp[3] ) {
        //resetare
        $('<%=LucrareProgramataConstants.JSP.PAPT_ID%>').value = -1;
        $('<%=LucrareProgramataConstants.JSP.PAPT_COD%>').value = '';
        $('<%=LucrareProgramataConstants.JSP.PAPT_DEN%>').value = '';
    }

    markAsDisabled( $('<%=LucrareProgramataConstants.JSP.TRAFO_DEN%>'), tmp[4] );

    if(tmp[4]) {
        //resetare
        $('<%=LucrareProgramataConstants.JSP.TRAFO_ID%>').value = -1;
        $('<%=LucrareProgramataConstants.JSP.TRAFO_COD%>').value = '';
        $('<%=LucrareProgramataConstants.JSP.TRAFO_DEN%>').value = '';
    }

    markAsDisabled( $('<%=LucrareProgramataConstants.JSP.BARA_DEN%>'), tmp[5] );

    if(tmp[5] ) {
        //resetare
        $('<%=LucrareProgramataConstants.JSP.BARA_ID%>').value = -1;
        $('<%=LucrareProgramataConstants.JSP.BARA_COD%>').value = '';
        $('<%=LucrareProgramataConstants.JSP.BARA_DEN%>').value = '';
    }
}

function markAsDisabled(el, toDisable) {

    if(el === null) return;

    el.disabled = toDisable;
    if (toDisable) {
        el.style.backgroundColor = '#DBDBDB';
    } else {
        el.style.backgroundColor = '';
    }
}
function fill_statii_selects(selectedValue, onEdit) {
    var tmp = getLineOfDizables();
    if (tmp[indexTmpStatie]) {
        resetComboLeaveFirstOption(document.zaforma.<%=LucrareProgramataConstants.JSP.STATIE_ID%>);
        resetComboLeaveFirstOption(document.zaforma.<%=LucrareProgramataConstants.JSP.STATIE_ID%>h);
        resetComboLeaveFirstOption(document.zaforma.<%=LucrareProgramataConstants.JSP.BOBINA_ID%>);

        //se populeaza dupa centru expl
        if (onEdit) {
            fillAllOnEdit();
        } else {
            fill_celula_selects();
            fill_bare_selects();
            fill_trafo_selects();
        }
    } else {
//            alert( 'fill statii ...')
        var codCe = document.zaforma.<%=LucrareProgramataConstants.JSP.CENTRU_ID%>h.value;
        var params = buildParams(<%=printVersion%>, '<%=LucrareProgramataConstants.JSP.STATIE_ID%>', <%=TipInstalatie.STATIE%>, codCe, 1, selectedValue);

//            alert( params);
        getNomenclatorFromServer(params, function (originalRequest) {
            $('<%=TipInstalatie.STATIE%>div').innerHTML = getResponseMesg(originalRequest.responseText);

            if (onEdit) {//celelalte functii de fill sunt dependente de statia selectata, de aceea ele treb apelate dupa ce s-a popuat si s=a selectat statia
                fillAllOnEdit();
            }
        });
    }
}

function fill_bobine_selects(selectedValue, fillOrfans) {
    //alert( 'fill bobine');
    var tmp = getLineOfDizables();
    if (tmp[indexTmpStatie]) {
        resetComboLeaveFirstOption(document.zaforma.<%=LucrareProgramataConstants.JSP.BOBINA_ID%>);
    } else {
        //alert( 'fill bobine by Statie ...');
        var codParent = '';
        var nrOfParent = 2;
        if (fillOrfans) {
            codParent = -1;
        } else {
            codParent = document.zaforma.<%=LucrareProgramataConstants.JSP.STATIE_ID%>h[ document.zaforma.<%=LucrareProgramataConstants.JSP.STATIE_ID%>.selectedIndex].value;
        }
        var params = buildParams(<%=printVersion%>, '<%=LucrareProgramataConstants.JSP.BOBINA_ID%>', <%=TipInstalatie.BOBINA%>, codParent, nrOfParent, selectedValue);

        getNomenclatorFromServer(params, function (originalRequest) {
            $('<%=TipInstalatie.BOBINA%>div').innerHTML = getResponseMesg(originalRequest.responseText);
            //alert( getResponseMesg (originalRequest.responseText));
        });
    }
}

function fill_derivatii_selects(selectedValue) {
    //alert( 'fill bobine');
    var tmp = getLineOfDizables();
    if (tmp[indexTmpLinie]) {
        resetComboLeaveFirstOption(document.zaforma.<%=LucrareProgramataConstants.JSP.DERIVATIE_ID%>);
    } else {
        //alert( 'fill bobine by Statie ...');
        var codParent = document.zaforma.<%=LucrareProgramataConstants.JSP.LINIE_ID%>h[ document.zaforma.<%=LucrareProgramataConstants.JSP.LINIE_ID%>.selectedIndex].value;
        var nrOfParent = 2;
        var params = buildParams(<%=printVersion%>, '<%=LucrareProgramataConstants.JSP.DERIVATIE_ID%>', <%=TipInstalatie.DERIVATIE%>, codParent, nrOfParent, selectedValue);
        //alert( params);

        getNomenclatorFromServer(params, function (originalRequest) {
            $('<%=TipInstalatie.DERIVATIE%>div').innerHTML = getResponseMesg(originalRequest.responseText);
            //alert( getResponseMesg (originalRequest.responseText));
        });
    }
}

function fill_linii_selects(selectedValue, onEdit, derivatieSelectedValue) {
    var tmp = getLineOfDizables();
    if (tmp[indexTmpLinie]) {
        resetComboLeaveFirstOption(document.zaforma.<%=LucrareProgramataConstants.JSP.LINIE_ID%>);
        resetComboLeaveFirstOption(document.zaforma.<%=LucrareProgramataConstants.JSP.DERIVATIE_ID%>);
    } else {
        //alert( 'fill linii by CE...');
        var codCe = document.zaforma.<%=LucrareProgramataConstants.JSP.CENTRU_ID%>h.value;
        var params = buildParams(<%=printVersion%>, '<%=LucrareProgramataConstants.JSP.LINIE_ID%>', <%=TipInstalatie.LINIE%>, codCe, 1, selectedValue);


        getNomenclatorFromServer(params, function (originalRequest) {
            $('<%=TipInstalatie.LINIE%>div').innerHTML = getResponseMesg(originalRequest.responseText);

            resetComboLeaveFirstOption(document.zaforma.<%=LucrareProgramataConstants.JSP.DERIVATIE_ID%>);
            if (onEdit) {
                fill_derivatii_selects(derivatieSelectedValue);
            }
        });
    }
}

function fill_pa_pt_selects(selectedValue) {
    var tmp = getLineOfDizables();
    if (tmp[indexTmpPA_PT]) {
        resetComboLeaveFirstOption(document.zaforma.<%=LucrareProgramataConstants.JSP.PAPT_ID%>);
    } else {
        //alert( 'fill pa_pt by CE ...');
        var codCe = document.zaforma.<%=LucrareProgramataConstants.JSP.CENTRU_ID%>h.value;
        var params = buildParams(<%=printVersion%>, '<%=LucrareProgramataConstants.JSP.PAPT_ID%>', <%=TipInstalatie.PA_PT%>, codCe, 1, selectedValue);

        getNomenclatorFromServer(params, function (originalRequest) {
            $('<%=TipInstalatie.PA_PT%>div').innerHTML = getResponseMesg(originalRequest.responseText);
        });
    }
}

function fill_celula_selects(selectedValue, fillOrfans) {
    var tmp = getLineOfDizables();
    if (tmp[indexTmpCelula]) {   //disabled
        resetComboLeaveFirstOption(document.zaforma.<%=LucrareProgramataConstants.JSP.CELULA_ID%>);
    } else {
        var codParent = '';
        var nrOfParent = 1;
        if (fillOrfans) {
            nrOfParent = 2;
            codParent = -1;
        } else if (tmp[indexTmpStatie]) {        //statia disabled
            codParent = document.zaforma.<%=LucrareProgramataConstants.JSP.CENTRU_ID%>h.value;
            nrOfParent = 1;             // fill by CE
        } else {
            codParent = document.zaforma.<%=LucrareProgramataConstants.JSP.STATIE_ID%>h[ document.zaforma.<%=LucrareProgramataConstants.JSP.STATIE_ID%>.selectedIndex].value;
            nrOfParent = 2;             //fill by statie
        }


        var params = buildParams(<%=printVersion%>, '<%=LucrareProgramataConstants.JSP.CELULA_ID%>', <%=TipInstalatie.CELULA%>, codParent, nrOfParent, selectedValue);

        //alert( params);
        getNomenclatorFromServer(params, function (originalRequest) {
            $('<%=TipInstalatie.CELULA%>div').innerHTML = getResponseMesg(originalRequest.responseText);
        });
    }
}

function fill_bare_selects(selectedValue, fillOrfans) {
    var tmp = getLineOfDizables();
    if (tmp[indexTmpBare]) {   //disabled
        resetComboLeaveFirstOption(document.zaforma.<%=LucrareProgramataConstants.JSP.BARA_ID%>);
    } else {
        var codParent = '';
        var nrOfParent = 1;
        if (fillOrfans) {
            nrOfParent = 2;
            codParent = -1;
        } else if (tmp[indexTmpStatie]) {        //statia disabled
            codParent = document.zaforma.<%=LucrareProgramataConstants.JSP.CENTRU_ID%>h.value;
            nrOfParent = 1;
            //alert( 'fill bare by CE');
        } else {
            codParent = document.zaforma.<%=LucrareProgramataConstants.JSP.STATIE_ID%>h[ document.zaforma.<%=LucrareProgramataConstants.JSP.STATIE_ID%>.selectedIndex].value;
            nrOfParent = 2;
            //alert( 'fill bare by Statie');
        }
        var params = buildParams(<%=printVersion%>, '<%=LucrareProgramataConstants.JSP.BARA_ID%>', <%=TipInstalatie.BARA%>, codParent, nrOfParent, selectedValue);

        //alert( params);
        getNomenclatorFromServer(params, function (originalRequest) {
            $('<%=TipInstalatie.BARA%>div').innerHTML = getResponseMesg(originalRequest.responseText);
        });
    }
}

function fill_trafo_selects(selectedValue, fillOrfans) {
    var tmp = getLineOfDizables();
//        alert( tmp);
    if (tmp[indexTmpTrafo]) {    //disabled
        resetComboLeaveFirstOption(document.zaforma.<%=LucrareProgramataConstants.JSP.TRAFO_ID%>);
    } else {
        var codParent = '';
        var nrOfParent = 1;
        if (fillOrfans) {
            nrOfParent = 2;
            codParent = -1;
        } else if (tmp[indexTmpStatie]) {  //statia disabled
            codParent = document.zaforma.<%=LucrareProgramataConstants.JSP.CENTRU_ID%>h.value;
            nrOfParent = 1;
            //alert( 'fill trafo by CE...');
        } else {
            codParent = document.zaforma.<%=LucrareProgramataConstants.JSP.STATIE_ID%>h[ document.zaforma.<%=LucrareProgramataConstants.JSP.STATIE_ID%>.selectedIndex].value;
            nrOfParent = 2;
            //alert( 'fill trafo by statie...');
        }
        var params = buildParams(<%=printVersion%>, '<%=LucrareProgramataConstants.JSP.TRAFO_ID%>', <%=TipInstalatie.TRAFO%>, codParent, nrOfParent, selectedValue);

        //alert( params);
        getNomenclatorFromServer(params, function (originalRequest) {
            $('<%=TipInstalatie.TRAFO%>div').innerHTML = getResponseMesg(originalRequest.responseText);
        });
    }
}

function fillEqOrfane() {
    fill_bobine_selects(-1, true);
    fill_bare_selects(-1, true);
    fill_celula_selects(-1, true);
    fill_trafo_selects(-1, true);
}

function on_statie_changed() {
    //alert( 'on statie changed');
    fill_all_selects();
    fill_bobine_selects();
    fill_bare_selects();
}

function on_linie_changed() {
    fill_derivatii_selects();
}

function fill_all_selects(celulaId, linieId, paptId, trafoId, derivatieId) {
    fill_celula_selects(celulaId);
    fill_linii_selects(linieId, true, derivatieId);
    fill_pa_pt_selects(paptId);
    fill_trafo_selects(trafoId);
}


function onFormatieChanged() {
    resetAndDisableLocalizareSelects();
    setHiddenCodSectieIfIsC1_or_C2();
}

function setHiddenCodSectieIfIsC1_or_C2() {
    try {
        var selectedIndex = document.zaforma.<%=LucrareProgramataConstants.JSP.FORMATIA_DE_LUCRU_ID%>.selectedIndex;
        var codSapOfCentru = document.zaforma.<%=LucrareProgramataConstants.JSP.CENTRU_ID%>h.getAttribute("codSapOriginal");  //codul original al Sectiei
        if (selectedIndex > 0) {
            var formatieCodSap = document.zaforma.<%=LucrareProgramataConstants.JSP.FORMATIA_DE_LUCRU_ID%>[selectedIndex].getAttribute("codSapAsCentru");
//                alert("Cod Sap from Option Attribute : "+ formatieCodSap);
            if (formatieCodSap && formatieCodSap.length > 0) {
                codSapOfCentru = formatieCodSap;
            }

            <%--var categoryId = document.zaforma.<%=IncidentConstants.FORMATIA_DE_LUCRU_ID%>[selectedIndex].value;--%>

            <%--// am luat dupa ID deoarece ele nu se vor schimba--%>
            <%--if( categoryId == 2011 ){  // C1, din  SECTIA 110 KV, din Sucursala Bucuresti--%>
            <%--codSapOfCentru = "8DF41111";--%>
            <%--}--%>

            <%--if( categoryId == 2012 ){   // C2, din  SECTIA 110 KV, din Sucursala Bucuresti--%>
            <%--codSapOfCentru = "8DF41112";--%>
            <%--}--%>
        }
        //setam codul SAP al sectiei
        document.zaforma.<%=LucrareProgramataConstants.JSP.CENTRU_ID%>h.value = codSapOfCentru;
    } catch (e) {
        alert(e.message);
    }
}

function resetAndDisableLocalizareSelects() {
    document.zaforma.<%=LucrareProgramataConstants.JSP.ANSAMBLU_ID%>.selectedIndex = 0;
    resetComboLeaveFirstOptionAndDisable(document.zaforma.<%=LucrareProgramataConstants.JSP.STATIE_ID%>);
    resetComboLeaveFirstOptionAndDisable(document.zaforma.<%=LucrareProgramataConstants.JSP.BOBINA_ID%>);
    resetComboLeaveFirstOptionAndDisable(document.zaforma.<%=LucrareProgramataConstants.JSP.CELULA_ID%>);
    resetComboLeaveFirstOptionAndDisable(document.zaforma.<%=LucrareProgramataConstants.JSP.BARA_ID%>);
    resetComboLeaveFirstOptionAndDisable(document.zaforma.<%=LucrareProgramataConstants.JSP.LINIE_ID%>);
    resetComboLeaveFirstOptionAndDisable(document.zaforma.<%=LucrareProgramataConstants.JSP.DERIVATIE_ID%>);
    resetComboLeaveFirstOptionAndDisable(document.zaforma.<%=LucrareProgramataConstants.JSP.PAPT_ID%>);
    resetComboLeaveFirstOptionAndDisable(document.zaforma.<%=LucrareProgramataConstants.JSP.TRAFO_ID%>);
}

</script>