function fnValidateBook() {
    var himsValidator = [new hims_ctl_val('ctl00_CP_CodeTextBox', "Code", "T", "", "", "", 20),
					 new hims_ctl_val('ctl00_CP_TitleTextBox', "Name", "T", "", "", "", 20),
					 new hims_ctl_val('ctl00_CP_ResourceTypeDropDownList', "ResourceType", "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]);
        $('#ctl00_CP_Bookimage').attr('src', input.files[0]);
        ajaxFileUpload();
    }
}
function OpenFileUpload() {
    $('#flUpload').click();
}

function isFile(file) {

    var _gif = /\.(gif|)$/i;
    var _jpeg = /\.(jpeg|)$/i;
    var _jpg = /\.(jpg|)$/i;
    var _png = /\.(png|)$/i;
    var count = document.getElementById('flUpload').files;
    var fileValu = document.getElementById('flUpload');
    var count_file = 0;
    for (var i = 0; i < count.length; i++) {
        var t = fileValu.files[i].name.split('.').pop();
        if (t == "jpg" || t == "jpeg" || t == "png" || t == "gif") {
            count_file = count_file + 1;
        }
        else {
            filename = fileValu.files[i].name;
            return false;
        }
    }
}
function ValidateImageDetails() {
    var fields;
    fields = "";
    var file = document.getElementById('flUpload').value;
    var count = document.getElementById('flUpload').files;
    var iSize = 0;
    var Size = 0;
    for (var i = 0; i < count.length; i++) {
        iSize = ($("#flUpload")[0].files[i].size / 1024);
        iSize = (Math.round((iSize / 1024) * 100) / 100);
        Size = Size + iSize;
    }
    //if (isFile(document.getElementById('flUpload').value) == false || document.getElementById('flUpload').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 ValidateImageSize() {
    var fields;
    fields = "";
    var file = document.getElementById('flUpload').value;
    var count = document.getElementById('flUpload').files;
    var iSize = 0;
    var Size = 0;
    for (var i = 0; i < count.length; i++) {
        iSize = ($("#flUpload")[0].files[i].size / 1024);
        //iSize = (Math.round((iSize / 1024) * 100) / 100);
        Size = Size + iSize;
    }
 
    if ($('#ctl00_CP_AttachmentSizeHiddenField').val() != '') {
        if (Size > $('#ctl00_CP_AttachmentSizeHiddenField').val()) {
            alert('File should be less than ' + $('#ctl00_CP_AttachmentSizeHiddenField').val() + ' KB');
            return false;
        }

        else {
            return true;
        }
    }
    else
    {
        return true;
    }
}
function LoadingMessage(msg1, msg2) {
    $('#dvMainLoadingmsg').show();
    $('#spn1msg').html(msg1);
    $('#spn2msg').html(msg2);
    $('.hls-bg').show();
}
function CloseLoadingpopup() {
    $('#dvMainLoadingmsg').hide();
    $('.hls-bg').hide();
}
function ajaxFileUpload() {
    if (ValidateImageDetails()) {
        if (ValidateImageSize()) {
            LoadingMessage('Uploading', '<b>Please Wait</b>');
            $("#loading").ajaxStart(function () {
                $(this).show();
            }).ajaxComplete(function () {
                $(this).hide();
            });
            $.ajaxFileUpload({
                url: '/UserControls/MultiFileUpload/common-file-upload.ashx',
                secureuri: false,
                fileElementId: 'flUpload',
                dataType: 'text',
                contentType: "application/json; charset=utf-8",
                data: { name: 'logan', id: 'id', hdn: '1' },
                success: function (response, status) {
                    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_Label2").text("Uploaded Successfully");
                    }, 100);
                },
                error: function (response, status, e) {
                    window.setTimeout(function () {
                        CloseLoadingpopup();
                    }, 100);
                }
            })
            return false;
        }
    }
}