function fnValidateAssignTask() {
    var himsValidator = [new hims_ctl_val('ctl00_CP_AssignTaskTabContainer_CircularDetails_AssignedDatePickerControl' + '_DateTextbox', "Assigned Date", "T", "", "", "", 10),
		new hims_ctl_val('ctl00_CP_AssignTaskTabContainer_CircularDetails_DueDatePickerControl' + '_DateTextbox', "Due Date", "T", "", "", "", 10),
		new hims_ctl_val("ctl00_CP_AssignTaskTabContainer_CircularDetails_SourceCodeTextBox", "ID No / Code", "T", "", "", "", 20),
		new hims_ctl_val("ctl00_CP_AssignTaskTabContainer_CircularDetails_RemarkTextBox", "Remark", "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]);
        filename = input.files[0].name;
        if ($('#ctl00_CP_AssignTaskTabContainer_CircularDetails_UploadConfigurationHiddenField').val() == "Azure") {
            ajaxFileUploadAzure();
        }
        else {
            ajaxFileUpload();
        }
    }
}
function OpenFileUpload() {
    $('#flUpload').click();
}

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;
                    }
                    /*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_AssignTaskTabContainer_CircularDetails_Label3").text("Uploaded Successfully");
                    }, 100);
                },
                error: function (response, status, e) {
                    window.setTimeout(function () {
                        CloseLoadingpopup();
                    }, 100);
                }
            })
            return false;
        }
    }
}

function ValidateImageDetails() {
    var fields;
    fields = "";
    var file = document.getElementById('flUpload').value;
    var iSize = ($("#flUpload")[0].files[0].size / 1024);
    iSize = (Math.round((iSize / 1024) * 100) / 100);
    //if (isFile(document.getElementById('flUpload').value) == false || document.getElementById('flUpload').value == null) {
    //    fields = fields + "\n Invalid file ";
    //}
    //else if (iSize > 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 iSize = ($("#flUpload")[0].files[0].size / 1024);
    //iSize = (Math.round((iSize / 1024) * 100) / 100);
   
    if ($('#ctl00_CP_AssignTaskTabContainer_CircularDetails_AttachmentSizeHiddenField').val() != '') {
        if (iSize > $('#ctl00_CP_AssignTaskTabContainer_CircularDetails_AttachmentSizeHiddenField').val()) {
            alert('File should be less than ' + $('#ctl00_CP_AssignTaskTabContainer_CircularDetails_AttachmentSizeHiddenField').val() + ' KB');
            return false;
        }
        else {
            return true;
        }
    }
    else {
        return true;
    }
}
function LoadingMessage(msg1, msg2) {
    $('#dvTestLoadingmsg').show();
    $('#spn1msg').html(msg1);
    $('#spn2msg').html(msg2);
    $('.hls-bg').show();
}
function CloseLoadingpopup() {
    $('#dvTestLoadingmsg').hide();
    $('.hls-bg').hide();
}

function isFile(file) {
    var video = /\.(xls|)$/i;
    var CSV = /\.(csv|)$/i;
    var xlsx = /\.(xlsx|)$/i;
    var jpg = /\.(jpg|)$/i;
    var png = /\.(png|)$/i;
    var jpeg = /\.(jpeg|)$/i;
    var gif = /\.(gif|)$/i;
    var pdf = /\.(pdf|)$/i;
    var pptx = /\.(pptx|)$/i;
    var ppt = /\.(ppt|)$/i;
    var docx = /\.(docx|)$/i;
    var doc = /\.(doc|)$/i;
    var txt = /\.(txt|)$/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.toLowerCase() == "jpg" || t.toLowerCase() == "jpeg" || t.toLowerCase() == "png" || t.toLowerCase() == "xls" || t.toLowerCase() == "csv" || t.toLowerCase() == "pdf" || t.toLowerCase() == "docx" || t.toLowerCase() == "doc" || t.toLowerCase() == "txt") {

            count_file = count_file + 1;
        }
        else {
            filename = fileValu.files[i].name;
            return false;
        }
    }

    //if (video.test(file) || CSV.test(file)   || jpg.test(file) || png.test(file) || jpeg.test(file) ||
    //    pdf.test(file) ||  docx.test(file) || doc.test(file) || txt.test(file))
    //    return true;
    //else
    //    return false;
}

function ajaxFileUploadAzure() {
    if (ValidateImageDetails()) {
        if (ValidateImageSize()) {
            LoadingMessage('Uploading', '<b>Please Wait</b>');
            $("#loading").ajaxStart(function () {
                $(this).show();
            }).ajaxComplete(function () {
                $(this).hide();
            });
            var ScreenTypeId = 29;
            var UserSourceTypeId = $('#ctl00_CP_AssignTaskTabContainer_CircularDetails_UserSourceTypeHiddenField').val();
            var UserSourceId = $('#ctl00_CP_AssignTaskTabContainer_CircularDetails_UserSourceIdHiddenField').val();
            var DocumentId = 0;
            if ($('#ctl00_CP_AssignTaskTabContainer_CircularDetails_TaskIdHiddenField').val() != '')
                DocumentId = $('#ctl00_CP_AssignTaskTabContainer_CircularDetails_TaskIdHiddenField').val();
            var DocumentName = filename;
            var GUID = 0;
            if ($('#ctl00_CP_AssignTaskTabContainer_CircularDetails_GUIDHiddenField').val() != '')
                GUID = $('#ctl00_CP_AssignTaskTabContainer_CircularDetails_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;
                    }
                    /*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_AssignTaskTabContainer_CircularDetails_Label3").text("Uploaded Successfully");

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