
function checkitem(checkBox, textBox1) {
    var e = document.getElementById(textBox1.id);
    var f = document.getElementById(checkBox.id);
    if (f.checked == true) {
        e.disabled = true;
    }
    else {
        e.disabled = false;
    }
}

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

function readResourceURL(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 {
            ajaxFileUploadNew();
        }
        document.getElementById("file1").value = '';
    }
}

function OpenFileUpload() {
    document.getElementById("file1").click();
}



function ajaxFileUploadNew() {
    if (ValidateResourceDetails()) {

        $("#loading").ajaxStart(function () {
            $(this).show();
        }).ajaxComplete(function () {
            $(this).hide();
        });
        $.ajaxFileUpload({
            url: '/UserControls/MultiFileUpload/ClassWise_file_upload.ashx',
            secureuri: false,
            fileElementId: 'file1',
            dataType: 'text',
            contentType: "application/json; charset=utf-8",
            data: { name: 'logan', id: 'id', hdn: '1' },
            success:
                function SendAttachment() {
                    debugger;
                    var ChatId = ctl00_CP_GroupIdHiddenField.value;
                    var SourceTypeId = ctl00_CP_GroupSourceTypeIdHiddenField.value;
                    $.ajax({
                        type: "POST",
                        url: "ChatWindow.aspx/SendAttachment",
                        data: JSON.stringify({ "FileName": filename, "ChatId": parseInt(ChatId), "SourceTypeId": parseInt(SourceTypeId) }),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (result) {

                            var obj = (result.d);
                            if (obj != "-201" || obj != "-202") {
                                document.getElementById("lname").value = "";
                                SyncChat(1);
                            }
                        }
                    });
                }

        })
        return false;
    }
}
function ajaxFileUploadAzure() {
    if (ValidateResourceDetails()) {
        $("#loading").ajaxStart(function () {
            $(this).show();
        }).ajaxComplete(function () {
            $(this).hide();
        });
        var ScreenTypeId = 10;
        var UserSourceTypeId = ctl00_CP_UserSourceTypeIdHiddenField.value;
        var UserSourceId = ctl00_CP_UserSourceIdHiddenField.value;
        var DocumentId = 0;
        if ($('#ctl00_CP_GroupIdHiddenField.value').val() != '')
            DocumentId = ctl00_CP_GroupIdHiddenField.value;
        var DocumentName = filename;
        var GUID = 0;

        $.ajaxFileUpload({
            url: '/UserControls/MultiFileUpload/DocumentUpload.ashx?ScreenTypeId=' + ScreenTypeId + '&UserSourceTypeId=' + UserSourceTypeId + '&UserSourceId=' + UserSourceId + '&DocumentId=' + DocumentId + '&DocumentName=' + DocumentName + '&GUID=' + GUID,
            secureuri: false,
            fileElementId: 'file1',
            dataType: 'text',
            contentType: "application/json; charset=utf-8",
            data: { name: 'logan', id: 'id', hdn: '1' },
            success: function SendAttachment() {
                debugger;
                var ChatId = ctl00_CP_GroupIdHiddenField.value;
                var SourceTypeId = ctl00_CP_GroupSourceTypeIdHiddenField.value;
                $.ajax({
                    type: "POST",
                    url: "ChatWindow.aspx/SendAttachment",
                    data: JSON.stringify({ "FileName": filename, "ChatId": parseInt(ChatId), "SourceTypeId": parseInt(SourceTypeId) }),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (result) {

                        var obj = (result.d);
                        if (obj != "-201" || obj != "-202") {
                            document.getElementById("lname").value = "";
                            SyncChat(1);
                        }
                    }
                });
            }
        })
        return false;
    }
}

function ValidateResourceDetails() {
    var fields;
    fields = "";
    var file = document.getElementById('file1').value;
    var iSize = ($("#file1")[0].files[0].size / 1024);
    iSize = (Math.round((iSize / 1024) * 100) / 100);

    if (isResourceFile(document.getElementById('file1').value) == false || document.getElementById('file1').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 isResourceFile(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 txt = /\.(.txt|)$/i;
    var doc = /\.(.doc|)$/i;
    var pdf = /\.(.pdf|)$/i;
    var count = document.getElementById('file1').files;
    var fileValu = document.getElementById('file1');
    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() == "csv" || t.toLowerCase() == "xls" || t.toLowerCase() == "txt" || t.toLowerCase() == "doc" || t.toLowerCase() == "docx" || t.toLowerCase() == "pdf" || t.toLowerCase() == "mp4" || t.toLowerCase() == "ppt") {
            count_file = count_file + 1;
        }
        else {
            filename = fileValu.files[i].name;
            return false;
        }
    }

    //if ((/\.(jpg|jpeg|png|csv|xls|txt|doc|docx|pdf|)$/i).test(file))
    //    return true;
    //else
    //    return false;
}