// 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)
	{
	    debugger;
	   // if (getElt_Id().selectedIndex!=0){
	            if (aspnetForm.ctl00_BodyContentPH_hdf_orgLevel!=null)
                    {
	                aspnetForm.ctl00_BodyContentPH_hdf_orgLevel.value=para2;
	            }
	            curDDL = para1;	   
		        var url
		        if(window.ActiveXObject)
                {
		            obj.onreadystatechange = ProcessResponse_getOrg;
   		             if (para2 == 'Select' && para1!=1 )
   		             {       
                           para2 = FindElt_Id_getOrg("ddlLevel"+(para1-2)).value;
    	             }		 
		            //url=geturl()					
		            //obj.open("POST", "../Masters/TA_UI_Master_getOrg.aspx?level="+para2+"&levelno"+para3,  false);
		            obj.open("POST", "../Masters/TA_UI_Master_getOrg.aspx?level="+para2 + "&ctr="+ para1,  false);
		            obj.send(null); 
		        }
		         else if(window.XMLHttpRequest)
		         {
   		             if (para2 == 'Select' && para1!=1 )
   		             {       
                           para2 = FindElt_Id_getOrg("ddlLevel"+(para1-2)).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;
	  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;
		        }
	        }
        }  
//	try
//	{
//		obj = new ActiveXObject("Msxml2.XMLHTTP");
//	}
//	catch(e)
//	{
//		try
//		{
//			obj = new ActiveXObject("Microsoft.XMLHTTP");
//		}
//		catch(e1)
//		{
//		
//			obj = null;
//		}
//	}
	if(obj!=null)
	{
		var url
		if(window.ActiveXObject)
        {
		    obj.onreadystatechange = ProcessResponse_getOrg;
		    url=geturl_getOrg();		
		    obj.open("POST", url+"level="+para1,  true);
		    obj.send(null);
		}   
		else if(window.XMLHttpRequest)
        {
	        url=geturl_getOrg();		
	        obj.open("POST", url+"level="+para1,  false);
	        obj.send(null);
	        obj.onreadystatechange = ProcessResponse_getOrg();
        }  
	    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++)
	{
	    debugger;
	        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);
	}
}
function ProcessResponse_getOrg()
{
    debugger;
    if(obj.readyState == 4)
	{	    
	    if(obj.status == 200)
		{
//	        var htm	        
//	        if (curDDL != 0 && curDDL<totLevels) 
//	        {	            
//	            val = curDDL + 1;
//	            htm = "<br><select name=ddlLevel" + curDDL + " runat='server' onChange='getChild("+ val +",this.value)'><option>Select Below</option></select><br>";	            
//	            document.getElementById('div_child'+ curDDL).innerHTML = htm;
//	         }  
			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 span1 = document.getElementById("ss");
			    var html = "";			
			    var text; 
			    var valu;
			    var listItem;    
			    listItem = new Option(Sel,'Select',  false, false);
			     //listItem = new Option(Select,'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;','&'); 
				    //alert("text =" +text);
				    listItem = new Option(text,valu,  false, false);
				    ddlOrders.options[ddlOrders.length] = listItem;
				    ddlOrders.options[count + 1].title=text;
			    }	
			}
//			if (document.getElementById("ctl00_BodyContentPH_ddl_Designation")!=null)
//			    {
//			    GetEmployeeData();              //fill designation ddl
//			    }
		}   
		else
		{
		    clearDDL_getOrg();
			//alert("Error retrieving data!" );
		}	  
	}
}
function createDDL_getOrg()
{
    debugger;
    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 Man-dat' 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 Man-dat' 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();
    } 
}

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;
			    }	      
	        //document.getElementById('div_child'+ iloop).innerHTML = "";
	    }
	return false
}   
//var iloopp = 0;
function assignValue_getOrg()
{ 
getChild_getOrg(0,0);
//     for(iloopp=0;iloopp<=levelArray.length-1;iloopp++)
//        {       
//            getChild_getOrg(iloopp+1,levelArray[levelArray.length-(iloopp+1)]) ;
//            seleDDL = FindElt_Id_getOrg("ddlLevel" + iloopp);
//            seleDDL.value= levelArray[levelArray.length-(iloopp+1)];    
//        }
//        document.getElementById('ddlLevel0').focus() ;
//        getChild_getOrg(iloopp+1,levelArray[levelArray.length-(iloopp+1)]) ;   
//        seleDDL = FindElt_Id_getOrg("ddlLevel" + iloopp);
//        seleDDL.value= levelArray[levelArray.length-(iloopp+1)];     
//        if(iloopp < levelArray.length )
//        {
//            iloopp = iloopp + 1;
//            assignValue_getOrg();
//        }
}
function test()
{
alert(totLevels)
getChild_getOrg(0,0);
    for(iloop=0;iloop<=totLevels-1;iloop++)
	    {
	    seleDDL = FindElt_Id_getOrg("ddlLevel"+iloop);
	   alert("name = " + seleDDL.name);
            if (seleDDL.length==2)
			    {
			        alert("test = " + seleDDL.name);
			        alert(seleDDL.options[seleDDL.length-1].value);
			        seleDDL.selectedIndex= seleDDL.length-1;
			        getChild_getOrg(iloop,seleDDL.options[seleDDL.length-1].value);
			    }
	    }
}