// (c)2009 Alan Lawson

PDQ.popup=function(o){this.id=o.id;this.docId=o.docId;this.dForm=o.form;this.window=null;this.title=o.title;this.basicDialog=null;this.iFrame=null;this.iFrameRef=null;this.form=null;this.contentWindow=null;this.body=null;this.resized=false;this.centeredAfterResize=false;this.moved=false;this.url=null;this.openTask=null;this.displayTask=null;this.closeTask=null;this.resizeTask=null;this.initialCE=false;this.formLoaded=false;this.currentUrl='';}
PDQ.popup.prototype.init=function(o){var url=PDQ.portalDir+'/ajax-loader.gif?OpenImageResource';var s='';if(o.autoScroll===false&&o.resizable===false){}else{o.bodyStyle='overflow-x:hidden;overflow-y:auto;';}
o.bodyStyle='overflow-x:hidden;overflow-y:auto;';s+='overflow:hidden;';var nHtml='<IFRAME id='+this.id+'-dlg-iFrame name='+this.id+'-dlg-iFrame'+' src="'+url+'" '+' scrolling="no" '+' frameborder="no" width="100%" style="'+s+'"'+' onload="parent.PDQ.closePopupOnBack({fId: this.id});" '+'></IFRAME>';o.html=nHtml;if(typeof o.autoHeight=='undefined')o.autoHeight=false;if(typeof o.autoScroll=='undefined')o.autoScroll=true;o.id=this.id+'-dlg';if(typeof o.renderTo=='undefined')o.renderTo=document.body;o.title='Loading...';o.width=300;o.height=192;o.shadow='sides';if(Ext.isIE){o.shadowOffset=14;}else{o.shadowOffset=6;}
if(typeof o.resizable=='undefined')o.resizable=true;if(Ext.isGecko)o.animCollapse=false;PDQ.log('Opening window');this.window=new Ext.Window(o);if(this.initX){}
this.window.body.dom.style.overflowX='hidden';this.window.addListener('move',this.move,this,true);this.window.addListener('beforeclose',this.beforeclose,this,true);this.window.addListener('close',this.afterclose,this,true);this.window.addListener('resize',this.setIframeHeight,this,true);this.window.setHeight(192);this.window.setWidth(300);this.window.center();this.moveTBC=true;this.window.show(o.animateTarget);this.url=o.url;this.iFrame=document.getElementById(this.id+'-dlg-iFrame');if(Ext.isGecko){this.iFrameRef=window.frames[this.id+'-dlg-iFrame'].content;}else{this.iFrameRef=document.frames[this.id+'-dlg-iFrame'];}
this.basicDialog=this.window;var fn=function(){var t='';var t='';var t='';var t='';this.iFrame.src=this.url;if(this.title){this.window.setTitle(this.title);}}
this.displayTask=new Ext.util.DelayedTask();this.displayTask.delay(10,fn,this);return;if(o.width)this.openWidth=o.width;if(o.height)this.openHeight=o.height;var bodyEl=this.window.body;var iFrameDom=Ext.DomHelper.insertHtml('afterBegin',bodyEl.dom,nHtml);this.iFrameEl=getEl(iFrameDom);this.setIframeHeight();this.height=this.el.getHeight();var url=Page.portalDir+'/ajax-loader.gif?OpenImageResource';this.iFrameEl.dom.src=url;this.window.addListener('resize',this.setIframeHeight,this,true);}
PDQ.popup.prototype.getContentHeight=function(){var t='';var t='';var t='';var t='';if(Ext.isGecko){var fd=this.iFrame.contentDocument;if(this.iFrame.contentDocument===undefined)return null;if(Ext.isEmpty(this.iFrame.contentDocument))return null;if(this.iFrame.contentDocument.body===null)return null;this.contentWindow=this.iFrame;this.body=this.iFrame.contentDocument.body;}else{var fd=window.frames[this.id+'-dlg-iFrame'].document;this.contentWindow=this.iFrame.contentWindow;this.body=this.iFrame.contentWindow.document.body;}
this.form=fd.forms[0];var h=null;var hS=this.body.scrollHeight;var hO=this.body.offsetHeight;if(this.form){var hF=this.form.scrollHeight
if(h===null)h=hF;h=(hF>h)?hF:h;if(this.form.Query_String_Decoded){h+=12;}}
if(hO==0&&hS>hF&&hS>h){h=hS;}
if(Ext.isGecko){}else if(Ext.isOpera){}else if(Ext.isIE){}
return h;}
PDQ.popup.prototype.setIframeHeight=function(){if(this.iFrame===null)return;var hCn=this.getContentHeight();PDQ.log('*** (setIframeHeight) iFrame contentHeith '+hCn);if(hCn===null)return;if(typeof this.dForm=='undefined'){this.iFrame.height=hCn+10;}else if(this.dForm==''){this.iFrame.height=hCn+10;}else if(this.dForm.indexOf('_')>=0){this.iFrame.height=hCn;}else{this.iFrame.height=hCn+10;}
var sz=this.window.getSize();var h=this.window.getInnerHeight();if(h!=hCn){var hFR=this.window.getFrameHeight();var hN=hCn+hFR;}}
PDQ.popup.prototype.beforeclose=function(o){var p=this.window.getPosition();this.initX=p[0];this.initY=p[1];return true;}
PDQ.popup.prototype.close=function(o){this.window.close();}
PDQ.popup.prototype.afterclose=function(o){PDQ.messageBox.hide();PDQ.closePopTask=new Ext.util.DelayedTask();PDQ.closePopTask.delay(50,PDQ.closePopup,PDQ,[{id:this.id}]);PDQ.doMarkedRefreshes();}
PDQ.popup.prototype.resize=function(o){PDQ.log('Resizing popup');if(typeof o=='undefined'){var o={};}
var sz=this.window.getSize();var hP=sz.height;var wP=sz.width;var hFR=this.window.getFrameHeight();var wFR=this.window.getFrameWidth();var hScroll=this.getContentHeight();hScroll=(typeof hScroll=='undefined')?null:hScroll;if(hScroll===null){setTimeout(this.resize.createDelegate(this),100);return;}
this.iFrame.style.height=hScroll;PDQ.log('(resize) iFrame height set to '+hScroll);if(typeof this.form._field=='undefined'){}
if(this.form._editMode===undefined){var edit='';}else{var edit=this.form._editMode.value;}
if(edit=='1'&&this.form._WidthEdit){var wS=this.form._WidthEdit.value;if(wS==''){var wS=this.form._Width.value;}}else{if(this.form._Width){var wS=this.form._Width.value;}else{var wS=800;}}
wS=(wS=='')?600:eval(wS)+wFR;if(edit=='1'&&this.form._HeightEdit){var hS=this.form._HeightEdit.value;if(hS==''){var hS=this.form._Height.value;}}else{if(this.form._Height){var hS=this.form._Height.value;}else{var hS='';}}
if((this.id=='login'||this.name=='login')&&hS=='270')hS='200';var wC=wP;var hC=hScroll+hFR;if(hS!='')hC=eval(hS)+hFR;var x=this.window.getPosition()[0];var y=this.window.getPosition()[1];var hMax=Ext.lib.Dom.getViewportHeight()-50;var h=(hMax<hC)?hMax:hC;if(!this.centeredAfterResize){var pW=Ext.lib.Dom.getViewportWidth();var pH=Ext.lib.Dom.getViewportHeight();var xN=(pW-wS)/2;var yN=(pH-h)/2;yN=(Math.abs(y-yN)<10)?y:yN;this.window.setPosition(xN,yN);}
this.window.setSize(wS,h);PDQ.log('*** (resize) 1:Window height set to '+h);if(this.form){var sT=this.form.scrollTop;var hW=this.form.clientHeight;}else{var sT=this.iFrame.document.body.scrollTop;var hW=this.iFrame.document.body.clientHeight;}
if((hC-sT)<hW){sT=hC-hW;sT=(sT<0)?0:sT;if(this.form){this.body.scrollTop=sT;}else{this.iFrame.document.body.scrollTop=sT;}}
var sz=this.window.getSize();var hC=sz.height;var wC=sz.width;if(wC==0){setTimeout(function(){this.resize();}.createDelegate(this),5);return;}
this.setIframeHeight();if(this.resized){var xN=x-(wC-wP)/2;}else{var xN=x;this.resized=true;}
if(Math.abs((y-document.body.scrollTop)-(Ext.lib.Dom.getViewportHeight()-hP)/2)<20){yN=(Ext.lib.Dom.getViewportHeight()-hC)/2;yN=(Math.abs(y-yN)<10)?y:yN;}else{yN=y;}
if(this.initY){yN=this.initY;}
var pW=Ext.lib.Dom.getViewportWidth();var pH=Ext.lib.Dom.getViewportHeight();var pastRight=xN+wC-pW;if(pastRight>0){xN=xN-pastRight-2;}
var pastBottom=yN+hC-pH;if(PDQ.showLog&&(pastBottom+30)>0){yN=yN-pastBottom-30;}
if(PDQ.showLog&&(pastBottom+4)>0){yN=yN-pastBottom-4;}
xN=(xN<4)?4:xN;yN=(yN<4)?4:yN;yN=yN+document.body.scrollTop;if(!this.centeredAfterResize){this.window.center();this.centeredAfterResize=true;}else if(yN!=y){this.window.setPagePosition(xN,yN)}
if(Ext.isGecko&&typeof o.count=='undefined'){this.resizeTask=new Ext.util.DelayedTask();this.resizeTask.delay(700,this.resize,this,[{count:1}]);}
PDQ.log('Collapse/expand '+this.id+' to fix resize handles');}
PDQ.popup.prototype.move=function(){var t='';var t='';var t='';var t='';if(this.moved===null){this.moved=false;}else{this.moved=true;}}