// JScript File

var objLeaveApplication = null;

function EmployeeNamesForEncashment()
{
        try
        {
             objLeaveApplication = new XMLHttpRequest();
        }
        catch(ex)
        {
            try
            {
                objLeaveApplication=new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e)
            {
                try
                {
                    objLeaveApplication=new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch(e1)
                {
                    objLeaveApplication=null;
                }
            }
        }
        
        if(objLeaveApplication!=null)
        {
        
            var k=Math.floor(Math.random()*111);
            objLeaveApplication.onreadystatechange=Process_Response_EmployeesEncashment;
            objLeaveApplication.open("GET","AjaxEmployeeForLeaveApplication.aspx?Sid="+k,true);
            objLeaveApplication.send(null);
        }
        
        return false;
}

function Process_Response_EmployeesEncashment()
{
    if (document.getElementById('ctl00_CP_Transaction_ddlEmployeeNames').options.length != null)
	{				
        for (var count = document.getElementById('ctl00_CP_Transaction_ddlEmployeeNames').options.length-1;count >-1; count--)
		{
		    document.getElementById('ctl00_CP_Transaction_ddlEmployeeNames').options[count] = null;
		}	
    }
    
		
	if(objLeaveApplication.readyState == 4)
	{
        if(objLeaveApplication.status == 200)
		{
            if(objLeaveApplication.responseText=="Fail" || objLeaveApplication.responseText=="")
			{
			    var newOptSelect = new Option('-Select-','-Select-',  false, false);
                document.getElementById('ctl00_CP_Transaction_ddlEmployeeNames').options[0]=newOptSelect;
           		alert("No Records");
				return;
			}
			else
			{
			    //alert(objLeaveApplication.responseTEXT);
				var xml=objLeaveApplication.responseXML;
				var dsRoot=xml.documentElement.getElementsByTagName("Details");  
				var totalRec = dsRoot.length;
                var newOptSelect = new Option('-Select-','-Select-',  false, false);
                document.getElementById('ctl00_CP_Transaction_ddlEmployeeNames').options[0]=newOptSelect;
                        
                for(i=0; i<totalRec; i++)
                {
                    var ConId = xml.getElementsByTagName("Employee_x0020_Name")[i];
                    var EmpId=xml.getElementsByTagName("Employee_Id")[i];
                    var newOpt=new Option(ConId.text,EmpId.text);
                    document.getElementById('ctl00_CP_Transaction_ddlEmployeeNames').options[i+1]=newOpt;
                }
                
                document.getElementById('ctl00_CP_Transaction_ddlEmployeeNames').focus();
            }
        }
        else
		{
		    alert("Error in retriving details");
		}
			
    }
}



function assignEmployeeIdEncashment(empid)
{
  document.getElementById('ctl00_CP_Transaction_hdnEmployeeId').value=empid;
  alert(empid);
  document.getElementById('ctl00_CP_Transaction_ddl_Leavetype').focus();
   
}



var objLeaveTypes = null;

function leaveOptionsEncashment(empid)
{
    
       if (document.getElementById('ctl00_CP_Transaction_ddlEmployeeNames').selectedIndex==0)
       {
            alert("Select Employee");
            
            if (document.getElementById('ctl00_CP_Transaction_ddl_Leavetype').options.length != null)
        {				
            for (var count = document.getElementById('ctl00_CP_Transaction_ddl_Leavetype').options.length-1;count >-1; count--)
            {
                document.getElementById('ctl00_CP_Transaction_ddl_Leavetype').options[count] = null;
            }	
        }
            
            return;   
    
       }
	
    
    
    if(leaveSource=="-Select-")
    {
        //alert("Select a type of Leave");
        if (document.getElementById('ctl00_CP_Transaction_ddl_Leavetype').options.length != null)
        {				
            for (var count = document.getElementById('ctl00_CP_Transaction_ddl_Leavetype').options.length-1;count >-1; count--)
            {
                document.getElementById('ctl00_CP_Transaction_ddl_Leavetype').options[count] = null;
            }	
        }
        
        
        var newOptSelect = new Option('-Select-','-Select-',  false, false);
        document.getElementById('ctl00_CP_Transaction_ddl_Leavetype').options[0]=newOptSelect;
        return;   
    }
    
        document.getElementById('ctl00_CP_Transaction_hdnEmployeeId').value=empid;
        //alert(empid);
        //document.getElementById('ctl00_CP_Transaction_ddl_Leavetype').focus();
  
 
    try
    {
        objLeaveTypes = new XMLHttpRequest();
    }
    catch(ex)
    {
        try
            {
                objLeaveTypes=new ActiveXObject("Msxml2.XMLHTTP");
            }
        catch(e)
            {
                try
                    {
                        objLeaveTypes=new ActiveXObject("Microsoft.XMLHTTP");
                    }
                catch(e1)
                    {
                        objLeaveTypes=null;
                    }
            }
    }

    if(objLeaveTypes!=null)
    {
        var k=Math.floor(Math.random()*111);
        var EmpId=document.getElementById('ctl00_CP_Transaction_hdnEmployeeId').value;
        
        var leaveSource="Encashment";
        
        if(EmpId==null || EmpId==0 || EmpId=="-Select-")
        {
            return;
            document.getElementById('ctl00_CP_Transaction_txtRemainingNoOfLeave').value=0.0;     
        }

        objLeaveTypes.onreadystatechange=Process_Response_EmployeesLeaveOptionsEncashment;
        objLeaveTypes.open("GET","AjaxLeaveOptions.aspx?leaveSource=" + leaveSource + "&EmpId="+ EmpId + "&k="  + k,true);
        objLeaveTypes.send(null);
    }
        
    return false;
}


//Fill Leave Types

function Process_Response_EmployeesLeaveOptionsEncashment()
{
    if (document.getElementById('ctl00_CP_Transaction_ddl_Leavetype').options.length != null)
	{				
	    for (var count = document.getElementById('ctl00_CP_Transaction_ddl_Leavetype').options.length-1;count >-1; count--)
		{
		    document.getElementById('ctl00_CP_Transaction_ddl_Leavetype').options[count] = null;
		}	
    }
			
	if(objLeaveTypes.readyState == 4)
	{
	    if(objLeaveTypes.status == 200)
		{
		    if(objLeaveTypes.responseText=="Fail" || objLeaveTypes.responseText=="")
			{
			    
			    var newOptSelect = new Option('-Select-','-Select-',  false, false);
                document.getElementById('ctl00_CP_Transaction_ddl_Leavetype').options[0]=newOptSelect;
				alert("No Records");
				return;
			}
			else
			{
			    var xml=objLeaveTypes.responseXML;
				var dsRoot=xml.documentElement.getElementsByTagName("Details");  
				var totalRec = dsRoot.length;
				var newOptSelect = new Option('-Select-','-Select-',  false, false);
                document.getElementById('ctl00_CP_Transaction_ddl_Leavetype').options[0]=newOptSelect;
          
                for(i=0; i<totalRec; i++)
                {                     
                        var ConId = xml.getElementsByTagName("LeaveType")[i];
                        var LeaveTypeId=xml.getElementsByTagName("LeaveType_Id")[i];
                        
                        var newOpt=new Option(ConId.text,LeaveTypeId.text);
                        document.getElementById('ctl00_CP_Transaction_ddl_Leavetype').options[i+1]=newOpt;
                                                                  
               }
               document.getElementById('ctl00_CP_Transaction_ddl_Leavetype').focus();
            }
        }
        else
	    {
	        alert("Error in retriving details");
        }
    }
}



// Get Remaining no of leaves per leave type

var objRemainingNoOfLeaves = null;

function RemainingNoOfEncashmentLeaves(leaveTypeId)
{
   // alert(leaveTypeId);
    if(leaveTypeId=='-Select-')
    {
  
        document.getElementById('ctl00_CP_Transaction_txt_RemainingNoOfDays').value="";
        document.getElementById('ctl00_CP_Transaction_hdnLeaveTypeId').value="";
        return;
    }

    try
        {
             objRemainingNoOfLeaves = new XMLHttpRequest();
        }
        catch(ex)
                  {
                    try
                        {
                            objRemainingNoOfLeaves=new ActiveXObject("Msxml2.XMLHTTP");
                        }
                    catch(e)
                        {
                            try
                                {
                                    objRemainingNoOfLeaves=new ActiveXObject("Microsoft.XMLHTTP");
                                }
                            catch(e1)
                                {
                                    objRemainingNoOfLeaves=null;
                                }
                        }
                 }
        
if(objRemainingNoOfLeaves!=null)
{

    var k=Math.floor(Math.random()*111);
    var EmpId=document.getElementById('ctl00_CP_Transaction_hdnEmployeeId').value;
    document.getElementById('ctl00_CP_Transaction_hdnLeaveTypeId').value=leaveTypeId;
    
    //alert(EmpId + leaveTypeId);
    
    objRemainingNoOfLeaves.onreadystatechange=Process_Response_EmployeesRemainingNoOfEncashmentDays;
    objRemainingNoOfLeaves.open("GET","AjaxRemainingNoOfEncashmentDays.aspx?leaveTypeId=" + leaveTypeId + "&EmpId="+ EmpId + "&k="  + k,true);
    objRemainingNoOfLeaves.send(null);
    
}
        
return false;
}


function Process_Response_EmployeesRemainingNoOfEncashmentDays()
{
    if(objRemainingNoOfLeaves.readyState == 4)
	{
	    if(objRemainingNoOfLeaves.status == 200)
		{
		    var xml=objRemainingNoOfLeaves.responseXML;
			
			if(objRemainingNoOfLeaves.responseText=="Fail" || objRemainingNoOfLeaves.responseText=="")
			{
			    document.getElementById('ctl00_CP_Transaction_txt_RemainingNoOfDays').value=0.0;
			    document.getElementById('ctl00_CP_Transaction_hdnRemainingNoOfDays.Value').value="0";
				return;
			}
			else
			{
			    var dsRoot=xml.documentElement.getElementsByTagName("Details"); 
				//alert(xml.getElementsByTagName("RemainingNoOfLeaves")[0].text);		
				var totalRec = dsRoot.length;
				document.getElementById('ctl00_CP_Transaction_txt_RemainingNoOfDays').value=xml.getElementsByTagName("RemainingNoOfLeaves")[0].text;
                
                document.getElementById('ctl00_CP_Transaction_hdnRemainingNoOfDays').value=xml.getElementsByTagName("RemainingNoOfLeaves")[0].text;
                
                // document.getElementById('ctl00_CP_Transaction_divRemainingNoOfLeave').innerHTML=xml.getElementsByTagName("RemainingNoOfLeaves")[0].text;
            }
        }
        else
		{
		    alert("Error in retriving details");
		}
			
	}
}