﻿/**
 * SUBMODAL v1.5
 * Used for displaying DHTML only popups instead of using buggy modal windows.
 *
 * By Seth Banks
 * http://www.subimage.com/
 *
 * Contributions by:
 * 	Eric Angel - tab index code
 * 	Scott - hiding/showing selects for IE users
 *	Todd Huss - inserting modal dynamically and anchor classes
 *
 * Up to date code can be found at http://www.subimage.com/dhtml/subModal
 * 
 *
 * This code is free for you to use anywhere, just keep this comment block.
 */

// Popup code
var gPopupMask = null;
var gPopupContainer = null;
var gPopFrame = null;
var gPopupIsShown = false;

/**
 * Initializes popup code on load.	
 */
function initPopUp() {
	// Add the HTML to the body
	theBody = document.getElementsByTagName('BODY')[0];
	popmask = document.createElement('div');
	popmask.id = 'popupMask';
	popcont = document.createElement('div');
	popcont.id = 'popupContainer';
	popcont.style.zIndex = "10001";
	popcont.innerHTML = '' +
		'<div id="popupInner">' +
			'<div id="popupTitleBar">' +
				'<div id="popupTitle"></div>' +
				'<div id="popupControls">' +
					'<a href="#" id="div1" onclick="hidePopWin();">close</a>' +
				'</div>' +
			'</div>' +
			'<iframe src="" style="width:100%;height:100%;background-color:transparent;" scrolling="auto" frameborder="0" allowtransparency="true" id="popupFrame" name="popupFrame" width="100%" height="100%"></iframe>' +
		'</div>';
	theBody.appendChild(popmask);
	theBody.appendChild(popcont);
	
	gPopupMask = document.getElementById("popupMask");
	gPopupContainer = document.getElementById("popupContainer");
	gPopFrame = document.getElementById("popupFrame");	
	
}
//addEvent(window, "load", initPopUp);

 /**
	* @argument width - int in pixels
	* @argument height - int in pixels
	* @argument url - url to display
	* @argument returnFunc - function to call when returning true from the window.
	* @argument showCloseBox - show the close box - default true
	*/

function showDivWindow(url,title, width, height,top,left) {
	
	//Init page
	initPopUp();
    
	gPopupIsShown = true;
	gPopupMask.style.display = "block";
	gPopupContainer.style.display = "block";
	
	//Set page controls to be disabled
	disableControls();
	
	// calculate where to place the window on screen
	locateDivWindow(width, height,left,top);

	gPopupContainer.style.width = width + "px";
	gPopupContainer.style.height = height + "px";
	
	//setMaskSize();

	// need to set the width of the iframe to the title bar width because of the dropshadow
	// some oddness was occuring and causing the frame to poke outside the border in IE6
	gPopFrame.style.width = width + "px";
	gPopFrame.style.height = (height) + "px";
	
	// set the url
	gPopFrame.src = url;
	window.setTimeout("setPopTitle();", 600);
}
function showChangePwd() {
	//Init page
	initPopUp();
    
	gPopupIsShown = true;
	gPopupMask.style.display = "block";
	gPopupContainer.style.display = "block";
	
	//Set page controls to be disabled
	disableControls();
	
	// calculate where to place the window on screen
	locateDivWindow(585, 380);

	gPopupContainer.style.width = 585 + "px";
	gPopupContainer.style.height = 380 + "px";
	
	//setMaskSize();

	// need to set the width of the iframe to the title bar width because of the dropshadow
	// some oddness was occuring and causing the frame to poke outside the border in IE6
	gPopFrame.style.width = 585 + "px";
	gPopFrame.style.height = (380) + "px";
	
	// set the url
	gPopFrame.src = "ChangePassword.aspx";
	window.setTimeout("setPopTitle();", 600);
}
function showForgetAccountAndPassword() {
	
	//Init page
	initPopUp();
    
	gPopupIsShown = true;
	gPopupMask.style.display = "block";
	gPopupContainer.style.display = "block";
	
	//Set page controls to be disabled
	disableControls();
	// calculate where to place the window on screen
	locateDivWindow(585, 380);

	gPopupContainer.style.width = 585 + "px";
	gPopupContainer.style.height = 380 + "px";
	
	//setMaskSize();

	// need to set the width of the iframe to the title bar width because of the dropshadow
	// some oddness was occuring and causing the frame to poke outside the border in IE6
	gPopFrame.style.width = 585 + "px";
	gPopFrame.style.height = (380) + "px";
	
	// set the url
	gPopFrame.src = "ForgetAccountAndPassword.aspx";
	window.setTimeout("setPopTitle();", 600);
}
function showDivWindow2(url,title, width, height,top,left) {
	
	//Init page
	initPopUp();
    
	gPopupIsShown = true;
	gPopupMask.style.display = "block";
	gPopupContainer.style.display = "block";
	//Set page controls to be disabled
//	disableControls();
	
	// calculate where to place the window on screen
	locateDivWindow(width, height,left,top);

	gPopupContainer.style.width = width + "px";
	gPopupContainer.style.height = height + "px";
	
	//setMaskSize();

	// need to set the width of the iframe to the title bar width because of the dropshadow
	// some oddness was occuring and causing the frame to poke outside the border in IE6
	gPopFrame.style.width = width + "px";
	gPopFrame.style.height = (height) + "px";
	
	// set the url
	gPopFrame.src = url;
	window.setTimeout("setPopTitle();", 600);
}

//
var gi = 0;
function locateDivWindow(width, height,top,left) {
	if (gPopupIsShown == true) {
		if (width == null || isNaN(width)) {
			width = gPopupContainer.offsetWidth;
		}
		if (height == null) {
			height = gPopupContainer.offsetHeight;
		}
		
		//var theBody = document.documentElement;
		var theBody = document.getElementsByTagName("BODY")[0];
		
		//theBody.style.overflow = "hidden";
		var scTop = parseInt(getScrollTop(),10);
		
		var scLeft = parseInt(theBody.scrollLeft,10);
	
		//setMaskSize();
		
		var fullHeight = getViewportHeight();
		var fullWidth = getViewportWidth();

		if (left==null && top==null)
		{
		    gPopupContainer.style.top = ((fullHeight - height) / 2) + "px";
		    gPopupContainer.style.left = ((fullWidth - width) / 2) + "px";
		}
		else
		{
		    gPopupContainer.style.top = top + "px";
		    gPopupContainer.style.left = left + "px";
		}
		//alert(fullWidth + " " + width + " " + gPopupContainer.style.left);
	}
}
//addEvent(window, "resize", locateDivWindow);
//addEvent(window, "scroll", locateDivWindow);
//window.onscroll = locateDivWindow;


/**
 * Sets the size of the popup mask.
 *
 */
function setMaskSize() {
	var theBody = document.getElementsByTagName("BODY")[0];
			
	var fullHeight = getViewportHeight();
	var fullWidth = getViewportWidth();
	
	// Determine what's bigger, scrollHeight or fullHeight / width
	if (fullHeight > theBody.scrollHeight) {
		popHeight = fullHeight;
	} else {
		popHeight = theBody.scrollHeight;
	}
	
	if (fullWidth > theBody.scrollWidth) {
		popWidth = fullWidth;
	} else {
		popWidth = theBody.scrollWidth;
	}
	
	gPopupMask.style.height = popHeight + "px";
	gPopupMask.style.width = popWidth + "px";
}

/**
 * @argument callReturnFunc - bool - determines if we call the return function specified
 * @argument returnVal - anything - return value 
 */
function hidePopWin() {
	gPopupIsShown = false;
	var theBody = window.parent.document.getElementsByTagName("BODY")[0];
	theBody.style.overflow = "";
	//Restore page controls to be enabled
	restoreControls();
	gPopupMask =  window.parent.document.getElementById("popupMask");
	gPopupContainer = window.parent.document.getElementById("popupContainer");
	if (gPopupMask == null) {
		return;
	}
    window.parent.document.body.removeChild(gPopupMask);
     window.parent.document.body.removeChild(gPopupContainer);
    gPopupMask.style.display = "none";
    gPopupContainer.style.display = "none";
//    window.parent.location.href = window.parent.location.href; 
}
function hidePopWin2() {
	gPopupIsShown = false;
	var theBody = window.parent.document.getElementsByTagName("BODY")[0];
	theBody.style.overflow = "";
	//Restore page controls to be enabled
	gPopupMask =  window.parent.document.getElementById("popupMask");
	gPopupContainer = window.parent.document.getElementById("popupContainer");
	if (gPopupMask == null) {
		return;
	}
    window.parent.document.body.removeChild(gPopupMask);

    window.parent.location.href = window.parent.location.href; 
}
/**
 * Sets the popup title based on the title of the html document it contains.
 * Uses a timeout to keep checking until the title is valid.
 */
function setPopTitle() {
	return;
	if (window.frames["popupFrame"].document.title == null) {
		window.setTimeout("setPopTitle();", 10);
	} else {
		document.getElementById("popupTitle").innerHTML = window.frames["popupFrame"].document.title;
	}
}
function close(callReturnFunc) {
	gPopupIsShown = false;
	var theBody = document.getElementsByTagName("BODY")[0];
	theBody.style.overflow = "";
	
	//restoreTabIndexes();
	
	restoreControls();
	
	if (gPopupMask == null) {
		return;
	}
	gPopupMask.style.display = "none";
	gPopupContainer.style.display = "none";
	if (callReturnFunc == true && gReturnFunc != null) {
		// Set the return code to run in a timeout.
		// Was having issues using with an Ajax.Request();
		gReturnVal = window.frames["popupFrame"].returnVal;
		window.setTimeout('gReturnFunc(gReturnVal);', 1);
	}
	gPopFrame.src = gDefaultPage;
	// display all select boxes
	if (gHideSelects == true) {
		displaySelectBoxes();
	}
}

function disableControls() {
    var sWidth,sHeight;
    //sWidth=document.body.offsetWidth;//瀏覽器工作區域內頁面寬度
    //sHeight=document.body.offsetHeight;//瀏覽器工作區域內頁面寬度
    sWidth=screen.width;//屏幕寬度（垂直分辨率）
    sHeight=screen.height;//屏幕高度（垂直分辨率）
    var bgObj=document.createElement("div");//創建一個div對象（背景層）
    bgObj.setAttribute('id','bgDiv');
    bgObj.style.position="absolute";
    bgObj.style.top="0";
    bgObj.style.background="#777";
    bgObj.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=3,opacity=25,finishOpacity=75";
    bgObj.style.opacity="0.6";
    bgObj.style.left="0";
    bgObj.style.width=sWidth + "px";
    bgObj.style.height=sHeight + "px";
    bgObj.style.zIndex = "10000";
    document.body.appendChild(bgObj);//在body內添加該div對象   
}

function restoreControls(){
    var bgObj=window.parent.document.getElementById("bgDiv");
    window.parent.document.body.removeChild(bgObj);//刪除背景層Div
}

var binfo = (function()
{
    var ua = navigator.userAgent.toLowerCase();
	return {
	    ie : /*@cc_on!@*/false,
		i7 : /*@cc_on!@*/false && (parseInt(ua.match(/msie (\d+)/)[1],10) >= 7)
	};
})();

var tool =
{
    getev : function()
	{
	    if(binfo.ie) return window.event;
		var func = tool.getev.caller;
		while( func != null )
		{
			var arg = func.arguments[0];
			if( arg && (arg + '').indexOf('Event') >= 0 ) return arg;
			func = func.caller;
		}
		return null;
	},
	getepos : function(o)
	{
		var l, t;
		if( o.getBoundingClientRect )
		{
			var el = o.getBoundingClientRect(); l = el.left; t = el.top;
		}
		else
		{
			l = o.offsetLeft - Math.max( document.documentElement.scrollLeft, document.body.scrollLeft );
			t = o.offsetTop - Math.max( document.documentElement.scrollTop, document.body.scrollTop );
		}
		return { x : l, y : t };
	}
};
