// JScript File

/// Count Number Remaining Leave Days



function NumberOfdays()
{

var EmployeeId;

EmployeeId=document.getElementById('ctl00_CP_Transaction_hdnEmployeeId').value;


if(EmployeeId=="" && document.getElementById('ctl00_CP_Transaction_ddl_Type').value!="-Select-")
{
//alert("Select an Employee");
return;
}

//alert(EmployeeId);

//alert(document.getElementById('ctl00_CP_Transaction_ddl_Type').selectedIndex);
if(document.getElementById('ctl00_CP_Transaction_ddl_Type').value=="-Select-")
{
document.getElementById('ctl00_CP_Transaction_ddl_Type').selectedIndex=0;
document.getElementById('ctl00_CP_Transaction_ddl_Type').focus();
//alert('Select Leave Type');
return;
}
if(document.getElementById('ctl00_CP_Transaction_ddl_LeaveType').value=="-Select-")
{
document.getElementById('ctl00_CP_Transaction_ddl_LeaveType').selectedIndex=0;

document.getElementById('ctl00_CP_Transaction_ddl_LeaveType').focus();
//alert('Select Leave Type');
return;
}

	try
	{
		obj = new XmlHttpRequest();
	}
	catch(e)
	{
		try
		{
			obj = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(e1)
		{
			obj = new ActiveXObject("Msxml2.XMLHTTP");
		}
	}
	
	if(obj != null)
	{
	  var FromDate=document.aspnetForm.ctl00_CP_Transaction_txtFromDate.value;
	  if(FromDate=="")
	  {
	    return false;
	  }
	  
	  var ToDate=document.aspnetForm.ctl00_CP_Transaction_txtToDate.value;
	  if(ToDate=="")
	  {
	    return false;
	   
	  }
	  var TosessionIndex=document.aspnetForm.ctl00_CP_Transaction_ddl_SessionTo.selectedIndex;
	  var Tosession=document.aspnetForm.ctl00_CP_Transaction_ddl_SessionTo.options[TosessionIndex].value;   
	  var FromsessionIndex=document.aspnetForm.ctl00_CP_Transaction_ddl_SessionFrom.selectedIndex;
	  var Fromsession=document.aspnetForm.ctl00_CP_Transaction_ddl_SessionFrom.options[FromsessionIndex].value;   
	    
	    
	    if(FromsessionIndex==0 && TosessionIndex==0)
	    {
	    	document.getElementById('ctl00_CP_Transaction_hdnNumberofDaysApplied').value="";
	        document.getElementById('ctl00_CP_Transaction_txt_Numberofdays').value="";
	        return false;
	    }
		else if(FromsessionIndex==0)
	    {
	        //alert("Select From Session");
	        document.getElementById('ctl00_CP_Transaction_hdnNumberofDaysApplied').value="";
	        document.getElementById('ctl00_CP_Transaction_txt_Numberofdays').value="";
	        return false;
	    }
	    else if(TosessionIndex==0)
	    {
	        //alert("Select To Session");
	        document.getElementById('ctl00_CP_Transaction_hdnNumberofDaysApplied').value="";
	        document.getElementById('ctl00_CP_Transaction_txt_Numberofdays').value="";
            return false;
	    }
	 
	  
	    var hdnLeaveTypeId=document.getElementById('ctl00_CP_Transaction_ddl_LeaveType').value;
	    var k=Math.floor(Math.random()*111);
		    var link = "NumberofDaysCount.aspx?";
		    link = link + "&" + "FromDate=" + FromDate;
		    link = link + "&" + "ToDate=" + ToDate;
		    link = link + "&" + "Fromsession=" + Fromsession;
		    link = link + "&" + "Tosession=" + Tosession;
		    link=link + "&" + "LeaveTypeId=" + hdnLeaveTypeId;
		    link=link + "&" + "EmployeeId=" + EmployeeId;
		    link=link + "&" + "k=" + k;
		    obj.onreadystatechange = ProcessResponseNumberOfDays;		
		    obj.open("POST", link, true);
		    obj.send(null);
		
	}
	
}
function ProcessResponseNumberOfDays()
{
	if (obj.readyState == 4)
	{
		if(obj.status == 200)
		{
	      var responseDays=obj.responseText;
	       
	      if(responseDays=="Holiday")
	      {
	      document.getElementById('ctl00_CP_Transaction_txt_Numberofdays').value="";
	      alert("You are selecting Holiday");
	      return;
	      } 
	      if(responseDays=="WeeklyOff")
	      {
	      document.getElementById('ctl00_CP_Transaction_txt_Numberofdays').value="";
	      alert("You are selecting Weekly Off");
	      return;
	      } 
	      
	      if(responseDays=="NoLeaveTypeId")
	      {
	      document.getElementById('ctl00_CP_Transaction_txt_Numberofdays').value="";
	      alert("Select Leave and LeaveTypeId");
	      return;
	      }
	      
	      var NumberOfDays=responseDays.split(",");
	      
	      if(NumberOfDays[0]<0)
	      {
	      alert("Select Valid From Date and To Date");
	      document.getElementById('ctl00_CP_Transaction_hdnNumberofDaysApplied').value="";
	      document.getElementById('ctl00_CP_Transaction_txt_Numberofdays').value="";
	      return;
	      }
	      
	      if(NumberOfDays[1]==1)
	      {
	      alert("You have choosed after weekly Off, One Day Added");
	      }
	      else if(NumberOfDays[1]==2)
	      {
	      alert("You have choosed Before weekly Off, One Day Added");
	      }
	       else if(NumberOfDays[1]==3)
	      {
	      alert("You have selected weekly Off / Global Holiday, Days Deducted");
	      }
	      document.getElementById('ctl00_CP_Transaction_hdnNumberofDaysApplied').value=NumberOfDays[0];
	      
          document.getElementById('ctl00_CP_Transaction_txt_Numberofdays').value=NumberOfDays[0];
    
    	}
	}
}