//function cpageLoad() {
//	var browser=navigator.userAgent.toLowerCase();
//	if(browser.indexOf('firefox')>=0){		
//		$("#ctl00_CP_FileUpload1").fileUpload({
//			'uploader' : '../../../UserControls/MultiFileUpload/uploader.swf',
//			'cancelImg': '../../../Images/cancel.png',
//			'script': '../../../UserControls/MultiFileUpload/Upload.ashx?ASPSESSID=' + $('#ctl00_CP_hdnSessionid').val(),
//			'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
//			'multi': false,
//			'auto': true
//		});
//	}
//	else {
//		$("#ctl00_CP_FileUpload1").fileUpload({
//			'uploader': '../../../UserControls/MultiFileUpload/uploader.swf',
//			'cancelImg': '../../../Images/cancel.png',
//			'script': '../../../UserControls/MultiFileUpload/Upload.ashx',
//			'fileExt': '*.jpg;*.jpeg;*.gif;*.png',
//			'multi': false,
//			'auto': true
//		});
//	}
//}

var DocId = "";
var FilePath = "";
var reader = new FileReader();
var filename = '';
var CodeName = '';
var AssignFor = '';
var fileUploadName = '';
var CandidateId = "";
var VacancyId = "";
reader.onload = function (e) {
}


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 doc = /\.(doc|)$/i;
    var txt = /\.(txt|)$/i;
    var jpg = /\.(jpg|)$/i;
    var png = /\.(png|)$/i;
    var jpeg = /\.(jpeg|)$/i;
    var gif = /\.(gif|)$/i;
    var pdf = /\.(pdf|)$/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() == "pdf" || t.toLowerCase() == "doc" || t.toLowerCase() == "docx") {
            count_file = count_file + 1;
        }
        else {
            filename = fileValu.files[i].name;
            return false;
        }
    }
    //if (count_file > 0) {
    //    return true;
    //}
    //else
    //    return false;
}

function ValidateImageDetails() {
    var fields;
    fields = "";
    var file = document.getElementById('flUpload').value;
    var count = document.getElementById('flUpload').files;
    var Size = 0;
    var SizeLimit = $("input[type='hidden'][id$='ImageMaxSize']").val()
    var iSizeKB = 0;

    for (var i = 0; i < count.length; i++) {
        iSizeKB = ($("#flUpload")[0].files[i].size / 1024);
        Size = (Math.round((iSizeKB / 1024) * 100) / 100);
        var fileValu = document.getElementById('flUpload');
        if (Size > 1) {
            alert('File ' + fileValu.files[i].name + ' should be less than 1 MB');
            return false;
        }
        else if (iSizeKB > SizeLimit) {
            alert('Image size not according to configuration');
            return false;
        }
    }
    //var filesize = this..size / 1024 / 1024;
    if (isFile(document.getElementById('flUpload').value) == false || document.getElementById('flUpload').value == null) {
        fields = fields + "\n Invalid file ";
    }
    if (fields != "") {
        fields = "Please fill in the following details:\n------------------------------------" + fields + filename;
        alert(fields);
        return false;
    }
    else {
        return true;
    }
}
function OpenFileUploadGrid2(documentid) {
    $('#DocumentFileUpload').click();
    DocId = documentid;

}
function readURLGrid2(input) {
    if (input.files && input.files[0]) {
        reader.readAsDataURL(input.files[0]);

        filename = input.files[0].name;
        if ($('#ctl00_CP_UploadConfigurationHiddenField').val() == "Azure") {
            ajaxFileUploadAzure2();
        }
        else {
            ajaxFileUploadGrid();
        }
    }
}
function isFileGrid(file) {
    var video = /\.(xls|)$/i;
    var CSV = /\.(csv|)$/i;
    var xlsx = /\.(xlsx|)$/i;
    var doc = /\.(doc|)$/i;
    var txt = /\.(txt|)$/i;
    var jpg = /\.(jpg|)$/i;
    var png = /\.(png|)$/i;
    var jpeg = /\.(jpeg|)$/i;
    var gif = /\.(gif|)$/i;
    var pdf = /\.(pdf|)$/i;

    var count = document.getElementById('NewUpload').files;
    var fileValu = document.getElementById('NewUpload');

    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() == "pdf" || t.toLowerCase() == "doc" || t.toLowerCase() == "docx") {
            count_file = count_file + 1;
        }
        else {
            filename = fileValu.files[i].name;
            return false;
        }
    }

}

function ajaxFileUploadGrid() {
    FilePath = $('#ctl00_CP_FilePathHiddenField').val();
    CandidateId = $('#ctl00_CP_CandidateIdHiddenField').val();
    VacancyId = $('#ctl00_CP_VacancyIdHiddenField').val();
    if (ValidateImageDetailsgrid()) {
        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?DocId=' + DocId + '&FilePath=' + FilePath + '&VacancyId=' + VacancyId + '&CandidateId= ' + CandidateId,
                secureuri: false,
                fileElementId: 'DocumentFileUpload',
                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_DocumentGridView_ctl02_DocumentRefreshbutton').click();
                    }, 100);
                },
                error: function (response, status, e) {
                    window.setTimeout(function () {
                        CloseLoadingpopup();
                    }, 100);
                }
            })
            return false;
        }
    }
}

function ajaxFileUploadAzure2() {
    FilePath = $('#ctl00_CP_FilePathHiddenField').val();
    CandidateId = $('#ctl00_CP_CandidateIdHiddenField').val();
    VacancyId = $('#ctl00_CP_VacancyIdHiddenField').val();
    if (ValidateImageDetailsgrid()) {
        if (ValidateImageSize()) {
            LoadingMessage('Uploading', '<b>Please Wait</b>');
            $("#loading").ajaxStart(function () {
                $(this).show();
            }).ajaxComplete(function () {
                $(this).hide();
            });

            var ScreenTypeId = $('#ctl00_CP_ScreenIdHiddenField').val();
            var UserSourceTypeId = $('#ctl00_CP_UserSourceTypeHiddenField').val();
            var UserSourceId = $('#ctl00_CP_UserSourceIdHiddenField').val();
            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 + '&FilePath=' + FilePath,
                secureuri: false,
                fileElementId: 'DocumentFileUpload',
                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_DocumentGridView_ctl02_DocumentRefreshbutton').click();
                        $("#ctl00_CP_Label2").text("Uploaded Successfully");

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

function checkallDocumnet(obj) {
    if ($(obj).is(':checked')) {
        $('#ctl00_CP_DocumentGridView input:checkbox').attr('checked', 'checked');
    }
    else
        $('input:checkbox').removeAttr('checked');
}
function OpenFileUploadDocument() {
    var a = $('#ctl00_CP_DocumentDropDownList').val();
    //alert(a);
    if (a > 0) {
        OpenFileUploadGrid(a);
    }
    else {
        alert('Please Select Document..!!');
    }
}
function ValidateImageDetailsgrid() {
    var fields;
    fields = "";
    var file = document.getElementById('DocumentFileUpload').value;
    var count = document.getElementById('DocumentFileUpload').files;
    var iSize = 0;
    var Size = 0;
    //for (var i = 0; i < count.length; i++) {
    //    iSize = ($("#DocumentFileUpload")[0].files[i].size / 1024);
    //    iSize = (Math.round((iSize / 1024) * 100) / 100);
    //    Size = iSize;
    //    var fileValu = document.getElementById('DocumentFileUpload');
    //    if (Size > 1) {
    //        alert('File ' + fileValu.files[i].name + ' should be less than 1 MB');
    //    }
    //}
    //if (isFileGrid(document.getElementById('DocumentFileUpload').value) == false || document.getElementById('DocumentFileUpload').value == null) {
    //    fields = fields + "\n Invalid file ";
    //}

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


}

function isFileGrid(file) {
    var video = /\.(xls|)$/i;
    var CSV = /\.(csv|)$/i;
    var xlsx = /\.(xlsx|)$/i;
    var doc = /\.(doc|)$/i;
    var txt = /\.(txt|)$/i;
    var jpg = /\.(jpg|)$/i;
    var png = /\.(png|)$/i;
    var jpeg = /\.(jpeg|)$/i;
    var gif = /\.(gif|)$/i;
    var pdf = /\.(pdf|)$/i;

    var count = document.getElementById('DocumentFileUpload').files;
    var fileValu = document.getElementById('DocumentFileUpload');

    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() == "pdf" || t.toLowerCase() == "doc" || t.toLowerCase() == "docx") {
            count_file = count_file + 1;
        }
        else {
            filename = fileValu.files[i].name;
            return false;
        }
    }

}

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

    if ($('#ctl00_CP_AttachmentSizeHiddenField').val() != '') {
        if (iSize > $('#ctl00_CP_AttachmentSizeHiddenField').val()) {
            alert('File should be less than ' + $('#ctl00_CP_AttachmentSizeHiddenField').val() + ' KB');
            return false;
        }
        else {
            return true;
        }
    }
    else {
        return true;
    }
}