﻿
// CV Uploader Javascript
// **********************

    function cvUploader(websiteRootPath, controlID)
    {
        // Initialisation
        var
            _this = this,
            _xmlHTTP = null,
            _uploadProgressTimeoutID = 0,
            _location = window.location.toString();

        if (_location.indexOf("#") > -1)
            _location = _location.substr(0, _location.indexOf("#"));

        // Controls
        _this.controlID = controlID;
        _this.hfiUploadKey = $("#" + _this.controlID + "_hfiUploadKey");
        _this.fupCVJS = $("#" + _this.controlID + "_fupCVJS");
        _this.hanCancelUpload = $("#" + _this.controlID + "_hanCancelUpload");
        _this.hgcProgress = $("#" + _this.controlID + "_hgcProgress");

        $(_this.hgcProgress).width(0);

        // Divs
        _this.divRightColumn = $(".CVUpload .Column")[1];
        _this.divUpload = $(".CVUpload .Column .Hide")[0];
        _this.divProgress = $(".CVUpload .Column .Hide")[1];
        _this.divComplete = $(".CVUpload .Column .Hide")[2];

        $(_this.divUpload).removeClass("Hide");

        function _setupIFrame()
        {
            try
            {
                _this.ifrPostback = document.createElement("<iframe name=\"ifrPostback\">");
            }
            catch (_e)
            {
                _this.ifrPostback = document.createElement("iframe");
                _this.ifrPostback.name = "ifrPostback";
            }

            _this.ifrPostback.id = _this.controlID + "_ifrPostback";
            _this.ifrPostback.style.position = "absolute";
            _this.ifrPostback.style.top = "0px";
            _this.ifrPostback.style.left = "0px";
            _this.ifrPostback.style.display = "none";
            document.body.appendChild(_this.ifrPostback);
        }
        _setupIFrame();

        function _updateProgress()
        {
            if (typeof (XMLHttpRequest) != "undefined")
                _xmlHTTP = new XMLHttpRequest();
            else if (typeof (ActiveXObject) != "undefined")
                _xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
            else return;

            _xmlHTTP.open("GET", websiteRootPath + "uploadprogress.ashx?UploadKey=" + _this.hfiUploadKey[0].value + '&TS=' + new Date().getTime(), false);
            _xmlHTTP.send("");

            if (_xmlHTTP.status == 200)
            {
                if (_xmlHTTP.responseXML.documentElement.getAttribute("Empty") == "true")
                    _this.hanCancelUpload.click();
                else
                {
                    $(_this.hgcProgress).width(_xmlHTTP.responseXML.documentElement.getAttribute("TotalProgress") + "%");
                    if (_xmlHTTP.responseXML.documentElement.getAttribute("TotalProgress") == "100")
                    {
                        setTimeout(function ()
                        {
                            $(_this.fupCVJS).addClass("Hide");
                            $(_this.divUpload).addClass("Hide");
                            $(_this.divProgress).addClass("Hide");
                            $(_this.divComplete).removeClass("Hide");
                        }, 500);
                    }
                    else
                        _uploadProgressTimeoutID = setTimeout(_updateProgress, 100);
                }
            }
            _this.reset();
        }

        _this.fupCVJS.change(function ()
        {
            // Start Upload
            $(_this.fupCVJS).addClass("Hide");
            $(_this.divUpload).addClass("Hide");
            $(_this.divProgress).removeClass("Hide");
            $(_this.divComplete).addClass("Hide");
            document.forms[0].action = _location + (_location.indexOf("?") > -1 ? "&" : "?") + "UploadKey=" + _this.hfiUploadKey[0].value + "&FileUploading=true";
            document.forms[0].target = "ifrPostback";
            document.forms[0].submit();
            _uploadProgressTimeoutID = setTimeout(_updateProgress, 100);
        });

        _this.hanCancelUpload.click(function ()
        {
            clearTimeout(_uploadProgressTimeoutID);
            $(_this.fupCVJS).removeClass("Hide");
            $(_this.divUpload).removeClass("Hide");
            $(_this.divProgress).addClass("Hide");
            $(_this.divComplete).addClass("Hide");
        });

        _this.reset = function ()
        {
            document.forms[0].action = _location;
            document.forms[0].target = "_self";
        };
    }