﻿
var EPA = function() {
    return {
        upload_file: function(id, p, ev) {

            var e;
            if (window.event)
                e = window.event.srcElement;
            else e = ev.target;

            var par = $(e).parent();
            if (par.find("iframe").size() > 0) {
                return;
            }
            par.find("a").hide();

            if (!p.uploadText) p.uploadText = "Upload this file";
            //par.append("<input type=file name='file" + id + "' id='file" + id + "'><div id='upl_" + id + "'><a href='#'>" + p.uploadText + "</a><iframe style='display:block' id='frame_" + id + "'></iframe></div>");
            par.append("<div id='upl_" + id + "'><div><iframe scrolling=no frameborder=0 marginheight=0 marginwidth=0 style='padding:0px;margin:0px;height:25px;width:400px;' id='frame_" + id + "'></iframe></div><a href='#'>" + p.uploadText + "</a></div>");
            var myframe = document.getElementById("frame_" + id);
            var formEls = "<input style='width:350px' type='file' name='file" + id + "' id='file" + id + "'>";
            var url = "/modules/common/upload.aspx"
            if (p.url) url = p.url;
            if (url.indexOf("?") == -1)
                url += "?";
            else
                url += "&";
            url += "uplid=" + id;
            if (p.objectType) url += "&objectType=" + p.objectType;
            if (p.objectID) url += "&objectID=" + p.objectID;
            if (p.documentType) url += "&documentType=" + p.documentType;
            if (p.elementID) url += "&elementID=" + p.elementID;

            var html = "<form style='margin:0px;padding:0px' method=post enctype='multipart/form-data' action='" + url + "'>" + formEls + "</form>";
            setTimeout(function() {
                myframe.contentWindow.document.body.innerHTML = html;
            }, 1);

            if (!document["clear_upload_element"]) {
                document["clear_upload_element"] = function(s, dn, fn) {
                    if (p.callback) p.callback(dn, fn);
                    var uplid = "#upl_" + s;
                    var spn = $(uplid + " span");
                    var upar = $(uplid).parent();
                    spn.html("Upload complete.");
                    var fout = function() {
                        spn.fadeOut("slow");
                        $(uplid).remove();
                        upar.find("a").show();
                    };
                    spn.fadeIn("fast", function() {
                        window.setTimeout(fout, 2000);
                    });
                    return false;
                }
            }

            par.find("div a").click(function() {
                var inpfile = myframe.contentWindow.document.getElementById("file" + id);
                if (inpfile.value == "") {
                    alert("Please select a file to upload");
                    return false;
                }
                $(myframe).hide();
                par.find("div:first").append("<span>&nbsp;<img src='/resources/images/loading-small.gif'></span>");
                par.find("div:first a").remove();
                //myframe.contentWindow.document.forms[0].appendChild(inpfile);
                myframe.contentWindow.document.forms[0].submit();
                return false;
            });
        }

    };
} ();
