
//Filname: popup.js
//By: David Brady

// ****** Instructions ******
// To popup a window from any page do the following two things:
// (1) Include this file, popup.js, by adding the following
//     line inside the <head> seaction of your document:
//    
//     <script type="text/javascript" src="popup.js"></script>
//
// (2) Add a link to popup the calculator. You can create many calculators
//     with different names, then point the link to each calculator
//     
//     Here's an example link:
//     <a href="Open Great New Page" onclick="showPopup('PopupContent.html')">
//        Open Cooling Calculator
//     </a>
//
//     Change the link name "CoolingCalculator.html" in the link for each calculator



//change the name inside showPopup() for each calculator version you create

//width and height of the popup window
var winWidth=730;
var winHeight=500;

//name of default page to load into the popup window. 
//Pass the name of the page you want to load from the link inside 
//the showPopup() method to overide this setting
var defaultPageName='/help_planner.html';

          
//Popup window options. **No spaces allowed** 
//Don't set width or height here. See below for more options          
var windowOptions='toolbar=0,address=0,location=0,menubar=0,scrollbars=1,resizable=1,directories=0';

/*
The following options are available when opening a new window:
toolbar=0|1 	Specifies whether to display the toolbar in the new window.
location=0|1 	Specifies whether to display the address line in the new window.
directories=0|1 Specifies whether to display the Netscape directory buttons.
status=0|1 	Specifies whether to display the browser status bar.
menubar=0|1 	Specifies whether to display the browser menu bar.
scrollbars=0|1 	Specifies whether the new window should have scrollbars.
resizable=0|1 	Specifies whether the new window is resizable.
 */      

//holds a reference to the popup window
var calcRef = {
     "pageName" : defaultPageName
};

//Opens the popup in a new window
function showPopup(linkRef){
     var targetPage=defaultPageName;
     if(linkRef!=null && linkRef!=""){
          targetPage=linkRef;
     }
     var targetName=targetPage.substring(0, targetPage.indexOf('.'));
     targetName=targetName.replace(/[ \t]/g, "");

     if(calcRef && calcRef[targetName] != null && !calcRef[targetName].closed){
          //show the window if it's already opened
          calcRef[targetName].focus();
     }else{
          //adjust popup window location
          var x = window.screenX;
          var y = window.screenY;
                    
          if(!x && window.screenLeft){
               x = window.screenLeft;//explorer adjustment
               if(!x || x<0) x=0;
          }
          if(!y && window.screenTop){
               y = window.screenTop-50;//explorer adjustment
               if(!y || y<0) y=0;
          }
          //x and y are now set to parent windows top-left corner
          //now adjust some more according to preference
          x+= winWidth/3;
          y+= winHeight/3;
          
          var dimOptions=",left="+x+",top="+y;
          var widthHeightOptions=",width="+winWidth+",height="+winHeight;
          var allWinOptions=windowOptions+dimOptions+widthHeightOptions;
          
          calcRef[targetName] = window.open(targetPage,targetName, allWinOptions);
     }
     
     return false;//don't follow the href link
}            
