//<script language="javascript">
var popupOnUnload = window.onunload;
var popupOnFocus = window.onfocus;
var popupOnFocus2 = document.onfocusin;
var popupWin = new Array();
var popupModal = -1;
var _popupParentWindow = popupParentWindow = window;
var _ieTestSize = 100;

function popup(url, name, x, y, w, h, callback, init) {
// modelessWindow(url, name, x, y, w, h, s, r, callback, init) {
	return modelessWindow(url, name, null, x, y, w, h, true, true, callback, init);
}

function modelessWindow(url, name, title, x, y, w, h, s, r, callback, init) {
  if(!w) w = 100;
  if(!h) h = 100;
  
  var v = 'var _x='+(x == -1 ? (screen.availWidth - w)/2 : x)+";\n"+
      'var _y='+(y == -1 ? (screen.availHeight - h)/2 : y)+";\n"+
      'var _w='+w+";\n"+
      'var _h='+h+";\n";
	var f = 'left=0,top=0,width='+_ieTestSize+',height='+_ieTestSize+',menubar=0,toolbar=0,addresbar=0' +
			',resizable=' + (r ? '1' : '0');

	var id = popupCreate(url, name, title, v, f, s, callback, init);
	
	return id;
}

function modalWindow(url, name, title, x, y, w, h, s, r, callback, init) {
  if(!w) w = 100;
  if(!h) h = 100;
  
  var v = 'var _x='+(x == -1 ? (screen.availWidth - w)/2 : x)+";\n"+
      'var _y='+(y == -1 ? (screen.availHeight - h)/2 : y)+";\n"+
      'var _w='+w+";\n"+
      'var _h='+h+";\n";
	var f = 'left=0,top=0,width=0,height=0,menubar=0,toolbar=0,addresbar=0' +
			',resizable=' + (r ? '1' : '0');

	var id = popupCreate(url, name, title, f, s, callback, init);
	
	popupModal = id;

	return id;
}

function popupCreate(url, name, title, vars, feature, scroll, callback, init) {
	var w = null;
	var i = 0;
	
	if(!name || !name.length)
		name = 'popup';
		
	if(!feature || !feature.length) {
	  feature = 'scrolling=0';
	} else {
	  feature += ',scrolling=0';
	}
	
		
	i = popupWin[name];
	if(i || i == 0) {
		w = popupWin[i];
		if(w) w.done();
		
		w = window.open("about:blank", name, feature);
		popupWin[i] = w;
	} else {
		w = window.open("about:blank", name, feature);
		i = popupWin.length;
		popupWin[i] = w;
		popupWin[name] = i;
	}

	if(callback && callback.indexOf('(') > -1) {
		callback += ';';
	} else if(callback && callback.length) {
		callback = 'parentWindow.' + callback + '(window.frames["main"], callback.arguments);';
	} else {
		callback = null;
	}

	var d = w.document;

  if(vars && vars.length) {
    vars += ' function _initsize() {' +
        ' if(!window.outerWidth) {' +
				  ' if(!document.body) {' +
				  	'window.setTimeout("_initsize();", 100);' +
				  	'return;' +
				  ' }' +
          ' window.resizeTo('+_ieTestSize+', '+_ieTestSize+');' +
          ' _pw = '+_ieTestSize+'-document.body.clientWidth;' +
          ' _ph = '+_ieTestSize+'-document.body.clientHeight;' +
        ' } else {' +
          ' _pw = window.outerWidth - window.innerWidth;' +
          ' _ph = window.outerHeight - window.innerHeight;' +
        ' }' +
        ' window.moveTo(_x, _y);' +
        ' window.resizeTo(_w+_pw, _h+_ph);' +
        ' init();' +
      '}\n' +
      ' window.onload = _initsize;\n';
  } else {
    vars = '';
  }

	d.open();
	d.write('<html>' +
		'<head>' +
			'<title>' + ((title && title.length) ? title : document.title + ' - Window') + '</title>\n' +
			'<' + 'script language="javascript">\n' +
			'var srcWindow = 0;' +
			'var parentWindow = 0;' +
			'var popupInit = 0;' +
			'var id = ' + i + ';' +
			'var name = "' + name + '";\n' +
			'var skinCSS = null;\n' +
			'var importCSS = null;\n' +
			'var dhtml = null;\n' +
			' _pw = 0;\n' +
      ' _ph = 0;\n' +
			'function debug(o) {' +
				'var h = "";' +
				'for(var i in o) {' +
					'h += i + ": " + o[i] + "\\n";' +
				'}' +
				'alert(h);' +
			'}\n' + 
			'function kill() {' +
				'if(parentWindow) { srcWindow.popupClosed(id); }' +
				'window.close();' +
			'}\n' +
			'function init() {' +
				' if(!parentWindow || !srcWindow) {' +
					'window.setTimeout("init();", 100);' +
					'return;' +
				'}' +
				'if(popupInit) return;\n' +
				'popupInit = 1;' +
				((title && title.length) ? '' : 'document.title = parentWindow.document.title + " - " + main.document.title;') +
				((init && init.legth) ? 'srcWindow.' + init + '(window.frames["main"]);' : '') +
				'if(!window.dhtml) window.dhtml = parentWindow.dhtml;\n' +
				' setSkin();' +
			'}\n' +
			'function done() {' +
				'kill();' +
			'}\n'+
			'function callback() {' +
				(callback ? callback : '') +
			'}\n' +
			'function setSkin(strCSS) {' +
				' if(strCSS)' +
				' skinCSS = strCSS;' +
				' if(skinCSS && skinCSS.length && popupInit && window.frames["main"].setSkin)' +
				' window.frames["main"].setSkin(skinCSS);' +
			'}\n' +
			'function setCSS(strCSS) {' +
				' if(strCSS)' +
				' importCSS = strCSS;' +
				' if(importCSS && importCSS.length && popupInit && window.frames["main"].setCSS)' +
				' window.frames["main"].setCSS(importCSS);' +
			'}\n' +
			'function move(x,y) {' +
			'}\n' +
			'function resize(w,h) {' +
			  ' if(!w && window.outerWidth) { w = window.innerWidth; }' +
			  ' if(!w && document.body) { w = document.body.clientWidth; }' +
			  ' if(!h && window.outerHeight) { h = window.innerHeight; }' +
			  ' if(!h && document.body) { h = document.body.clientHeight; }' +
			  ' window.resizeTo(w+_pw, h+_ph);' +
			'}\n' +
			' window.onunload = kill;\n' +
			(vars.length ? vars : ' window.onload = init;\n') +
			'</' + 'script>' +
		'</head>\n' +
		'<frameset rows="*, 0" border=0 frameborder=0 framespacing=0>\n' +
			'<frame name="main" src="' + (url && url.length ? url : 'about:blank') + '" border=0 frameborder=0 framespacing=0 marginwidth=0 marginheight=0 scrolling="' + (scroll ? 'yes' : 'no' ) + '" noresize>\n' +
			'<frame src="about:blank" border=0 frameborder=0 framespacing=0 marginwidth=0 marginheight=0 scrolling="no" noresize>\n' +
		'</frameset>' +
		'<' + '/html>');
	d.close();
	w.dhtml = window.dhtml;
	
	window.setTimeout("popupInit(" + i + ");", 100);
	
	return i;
}

function popupInit(id) {
	if(popupWin[id].parentWindow == 0) {
		popupWin[id].srcWindow = window;
		popupWin[id].parentWindow = popupParentWindow;
		if(_popupParentWindow != popupParentWindow)
			popupParentWindow = _popupParentWindow;
	} else {
		window.setTimeout("popupInit(" + id + ");", 100);
	}
}

function popupClosed(id) {
	if(popupModal == id)
		popupModal = -1;
	popupWin[id] = null;
}

function popupFocus() {
	if(popupModal > -1) {
		if(popupWin[popupModal].focus) {
			popupWin[popupModal].focus();
		}
	}
	
	if(popupOnFocus)
		return popupOnFocus();
	
	return true;
}

function popupFocus2() {
	if(popupModal > -1) {
		if(popupWin[popupModal].focus) {
			popupWin[popupModal].focus();
		}
		if(popupWin[popupModal].focus) {
			popupWin[popupModal].focus();
		}
	}
	
	if(popupOnFocus2)
		return popupOnFocus2();
	
	return true;
}

function popupUnload() {
	for(var i = 0; i < popupWin.length; i++) {
		if(popupWin[i] && popupWin[i].kill) {
			popupWin[i].pwindow = 0;
			popupWin[i].kill();
			popupWin[i] = null;
		}
	}
	
	if(popupOnUnload)
		return popupOnUnload();
		
	return true;
}

function popupObject_OnClick(e) {
	popup(this.href, (this.target.toLowerCase() == "_blank" ? '' : this.target), this.popupLeft, this.popupTop, this.popupWidth, this.popupHeight);

	return false;
}

function aObject_MakePopup(width, height, left, top) {
	dhtml.quickNorm(this);

	this.popupWidth = width;
	this.popupHeight = height;
	this.popupLeft = left;
	this.popupTop = top;

	this.addEvent("click", popupObject_OnClick);
}

function proc_PopupEffect(a, doc) {
	var attr;

	var target = "";
	var para1 = "";
	var para2 = "";
	var para3 = "";
	var para4 = "";

	a = dhtml.quickNorm(a);

	if(attr = a.getAttribute("target"))
		target = attr;

	if(target && target.length) {
		if(attr = a.getAttribute("width"))
			para1 = parseInt(attr, 10);
		else
			para1 = 0;

		if(attr = a.getAttribute("height"))
			para2 = parseInt(attr, 10);
		else
			para2 = 0;

		if(attr = a.getAttribute("left"))
			para3 = parseInt(attr, 10);
		else
			para3 = 0;

		if(attr = a.getAttribute("top"))
			para4 = parseInt(attr, 10);
		else
			para4 = 0;

		if(para1 > 0 && para2 > 0)
			dhtml.setType(a, aObject_MakePopup, para1, para2, para3, para4);
	}
	
	return a;
}

if(window.dhtml) {
	dhtml.addTag('A', proc_PopupEffect);
}

window.onfocus = popupFocus;
window.onunload = popupUnload;
document.onfocusin = popupFocus2;

