window.onload = resizeAndCenter;

function winBRopen(theURL, Name, popW, popH) {
	var winHorz = (screen.width-popW)/2;
	var winVert = (screen.height-popH)/2;
	winProp = 'width='+popW+',height='+popH+',left='+winHorz+',top='+winVert+',scrollbars=yes,resizable=yes';
	var Win = window.open(theURL, Name, winProp);
	if (parseInt(navigator.appVersion) >= 4) { Win.window.focus(); }
}

function closeupwindow(){
	window.close();
}

function popupWindow(filename){
	if(filename.length > 0 && strPopUpDirURL.length > 0) {
		puwURL = strPopUpDirURL + filename;
		winBRopen(puwURL,'popupWin1',640,480);
	}
}

function popupWindowNV(){
	if(strPopUpFileURL.length > 0) winBRopen(strPopUpFileURL,'popupWin2',640,480);
}

function closeAndMove(){
	if(strBuyNowURL.length > 0) window.open(strBuyNowURL);
}

function trackFlash(fAction, pfAction, dupClick, diffTime) {
	var tPage = flashTrackPage;
	tPage +='\&fAction=' + escape(fAction) + '\&pfAction=' + escape(pfAction) + '\&dupClick=' + dupClick + '\&diffTime=' + diffTime;
	var xmlHttp = XmlHttp.create();
	var async = true;
	xmlHttp.open("GET", tPage, async);
	xmlHttp.send(null);
}

function setVars(){
	var url = setVars.arguments[0];
	var varStr = '?';

	for(var x=1; x<setVars.arguments.length; x++) {
		varStr += escape(setVars.arguments[x]) + '=' + escape(eval(setVars.arguments[x]));
		if(x < setVars.arguments.length-1) varStr += '&';
	}

	theURL = url + varStr;
	winBRopen(theURL,'EmailFriend',425,425);
} 

function SendInfo(urlPage,referString){
 	myString = referString;
	setVars(urlPage,'myString');
}

function LILWindow(url)
{
	var intHeight = screen.height;
	var intWidth = screen.width;

	var winName = "_blank";
	var intXPos = 10;
	var intYpos = 10;

	var bTool = 'no';
	var	bLoc = 'no';
	var bStatus = 'no';
	var bMenu = 'no';
	var bScroll = 'yes';
	var bResize = 'yes';
	var bHist = 'no';

	var NS4Plus = (document.layers) ? 1 : 0;
	var IE4Plus = (document.all) ? 1 : 0;

	if (intWidth <= 800) {
		intWidth = 750;
		intHeight = 500;
	} else {
		if(IE4Plus) {
			intWidth = 955;
			intHeight = 668;
		} else {
			intWidth = 1000;
			intHeight = 668;
		}
	}

	strProp = '';
	strProp	= strProp + "toolbar=" + bTool + ",location=" + bLoc + ",status=" + bStatus + ",menubar=" + bMenu + ","
	strProp	= strProp + "scrollbars=" + bScroll + ",resizable=" + bResize + ",copyhistory=" + bHist + ",";
	strProp	= strProp + "width=" + intWidth + ",height=" + intHeight + ",left=" + intXPos + ",top=" + intYpos + "";

	window.open(url, winName, strProp);
}

var surveyTable = '';
var surveyTableLink = '';
var layerwidth=300;
var layerheight=90;
var floatX=-10-layerwidth;
var floatY=-10-layerheight;
var lastX;
var lastY;

var NS6=false;
var IE4=(document.all);
if (!IE4) {NS6=(document.getElementById);}

function showSurveyLayer() {
	if (NS6) {
		floatX=Math.round((window.innerWidth-20)/2)-Math.round(layerwidth/2);
		floatY=Math.round((window.innerHeight-20)/2)-Math.round(layerheight/2);
		lastX=window.pageXOffset + floatX;
		lastY=window.pageYOffset + floatY;
		document.getElementById('floatlayer').style.left=lastX;
		document.getElementById('floatlayer').style.top=lastY;
		
	} else if (IE4) {
		floatX=Math.round((document.body.offsetWidth-20)/2)-Math.round(layerwidth/2);
		floatY=Math.round((document.body.offsetHeight-20)/2)-Math.round(layerheight/2);
		lastX=document.body.scrollLeft + floatX;
		lastY=document.body.scrollTop + floatY;
		document.all['floatlayer'].style.posLeft = lastX;
		document.all['floatlayer'].style.posTop = lastY;
	}
	
	var tmp = document.getElementById("floatlayer");
	tmp.style.visibility = '';
}

function hideSurveyLayer() {
	var tmp = document.getElementById("floatlayer");
	tmp.style.visibility = 'hidden';
}

surveyTable += '<div id="floatlayer" style="visibility:hidden; position:absolute; left:' + floatX + '; top:' + floatY + ';">';
surveyTable += '<table width="300" height="90" border="0" cellpadding="6" cellspacing="0" bgcolor="#E0DFE3">';
surveyTable += '<tr><td style="font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: bold; color:#000000;">Are you done with this module?<br>Can we ask you a few questions?</td></tr>';
if(window.opener != null) {
	surveyTable += '<tr><td style="font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: normal; color:#000000;"><a style="font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: normal; color:#333399;" href="' + strSurveyURL + '">YES</a>, show me the survey.</td></tr>';
} else {
	surveyTable += '<tr><td style="font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: normal; color:#000000;"><a style="font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: normal; color:#333399;" href="javascript:;" onClick="winBRopen(\'' + strSurveyURL + '\', \'survey\', 600, 600)">YES</a>, show me the survey.</td></tr>';
}
surveyTable += '<tr><td style="font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: normal; color:#000000;"><a style="font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: normal; color:#333399;" href="javascript:;" onClick="hideSurveyLayer()">NO</a>, I want to return to the module.</td></tr>';
surveyTable += '</table>';
surveyTable += '</div>';

surveyTableLink = '<div align="center"><a style="background-color:#FFFFCC; font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: normal; color:#000000;" href="javascript:;" onClick="showSurveyLayer()">&gt;&gt; Click here to take our survey. &lt;&lt;</a></div>';

surveyDirectLaunch = '<div align="center"><a style="background-color:#FFFFCC; font-family: Arial, Helvetica, sans-serif; font-size: 11px; font-weight: normal; color:#000000;" href="javascript:;" onClick="winBRopen(\'' + strSurveyURL + '\', \'survey\', 600, 600)">&gt;&gt; Click here to take our survey. &lt;&lt;</a></div>';


function resizeAndCenter() {
	var oAPC = document.getElementById('allPageContent');
	var oFC = document.getElementById('flashcontent');

	if((oAPC || oFC) && window.opener != null) {
		var oBody = document.getElementsByTagName("body")[0];
		oBody.style.overflow = 'hidden';
	
		// Check sizes and resize window accordingly.
		var arrWinSizes = checkWindowSizes();
		var arrContentSizes = checkContentSizes();
		var iDiffWidth = (arrContentSizes[0] - arrWinSizes[0]) + 2;
		var iDiffHeight = (arrContentSizes[1] - arrWinSizes[1]) + 2;
		window.resizeBy(iDiffWidth, iDiffHeight);
	
	} else if(window.opener != null) {
		window.resizeTo(intWinWidth,intWinHeight);
	}
	
	if(window.opener != null) {
		// Check window size and center on screen.
		var scrWidth = self.screen.width;
		var scrHeight = self.screen.height;
		var arrWinSizes = checkWindowSizes();
		var newX = Math.round((scrWidth - arrWinSizes[0] - 10)/2);
		var newY = Math.round((scrHeight - arrWinSizes[1] - 50)/2);
		window.moveTo(newX,newY);
	}
	/*
	if(window.opener == null && parent.window) {
		if(parent.window.adjustParentFrame) {
			var arrContentSizes = checkContentSizes();
			parent.window.adjustParentFrame(arrContentSizes[0],arrContentSizes[1]);
		}
	}
	*/	
}

function checkWindowSizes() {
	var x,y;
	if (self.innerWidth) {
		// all except Explorer
		x = self.innerWidth;
		y = self.innerHeight;
		
	} else if(document.documentElement && document.documentElement.clientWidth) {
		// Explorer 6 Strict Mode
		x = document.documentElement.clientWidth;
		y = document.documentElement.clientHeight;
		
	} else if(document.body) {
		// other Explorers
		x = document.body.clientWidth;
		y = document.body.clientHeight;
		
	} else {
		x = intWinWidth;
		y = intWinHeight;
	}
	
	return [x,y];
}

function checkContentSizes() {
	var x,y;
	var oAPC = document.getElementById('allPageContent');
	var oFC = document.getElementById('flashcontent');
	
	if(oAPC) {  // All page content is contained in a table with id="allPageContent"
		x = oAPC.offsetWidth;
		y = oAPC.offsetHeight;

	} else if(oFC) {  // Assumes width of Flash. Adds together position of Flash from top and height of Flash. 
		var oFOTag = oFC.getElementsByTagName("object")[0];		
		x = parseInt(oFOTag.width);	
		var arrPos = findTopLeft(oFOTag);
		y = arrPos[1] + parseInt(oFOTag.height);
		if(strSurveyURL != '') y += 20;
		
	} else {
		x = intWinWidth;
		y = intWinHeight;
	}

	return [x,y];
}

function findTopLeft(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}
	return [curleft,curtop];
}

