// 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;
		    }
	    }
	    if(obj!=null)
	    {
	        curDDL = para1;	   
		    var url
		    ses=0;
		    obj.onreadystatechange = ProcessResponse_getOrg;
		    if (para2 == 'Select' && para1!=1 )
		    {       
                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;
	        }		    
		    obj.open("POST", "../Masters/TA_UI_Master_getOrg.aspx?level="+para2 + "&ctr="+ para1 + "&ses=" + ses ,  false);
		    obj.send(null);  	        
	        return false;
	    }
    }
	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 (document.getElementById("ctl00_BodyContentPH_hdf_orgLevel")!=null)
        {
	        document.getElementById("ctl00_BodyContentPH_hdf_orgLevel").value=para2;
        }
        curDDL = para1;	   
		var url
		if (para2 == 'Select' && para1!=1 )
		{       
            para2 = FindElt_Id_getOrg("ddlLevel"+(para1-1)).value;
        }		 
        obj.open("POST", "../Masters/TA_UI_Master_getOrg.aspx?level="+para2 + "&ctr="+ para1,  false);
		obj.send(null); 
		obj.onreadystatechange = ProcessResponse_getOrg();
    }
	return false;	
}
function chckLevel1_getOrg(para1, para2, Select)
{ 
    Sel = Select; 
	try
	{
		obj = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			obj = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e1)
		{
			obj = null;
		}
	}
	if(obj!=null)
	{
		var url
		obj.onreadystatechange = ProcessResponse_getOrg;
		url=geturl_getOrg()			
		obj.open("POST", url+"level="+para1,  true);
		obj.send(null);     
	}	
	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);
}
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;
            }	
	    }
	    return dsRoot.getElementsByTagName(para);
	}
}
function ProcessResponse_getOrg()
{
    if(obj.readyState == 4)
	{	    
	    if(obj.status == 200)
		{
			var ddlOrders = getElt_Id_getOrg()		
			if (ddlOrders != null){				
			    for (var count = ddlOrders.options.length-1;count >-1; count--)
			    {
				    ddlOrders.options[count] = null;
			    }			
			    var tagVal = getTag_Name_getOrg('id')
			    var tagNam = getTag_Name_getOrg('name')
			    var html = "";			
			    var text; 
			    var valu;
			    var listItem;    
			    listItem = new Option(Sel,'Select',  false, false);			    
				ddlOrders.options[ddlOrders.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);				    
				    text = text.replace('&amp;','&'); 
				    listItem = new Option(text,valu,  false, false);
				    ddlOrders.options[ddlOrders.length] = listItem;
				    ddlOrders.options[count + 1].title=text;
			    }
			}			
			if (document.getElementById("ctl00_BodyContentPH_rbtnFlexibleTimingsYes")!=null)
			{
			if (checkPageLoad==1)
			{
    			GetFlexibleEmployeeData();			
			}			
			}			
		}   
		else
		{
		    clearDDL_getOrg();
		}	    
	}
	return;
}
function createDDL_getOrg()
{    
    var htm='';
    htm += "<div class='row'>";
    var chVal = 0;
    var val=0;    
    for (i=0;i<=totLevels-1;i++)
    {
        if (chVal>=2)
        {
            chVal=0
            htm += "</div><div class='row'>"
        }
        chVal += 1;
       val = parseFloat(i)+1           
       htm += "<div class='col-md-2'>" + levelNameArray[i] + " </div><div class='col-md-4'>";
       htm += "<select id=ddlLevel" + i + " runat='server' class='form-control' onChange='getChild_getOrg("+ val +",this.value)'><option>"+ Sel +"</option></select>";
       htm += "</div>";
    }   
    if (chVal<2)
    {
      htm += "<div class='col-md-2'></div><div class='col-md-4'></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();
    } 
}

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;
			    }
	    }
	return false
}
function assignValue_getOrg()
{      
        //alert("Loading Organization Levels....");
     getChild_getOrg(0,0);   
     for(iloopp=0;iloopp<=orgArray.length-2;iloopp++)
        {   
           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){
            alert("hi");
            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)
        }
}