function fnValidateLessonPlan() {
    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_SubjectDropDownList', "Subject", "T", "", "", "", 20),
        new hims_ctl_val('ctl00_CP_TopicDropDownList', "Topic", "T", "", "", "", 20),
        new hims_ctl_val('ctl00_CP_StartDatePickerControl', "Start Date", "T", "", "", "", 20),
        new hims_ctl_val('ctl00_CP_EndDateDatepickercontrol', "End Date", "T", "", "", "", 20)
    ];
    if (fnDoValidate(himsValidator))
        return true;
    else
        return 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 FormatIt(obj) {
    if (obj.value.length == 2)
        obj.value = obj.value + "/";
    if (obj.value.length == 5)
        obj.value = obj.value + "/";
}

var reader = new FileReader();
var filename = '';

function OpenFileUpload() {
    $('#FileUpload').click();
}

function readURL(input) {
    if (input.files && input.files[0]) {
        reader.readAsDataURL(input.files[0]);
        filename = input.files[0].name;
        if ($('#ctl00_CP_UploadConfigurationHiddenField').val() == "Azure") {
            ajaxFileUploadAzure();
        }
        else {
            ajaxFileUpload();
        }
    }
}


function ajaxFileUpload() {
    if (ValidateAttachmentImageDetails()) {
        if (ValidateAttachmentImageSize()) {
            $("#loading").ajaxStart(function () {
                $(this).show();
            }).ajaxComplete(function () {
                $(this).hide();
            });
            $.ajaxFileUpload({
                url: '/UserControls/MultiFileUpload/common-file-upload.ashx',
                secureuri: false,
                fileElementId: 'FileUpload',
                dataType: 'text',
                contentType: "application/json; charset=utf-8",
                data: { name: 'logan', id: 'id', hdn: '2' },
                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;
                    }
                    window.setTimeout(function () {
                       // CloseLoadingpopup();
                        $("#ctl00_CP_AttachmentUploadLabel").text("Uploaded Successfully");
                        $("#ctl00_CP_AttachmentUploadHiddenField").val("Uploaded Successfully");
                    }, 100);
                },
                error: function (response, status, e) {
                    window.setTimeout(function () {
                        //CloseLoadingpopup();
                    }, 100);
                }
            })
            return false;
        }
    }
}

function ajaxFileUploadAzure() {
    if (ValidateAttachmentImageDetails()) {
        if (ValidateAttachmentImageSize()) {
            $("#loading").ajaxStart(function () {
                $(this).show();
            }).ajaxComplete(function () {
                $(this).hide();
            });

            var ScreenTypeId = 38;
            var UserSourceTypeId = $('#ctl00_CP_UserSourceTypeHiddenField').val();
            var UserSourceId = $('#ctl00_CP_UserSourceIdHiddenField').val();
            var DocumentId = $('#ctl00_CP_IdHiddenField').val();
            var DocumentName = filename;
            var GUID = 0;
            if ($('#ctl00_CP_GUIDHiddenField').val() != '')
                GUID = $('#ctl00_CP_GUIDHiddenField').val();

            $.ajaxFileUpload({
                url: '/UserControls/MultiFileUpload/DocumentUpload.ashx?ScreenTypeId=' + ScreenTypeId + '&UserSourceTypeId=' + UserSourceTypeId + '&UserSourceId=' + UserSourceId + '&DocumentId=' + DocumentId + '&DocumentName=' + DocumentName + '&GUID=' + GUID,
                secureuri: false,
                fileElementId: 'FileUpload',
                dataType: 'text',
                contentType: "application/json; charset=utf-8",
                data: { name: 'logan', id: 'id', hdn: '2' },
                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;
                    }
                    window.setTimeout(function () {
                        //CloseLoadingpopup();
                        $("#ctl00_CP_AttachmentUploadLabel").text("Uploaded Successfully");
                        $("#ctl00_CP_AttachmentUploadHiddenField").val("Uploaded Successfully");

                    }, 100);
                },
                error: function (response, status, e) {
                    window.setTimeout(function () {
                        //CloseLoadingpopup();
                    }, 100);
                }
            })
            return false;
        }
    }
}

function ValidateAttachmentImageDetails() {
    var fields;
    fields = "";
    var file = document.getElementById('FileUpload').value;
    if (isAttachmentFile(document.getElementById('FileUpload').value) == false || document.getElementById('FileUpload').value == null) {
        fields = fields + "\n Invalid file ";
        alert(fields);
        return false;
    }
    else {
        return true;
    }
}   

function ValidateAttachmentImageSize() {
    var fields = "";
    var file = document.getElementById('FileUpload').value;
    var iSize = ($("#FileUpload")[0].files[0].size / 1024);
    iSize = (Math.round((iSize / 1024) * 100) / 100);
    if ($('#ctl00_CP_FileAttachmentSizeHiddenField').val() != '') {
        if (iSize > $('#ctl00_CP_FileAttachmentSizeHiddenField').val()) {
            alert('File should be less than ' + $('#ctl00_CP_FileAttachmentSizeHiddenField').val() + ' MB');
            return false;
        }
        else
            return true;
    }
    else
        return true;
}

function isAttachmentFile(file) {
    var jpg = /\.(jpg|)$/i;
    var png = /\.(png|)$/i;
    var jpeg = /\.(jpeg|)$/i;
    var doc = /\.(.doc|)$/i;
    var pdf = /\.(.pdf|)$/i;
    if ((/\.(jpg|jpeg|png|doc|docx|xls|pdf|)$/i).test(file.toLowerCase())) {
        return true;
    }
    else {
        return false;
    }
}