// JScript File
var objOT = null;
    function ajaxOTRoundoff(EnterAllthethree,NoDetails)
    {
var FromTime,Totime,Roundtime;
var rowCount=0
var p=0;
var FromTime1='',Totime1='',Roundtime1='';
var Fromarray=new Array();
var Toarray=new Array();
var Roundarray=new Array();
        if(window.ActiveXObject)
        {    
            try
            {
                objOT = new ActiveXObject("Msxml2.XMLHTTP");
            }
            catch(e)
            {
                try
                {
	                objOT = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch(e1)
                {
	                objOT = null;
                }
            }
        }  
         else if(window.XMLHttpRequest)
         {
            try
            {
                objOT = new XMLHttpRequest("Msxml2.XMLHTTP");
            }
            catch(e)
            {
                try
                {
	                objOT = new XMLHttpRequest("Microsoft.XMLHTTP");
                }
                catch(e1)
                {
	                objOT = null;
                }
            }
          }
//        try
//        {
//             objOT = new XMLHttpRequest();
//        }
//        catch(ex)
//        {
//            try
//            {
//                objOT=new ActiveXObject("Msxml2.XMLHTTP");
//            }
//            catch(e)
//            {
//                try
//                {
//                    objOT=new ActiveXObject("Microsoft.XMLHTTP");
//                }
//                catch(e1)
//                {
//                    objOT=null;
//                }
//            }
//        }
        if(objOT!=null)
        {                           
            var i;    
            // var mn = '<%= Session("OTRowCount") %>'; 
            //var nm = document.getElementById('<%= hdn_OTRowCount %>');
            if(document.getElementById("ctl00_BodyContentPH_hdf_controlID").value=="")
            {
             i=0;
            }
            else
            {
             i= parseInt(document.getElementById("ctl00_BodyContentPH_hdf_controlID").value);
             if(i>0)
             {
                   var from,to,roundedAs;
                   from = 'txt_From'+i;   
                   to = 'txt_To'+i;
                   roundedAs = 'txt_RoundedAs'+i;
                   if(document.getElementById(from)!=null && document.getElementById(to)!=null && document.getElementById(roundedAs)!=null)                 
                   {
                        from = document.getElementById(from).value; 
                        to = document.getElementById(to).value; 
                        roundedAs = document.getElementById(roundedAs).value;
                        if(from=='' || to=='' || roundedAs=='')
                        {
                         //alert('Enter all the three OT Roundoff values');
                         alert(EnterAllthethree);
                         return false;
                        } 
                   }
             }
             rowCount=parseInt(document.getElementById("ctl00_BodyContentPH_hdf_controlID").value);;
             if (rowCount >0)
             {
             var j=0;
                 for(j=1;j<=rowCount;j++)
                 {
                       FromTime = 'txt_From'+j;   
                       Totime = 'txt_To'+j;
                       Roundtime = 'txt_RoundedAs'+j;
                           if(document.getElementById(FromTime)!=null && document.getElementById(Totime)!=null && document.getElementById(Roundtime)!=null)                 
                           {
                               if(FromTime1.length !=0)
                               //if(document.getElementById(FromTime).value.length !=0)
                               {
                               FromTime1  =FromTime1 +','+document.getElementById(FromTime).value;
                               }
                               else
                               {
                               FromTime1=document.getElementById(FromTime).value;
                               }
                               if(Totime1.length !=0)
                               //if(document.getElementById(FromTime).value.length !=0)
                               {
                               Totime1 =Totime1 + ','+document.getElementById(Totime).value;
                               }
                               else
                               {
                               Totime1=document.getElementById(Totime).value;
                               }
                               if(Roundtime1.length !=0)
                               //if(document.getElementById(FromTime).value.length !=0)
                               {
                               Roundtime1=Roundtime1 + ','+document.getElementById(Roundtime).value;
                               }
                               else
                               {
                               Roundtime1=document.getElementById(Roundtime).value;
                               }
                              p=p+1;
                           }
                 }
             }
            }
            i=i+1;
            document.getElementById("ctl00_BodyContentPH_hdf_controlID").value=i; 
            var k=Math.floor(Math.random()*111);           
            if(window.ActiveXObject)
            {
                objOT.onreadystatechange=Process_Response_OTRoundoff;
                objOT.open("GET","OTRoundoff_Remote.aspx?controlID=" + i+"&Random="+k,true);
                objOT.send(null); 
            } 
            else if(window.XMLHttpRequest)
            {
                objOT.open("GET","OTRoundoff_Remote.aspx?controlID=" + i+"&Random="+k,false);
                objOT.send(null); 
                objOT.onreadystatechange=Process_Response_OTRoundoff();
            }
            Fromarray=FromTime1.split(",");
            Toarray=Totime1.split(",");
            Roundarray=Roundtime1.split(",");
                var b=0;
                if (p >0)
                {
                    var j=0;
                     for(j=1;j<=rowCount;j++)
                      {
                             FromTime = 'txt_From'+j;   
                             Totime = 'txt_To'+j;
                             Roundtime = 'txt_RoundedAs'+j;
                             if(document.getElementById(FromTime)!=null && document.getElementById(Totime)!=null && document.getElementById(Roundtime)!=null)                 
                              {
                             if(Fromarray.length >0)
                             {
                             document.getElementById(FromTime).value=Fromarray[b];
                             }
                             if(Toarray.length >0)
                             {
                             document.getElementById(Totime).value=Toarray[b];
                             }
                             if(Roundarray.length >0)
                             {
                             document.getElementById(Roundtime).value=Roundarray[b];
                             }
                             b=b+1;
                          }
                      }
                }
        }
        return false;
    }
    function Process_Response_OTRoundoff(NoDetails)
	{
		if(objOT.readyState == 4)
		{
			if(objOT.status == 200)
			{
				var retval=objOT.responseText;	
				if (retval.length > 0)
				{
				   document.getElementById('ctl00_BodyContentPH_divOTRoundoff').innerHTML+=retval;
				}
				else
				{
				    //alert("No Details found!");
				    alert(NoDetails);
				}
			}
		}
	}
	function RoundoffValidationnnn(ctrl, ctrlId)
	{
////	   if (RoundoffValidationNext(ctrl, ctrlId)==false)
////	   {
////	       return false;
////	   }
//        ctrl1 = document.getElementById('txt_From'+ctrlId).value;
//	if(ctrl1=='')
//      {
//        alert('Enter Start Time. The Range is 00:01 to 00:58');
//        document.getElementById('txt_From'+ctrlId).focus();  
//        return false;
//      }
        maxValue = document.getElementById('ctl00_BodyContentPH_hdf_controlID').value;      
        if(maxValue>0)
          {
            assign_hidden_otroundoffAdd();
          }
        if (CheckFirstControl(ctrl,ctrlId))
        {
        }
	}
	function CheckFirstControl(ctrl,ctrlId)
	{
	      var ctrl1,ctrl2,ctrl3,maxValue;      
          ctrl1 = document.getElementById('txt_From'+ctrlId).value;
          ctrl2 = document.getElementById('txt_To'+ctrlId).value;     
          ctrl3 = document.getElementById('txt_RoundedAs'+ctrlId).value;     
          maxValue = document.getElementById('ctl00_BodyContentPH_hdf_controlID').value;      
          var hrsvalue1,hrs1,mins1,minsvalue1;
          var hrsvalue2,hrs2,mins2,minsvalue2;
          var hrsvalue3,hrs3,mins3,minsvalue3;
	        ctrl1 = document.getElementById('txt_From'+ctrlId).value;
	        if(ctrl1=='')
              {
                alert('Enter Start Time. The Range is 00:01 to 00:58');
                document.getElementById('txt_From'+ctrlId).focus();  
                return false;    
                //valid = false;
              }
              else
              {
                 hrsvalue1=ctrl1;
                 hrs1 = hrsvalue1.substring(0,2);    
                 mins1 = hrsvalue1.substring(3,5);
                 minsvalue1 = parseFloat(hrs1)*60;    
                 minsvalue1 = parseFloat(minsvalue1) + parseFloat(mins1);
                 if(minsvalue1==0 || minsvalue1>58)
                 {
                     document.getElementById('txt_From'+ctrlId).focus();
                     alert('Enter Start Time. The Range is 00:01 to 00:58');             
                     return false;
                 } 
                     if(maxValue>1)
                     {
                        var i;
                        i = ctrlId - 1;
                        var to = "txt_To" + i;
                        var currToTime = "txt_To" + ctrlId; 
                        if(document.getElementById(to)!=null)
                         {
                            to = document.getElementById(to).value; 
                        var to2=to;
                        var tohrs2 = to2.substring(0,2);    
                        var tomins2 = to2.substring(3,5);
                        var tominsvalue2 = parseFloat(tohrs2)*60;    
                        tominsvalue2 = parseFloat(tominsvalue2) + parseFloat(tomins2);
                        var prevEndMins = tominsvalue2;                                               
                        var currentStartMins = minsvalue1 - 1;               
                        if(prevEndMins!=currentStartMins) 
                                  {
                                          alert('Given Start Time should be greater than one minute of previous End Time. Please verify')
                                          document.getElementById('txt_From'+ctrlId).focus();
                                          return false;
                                  } 
                         var currEndMins;         
                         if(document.getElementById(currToTime)!=null)
                         {
                            currEndMins = document.getElementById(currToTime).value; 
                         }
                         if(currEndMins!='')
                         {
                            if(currentStartMins>=currEndMins) 
                                  {
                                          alert('Given Start Time should be lesser than current End Time. Please verify')
                                          document.getElementById('txt_From'+ctrlId).focus();
                                          return false;
                                  } 
                         }                                
                        }                                      
                     }
              }  
	}
function RoundoffValidation(ctrl, ctrlId,EnterStartTimeTheRangeis,EnterStartTimeTheRangeiss,GivenStartTimeshoul,GivenStartTime , greaterthanStartTime,EntervalidEndTime, AlertStartTimeandmaximumof,AlertaterthanStartTime,AlertxtStartTimePleaseverify, AlertvenEndTimeshou,AlertEnterRoundedAsTheRangeis,Round,AlertDeleteonebyonefromlastdata)
{  
  var reExHoursMinitues=/^([0-1][0-9]|[2][0-3]):([0-5][0-9])$/;        
  var ctrl1,ctrl2,ctrl3,maxValue;      
  ctrl1 = document.getElementById('txt_From'+ctrlId).value;
  ctrl2 = document.getElementById('txt_To'+ctrlId).value;     
  ctrl3 = document.getElementById('txt_RoundedAs'+ctrlId).value;     
  maxValue = document.getElementById('ctl00_BodyContentPH_hdf_controlID').value;      
  var hrsvalue1,hrs1,mins1,minsvalue1;
  var hrsvalue2,hrs2,mins2,minsvalue2;
  var hrsvalue3,hrs3,mins3,minsvalue3;
  if(maxValue>0)
  {
    assign_hidden_otroundoffAdd();
  }
//     if(ctrl1=='')
//      {
//        //alert('Enter Start Time. The Range is 00:01 to 00:58');
//        alert(EnterStartTimeTheRangeis);
//        document.getElementById('txt_From'+ctrlId).focus();  
//        return false;            
//      }
      if(ctrl1 !='')
      {
         if (document.getElementById('txt_From'+ctrlId).value.search(reExHoursMinitues)==-1)   
         {          
          if (document.activeElement.name != 'txt_From'+ctrlId)
            {
              document.getElementById('txt_From'+ctrlId).focus();
             // alert("Enter valid Start Time (hh:mm)");  
             alert(EnterStartTimeTheRangeiss);             
            } 
            document.getElementById('txt_From'+ctrlId).focus();
            return false;
         }
         hrsvalue1=ctrl1;
         hrs1 = hrsvalue1.substring(0,2);    
         mins1 = hrsvalue1.substring(3,5);
         minsvalue1 = parseFloat(hrs1)*60;    
         minsvalue1 = parseFloat(minsvalue1) + parseFloat(mins1);
         if(minsvalue1==0 || minsvalue1>58)
         {
             if (document.activeElement.name != 'txt_From'+ctrlId)
            { 
                 document.getElementById('txt_From'+ctrlId).focus();
                 //alert('Enter Start Time. The Range is 00:01 to 00:58'); 
                 alert(EnterStartTimeTheRangeis);                            
             }
             document.getElementById('txt_From'+ctrlId).focus();
             return false; 
         } 
             if(maxValue>1)
             {
                var i;
                i = ctrlId - 1;
                var to = "txt_To" + i;
                var currToTime = "txt_To" + ctrlId; 
                if(document.getElementById(to)!=null)
                 {
                    to = document.getElementById(to).value; 
                var to2=to;
                var tohrs2 = to2.substring(0,2);    
                var tomins2 = to2.substring(3,5);
                var tominsvalue2 = parseFloat(tohrs2)*60;    
                tominsvalue2 = parseFloat(tominsvalue2) + parseFloat(tomins2);
                var prevEndMins = tominsvalue2;                                               
                var currentStartMins = minsvalue1 - 1;               
                if(prevEndMins!=currentStartMins) 
                          {
                                  if (document.activeElement.name != 'txt_From'+ctrlId)
                                  {
                                     // alert('Given Start Time should be greater than one minute of previous End Time. Please verify')
                                     alert(GivenStartTimeshoul);
                                      document.getElementById('txt_From'+ctrlId).focus();                                      
                                  }
                                  document.getElementById('txt_From'+ctrlId).focus();
                                  return false;
                          } 
                 var currEndMins;         
                 if(document.getElementById(currToTime)!=null)
                 {
                    currEndMins = document.getElementById(currToTime).value; 
                 }
                 if(currEndMins!='')
                 {
                    if(currentStartMins>=currEndMins) 
                          {
                                  if (document.activeElement.name != 'txt_From'+ctrlId)
                                  {
                                      //alert('Given Start Time should be lesser than current End Time. Please verify')
                                      alert(GivenStartTime);
                                      document.getElementById('txt_From'+ctrlId).focus();                                  
                                  }
                                  document.getElementById('txt_From'+ctrlId).focus();
                                  return false;
                          } 
                 }                                
                }                                      
             }
      }  
//      if(ctrl2=='')
//      {    
//            if (document.activeElement.name != 'txt_To'+ctrlId)
//            {                        
//               // alert('Enter End Time, which should be greater than Start Time.');
//               alert(greaterthanStartTime);
//                document.getElementById('txt_To'+ctrlId).focus();                    
//            }
//            document.getElementById('txt_To'+ctrlId).focus();  
//            return false;         
//      }  
      if(ctrl2 !='')
      {
        if (document.getElementById('txt_To'+ctrlId).value.search(reExHoursMinitues)==-1)   
         {    if (document.activeElement.name != 'txt_To'+ctrlId)
                 {      
                  //alert("Enter valid End Time (hh:mm)");
                  alert(EntervalidEndTime);
                  document.getElementById('txt_To'+ctrlId).focus(); 
                 } 
                 document.getElementById('txt_To'+ctrlId).focus();  
                 return false;
         }
        hrsvalue2=ctrl2;
        hrs2 = hrsvalue2.substring(0,2);    
        mins2 = hrsvalue2.substring(3,5);
        minsvalue2 = parseFloat(hrs2)*60;    
        minsvalue2 = parseFloat(minsvalue2) + parseFloat(mins2);
            if(minsvalue2<= minsvalue1 || minsvalue2>59)
             {
                 if (document.activeElement.name != 'txt_To'+ctrlId)
                 {
                     //alert('Enter End Time, which should be greater than Start Time and maximum of 00:59.');
                     alert(AlertStartTimeandmaximumof);
                     document.getElementById('txt_To'+ctrlId).focus();                     
                 }
                 document.getElementById('txt_To'+ctrlId).focus();  
                 return false;   
             }
             if(minsvalue2<minsvalue1)
             {
                  if (document.activeElement.name != 'txt_To'+ctrlId)
                  {
                      //alert('End Time should be greater than Start Time');
                      alert(AlertaterthanStartTime);
                      document.getElementById('txt_To'+ctrlId).focus();                         
                  }
                  document.getElementById('txt_To'+ctrlId).focus();  
                  return false; 
             }
             if(maxValue>1)
             {
                var i;
                i = ctrlId + 1;
                var from = "txt_From" + i;
                var currStartTime = "txt_From" + ctrlId; 
                if(document.getElementById(from)!=null)
                 {
                        from = document.getElementById(from).value;
                        if(from!='')
                        {
                         var from2=from;
                    var fromhrs2 = from2.substring(0,2);    
                    var frommins2 = from2.substring(3,5);
                    var fromminsvalue2 = parseFloat(fromhrs2)*60;    
                    fromminsvalue2 = parseFloat(fromminsvalue2) + parseFloat(frommins2);
                    var nextStartMins = fromminsvalue2;
                    var currentEndMins = minsvalue2 + 1; 
                    if(nextStartMins!=currentEndMins) 
                              {
                                      if (document.activeElement.name != 'txt_To'+ctrlId)
                                      {
                                          //alert('Given End Time should be lesser than one minute of next Start Time. Please verify')
                                          alert(AlertxtStartTimePleaseverify);
                                          document.getElementById('txt_To'+ctrlId).focus();                                         
                                      }
                                      document.getElementById('txt_To'+ctrlId).focus();  
                                      return false;
                              } 
                     var currStartMins;         
                     if(document.getElementById(currStartTime)!=null)
                     {
                        currStartMins = document.getElementById(currStartTime).value; 
                     }
                     if(currStartMins!='')
                     {
                        if(currentEndMins<=currStartMins) 
                              {
                                     if (document.activeElement.name != 'txt_To'+ctrlId)
                                      {
                                      //alert('Given End Time should be greater than current Start Time. Please verify')
                                      alert(AlertvenEndTimeshou);
                                      document.getElementById('txt_To'+ctrlId).focus();                                     
                                      }
                                      document.getElementById('txt_To'+ctrlId).focus();  
                                      return false;                                      
                              } 
                     }  
                 }
                }                                                                 
           }  
      }
//      if(ctrl3=='')
//      {
//        if (document.activeElement.name != 'txt_RoundedAs'+ctrlId)
//            {
//               // alert('Enter RoundedAs , The Range is 00:00 to 01:00');
//               
//               alert(AlertEnterRoundedAsTheRangeis);
//                document.getElementById('txt_RoundedAs'+ctrlId).focus(); 
//                return false;     
//           }
//           document.getElementById('txt_RoundedAs'+ctrlId).focus(); 
//           return false;  
//      }
      if(ctrl3 !='')
      {
      if (document.getElementById('txt_RoundedAs'+ctrlId).value.search(reExHoursMinitues)==-1)   
         {                  
          // alert("Enter valid RoundedAs (hh:mm)");
          alert(Round);
           document.getElementById('txt_RoundedAs'+ctrlId).focus(); 
           return false;                                  
         }
        hrsvalue3=ctrl3;
        hrs3 = hrsvalue3.substring(0,2);    
        mins3 = hrsvalue3.substring(3,5);
        minsvalue3 = parseFloat(hrs3)*60;    
        minsvalue3 = parseFloat(minsvalue3) + parseFloat(mins3);
            if( minsvalue3>60)
             {
                 //alert('Enter RoundedAs , The Range is 00:00 to 01:00');
                 alert(AlertDeleteonebyonefromlastdata);
                 document.getElementById('txt_RoundedAs'+ctrlId).focus(); 
                 return false;
             }  
      }                                                   
}
function removeRowOT(Id, con,RemoveOnebyOne)
{
    var maxValue = document.getElementById('ctl00_BodyContentPH_hdf_controlID').value; 
    if(maxValue==Id)
     {
//            var cel = con.parentElement;
//            var row = cel.parentElement;
//            var tbl = row.parentElement;
            var valused='tbl_LeavePolicy'+Id.toString(); 
            var div = document.getElementById(valused);
            if (div) {
                div.parentNode.removeChild(div);
                document.getElementById('ctl00_BodyContentPH_hdf_OTRoundoff').value = str;
                document.getElementById('ctl00_BodyContentPH_hdf_controlID').value = maxValue - 1;
                assign_hidden_otroundoffAdd();
            }
            
            var cel = con.parentNode;
            var row = cel.parentNode;
            var tbl = row.parentNode;
            var hid = '';
            tbl.deleteRow(row.rowIndex);
             var str = "";
             str = document.getElementById('ctl00_BodyContentPH_hdf_OTRoundoff').value;
            //The below if condition add by karthikeyan for the bootstrap auto generate control
             if(Id==1)
             {
                if (document.getElementById('tbl_header')) {
                document.getElementById('tbl_header').remove();
                }
             }
            //End
             var arr_List = new Array();
             arr_List = str.split("*");
             for(var i=0; i < arr_List.length; i++)
             {    
                var p =  arr_List[i].lastIndexOf(',');     
                var len=arr_List[i].length;     
                var id=arr_List[i].substring(p+1,len);     
                var intId=parseInt(id);
                if(Id == intId)
                {
                    arr_List[i] = null;
                    break;
                }
             }    
             str = "";
             for(var i=0; i < arr_List.length; i++)
             {
                if(arr_List[i] != null && arr_List[i] != "0")
                {
                    if(str == "")
                    {
                        str = arr_List[i];
                    }
                    else
                    {
                        str = str + "*" + arr_List[i];
                    }
                }
             }
            document.getElementById('ctl00_BodyContentPH_hdf_OTRoundoff').value = str;
            document.getElementById('ctl00_BodyContentPH_hdf_controlID').value = maxValue-1;   
            assign_hidden_otroundoffAdd();
   }
   else
   {
   // alert('Delete one by one from last data');
   alert(RemoveOnebyOne);
   }         
}
//assign the policy details to hidden field
	function assign_hidden_otroundoff()
    {
        var no_of_rows=document.getElementById("ctl00_BodyContentPH_hdf_controlID").value;
        var data="";
        for(var i=1; i<= no_of_rows; i++)
            {
                var from = "txt_From" + i;
                var to = "txt_To" + i;
                var id = "div_" + i;
                if(data == "")
                {
                if(document.getElementById(from)!=null && document.getElementById(to)!=null && document.getElementById(id)!=null)
                    {
                    data = document.getElementById(from).value + "," + document.getElementById(to).value + "," + document.getElementById(id).value;
                    }
                }
                else
                {
                if(document.getElementById(from)!=null && document.getElementById(to)!=null && document.getElementById(id)!=null)
                    {
                    data = data + "*" + document.getElementById(from).value + "," + document.getElementById(to).value + "," + document.getElementById(id).value;
                    }
                }
            }
            document.getElementById("ctl00_BodyContentPH_hdf_Policy").value=data;
    } 
function assign_hidden_otroundoffAdd()
    {
        var no_of_rows=parseInt(document.getElementById("ctl00_BodyContentPH_hdf_controlID").value) + 1;
        var data="";
        for(var i=1; i<= no_of_rows; i++)
            {
                var from = "txt_From" + i;
                var to = "txt_To" + i;
                var roundedAs = "txt_RoundedAs" + i;
//                var id = "div_" + i;
                if(data == "")
                {
                if(document.getElementById(from)!=null && document.getElementById(to)!=null && document.getElementById(roundedAs)!=null)
                    {
                    data = document.getElementById(from).value + "," + document.getElementById(to).value + "," + document.getElementById(roundedAs).value;
                    }
                }
                else
                {
                if(document.getElementById(from)!=null && document.getElementById(to)!=null && document.getElementById(roundedAs)!=null)
                    {
                    data = data + "*" + document.getElementById(from).value + "," + document.getElementById(to).value + "," + document.getElementById(roundedAs).value;
                    }
                }
            }           
          document.getElementById("ctl00_BodyContentPH_hdf_OTRoundoff").value=data;
    } 
