function pa_getCookieValue(cookieName)
{
   var cookieValue = document.cookie;
   var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");

   if (cookieStartsAt == -1)
   {
      cookieStartsAt = cookieValue.indexOf(cookieName + "=");
   }

   if (cookieStartsAt == -1)
   {
      cookieValue = null;
   }
   else
   {
      cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
      var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
      if (cookieEndsAt == -1)
      {
         cookieEndsAt = cookieValue.length;
      }
      cookieValue = unescape(cookieValue.substring(cookieStartsAt,
         cookieEndsAt));
   }

   return cookieValue;
}

function pa_setCookie(cookieName, cookieValue)
{
   cookieValue = escape(cookieValue);
   document.cookie = cookieName + "=" + cookieValue;
}


// This will always be the last window opened, change to array indexed on 
// window name if necessary to selectively close
var pa_PopUpWin = ""

function pa_PopUp(sUrl, sPos, sName, sWidth, sHeight)
{	
// If this close is left in, will be impossible to open two popups at the 
// same time by the same caller. Need array indexed on name for it to work correctly
//	if (pa_PopUpWin > "")
//		pa_PopUpWin.close()
	sPos = "" + sPos;
	sPos = sPos.toLowerCase();
	var useable_width = window.screen.availWidth;
	var useable_height = window.screen.availHeight;
	var window_inner_width
	if (sWidth > "")
		window_inner_width = parseInt(sWidth);
	else
		window_inner_width = 396;
		
	var window_inner_height;
	if (sHeight > "")
		window_inner_height = parseInt(sHeight);
	else
		window_inner_height = 295;
		
	var window_name;
	if (sName > "")
		window_name = sName;
	else
		window_name = "popup";

	var top_value;
	var left_value;
	// numeric literals horiz_margin and vert_margin are fudge factors for window margins			
	var vert_margin = 10;
	var horiz_margin = 60;
	switch (sPos) {
		case "ul":
			top_value = 0;
			left_value = 0;
			break;
		case "ur":
			top_value = 0;
			left_value = useable_width - window_inner_width - vert_margin;
			break;
		case "ll":
			top_value = useable_height - window_inner_height - horiz_margin;
			left_value = 0;
			break;
		case "c":
			top_value = (useable_height - window_inner_height - horiz_margin) / 2;
			left_value = (useable_width - window_inner_width - vert_margin) / 2;
			break;
		default: // default to "lr"
			top_value = useable_height - window_inner_height - horiz_margin;
			left_value = useable_width - window_inner_width - vert_margin;
			break;
	}
	var position_feature = "top=" + top_value + ", left=" + left_value + ", ";
	if (navigator.appName != 'Netscape')
		position_feature = "top=" + top_value + ", left=" + left_value + ", ";	
	else
		position_feature = "screenY=" + top_value + ", screenX=" + left_value + ", ";
	var size_feature = "width=" + window_inner_width + ", height=" + window_inner_height + ", ";
	var features = position_feature + size_feature + "status=yes, resizable=no, scrollbars=no";
	
//	pa_PopUpWin = window.open(sUrl, "popup", "width=395, height=375, status=yes, resizable=no, scrollbars=no");	
	pa_PopUpWin = window.open(sUrl, window_name, features);		
	return false;
}

// Closes whatever window is in the variable pa_PopUPWin. If necessary to close
// selectively, use array of windows indexed on window name and pass window name
// to this function. It will need to search array to find which one to close
function pa_PopUpClose()
{	
	if (pa_PopUpWin > "")
		pa_PopUpWin.close()
}

function pa_SessionPopUp(sUrl, sPos, sName, sWidth, sHeight)
{
	pa_PopUp(sUrl, sPos, sWidth, sHeight)

	return false;
}

function pa_DpWindowExitPopUp(sUrl)
{
	pa_ConditionalPopUp(sUrl, "WindowExitPopupAdShown");
	return false;
}

function pa_ConditionalPopUp(sUrl, sCookie, sPos, sWidth, sHeight)
{
	var sCookieValue = pa_getCookieValue(sCookie);
	if (sCookieValue == "TRUE")
		return false;
	pa_setCookie(sCookie, "TRUE");
	pa_PopUp(sUrl, sPos)
}	

function CloseChild(ChildWin)
{
	ChildWin.close()
}
