


//+ -----------------------------------------------------------------------------------------
//     launchEService functionality
//- -----------------------------------------------------------------------------------------
function launchEservice(productURL, state, audio) {

	// 12/18/06 flag for lionbridge eBooks (which don't use query string in launchURL)
	var lionbridge = false;
	var soartosuccess = false;
		
	var eservicesHostDomain = 'http://eservices.eduplace.com';
	if (window.location.host.indexOf('dev') != -1) {
		eservicesHostDomain = 'http://dev.eservices.eduplace.com';
	} else if (window.location.host.indexOf('stg') != -1) {
		eservicesHostDomain = 'http://stg.eservices.eduplace.com';
	}

	var launchURL = eservicesHostDomain + productURL;

	// strip off anything other than the initial launch url (just in case)
	var regex = /^([\w\-\/\:\.\?\&\=\+\%]+)/;
	var regexResults = launchURL.match(regex);
	if (regexResults) {
		launchURL = regexResults[1];
	}

	// UPDATE IN eservices_docroot/secure_docroot/js/launch.js
	// handle program specific user state handling
	if (launchURL.indexOf('/epe/mw') != -1) {

		// math 05 ePE
		if (launchURL.indexOf('?') == -1) {
			launchURL += '?s=' + state;
		} else {
			launchURL += '&s=' + state;
		}

	} else if (launchURL.indexOf('/ete/mw') != -1) {

		// math 05 eTE

		// !!!*** LAUNCH URL HACK ***!!!
		// Launch URL in eMeta/WebMan was modified to add /MT*0/ as 3rd folder
		// This is so the 4th folder will give the image name
		// The hack here is to remove the 3rd folder to get the true launch page
		// This is only needed for mw ete's since they do not follow folder convention
		// !!!*** LAUNCH URL HACK ***!!!
		launchURL = launchURL.replace(/\/ete\/mw\/\w\w\w\w\//, '/ete/mw/');
		// !!!*** LAUNCH URL HACK ***!!!

		// append state parameter
		if (launchURL.indexOf('?') == -1) {
			launchURL += '?s=' + state;
		} else {
			launchURL += '&s=' + state;
		}

	} else if (launchURL.indexOf('/epe/hmm') != -1) {

		// math 07 ePE
		if ((launchURL.indexOf('CA9B') != -1) ||
			(launchURL.indexOf('CAS9B')!= -1) ||
			(launchURL.indexOf('GA7A') != -1) ||
			(launchURL.indexOf('TX9A') != -1) ||
			(launchURL.indexOf('TX9B') != -1) ||
			(launchURL.indexOf('TX9C') != -1) ||
			(launchURL.indexOf('TXS9A') != -1) ||
			(launchURL.indexOf('TXS9C') != -1)) {
			lionbridge = true;
		}
		else {
			if (launchURL.indexOf('?') == -1) {
				launchURL += '?s=' + state;
			} else {
				launchURL += '&s=' + state;
			}
		}

	} else if (launchURL.indexOf('/ete/hmm') != -1) {

		// math 07 eTE
		if ((launchURL.indexOf('CA9B') != -1) ||
			(launchURL.indexOf('CAS9B')!= -1) ||
			(launchURL.indexOf('GA7A') != -1) ||
			(launchURL.indexOf('TX9A') != -1) ||
			(launchURL.indexOf('TX9B') != -1) ||
			(launchURL.indexOf('TX9C') != -1) ||
			(launchURL.indexOf('TXS9A') != -1) ||
			(launchURL.indexOf('TXS9C') != -1)) {
			lionbridge = true;
		}
		else {
			// append state parameter
			if (launchURL.indexOf('?') == -1) {
				launchURL += '?s=' + state;
			} else {
				launchURL += '&s=' + state;
			}
		}
	
	} else if (launchURL.indexOf('/epe/hmss05') != -1) {

		// Social Studies ePE
		
		// 3 & 4 fl to launch changes
		if (state == 'FL') {
			if (launchURL.indexOf('SP3') != -1) {
				// Florida grade 3 has different ebook frame set
				// use eb_f2_F.html to launch instead of eb_f2.html
				launchURL = launchURL.replace(/\.html/, '_F.html');
			} else if (launchURL.indexOf('SP4') != -1) {
				// Florida grade 4 has its own ebook folder
				// use folder SP40-FL5B instead of SP40-NA5B
				launchURL = launchURL.replace(/\-NA/, '-FL');
			}
		}
		
		// append state parameter
		if (launchURL.indexOf('?') == -1) {
			launchURL += '?s=' + state;
		} else {
			launchURL += '&s=' + state;
		}
		
	} else if (launchURL.indexOf('/ete/hmss05') != -1) {

		var bookState = state;
		
		// Social Studies eTE
		if (launchURL.indexOf('ST5') != -1) {
			if (launchURL.indexOf('ST51') != -1) {
				// no Florida version for grade 51
				bookState = 'NA';
			} else if (launchURL.indexOf('ST52') != -1) {
				// no Florida version for grade 52
				bookState = 'NA';
			}
		} else if (launchURL.indexOf('ST6') != -1) {
			// no Florida version for grade 6
			bookState = 'NA';
		}

		// grade 3 & 4 have FL launchURL changes
		if (bookState == 'FL') {
			// use *FL.html to launch instead of *NA.html
			launchURL = launchURL.replace(/NA\.html/, 'FL.html');
	
			// if grade 3 or 4 then 
			// look in Florida folder (SP40-FL5B or SP40-FL5B) 
			// instead of national (SP30-NA5B or SP40-NA5B)
			if ((launchURL.indexOf('ST3') != -1) || (launchURL.indexOf('ST4') != -1)) {
				launchURL = launchURL.replace(/\-NA/, '-FL');
			}
		} 

		// append state parameter
		if (launchURL.indexOf('?') == -1) {
			launchURL += '?s=' + state;
		} else {
			launchURL += '&s=' + state;
		}
		
	} else if (launchURL.indexOf('/epe/socsci') != -1) {

		// Social Studies ePE
		
		if ((launchURL.indexOf('GA6B') != -1) ||
			(launchURL.indexOf('OH8A') != -1) || 
			(launchURL.indexOf('KY8A') != -1) || 
			(launchURL.indexOf('NC8A') != -1) || 
			(launchURL.indexOf('NC8B') != -1) ||
			(launchURL.indexOf('TN9A') != -1) || 
			(launchURL.indexOf('NYC9A') != -1) ||
			(launchURL.indexOf('NY9A') != -1) ||
			(launchURL.indexOf('TN9B') != -1)) {
			// NOTE: SC7B is not listed since it is HTML
			//       even though the eTE is flash/lionbridge
			lionbridge = true;
		}
		else {
			// append state parameter
			if (launchURL.indexOf('?') == -1) {
				launchURL += '?s=' + state;
			} else {
				launchURL += '&s=' + state;
			}
		}
		
	} else if (launchURL.indexOf('/ete/socsci') != -1) {

		// Social Studies eTE
		
		if ((launchURL.indexOf('GA6B') != -1) ||
			(launchURL.indexOf('OH8A') != -1) || 
			(launchURL.indexOf('KY8A') != -1) ||
			(launchURL.indexOf('NC8A') != -1) ||
			(launchURL.indexOf('NC8B') != -1) ||
			(launchURL.indexOf('SC7B') != -1) ||
			(launchURL.indexOf('TN9A') != -1) ||
			(launchURL.indexOf('NYC9A') != -1) ||
			(launchURL.indexOf('NY9A') != -1) ||
			(launchURL.indexOf('TN9B') != -1)) {
			lionbridge = true;
		}
		else {
			// append state parameter
			if (launchURL.indexOf('?') == -1) {
				launchURL += '?s=' + state;
			} else {
				launchURL += '&s=' + state;
			}
		}
		
	} else 	if (launchURL.indexOf('/epe/hmsc') != -1) {

		// science epe
		
		// CA ePE
		if (launchURL.indexOf('CA7B') != -1){
			lionbridge = true;
		}
		else {
			// use *FL.html to launch instead of *NA.html except for grade 6 (no FL)
			if (state == 'FL' && launchURL.indexOf('CP6') == -1) {
				launchURL = launchURL.replace(/NA\.html/, state + '.html');
			}

			// append state parameter
			if (launchURL.indexOf('?') == -1) {
				launchURL += '?s=' + state;
			} else {
				launchURL += '&s=' + state;
			}
		}

	} else if (launchURL.indexOf('/ete/hmsc') != -1) {

		// science ete
		
		// use *FL.html to launch instead of *NA.html except for grade 6 (no FL)
		if (state == 'FL' && launchURL.indexOf('CT6') == -1) {
			launchURL = launchURL.replace(/NA\.html/, state + '.html');
		} 

		// append state parameter
		if (launchURL.indexOf('?') == -1) {
			launchURL += '?s=' + state;
		} else {
			launchURL += '&s=' + state;
		}

	} else if (launchURL.indexOf('/epe/hmhmx') != -1) {

		// math expressions epe
		
		// NA ePE
		if (launchURL.indexOf('NA9A') != -1){
			lionbridge = true;
		}
		else {		

			// append state parameter
			if (launchURL.indexOf('?') == -1) {
				launchURL += '?s=' + state;
			} else {
				launchURL += '&s=' + state;
			}
		}

	} else if (launchURL.indexOf('/ete/hmhmx') != -1) {

		// math expressions ete
		
		// NA eTE
		if (launchURL.indexOf('NA9A') != -1){
			lionbridge = true;
		}
		
		// append state parameter
		if (launchURL.indexOf('?') == -1) {
			launchURL += '?s=' + state;
		} else {
			launchURL += '&s=' + state;
		}

	} else if (launchURL.indexOf('/epe/olbc') != -1) {

		// customized student olb's
		if (launchURL.indexOf('?') == -1) {
			launchURL += '?s=' + state;
		} else {
			launchURL += '&s=' + state;
		}

	} else if (launchURL.indexOf('/ete/olbc') != -1) {

		// customized teacher olb's
		if (launchURL.indexOf('?') == -1) {
			launchURL += '?s=' + state;
		} else {
			launchURL += '&s=' + state;
		}

	} else if ((launchURL.indexOf('/epe/olbs') != -1) || (launchURL.indexOf('/olb/student') != -1)) {

		// online leveled books for kids
		launchURL = 'http://eservices.eduplace.com/olb/student/start.do';
		if (window.location.host.indexOf('dev.eservices') != -1) {
			launchURL = 'http://dev.eservices.eduplace.com/ete/olbs/LT00/LT00-NA6B/olb_not_avail.html';
		} else if (window.location.host.indexOf('eservices') != -1) {
			launchURL = 'http://eservices.eduplace.com/ete/olbs/LT00/LT00-NA6B/olb_not_avail.html';
		} else if (window.location.host.indexOf('dev') != -1) {
			launchURL = 'http://dev.eservices.eduplace.com/olb/student/start.do';
		}
	
		// append state parameter
		if (launchURL.indexOf('?') == -1) {
			launchURL += '?s=' + state;
		} else {
			launchURL += '&s=' + state;
		}

	} else if ((launchURL.indexOf('/ete/olbs') != -1) || (launchURL.indexOf('/olb/teacher') != -1)) {

		// online leveled books for teachers
		launchURL = 'http://eservices.eduplace.com/olb/teacher/start.do';
		if (window.location.host.indexOf('dev.eservices') != -1) {
			launchURL = 'http://dev.eservices.eduplace.com/ete/olbs/LT00/LT00-NA6B/olb_not_avail.html';
		} else if (window.location.host.indexOf('eservices') != -1) {
			launchURL = 'http://eservices.eduplace.com/ete/olbs/LT00/LT00-NA6B/olb_not_avail.html';
		} else if (window.location.host.indexOf('dev') != -1) {
			launchURL = 'http://dev.eservices.eduplace.com/olb/teacher/start.do';
		}
		
		// append state parameter
		if (launchURL.indexOf('?') == -1) {
			launchURL += '?s=' + state;
		} else {
			launchURL += '&s=' + state;
		}

	} else if ((launchURL.indexOf('/epe/ssms') != -1) || (launchURL.indexOf('/ssms/student') != -1)) {
		// check if launch request from admin application
		if (window.location.host.indexOf('dev.eservices') != -1) {
			launchURL = 'http://dev.eservices.eduplace.com/ete/ssms/AT00/AT00-TX9A/ssms_not_avail.html';
		} else if (window.location.host.indexOf('stg.eservices') != -1) {
			launchURL = 'http://stg.eservices.eduplace.com/ete/ssms/AT00/AT00-TX9A/ssms_not_avail.html';
		} else if (window.location.host.indexOf('eservices') != -1) {
			launchURL = 'http://eservices.eduplace.com/ete/ssms/AT00/AT00-TX9A/ssms_not_avail.html';
		} else {
			// soar to success for students
			soartosuccess = true;
		}
	
	//} else if ((launchURL.indexOf('/ete/ssms') != -1) || (launchURL.indexOf('/ssms/teacher') != -1)) {
	    }else if ((launchURL.indexOf('/web/teacher') != -1)) {
		// check if launch request from admin application
		if (window.location.host.indexOf('dev.eservices') != -1) {
			launchURL = 'http://dev.eservices.eduplace.com/ete/ssms/AT00/AT00-TX9A/ssms_not_avail.html';
		} else if (window.location.host.indexOf('stg.eservices') != -1) {
			launchURL = 'http://stg.eservices.eduplace.com/ete/ssms/AT00/AT00-TX9A/ssms_not_avail.html';
		} else if (window.location.host.indexOf('eservices') != -1) {
			launchURL = 'http://eservices.eduplace.com/ete/ssms/AT00/AT00-TX9A/ssms_not_avail.html';
		} else {
			// soar to success for students
			soartosuccess = true;
		}
	    }else if ((launchURL.indexOf('/web/student') != -1)) {
		// check if launch request from admin application
		if (window.location.host.indexOf('dev.eservices') != -1) {
			launchURL = 'http://dev.eservices.eduplace.com/ete/ssms/AT00/AT00-TX9A/ssms_not_avail.html';
		} else if (window.location.host.indexOf('stg.eservices') != -1) {
			launchURL = 'http://stg.eservices.eduplace.com/ete/ssms/AT00/AT00-TX9A/ssms_not_avail.html';
		} else if (window.location.host.indexOf('eservices') != -1) {
			launchURL = 'http://eservices.eduplace.com/ete/ssms/AT00/AT00-TX9A/ssms_not_avail.html';
		} else {
			// soar to success for students
			soartosuccess = true;
		}

	} else {
	
		// unknown program
		alert("Unrecognized eBook Program URL:" + launchURL);

	}

	if ((lionbridge == false) && (soartosuccess == false)) {
		// handle audio setting
		if (audio == null) {
			launchURL += '&a=t';
		} else {
			launchURL += '&a=' + audio;
		}
		
		// tell ebook we're launching from the portal
		launchURL += '&es=t';
	}	

//alert(launchURL);
	
	// popup the ebook
	if (lionbridge == true) {
		if (launchURL.indexOf('ete') != -1) {
			//var args = launchURL.split(/,/);
			NewWindowLionbridgeTE(launchURL,"HMCOIntro",0,0,"no","center");
		}
		else if (launchURL.indexOf('epe') != -1) {
			//var args = launchURL.split(/,/);
			NewWindowLionbridgePE(launchURL,"HMCOIntro",0,0,"no","center");
		}
	}
	else if (soartosuccess == true) {
		if (launchURL.indexOf('teacher') != -1) {

			NewWindowSoarToSuccessTMS(launchURL,"HMCOIntro");

		}
		else if (launchURL.indexOf('student') != -1) {

			NewWindowSoarToSuccess(launchURL,"HMCOIntro");

		}
	}
	else {
		eBookPopupWindow = popupWindow(launchURL, POPUP_ESERVICE_NAME, 
										POPUP_ESERVICE_WIDTH, POPUP_ESERVICE_HEIGHT, 
										POPUP_ESERVICE_MIN_WIDTH, POPUP_ESERVICE_MIN_HEIGHT, 
										POPUP_CHROME_MINIMUM, false);
	}
	
	
} // launchEservice

POPUP_ESERVICE_WIDTH = 925;
POPUP_ESERVICE_HEIGHT = 820;
POPUP_ESERVICE_MIN_WIDTH = 760;
POPUP_ESERVICE_MIN_HEIGHT = 540;
POPUP_ESERVICE_NAME = 'Houghton_Mifflin_eServices';
POPUP_CHROME = 'channelmode=no,directories=no,fullscreen=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no';

// pop-up constants
POPUP_LEFT_OFFSET = 50;
POPUP_TOP_OFFSET = 50;
POPUP_LEFT_OFFSET_MINIMUM = 10;
POPUP_TOP_OFFSET_MINIMUM = 10;
POPUP_MINIMUM_WIDTH = 500;
POPUP_MINIMUM_HEIGHT = 350;
SCREEN_MARGIN = 25;
SCREEN_MIN_MARGIN = 5;
POPUP_CHROME_MINIMUM = 0;
POPUP_CHROME_MENUBAR = 1;
POPUP_CHROME_FULL = 2;
POPUP_CHROME = new Array();
POPUP_CHROME[POPUP_CHROME_MINIMUM] = 'channelmode=no,directories=no,fullscreen=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no';
POPUP_CHROME[POPUP_CHROME_MENUBAR] = 'channelmode=no,directories=no,fullscreen=no,location=no,menubar=yes,resizable=yes,scrollbars=yes,status=yes,titlebar=no,toolbar=no';
POPUP_CHROME[POPUP_CHROME_FULL] = 'channelmode=no,directories=no,fullscreen=no,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,titlebar=yes,toolbar=yes';
POPUP_CHROME_HEIGHT = new Array();
POPUP_CHROME_HEIGHT[POPUP_CHROME_MINIMUM] = 50;
POPUP_CHROME_HEIGHT[POPUP_CHROME_MENUBAR] = 75;
POPUP_CHROME_HEIGHT[POPUP_CHROME_FULL] = 150;
POPUP_CHROME_WIDTH = new Array();
POPUP_CHROME_WIDTH[POPUP_CHROME_MINIMUM] = 10; // changed from 25 to fit 800x600
POPUP_CHROME_WIDTH[POPUP_CHROME_MENUBAR] = 25;
POPUP_CHROME_WIDTH[POPUP_CHROME_FULL] = 25;

var eBookPopupWindow=null;
var timeoutid=null;

function NewWindowLionbridgeTE(mypage,myname,w,h,scroll,pos){
	if (timeoutid==null) {
		timeoutid=setTimeout("popupBlocker()",5000);
	}
	w=(screen.width-15);
	h=(screen.height-90);
	if (navigator.appName.toLowerCase()=="netscape") {
		h=(screen.height-80);
	}
	if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
	if(pos=="center"){LeftPosition=0;TopPosition=0;}
	else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
	settings='left=0px,top=0px,width='+w+',height='+h+',scrollbars=no,location=no,directories=no,status=yes,menubar=no,toolbar=no,resizable=no';
	eBookPopupWindow=window.open(mypage,myname,settings);
	eBookPopupWindow.moveTo(0,0);
}


function NewWindowLionbridgePE(mypage,myname,w,h,scroll,pos){
	if (timeoutid==null) {
		timeoutid=setTimeout("popupBlocker()",3000);
	}
	w=(screen.width-15);
	h=(screen.height-90);
	if (navigator.appName.toLowerCase()=="netscape") {
		h=(screen.height-80);
	}
	if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
	if(pos=="center"){LeftPosition=(screen.width)?((screen.width-w)/2)-5:100;TopPosition=0;}
	else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
	settings='left=0px,top=0px,width='+w+',height='+h+',scrollbars=yes,location=no,directories=no,status=yes,menubar=no,toolbar=no,resizable=no';
	eBookPopupWindow=window.open(mypage,myname,settings);
	eBookPopupWindow.moveTo(0,0);
}


function NewWindowSoarToSuccessTMS(mypage,myname){

// !!!!! temp URL override to redirect to Fusion servers !!!!!
//mypage = "http://dev.ssms-app.hmco.com:8080/web/eServices/login.jsp";

	if (timeoutid==null) {
		timeoutid=setTimeout("popupBlocker()",3000);
	}
	
	var w=850;
	var h=600;

	var browserWidth = w + POPUP_CHROME_WIDTH[POPUP_CHROME_MENUBAR];
	var browserHeight = h + POPUP_CHROME_HEIGHT[POPUP_CHROME_MENUBAR];

	if (screen.availWidth && (screen.availWidth<browserWidth)) {
		w = screen.availWidth - 2 * POPUP_CHROME_WIDTH[POPUP_CHROME_MINIMUM];
	}
	if (screen.availHeight && (screen.availHeight<browserHeight)) {
		h = screen.availHeight - 2 * POPUP_CHROME_HEIGHT[POPUP_CHROME_MINIMUM];
	}
	LeftPosition=(screen.width)?((screen.width-w)/2)-5:20;
	TopPosition=(screen.height)?((screen.height-h)/2)-5:20;
	settings='left='+LeftPosition+',top='+TopPosition+',width='+w+',height='+h+',scrollbars=yes,location=no,directories=no,status=yes,menubar=no,toolbar=no,resizable=yes';
	eBookPopupWindow=window.open(mypage,myname,settings);
}


function NewWindowSoarToSuccess(mypage,myname){

// !!!!! temp URL override to redirect to Fusion servers !!!!!

//mypage = "http://dev.ssms-app.hmco.com:8080/web/student/launcher.jsp";

	if (timeoutid==null) {
		timeoutid=setTimeout("popupBlocker()",3000);
	}
	w=800;
	h=600;
	LeftPosition=(screen.width)?((screen.width-w)/2)-5:20;
	TopPosition=(screen.height)?((screen.height-h)/2)-5:20;
	settings='left='+LeftPosition+',top='+TopPosition+',width='+w+',height='+h+',scrollbars=no,location=no,directories=no,status=yes,menubar=no,toolbar=no,resizable=no';
	eBookPopupWindow=window.open(mypage,myname,settings);
}


function popupBlocker() {
	if (eBookPopupWindow==null) {
		alert("It appears that the Pop-up Blocker is ON.\nClick anywhere on the screen to launch the course.");
	} else {
		winclicked=true;
	}
	clearTimeout(timeoutid);
}


function popupWindow (URL, name, width, height, minWidth, minHeight, chromeType, relativeToOpener) {

	// default pop-up sizing
	if (! width) {width = 760};
	if (! height) {height = 540};

	// get browsers sizing from content + chrome
	browserWidth = width + POPUP_CHROME_WIDTH[chromeType];
	browserHeight = height + POPUP_CHROME_HEIGHT[chromeType];
	
	var clientGeometry = null;
	
	// default pop-up location
	var left = SCREEN_MARGIN;
	var top = SCREEN_MARGIN;

	// center on screen 
			
	// reduce size of window if larger than screen
	if (browserWidth > screen.availWidth) { 
		width = screen.availWidth - 2*SCREEN_MARGIN - POPUP_CHROME_WIDTH[chromeType];
		browserWidth = width + POPUP_CHROME_WIDTH[chromeType];
	}
	if (browserHeight > screen.availHeight) {
		height = screen.availHeight - 2*SCREEN_MARGIN -  POPUP_CHROME_HEIGHT[chromeType];
		browserHeight = height + POPUP_CHROME_HEIGHT[chromeType];
	}

	// center window on screen, taking into account additional height for chrome
	left = Math.ceil((screen.availWidth/2) - (browserWidth/2));
	top = Math.ceil((screen.availHeight/2) - (browserHeight/2));

	// make sure left,top and right,bottom are on screen, and that window at least minimum size	
	if (left < SCREEN_MARGIN) left = SCREEN_MARGIN;
	right = left + browserWidth;
	if (right > screen.availWidth - SCREEN_MARGIN) {
		if (relativeToOpener) { 
			left = left - POPUP_LEFT_OFFSET + POPUP_LEFT_OFFSET_MINIMUM;
			right = left + browserWidth;
			if (right > screen.availWidth - SCREEN_MARGIN) {
				width = screen.availWidth - left - POPUP_CHROME_WIDTH[chromeType] - SCREEN_MARGIN;
			}
		} else {
			width = screen.availWidth - left - POPUP_CHROME_WIDTH[chromeType] - SCREEN_MARGIN;
		}
	}
	if (width < minWidth) {
		width = minWidth;
		maxWidth = screen.availWidth - 2*SCREEN_MIN_MARGIN - POPUP_CHROME_WIDTH[chromeType];
		if (width > maxWidth) width = maxWidth;
		left = screen.availWidth - width - SCREEN_MARGIN - POPUP_CHROME_WIDTH[chromeType];
	}
	if (left < SCREEN_MARGIN) {
		left = SCREEN_MIN_MARGIN;
	}
	
	if (top < SCREEN_MARGIN) top = SCREEN_MARGIN;
	bottom = top + browserHeight;	
	if (bottom > screen.availHeight - SCREEN_MARGIN) {
		if (relativeToOpener) { 
			top = top - POPUP_TOP_OFFSET + POPUP_TOP_OFFSET_MINIMUM;
			bottom = top + browserHeight;
			if (bottom > screen.availHeight - SCREEN_MARGIN) {
				height = screen.availHeight - top - POPUP_CHROME_HEIGHT[chromeType] - SCREEN_MARGIN;
			}
		} else {
			height = screen.availHeight - top - POPUP_CHROME_HEIGHT[chromeType] - SCREEN_MARGIN;
		}
	}
	if (height < minHeight) { 
		height = minHeight;
		maxHeight = screen.availHeight - 2*SCREEN_MIN_MARGIN - POPUP_CHROME_HEIGHT[chromeType];
		if (height > maxHeight) height = maxHeight;
		top = screen.availHeight - height - SCREEN_MARGIN - POPUP_CHROME_HEIGHT[chromeType];
	}
	if (top < SCREEN_MARGIN) {
		top = SCREEN_MIN_MARGIN;
	}

	// create properties string
	var windowProperties = "width=" + width + ",height=" + height + ",top=" + top + ",left=" + left;

	// popup chrome
	windowProperties += "," + POPUP_CHROME[chromeType];
	
	// popup the resource and save reference to it
 	return window.open(URL, name, windowProperties);
 	
} // popupWindow


