



var DEFAULT_POPUP_SCALE_FACTOR = 0.9;


var DEFAULT_POPUP_FEATURES = {
'width'      : Math.floor(window.screen.availWidth  * DEFAULT_POPUP_SCALE_FACTOR),
'height'     : Math.floor(window.screen.availHeight * DEFAULT_POPUP_SCALE_FACTOR),
'location'   : 'no',
'status'     : 'no',
'menubar'    : 'no',
'resizable'  : 'yes',
'scrollbars' : 'yes'
};


function popupWindow(url,windowName,features,callback) {
windowName = windowName.replace(/ /g,"");

var callback = (arguments.length >= 4 ? arguments[3] : null);
if (features == null)
features = {};
else if (typeof(features) == "string") {
var featuresList = features.split(',');
var attr;
features = {};
for (var i = 0; i < featuresList.length; i++) {
attr = featuresList[i].split('=');
features[attr[0]] = attr[1];
}
}

for (var name in DEFAULT_POPUP_FEATURES)
if (features[name] == null || ! features[name] || features[name] == "")
features[name] = DEFAULT_POPUP_FEATURES[name];

var left = features['left'];
var top  = features['top'];
var screenX = features['screenX'];
var screenY = features['screenY'];

if (! isNaN(features['width' ]) && isNaN(left) && isNaN(screenX))
left = Math.floor((window.screen.availWidth - features['width' ]) / 2);

if (! isNaN(features['height']) && isNaN(top ) && isNaN(screenY))
top = Math.floor((window.screen.availHeight - features['height']) / 2);

if (  isNaN(left) && ! isNaN(screenX)) left    = screenX;
if (  isNaN(top ) && ! isNaN(screenY)) top     = screenY;
if (! isNaN(left) &&   isNaN(screenX)) screenX = left;
if (! isNaN(top ) &&   isNaN(screenY)) screenY = top;

features['left'   ] = left;
features['screenX'] = screenX;
features['top'    ] = top;
features['screenY'] = screenY;

var featuresArray = [];
for (var name in features)
featuresArray.push(name + "=" + features[name]);
var featuresString = featuresArray.join(",");

var newWindow = window.open(url,windowName,featuresString);

if (newWindow != null)
{
newWindow.focus();
}
else
{
alert(MESput1);
}

if (callback != null) {
newWindow.callback = callback;
}

return newWindow;
}


function helpPopup(url)
{
var features = (arguments.length >= 2 ? arguments[1] : "width=700,height=550,scrollbars=yes");
return popupWindow(url, '', features);
}



function doPopupToUrl(url, callback, windowname)
{
if (windowname == null)
{
windowname = 'name';
}
return popupWindow(url,windowname,null,callback);
}


if(typeof(browserDOMVersion == "undefined"))
{
var browserDOMVersion;
if (document.getElementById)
browserDOMVersion = "getElementById";
else if (document.all)
browserDOMVersion = "all";
else if (document.layers)
browserDOMVersion = "layers";
else
browserDOMVersion = "";
}

var MESput1="Popup window failed to open.  If you have popup blocking\nsoftware running, you will need to disable it in order to view this window.";
var MESput2="Rejoinder";

function getAnchorPosition(anchorname) {
var useWindow=false;
var coordinates=new Object();
var x=0,y=0;
var use_gebi=false, use_css=false, use_layers=false;
if (browserDOMVersion == "getElementById") { use_gebi=true; }
else if (browserDOMVersion == "all") { use_css=true; }
else if (browserDOMVersion == "layers") { use_layers=true; }

if (use_gebi) {
var o=document.getElementById(anchorname);
x=AnchorPosition_getPageOffsetLeft(o);
y=AnchorPosition_getPageOffsetTop(o);
}
else if (use_css) {
x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
}
else if (use_layers) {
var found=0;
for (var i=0; i<document.anchors.length; i++) {
if (document.anchors[i].name==anchorname) { found=1; break; }
}
if (found==0) {
coordinates.x=0; coordinates.y=0; return coordinates;
}
x=document.anchors[i].x;
y=document.anchors[i].y;
}
else {
coordinates.x=0; coordinates.y=0; return coordinates;
}
coordinates.x=x;
coordinates.y=y;
return coordinates;
}

function getAnchorWindowPosition(anchorname) {
var coordinates=getAnchorPosition(anchorname);
var x=0;
var y=0;
if (browserDOMVersion == "getElementById") {
if (isNaN(window.screenX)) {
x=coordinates.x-document.body.scrollLeft+window.screenLeft;
y=coordinates.y-document.body.scrollTop+window.screenTop;
}
else {
x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
}
}
else if (browserDOMVersion == "all") {
x=coordinates.x-document.body.scrollLeft+window.screenLeft;
y=coordinates.y-document.body.scrollTop+window.screenTop;
}
else if (browserDOMVersion == "layers") {
x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
}
coordinates.x=x;
coordinates.y=y;
return coordinates;
}

function AnchorPosition_getPageOffsetLeft (el) {
var ol=el.offsetLeft;
while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
return ol;
}
function AnchorPosition_getWindowOffsetLeft (el) {
return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
}
function AnchorPosition_getPageOffsetTop (el) {
var ot=el.offsetTop;
while((el=el.offsetParent) != null) { ot += el.offsetTop; }
return ot;
}
function AnchorPosition_getWindowOffsetTop (el) {
return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
}



function callPopup ( anchorname, msg )
{
var x, y, pheight, pwidth, pscroll;
var maxH = window.screen.availHeight;
var maxW = window.screen.availWidth;
pheight = 100; pwidth = 200; pscroll = "no";
if (msg.length > 100) {pheight= 400; pwidth=400; pscroll ="yes"};
var anchor = getAnchorWindowPosition(anchorname);
x = anchor.x + 10; y = anchor.y + 10;
if (parseInt(anchor.y + pheight) > maxH) {y = maxH-pheight-10;};
if (parseInt(anchor.x + pwidth) > maxW) {x = maxW-pwidth-10};
rejoinderWindow = window.open("/blank.html","window_"+anchorname,"toolbar=no,location=no,status=no,menubar=no,scrollbars=" + pscroll +",resizable,alwaysRaised,dependent,titlebar=no,width="+ pwidth +",height="+ pheight +",screenX="+ x +",left="+ x +",screenY="+ y +",top=" + y);
if (isWindow(rejoinderWindow))
{
rejoinderWindow.document.open();
rejoinderWindow.document.write("<head><title>"+MESput2+"</title>");
rejoinderWindow.document.write("<link rel='stylesheet' type='text/css' href='/style_sheet_v2.css'></head>");
rejoinderWindow.document.write('<body bgcolor="silver"><div align="left" style="display:inline"><a name="leftTop">&nbsp;</a></div>');
rejoinderWindow.document.write('<div align="center"><table width="100%" height="100%" border="0" align="left"><tr><td><b>' + msg + '</b></td></tr></table></div></body>');
}
else
alert(MESput1);
}


function measureAnchors( a1, a2 )
{
var anchor1 = getAnchorPosition(a1);
var anchor2 = getAnchorPosition(a2);
var anchorDistance = new Object();
anchorDistance.x = abs(anchor2.x - anchor1.x);
anchorDistance.y = abs(anchor2.y - anchor1.y);
return anchorDistance;
}

function getCenterCoordinates( width, height )
{
var obj = new Object();
obj.x = Math.floor((window.screen.availWidth  - width ) / 2);
obj.y = Math.floor((window.screen.availHeight - height) / 2);
return obj;
}

function Coords(width, height)
{
this.x = Math.floor((window.screen.availWidth  - width ) / 2);
this.y = Math.floor((window.screen.availHeight - height) / 2);
}



function displayRejoinderPopup(URL, uid)
{


if(URL.length < 3)
return;

var winLoc = window.location.href;
var myURL;
var win;

if(winLoc.search(/context-quiz/) != -1)
{
var arrURL = winLoc.split("run");
myURL = arrURL[0] + URL;
}
else
{
myURL = URL;
}


if(URL.search(/error/) != -1)
win = popupWindow(myURL, "rejoinderPopupWin", "width=640,height=480,scrollbars,resizable");
}

