//open new window
var __EMP_COMMON_TIMER;
var __EMP_COMMON_X;
var __EMP_COMMON_Y;
var __EMP_COMMON_TOP_WINDOW;
function __EMP_COMMON_GET_TOP_WINDOW() {
	var ow = window;
	while (ow.parent != null) {
		var owp = ow.parent;
		var ok = false;
		for (var i = 0; i < owp.frames.length; i++) {
			if (owp.frames[i].location.href == ow.location.href) {
				ok = true;
				ow = owp;
				break;
			}
		}
		if (ok == false) {
			break;
		}
	}
	//alert(ow.location);
	return ow;
}

//--------- class of frame------------
function __EMP_COMMON_WND(parentWindow) {
	this._TableFrame;
	this._TableContent;
	this._TableCaption;
	this._DivFrame;
//----------------------------
	this._Name;
//----------------------------	
	this.OffsetWidth = 4;
	this.OffsetHeight = 4;
	this.OffsetContentTop = 25;
	this.OffsetContentLeft = 5;
	this.OffsetContentBottom = 4;
	this.OffsetContentRight = 3;
//---------------------------------
	this._ParentWindow = parentWindow;
	this._OpenerWindow = window;
	this.OpendObject = document.all ? (window.event?window.event.srcElement:null) : null;
//---------------------------------
	this.CloseWindow=function(){
		this._DivFrame.style.display='none';
		if(this.OpendObject){
			this.OpendObject.focus();
		}
	}
	this.SetNewSize = function (width, height) {
		var contentWidth = width + this.OffsetContentLeft + this.OffsetContentRight;
		var contentHeight = height + this.OffsetContentTop + this.OffsetContentBottom;
		this._TableContent.width = contentWidth;
		this._TableContent.height = contentHeight;
		this._TableContent.rows[1].cells[0].height = height;
		this._TableContent.rows[1].cells[1].width = width;
		this._TableContent.rows[1].cells[1].height = height;
		this._TableContent.rows[1].cells[2].height = height;
		this._TableFrame.rows[0].cells[0].width = contentWidth;
		this._TableFrame.rows[0].cells[0].height = contentHeight;
		this._TableFrame.rows[2].cells[1].width = contentWidth;
		this._TableFrame.rows[1].cells[0].height = contentHeight;
		var divWidth = contentWidth + this.OffsetWidth * 2;
		var divHeight = contentHeight + this.OffsetHeight * 2;
		this._DivFrame.style.posHeight = divHeight;
		this._DivFrame.style.posWidth = divWidth;
	};
	this.Move = function (x, y) {
		this._DivFrame.style.posTop = y;
		this._DivFrame.style.posLeft = x;
	};
	this.MoveCenter = function (width, height) {
		var h1 = this._ParentWindow.document.body.clientHeight;
		var w1 = this._ParentWindow.document.body.clientWidth;
		var h2 = (h1 - height) / 2 - 20;
		var w2 = (w1 - width) / 2 - 20;
		if (w2 < 0) {
			w2 = 0;
		}
		if (h2 < 0) {
			h2 = 0;
		}
		this.Move(w2, h2);
	};
	this.getContentTd = function () {
		return this._TableContent.rows[1].cells[1];
	};
	this.GetIframeWindow = function () {
		var n1 = "__EMP_COMMON_IFRAME" + this._Name;
		var o1;
		if (document.all) {
			o1 = this._ParentWindow.frames[n1];
		} else {
			for (var i = 0; i < this._ParentWindow.frames.length; i = i + 1) {
				if (this._ParentWindow.frames[i].name == n1) {
					//alert(this._ParentWindow.frames[i].name);
					o1 = null;
					o1 = this._ParentWindow.frames[i];
					break;
				}
			}
		}
		return o1;
	};
	this.SetCaption = function (txtCaption) {
		this._TableCaption.rows[0].cells[0].innerHTML = txtCaption;
	};
	//设定链接地址
	this.SetUrl = function (url) {
		this._TableContent.rows[1].cells[1].childNodes[0].src = url;
	};
	this.Create = function (width, height, name) {
		this._Name = name;
		this._CreateDivFrame(width, height);
		this.ScrollMoveDiv();
		this._ParentWindow.document.body.onscroll = function () {
			__EMP_COMMON_CLASS_WND.ScrollMoveDiv();
		};
	};
	this._CreateDivFrame = function (width, height) {
		//var h1 = screen.availHeight;
		//var w1 = screen.availWidth;
		var h1 = this._ParentWindow.document.body.clientHeight;
		var w1 = this._ParentWindow.document.body.clientWidth;
		var h2 = (h1 - height) / 2 - 20;
		var w2 = (w1 - width) / 2 - 20;
		if (w2 < 0) {
			w2 = 0;
		}
		if (h2 < 0) {
			h2 = 0;
		}
		var objDiv0 = this._CreateDiv(width, height, h2, w2);
		objDiv0.setAttribute("_LocX", w2);
		objDiv0.setAttribute("_LocY", h2);
		objDiv0.id = "__EMP_COMMON_OPEND_WINDOW" + this._Name;
		this._CreateTableFrame(width, height);
		objDiv0.appendChild(this._TableFrame);
		this._DivFrame = objDiv0;
		this._ParentWindow.document.body.appendChild(this._DivFrame);
		objDiv0 = null;
	};
	this.ScrollMoveDiv = function () {
		var sx = this._ParentWindow.document.body.scrollLeft;
		var sy = this._ParentWindow.document.body.scrollTop;
		this._DivFrame.style.posTop = this._DivFrame.getAttribute("_LocY") * 1 + sy;
		this._DivFrame.style.posLeft = this._DivFrame.getAttribute("_LocX") * 1 + sx;
	};
	this._CreateDiv = function (width, height, top, left) {
		var obj = this._ParentWindow.document.createElement("DIV");
		obj.style.width = width + "px";
		obj.style.height = height + "px";
		obj.style.left = left + "px";
		obj.style.top = top + "px";
		obj.style.position = "absolute";
		return obj;
	};
		this._CreateTableFrame = function (width, height) {
		var tb = this._CreateTable();
		tb.onselectstart = function(){
			return false;
		}
		var frameWidth = width - this.OffsetWidth * 2;
		var frameHeight = height - this.OffsetHeight * 2;
		//------------- row1 -----------
		var tr = tb.insertRow(-1);
		//row1.td1
		var td = tr.insertCell(-1);
		td.colSpan = 2;
		td.rowSpan = 2;
		td.bgColor = "#EFF1F3";
		td.style.border = "1px solid gray";
		td.width = frameWidth;
		td.height = frameHeight;
		this._CreateTableContent(frameWidth, frameHeight);
		td.appendChild(this._TableContent);
	 
		//row1.td2
		td = null;
		td = tr.insertCell(-1);
		td.width = this.OffsetWidth;
		td.height = this.OffsetHeight;
		//----------- row2 shadow right-------------
		tr = null;
		tr = tb.insertRow(-1);
		//row2.td1
		td = null;
		td = tr.insertCell(-1);
		td.width = this.OffsetWidth;
		td.height = frameHeight;
		td.bgColor = "#C3CBCF"; //#C3CBCF
		td.style.filter = "alpha(opacity='70')";
		//------------ row3 --------------
		tr = null;
		tr = tb.insertRow(-1);
		//row3.td1
		td = null;
		td = tr.insertCell(-1);
		td.width = this.OffsetWidth;
		td.height = this.OffsetHeight;
		//row3.td2
		td = null;
		td = tr.insertCell(-1);
		td.width = frameWidth;
		td.height = this.OffsetHeight;
		td.bgColor = "#C3CBCF";	//shadow bottom
		td.style.filter = "alpha(opacity='70')";
		
		//row3.td3
		td = null;
		td = tr.insertCell(-1);
		td.width = this.OffsetWidth;
		td.height = this.OffsetHeight;
		td.bgColor = "#C3CBCF"; //shadow right-bottom
		td.style.filter = "alpha(opacity='70')";
		//---------
		this._TableFrame = tb;
		tb = null;
		tr = null;
		td = null;
	};
	this._CreateTableContent = function (width, height) {
		var contentWidth = width - this.OffsetContentLeft - this.OffsetContentRight;
		var contentHeight = height - this.OffsetContentTop - this.OffsetContentBottom;
		var tb = this._CreateTable();
		tb.width = width;
		tb.height = height;
		//row1
		var tr = tb.insertRow(-1);
		//row1.td1
		var td = tr.insertCell(-1);
		td.height = this.OffsetContentTop;
		td.width = this.OffsetContentLeft;
		//row1.td2
		td = null;
		td = tr.insertCell(-1);
		td.width = contentWidth;
		this._CreateTableCaption();
		td.appendChild(this._TableCaption);
		
		//row1.td3
		td = null;
		td = tr.insertCell(-1);
		td.width = this.OffsetContentRight;
		
		//row2
		tr = null;
		tr = tb.insertRow(-1);
		//row2.td1
		td = null;
		td = tr.insertCell(-1);
		td.height = contentHeight;
		//row2.td2		
		td = null;
		td = tr.insertCell(-1);
		td.height = contentHeight;
		td.width = contentWidth;
		td.innerHTML = "<iframe width=100% height=100% name='__EMP_COMMON_IFRAME" + this._Name + "' frameborder=0 src=\"about:blank\"></iframe>";
		//td.style.border = "1px solid black";
		td.style.borderTop = "1px solid #000000";
		td.style.borderLeft = "1px solid #000000";
		td.style.borderBottom = "1px solid #dcdcdc";
		td.style.borderRight = "1px solid #dcdcdc";
		td.bgColor = "white";
		td = null;
		td = tr.insertCell(-1);
		td.height = contentHeight;
		
		//row3
		tr = null;
		tr = tb.insertRow(-1);
		//row2.td1
		td = null;
		td = tr.insertCell(-1);
		td.height = this.OffsetContentBottom;
		td = null;
		td = tr.insertCell(-1);
		td.height = this.OffsetContentBottom;
		td = null;
		td = tr.insertCell(-1);
		td.height = this.OffsetContentBottom;
		this._TableContent = tb;
		tb = null;
		tr = null;
		td = null;
	};
	this._CreateTableCaption = function () {
		var tb = this._CreateTable();
		tb.width = "100%";
		tb.height = "100%";
		//row1
		var tr = tb.insertRow(-1);
		//row1.td1
		var td = tr.insertCell(-1);
		td.style.fontFamily = "Arial";
		td.style.fontSize = "12px";
		td.innerHTML = "..........";
		td.width = "99%";
		td.onmousedown = function () {
			var evt = this.document.parentWindow.event;
			this.setAttribute("__EMP_COMMON_MOUSE_DOWN", "1");
			this.setAttribute("__EMP_COMMON_X", evt.x);
			this.setAttribute("__EMP_COMMON_Y", evt.y);
		};
		td.onmouseup = function () {
			this.setAttribute("__EMP_COMMON_MOUSE_DOWN", "0");
		};
		td.onmouseout = function () {
			this.setAttribute("__EMP_COMMON_MOUSE_DOWN", "0");
		};
		td.onmousemove = function () {
			if (this.getAttribute("__EMP_COMMON_MOUSE_DOWN") != 1) {
				return;
			}
			var evt = this.document.parentWindow.event;
			var dx = evt.x - this.getAttribute("__EMP_COMMON_X") * 1;
			var dy = evt.y - this.getAttribute("__EMP_COMMON_Y") * 1;
			var obj = this.parentNode;
			while (!(obj.tagName == "DIV" && obj.id.indexOf("__EMP_COMMON_OPEND_WINDOW") == 0)) {
				obj = obj.parentNode;
			}
			obj.style.posLeft += dx;
			obj.style.posTop += dy;
			obj.setAttribute("_LocX", obj.getAttribute("_LocX") + dx);
			obj.setAttribute("_LocY", obj.getAttribute("_LocY") + dy);
			this.setAttribute("__EMP_COMMON_X", evt.x);
			this.setAttribute("__EMP_COMMON_Y", evt.y);
		};
		td.style.cursor = "pointer";
		td.onselectstart = function () {
			return false;
		};
		
		//row1.td2
		td = null;
		td = tr.insertCell(-1);
		td.align = "right";
		var img = td.ownerDocument.createElement("img");
		img.src = __EMP_CONTEXT_PATH + "/EMP_STYLE/images/open_window/but_1.gif";
		img.height = 18;
		img.style.cursor = "pointer";
		img.onmouseover = function () {
			var m = this.src.lastIndexOf(".");
			var s = this.src.substring(0, m);
			if (s.substring(s.length - 1).toUpperCase() == "C") {
				return;
			}
			s = s + "c" + this.src.substring(m);
			this.src = s;
		};
		img.onmouseout = function () {
			var m = this.src.lastIndexOf(".");
			var s = this.src.substring(0, m - 1);
			s = s + this.src.substring(m);
			this.src = s;
		};
		img.onclick = function () {
			var obj = this.parentNode;
			while (!(obj.tagName == "DIV" && obj.id.indexOf("__EMP_COMMON_OPEND_WINDOW") == 0)) {
				obj = obj.parentNode;
			}
			obj.style.display = "none";
			obj = null;
		};
		td.appendChild(img);
		img = null;
		this._TableCaption = tb;
		tb = null;
		tr = null;
		td = null;
	};
	this._CreateTable = function () {
		var table = this._ParentWindow.document.createElement("table");
		table.cellSpacing = 0;
		table.cellPadding = 0;
		table.border = 0;
		return table;
	};
	//child window location is changed
	this.ChildWindowChange = function (timerHandle) {
	};
}
//--------------OpenWindow----------------
var __EMP_COMMON_CLASS_WND;
var __EMP_COMMON_TIMER_OUT = 0;
var __EMP_COMMON_CHILD_HREF = "about.blank";
function OpenWindow(url, name, width, height, IsSelfWindow) {
	__EMP_COMMON_CLASS_WND = null;
	var topWindow;
	if (IsSelfWindow) {
		topWindow = window;
	} else {
		var topWindow = __EMP_COMMON_GET_TOP_WINDOW();
	}
	var pid = "__EMP_COMMON_OPEND_WINDOW" + name;
	var opendBeforeWindow = topWindow.document.getElementById(pid);
	if (opendBeforeWindow != null) {//remove old window
		topWindow.document.body.removeChild(opendBeforeWindow);
	}
	__EMP_COMMON_TOP_WINDOW = topWindow;
	topWindow.__EMP_COMMON_OPENER = window;
	var u1 = url;
	if (url.indexOf("?") > 0) {
		u1 += "&__gdx__=" + Math.random();
	}
	var mm = new __EMP_COMMON_WND(topWindow);
	mm.Create(width, height, name);
	mm.SetUrl(u1);
	__EMP_COMMON_CLASS_WND = mm;
	//if(u1.indexOf('/_es_/')<0){
	__EMP_COMMON_TIMER = window.setInterval(__EMP_COMMON_MOINTER, 255);
	//}
}
function __EMP_COMMON_MOINTER() {
	__EMP_COMMON_TIMER_OUT += 1;
	var oFrame = __EMP_COMMON_CLASS_WND.GetIframeWindow();
	if (oFrame == null) { //window closed
		window.clearInterval(__EMP_COMMON_TIMER);
		return;
	}
	if (oFrame.location.href == __EMP_COMMON_CHILD_HREF) {
		return;
	}
	if (oFrame.document.body != null && oFrame.document.body.innerHTML.length > 100) {
		__EMP_COMMON_CHILD_HREF = oFrame.location.href;
		__EMP_COMMON_SET_TITLE();
	}
}
function __EMP_COMMON_SET_TITLE() {
	var oFrame = __EMP_COMMON_CLASS_WND.GetIframeWindow();
	oFrame.__EMP_OPEND_DIV = __EMP_COMMON_CLASS_WND._Name;
	oFrame.__EMP_FROM_WINDOW = __EMP_COMMON_CLASS_WND._OpenerWindow;
	oFrame.__EMP_OPEND_OBJECT = __EMP_COMMON_CLASS_WND.OpendObject;
	__EMP_COMMON_CLASS_WND.SetCaption(oFrame.document.title);
	var buts = oFrame.document.getElementsByTagName("INPUT");
	var focus = false;
	oFrame.document.body.onunload = function () {
		__EMP_COMMON_CHILD_HREF = "";
	};
	oFrame.EmpCommonWindow=__EMP_COMMON_CLASS_WND;
	for (var i = 0; i < buts.length; i++) {
		if (focus == false && buts[i].type == "text" && 
			(buts[i].onblur == null || buts[i].onblur.toString().indexOf('__EMP_CHECK_VALID')>=0)) {
			try {
				buts[i].focus();
				focus = true;
			}
			catch (Exception) {
				focus = false;
			}
		}else if (buts[i].type == "button" &&
			(buts[i].value.indexOf("\u5173\u95ed")>=0 || buts[i].value.toUpperCase().indexOf("CLOSE")>=0)) {
			if (buts[i].onclick != null && buts[i].onclick.toString().indexOf("__emp_list_search") >= 0) {
				continue;
			}
			buts[i].setAttribute("__NN__", __EMP_COMMON_CLASS_WND._Name);
			buts[i].id = "__EMP_COMMON_CLOSE_BUTTON";
			buts[i].onclick = function () {
				__EMP_COMMON_CLASS_WND.CloseWindow();
			};
		}
	}
	var w = oFrame.document.getElementById("__EMP_FRAME");
	if (w != null) {
		oFrame.document.body.style.margin = "0px";
		oFrame.document.body.style.overflow = "hidden";
	
		var wa,ha;
		var w1=w.childNodes[0];
		wa=w1.clientWidth;
		ha=w1.clientHeight;
		if(wa<30 || ha<10){
			wa=w.scrollWidth;
			ha=w.scrollHeight;
		}
		if(!(wa<30 || ha<10)){
			__EMP_COMMON_CLASS_WND.SetNewSize(wa + 2, ha + 3);
			__EMP_COMMON_CLASS_WND.MoveCenter(wa + 2, ha + 3);
		}
	} else {
		var body = oFrame.document.body;
		var m = 0;
		var w = 0;
		for (var i = 0; i < body.childNodes.length; i++) {
			m += body.childNodes[i].clientHeight * 1;
			var w1 = body.childNodes[i].clientWidth * 1;
			if (w1 > w) {
				w = w1;
			}
		}
		//__EMP_COMMON_CLASS_WND.SetNewSize(w + 2, m + 3);
		//__EMP_COMMON_CLASS_WND.MoveCenter(w + 2, m + 3);
	}
}
function __EMP_COMMON_FIND_SELF(op) {
	return op.__EMP_COMMON_OPENER;
} 

