// JScript File
var obj;
var curDDL = 0;
var Sel = "";
function getChild_getOrg(para1, para2) {
    if (window.ActiveXObject) {
        try {
            obj = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                obj = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e1) {
                obj = null;
            }
        }
    }
    else if (window.XMLHttpRequest) {
        try {
            obj = new XMLHttpRequest("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                obj = new XMLHttpRequest("Microsoft.XMLHTTP");
            }
            catch (e1) {
                obj = null;
            }
        }
    }
    if (obj != null) {
        // if (getElt_Id().selectedIndex!=0){
        //document.write("<br>../Masters/TA_UI_Master_getOrg.aspx?level="+para2+"<BR>")
        curDDL = para1;
        var url
        ses = 0;
        if (para1 == 1) {
            if (para2 != "Select")
                checkPageLoad = 1;
            else
                checkPageLoad = 0;
        }
        else if (para1 == 0) {
            checkPageLoad = 0;
        }
        else {
            checkPageLoad = 2;
        }
        if (window.ActiveXObject) {
            obj.onreadystatechange = ProcessResponse_getOrg;
            if (para2 == 'Select' && para1 != 1) {
                //para2 = FindElt_Id_getOrg("ddlLevel"+(para1-2)).value;
                pp = para1 - 1;
                para2 = FindElt_Id_getOrg("ddlLevel" + (pp)).value;
                ses = FindElt_Id_getOrg("ddlLevel" + (para1 - 2)).value;
            }
            if (aspnetForm.ctl00_BodyContentPH_hdf_orgLevel != null) {
                aspnetForm.ctl00_BodyContentPH_hdf_orgLevel.value = para2;
            }
            if (aspnetForm.ctl00_BodyContentPH_hdf_Levelalert != null) {
                aspnetForm.ctl00_BodyContentPH_hdf_Levelalert.value = document.getElementById("ddlLevel0").value;
            }
            obj.open("POST", "../Masters/TA_UI_Master_getOrg.aspx?level=" + para2 + "&ctr=" + para1 + "&ses=" + ses, false);
            obj.send(null);
        }
        else if (window.XMLHttpRequest) {
            if (para2 == 'Select' && para1 != 1) {
                pp = para1 - 1;
                para2 = FindElt_Id_getOrg("ddlLevel" + (pp)).value;
                ses = FindElt_Id_getOrg("ddlLevel" + (para1 - 2)).value;
            }
            if (document.getElementById("ctl00_BodyContentPH_hdf_orgLevel") != null) {
                document.getElementById("ctl00_BodyContentPH_hdf_orgLevel").value = para2;
            }
            if (document.getElementById("ctl00_BodyContentPH_hdf_Levelalert") != null) {
                document.getElementById("ctl00_BodyContentPH_hdf_Levelalert").value = document.getElementById("ddlLevel0").value;
            }
            obj.open("POST", "../Masters/TA_UI_Master_getOrg.aspx?level=" + para2 + "&ctr=" + para1 + "&ses=" + ses, false);
            obj.send(null);
            obj.onreadystatechange = ProcessResponse_getOrg();

        }
        return false;
    }
}
function chckLevel1_getOrg(para1, para2, Select) {
    Sel = Select;
    createDDL_getOrg();
    return false;
}
function geturl_getOrg() {
    var str
    str = "../Masters/TA_UI_Master_getOrg.aspx?"
    return str
}
function FindElt_Id_getOrg(para) {
    return document.getElementById(para);
}
function getElt_Id_getOrg() {
    return document.getElementById("ddlLevel" + curDDL);
}
//Added by MAnjula.P on 07-Jan-2014
function getDesignationDDL() {
    return document.getElementById("ctl00_BodyContentPH_ddl_Designation");
}
function getReportingToDDL() {
    return document.getElementById("ctl00_BodyContentPH_ddl_ReportingTo");
}
function getAuthorityDDL() {
    return document.getElementById("ctl00_BodyContentPH_ddl_LeaveSancAuthority");
}
//************************************
function getTag_Name_getOrg(para) {
    if (obj.responseXML == null) {
        clearDDL_getOrg();
        return false;
    }
    var dsRoot = obj.responseXML.documentElement;
    if (obj.responseXML.documentElement == null) {
        clearDDL_getOrg();
        return false;
    } else {
        for (iloop = (curDDL + 1) ; iloop <= totLevels - 1; iloop++) {
            seleDDL = FindElt_Id_getOrg("ddlLevel" + iloop);
            for (var count = seleDDL.options.length; count > 0; count--) {
                seleDDL.options[count] = null;
            }
            //	        document.getElementById('div_child'+ iloop).innerHTML = "";
        }
        return dsRoot.getElementsByTagName(para);
    }
}
//Added by MAnjula.P on 07-Jan-2014
function getTag_Name_getDesgnation(para) {
    if (obj.responseXML != null) {
        if (obj.responseXML.documentElement != null) {
            var dsDesign = obj.responseXML.documentElement;
            return dsDesign.getElementsByTagName(para);
        }
    }
}
function ProcessResponse_getOrg() {
    if (obj.readyState == 4) {
        if (obj.status == 200) {

            var levelddl = getElt_Id_getOrg()
            if (levelddl != null) {
                levelddl.innerHTML = "";
                levelddl.innerText = "";
                var tagVal = getTag_Name_getOrg('id')
                var tagNam = getTag_Name_getOrg('name')
                var html = "";
                var text;
                var valu;
                var listItem;
                var index;
                listItem = new Option(Sel, 'Select', false, false);
                levelddl.options[levelddl.length] = listItem;
                for (var count = 0; count < tagNam.length; count++) {
                    text = (tagNam[count].textContent || tagNam[count].innerText || tagNam[count].text);
                    valu = (tagVal[count].textContent || tagVal[count].innerText || tagVal[count].text);
                    //alert("text =" +text);
                    text = text.replace('&amp;', '&');
                    listItem = new Option(text, valu, false, false);
                    levelddl.options[levelddl.length] = listItem;
                    levelddl.options[count + 1].title = text;
                }
            }
            //Added by Manjula.P on 10-Jan-2014 for getting reportingto and sanctonauthority dropdown values
            if (checkPageLoad == 1) {
                var ddl_ReportingTo = getReportingToDDL();
                var ddl_LeaveSancAuthority = getAuthorityDDL();
                //Forward ddl
                if (ddl_ReportingTo != null) {
                    //Clearing existing values from designation ddl
                    ddl_ReportingTo.innerHTML = "";
                    ddl_ReportingTo.innerText = "";
                    var ReportToVal = getTag_Name_getDesgnation('Employee_Id');
                    var ReportToNam = getTag_Name_getDesgnation('Employee_FirstName');
                    listItem = new Option(Sel, '0', false, false);
                    ddl_ReportingTo.options[ddl_ReportingTo.length] = listItem;
                    if (ReportToVal != undefined && ReportToNam != undefined) {
                        for (var count = 0; count < ReportToNam.length; count++) {
                            text = (ReportToNam[count].textContent || ReportToNam[count].innerText || ReportToNam[count].text);
                            valu = (ReportToVal[count].textContent || ReportToVal[count].innerText || ReportToVal[count].text);
                            //alert("text =" +text);
                            text = text.replace('&amp;', '&');
                            listItem = new Option(text, valu, false, false);
                            ddl_ReportingTo.options[count + 1] = listItem;
                            if (valu == ReportingTo) {
                                ddl_ReportingTo.value = ReportingTo;
                                ddl_ReportingTo.options[count + 1].selected = true;
                            }
                        }
                    }
                }
                //Authority
                if (ddl_LeaveSancAuthority != null) {
                    //Clearing existing values from designation ddl
                    ddl_LeaveSancAuthority.innerHTML = "";
                    ddl_LeaveSancAuthority.innerText = "";
                    var AuthorityVal = getTag_Name_getDesgnation('Employee_Id');
                    var AuthorityNam = getTag_Name_getDesgnation('Employee_FirstName');
                    listItem = new Option(Sel, '0', false, false);
                    ddl_LeaveSancAuthority.options[ddl_LeaveSancAuthority.length] = listItem;
                    if (AuthorityVal != undefined && AuthorityNam != undefined) {
                        for (var count = 0; count < AuthorityNam.length; count++) {
                            text = (AuthorityNam[count].textContent || AuthorityNam[count].innerText || AuthorityNam[count].text);
                            valu = (AuthorityVal[count].textContent || AuthorityVal[count].innerText || AuthorityVal[count].text);
                            //alert("text =" +text);
                            text = text.replace('&amp;', '&');
                            listItem = new Option(text, valu, false, false);
                            ddl_LeaveSancAuthority.options[count + 1] = listItem;
                            if (valu == AthorityTo) {
                                ddl_LeaveSancAuthority.value = AthorityTo;
                                ddl_LeaveSancAuthority.options[count + 1].selected = true;
                            }
                        }
                    }
                }
                //Desingation
                //Added by MAnjula.P on 07-Jan-2014 for loading designation based on levels id
                var ddl_Designation = getDesignationDDL();
                if (ddl_Designation != null) {
                    ddl_Designation.innerHTML = "";
                    ddl_Designation.innerText = "";
                    var DesignVal = getTag_Name_getDesgnation('TitleId');
                    var DesignNam = getTag_Name_getDesgnation('TitleName');
                    listItem = new Option(Sel, '0', false, false);
                    ddl_Designation.options[ddl_Designation.length] = listItem;
                    if (DesignVal != undefined && DesignNam != undefined) {
                        for (var count = 0; count < DesignNam.length; count++) {
                            text = (DesignNam[count].textContent || DesignNam[count].innerText || DesignNam[count].text);
                            valu = (DesignVal[count].textContent || DesignVal[count].innerText || DesignVal[count].text);
                            //alert("text =" +text);
                            text = text.replace('&amp;', '&');
                            listItem = new Option(text, valu, false, false);
                            ddl_Designation.options[count + 1] = listItem;
                            if (valu == OrgDesignationId) {
                                ddl_Designation.options[count + 1].selected = true;
                                ddl_Designation.value = OrgDesignationId;
                                //Loading forward and approved authority value while getting loading
                                //ddl_Designation.onchange();			       
                            }
                        }
                        //ddl_Designation.value=OrgDesignationId;
                    }
                }
            }
            //alert(document.getElementById("ctl00_BodyContentPH_rbtnFlexibleTimingsYes")); 
            if (document.getElementById("ctl00_BodyContentPH_rbtnFlexibleTimingsYes") != null) {
                if (checkPageLoad == 1) {
                    GetFlexibleEmployeeData();
                }
            }
        }
        else {
            clearDDL_getOrg();
        }
    }
}
function createDDL_getOrg() {
    var htm = '';
    htm += "<div class='row form-group'>";
    var chVal = 0;
    var val = 0;
    for (i = 0; i <= totLevels - 1; i++) {
        if (chVal >= 2) {
            chVal = 0
            htm += "</div><div class='row form-group'>"
        }
        chVal += 1;
        val = parseFloat(i) + 1

        if (i == 0) {
            htm += "<div class='col-md-2'>" + levelNameArray[i] + " </div><div class='col-md-3'>";
            htm += "<select id=ddlLevel" + i + " runat='server' class='form-control select2' onChange='getChild_getOrg(" + val + ",this.value)'><option>" + Sel + "</option></select>";
            htm += "</div><div class='col-md-1'></div>";
        }
        else {
            htm += "<div class='col-md-2'>" + levelNameArray[i] + " </div><div class='col-md-3'>";
            htm += "<select id=ddlLevel" + i + " runat='server' class='form-control select2' onChange='getChild_getOrg(" + val + ",this.value)'><option>" + Sel + "</option></select>";
            htm += "</div><div class='col-md-1'></div>";
        }
    }
    if (chVal < 2) {
        htm += "<div class='col-md-3'></div><div class='col-md-3'></div>";
    }
    htm += "</div>";
    //Adding if condition for checking whether this values is null or not by -Added by MAnjula.P on 27-Jan-2014 for javascript error
    if (document.getElementById('div_child') != null) {
        document.getElementById('div_child').innerHTML = htm;
        //checkPageLoad=1;
        assignValue_getOrg();
        LoadDropdownScript();
    }
}

function clearDDL_getOrg() {
    for (iloop = curDDL; iloop <= totLevels - 1; iloop++) {
        seleDDL = FindElt_Id_getOrg("ddlLevel" + iloop);
        for (var count = seleDDL.options.length - 1; count > 0; count--) {
            seleDDL.options[count] = null;
        }
    }
    LoadDropdownScript();

    return false
}
function assignValue_getOrg() {
    //alert("Loading Organization Levels....");

    getChild_getOrg(0, 0);
    for (iloopp = 0; iloopp <= orgArray.length - 2; iloopp++) {
        //organization not selected.
        if (orgArray[0] == 0) {
            getChild_getOrg(iloopp, orgArray[iloopp]);
        }
            //organization selected.
        else {
            getChild_getOrg(iloopp + 1, orgArray[iloopp]);
        }
        seleDDL = FindElt_Id_getOrg("ddlLevel" + iloopp);
        if (orgArray[iloopp] != 0) {
            seleDDL.value = orgArray[iloopp];
        }
    }
    checkPageLoad = 1;
}
function assignValue_getOrg2(iloopp) {
    if (iloopp == null) {
        iloopp = 0
    }
    document.write(iloopp + 1 + " " + orgArray[iloopp])
    getChild_getOrg(iloopp + 1, orgArray[iloopp]);
    seleDDL = FindElt_Id_getOrg("ddlLevel" + iloopp);
    if (orgArray[iloopp] != 0) {
        seleDDL.value = orgArray[iloopp];
    }
    for (j = 0; j <= seleDDL.length; j++) {
        document.write(seleDDL.options[j])
    }
    iloopp = iloopp + 1;
    if (iloopp <= orgArray.length - 2) {
        assignValue_getOrg(iloopp)
    }
}
