function isNumberKey(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}
function fnValidateSetupMaster() {
    var himsValidator = [
						 new hims_ctl_val('ctl00_CP_CodeFindTextBox', "Id No.", "T", "", "", "", 20),
						 new hims_ctl_val('ctl00_CP_FirstNameTextBox', "First Name", "T", "", "", "", 20),
						 new hims_ctl_val('ctl00_CP_ClassIdDropDownList', "Class", "T", "", "", "", 20),
						 new hims_ctl_val('ctl00_CP_DivisionIdDropDownList', "Division", "T", "", "", "", 20),
						 new hims_ctl_val('ctl00_CP_StatusDropDownList', "Status", "T", "", "", "", 20),
						 new hims_ctl_val("ctl00_CP_AdmissionCategoryDropDownList", "Admission Category", "T", "", "", "", 20),
						 new hims_ctl_val('ctl00_CP_AdmissionDateDatePickerControl' + '_DateTextbox', "Admission Date", "T", "", "", "", 10),
						 new hims_ctl_val('ctl00_CP_GenderDropDownList', "Gender", "T", "", "", "", 20)
    ];

    if (fnDoValidate(himsValidator))
        return true;
    else
        return false;
}

function fnValidateSetupMasterAuto() {
    var himsValidator = [
						 new hims_ctl_val('ctl00_CP_FirstNameTextBox', "First Name", "T", "", "", "", 20),
						 new hims_ctl_val('ctl00_CP_ClassIdDropDownList', "Class", "T", "", "", "", 20),
						 new hims_ctl_val('ctl00_CP_DivisionIdDropDownList', "Division", "T", "", "", "", 20),
						 new hims_ctl_val('ctl00_CP_StatusDropDownList', "Status", "T", "", "", "", 20),
						 new hims_ctl_val("ctl00_CP_AdmissionCategoryDropDownList", "Admission Category", "T", "", "", "", 20),
						 new hims_ctl_val('ctl00_CP_AdmissionDateDatePickerControl' + '_DateTextbox', "Admission Date", "T", "", "", "", 10),
						 new hims_ctl_val('ctl00_CP_GenderDropDownList', "Gender", "T", "", "", "", 20)
    ];

    if (fnDoValidate(himsValidator))
        return true;
    else
        return false;
}

var reader = new FileReader();
var filename = '';
reader.onload = function (e) {
}


function readURL(input) {
    if (input.files && input.files[0]) {
        reader.readAsDataURL(input.files[0]);
        ajaxFileUpload(input);
    }
}
function OpenFileUpload() {
    $('#StudentFileUpload').click();
}

function readURL_Father(input) {
    if (input.files && input.files[0]) {
        reader.readAsDataURL(input.files[0]);
        ajaxFileUpload_Father(input);
    }
}
function OpenFileUpload_Father() {
    $('#FatherFileUpload').click();


}

function readURL_Mother(input) {
    if (input.files && input.files[0]) {
        reader.readAsDataURL(input.files[0]);
        ajaxFileUpload_Mother(input);
    }
}
function OpenFileUpload_Mother() {
    $('#MotherFileUpload').click();

}

function readURL_Guardian(input) {
    if (input.files && input.files[0]) {
        reader.readAsDataURL(input.files[0]);
        ajaxFileUpload_Guardian(input);
    }
}
function OpenFileUpload_Guardian() {
    $('#GuardianFileUpload').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(obj) {
    if (ValidateFileSize(obj)) {
        if (ValidateImageDetails(obj)) {
            LoadingMessage('Uploading', '<b>Please Wait</b>');
            $("#loading").ajaxStart(function () {
                $(this).show();
            }).ajaxComplete(function () {
                $(this).hide();
            });
            $.ajaxFileUpload({
                url: '/UserControls/MultiFileUpload/common-file-upload.ashx?FolderName=Student',
                secureuri: false,
                fileElementId: 'StudentFileUpload',
                dataType: 'text',
                contentType: "application/json; charset=utf-8",
                data: { name: 'logan', id: 'id', hdn: '1' },
                success: function (response, status) {
                    if (response == "-007") {
                        alert('Cannot upload file as the used space exceeds allocated space')
                        return
                    }
                    if (response == "-008") {
                        alert('Invalid Extention')
                        return
                    }
                    if (response == "-009") {
                        alert('Cannot include / or .. in a filename')
                        return;
                    }
                    /*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_Label2").text("Uploaded Successfully");
                    }, 100);
                },
                error: function (response, status, e) {
                    window.setTimeout(function () {
                        CloseLoadingpopup();
                    }, 100);
                }
            })
            return false;
        }
    }
}

function ajaxFileUpload_Father(obj) {
    if (ValidateFileSize(obj)) {
        if (ValidateImageDetails(obj)) {
            LoadingMessage('Uploading', '<b>Please Wait</b>');
            $("#loading").ajaxStart(function () {
                $(this).show();
            }).ajaxComplete(function () {
                $(this).hide();
            });
            $.ajaxFileUpload({
                url: '/UserControls/MultiFileUpload/common-file-upload.ashx?FolderName=Father',
                secureuri: false,
                fileElementId: 'FatherFileUpload',
                dataType: 'text',
                contentType: "application/json; charset=utf-8",
                data: {
                    name: 'logan', id: 'id', hdn: '1'
                },
                success: function (response, status) {
                    if (response == "-007") {
                        alert('Cannot upload file as the used space exceeds allocated space')
                        return
                    }
                    if (response == "-008") {
                        alert('Invalid Extention')
                        return
                    }
                    if (response == "-009") {
                        alert('Cannot include / or .. in a filename')
                        return;
                    }
                    /*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_Label2").text("Uploaded Successfully");
                    }, 100);
                },
                error: function (response, status, e) {
                    window.setTimeout(function () {
                        CloseLoadingpopup();
                    }, 100);
                }
            })
            return false;
        }
    }
}

function ajaxFileUpload_Mother(obj) {
    if (ValidateFileSize(obj)) {
        if (ValidateImageDetails(obj)) {
            LoadingMessage('Uploading', '<b>Please Wait</b>');
            $("#loading").ajaxStart(function () {
                $(this).show();
            }).ajaxComplete(function () {
                $(this).hide();
            });
            $.ajaxFileUpload({
                url: '/UserControls/MultiFileUpload/common-file-upload.ashx?FolderName=Mother',
                secureuri: false,
                fileElementId: 'MotherFileUpload',
                dataType: 'text',
                contentType: "application/json; charset=utf-8",
                data: { name: 'logan', id: 'id', hdn: '1' },
                success: function (response, status) {
                    if (response == "-007") {
                        alert('Cannot upload file as the used space exceeds allocated space')
                        return
                    }
                    if (response == "-008") {
                        alert('Invalid Extention')
                        return
                    }
                    if (response == "-009") {
                        alert('Cannot include / or .. in a filename')
                        return;
                    }
                    /*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_Label2").text("Uploaded Successfully");
                    }, 100);
                },
                error: function (response, status, e) {
                    window.setTimeout(function () {
                        CloseLoadingpopup();
                    }, 100);
                }
            })
            return false;
        }
    }
}


function ajaxFileUpload_Guardian(obj) {
    if (ValidateFileSize(obj)) {
        if (ValidateImageDetails(obj)) {
            LoadingMessage('Uploading', '<b>Please Wait</b>');
            $("#loading").ajaxStart(function () {
                $(this).show();
            }).ajaxComplete(function () {
                $(this).hide();
            });
            $.ajaxFileUpload({
                url: '/UserControls/MultiFileUpload/common-file-upload.ashx?FolderName=Guardian',
                secureuri: false,
                fileElementId: 'GuardianFileUpload',
                dataType: 'text',
                contentType: "application/json; charset=utf-8",
                data: { name: 'logan', id: 'id', hdn: '1' },
                success: function (response, status) {
                    if (response == "-007") {
                        alert('Cannot upload file as the used space exceeds allocated space')
                        return
                    }
                    if (response == "-008") {
                        alert('Invalid Extention')
                        return
                    }
                    if (response == "-009") {
                        alert('Cannot include / or .. in a filename')
                        return;
                    }
                    /*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_Label2").text("Uploaded Successfully");
                    }, 100);
                },
                error: function (response, status, e) {
                    window.setTimeout(function () {
                        CloseLoadingpopup();
                    }, 100);
                }
            })
            return false;
        }
    }
}


function isFile(file) {

    var _gif = /\.(gif|)$/i;
    var _jpeg = /\.(jpeg|)$/i;
    var _jpg = /\.(jpg|)$/i;
    var _png = /\.(png|)$/i;
    var count = document.getElementById('StudentFileUpload').files;
    var fileValu = document.getElementById('StudentFileUpload');
    var count_file = 0;
    for (var i = 0; i < count.length; i++) {
        var t = fileValu.files[i].name.split('.').pop();
        if (t.toLowerCase() == "jpg" || t.toLowerCase() == "jpeg") {
            count_file = count_file + 1;
        }
        else {
            filename = fileValu.files[i].name;
            return false;
        }
    }
    //if (_jpg.test(file) || _jpeg.test(file) )
    //    return true;
    //else
    //    return false;
}

function ValidateImageDetails(obj) {
    var fields;
    fields = "";
    var id = obj.id;
    var file = document.getElementById(id).value;
    var count = document.getElementById(id).files;
    var iSize = 0;
    var Size = 0;
    var iSizeMB = 0;
    var SizeLimit = $("input[type='hidden'][id$='ImageMaxSize']").val()
    for (var i = 0; i < count.length; i++) {
        iSize = (document.getElementById(id).files[i].size / 1024);
        iSizeMB = (Math.round((iSize / 1024) * 100) / 100);
        Size = iSizeMB;
        var fileValu = document.getElementById(id);
        //var validExtensions = ["jpg", "jpeg", "bmp", "png"]
        //var file1 = file.split('.').pop();
        //if (validExtensions.indexOf(file1) == -1) {
        //    alert("Image should be in format : " + validExtensions.join(', '));
        //    document.getElementById(id).value = "";
        //    return false;
        //}
        //else
        //{
        if (id == "StudentFileUpload") {
            document.getElementById('ctl00_CP_StudentImage').src = window.URL.createObjectURL(obj.files[0]);
            document.getElementById('ctl00_CP_StudentSuccessImageLabel').style.display = 'inherit';
        }
        else if (id == "FatherFileUpload") {
            document.getElementById('ctl00_CP_FatherImage').src = window.URL.createObjectURL(obj.files[0]);
            document.getElementById('ctl00_CP_FatherSuccessImageLabel').style.display = 'inherit';
        }

        else if (id == "MotherFileUpload") {
            document.getElementById('ctl00_CP_MotherImage').src = window.URL.createObjectURL(obj.files[0]);
            document.getElementById('ctl00_CP_MotherSuccessImageLabel').style.display = 'inherit';
        }

        else if (id == "GuardianFileUpload") {
            document.getElementById('ctl00_CP_GuardianImage').src = window.URL.createObjectURL(obj.files[0]);
            document.getElementById('ctl00_CP_GuardianSuccessImageLabel').style.display = 'inherit';
        }
        //}
    }
    if (document.getElementById(id).value == null) {
        fields = fields + "\n  Invalid file  ";
    }
    //if (Size > 1) {
    //    alert('File should be less than 1 MB');
    //    return false;
    //}
    if (fields != "") {
        fields = "Please fill in the following details:\n------------------------------------" + fields + filename;
        alert(fields);
        return false;
    }
    else {
        return true;
    }
}



function ValidateFileSize(obj) {

    var fields;
    fields = "";
    var id = obj.id;
    var file = document.getElementById(id).value;
    var count = document.getElementById(id).files;
    var iSize = 0;
    var Size = 0;
    var iSizeMB = 0;

    for (var i = 0; i < count.length; i++) {
        iSize = (document.getElementById(id).files[i].size / 1024);
        //iSizeMB = (Math.round((iSize / 1024) * 100) / 100);
        //Size = iSizeMB;
        var fileValu = document.getElementById(id);
        if ($('#ctl00_CP_AttachmentSizeHiddenField').val() != '') {
            if (iSize > $('#ctl00_CP_AttachmentSizeHiddenField').val()) {
                alert('File should be less than ' + $('#ctl00_CP_AttachmentSizeHiddenField').val() + ' KB');
                return false;
            }

            else {
                return true;
            }
        }
        else {
            return true;
        }
    }
}








