var Upload = function () {
var fileID = 0;
function FormatBytes(bytes, decimals) {
if (bytes === 0) return '0 Byte';
var k = 1000;
var dm = decimals + 1 || 3;
var sizes = ['KB'];
var i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm));
}
function AddReceipt(objFile, objFileSize) {
var max = maxImageSize;
var message = invalidImageSize;
var filename = objFile.val().split('/').pop().split('\\').pop()
var filesize = objFileSize.files[0].size;
var filepath = objFile.val();
if (filesize >= max) {
objFile.val("");
alert(message);
event.preventDefault();
return false;
}
var result =
'
';
$("#UploadReceiptResult")
.append(result);
if (fileID < 1) fileID++;
RemoveReceipt();
PreviewReceipt(objFileSize);
}
function RemoveReceipt() {
$("#UploadReceiptResult").find(".remove").click(function () {
var id = $(this).closest("ul").attr("id");
$(this).closest(".form-group").remove();
$('.fileupload:eq(' + id + ')').val("");
if (fileID > 0) fileID--;
});
}
function PreviewReceipt(input) {
var path = "";
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
path = e.target.result;
//$('#profile-img-tag').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
$("#UploadReceiptResult").find(".receiptPreview").click(function () {
var img = new Image();
img.src = path;
var top = (screen.height / 2) - (250);
var left = (screen.width / 2) - (250);
var w = img.width;
var h = img.height;
var preview = window.open(path, "ReceiptPreviewWindow", 'scrollbars=no,menubar=no,width=' + "500" + ',height=' + "500" + ',resizable=no,toolbar=no,status=no,top=' + top + ",left=" + left);
});
}
return {
init: function () {
$("#AddReceipt").click(function () {
var count = $(".fileupload").length;
for (i = 0; i < count; i++) {
if ($(".fileupload").get(i).files.length === 0) {
$('.fileupload:eq(' + i + ')').trigger('click');
return false;
}
}
});
$(".fileupload").change(function () {
AddReceipt($(this), this);
});
},
}
}();
$(function () {
Upload.init();
});