
var gIEAgent = navigator.userAgent.toLowerCase();
var gIsOpera = (gIEAgent.indexOf("opera") != -1);
var gTitle = "";
var gIFrameNo = 0;

/* 弹出蒙板方式的对话框 */
function Dialog(w, h, title) 
{
    var width = w;
    var height = h;
    var title = title;
    gTitle = title;

    var dlgBoxHtml = '\
		<div id="divCurrentDialogBox" style="display:none;z-index:19999;width:' + width + 'px;">\
          <table width="' + (width) + 'px" height="' + (height) + 'px" border="0" cellspacing="0" cellpadding="0">\
            <tr height="30">\
              <td><img src="' + UrlBase + 'Images/Common/dialog_title_left.gif" width="9" height="30" /></td>\
              <td width="100%" background="' + UrlBase + 'Images/Common/dialog_title_bg.gif">\
                  <table width="100%" border="0" cellspacing="0" cellpadding="0">\
                    <tr>\
                      <td id="divCurrentDialogBoxTitle" style="font-size:14px; color:grey; font-weight:bold; text-decoration:none;">' + title + '</td>\
			          <td id="divCurrentDialogClose" align="right"><a href="#" onclick="javascript:new Dialog().close();"><img src="' + UrlBase + 'Images/Common/dialog_close.gif" width="24" height="30" border="0"></a></td>\
                    </tr>\
                  </table>\
               </td>\
              <td><img src="' + UrlBase + 'Images/Common/dialog_title_right.gif" width="9" height="30" /></td>\
            </tr>\
            <tr>\
              <td colspan="3" id="DialogBody" style="border-left:1px solid #aeaeae;border-right:1px solid #aeaeae; background:#f6f6f6;height:' + (height - 28) + 'px"></td>\
              </tr>\
            <tr height="9">\
              <td><img src="' + UrlBase + 'Images/Common/dialog_footer_left.gif" width="9" height="9" /></td>\
              <td background="' + UrlBase + 'Images/Common/dialog_footer_bg.gif"></td>\
              <td><img src="' + UrlBase + 'Images/Common/dialog_footer_right.gif" width="9" height="9" /></td>\
            </tr>\
          </table>\
		</div><div id="divCurrentDialogBoxShadow" style="display:none;z-index:19998;"></div>\
	';


    var iframeHtml = '\
		<iframe id="iframeDialogBG" name="iframeDialogBG" frameborder="0" marginheight="0" marginwidth="0" hspace="0" vspace="0" scrolling="no" style="position:absolute;z-index:19997;display:none;"></iframe>\
	';

    var bgHtml = '\
		<div id="divCurrentDialogBoxBG" style="position:absolute;top:0px;left:0px;width:100%;height:100%;"></div>\
	';

    this.init = function() {
        $('divCurrentDialog') ? $('divCurrentDialog').parentNode.removeChild($('divCurrentDialog')) : function() { };
        var divDialog = document.createElement('span');
        divDialog.id = "divCurrentDialog";
        if (!gIsOpera) {
            divDialog.innerHTML = bgHtml + iframeHtml + dlgBoxHtml;
        }
        else {
            divDialog.innerHTML = bgHtml + dlgBoxHtml;
        }
        document.body.appendChild(divDialog);
    }

    this.open = function(url) {
        this.show();
        var openIframe = "<iframe width='100%' height='100%' name='iframeDialogContent' id='iframeDialogContent' src='" + url + "' frameborder='0' scrolling='auto'></iframe>";
        jscomReplaceInnerHTML('DialogBody', openIframe);
    }

    this.show = function() {
        this.middle('divCurrentDialogBox');
        if ($('iframeDialogBG')) {
            $('iframeDialogBG').style.top = $('divCurrentDialogBox').style.top;
            $('iframeDialogBG').style.left = $('divCurrentDialogBox').style.left;
            $('iframeDialogBG').style.width = $('divCurrentDialogBox').offsetWidth + "px";
            $('iframeDialogBG').style.height = $('divCurrentDialogBox').offsetHeight + "px";
            $('iframeDialogBG').style.display = 'block';
        }
        if (!gIsOpera) {
            this.shadow();
        }
    }
    //刷新
    this.refresh = function() {
        this.close();
        parent.location.reload();
    }
    //重置
    this.reset = function() {
        this.close();
    }
    //打开其他页面
    this.navigate = function(url) {
        this.close();
        parent.location.href = url;
    }
    //关闭
    this.close = function() {
        if (window.removeEventListener) {
            window.removeEventListener('resize', this.event_b, false);
            window.removeEventListener('scroll', this.event_b, false);
        }
        else if (window.detachEvent) {
            try {
                window.detachEvent('onresize', this.event_b);
                window.detachEvent('onscroll', this.event_b);
            } catch (e) { }
        }
        if ($('iframeDialogBG')) {
            $('iframeDialogBG').style.display = 'none';
        }
        $('divCurrentDialogBox').style.display = 'none';
        $('divCurrentDialogBoxBG').style.display = 'none';
        $('divCurrentDialogBoxShadow').style.display = "none";
        if (typeof (parent.ondivCurrentDialogClose) == "function") {
            parent.ondivCurrentDialogClose($('divCurrentDialogBoxTitle').innerHTML);
        }
    }

    this.shadow = function() {
        this.event_b_show();
        if (window.attachEvent) {
            window.attachEvent('onresize', this.event_b);
            window.attachEvent('onscroll', this.event_b);
        }
        else {
            window.addEventListener('resize', this.event_b, false);
            window.addEventListener('scroll', this.event_b, false);
        }
    }

    this.event_b = function() {
        var oShadow = $('divCurrentDialogBoxShadow');

        if (oShadow.style.display != "none") {
            if (this.event_b_show) {
                this.event_b_show();
            }
        }
    }

    this.event_b_show = function() {
        var oShadow = $('divCurrentDialogBoxShadow');
        oShadow['style']['position'] = "absolute";
        oShadow['style']['display'] = "";
        oShadow['style']['opacity'] = "0.2";
        oShadow['style']['filter'] = "alpha(opacity=20)";
        oShadow['style']['background'] = "#000";
        var sClientWidth = parent ? parent.document.body.offsetWidth : document.body.offsetWidth;
        var sClientHeight = parent ? parent.document.body.offsetHeight : document.body.offsetHeight;
        var sScrollTop = parent ? (parent.document.body.scrollTop + parent.document.documentElement.scrollTop) : (document.body.scrollTop + document.documentElement.scrollTop);
        oShadow['style']['top'] = '0px';
        oShadow['style']['left'] = '0px';
        oShadow['style']['width'] = sClientWidth + "px";
        oShadow['style']['height'] = (sClientHeight + sScrollTop) + "px";
    }

    this.middle = function(_sId) {
        $(_sId)['style']['display'] = '';
        $(_sId)['style']['position'] = "absolute";

        var sClientWidth = parent.document.body.clientWidth;
        var sClientHeight = parent.document.body.clientHeight;
        var sScrollTop = parent.document.body.scrollTop + parent.document.documentElement.scrollTop;

        var sleft = (sClientWidth - $(_sId).offsetWidth) / 2;
        var iTop = sScrollTop + 150;
        var sTop = iTop > 0 ? iTop : 0;

        //是IFrame的子窗口
        if (parent.window != window) {
            sleft = "10";
            sTop = "10";
        }

        $(_sId)['style']['left'] = sleft + "px";
        $(_sId)['style']['top'] = sTop + "px";
    }
}

function jscomOpenPopDialog(url, width, height, title) 
{
    var dlg = new Dialog(width, height, title);
    dlg.init();
    dlg.open(url);
}

function jscomOpenAlert(word, button, width, height, title, jsAction) 
{
    return _openAlert(word, button, width, height, title, jsAction, "");
}

function _openAlert(word, button, width, height, title, jsAction, cssText) 
{
    var dlg = new Dialog(width, height, title);
    dlg.init();
    dlg.show();

    alert(dlg);
    
    var frameName = "iframeDialogContent_" + gIFrameNo++;
    var openIframe = "<iframe width='100%' height='100%' name='" + frameName + "' id='" + frameName + "' src='' frameborder='0' scrolling='no'></iframe>";
    jscomReplaceInnerHTML('DialogBody', openIframe);
    var iframe = window.frames[frameName];
    iframe.document.open();
    iframe.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n');
    iframe.document.write('<html xmlns="http://www.w3.org/1999/xhtml">\n');
    iframe.document.write('<head>\n');
    iframe.document.write('<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />\n');
    iframe.document.write('<link href="../App_Themes/Passport/Web.css" rel="stylesheet" type="text/css" />\n');
    if (cssText && cssText.length) {
        iframe.document.write('<style>\n');
        iframe.document.write(cssText + '\n');
        iframe.document.write('</style>\n');
    }
    iframe.document.write('</head>\n');
    iframe.document.write('<body>\n');
    if (jsAction == undefined) {
        jsAction = "new parent.Dialog().reset();";
    }
    iframe.document.write(_alertHtml(word, button, jsAction) + '\n');
    iframe.document.write('</body>\n');
    iframe.document.write('</html>\n');
    iframe.document.close();
}

function _alertHtml(word, button, jsAction) 
{
    var html = "";

    var html = '<div class="ts4">\
			<div class="ts45" style="border-top:none;padding-top:0;">\
				 ' + word + '\
				<div class="c"></div>\
			</div>\
			<div class="ts42 r">\
				<div class="rbs1"><input type="button" value="' + button + '" title="' + button + '" class="rb1-12" onmouseover="this.className=\'rb2-12\';" onmouseout="this.className=\'rb1-12\';" onclick="javascript:' + jsAction + '" /></div>\
				<div class="c"></div>\
			</div>\
		   </div>';

    return html;
}

