function gApp(){}
gApp.tabHeight=26;
gApp.pageHeight=0;
gApp.contentWidth=0;
gApp.panelWidth=0;
gApp.ie=true;
gApp.app=null;
gApp.panelHeaderHeight=0;
gApp.currentPanel;

function App(pWidth,hasToolbar,hasTabs,pHeight){
  gApp.panelHeaderHeight=pHeight;
  this.pages=new Array(0);
  this.panels=new Array(0);
  gApp.panelWidth=pWidth;
  var cHeight=document.body.clientHeight;
  if(cHeight==0)
    cHeight=document.body.offsetHeight;
  if(navigator.appName=="Netscape"){
    gApp.ie=false;
  }
  var cWidth=new Number(document.body.clientWidth)-10;
  gApp.pageHeight=cHeight;
  var i=pWidth.indexOf("%");
  if(i>0){
    i=new Number(panelWidth.substring(0,i));
    gApp.contentWidth=(100-i)*cWidth;
  }
  else {
    i=pWidth.indexOf("px");
    if(i>0){
      i=new Number(pWidth.substring(0,i));
    gApp.contentWidth=(cWidth-i+2);
    }
    else
      i=new Number(pWidth);
    gApp.contentWidth=(cWidth-i-10);
  }
  if(hasToolbar==true){
    this.toolbar=new AppToolbar(this);
 //   this.toolbar2=new AppToolbar2(this);
  }
  this.panelDiv=document.createElement("div");
  this.panelDiv.className="panel_";
  this.panelDiv.style.width=pWidth;
  this.panelDiv.style.overflow="hidden";
  this.panelDiv.style.position="absolute";
  this.panelDiv.style.top="10px";
  this.panelDiv.style.left="0";
  this.panelDiv.style.margin="0";
  this.panelDiv.style.padding="0";
  this.panelDiv.backgroundColor="white";
  this.panelDiv.style.zIndex="3";
  document.body.appendChild(this.panelDiv);
  gApp.app=this;
}



App.prototype.addPage=function(page){
  page.owner=this;
  var	newPages=new Array(this.pages.length+1);
  if(this.pages.length>0){
    for(var n=0;n<=this.pages.length;n++)
      newPages[n]=this.pages[n];
    }
  newPages[this.pages.length]=page;
  this.pages=newPages;
  document.body.appendChild(page.page);
}


App.prototype.addPanel=function(panel){
  panel.parent=this;
//  panel.panelHeader.style.width="100%";
//  panel.panelContent.style.width="100%";
  var panelPos=new Number("0");
  var	newPanels=new Array(this.panels.length+1);
  if(this.panels.length>0){
    for(var n=0;n<this.panels.length;n++){
      newPanels[n]=this.panels[n];
      panelPos+=this.panels[n].getHeaderHeight();
    }
  }
  panel.panelHeader.style.top=panelPos;
  newPanels[this.panels.length]=panel;
  this.panels=newPanels;
  this.panelDiv.appendChild(panel.panelHeader);
  this.panelDiv.appendChild(panel.panelContent);
  if(gApp.ie){
    panel.panelHeader.attachEvent("onclick",this.panelClicked);
    panel.panelContent.attachEvent("onclick",this.panelContentClicked);
    }
  else{
    panel.panelHeader.addEventListener("click",this.panelClicked,true);
    panel.panelContent.addEventListener("click",this.panelContentClicked,true);
    }
}

App.prototype.getHeight=function(){
  var cHeight=document.body.clientHeight;
  if(cHeight==0)
    cHeight=document.body.offsetHeight;
 gApp.pageHeight=cHeight;
 return cHeight;
}

App.prototype.getWidth=function(){
  var cWidth=new Number(document.body.clientWidth)-10;
  var i=gApp.panelWidth.indexOf("%");
  if(i>0){
    i=new Number(gApp.panelWidth.substring(0,i));
    cWidth=(100-i)*cWidth;
  }
  else {
    i=gApp.panelWidth.indexOf("px");
    if(i>0){
      i=new Number(gApp.panelWidth.substring(0,i));
    cWidth=(cWidth-i+2);
    }
    else
      i=new Number(gApp.panelWidth);
    gApp.contentWidth=(cWidth-i-10);
}
   return gApp.contentWidth;

}
App.prototype.getPanelDiv=function(){
return this.panelDiv;
}
App.prototype.getPageCount=function(){
return this.pages.length;
}
App.prototype.getPanelCount=function(){
return this.panels.length;
}

//App.prototype.hideMenu=function(){
//  gApp.app.controlTab.style.display="none";
//	gApp.app.menuTab.style.display="none";
//}

App.prototype.openPanel=function(panel,set){
// if(panel.panelText.lastChild.nodeValue==" NT Reports"){
	// if(globals.token==""){
		// alert("You must be logged in to use this tool");
		// return;
		// }
  // }
  if (set!=null && set!="undefined")
      panel.isOpen=!set;
  var vPos=0;
  if(this.panels.length>0){
    for(var n=0;n<this.panels.length;n++){
      if(this.panels[n]==panel){
//        alert(n + "-" + vPos);
        panel.setPos(vPos);
        panel.show(!panel.isOpen);
        vPos+=panel.getContentHeight();
      }
      else{
//        alert(n + "-" + vPos);
        this.panels[n].setPos(vPos);
        this.panels[n].show(false);
      }
      vPos+=gApp.panelHeaderHeight;
    }
    }
}


App.prototype.panelClicked=function(e){
  var p;
  if(gApp.ie){
    if(window.event.srcElement.id=="i"){
      p=window.event.srcElement;
      var z=p.previousSibling.lastChild.nodeValue.substring(1);
      window.open("help/" + z +".htm",z.substring(0,2) ,"height=750,width=500,left=400,top=100");
    }
    else{
      if(window.event.srcElement!="undefined"){
        if(window.event.srcElement.tagName=="DIV")
          p=window.event.srcElement.parent;
        else if(window.event.srcElement.tagName=="IMG")
          p=window.event.srcElement.parentElement.parentElement.parent;
        else
          p=window.event.srcElement.parentElement.parent;
      }
      p.parent.openPanel(p);
      if(p.mouseClickEvt!=null)
        p.mouseClickEvt();
    }
  }
  else{
    if(e.target.id=="i"){
      p=e.target;
      window.open("help/" + p.previousSibling.lastChild.nodeValue.substring(1) +
      ".htm",p.previousSibling.lastChild.nodeValue.replace(" ","_"),"height=750,width=500,left=400,top=100");
    }
    else{
      p=this.parent;
      p.parent.openPanel(p);
      if(p.workPage!=null && p.workPage!="undefined"){
        p.parent.showPage(p.workPage);
      }
      if(p.mouseClickEvt!=null)
      p.mouseClickEvt();
    }
  }

}

var panels;
App.prototype.panelContentClicked=function(e){
  var p;
  if(gApp.ie){
    if(window.event.srcElement!="undefined"){
      if(window.event.srcElement.tagName=="DIV")
        p=window.event.srcElement.parent;
      else
        p=window.event.srcElement.parentElement.parent;
     }
  }
  else
   p=this.parent;
 panels=gApp.app.panels;
  setTimeout(qx,200);
  // if the size of this has changed move the panels below

}

function qx(){
  var vPos=0;
  var move=false;
    for(var n=0;n<panels.length;n++){
      if(panels[n].isOpen==true){
        vPos+=panels[n].getContentHeight();
        move=true;
      }
      else if (move==true){
        panels[n].setPos(vPos);
      }
      vPos+=gApp.panelHeaderHeight;
    }
}

App.prototype.showPage=function(page){
for(var n=0;n<this.pages.length;n++){
    if(this.pages[n]==page)
      this.pages[n].show(true);
    else
      this.pages[n].show(false);
  }
}

App.prototype.showPanel=function(panel,set){
    if (set!=null && set!="undefined")
        panel.isVisible=set;
    this.openPanel(panel,set)
  }


function Panel(headerText,info){
//	alert(headerText);
  this.isVisible=true;
  this.isOpen=false;
  this.workPage;
  this.mouseClickEvt;
  this.panelHeader=document.createElement("div");
  this.panelImg=document.createElement("img");
  this.panelImg.src="images/ps.png";
  this.panelHeader.className="panelheader_";
//  this.panelHeader.style.position="absolute";
  this.panelHeader.style.left="0";
//  this.panelHeader.style.width="200px";
  this.panelHeader.style.cursor="pointer";
  this.panelHeader.style.margin="0";
  this.panelHeader.style.padding="4px";
  this.panelImg.style.paddingRight="1px";
  this.panelHeader.style.borderStyle="none";
  this.panelHeader.style.borderWidth="0px";
  this.panelHeader.parent=this;
  this.panelHeader.appendChild(this.panelImg);
  this.panelText=document.createElement("span");
  this.panelText.id="t";
  if(gApp.ie){
   this.panelText.style.width=145;
   }
  else{
    this.panelText.style.display="inline-block";
    this.panelText.style.minWidth=145;
   }
  this.panelText.innerHTML=headerText;
  this.panelHeader.appendChild(this.panelText);
  if(info==true){
    this.info=document.createElement("span");
    this.info.innerHTML="more info";
    this.info.id="i";
    this.info.style.fontFamily="Arial";
    this.info.style.fontFace="regular";
    this.info.style.fontStyle="italic";
	this.info.style.fontSize="7pt";
    this.info.style.color="#490B05";
    this.info.style.zIndex="-50";
    this.panelHeader.appendChild(this.info);
  }
  this.panelContent=document.createElement("div");
  this.panelContent.className="panelcontent_";
  this.panelContent.parent=this;
  this.panelContent.style.cursor="pointer";
  this.panelContent.style.left="0";
  this.panelContent.style.margin="0";
  this.panelContent.style.paddingLeft="7px";
}

Panel.prototype.addEvent=function(type,fn){
  if(type=="mouseClick"){
    this.mouseClickEvt=fn;
  }
}



Panel.prototype.getHeaderHeight=function(){
  if(this.isVisible)
    return this.panelHeader.clientHeight;
  else
    return 0;
}

Panel.prototype.getContent=function(){
return this.panelContent.firstChild;

} 


Panel.prototype.getContentHeight=function(){
  if (this.isOpen && this.isVisible)
    return this.panelContent.clientHeight;
  else
    return 0;
} 

Panel.prototype.hideHeader=function(bool){
this.isVisible=!bool;
}

Panel.prototype.setContentHtml=function(htmlText){
  this.panelContent.innerHTML=htmlText;
  
}
Panel.prototype.setContentUrl=function(url,ht,id){
    var srcDoc=document.createElement("iframe");
    srcDoc.id=id;
    srcDoc.style.height=ht;
//    srcDoc.style.overflow="auto";
    srcDoc.frameBorder=false;
    srcDoc.allowTransparency=true;
    srcDoc.style.margin="0";
    srcDoc.style.border="0";
    srcDoc.style.padding="0";
    srcDoc.style.width=gApp.panelWidth-10;
    srcDoc.src=url;
    this.panelContent.appendChild(srcDoc);  
}



Panel.prototype.setPos=function(pos){
  this.panelHeader.style.top=pos;
    this.panelContent.style.top=gApp.panelHeaderHeight+pos;
}

Panel.prototype.setWorkPage=function(page){
  this.workPage=page;
}



Panel.prototype.show=function(bool){
  this.isOpen=bool;
  if(bool){
    this.panelContent.style.display="block";
    this.panelImg.src="images/po.png";
    if(this.isVisible)
      this.panelHeader.style.display="block";
  }  
  else{
    this.panelContent.style.display="none";
    this.panelImg.src="images/ps.png";
    if(this.isVisible)
      this.panelHeader.style.display="block";
    else
      this.panelHeader.style.display="none";
  }
}


function Page(){
  this.page=document.createElement("iframe");
  this.page.className="page_";
  this.page.style.height=new Number(gApp.pageHeight)-55; //"100%";
  if(document.body.clientWidth<=0)
    this.page.style.width="67%";
  else
    this.page.style.width=gApp.contentWidth+10;
  this.page.style.backgroundColor="white";
  this.page.style.marginTop="70";
  this.page.style.padding="0";
  this.page.style.position="absolute";
  this.page.style.left=new Number(gApp.panelWidth)+2;
  this.page.border="";
  this.page.style.zIndex="-1";
  this.owner=null;
}

Page.prototype.setContent=function(url){
	this.page.src=url;
}
Page.prototype.show=function(bool){
  if(bool){
//	gApp.app.setTitle("");
//	gApp.app.titleDiv.style.zIndex=-1;
    this.page.style.zIndex=5;
    }
  else{
    this.page.style.zIndex=-1;
    }
}

// tabbed page has a tab header and tab content
function TabbedPage(tle){
  this.page=document.createElement("div");
  this.page.className="tabbedpage_";
  this.page.style.position="absolute";
  this.page.style.top=22;//gApp.tabHeight;
  this.page.style.margin="0";
  this.page.style.padding="0";
  this.page.style.left=gApp.panelWidth;
  this.page.style.height=gApp.pageHeight; //"100%";
  this.page.style.zIndex="-1";
//  this.page.style.overflow="scroll";
  this.page.style.width=gApp.contentWidth;
  this.owner=null;
  this.title=tle;
  this.tabs;
  this.opened=false;
}





TabbedPage.prototype.addTab=function(tab){
  var n=0;
  var width;
  tab.parent=this;
  var	newTabs;
  if(this.tabs!=null)
    newTabs=new Array(this.tabs.length+1);
  else
    newTabs=new Array(1);
  width=Math.floor(gApp.contentWidth/newTabs.length);
  tab.tabHeader.style.width=width;
  if(newTabs.length>1){
    for(n=0;n<this.tabs.length;n++){
      newTabs[n]=this.tabs[n];
      newTabs[n].tabHeader.style.width=width;
      newTabs[n].tabHeader.style.left=(width*n);
    }
  }
  newTabs[n]=tab;
    newTabs[n].tabHeader.style.width=width;
    newTabs[n].tabHeader.style.left=(width*n);
  this.tabs=newTabs;
  this.page.appendChild(tab.tabHeader);
  this.page.appendChild(tab.tabContent);
  if(gApp.ie)
    tab.tabHeader.attachEvent("onclick",this.tabClicked,false);
  else
    tab.tabHeader.addEventListener("click",this.tabClicked,false);
}

TabbedPage.prototype.show=function(bool){
  if(bool){
//	gApp.app.setTitle(this.title);
    this.page.style.zIndex=4;
//gApp.app.titleDiv.style.zIndex=5;
    if (!this.opened){
//      this.tabs[0].show(true);
      this.opened=true;
    }
  }
  else{
    this.page.style.zIndex=-1;
  }
}

TabbedPage.prototype.showTab=function(tab){
  if(this.tabs.length>0){
    for(var n=0;n<this.tabs.length;n++)
      if(this.tabs[n]==tab){
        this.tabs[n].show(true);
      }
      else{
        this.tabs[n].show(false);
      }
    }
}

TabbedPage.prototype.tabClicked=function(e){
  var p;
  if(gApp.ie)
    p=window.event.srcElement;
  else
   p=e.target;
//   alert(p.tagName);
   if (p.tagName=="DIV")
   p=p.parent; else
   p=p.parentNode.parent;
  p.parent.showTab(p);
  if(p.mouseClickEvt!=null)
    p.mouseClickEvt();
} 


function Tab(headerText){
  this.win;
  this.isVisible=false;
  this.workPage;
  this.mouseClickEvt;
  this.tabHeader=document.createElement("div");
  var txt=document.createElement("span");
  txt.style.position="absolute";
  txt.style.top="5px";
  txt.style.left="10px";
  txt.innerHTML=headerText;
  this.tabHeader.appendChild(txt);
  this.tabHeader.style.position="absolute";
  this.tabHeader.style.height=gApp.tabHeight;
  this.tabHeader.style.width=gApp.contentWidth-22;
  this.tabHeader.style.top="2";
  this.tabHeader.style.margin="0";
  this.tabHeader.style.padding="0";
  this.tabHeader.style.zIndex="2";
  this.tabHeader.style.cursor="pointer";
  this.tabHeader.className="tabheader_inactive_";
  this.tabHeader.parent=this;
  this.tabContent=document.createElement("iframe");
  this.tabContent.style.margin="0";
  this.tabContent.style.padding="0";
  this.tabContent.style.position="absolute";
  this.tabContent.style.left="0";
//  this.tabContent.style.overflow="scroll";
  this.tabContent.style.top=gApp.tabHeight+2;
  this.tabContent.style.height=gApp.pageHeight-gApp.tabHeight-40;
  this.tabContent.style.width=gApp.contentWidth-2;
  this.tabContent.frameBorder=false;
}

Tab.prototype.show=function(bool){
  this.isVisible=bool;
  if(bool){
  this.tabHeader.className="tabheader_active_";
    this.tabContent.style.zIndex=2;
    }
  else{
  this.tabHeader.className="tabheader_inactive_";
    this.tabContent.style.zIndex=1;
  }
}

Tab.prototype.setContent=function(url){
	this.tabContent.src=url;
}

Tab.prototype.setClickEvt=function(fn){
	this.mouseClickEvt=fn;
}
Tab.prototype.getContent=function(){
	return this.tabContent;
}


  function AppToolbar(app){
    this.cl; // table cells
    this.origImg; // last mouseover image
    this.btn; //current mouseover button
    this.pallette=document.createElement("div");
    this.pallette.style.position="absolute";
    this.pallette.className="toolbar_";
    this.pallette.style.zIndex=10;
    this.pallette.style.margin=0;
    this.pallette.style.height=42;
    this.pallette.style.overflow="hidden";
    this.pallette.style.width=new Number(gApp.contentWidth)+14;
    this.pallette.style.left=new Number(gApp.panelWidth)+2;
    this.msg =document.createElement("span"); // logged in message
    this.msg.style.position="absolute";
    this.msg.className="tbmessage_";
    this.msg.style.margin=0;
    this.msg.style.height=10;
    this.msg.style.top=15;
    this.msg.style.color="#80ff80";
    this.msg.style.fontSize="18pt";
    this.msg.style.left=50;
    this.msg.innerHTML="";
    this.pallette.appendChild(this.msg);
    document.body.appendChild(this.pallette);
    this.parent=app;
  }

  AppToolbar.prototype.addToolbarButton=function(id,eventHandler,label,src){
    var wrapper=document.createElement("a");
    wrapper.href="#";
    wrapper.title=label;
    var button=document.createElement("img");
    button.style.height="48px";
    button.style.width="50px";
    button.style.zIndex=11;
    if(id=="spacer"){
      button.style.borderStyle="none"
      button.src="images/spacer.jpg";
    }
    else{
      button.id= id;
      button.src=src;
      button.alt=label;      
      if(gApp.ie){
 //       button.attachEvent("onmouseover",this.hover); 
 //       button.attachEvent("onmouseout",this.unHover);
        button.attachEvent("onclick",eventHandler);
      }
      else{
//        button.addEventListener("mouseover",this.hover,false);
//        button.addEventListener("mouseout",this.unHover,false);
        button.addEventListener("click",eventHandler,false);
      }
    }
    wrapper.appendChild(button);
    this.pallette.appendChild(wrapper);
  }

  AppToolbar.prototype.disableBtn=function(id,bool){
    document.getElementById(id).disabled=bool;
  }

  function AppToolbar2(app){
    this.pallette=document.createElement("div");
    this.pallette.style.position="absolute";
    this.pallette.className="toolbar_";
    this.pallette.style.zIndex=11;
    this.pallette.style.margin=0;
    this.pallette.style.height=42;
    this.pallette.style.overflow="hidden";
    this.pallette.style.width=new Number(gApp.contentWidth)-600;
    this.pallette.style.left=new Number(gApp.panelWidth) +520;
    document.body.appendChild(this.pallette);
    this.parent=app;
  }
  
  AppToolbar2.prototype.addToolbarButton=function(id,eventHandler,label){
    var button=document.createElement("button");
    button.style.fontFamily="Tahoma";
    button.style.fontSize="8pt";
    button.style.margin="2px";
    button.style.marginTop="10px";
    button.style.height="25px";
    button.style.width="85px";
    button.style.zIndex=12;
    button.id= id;
    button.innerHTML=label;
    if(gApp.ie){
      button.attachEvent("onclick",eventHandler);
    }
    else{
      button.addEventListener("click",eventHandler,false);
  }
    this.pallette.appendChild(button);
  }
/*

  AppToolbar.prototype.hover=function(e){
    var btn;
    if (globals.theMap.ie)
      btn=window.event.srcElement;
    else
      btn=e.target;
    globals.theMap.toolbar.origImg=btn.src;
    btn.setAttribute("src","images/" + btn.getAttribute("id") + "_1_m.png");
  }

  AppToolbar.prototype.unHover=function(e){
    var btn;
    if (globals.theMap.ie)
      btn=window.event.srcElement;
    else
      btn=e.target;
    btn.setAttribute("src","images/" + btn.getAttribute("id") + "_1.png");
  }

*/
