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

function fnValidateVehicleMaster() {
	var himsValidator = [new hims_ctl_val('ctl00_CP_SearchTitleTextBox', "Name", "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 uppercase() {
	key = window.event.keyCode;
	if ((key > 0x60) && (key < 0x7B))
		window.event.keyCode = key - 0x20;
}  
function FormatIt(obj) {
	if (obj.value.length == 2)
		obj.value = obj.value + "/";
	if (obj.value.length == 5) 
		obj.value = obj.value + "/";
}

// #Start MantisId : 0032433

function OpenFileUploadGrid(documentid) {
    $('#NewUpload').click();
    DocId = documentid;
}

function readURLGrid(input) {
    if (input.files && input.files[0]) {
        reader.readAsDataURL(input.files[0]);
        filename = input.files[0].name;
        if ($('#UploadConfigurationHiddenField').val() == "Azure") {
            ajaxFileUploadAzure();
        }
        else {
            ajaxFileUploadGrid();
        }
    }
}
function ajaxFileUploadAzure() {
    if (ValidateImageDetailsgrid()) {
        if (ValidateDocument()) {
            LoadingMessage('Uploading', '<b>Please Wait</b>');
            $("#loading").ajaxStart(function () {
                $(this).show();
            }).ajaxComplete(function () {
                $(this).hide();
            });
            var ScreenTypeId = $('#ScreenIdHiddenField').val();
            var UserSourceTypeId = 0;
            var UserSourceId = 0;
            var DocumentId = 0;
            DocumentId = DocId;
            var DocumentName = filename;
            var GUID = 1;
            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: 'NewUpload',
                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();
                        $('#DocumentRefreshbutton').click();
                    }, 100);
                },
                error: function (response, status, e) {
                    window.setTimeout(function () {
                        CloseLoadingpopup();
                    }, 100);
                }
            })
            return false;
        }
    }
}

function ajaxFileUploadGrid() {
    if (ValidateImageDetailsgrid()) {
        if (ValidateDocument()) {
            LoadingMessage('Uploading', '<b>Please Wait</b>');
            $("#loading").ajaxStart(function () {
                $(this).show();
            }).ajaxComplete(function () {
                $(this).hide();
            });
            $.ajaxFileUpload({
                url: '/UserControls/MultiFileUpload/common-file-upload.ashx?DocId=' + DocId,
                secureuri: false,
                fileElementId: 'NewUpload',
                dataType: 'text',
                contentType: "application/json; charset=utf-8",
                data: { name: 'logan', id: 'id', hdn: '1' },
                success: function (response, status) {

                    window.setTimeout(function () {
                        CloseLoadingpopup();
                        $('#DocumentRefreshbutton').click();
                    }, 100);
                },
                error: function (response, status, e) {
                    window.setTimeout(function () {
                        CloseLoadingpopup();
                    }, 100);
                }
            })
            return false;
        }
    }
}

function ValidateImageDetailsgrid() {
    var fields;
    fields = "";
    var file = document.getElementById('NewUpload').value;
    var count = document.getElementById('NewUpload').files;
    var iSize = 0;
    var Size = 0;

    if (isAttachmentFile(document.getElementById('NewUpload').value) == false || document.getElementById('NewUpload').value == null) {
        fields = fields + "\n Invalid file ";
        alert(fields);
        return false;
    }

    for (var i = 0; i < count.length; i++) {
        iSize = ($("#NewUpload")[0].files[i].size / 1024);

        Size = iSize;
        var fileValue = document.getElementById('NewUpload');
    }

    if (fields != "") {
        fields = "Please fill in the following details:\n------------------------------------" + fields + filename;
        alert(fields);
        return false;
    }
    else {
        return true;
    }
}

function ValidateDocument() {
    var fields;
    fields = "";
    var file = document.getElementById('NewUpload').value;
    var count = document.getElementById('NewUpload').files;
    var iSize = 0;
    var Size = 0;
    for (var i = 0; i < count.length; i++) {
        iSize = ($("#NewUpload")[0].files[i].size / 1024);
        Size = iSize;
        var fileValu = document.getElementById('NewUpload');
        if ($('#AttachmentSizeHiddenField').val() != '') {

            if (Size > $('#AttachmentSizeHiddenField').val()) {
                alert('File should be less than ' + $('#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 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;
    }
}
// #End MantisId : 0032433