var URL = window.location.href;
function isValidDate2(inputText) {
    if (inputText.value != '') {
        var dateformat = /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/;
        if (inputText.value.match(dateformat)) {
            var opera1 = inputText.value.split('/');
            var opera2 = inputText.value.split('-');
            lopera1 = opera1.length;
            lopera2 = opera2.length;
            if (lopera1 > 1) {
                var pdate = inputText.value.split('/');
            }
            else if (lopera2 > 1) {
                var pdate = inputText.value.split('-');
            }
            var dd = parseInt(pdate[0]);
            var mm = parseInt(pdate[1]);
            var yy = parseInt(pdate[2]);
            if (yy <= 1752) {
                alert('Invalid date!');
                inputText.value = '';
                return false;
            }
            var ListofDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
            if (mm == 1 || mm > 2) {
                if (dd > ListofDays[mm - 1]) {
                    alert('Invalid date!');
                    inputText.value = '';
                    return false;
                }
            }
            if (mm == 2) {

                var lyear = false;
                if ((!(yy % 4) && yy % 100) || !(yy % 400)) {
                    lyear = true;
                }
                if ((lyear == false) && (dd >= 29)) {
                    alert('Invalid date!');
                    inputText.value = '';
                    return false;
                }
                if ((lyear == true) && (dd > 29)) {
                    alert('Invalid date!');
                    inputText.value = '';
                    return false;
                }
            }
        }
        else {
            alert('Invalid date!');
            inputText.value = '';
            return false;
        }
    }
}

function FormatIt(obj) {
    if (obj.value.length == 2) // Day
        obj.value = obj.value + "/";
    if (obj.value.length == 5) // month
        obj.value = obj.value + "/";
}
function LoadingMessage(msg1, msg2) {
    $('#dvLoadingmsg').show();
    $('#spn1msg').html(msg1);
    $('#spn2msg').html(msg2);
    $('.hls-bg').show();
}
function CloseHlsPopup() {
    $('.hls-bg').hide();
    $('.hls-wrap').hide();
    $('.hls-wrap1').hide();
}
function OpenProductTab(_this, tab) {
    $('a[group=ProductTab]').each(function () {
        $(this).removeClass('active');
    });
    $(_this).addClass('active');
    $('div[group=ProudctTabDiv]').each(function () {
        $(this).css('display', 'none');
    });
    $('#div' + tab + 'Tab').css('display', '');
}


function LoadBatchAndClass() {
    //if ($('#drpGroupBatch').val() > 0 && $('#drpOrganization').val() > 0) {
    var obj = {
        "OrganizationId": $('#drpOrganization').val(),
        "BatchId": $('#drpBatch').val()
    };
    $.ajax({
        type: "POST",
        url: URL + "/LoadBatchAndClass",
        data: JSON.stringify(obj),
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (data) {
            var dt = $.parseJSON(data.d);

            $('#drpDepartment').html(dt.DepratmentRes);
            $('#drpBatch').html(dt.BatchRes);

            $('#drpClass').html(dt.classdivRes);
            $('#MultiClass').html(dt.ClassCheckBoxList);
            $("#citychkdropdown").next().remove();
            $("#Areachkdropdown").next().remove();
            $("#Fatherqulificationchkdropdown").next().remove();
            $("#Motherqulificationchkdropdown").next().remove();
            $("#FatherOccupationchkdropdown").next().remove();
            $("#MotherOccupationchkdropdown").next().remove();
            $("#HOusechkdropdown").next().remove();

            $('#divisondropdown').removeData();
            $("#classchkdropdown").removeAttr("style");
            $("#citychkdropdown").removeAttr("style");
            $("#Areachkdropdown").removeAttr("style");
            $("#Fatherqulificationchkdropdown").removeAttr("style");
            $("#Motherqulificationchkdropdown").removeAttr("style");
            $("#FatherOccupationchkdropdown").removeAttr("style");
            $("#MotherOccupationchkdropdown").removeAttr("style");
            $("#HOusechkdropdown").removeAttr("style");

            $("#classchkdropdown").removeAttr("multiple");
            $("#citychkdropdown").removeAttr("multiple");
            $("#Areachkdropdown").removeAttr("multiple");
            $("#Fatherqulificationchkdropdown").removeAttr("multiple");
            $("#Motherqulificationchkdropdown").removeAttr("multiple");
            $("#FatherOccupationchkdropdown").removeAttr("multiple");
            $("#MotherOccupationchkdropdown").removeAttr("multiple");
            $("#HOusechkdropdown").removeAttr("multiple");

            $("#classchkdropdown").attr("multiple", "multiple");
            $("#citychkdropdown").attr("multiple", "multiple");
            $("#Areachkdropdown").attr("multiple", "multiple");
            $("#Fatherqulificationchkdropdown").attr("multiple", "multiple");
            $("#Motherqulificationchkdropdown").attr("multiple", "multiple");
            $("#FatherOccupationchkdropdown").attr("multiple", "multiple");
            $("#MotherOccupationchkdropdown").attr("multiple", "multiple");
            $("#HOusechkdropdown").attr("multiple", "multiple");


            $("#classchkdropdown").html(dt.ClassCheckBoxList);
            $("#citychkdropdown").html(dt.CityDropDown);
            $("#Areachkdropdown").html(dt.AreaDropDown);
            $("#Fatherqulificationchkdropdown").html(dt.FatherQualification);
            $("#Motherqulificationchkdropdown").html(dt.MotherQualification);
            $("#FatherOccupationchkdropdown").html(dt.FatherOccupation);
            $("#MotherOccupationchkdropdown").html(dt.MotherOccupation);
            $("#HOusechkdropdown").html(dt.HouseDropDown);

            $("#classchkdropdown").removeData();
            $("#citychkdropdown").removeData();
            $("#Areachkdropdown").removeData();
            $("#Fatherqulificationchkdropdown").removeData();
            $("#Motherqulificationchkdropdown").removeData();
            $("#FatherOccupationchkdropdown").removeData();
            $("#MotherOccupationchkdropdown").removeData();
            $("#HOusechkdropdown").removeData();

            $('#classchkdropdown').multiSelect();
            $('#MultiClass').multiSelect();
            $('#MultiClass').next().next().hide();
            $("#citychkdropdown").multiSelect();
            $("#Areachkdropdown").multiSelect();
            $("#Fatherqulificationchkdropdown").multiSelect();
            $("#Motherqulificationchkdropdown").multiSelect();
            $("#FatherOccupationchkdropdown").multiSelect();
            $("#MotherOccupationchkdropdown").multiSelect();
            $("#HOusechkdropdown").multiSelect();

        },
        error: function (xhr, status, error) {
            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });
}



function BindDropDowns() {
    $.ajax({
        type: "POST",
        url: URL + "/GetSession",
        data: '{}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (dt) {
            if (dt.d > 0) {
                $.ajax({
                    type: "POST",
                    url: URL + "/BindDropDowns",
                    data: '{}',
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        //  debugger;
                        var dt = $.parseJSON(data.d);

                        $('#drpOrganization').html(dt.OrganizationRes);
                        $('#Statusdropdown').html(dt.StatusRes);
                        $('#Genderchkdropdown').html(dt.Gendercheckboxlist);
                        $('#Religionchkdropdown').html(dt.Religionchklist);
                        $('#castecategorychkdropdown').html(dt.castcategorychklist);
                        $('#bloodgroupchkdropdown').html(dt.bloodgrpchklist);
                        LoadBatchAndClass();
                        $('#MultiClass').removeData();
                        $('#MultiClass').html(dt.classdivRes);
                        $('#drpClass').html(dt.ClassRes);

                        $('#drpSavedReport').html(dt.SavedReportList);
                        $('#MultiClass').multiSelect();
                        $("#Genderchkdropdown").multiSelect();
                        $("#Religionchkdropdown").multiSelect();
                        $("#castecategorychkdropdown").multiSelect();
                        $("#bloodgroupchkdropdown").multiSelect();

                        
                        $('#drpAvailableFields').html(dt.AvailableRes + dt.AvailableAdmissionCustomRes);
                        $('#drpAvailableFieldsO').html(dt.AvailableRes);

                        $('#drpAvailableFields option').each(function (i, selected) {
                            //debugger;
                            $(this).text($(this).text().replace("|", "[").replace("||", "]"));
                            $(this).val($(this).text().replace("|", "[").replace("||", "]"));
                        });
                        $('#drpAvailableFieldsO option').each(function (i, selected) {
                            //debugger;
                            $(this).text($(this).text().replace("|", "[").replace("||", "]"));
                            $(this).val($(this).text().replace("|", "[").replace("||", "]"));
                        });

                        if (dt.ReportList != '')
                            $('#tblReportList').html(dt.ReportList);
                        else
                            $('#tblReportList').html('<tr><td colspan="5">No Records Available</td></tr>');
                        window.setTimeout(function () { CloseHlsPopup(); }, 500);

                        //alert(dt.statusChechboxlist);
                        var mhtml = "";
                        for (var i = 0; i < $(dt.statusChechboxlist).length; ++i) {
                            mhtml = mhtml + $($(dt.statusChechboxlist)[i]).html() + ", ";
                        }
                        //alert(mhtml)
                        var mFhtml = mhtml.substring(0, mhtml.length - 2);
                        //alert(mFhtml);
                        $("#Statuschkdropdown").next().find(".multi-select-button").html(mFhtml);

                        $("#Statuschkdropdown").next().find(".multi-select-menuitems").children().each(function (index, element) {
                            //alert("h");
                            //alert("his");
                            $(this).find('input[type=checkbox]').attr("checked", "true");
                            $(this).find('input[type=checkbox]').val(1).change();
                            //$(this).children("option:selected").val();

                        });
                        $("#Statuschkdropdown").next().find(".multi-select-button").html();
                        //LoadClassAndDepartment();

                        BindCustomFields()
                    },
                    error: function (xhr, status, error) {
                        var err = eval("(" + xhr.responseText + ")");
                        console.log(err.Message);
                    }
                });
            }
            else {
                CloseHlsPopup();
                LoadingMessage('Your session has expired, we are redirecting to login page', '<b>Please Wait ...</b>');
                window.setTimeout(function () { window.location = '/Pages/BaseFramework/Security/Login.aspx'; }, 2000);
            }
        },
        error: function (xhr, status, error) {
            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });
}
function BindCustomFields() {
    $.ajax({
        type: "POST",
        url: URL + "/GetSession",
        data: '{}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (dt) {
            if (dt.d > 0) {
                var obj = {
                    "BatchId": $('#drpBatch').val()
                };
                $.ajax({
                    type: "POST",
                    url: URL + "/BindCustomFields",
                    data: JSON.stringify(obj),
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        //  debugger;
                        var dt = $.parseJSON(data.d);
                        $('#drpAvailableFields').html("");
                        $('#drpAvailableFieldsO').html("");
                        $('#drpAvailableFields').html(dt.AvailableResCust1 + dt.AvailableAdmissionCustomResCust);
                        $('#drpAvailableFieldsO').html(dt.AvailableResCust1 + dt.AvailableAdmissionCustomResCust);

                        $('#drpAvailableFields option').each(function (i, selected) {
                            //debugger;
                            $(this).text($(this).text().replace("|", "[").replace("||", "]"));
                            $(this).val($(this).text().replace("|", "[").replace("||", "]"));
                        });
                        $('#drpAvailableFieldsO option').each(function (i, selected) {
                            //debugger;
                            $(this).text($(this).text().replace("|", "[").replace("||", "]"));
                            $(this).val($(this).text().replace("|", "[").replace("||", "]"));
                        });
                        //alert("working fine tho..")

                    },
                    error: function (xhr, status, error) {
                        var err = eval("(" + xhr.responseText + ")");
                        console.log(err.Message);
                    }
                });
            }
            else {
                CloseHlsPopup();
                LoadingMessage('Your session has expired, we are redirecting to login page', '<b>Please Wait ...</b>');
                window.setTimeout(function () { window.location = '/Pages/BaseFramework/Security/Login.aspx'; }, 2000);
            }
        },
        error: function (xhr, status, error) {
            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });
}

function LoadCustomFields(cobj) {
    $.ajax({
        type: "POST",
        url: URL + "/GetSession",
        data: '{}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (dt) {
            if (dt.d > 0) {
                var obj = {
                    "ClassId": $('#drpClass').val()
                };
                $.ajax({
                    type: "POST",
                    url: URL + "/LoadCustomFields",
                    data: JSON.stringify(obj),
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        var dt = $.parseJSON(data.d);
                        $('#drpAvailableFields').html("");
                        $('#drpAvailableFieldsO').html("");
                        $('#drpAvailableFields').html(dt.AvailableResCust1 + dt.AvailableAdmissionCustomResCust);
                        $('#drpAvailableFieldsO').html(dt.AvailableResCust1 + dt.AvailableAdmissionCustomResCust);

                        $('#drpAvailableFields option').each(function (i, selected) {
                            //debugger;
                            $(this).text($(this).text().replace("|", "[").replace("||", "]"));
                            $(this).val($(this).text().replace("|", "[").replace("||", "]"));
                        });
                        $('#drpAvailableFieldsO option').each(function (i, selected) {
                            //debugger;
                            $(this).text($(this).text().replace("|", "[").replace("||", "]"));
                            $(this).val($(this).text().replace("|", "[").replace("||", "]"));
                        });
                        //alert("working fine tho..")
                    },
                    error: function (xhr, status, error) {
                        var err = eval("(" + xhr.responseText + ")");
                        console.log(err.Message);
                    }
                });
            }
            else {
                CloseHlsPopup();
                LoadingMessage('Your session has expired, we are redirecting to login page', '<b>Please Wait ...</b>');
                window.setTimeout(function () { window.location = '/Pages/BaseFramework/Security/Login.aspx'; }, 2000);
            }
        },
        error: function (xhr, status, error) {
            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });
}


function LoadClass(cobj) {
    $.ajax({
        type: "POST",
        url: URL + "/GetSession",
        data: '{}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (dt) {
            if (dt.d > 0) {
                var obj = {
                    "BatchId": $(cobj).val()
                };
                $.ajax({
                    type: "POST",
                    url: URL + "/LoadClass",
                    data: JSON.stringify(obj),
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        var dt = $.parseJSON(data.d);
                        $('#drpClass').html(dt.ClassRes);
                        $('#MultiClass').html(dt.ClassCheckBoxList);
                        $('#MultiClass').removeData();
                        $('#MultiClass').multiSelect();
                        $('#MultiClass').next().next().hide();
                        LoadCustomFields();
                    },
                    error: function (xhr, status, error) {
                        var err = eval("(" + xhr.responseText + ")");
                        console.log(err.Message);
                    }
                });
            }
            else {
                CloseHlsPopup();
                LoadingMessage('Your session has expired, we are redirecting to login page', '<b>Please Wait ...</b>');
                window.setTimeout(function () { window.location = '/Pages/BaseFramework/Security/Login.aspx'; }, 2000);
            }
        },
        error: function (xhr, status, error) {
            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });
}

function ChangePage(pageindex) {
    $('#hdnPageIndex').val(pageindex);
    SearchStudents();
}

function ShowHideAndBind(cobj, flag) {
    if ($(cobj).is(':checked')) {
        $(cobj).parent().next('div').show();
        $(cobj).parent().next('div').html('&nbsp;<img src="../../Images/loading-ajax.gif" />' + '&nbsp; Loading....');
        BindValues(cobj, flag);
    }
    else {
        $(cobj).parent().next('div').hide();
        $(cobj).parent().next('div').html('');
    }
}

function BindValues(cobj, flag) {
    $.ajax({
        type: "POST",
        url: URL + "/GetSession",
        data: '{}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (dt) {
            if (dt.d > 0) {
                var obj = {
                    "flag": flag,
                };
                $.ajax({
                    type: "POST",
                    url: URL + "/BindValues",
                    data: JSON.stringify(obj),
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        var datas = $.parseJSON(data.d);
                        if (datas.Result != '') {
                            $(cobj).parent().next('div').html(datas.Result);
                            $(cobj).parent().next('div').css('border', '1px solid Silver');
                            $(cobj).parent().next('div').slimScroll({ height: '100px' });
                        }
                        else {
                            $(cobj).parent().next('div').html('&nbsp;No Data');
                        }
                    },
                    error: function (xhr, status, error) {
                        var err = eval("(" + xhr.responseText + ")");
                        console.log(err.Message);
                    }
                });
            }
            else {
                CloseHlsPopup();
                LoadingMessage('Your session has expired, we are redirecting to login page', '<b>Please Wait ...</b>');
                window.setTimeout(function () { window.location = '/Pages/BaseFramework/Security/Login.aspx'; }, 2000);
            }
        },
        error: function (xhr, status, error) {
            var err = eval("(" + xhr.responseText + ")");
            console.log(err.Message);
        }
    });
}


function FormatIt(obj) {
    if (obj.value.length == 2) // Day
        obj.value = obj.value + "/";
    if (obj.value.length == 5) // month
        obj.value = obj.value + "/";
}

function ShowHideSaveReport(obj) {
    if ($(obj).is(':checked')) {
        $('div[group=dvreportgroup]').show();
    }
    else {
        $('div[group=dvreportgroup]').hide();
        $('#chkIsCommon').prop('checked', false);
        $('#chkIsCommonForOrg').prop('checked', false);
    }
}

function SearchStudents() {

    //alert("hi");
    $.ajax({
        type: "POST",
        url: URL + "/GetSession",
        data: '{}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (dt) {
            if (dt.d > 0) {

                var Organization = '', BatchId = '', ClassStr = '';
                var OrderByStr = '', ClassIds = '', FormNo = '', StatusStr = '', GenderStr = '', ReligionStr = '';
                var CastCatStr = '', CountryStr = '';
                var BloodGroupStr = '';
                var TongueStr = '', SchoolStr = '';

                $('#MultiClass option:selected').each(function (i, sel) {
                    ClassIds = ClassIds + $(sel).val() + ',';
                });
                $('#Statuschkdropdown option:selected').each(function (i, sel) {

                    StatusStr = StatusStr + $(sel).val() + ',';
                });
                $('#Genderchkdropdown option:selected').each(function (i, sel) {
                    GenderStr = GenderStr + $(sel).val() + ',';
                });
                $('#Religionchkdropdown option:selected').each(function (i, sel) {
                    ReligionStr = ReligionStr + $(sel).val() + ',';
                });
                $('#castecategorychkdropdown option:selected').each(function (i, sel) {
                    CastCatStr = CastCatStr + $(sel).val() + ',';
                });
                $('#bloodgroupchkdropdown option:selected').each(function (i, sel) {
                    BloodGroupStr = BloodGroupStr + $(sel).val() + ',';
                });


                if ($('#chkDivision').is(':checked')) {
                    $('#dvDivision').children('input[type=checkbox]').each(function (index, element) {
                        if ($(this).is(':checked')) {
                            ClassIds = ClassIds + $(this).attr('attrid') + ',';
                        }
                    });
                }
                if ($('#chkStatus').is(':checked')) {
                    $('#dvStatus').children('input[type=checkbox]').each(function (index, element) {
                        if ($(this).is(':checked')) {
                            StatusStr = StatusStr + $(this).attr('attrid') + ',';
                        }
                    });
                }
                if ($('#chkGender').is(':checked')) {
                    $('#dvGender').children('input[type=checkbox]').each(function (index, element) {
                        if ($(this).is(':checked')) {
                            GenderStr = GenderStr + $(this).attr('attrid') + ',';
                        }
                    });
                }
                if ($('#chkReligion').is(':checked')) {
                    $('#dvReligion').children('input[type=checkbox]').each(function (index, element) {
                        if ($(this).is(':checked')) {
                            ReligionStr = ReligionStr + $(this).attr('attrid') + ',';
                        }
                    });
                }
                if ($('#chkCastCategory').is(':checked')) {
                    $('#dvCastCategory').children('input[type=checkbox]').each(function (index, element) {
                        if ($(this).is(':checked')) {
                            CastCatStr = CastCatStr + $(this).attr('attrid') + ',';
                        }
                    });
                }   
                if ($('#chkBloodGroup').is(':checked')) {
                    $('#dvBloodGroup').children('input[type=checkbox]').each(function (index, element) {
                        if ($(this).is(':checked')) {
                            BloodGroupStr = BloodGroupStr + $(this).attr('attrid') + ',';
                        }
                    });
                }

                if ($('#chkPreSchool').is(':checked')) {
                    $('#dvPreSchool').children('input[type=checkbox]').each(function (index, element) {
                        if ($(this).is(':checked')) {
                            SchoolStr = SchoolStr + $(this).attr('attrid') + ',';
                        }
                    });
                }
                OrganizationStr = $('#drpOrganization').val()
                BatchIdStr = $('#drpBatch').val()
                var obj = {
                    "OrganizationId": OrganizationStr,
                    "BatchId": BatchIdStr,
                    "ClassId": $('#drpClass').val(),
                    "ClassIds": ClassIds,
                    "FormNo": $('#txtIdNo').val(),
                    "FromFormNo": $('#txtFIdNo').val(),
                    "ToFormNo": $('#txtTIdNo').val(),
                    "StatusId": $('#Statusdropdown').val(),

                    "FirstName": $('#txtFirstName').val(),
                    "LastName": $('#txtLastName').val(),
                    "FatherName": $('#txtFather').val(),
                    "MotherName": $('#txtMother').val(),

                    "FromDateOfBirth": $('#txtFDateOfBirth').val(),
                    "ToDateOfBirth": $('#txtTDateOfBirth').val(),
                    "FromAdmissionDate": $('#txtFAdminssionDate').val(),
                    "ToAdmissionDate": $('#txtTAdminssionDate').val(),

                    "Country": $('#txtCountry').val(),
                    "State": $('#txtState').val(),
                    "City": $('#txtCity').val(),
                    "ContactNo": $('#txtContactNo').val(),
                    "GenderStr": GenderStr,
                    "ReligionStr": ReligionStr,
                    "CastCatStr": CastCatStr,
                    "Nationality": $('#txtNationality').val(),
                    "BloodGroupStr": BloodGroupStr,
                    "TongueStr": $('#txtMotherTongue').val(),
                    "PreSchoolStr": $('#txtPreviousSchool').val(),
                    "fQualificationStr": $('#txtFatherQualification').val(),
                    "mQualificationStr": $('#txtMotherQualification').val(),
                    "fOccupationStr": $('#txtFatherOccupation').val(),
                    "mOccupationStr": $('#txtMotherrOccupation').val(),
                    "PageIndex": $('#hdnPageIndex').val()
                };
                $('#TableDiv').show();
                $('#TableDiv1').hide();
                $.ajax({
                    type: "POST",
                    url: URL + "/SearchAdmissions",
                    data: JSON.stringify(obj),
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        
                        
                        var table = data.d.split('::')[0];
                        //var tablenew = "<div style='margin: 10px 10px 10px 10px;'> <table id='AdmissionCustomSearchTable' class='table table-striped table-bordered'>"
                        document.getElementById('TableDiv').innerHTML = data.d.split('::')[0];
                        //var hdn = $('#hdnPageIndex').val();
                        //var startindex = hdn * 10 - 10;
                        //var endindex = hdn * 10;
                        //var tablehtml = document.getElementById("AdmissionCustomSearchTable").rows[0];
                        //for (var i = startindex + 1; startindex < endindex; startindex++) {
                            //tablenew += document.getElementById("AdmissionCustomSearchTable").rows[startindex];
                            //$('#tbladmissionlist').append(document.getElementById("AdmissionCustomSearchTable").rows[startindex])
                        //}
                        //$('#tbladmissionlist').innerHTML = tablehtml;
                        //var dt = $.parseJSON(data.d);
                        var t = JSON.parse(data.d.split('::')[1]);
                        $('#pagging').html(t['Paging'])
                        $('#lblTotalRecords').html('Total Records : ' + t['TotalRecords'] + '&nbsp;&nbsp;&nbsp;&nbsp;');
                        $('#hdnPageIndex').val(1);
                    },
                    error: function (xhr, status, error) {
                        var err = eval("(" + xhr.responseText + ")");
                        console.log(err.Message);
                    }
                });
            }
        },
    });
}

function SelectAdmissoinForm(sobj) {
    
    $.ajax({
        type: "POST",
        url: URL + "/GetSession",
        data: '{}',
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        success: function (dt) {
            if (dt.d > 0) {
                $FormId = sobj.parentNode.parentNode.childNodes[7].textContent;
                $BatchId = sobj.parentNode.parentNode.childNodes[8].textContent;
                var obj = {
                    "AdmissionId": $FormId,
                    "BatchId": $BatchId,
                    "OrgId": $('#drpOrganization').val()
                };
                $.ajax({
                    type: "POST",
                    url: URL + "/SelectAdmission",
                    data: JSON.stringify(obj),
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    success: function (data) {
                        window.location = '/Pages/Admission/AdmissionAllDetail.aspx';
                    },
                    error: function (xhr, status, error) {
                        var err = eval("(" + xhr.responseText + ")");
                        console.log(err.Message);
                    }
                });
            }
    else {
                CloseHlsPopup();
    LoadingMessage('Your session has expired, we are redirecting to login page', '<b>Please Wait ...</b>');
    window.setTimeout(function () { window.location = '/Pages/BaseFramework/Security/Login.aspx'; }, 2000);
}
},
error: function (xhr, status, error) {
    var err = eval("(" + xhr.responseText + ")");
    console.log(err.Message);
}
});
}

function ResetControls() {
    $('.multi-select-button').html('-- Select --');
    $("#Genderchkdropdown").val('');
    $("#Religionchkdropdown").val('');
    $("#castecategorychkdropdown").val('');
    $("#bloodgroupchkdropdown").val('');
   
    $("#mothertoungechkdropdown").val('');
    $("#Priviosschoolchkdropdown").val('');
    $("#Fatherqulificationchkdropdown").val('');
    $("#Motherqulificationchkdropdown").val('');
    $("#FatherOccupationchkdropdown").val('');
    $("#MotherOccupationchkdropdown").val('');
  
    $("#customtagchkdropdown").val('');
    $("#Customgroupchkdropdown").val('');
    $('.multi-select-menuitems').each(function (index, element) {
        $(this).find('input[type=checkbox]').prop('checked', false);
    });
    $('#hdnPageIndex').val(1);
    $('#drpDepartment').val(0);
    $('#drpClass').val(0);
    $('#drpBatch').val(0);
    $('#drpDivision').val(0);
    $('#drpStatus').val(0);
    $('#txtIdNo').val('');
    $('#txtFirstName').val('');
    $('#txtLastName').val('');
    $('#txtCountry').val('');
    $('#txtState').val('');
    $("#txtCity").val('');
    $('#txtFDateOfBirth').val('');
    $('#txtTDateOfBirth').val('');
    $('#txtFAdminssionDate').val('');
    $('#txtTAdminssionDate').val('');
    $("#txtNationality").val('');
    $('#txtMotherTongue').val('');
    $('#txtPreviousSchool').val
    $('#txtFatherQualification').val('');
    $('#txtMotherQualification').val('');
    $('#txtFatherOccupation').val('');
    $('#txtMotherrOccupation').val('');

    $('#TableDiv').hide();
    $('#TableDiv1').show();


    $('#txtFIdNo').val('');
    $('#txtTIdNo').val('');
    $('#txtFather').val('');
    $('#txtMother').val('');
    $('#txtReportName').val('');
    $('#txtContactNo').val('');

    $('#chkClass').prop('checked', false);
    $('#dvClass').html('');
    $('#dvClass').hide();
    $('#dvClass').css('height', '0');
    $("#dvClass").slimScroll({ destroy: true });

    $('#chkStatus').prop('checked', false);
    $('#dvStatus').html('');
    $('#dvStatus').hide();
    $('#dvStatus').css('height', '0');
    $("#dvStatus").slimScroll({ destroy: true });

    $('#chkGender').prop('checked', false);
    $('#dvGender').html('');
    $('#dvGender').hide();
    $('#dvGender').css('height', '0');
    $("#dvGender").slimScroll({ destroy: true });

    $('#chkReligion').prop('checked', false);
    $('#dvReligion').html('');
    $('#dvReligion').hide();
    $('#dvReligion').css('height', '0');
    $("#dvReligion").slimScroll({ destroy: true });

    $('#chkCastCategory').prop('checked', false);
    $('#dvCastCategory').html('');
    $('#dvCastCategory').hide();
    $('#dvCastCategory').css('height', '0');
    $("#dvCastCategory").slimScroll({ destroy: true });

    $('#chkCity').prop('checked', false);
    $('#dvCity').html('');
    $('#dvCity').hide();
    $('#dvCity').css('height', '0');
    $("#dvCity").slimScroll({ destroy: true });

    $('#chkNationality').prop('checked', false);
    $('#dvNationality').html('');
    $('#dvNationality').hide();
    $('#dvNationality').css('height', '0');
    $("#dvNationality").slimScroll({ destroy: true });

    

    $('#chkBloodGroup').prop('checked', false);
    $('#dvBloodGroup').html('');
    $('#dvBloodGroup').hide();
    $('#dvBloodGroup').css('height', '0');
    $("#dvBloodGroup").slimScroll({ destroy: true });
     
    $('#chkFatherQ').prop('checked', false);
    $('#dvFatherQ').html('');
    $('#dvFatherQ').hide();
    $('#dvFatherQ').css('height', '0');
    $("#dvFatherQ").slimScroll({ destroy: true });

    $('#chkMotherQ').prop('checked', false);
    $('#dvMotherQ').html('');
    $('#dvMotherQ').hide();
    $('#dvMotherQ').css('height', '0');
    $("#dvMotherQ").slimScroll({ destroy: true });

    $('#chkFatherO').prop('checked', false);
    $('#dvFatherO').html('');
    $('#dvFatherO').hide();
    $('#dvFatherO').css('height', '0');
    $("#dvFatherO").slimScroll({ destroy: true });

    $('#chkMotherO').prop('checked', false);
    $('#dvMotherO').html('');
    $('#dvMotherO').hide();
    $('#dvMotherO').css('height', '0');
    $("#dvMotherO").slimScroll({ destroy: true });
 
    $('#chkPhisicalD').prop('checked', false);
    $('#chkMajorILL').prop('checked', false);
    $('#chkBlackList').prop('checked', false);
    $('#chkSaveReport').prop('checked', false);
    $('#chkIsCommon').prop('checked', false);
    $('#chkIsCommonForOrg').prop('checked', false);
    $('#tblStudentList').html('');
    $('#pagging').html('');
    $('#lblTotalRecords').html('');
    //$('.btn-primary').hide();
    $("#customPaging").hide();
    $("#drpSelectedFields").html('');

    $('#chkRecordTag').prop('checked', false);
    $('#dvRecordTag').html('');
    $('#dvRecordTag').hide();
    $('#dvRecordTag').css('height', '0');
    $("#dvRecordTag").slimScroll({ destroy: true });
}

function AppendSelected(flag) {
    if (flag == 1) {
        if ($("#drpAvailableFields option:selected").length > 0) {
            if ($("#drpAvailableFields option:selected").length == 1) {
                var option = new Option($('#drpAvailableFields').val(), "value");
                var dup = false;
                $('#drpSelectedFields option').each(function (i, selected) {
                    if ($(selected).text() == $('#drpAvailableFields').val()) {
                        dup = true;
                    }
                });
                if (dup == false) {
                    $('#drpSelectedFields').append(option);
                }
            }
            else {
                $("#drpAvailableFields option:selected").each(function (index, element) {
                    var dup = false;
                    var sTexts = $(this).val();
                    $('#drpSelectedFields option').each(function (i, selected) {
                        if ($(selected).text() == sTexts) {
                            dup = true;
                        }
                    });
                    if (dup == false) {
                        sTexts = sTexts.replace("|", "[").replace("||", "]");
                        var option = new Option(sTexts, "value");
                        $('#drpSelectedFields').append(option);
                    }
                });
            }
        }
        else {
            alert('Please select at least one value from list box');
        }
    }
    else {
        if ($("#drpSelectedFields option:selected").length > 0) {
            $("#drpSelectedFields option:selected").remove();
        }
        else {
            alert('Please select at least one value from list box');
        }
    }
}

    function UpDownSelected(flag) {
        if ($("#drpSelectedFields option:selected").length > 0) {
            var $op = $('#drpSelectedFields option:selected');
            if ($op.length) {
                (flag == '1') ? $op.first().prev().before($op) : $op.last().next().after($op);
            }
        }
    }
    function AppendSelectedO(flag) {
        if (flag == 1) {
            if ($("#drpAvailableFieldsO option:selected").length > 0) {
                if ($("#drpAvailableFieldsO option:selected").length == 1) {
                    var option = new Option($('#drpAvailableFieldsO').val(), "value");
                    var dup = false;
                    $('#drpSelectedFieldsO option').each(function (i, selected) {
                        if ($(selected).text() == $('#drpAvailableFieldsO').val()) {
                            dup = true;
                        }
                    });
                    if (dup == false) {
                        $('#drpSelectedFieldsO').append(option);
                    }
                }
                else {
                    $("#drpAvailableFieldsO option:selected").each(function (index, element) {
                        var dup = false;
                        var sTexts = $(this).val();
                        $('#drpSelectedFieldsO option').each(function (i, selected) {
                            if ($(selected).text() == sTexts) {
                                dup = true;
                            }
                        });
                        if (dup == false) {
                            sTexts = sTexts.replace("|", "[").replace("||", "]");
                            var option = new Option(sTexts, "value");
                            $('#drpSelectedFieldsO').append(option);
                        }
                    });
                }
            }
            else {
                alert('Please select at least one value from list box');
            }
        }
        else {
            if ($("#drpSelectedFieldsO option:selected").length > 0) {
                $("#drpSelectedFieldsO option:selected").remove();
            }
            else {
                alert('Please select at least one value from list box');
            }
        }
    }
    function UpDownSelectedO(flag) {
        if ($("#drpSelectedFieldsO option:selected").length > 0) {
            var $op = $('#drpSelectedFieldsO option:selected');
            if ($op.length) {
                (flag == '1') ? $op.first().prev().before($op) : $op.last().next().after($op);
            }
        }
    }
    function GenerateReport() {
        if ($("#drpSelectedFields option").length > 0 || parseInt($('#drpSavedReport').val()) != 0) {
            if ($("#drpSelectedFields option").length > 9 && $('#drpViewMode').val() == "PDF") {
                alert('Select maximum 9 custom fields');
            }
            else {
                $.ajax({
                    type: "POST",
                    url: URL + "/GetSession",
                    data: '{}',
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    success: function (dt) {
                        if (dt.d > 0) {
                            //LoadingMessage('Loading Details', '<b>Please Wait ...</b>');
                            var OrderByStr = '',  StatusStr = '', GenderStr = '', ReligionStr = '';
                            var CastCatStr = '', ClassIds = '';
                            var BloodGroupStr = '';
                            var TongueStr = '', SchoolStr = '', fQualificationStr = '', mQualificationStr = '';
                            var fOccupationStr = '', mOccupationStr = '';
                            var CustomFields = '', RecordTagStr = '';

                            $('#MultiClass option:selected').each(function (i, sel) {
                                ClassIds = ClassIds + $(sel).val() + ',';
                            });
                            $("#drpSelectedFieldsO option").each(function (index, element) {
                                OrderByStr = OrderByStr + $(this).text() + ',';
                            });
                            $("#drpSelectedFields option").each(function (index, element) {
                                CustomFields = CustomFields + $(this).text() + ',';
                            });
                           
                            $('#Genderchkdropdown option:selected').each(function (i, sel) {
                                GenderStr = GenderStr + $(sel).val() + ',';
                            });
                            $('#Religionchkdropdown option:selected').each(function (i, sel) {
                                ReligionStr = ReligionStr + $(sel).val() + ',';
                            });
                            $('#castecategorychkdropdown option:selected').each(function (i, sel) {
                                CastCatStr = CastCatStr + $(sel).val() + ',';
                            });

                            $('#bloodgroupchkdropdown option:selected').each(function (i, sel) {
                                BloodGroupStr = BloodGroupStr + $(sel).val() + ',';
                            });

                            if ($('#chkDivision').is(':checked')) {
                                $('#dvDivision').children('input[type=checkbox]').each(function (index, element) {
                                    if ($(this).is(':checked')) {
                                        ClassIds = ClassIds + $(this).attr('attrid') + ',';
                                    }
                                });
                            }
                            if ($('#chkClass').is(':checked')) {
                                $('#dvClass').children('input[type=checkbox]').each(function (index, element) {
                                    if ($(this).is(':checked')) {
                                        ClassStr = ClassStr + $(this).attr('attrid') + ',';
                                    }
                                });
                            }
                            if ($('#chkStatus').is(':checked')) {
                                $('#dvStatus').children('input[type=checkbox]').each(function (index, element) {
                                    if ($(this).is(':checked')) {
                                        StatusStr = StatusStr + $(this).attr('attrid') + ',';
                                    }
                                });
                            }
                            if ($('#chkGender').is(':checked')) {
                                $('#dvGender').children('input[type=checkbox]').each(function (index, element) {
                                    if ($(this).is(':checked')) {
                                        GenderStr = GenderStr + $(this).attr('attrid') + ',';
                                    }
                                });
                            }
                            if ($('#chkReligion').is(':checked')) {
                                $('#dvReligion').children('input[type=checkbox]').each(function (index, element) {
                                    if ($(this).is(':checked')) {
                                        ReligionStr = ReligionStr + $(this).attr('attrid') + ',';
                                    }
                                });
                            }
                            if ($('#chkCastCategory').is(':checked')) {
                                $('#dvCastCategory').children('input[type=checkbox]').each(function (index, element) {
                                    if ($(this).is(':checked')) {
                                        CastCatStr = CastCatStr + $(this).attr('attrid') + ',';
                                    }
                                });
                            }
                            if ($('#chkCity').is(':checked')) {
                                $('#dvCity').children('input[type=checkbox]').each(function (index, element) {
                                    if ($(this).is(':checked')) {
                                        CityStr = CityStr + $(this).attr('attrid') + ',';
                                    }
                                });
                            }
                            if ($('#chkBloodGroup').is(':checked')) {
                                $('#dvBloodGroup').children('input[type=checkbox]').each(function (index, element) {
                                    if ($(this).is(':checked')) {
                                        BloodGroupStr = BloodGroupStr + $(this).attr('attrid') + ',';
                                    }
                                });
                            }
                            if ($('#chkPreSchool').is(':checked')) {
                                $('#dvPreSchool').children('input[type=checkbox]').each(function (index, element) {
                                    if ($(this).is(':checked')) {
                                        SchoolStr = SchoolStr + $(this).attr('attrid') + ',';
                                    }
                                });
                            }
                            if ($('#chkFatherQ').is(':checked')) {
                                $('#dvFatherQ').children('input[type=checkbox]').each(function (index, element) {
                                    if ($(this).is(':checked')) {
                                        fQualificationStr = fQualificationStr + $(this).attr('attrid') + ',';
                                    }
                                });
                            }
                            if ($('#chkMotherQ').is(':checked')) {
                                $('#dvMotherQ').children('input[type=checkbox]').each(function (index, element) {
                                    if ($(this).is(':checked')) {
                                        mQualificationStr = mQualificationStr + $(this).attr('attrid') + ',';
                                    }
                                });
                            }
                            if ($('#chkFatherO').is(':checked')) {
                                $('#dvFatherO').children('input[type=checkbox]').each(function (index, element) {
                                    if ($(this).is(':checked')) {
                                        fOccupationStr = fOccupationStr + $(this).attr('attrid') + ',';
                                    }
                                });
                            }
                            if ($('#chkMotherO').is(':checked')) {
                                $('#dvMotherO').children('input[type=checkbox]').each(function (index, element) {
                                    if ($(this).is(':checked')) {
                                        mOccupationStr = mOccupationStr + $(this).attr('attrid') + ',';
                                    }
                                });
                            }
                            if ($('#chkAdmissionCat').is(':checked')) {
                                $('#dvAdmissionCat').children('input[type=checkbox]').each(function (index, element) {
                                    if ($(this).is(':checked')) {
                                        AdmissionStr = AdmissionStr + $(this).attr('attrid') + ',';
                                    }
                                });
                            }
                            if ($('#chkCustomTag').is(':checked')) {
                                $('#dvCustomTag').children('input[type=checkbox]').each(function (index, element) {
                                    if ($(this).is(':checked')) {
                                        TagStr = TagStr + $(this).attr('attrid') + ',';
                                    }
                                });
                            }
                            if ($('#chkCustomGroup').is(':checked')) {
                                $('#dvCustomGroup').children('input[type=checkbox]').each(function (index, element) {
                                    if ($(this).is(':checked')) {
                                        GroupStr = GroupStr + $(this).attr('attrid') + ',';
                                    }
                                });
                            }
                            if ($('#chkClub').is(':checked')) {
                                $('#dvClub').children('input[type=checkbox]').each(function (index, element) {
                                    if ($(this).is(':checked')) {
                                        ClubStr = ClubStr + $(this).attr('attrid') + ',';
                                    }
                                });
                            }
                            if ($('#chkHostel').is(':checked')) {
                                $('#dvHostel').children('input[type=checkbox]').each(function (index, element) {
                                    if ($(this).is(':checked')) {
                                        HostelStr = HostelStr + $(this).attr('attrid') + ',';
                                    }
                                });
                            }
                            if ($('#chkStudentGroup').is(':checked')) {
                                $('#dvStudentGroup').children('input[type=checkbox]').each(function (index, element) {
                                    if ($(this).is(':checked')) {
                                        sGroupStr = sGroupStr + $(this).attr('attrid') + ',';
                                    }
                                });
                            }
                            if ($('#chkStudentOtherGroup').is(':checked')) {
                                $('#dvStudentOtherGroup').children('input[type=checkbox]').each(function (index, element) {
                                    if ($(this).is(':checked')) {
                                        sOtherGroupStr = sOtherGroupStr + $(this).attr('attrid') + ',';
                                    }
                                });
                            }
                            if ($('#chkExemptionType').is(':checked')) {
                                $('#dvExemptionType').children('input[type=checkbox]').each(function (index, element) {
                                    if ($(this).is(':checked')) {
                                        ExemptionStr = ExemptionStr + $(this).attr('attrid') + ',';
                                    }
                                });
                            }
                            if ($('#chkRecordTag').is(':checked')) {
                                $('#dvRecordTag').children('input[type=checkbox]').each(function (index, element) {
                                    if ($(this).is(':checked')) {
                                        RecordTagStr = RecordTagStr + $(this).attr('attrid') + ',';
                                    }
                                });
                            }
                            var isCommon = $('#chkIsCommon').is(':checked') ? 1 : 0;
                            var isCommonForOrg = $('#chkIsCommonForOrg').is(':checked') ? 1 : 0;

                            //if ($('#chkSaveReport').is(':checked') && ($('#drpClass').val() > 0 || $('#drpDivision').val() > 0 || $('#drpStatus').val() > 0)) {
                            //    ClassId = 0;
                            //    DivisionId = 0;
                            //    StatusId = 0;
                            //    alert('For Saving Report you must Select Class, Division and Status from MultiSelect');
                            //    CloseHlsPopup();
                            //    return;
                            //}
                            //else {
                            //    ClassId = $('#drpClass').val();
                            //    DivisionId = $('#drpDivision').val();
                            //    StatusId = $('#drpStatus').val();
                            //}
                            OrganizationStr = $('#drpOrganization').val()
                            BatchIdStr = $('#drpBatch').val()
                            var obj = {
                                "OrganizationId": OrganizationStr,
                                "BatchId": BatchIdStr,
                                "ClassId": $('#drpClass').val(),
                                "ClassIds": ClassIds,
                                "FormNo": $('#txtIdNo').val(),
                                "FromFormNo": $('#txtFIdNo').val(),
                                "ToFormNo": $('#txtTIdNo').val(),
                                "StatusId": $('#Statusdropdown').val(),

                                "FirstName": $('#txtFirstName').val(),
                                "LastName": $('#txtLastName').val(),
                                "FatherName": $('#txtFather').val(),
                                "MotherName": $('#txtMother').val(),

                                "FromDateOfBirth": $('#txtFDateOfBirth').val(),
                                "ToDateOfBirth": $('#txtTDateOfBirth').val(),
                                "FromAdmissionDate": $('#txtFAdminssionDate').val(),
                                "ToAdmissionDate": $('#txtTAdminssionDate').val(),

                                "Country": $('#txtCountry').val(),
                                "State": $('#txtState').val(),
                                "City": $('#txtCity').val(),
                                "ContactNo": $('#txtContactNo').val(),
                                "GenderStr": GenderStr,
                                "ReligionStr": ReligionStr,
                                "CastCatStr": CastCatStr,
                                "Nationality": $('#txtNationality').val(),
                                "BloodGroupStr": BloodGroupStr,
                                "TongueStr": $('#txtMotherTongue').val(),
                                "PreSchoolStr": $('#txtPreviousSchool').val(),
                                "fQualificationStr": $('#txtFatherQualification').val(),
                                "mQualificationStr": $('#txtMotherQualification').val(),
                                "fOccupationStr": $('#txtFatherOccupation').val(),
                                "mOccupationStr": $('#txtMotherrOccupation').val(),
                                "Title": $('#txtReportName').val(),
                                "OrderByStr": OrderByStr,
                                "CustomFields": CustomFields,
                                "isCommon": isCommon,
                                "isCommonForOrg": isCommonForOrg,
                                "ReportName": $('#txtReportName').val(),
                                "PageIndex": $('#hdnPageIndex').val(),
                                "SavedReport": $('#drpSavedReport').val(),
                            };
                            $.ajax({
                                type: "POST",
                                url: URL + "/GenerateAdmissionReport",
                                data: JSON.stringify(obj),
                                contentType: "application/json;charset=utf-8",
                                dataType: "json",
                                success: function (data) {
                                    
                                    if ($('#drpViewMode').val() == "Screen")
                                        $('#btnGenerate').click();
                                    else
                                        $('#btnView').click();
                                    if ($('#txtReportName').val() != '') {
                                        $.ajax({
                                            type: "POST",
                                            url: URL + "/BindDropDowns",
                                            data: '{}',
                                            contentType: "application/json;charset=utf-8",
                                            dataType: "json",
                                            success: function (data) {
                                                //  debugger;
                                                var dt = $.parseJSON(data.d);
                                                if (dt.ReportList != '')
                                                    $('#tblReportList').html(dt.ReportList);
                                                else
                                                    $('#tblReportList').html('<tr><td colspan="5">No Records Available</td></tr>');
                                            },
                                            error: function (xhr, status, error) {
                                                var err = eval("(" + xhr.responseText + ")");
                                                console.log(err.Message);
                                            }
                                        });
                                    }
                                    window.setTimeout(function () { CloseHlsPopup(); }, 400);
                                },
                                error: function (xhr, status, error) {
                                    var err = eval("(" + xhr.responseText + ")");
                                    console.log(err.Message);
                                }
                            });
                        }
                        else {
                            CloseHlsPopup();
                            LoadingMessage('Your session has expired, we are redirecting to login page', '<b>Please Wait ...</b>');
                            window.setTimeout(function () { window.location = '/Pages/BaseFramework/Security/Login.aspx'; }, 2000);
                        }
                    },
                    error: function (xhr, status, error) {
                        var err = eval("(" + xhr.responseText + ")");
                        console.log(err.Message);
                    }
                });
            }
        }
        else {
            alert('select atleast one field in custom field');
        }
    }

    $("#chkMail").click(function (e) {
        //alert("hi");
        var ischkMail = $('#chkMail').is(':checked') ? 1 : 0;
        var isChkMobileApp = $('#chkMobile').is(':checked') ? 1 : 0;

        //alert(ischkMail);
        // alert(isChkMobileApp);
        if (ischkMail == 1 && isChkMobileApp == 0) {
            $("#dvSubject").show();
        }
        else if (ischkMail == 0 && isChkMobileApp == 1) {
            $("#dvSubject").show();
        }
        else if (ischkMail == 1 && isChkMobileApp == 1) {
            $("#dvSubject").show();
        }
        else {
            $("#dvSubject input").val('');
            $("#dvSubject").hide();
        }

    });
    $("#chkMobile").click(function (e) {
        var ischkMail = $('#chkMail').is(':checked') ? 1 : 0;
        var isChkMobileApp = $('#chkMobile').is(':checked') ? 1 : 0;

        //alert(ischkMail);
        // alert(isChkMobileApp);
        if (ischkMail == 1 && isChkMobileApp == 0) {
            $("#dvSubject").show();
        }
        else if (ischkMail == 0 && isChkMobileApp == 1) {
            $("#dvSubject").show();
        }
        else if (ischkMail == 1 && isChkMobileApp == 1) {
            $("#dvSubject").show();
        }
        else {
            $("#dvSubject input").val('');
            $("#dvSubject").hide();
        }

    });

    function CheckUncheckMessage(flag) {
        if (flag == 1) {
            $('#dvSubject').hide();
            $('#txtSubject').val('');
            $('#lblMail').show();
            $('#lblMobile').show();
        }
        else if (flag == 2) {
            $('#dvSubject').show();
            $('#txtSubject').val('');
            $('#lblMobile').show();
            $('#lblMail').hide();
        }
        else {
            $('#dvSubject').show();
            $('#txtSubject').val('');
            $('#lblMail').hide();
            $('#lblMobile').hide();
        }
    }
    function grow(text, long, counter) {
        var id = (text.id);
        CountDesc(text, long);
        textCount(text, counter, long);
        TextBoxSizeOnLoad(id);
    }
    function CountDesc(text, long) {
        var maxlength = new Number(long); // Change number to your max length.
        var id = (text.id);
        if (document.getElementById('txtMessage').value.length > maxlength) {
            text.value = text.value.substring(0, maxlength);
        }
    }
    function TextBoxSizeOnLoad(id) {
        $('#lblCharacter').html(($('#txtMessage').val().length));
        var desc = $('#txtMessage').val().length;
        if (desc <= 25000) {
            $('#lblCharacter').html(desc);
        }
        else {
            $('#lblCharacter').html(0);
        }
    }
    function ViewReport(Id) {
        $.ajax({
            type: "POST",
            url: URL + "/GetSession",
            data: '{}',
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (dt) {
                if (dt.d > 0) {
                    var x = confirm('Do you want to Download?');
                    if (x == true) {
                        //LoadingMessage('Loading Details', '<b>Please Wait ...</b>');
                        var obj = {
                            "Id": Id
                        };
                        $.ajax({
                            type: "POST",
                            url: URL + "/ViewReport",
                            data: JSON.stringify(obj),
                            contentType: "application/json;charset=utf-8",
                            dataType: "json",
                            success: function (data) {
                                $('#btnSaveReport').click();
                                CloseHlsPopup();
                            },
                            error: function (xhr, status, error) {
                                var err = eval("(" + xhr.responseText + ")");
                                console.log(err.Message);
                            }
                        });
                    }
                }
                else {
                    CloseHlsPopup();
                    LoadingMessage('Your session has expired, we are redirecting to login page', '<b>Please Wait ...</b>');
                    window.setTimeout(function () { window.location = '/Pages/BaseFramework/Security/Login.aspx'; }, 2000);
                }
            },
            error: function (xhr, status, error) {
                var err = eval("(" + xhr.responseText + ")");
                console.log(err.Message);
            }
        });
    }
    function DeleteReport(Id) {
        $.ajax({
            type: "POST",
            url: URL + "/GetSession",
            data: '{}',
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (dt) {
                if (dt.d > 0) {
                    var x = confirm('Do you want to delete?');
                    if (x == true) {
                        //LoadingMessage('Loading Details', '<b>Please Wait ...</b>');
                        var obj = {
                            "Id": Id
                        };
                        $.ajax({
                            type: "POST",
                            url: URL + "/DeleteReport",
                            data: JSON.stringify(obj),
                            contentType: "application/json;charset=utf-8",
                            dataType: "json",
                            success: function (data) {
                                location.reload();
                                alert('Deleted successfully....!!');
                                CloseHlsPopup();
                            },
                            error: function (xhr, status, error) {
                                var err = eval("(" + xhr.responseText + ")");
                                console.log(err.Message);
                            }
                        });
                    }
                }
                else {
                    CloseHlsPopup();
                    LoadingMessage('Your session has expired, we are redirecting to login page', '<b>Please Wait ...</b>');
                    window.setTimeout(function () { window.location = '/Pages/BaseFramework/Security/Login.aspx'; }, 2000);
                }
            },
            error: function (xhr, status, error) {
                var err = eval("(" + xhr.responseText + ")");
                console.log(err.Message);
            }
        });
    }
    var bName = navigator.appName;
    function textCount(taObj, Cnt, maxL) {
        objCnt = createObject(Cnt);
        objVal = taObj.value;
        if (objVal.length <= maxL) {
            if (objCnt) {
                if (bName == "Netscape") {
                    objCnt.textContent = maxL - objVal.length;
                }
                else {
                    objCnt.innerText = maxL - objVal.length;
                }
            }
        }
        else {
            objCnt.innerText = 0;
        }
        return true;
    }
    function Count(obj, long, id) {
        var maxlength = new Number(long);
        if ($(obj).val().length > maxlength) {
            obj.value = obj.value.substring(0, maxlength);
        }
        else {
            $('#lblCharacter').html(($(obj).val().length));
            $('#lblMessage').html(1);
        }
    }

    function ValidateAlert() {
        var fields = '';
        var Title = 'Please fill the following details ___________________ \n\n';
        if ($('#rdoMail').is(':checked') || $('#rdoMobile').is(':checked')) {
            if ($('#txtSubject').val() == '') {
                fields = fields + 'Subject \n';
            }
        }
        if ($('#rdoSMS').is(':checked')) {
            var ischkMail = $('#chkMail').is(':checked') ? 1 : 0;
            var isChkMobileApp = $('#chkMobile').is(':checked') ? 1 : 0;

            if (ischkMail == 1 || isChkMobileApp == 1) {
                if ($('#txtSubject').val() == '') {
                    fields = fields + 'Subject \n';
                }
            }

        }
        if ($('#txtMessage').val() == '') {
            fields = fields + 'Message \n';
        }
        if (fields.length > 0) {
            alert(Title + fields);
            return false;
        }
        else {
            var x = confirm('Do you want to send alert?');
            if (x == true) {
                SendAlert();
            }
        }
    }

    function SendAlert() {
        $.ajax({
            type: "POST",
            url: URL + "/GetSession",
            data: '{}',
            contentType: "application/json;charset=utf-8",
            dataType: "json",
            success: function (dt) {
                if (dt.d > 0) {
                    //LoadingMessage('Loading Details', '<b>Please Wait ...</b>');
                    var obj = {
                        "Subject": $('#txtSubject').val(),
                        "Message": $('#txtMessage').val(),
                        "OrgId": $('#drpOrganization').val()
                    };
                    $.ajax({
                        type: "POST",
                        url: URL + "/SendAlert",
                        data: JSON.stringify(obj),
                        contentType: "application/json;charset=utf-8",
                        dataType: "json",
                        success: function (data) {
                            var datas = data.d;
                            if (datas == 1) {
                                alert('An Alert has been sent sucessfully!!..');
                            }
                            else {
                                alert('Oops! Something went wrong..Please try again later.');
                            }
                            window.setTimeout(function () { CloseHlsPopup(); }, 400);
                        },
                        error: function (xhr, status, error) {
                            var err = eval("(" + xhr.responseText + ")");
                            console.log(err.Message);
                        }
                    });
                }
                else {
                    CloseHlsPopup();
                    LoadingMessage('Your session has expired, we are redirecting to login page', '<b>Please Wait ...</b>');
                    window.setTimeout(function () { window.location = '/Pages/BaseFramework/Security/Login.aspx'; }, 2000);
                }
            },
            error: function (xhr, status, error) {
                var err = eval("(" + xhr.responseText + ")");
                console.log(err.Message);
            }
        });
    }