// JScript File
/// Count Number of Hours Remaining
function NumberOfHours
(
SelectEmployee,
SelectvalidPermissionDate,
SelectvalidPermissionFromDateandTime, 
SelectvalidPermissionToDateandTime,
Selectvalidyear,                       
Permissionshouldnotbeappliedbeforedateofjoining,
PermissionHoursfallsonWeeklyOffGlobalHoliday, 
SelectValidPermissionHours,
ShiftNotAllocated,
PermissionHoursdoesnotfallinsidetheshift, 
PermissionHoursfallwiththeshiftbreaktimes,
PermissionHoursShouldNotInNegativeHours,                      
PermissionHoursNotdefinedforthisemployee,
MaxNumberofInstancespermonthExceeds 
)
{
   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 XmlHttpRequest();
//	}
//	catch(e)
//	{
//		try
//		{
//			obj = new ActiveXObject("Microsoft.XMLHTTP");
//		}
//		catch(e1)
//		{
//			obj = new ActiveXObject("Msxml2.XMLHTTP");
//		}
//	}
	if(obj != null)
	{
	//Included by A.Amsaveni on 04/10/2013 changed txt_starttime as txt_FromDateTime 
	 // var FromTime=document.aspnetForm.ctl00_BodyContentPH_txt_StartTime.value;
	 //Changed FromDateTime to txt_StartTime for Leavepermission for hiding calender for every selection problem by MAnjula.P on 13-Feb-2014
	 var FromTime= '';
	    if(document.aspnetForm.ctl00_BodyContentPH_txt_StartTime != null){
          FromTime= document.aspnetForm.ctl00_BodyContentPH_txt_StartTime.value;
        }
//        else if(document.aspnetForm.ctl00_BodyContentPH_txt_StartTime != null){
//          FromTime= document.aspnetForm.ctl00_BodyContentPH_txt_StartTime.value;
//        }
	  if(FromTime=="")
	  {
	    return false;
	  }
	  var EndTime=document.aspnetForm.ctl00_BodyContentPH_txt_EndTime.value;
	  if(EndTime=="")
	  {
	    return false;
	  }
	  var PermissionDate=document.aspnetForm.ctl00_BodyContentPH_TxtPermissionDate.value;
	  if(PermissionDate=="")
	  {
	    return false;
	  }
		    var EmployeeID=document.getElementById('ctl00_BodyContentPH_hdnEmployeeId').value;
		    //alert(EmployeeID);
		    if(EmployeeID=="-Select-" || EmployeeID=="" )
	        {
	            document.getElementById('ctl00_BodyContentPH_hdnRemainNoOfHours').value="00:00";
                document.getElementById('tdRemainNoOfHours').innerHTML="00:00";
                document.getElementById('ctl00_BodyContentPH_txt_NumberOfHours').value="";
                alert(SelectEmployee);
	            return false;
	        }
		    var link = "NumberofHoursCount.aspx?";
		    link = link + "&" + "FromTime=" + FromTime;
		    link = link + "&" + "EndTime=" + EndTime;
		    link = link + "&" + "PermissionDate=" + PermissionDate;
		    link = link + "&" + "EmpID=" + EmployeeID;
		    if(window.ActiveXObject)
            {
		        obj.onreadystatechange = ProcessResponseHours;		
		        obj.open("POST", link, true);
		        obj.send(null);
		    }
		    else if(window.XMLHttpRequest)
            {
		        obj.open("POST", link, false);
		        obj.send(null);
		        obj.onreadystatechange = ProcessResponseHours();		
            }
	}
}
function ProcessResponseHours()
{
	if (obj.readyState == 4)
	{
		if(obj.status == 200)
		{
	      var response=obj.responseText;
	      var HoursMaxandRemain=response.split(",");
	      if(response=="InvalidPermissionDate")
	      {
	      document.getElementById('ctl00_BodyContentPH_txt_NumberOfHours').value="";
	      alert(SelectvalidPermissionDate);
	      return;
	      }
	      if(response=="InvalidFromDate")
	      {
	      document.getElementById('ctl00_BodyContentPH_txt_NumberOfHours').value="";
	      alert(SelectvalidPermissionFromDateandTime);
	      return;
	      }
	      if(response=="InvalidToDate")
	      {
	      document.getElementById('ctl00_BodyContentPH_txt_NumberOfHours').value="";
	      alert(SelectvalidPermissionToDateandTime);
	      return;
	      }      
	      if(response=="InvalidYear")
	      {
	      document.getElementById('ctl00_BodyContentPH_txt_NumberOfHours').value="";
	      alert(Selectvalidyear);
	      return;
	      }
	      if(response=="DateOfJoining")
	      {
	      document.getElementById('ctl00_BodyContentPH_txt_NumberOfHours').value="";
	      alert(Permissionshouldnotbeappliedbeforedateofjoining);
	      return;
	      }
	      if(response=="WeeklyOffHoliday")
	      {
	      alert(PermissionHoursfallsonWeeklyOffGlobalHoliday);
	      document.getElementById('ctl00_BodyContentPH_txt_NumberOfHours').value="";
	      return false;
	      }
	      else if(response=="FillBothTime")
	      {
	      document.getElementById('ctl00_BodyContentPH_txt_NumberOfHours').value="";
	      return false;
	      }
	      else if(response=="SelectValidHours")
	      {
	        alert(SelectValidPermissionHours);
	        document.getElementById('ctl00_BodyContentPH_txt_NumberOfHours').value="";
	        return false;
	      }
	      else if (response=="ShiftNotAllocated")
	      {
	        alert(ShiftNotAllocated);
	        document.getElementById('ctl00_BodyContentPH_txt_NumberOfHours').value="";
	        return false;
	      }
	       else if (response=="InValidHours")
	      {
	        alert(PermissionHoursdoesnotfallinsidetheshift);
	        document.getElementById('ctl00_BodyContentPH_txt_NumberOfHours').value="";
	        return false;
	      }
//	      else if (HoursMaxandRemain[1]=="InBetweenBreakHours")
//	      {
//	        alert(PermissionHoursfallwiththeshiftbreaktimes);
//	      }
          else if (response=="InBetweenBreakHours")
          {
            alert("Permission hours fall with the shift break times");
            return false;
          }
          else if (HoursMaxandRemain[1]=="InBetweenBreakHours_DeductHours")
          {
            alert("Break hours will be deducted from this permission hours");
          }
	       else if(response=="Fail")
	      {	      
	      if(document.getElementById('ctl00_BodyContentPH_hdnNegativeValue').value!="-1")
	      {	        
	        alert(PermissionHoursShouldNotInNegativeHours);	        
	        return false;
	      }
	      else
	      {
	      return false;
	      }
	      }
          document.getElementById('ctl00_BodyContentPH_txt_NumberOfHours').value=HoursMaxandRemain[0];
          document.getElementById('ctl00_BodyContentPH_hdnNegativeValue').value="";    
    	}
	}
}
function assignEmployeeIdPermission(con,SelectEmployee,SelectvalidPermissionDate,SelectvalidPermissionFromDateandTime, SelectvalidPermissionToDateandTime,Selectvalidyear,Permissionshouldnotbeappliedbeforedateofjoining,PermissionHoursfallsonWeeklyOffGlobalHoliday,SelectValidPermissionHours,ShiftNotAllocated,PermissionHoursdoesnotfallinsidetheshift,PermissionHoursfallwiththeshiftbreaktimes,PermissionHoursShouldNotInNegativeHours,PermissionHoursNotdefinedforthisemployee,MaxNumberofInstancespermonthExceeds)
{  
	var EmpId=document.getElementById('ctl00_BodyContentPH_hdnEmployeeId').value;	
	var permissionDate=document.getElementById('ctl00_BodyContentPH_TxtPermissionDate').value;   
    document.getElementById('ctl00_BodyContentPH_hdnRemainNoOfHours').value="00:00";
    document.getElementById('tdRemainNoOfHours').innerHTML="00:00";
    document.getElementById('ctl00_BodyContentPH_txt_NumberOfHours').value="";	
	if(EmpId=="-Select-" || EmpId=="" )
	{
	    return false;
	}
	else
	{	
	    EmpId=document.getElementById('ctl00_BodyContentPH_hdnEmployeeId').value;	
	}
	if(window.ActiveXObject)
    {    
	    try
	    {
		    objReMainHours = new ActiveXObject("Msxml2.XMLHTTP");
	    }
	    catch(e)
	    {
		    try
		    {
			    objReMainHours = new ActiveXObject("Microsoft.XMLHTTP");
		    }
		    catch(e1)
		    {
			    objReMainHours = null;
		    }
	    }
    }  
     else if(window.XMLHttpRequest)
     {
	    try
	    {
		    objReMainHours = new XMLHttpRequest("Msxml2.XMLHTTP");
	    }
	    catch(e)
	    {
		    try
		    {
			    objReMainHours = new XMLHttpRequest("Microsoft.XMLHTTP");
		    }
		    catch(e1)
		    {
			    objReMainHours = null;
		    }
	    }
    }	
	if(objReMainHours != null)
	{
		    var link = "NumberofRemainingHoursCount.aspx?";
		     var k=Math.floor(Math.random()*111);
		    link = link + "&" + "EmpId=" + EmpId;
		    link = link + "&" + "permissionDate=" + permissionDate;
		    link = link + "&" + "k=" + k;
		    if(window.ActiveXObject)
            {
		        objReMainHours.onreadystatechange = ProcessResponseRemainingHours;		
		        objReMainHours.open("POST", link, true);
		        objReMainHours.send(null);
		    }
		    else if(window.XMLHttpRequest)
		    {
		        objReMainHours.open("POST", link, false);
		        objReMainHours.send(null);
		        objReMainHours.onreadystatechange = ProcessResponseRemainingHours();		
		    }		
	}
}
function ProcessResponseRemainingHours()
{
	if (objReMainHours.readyState == 4)
	{
		if(objReMainHours.status == 200)
		{
	        var response=objReMainHours.responseText;
	        var rangeDemoFormat =  document.getElementById('ctl00_BodyContentPH_Hidden1').value;
	        //ToDo: To Check the Date
//            var rangeDemoConv = new AnyTime.Converter({format:rangeDemoFormat});if(rangeDemoFormat =='DD-MMM-YYYY'){var Earlier="01-Jan-1900";var Final="31-Dec-9999";}else{var Earlier="01/01/1900";var Final="12/31/9999";}    
//            $("#ctl00_BodyContentPH_TxtPermissionDate").AnyTime_noPicker();
//            $("#ctl00_BodyContentPH_TxtPermissionDate").AnyTime_picker({format:rangeDemoFormat});    
//            var rangeDemoFormats =  document.getElementById('ctl00_BodyContentPH_Hidden1').value + " " + document.getElementById('ctl00_BodyContentPH_Hidden2').value;
//            var rangeDemoConvs = new AnyTime.Converter({format:rangeDemoFormats});if(rangeDemoFormats =='DD-MMM-YYYY'){var Earlier="01-Jan-1900";var Final="31-Dec-9999";}else{var Earlier="01/01/1900";var Final="12/31/9999";}    
//Commented by Manjula.P on 14-Feb-2014 . This is the reason for closing from date in permission automatically issue
//            $("#ctl00_BodyContentPH_txt_StartTime").AnyTime_noPicker();
//            $("#ctl00_BodyContentPH_txt_StartTime").AnyTime_picker({format:rangeDemoFormats});
	      if(response=="Fail")
	      {
	      	//document.getElementById('ctl00_BodyContentPH_txt_FromDateTime').value='';
	      	document.getElementById('ctl00_BodyContentPH_txt_StartTime').value='';	 
	      	//Commented by MAnjula.P on 08-Feb-2014
	        //document.getElementById('ctl00_BodyContentPH_ddlEmployeeNames').focus(); 
	        document.getElementById('ctl00_BodyContentPH_txt_EmployeeId').focus(); 
	        alert(PermissionHoursNotdefinedforthisemployee);    
	        return false;
	      }
	      else if(response=="MaxInstancesMonthExceed")
	      {
	      	//document.getElementById('ctl00_BodyContentPH_TxtPermissionDate').focus();
	      	document.getElementById('ctl00_BodyContentPH_TxtPermissionDate').value='';
	        alert(MaxNumberofInstancespermonthExceeds);
	        document.getElementById('ctl00_BodyContentPH_hdnRemainNoOfHours').value="00:00";
	        document.getElementById('tdRemainNoOfHours').innerHTML="00:00";
	        return false;
	      }
	      var HoursMaxandRemain=response.split(",");
	    document.getElementById('ctl00_BodyContentPH_hdnRemainNoOfHours').value=HoursMaxandRemain[0];         
        document.getElementById('tdRemainNoOfHours').innerHTML=HoursMaxandRemain[0];   
    	}
	}
}