if(typeof(BC)=="undefined") {
  var BC = null;
  try {
      BC = new BrowserComptability();
  } catch (e) {
    alert("" +
      "javascript error:"+
      "\n     file: popDivWin.js"+
      "\n     message: "+e.Message+
      "\n     suggestion: Please make sure you have included browserSpecific.js and initialized the BC object."+            
      "");
  }
}

// Global Variables
var popWin         = null;
var popWinName     = "popWin";
var headerHeight   = 18;
var popWinMoveFlag = false;
var popWinX        = 0;
var popWinY        = 0;

function popWindow(url, width, height, bMenu, bResize, bScroll, bStatus) {
	height += headerHeight;
	closePopup();
	if(!popWin) {
		//var HTML                = drawPopup();
		//document.body.innerHTML = document.body.innerHTML + HTML;
		popWin                  = findPopup();
		showPopup(url, width, height, bMenu, bResize, bScroll, bStatus);
	} else {
		showPopup(url, width, height, bMenu, bResize, bScroll, bStatus);
	}
}

function popModalWindow(url, width, height, bMenu, bResize, bScroll, bStatus) {
  	height += headerHeight;
	closePopup();
	if(!popWin) {
		popWin                  = findPopup();
		showPopup(url, width, height, bMenu, bResize, bScroll, bStatus);
	} else {
		showPopup(url, width, height, bMenu, bResize, bScroll, bStatus);
	}
}


function closePopup() {
  if (popWin)
    BC.hide(popWin);
  else if(parent.popWin)
    BC.hide(parent.popWin);
}

function popupGetHeader() {
  return popupFind(popWinName + "_header");
}

function popupGetContent() {
  return popupFind(popWinName + "_content");
}

function popupGetFrame() {
  return popupFind(popWinName + "_frame");
}

function resizePopup(width, height) {
  //var Header  = popupGetHeader();
  var Content = popupGetContent();
  var Frame   = popupGetFrame();
  var Win = findPopWin();
  if(Win) {
    Win.style.height     = height + headerHeight + 1; // +1 is Firefox workaround
    Content.style.height = height;
    Frame.style.height   = height;
    Win.style.width      = width;
    //Content.style.width  = width;
    //Frame.style.width    = width;
  }
}

function drawPopup() {
	var HTML = '' +
			'\n	<div class="popWin" id="' + popWinName + '" style="WIDTH: ' + 0 + 'px;  HEIGHT: ' + 0 + 'px; TOP: ' + 0 + 'px; LEFT: ' + 0 + 'px; Z-INDEX: 20; POSITION: absolute; background-color: #FFFFFF; visibility: hidden;">' +
			'\n		<div id="' + popWinName + '_header">' +
			'\n			<table cellpadding="0" cellspacing="0" class="popWinCaption" id="' + popWinName + '_title" width="100%">' +
			'\n			    <tr height="' + headerHeight + 'px">' +
			'\n			        <td width="100%" style="padding = 2px;" onmousemove="movePopup(event);" onmousedown="setMoveFlag(event, true);" onmouseup="setMoveFlag(event, false);" onmouseout="setMoveFlag(event, false);">Popup Window</td>' +
			'\n			        <td class="popWinCaptionButtons" onclick="closePopup();" style="padding: 2px;">X</td>' +
			'\n			    </tr>' +
			'\n			</table>'+
			'\n		</div>' +
			'\n		<div id="' + popWinName + '_content" style="CLEAR: both; OVERFLOW: auto; WIDTH: 100%; HEIGHT: 100%;">' +
			'\n			<IFRAME id="' + popWinName + '_frame" src="" frameBorder="0"></IFRAME>' +
			'\n		</div>' +
			'\n	</div>';
	return HTML;
}

function showPopup(url, width, height, bMenu, bResize, bScroll, bStatus) {
	var style        = popWin.style;
    style.width      = width;
    style.height     = height + 1; // Fix for FF

    centerPopup();

    var iFrame       = document.getElementById(popWinName+ "_frame");
    iFrame.src       = url;
    
    style            = iFrame.style;
    style.width      = "100%";
    style.height     = height - headerHeight;
	
	// Show the popup
	BC.show(popWin);
}

function popupFind(id) {
  var ret = document.getElementById(id);
  if(!ret && parent.document)
    ret = parent.document.getElementById(id);    
  return ret;
}

function findPopup() {
	return popupFind(popWinName);
}

function findPopWin() {
  var Win = popWin;
  if (!Win && parent.popWin) Win = parent.popWin
  return Win;
}

function setMoveFlag(e, value) {
	if(!e) var e = window.event;
    popWinMoveFlag = value;
	var X = BC.eventGetX(e);
	var Y = BC.eventGetY(e);
    popWinX = X;
    popWinY = Y;
}

function centerPopup() {
    var style  = popWin.style;
    var width  = parseInt(style.width);
    var height = parseInt(style.height);
    var Left   = BC.centerPageX() - (width/2);
    var Top    = BC.centerPageY() - (height/2);
    if(Left<0) Left=0;
    if(Top<0) Top=0;
    style.top  = Top;
    style.left = Left;
}

function movePopup(e) {
	if(popWinMoveFlag) {
		if(!e) var e = window.event;
		var X = BC.eventGetX(e);
		var Y = BC.eventGetY(e);
		
		var xOffset = X - popWinX;
		var yOffset = Y - popWinY;
		if(popWin != null) {
		    var oldX = parseInt(popWin.style.left);
		    var oldY = parseInt(popWin.style.top);
		    var newX = oldX + xOffset;
		    var newY = oldY + yOffset;
		    popWin.style.left = newX;
		    popWin.style.top  = newY;
		    popWinX = X; popWinY = Y;
		}
  }
}