function fnSaveValidate() {
    var himsValidator;
    himsValidator = [new hims_ctl_val("ctl00_CP_NameTextBox", "Name", "T", "", "", "", "", "", ""),
                        new hims_ctl_val("ctl00_CP_AssetCodeTextBox", "Code", "T", "", "", "", "", "", ""),
                        new hims_ctl_val("ctl00_CP_CategoryDropDownList", "Asset Category", "T", "", "", "", "", "", ""),
                        new hims_ctl_val("ctl00_CP_NoofRecordsTextBox", "Quantity", "T", "", "", "", "", "", ""),
                        new hims_ctl_val('ctl00_CP_ServiceEntryTextBoxDatePicker' + "_DateTextbox", "Service Entry", "T", "", "", "", ""),
                        new hims_ctl_val("ctl00_CP_MaintenancePeriodTextBox", "Maintenance Period(Days)", "T", "", "", "", "", "", ""),
                        new hims_ctl_val("ctl00_CP_MaintenanceAlertPeriodTextBox", "Maintenance Alert Period(Days)", "T", "", "", "", "", "", ""),
                        new hims_ctl_val("ctl00_CP_DeptDropDownList", "Current Department", "T", "", "", "", "", "", ""),
                        new hims_ctl_val("ctl00_CP_LocationDropDownList", "Current Location", "T", "", "", "", "", "", "")
    ];
    if (fnDoValidate(himsValidator))
        return true;
    else
        return false;
}


function fnValidateAssetCheckLog() {
    var himsValidator = [
                                new hims_ctl_val('ctl00_CP_AssetDropDownList', "Asset", "T", "", "", "", 20),
                                    new hims_ctl_val('ctl00_CP_NumberTextBox', "Number", "T", "", "", "", 20),
                                new hims_ctl_val('ctl00_CP_CheckedDateDatePickerControl_DateTextbox', "Check Date", "T", "", "", "", 20),
                                  new hims_ctl_val('ctl00_CP_CheckStatusIdDropDownList', "Check Status", "T", "", "", "", 20)


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



function fnValidateAssetIncharge() {
    var himsValidator = [
                                new hims_ctl_val('ctl00_CP_CodeTextbox', "Code", "T", "", "", "", 20),
                                    new hims_ctl_val('ctl00_CP_UserDropDownList', "Incharge User", "T", "", "", "", 20)



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

function fnValidateAssetCheck() {
    var himsValidator = [
                                new hims_ctl_val('ctl00_CP_CheckedLastDateDataPickerControl_DateTextbox', "Checked Date", "T", "", "", "", 20)




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

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

function OpenFileUpload() {
    if ($('#ctl00_CP_Assetimage').prop('src') === undefined) {
        $('#flUpload').click();
    } else {
        alert("Delete Asset Photo First...");
    }
}

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

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

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

    }
}

function ajaxFileUpload1() {
    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 ajaxFileUploadAzure1() {
    if (ValidateAttachmentImageDetails()) {
        if (ValidateAttachmentImageSize()) {
            $("#loading").ajaxStart(function () {
                $(this).show();
            }).ajaxComplete(function () {
                $(this).hide();
            });

            var ScreenTypeId = 37;
            var UserSourceTypeId = $('#ctl00_CP_UserSourceTypeHiddenField').val();
            var UserSourceId = $('#ctl00_CP_UserSourceIdHiddenField').val();
            var DocumentId = $('#ctl00_CP_AssetMasterHiddenField').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 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_UploadLabel").text("Uploaded Successfully");
                        $("#ctl00_CP_UploadLabelHiddenField").val("Uploaded Successfully");
                    }, 100);
                },
                error: function (response, status, e) {
                    window.setTimeout(function () {
                        CloseLoadingpopup();
                    }, 100);
                }
            })
            return false;
        }
    }
}
function ajaxFileUploadAzure() {
    if (ValidateImageDetails()) {
        if (ValidateImageSize()) {
            $("#loading").ajaxStart(function () {
                $(this).show();
            }).ajaxComplete(function () {
                $(this).hide();
            });

            var ScreenTypeId = 37;
            var UserSourceTypeId = $('#ctl00_CP_UserSourceTypeHiddenField').val();
            var UserSourceId = $('#ctl00_CP_UserSourceIdHiddenField').val();
            var DocumentId = $('#ctl00_CP_AssetMasterHiddenField').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: '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_UploadLabel").text("Uploaded Successfully");
                        $("#ctl00_CP_UploadLabelHiddenField").val("Uploaded Successfully");

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

function CloseLoadingpopup() {
    $('#dvMainLoadingmsg').hide();
}

function LoadingMessage(msg1, msg2) {
    $('#dvMainLoadingmsg').show();
    $('#spn1msg').html(msg1);
    $('#spn2msg').html(msg2);
    $('.hls-bg').show();
}
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 isFile(file) {
    var jpg = /\.(jpg|)$/i;
    var png = /\.(png|)$/i;
    var jpeg = /\.(jpeg|)$/i;
    var gif = /\.(gif|)$/i;
    if (jpg.test(file.toLowerCase()) || png.test(file.toLowerCase()) || jpeg.test(file.toLowerCase()) || gif.test(file.toLowerCase()))
        return true;
    else
        return false;
}

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() + ' MB');
            return false;
        }

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

function ValidateAttachmentImageDetails() {
    var fields;
    fields = "";
    var file = document.getElementById('FileUpload').value;
    var iSize = ($("#FileUpload")[0].files[0].size / 1024);
    iSize = (Math.round((iSize / 1024) * 100) / 100);
    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|pdf|)$/i).test(file.toLowerCase())) {
        return true;
    }
    else {
        return false;
    }
}