

function LoadData(DB) {
    //var DB = "da1";
    var ViewBy = "0"; //0=Standard,1=Exceed-Time
    var StartDate = document.getElementById(txtStartDate).value;
    var EndDate = document.getElementById(txtEndDate).value;    
    document.getElementById('lblReportSearchResult').innerHTML = "Search Result - from " + StartDate + " to " + EndDate;
    jQuery.ajax({
        cache: false,
        type: "POST",
        url: "TimedurationReport.aspx/LoadData",
        data: "{'DB':'" + DB + "','StartDate':'" + StartDate + "','EndDate':'" + EndDate + "'}",
        async: false,
        contentType: "application/json;charset=utf8",
        error: function () {
            console.log("Error Loading DATA");
        }, success: function (data) {
                
            var jsonResult = JSON.parse(data.d);
            var jsonResultCount = jsonResult.length;
            var Table = document.getElementById("tblBodyData");

            $("#tblBodyDataExceed tr").remove();
            for (var i = 0; i < jsonResultCount; i++) {
                var TransID = jsonResult[i].TransID;
                var RefNo = jsonResult[i].RefNo;
                var TransDate = jsonResult[i].TransDate;
                var TransTime = jsonResult[i].TransTime;
                var CompletedDate = jsonResult[i].CompletedDate;
                var CompletedTime = jsonResult[i].CompletedTime;
                var Duration = jsonResult[i].Duration;

                var row = Table.insertRow(i);

                var Min = "00:" + GetTimeDuration(DB) + ":00";

                var cell1 = row.insertCell(0);
                var element1 = document.createElement("label")
                element1.innerHTML = TransID;                
                cell1.appendChild(element1);

                var cell2 = row.insertCell(1);
                var element2 = document.createElement("label");
                element2.innerHTML = RefNo;
                cell2.appendChild(element2);

                var cell3 = row.insertCell(2);
                var element3 = document.createElement("label");
                element3.innerHTML = TransDate.substring(0, 10);
                cell3.appendChild(element3);

                var cell4 = row.insertCell(3);
                var element4 = document.createElement("label");
                element4.innerHTML = TransTime;
                cell4.appendChild(element4);

                var cell5 = row.insertCell(4);
                var element5 = document.createElement("label");                
                if (CompletedDate != null) {
                    element5.innerHTML = CompletedDate.substring(0, 10);
                } else {
                    element5.innerHTML = CompletedDate;
                }                
                cell5.appendChild(element5);

                var cell6 = row.insertCell(5);
                var element6 = document.createElement("label");
                element6.innerHTML = CompletedTime;
                cell6.appendChild(element6);

                var cell7 = row.insertCell(6);
                var element7 = document.createElement("label");
                element7.innerHTML = Duration;

                cell7.appendChild(element7)

                if (Duration > Min) {
                    cell1.style = "background-color:#FFC300;";
                    cell2.style = "background-color:#FFC300;";
                    cell3.style = "background-color:#FFC300;";
                    cell4.style = "background-color:#FFC300;";
                    cell5.style = "background-color:#FFC300;";
                    cell6.style = "background-color:#FFC300;";
                    cell7.style = "background-color:#FFC300;";
                }
            }
        }
    });
    document.getElementById("tblFooterCol1").innerHTML = "Summary from " + StartDate + " to " + EndDate;

}

function LoadDataExceed(DB,TimeDuration) {
    //var DB = "da1";
    var ViewBy = "0"; //0=Standard,1=Exceed-Time
    var StartDate = document.getElementById(txtStartDate).value;
    var EndDate = document.getElementById(txtEndDate).value;

    document.getElementById('lblReportSearchResult').innerHTML = "Search Result - from " + StartDate + " to " + EndDate;

    $("#tblBodyDataExceed tr").remove();
    jQuery.ajax({
        cache: false,
        type: "POST",
        url: "TimedurationReport.aspx/LoadDataExceed",
        data: "{'DB':'" + DB + "','StartDate':'" + StartDate + "','EndDate':'" + EndDate + "','TimeDuration':'" + TimeDuration + "'}",
        async: false,
        contentType: "application/json;charset=utf8",
        error: function () {
            console.log("Error Loading LoadDataExceed function");
        }, success: function (data) {
            //console.log(data.d)
            var jsonResult = JSON.parse(data.d);
            var jsonResultCount = jsonResult.length;
            var Table = document.getElementById("tblBodyDataExceed");


            for (var i = 0; i < jsonResultCount; i++) {
                var TransID = jsonResult[i].TransID;
                var RefNo = jsonResult[i].RefNo;
                var TransDate = jsonResult[i].TransDate;
                var TransTime = jsonResult[i].TransTime;
                var CompletedDate = jsonResult[i].CompletedDate;
                var CompletedTime = jsonResult[i].CompletedTime;
                var Duration = jsonResult[i].Duration;
                var row = Table.insertRow(i);
                var Min = "00:15:00";


                var cell1 = row.insertCell(0);
                var element1 = document.createElement("label")
                element1.innerHTML = TransID;
                cell1.appendChild(element1);

                var cell2 = row.insertCell(1);
                var element2 = document.createElement("label");
                element2.innerHTML = RefNo;
                cell2.appendChild(element2);

                var cell3 = row.insertCell(2);
                var element3 = document.createElement("label");
                element3.innerHTML = TransDate.substring(0, 10);
                cell3.appendChild(element3);

                var cell4 = row.insertCell(3);
                var element4 = document.createElement("label");
                element4.innerHTML = TransTime;
                cell4.appendChild(element4);

                var cell5 = row.insertCell(4);
                var element5 = document.createElement("label");
                element5.innerHTML = CompletedDate.substring(0, 10);
                cell5.appendChild(element5);

                var cell6 = row.insertCell(5);
                var element6 = document.createElement("label");
                element6.innerHTML = CompletedTime;
                cell6.appendChild(element6);

                var cell7 = row.insertCell(6);
                var element7 = document.createElement("label");
                element7.innerHTML = Duration;

                cell7.appendChild(element7)
            }
        }

    });
    document.getElementById("tblFooterExceedCol1").innerHTML = "Summary from " + StartDate + " to " + EndDate;
}

function LoadDataFooter(DB) {
    //var DB = "da1";
    var StartDate = document.getElementById(txtStartDate).value;
    var EndDate = document.getElementById(txtEndDate).value;
    var ReturnVal;
    jQuery.ajax({
        cache: false,
        type: "POST",
        url: "TimedurationReport.aspx/LoadDataFooter",
        data: "{'DB':'" + DB + "','StartDate':'" + StartDate + "','EndDate':'" + EndDate + "'}",
        async: false,
        contentType: "application/json;charset=utf8",
        error: function () {
            console.log("Error on Loading LoadDataFooter()");
        }, success: function (data) {
            var jsonResult = JSON.parse(data.d);
            var jsonResultCount = jsonResult.length;
            for (var i = 0; i < jsonResultCount; i++) {
                var TotalTransaction = jsonResult[i].TotalTransaction;
                var TotalDuration = jsonResult[i].TotalDuration;                
                document.getElementById("tblFooterCol2").innerHTML = TotalTransaction + " transaction";
                document.getElementById("tblFooterCol3").innerHTML = TotalDuration;
            }

            if (jsonResultCount == 0) {
                document.getElementById("tblFooterCol2").innerHTML = "0 transaction";
                document.getElementById("tblFooterCol3").innerHTML = "";
            }

        }
    });

}

function LoadDataExceedFooter(DB, TimeDuration) {    
    //var DB = "da1";
    var StartDate = document.getElementById(txtStartDate).value;
    var EndDate = document.getElementById(txtEndDate).value;
    var ReturnVal;
    jQuery.ajax({
        cache: false,
        type: "POST",
        url: "TimedurationReport.aspx/LoadDataExceedFooter",
        data: "{'DB':'" + DB + "','StartDate':'" + StartDate + "','EndDate':'" + EndDate + "','TimeDuration':'" + TimeDuration + "'}",
        async: false,
        contentType: "application/json;charset=utf8",
        error: function () {
            console.log("Error on Loading LoadDataFooter()");
        }, success: function (data) {
            var jsonResult = JSON.parse(data.d);
            var jsonResultCount = jsonResult.length;
            for (var i = 0; i < jsonResultCount; i++) {
                var TotalTransaction = jsonResult[i].TotalTransaction;
                var TotalDuration = jsonResult[i].TotalDuration;
                document.getElementById("tblFooterExceedCol2").innerHTML = TotalTransaction + " transaction";
                document.getElementById("tblFooterExceedCol3").innerHTML = TotalDuration;
            }

            if (jsonResultCount == 0) {
                document.getElementById("tblFooterExceedCol2").innerHTML = "0 transaction";
                document.getElementById("tblFooterExceedCol3").innerHTML = "";
            }

        }
    });

}



function GetTimeDuration(DB) {
    //var DB = "da1";
    var RetValue;
    jQuery.ajax({
        cache: false,
        type: "POST",
        url: "TimedurationReport.aspx/GetTimeDuration",
        data: "{'DB':'" + DB + "'}",
        async: false,
        contentType: "application/json;charset=utf8",
        error: function () {
            console.log("Error on GetTimeDuration()");
        }, success: function (data) {
            var json = JSON.parse(data.d);
            RetValue = json[0].Value;

        }
    });
    return RetValue;
}

function GetDB(SelectedBranch, BranchHQ, UserID) {
    var RetValue;
    jQuery.ajax({
        cache: false,
        type: "POST",
        url: "TimedurationReport.aspx/GetDB",
        data: "{'SelectedBranch':'" + SelectedBranch + "','BranchHQ':'" + BranchHQ + "','UserID':'" + UserID + "'}",
        async: false,
        contentType: "application/json;charset=utf8",
        error: function () {
            console.log("Error on GetDB()");
        }, success: function (data) {
            // var json = JSON.parse(data.d);
            RetValue = data.d;
            //RetValue = json[0].Value;

        }
    });
    return RetValue;
}


function DeleteRow() {
    $("#tblBodyData tr").remove();
    $("#tblBodyDataExceed tr").remove();
};


