﻿
// CV Uploader Javascript
// **********************

    function cvUploader(controlID)
    {
        // Initialisation
        var 
            _this = this,
            _xmlHTTP = null,
            _uploadProgressTimeoutID = 0,
            _location = window.location.toString();

        _this.hasFile = false;

        if (_location.indexOf("#") > -1)
            _location = _location.substr(0, _location.indexOf("#"));

        // Controls
        _this.controlID = controlID;
        _this.hfiUploadKeyHiddenField = $("#UploadKeyHiddenField");
        _this.fupCVJS = $("#" + _this.controlID + "_fupCVJS");
        $(_this.fupCVJS).removeClass("Hide");
        _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", WEBSITE_ROOT_URL + "uploadprogress.ashx?UploadKeyHiddenField=" + _this.hfiUploadKeyHiddenField[0].value + '&TS=' + Math.floor(Math.random() * 1000000 + 1), 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");

                            while (_this.divComplete.childNodes.length)
                                _this.divComplete.removeChild(_this.divComplete.childNodes[0]);

                            var 
                                p = document.createElement("p"),
                                tn = document.createTextNode("File: " + _this.fileName),
                                br = document.createElement("br"),
                                a = document.createElement("a");

                            _this.divComplete.appendChild(p);
                            p.appendChild(tn);
                            p.appendChild(br);
                            a.appendChild(document.createTextNode("Remove it?"));
                            a.href = "#";
                            a.onclick = function (ev)
                            {
                                var evt = typeof event != "undefined" ? event : ev;
                                _this.resetUpload();
                                if (evt.preventDefault)
                                    evt.preventDefault();
                                evt.returnValue = false;
                                return false;
                            };
                            p.appendChild(a);

                            $(_this.divComplete).removeClass("Hide");

                            _this.hasFile = true;
                            _this.reset();

                        }, 500);
                    }
                    else
                        _uploadProgressTimeoutID = setTimeout(_updateProgress, 100);
                }
            }
        }

        _this.fupCVJS.change(function ()
        {
            // Start Upload
            if (_this.fupCVJS[0].value.length > 5)
            {
                if (
                    _this.fupCVJS[0].value.substr(_this.fupCVJS[0].value.length - 4).toLowerCase() == "docx" ||
                    _this.fupCVJS[0].value.substr(_this.fupCVJS[0].value.length - 3).toLowerCase() == "doc" ||
                    _this.fupCVJS[0].value.substr(_this.fupCVJS[0].value.length - 3).toLowerCase() == "pdf"
                   )
                {
                    _this.fileName = _this.fupCVJS[0].value.substr(_this.fupCVJS[0].value.lastIndexOf("\\") + 1);

                    $(_this.fupCVJS).addClass("Hide");
                    $(_this.divUpload).addClass("Hide");
                    $(_this.divProgress).removeClass("Hide");
                    $(_this.hgcProgress).width("0%");
                    $(_this.divComplete).addClass("Hide");
                    document.forms[0].action = _location + (_location.indexOf("?") > -1 ? "&" : "?") + "UploadKeyHiddenField=" + _this.hfiUploadKeyHiddenField[0].value + "&FileUploading=true";
                    document.forms[0].target = "ifrPostback";
                    document.forms[0].submit();
                    _uploadProgressTimeoutID = setTimeout(_updateProgress, 10);
                }
            }
        });

        _this.hanCancelUpload.click(function ()
        {
            clearTimeout(_uploadProgressTimeoutID);
            _uploadProgressTimeoutID = 0;
            _this.resetUpload();
        });

        _this.resetUpload = function ()
        {
            _this.resetForm();
            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";
        };

        _this.resetForm = function ()
        {
            var 
                bFreelance = $(".RoleType").length > 0 && $(".RadioButton-Freelance input").attr("checked"),
                bWorkPlacement = $(".RoleType").length > 0 && $(".RadioButton-WorkPlacement input").attr("checked"),
                bSpeculative = $(".RoleType").length > 0 && $(".RadioButton-Speculative input").attr("checked"),
                sFirstName = $(".TextBox-FirstName")[0].value,
                sSurname = $(".TextBox-Surname")[0].value,
                sEmail = $(".TextBox-Email")[0].value,
                sConfirmEmail = $(".TextBox-ConfirmEmail")[0].value,
                sTelephone = $(".TextBox-Telephone")[0].value;

            document.forms[0].reset();

            if ($(".RoleType").length > 0)
            {
                $(".RadioButton-Freelance input")[0].checked = bFreelance;
                $(".RadioButton-WorkPlacement input")[0].checked = bWorkPlacement;
                $(".RadioButton-Speculative input")[0].checked = bSpeculative;
            }

            $(".TextBox-FirstName")[0].value = sFirstName;
            $(".TextBox-Surname")[0].value = sSurname;
            $(".TextBox-Email")[0].value = sEmail;
            $(".TextBox-ConfirmEmail")[0].value = sConfirmEmail;
            $(".TextBox-Telephone")[0].value = sTelephone;

            _this.hasFile = false;
            _this.reset();


        };
    }
