﻿String.prototype.format = function() {
    var args = arguments;
    return this.replace(/\{(\d+)\}/g, function(m, i) {
        return args[i];
    });
};
var Dialog = {
    url: function(p, a) {
        var b = "[\?&]" + p + "=([^&]*)";
        var c = new RegExp(b, "gi");
        if (!c.test(a)) return "";
        c.exec(a);
        return RegExp.$1
    },
    addEvent: function(o, a, b) {
        o.attachEvent ? o.attachEvent('on' + a, function() { b.call(o); }) : o.addEventListener(a, b, false);
    },
    UI: {
        Html: function(arg, v) {
            Dialog.Alert.Args = arg;
            var title = arg.Title ? arg.Title : '';
            var content = arg.Content ? arg.Content : '';
            var width = arg.Width ? arg.Width : 350;
            var Id = 'simulationLayer';
            var o = document.getElementById(Id);
            if (o == null) {
                o = document.createElement('div');
                o.style.width = width + 'px';
                o.setAttribute('id', Id);
                o.className = 'simulationLayer';
            }
            var s = [];
            var b = [];
            switch (v) {
                case 'error':
                case 'warning':
                case 'success':
                case 'confirm':
                case 'tooltip':
                    b.push('        <div class="simulationIco {0}"></div>'.format(v));
                    b.push('        <div class="simulationDialog">{0}</div>'.format(unescape(content)));
                    break;
                case 'iframe':
                    b.push("<iframe frameBorder=\"0\" name=\"iframe_parent\" id=\"iframe_parent\"  marginHeight=\"0\" marginWidth=\"0\" width=\"100%\" height=\"100%\"  src=\"" + arg.Href + "\"></iframe>");
                    break;
            }
            s.push('<div class="simulationLayers">');
            s.push('    <h3><label onclick="Dialog.UI.Remove({0})"></label>{1}</h3>'.format((v == 'confirm' ? false : true), unescape(title)));
            s.push('    <div class="simulations" id="f{0}">'.format(Id));
            s.push(b.join(''));
            s.push('    </div>');
            if (v != 'iframe') s.push('<div class="simulationState">{0}{1}</div>'.format('<input type="button" class="simulationOk" value="" onclick="Dialog.UI.Remove(true)" />', (v == 'confirm' ? '<input type="button" class="simulationCancel" value="" onclick="Dialog.UI.Remove(false)" />' : '')));
            s.push('</div>');
            o.innerHTML = s.join('');
            this.Append(o);
            this.Mask();
            var h = o.offsetHeight;
            var f = document.getElementById('f' + Id);
            if (v == 'iframe') {
                o.style.height = arg.Height + 'px';
                f.style.height = (arg.Height - 32) + 'px';
            } else {
                o.style.height = (h + 40) + 'px';
                f.style.height = (h + 8) + 'px';
            }
            this.Resize();
            Dialog.addEvent(window, "scroll", Dialog.UI.Resize);
            Dialog.addEvent(window, "resize", Dialog.UI.Resize);
//            var ieset = navigator.userAgent;
//            if (ieset.indexOf("MSIE 6.0") > -1)
//                setTimeout("document.frames('iframe_parent').location.reload();", 0);
            return o;
        },
        Mask: function() {
            var o = document.getElementById('msimulationLayer');
            if (o == null) {
                var ss = this.Size();
                var h = ss[0];
                var w = ss[1];
                o = document.createElement("div");
                o.setAttribute('id', 'msimulationLayer');
                o.style.cssText = "position:absolute;left:0;top:0;width:{0}px;height:{1}px;background:{2};z-index:999998;filter:alpha(opacity={3}0);opacity:0.{3};".format(w, h, 'black', 6);
                this.Append(o);
            }
        },
        Size: function() {
            var a = document.documentElement;
            var b = document.body;
            var c = Math.max;
            var d = window.innerWidth || c(b.clientWidth, a.clientWidth, b.offsetWidth, a.offsetWidth);
            var e = window.innerHeight || c(b.clientHeight, a.clientHeight, b.offsetHeight, a.offsetHeight);
            var f = c(b.scrollWidth, a.scrollWidth);
            var g = c(b.scrollHeight, a.scrollHeight);
            return [c(e, g), Math.min(d, f)];
        },
        Append: function(v) {
            var f = document.body.getElementsByTagName('form');
            if (f.length > 0) {
                f[0].appendChild(v);
            } else {
                document.body.appendChild(v);
            }
            return Dialog.UI.Append;
        },
        Resize: function() {
            var t = document.documentElement.scrollTop;
            var l = document.documentElement.scrollLeft;
            var w = document.documentElement.clientWidth;
            var h = document.documentElement.clientHeight;
            var v = 'simulationLayer';
            var o = document.getElementById(v);
            var f = document.getElementById('m' + v);
            if (Dialog.Iframe.FullScreen) {
                var ff = document.getElementById('f' + v);
                if (o) {
                    o.style.width = (w - 60) + "px";
                    o.style.height = (h - 60) + "px";
                }
                if (ff) {
                    ff.style.height = (h - 60) + "px";
                    ff.style.height = (h - 92) + "px";
                }
            }
            if (o) {
                o.style.top = (t + (h - o.offsetHeight) / 2) + 'px';
                o.style.left = (l + (w - o.offsetWidth) / 2) + 'px';
            }
            if (f) {
                var ss = Dialog.UI.Size();
                var hh = ss[0];
                var ww = ss[1];
                f.style.width = ww + 'px';
                f.style.height = hh + 'px';
            }
        },
        Remove: function(v) {
            Dialog.Alert.Args.Redirect && (top.location.href = Dialog.Alert.Args.Redirect);
            v && Dialog.Alert.Args.OK && Dialog.Alert.Args.OK();
            !v && Dialog.Alert.Args.Cancel && Dialog.Alert.Args.Cancel();
            var m = document.getElementById('msimulationLayer');
            var o = document.getElementById('simulationLayer');
            if (o) o.parentNode.removeChild(o);
            if (m) m.parentNode.removeChild(m);
        },
        ClickEvent: function(name) {
            var o = document.getElementById(name);
            if (!o) return false;
            if (document.all) {
                document.getElementById(name).click();
            }
            else {
                var evt = document.createEvent("MouseEvents");
                evt.initEvent("click", true, true);
                document.getElementById(name).dispatchEvent(evt);
            }
        },
        Resubmit: function(v) {
            var f = v.State;
            var t = v.ControlID;
            var e = v.ClickEvent;
            if (!f) return false;
            window[e] = function() {
                return true;
            };
            Dialog.UI.ClickEvent(t);
        }
    },
    Alert: {
        Args: false,
        Tooltip: function(arg) {
            Dialog.UI.Html(arg, 'tooltip');
        },
        Error: function(arg) {
            Dialog.UI.Html(arg, 'error');
        },
        Warning: function(arg) {
            Dialog.UI.Html(arg, 'warning');
        },
        Success: function(arg) {
            Dialog.UI.Html(arg, 'success');
        },
        Confirm: function(arg) {
            Dialog.UI.Html(arg, 'confirm');
        }
    }
};
Dialog.Iframe = {
    FullScreen: false,
    Init: function() {
        var s = document.getElementsByTagName("a");
        for (var i = 0; i < s.length; i++) {
            var o = s[i];
            var t = o.title || o.name || null;
            var a = o.href || o.alert;
            if (o.className.indexOf("dialog") != -1) {
                o.onclick = (function(t, a) {
                    return function() {
                        Dialog.Iframe.Execute(t, a);
                        this.blur();
                        return false;
                    }
                })(t, a);
            }
        }
    },
    Execute: function(title, href) {
        var r = /^[+-]?\d+(\.\d+)?$/;
        var width = Dialog.url('width', href);
        var height = Dialog.url('height', href);
        width = r.test(width) ? width : 0;
        height = r.test(height) ? height : 0;
        Dialog.Iframe.FullScreen = width == 0 || height == 0 ? true : false;
        width = width == 0 ? (document.documentElement.clientWidth - 60) : width;
        height = height == 0 ? (document.documentElement.clientHeight - 60) : height;
        Dialog.UI.Html({ Width: width, Height: height, Href: href, Title: title }, 'iframe');
    }
};

Dialog.addEvent(window, "load", Dialog.Iframe.Init);