// JScript File
var objOT = null;
function ajaxLateDeduction(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;
            }
        }
    }
    if(objOT!=null)
    {                              
        var i;                
        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(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)
                        {
                            FromTime1  =FromTime1 +','+document.getElementById(FromTime).value;
                        }
                        else
                        {
                            FromTime1=document.getElementById(FromTime).value;
                        }
                        if(Totime1.length !=0)
                        {
                            Totime1 =Totime1 + ','+document.getElementById(Totime).value;
                        }
                        else
                        {
                            Totime1=document.getElementById(Totime).value;
                        }
                        if(Roundtime1.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","ConfigurationControlsCreation.aspx?controlID=" + i+"&Random="+k,true);
            objOT.send(null); 
        } 
        else if(window.XMLHttpRequest)
        {
            objOT.open("GET","ConfigurationControlsCreation.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_ContentPlaceHolder1_divOTRoundoff').innerHTML+=retval;
            }
			else
			{
                alert(NoDetails);
            }
        }
    }
}
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 !='')
    {
        if (document.getElementById('txt_From'+ctrlId).value.search(reExHoursMinitues)==-1)   
        {          
            if (document.activeElement.name != 'txt_From'+ctrlId)
            {
                document.getElementById('txt_From'+ctrlId).focus();
                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(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(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(GivenStartTime);
                            document.getElementById('txt_From'+ctrlId).focus();                                  
                        }
                        document.getElementById('txt_From'+ctrlId).focus();
                        return false;
                    }                  
                }                                
            }                                      
        }
    }  
    if(ctrl2 !='')
    {
        if (document.getElementById('txt_To'+ctrlId).value.search(reExHoursMinitues)==-1)   
        {    
            if (document.activeElement.name != 'txt_To'+ctrlId)
            {      
                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(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(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(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(AlertvenEndTimeshou);
                                document.getElementById('txt_To'+ctrlId).focus();                                     
                            }
                            document.getElementById('txt_To'+ctrlId).focus();  
                            return false;                                      
                        }                      
                     }  
                 }
            }                                                                 
        }  
    }
    if(ctrl3 !='')
    {
        if (document.getElementById('txt_RoundedAs'+ctrlId).value.search(reExHoursMinitues)==-1)   
        {                  
            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(AlertDeleteonebyonefromlastdata);
            document.getElementById('txt_RoundedAs'+ctrlId).focus(); 
            return false;
        }  
    }                                                   
}
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;
        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_controlID").value=data;
}