/* -------------------------------------------------------------------------------------------------------------
   POPUP.JS
   -------------------------------------------------------------------------------------------------------------
	Description		:	All of the benefits of window.open with none of the drawbacks
	                  Specifically, if someone opens the same popup twice, things still work.
	Last Modified	:	Nov. 5, 2001 by Jim McGinley
	Code found in	:	\\Webdev\WebDevRoot\2001\INTERNAL_PROJECTS\shared_code\javascript_popup
	Code tested in	:	PC  IE 4.72.3612.1713, 5.50.4134.0600, Netscape 4.08
							MAC IE 4.01, 5, Netscape 4.06
	Notes				:	Does NOT work in Netscape 3.X (Script SRC= not supported)
	Usage          :  <SCRIPT SRC = popup.js>
				         void openWin("jimdaily", "http://www.dailyradar.com", 400, 300, false)

	KNOWN BUGS
	1. Width & Height (in full screen mode too) are not accurate when "menubar, toolbar, location and directories" features are all yes.
	   i.e. "menubar=yes,toolbar=yes,location=yes,directories=yes" 
	2. In some cases, depending on the features, full screen will yield a popup that is vertically too big.
	   In other cases, depending on the features, full screen will not be wide enough.
	3. In MAC IE4, it does not center the popup. Nor does it allow you to position it.
	
	REMEMBER
	1. fullscreen=1; also seemed to do the trick (in an unrelated experiment)
		type=fullWindow,fullscreen is how you get netscape and ie to do a real fullscreen.
	   however, it ignores ALL your other settings i.e. you will get no menubar regardless of whether you want one.
		note: alt+f4 closes the window
	2. "1,2 , 3 , 4 ,5".split(\s*,\s*);	// Returns ["1,", "2", "3", "4", "5"]
	3. if then shortcut : words.is_required = (planguage == 1) ? " obligatoire.\n" : " is required.\n";
	4. window.open with NO FEATURES specified turns EVERYTHING on (scrollbars, resizeable etc.)
	   As soon as you specify 1 of the features (left, width, height), they all default to off.

	// determine whether the user wanted scrollbars or not
	if (typeof(pscrollbars) == "boolean") {
		scrollbars = pscrollbars;
	} else {
		hold = pscrollbars.toString().toLowerCase();
		if ((hold == "1") || (hold == "yes") || (hold == "scrollbars")) {scrollbars = true;}
	}
*/

var popups = new Array;

function popup(pname, pURL, pwidth, pheight, pfeature, pscreenx, pscreeny) {
//Pops up a window. If window is already popped up, it just sets focus to it.
	// 1. pname 	-> giving 2 popups the same name (including "") causes them to overwrite each other.
	//					-> giving 2 popups different names will cause 2 different popups
	// 2. pURL		-> URL you want to popup, relative URLS work.
	// 3. pwidth 4. pheight	-> Width of popup. Height of popup. Passing in 0, 0 causes a full-screen popup
	// 5. pfeature -> feature to override default feature.
	// 6. pscreenx 7. pscreeny -> Position of popup. If not passed in, centers the popup.
	// returns     -> pointer to window that was opened
	// Usage: openWin("jimdaily", "http://www.dailyradar.com", 400, 300, "scrollbars=yes", 100, 200)

	var width = 0, height = 0;
	var deltawidth= 0, deltaheight = 0;
	var screenx = 0, screeny = 0;
	var i = 0; popupindex = -1, newwindow = null;
	var is_ie  = ((navigator.appVersion.toLowerCase().indexOf('msie')) != -1);
	var is_mac = (navigator.userAgent.toLowerCase().indexOf("mac") != -1);																										
	var version = parseFloat(navigator.appVersion);

	var feature = "scrollbars=no,menubar=no,toolbar=no,location=no,directories=no,status=no,resizable=no";

	// search through the list of open popups to see if it's already open
	for (i = 0; i < popups.length ; i++) {
		if ((popups[i].window != null) && (!popups[i].window.closed)) {
			if (popups[i].name == pname) {popupindex = i;}
			screenx = popups[i].screenx + 25; screeny = popups[i].screeny + 25;	// cascade multiple popups
		}
	}

	// in IE4, it's a security violation to touch a popup after it's been open (and points to a URL on a different site)
	// if all popups are on the same site as the page that called them, this code can be removed.
	if ((popupindex != -1) && (is_ie) && (version <= 4)) {	
		popups[popupindex].window.close(); popupindex = -1;
	}
	
	if (popupindex == -1) {
		// the requested window is NOT already open. open it, and add it to the list of popups.
		popupindex = popups.length;
		
		// configure the feature of the window (including scrollbars)
		if (pfeature) {
			if (typeof(pfeature) != "string") {alert("BUG:Popup.js was passed an invalid feature. It MUST be a string."); return false;}
			if (pfeature.toString() != "") {feature = pfeature.toString().replace(" ", "");}
		}
		
		// tweak the passed in width & height depending on requested features - this is far from perfect
		deltawidth = 10;
		deltawidth += (((feature.search("resizable=yes")) != -1) && (is_ie)) ? 2 : 0;
		
		deltaheight = 29;
		deltaheight += (((feature.search("resizable=yes")) != -1) && (is_ie)) ? 2 : 0;
		deltaheight += (((feature.search("menubar=yes")) != -1) && (is_ie)) ? 48 : 0;
		deltaheight += (((feature.search("status=yes")) != -1) && (is_ie)) ? 20 : 0;

		// build the feature list with proper screenx and width
		if ((pwidth == 0) && (pheight == 0)) {
			// full screen popup
			width = screen.availWidth - deltawidth; height = screen.availHeight - deltaheight;
			screenx = 0; screeny = 0;
		} else {
			width = pwidth - deltawidth; height = pheight - deltaheight;
			// center the new window on the screen
			if ((typeof(pscreenx) == "number") && (typeof(pscreeny) == "number") && (pscreenx != 0) && (pscreeny != 0)) {
				screenx = pscreenx; screeny = pscreeny;
			} else if ((screenx == 0) && (screeny == 0)) {
				if ((is_ie) && (is_mac)) {pwidth -= 15;}
				screenx = parseInt((screen.availWidth - (pwidth + deltawidth)) / 2); screeny = parseInt((screen.availHeight - (pheight + deltaheight)) / 2);
			}
			if (screenx < 0) screenx = 0; if (screeny < 0) screeny = 0;
		}
		feature += ",left=" + screenx + ",screenX=" + screenx + ",top=" + screeny + ",screenY=" + screeny + ",width=" + width + ",height=" + height;

		newwindow = window.open(pURL, pname, feature);
		if (newwindow != null) {
			if (!((is_ie) && (version <= 4))) {newwindow.focus();}	// security violation in IE4 if URL is points to a different site

			// memorize the passed-in parameters for later reference
			popups[popupindex] = new Object();
			popups[popupindex].window = newwindow;
			popups[popupindex].name = pname;
			popups[popupindex].url = pURL;
			popups[popupindex].screenx = screenx; popups[popupindex].screeny = screeny;
		}

	} else {
		// it's already open, so reuse the popup that's already out there (browser will NOT let us override feature, only URL).
		if (popups[popupindex].url != pURL) {	// are we changing URLs in currently open window?
		 	popups[popupindex].window.location = pURL;
			popups[popupindex].url = pURL;
		}
		popups[popupindex].window.focus();
	}

	return popups[popupindex].window;
}
