﻿// Begin. Upload files
var PROGRESS_INTERVAL = 500;
var PROGRESS_COLOR = '#7d000b';

var _divFrame;
var _divUploadMessage;
var _divUploadProgress;
var _ifrCV;

var _loopCounter = 1;
var _maxLoop = 10;
var _CVUploadProgressTimer;
// End. Upload files

function ValidateExtensions(file_name) {
    var extension = new Array();
    
    // Add the file name extensions that are okay (with 
    //    the period), for the variables with their numbers 
    //    in sequential order, as many or as few as needed, 
    //    starting with 0. (These are case sensitive.)

    extension[0] = ".doc";
    extension[1] = ".rtf";
    extension[2] = ".txt";
    extension[3] = ".pdf";
    extension[4] = ".docx";

    // Convert to Lower Case
    file_name = file_name.toLowerCase();
    
    // No other customization needed.
    var thisext = file_name.substr(file_name.lastIndexOf('.'));
    for (var i = 0; i < extension.length; i++) {
        if (thisext == extension[i]) { return true; }
    }
    return false;
}

function initCVUpload() {
    _divFrame = document.getElementById('divFrame');
    _divUploadMessage = document.getElementById('divUploadMessage');
    _divUploadProgress = document.getElementById('divUploadProgress');
    _ifrCV = document.getElementById('ifrCV');

    var btnUpload = _ifrCV.contentWindow.document.getElementById('btnUpload');

    btnUpload.onclick = function(event) {
        var filCV = _ifrCV.contentWindow.document.getElementById('filCV');
        _divUploadMessage.style.display = 'none';

        // Begin. Baisic validation for CV
        if (filCV.value.length == 0) {
            _divUploadMessage.innerHTML = '<span style=\"color:#7d000b\; font-size:9pt">Please specify the file.</span>';
            _divUploadMessage.style.display = '';
            filCV.focus();
            return;
        }

        if (!ValidateExtensions(filCV.value)){
            _divUploadMessage.innerHTML = '<span style=\"color:#7d000b\; font-size:9pt">Invalid file type. Only supports doc, docx, rtf, txt and pdf.</span>';
            _divUploadMessage.style.display = '';
            filCV.focus();
            return;
        }
        // End. Baisic validation for CV

        beginCVUploadProgress();
        _ifrCV.contentWindow.document.getElementById('CVUpload').submit();
        _divFrame.style.display = 'none';
    }
}

function beginCVUploadProgress() {
    _divUploadProgress.style.display = '';
    clearCVUploadProgress();
    _CVUploadProgressTimer = setTimeout(updateCVUploadProgress, PROGRESS_INTERVAL);
}

function clearCVUploadProgress() {
    for (var i = 1; i <= _maxLoop; i++) {
        document.getElementById('tdProgress' + i).style.backgroundColor = 'transparent';
    }

    document.getElementById('tdProgress1').style.backgroundColor = PROGRESS_COLOR;
    _loopCounter = 1;
}

function updateCVUploadProgress() {
    _loopCounter += 1;

    if (_loopCounter <= _maxLoop) {
        document.getElementById('tdProgress' + _loopCounter).style.backgroundColor = PROGRESS_COLOR;
    }
    else {
        clearCVUploadProgress();
    }

    if (_CVUploadProgressTimer) {
        clearTimeout(_CVUploadProgressTimer);
    }

    _CVUploadProgressTimer = setTimeout(updateCVUploadProgress, PROGRESS_INTERVAL);
}

function CVUploadComplete(message, isError) {
    clearCVUploadProgress();

    if (_CVUploadProgressTimer) {
        clearTimeout(_CVUploadProgressTimer);
    }
    
    _divUploadProgress.style.display = 'none';
    _divUploadMessage.style.display = 'none';
    _divFrame.style.display = '';
    
    if (message.length) {
        var color = (isError) ? '#7d000b' : '#717174';

        _divUploadMessage.innerHTML = '<span style=\"color:' + color + '\;font-size:9pt">' + message + '</span>';
        _divUploadMessage.style.display = '';

        if (isError) {
            _ifrCV.contentWindow.document.getElementById('filCV').focus();
        }
    }
}