var PageIndex = 1;
function BindRegisterList() {
    $('[id$=MainUpdateProgress]').show();
    $.ajax({
        type: "POST",
        url: "/Pages/Attendance/DivisionRegister.aspx/BindRegisterList",
        data: '{"cid":"' + $('[id$=ClassDropDownList]').val() + '","did":"' + $('[id$=DivisionDropDownList]').val() + '","aid":"' + $('[id$=AttendanceTypeDropDownList]').val() + '","fm":"' + $('[id$=ForMonthDropDownList]').val() + '","fy":"' + $('[id$=ForYearDropDownList]').val() + '","pi":"' + PageIndex + '"}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            var datas = $.parseJSON(data.d);
           
            if (datas.Grid != '') {
                $('#tblList').html('');
                $('#trHeader').html('');
                $('#Div1').show();
                $('#trHeader').html(datas.Header);
                $('#tblList').html(datas.Grid);
                $('#PresentPercentage').html(datas.present_Per);
                $('#TotalCountLabel').html(datas.Total_Count);
                $('#MotherPresentLabel').html(datas.Present);
                $('#FatherPresentLabel').html(datas.Absent_Count);
                $('#LateCountLabel').html(datas.Late_Present);
                $('#EarlyCountLabel').html(datas.Early_Present);

                window.setTimeout(function () { $('[id$=MainUpdateProgress]').hide(); }, 300);
            }
            else {
                $('#Div1').show();
                $('#trHeader').html("<tr><td colspan='37'>No Records found !</td></tr>");
                $('#tblList').html("");
                $('[id$=MainUpdateProgress]').hide();
            }
        },
        error: function (xhr, status, error, data) {
            $('[id$=MainUpdateProgress]').hide();
            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });
}
function ChangePage(pi) {
    PageIndex = pi;
    BindRegisterList();
}
function Test() {
    $('#tblList').find('tr').each(function () {
        $(this).find('td').each(function () {
            if ($(this).html() == '<img src="../../Images/Yes.png">') {
                alert(1);
            }
        });
    });
}
function Sum() {
    var LabelItem1 = document.getElementById("Item1");
    var LabelItem2 = document.getElementById("Item2");
    var LabelItem3 = document.getElementById("Item3");
    var Result = parseInt(LabelItem1) + parseInt(LabelItem2);
    if (!isNaN(Result)) {
        LabelItem3 = parseInt(Result);
    }
}
function test() {
    var grid = $("[id$=DivisionRegisterGridView]");
    $(this).find('tr').each(function () {
        $(this).find('td').each(function () {
            var Item1 = $('[Id$=Item1]').find();
            var Item2 = $('[Id$=Item2]').find();
            var result = parseInt(Item1) + parseInt(Item2);
            var Item3 = $('[Id$=Item3').find();
            Item3 = parseInt(result);
        });
    });
}
function fnValidateMonthlyRegister() {
    var himsValidator = [new hims_ctl_val("ctl00_CP_ClassDropDownList", "Class", "T", "", "", "", 20),
		new hims_ctl_val("ctl00_CP_DivisionDropDownList", "Division", "T", "", "", "", 20),
		new hims_ctl_val("ctl00_CP_AttendanceTypeDropDownList", "Attendance Type", "T", "", "", "", 20),
		new hims_ctl_val("ctl00_CP_ForYearDropDownList", "For Year", "T", "", "", "", 20),
		new hims_ctl_val("ctl00_CP_ForMonthDropDownList", "For Month", "T", "", "", "", 20)];
    if (fnDoValidate(himsValidator))
        return true;
    else
        return false;
}
function checkitem(checkBox, textBox1) {
    var e = document.getElementById(textBox1.id);
    var f = document.getElementById(checkBox.id);
    if (f.checked == true) {
        e.disabled = true;
    }
    else {
        e.disabled = false;
    }
}
function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}
//function cpageLoad() {
//    var browser = navigator.userAgent.toLowerCase();
//    if (browser.indexOf('firefox') >= 0) {
//        $("#ctl00_CP_FileUpload1").fileUpload({
//            'uploader': '../../../UserControls/MultiFileUpload/uploader.swf',
//            'cancelImg': '../../../Images/cancel.png',
//            'script': '../../../UserControls/MultiFileUpload/Upload.ashx?ASPSESSID=' + $('#ctl00_CP_hdnSessionid').val(),
//            'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
//            'multi': false,
//            'auto': true
//        });
//    }
//    else {
//        $("#ctl00_CP_FileUpload1").fileUpload({
//            'uploader': '../../../UserControls/MultiFileUpload/uploader.swf',
//            'cancelImg': '../../../Images/cancel.png',
//            'script': '../../../UserControls/MultiFileUpload/Upload.ashx',
//            'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
//            'multi': false,
//            'auto': true
//        });
//    }
//}


var reader = new FileReader();
reader.onload = function (e) {
}
function readURL(input) {
    if (input.files && input.files[0]) {
        reader.readAsDataURL(input.files[0]);
        ajaxFileUpload();
    }
}
function OpenFileUpload() {
    $('#flUpload').click();
}
function LoadingMessage(msg1, msg2) {
    $('#dvTestLoadingmsg').show();
    $('#spn1msg').html(msg1);
    $('#spn2msg').html(msg2);
    $('.hls-bg').show();
}
function CloseLoadingpopup() {
    $('#dvTestLoadingmsg').hide();
    $('.hls-bg').hide();
}
function ajaxFileUpload() {
    if (ValidateImageDetails()) {
        LoadingMessage('Uploading', '<b>Please Wait</b>');
        $("#loading").ajaxStart(function () {
            $(this).show();
        }).ajaxComplete(function () {
            $(this).hide();
        });
        $.ajaxFileUpload({
            url: '/UserControls/MultiFileUpload/common-file-upload.ashx',
            secureuri: false,
            fileElementId: 'flUpload',
            dataType: 'text',
            contentType: "application/json; charset=utf-8",
            data: { name: 'logan', id: 'id', hdn: '1' },
            success: function (response, status) {
                /*var str = response;
				str = str.replace('<pre style="word-wrap: break-word; white-space: pre-wrap;">','');
				str = str.replace('</pre>','');				
				$('#hdnFileName').val(str);*/
                window.setTimeout(function () {
                    CloseLoadingpopup();
                    $("#ctl00_CP_Label10").text("Uploaded Successfully");
                }, 100);
            },
            error: function (response, status, e) {
                window.setTimeout(function () {
                    CloseLoadingpopup();
                }, 100);
            }
        })
        return false;
    }
}
function isFile(file) {
    var video = /\.(xls|)$/i;
    var CSV = /\.(csv|)$/i;
    var xlsx = /\.(xlsx|)$/i;
    if (video.test(file.toLowerCase()))
        return true;
    else
        return false;
}
function ValidateImageDetails() {
    var fields;
    fields = "";
    var file = document.getElementById('flUpload').value;
    if (isFile(document.getElementById('flUpload').value) == false || document.getElementById('flUpload').value == null) {
        fields = fields + "\n-- Invalid file --";
    }
    if (fields != "") {
        fields = "Please fill in the following details:\n------------------------------------" + fields;
        alert(fields);
        return false;
    }
    else {
        return true;
    }
}