//////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 1998, Siebel Systems, Inc., All rights reserved.
//
// FILE:       swecommon.js
//  $Revision: 145 $
//      $Date: 11/09/01 4:30p $
//    $Author: Wlai $ of last update
//
// CREATOR:    TDiec
//
// DESCRIPTION
//    
// JavaScript Functions used by the Siebel Web Engine.
//
//////////////////////////////////////////////////////////////////////////////

if (top._swe && top._swe._swejssview != null)
   top._swejssview = top._swe._swejssview;

var SWEIsHighInteract = false;
var SWEIsAutomation = false;
var SWEPopupResizeNoHide = false;

function _SWEShowPopup (url, height, width, bFull, bFree, extraFeatures)
{
   SWEDoShowPopup (url, height, width, bFull, bFree, "scrollbars");
}

// 12-EBDFCN
function SWEConfirmNExecute(msg, okurl,cancelurl)
{
   var tf = SWEConfirm(msg);

   if (tf && okurl != null && okurl != '')
   {
      SWEGotoURL(okurl);
   }
   else if (cancelurl != null && cancelurl != '')
      SWEGotoURL(cancelurl);
}


function SWEDoShowPopup (url, height, width, bFull, bFree, extraFeatures)
{
   var position;
   var sFeatures;
   // use full toolbars if full 
   if (bFull == true || bFull == "true")
   {
      sFeatures = "scrollbars=yes,resizable,location,toolbar,menubar,status,personalbar,";
   } 
   else
   {
      sFeatures = "resizable,scrollbars=yes,";
   }

   // set the size
   if (!Top().SWEPopupResizeNoAuto && (typeof(height) == "undefined" || typeof(width) == "undefined" || 
      height <= 0 || width <= 0 ||
      SWEGetAvailScreenWidth()-width <0 || SWEGetAvailScreenHeight()-height <0))
   {
      height = 480;	
      width = 640;
   }
   
   // AOL 6.0 has defect that position is interpreted as position of content
   // in popup window as opposed to position of popup window itself MEK
   if (navigator.userAgent.toUpperCase().indexOf("AOL 6.") > -1)  // 12-ENMO2V
   {
      position = SWEPosition(0,0); 
   }
   else
   {
      position = SWEPosition(parseInt((SWEGetScreenWidth()-width)/2),
                             parseInt((SWEGetScreenHeight()-height)/2));
   }

   sFeatures += ("height=" + height + ",width= " + width);

   if (typeof(extraFeatures)  != 'undefined' && extraFeatures != null && extraFeatures != "")
      sFeatures = sFeatures + "," + extraFeatures;

   // open a free window if bFree
   if (bFree == true || bFree == "true")
   {
	   open (url, "", sFeatures);
	   return true;
   }

   var psource;
   if (SWEIsHighInteract)
   {
      psource = App().popup.source;
      App().popup.started = true;
      App().popup.source = "SI";
   }

   if (Top().SWEPopupWin != null && typeof(Top().SWEPopupWin.closed) != "unknown" && !Top().SWEPopupWin.closed)
   {
      if (SWEIsHighInteract && psource == "HI")
         Top().SWEPopupWin.opener = window;

      if (url != "")
      {
         Top().SWEPopupWin.location.replace (url);
      }
      return false;
   }
   else
   {
      Top().SWEPopupWin = open (url, Top().SWEHtmlPopupName, sFeatures + position);
      return true; 
   }
}


function SWEShowNamedPopup (name, url, height, width)
{
   if (height <= 0 || width <= 0)
   {
      height = 600;
      width = 600;
   }

   open (url, name, "height=" + height + ",width= " + width +",scrollbars=yes,resizable");
}

function SWEShowPopup (url, height, width, bFull, bFree, extraFeatures)
{
   SWEDoShowPopup (url, height, width, bFull, bFree, "scrollbars");
}

//
//Used to goto a view. 
// 
function SWEGotoView(viewname, busObjZone, url, target)
{
   if (SWEIsHighInteract)
   {
      App().GotoView (viewname, busObjZone, url, target);
   }
   else 
   {
      SWETargetGotoURL (url, target, false);
   }
}

//
//Used to goto a url. Will append a time stamp to enable the server to identify a refresh
// 
function SWETargetGotoURL(url, target, bLeaveApplet)
{
   if (pendingChanges() && url.indexOf("SkipDataLossWarning")<0 && StopForSavingData())
      return;

   var frameObj;
   var now = new Date();

   if( typeof(target) == "string")
   {
      if (target != "")
         frameObj = SWEFindFrame(top, target);
      else
         frameObj = null;
   }
   else 
      frameObj = target;

   if(frameObj == null) 
      frameObj = this;

   //Only add the time stamp if the GotoURL is a SWE URL.
   //If this is an external URL, then we don't want to add the time stamp
   if (url.search("start.swe") != -1)
   {
      url = SWEAppendArgsToURL (url, "SWETS", now.getTime());
      // Fix for 12-G8DVVA. MSN browser sends file download request twice
      // So we set a URL argument indicating that a refresh should not be
      // attempted for such a case. When the server parses this argument, 
      // it will send the file again instead of doing a refresh. We check 
      // frameObj against top to gaurd against misconfiguration
      if (!SWEIsHighInteract && (frameObj != top) && (url.indexOf("SWEMethod=Drilldown") != -1)
          && (navigator.userAgent.indexOf("MSN") > -1))
      {
         url = SWEAppendArgsToURL (url, "SWENR", 1);
      }
   }
   
   if (SWEIsHighInteract)
   {
      var app = App();
      if (bLeaveApplet == true && app.mainView)
      {
         if (app.mainView.SetActiveApplet(null))
            app.GotoURL(url, frameObj);
            //else back up original status
      }
      else
         app.GotoURL(url, frameObj);
   }
   else 
   {
      if (typeof(top._samePage) != 'undefined' && top._samePage!="")
      {
         //unlikely to have two immediate post changes from the same page
         //otherwise we have to take out the old value and replace with the new one.
         SWEAppendArgsToURL (url, "SWESPa", top._samePage); 
         top._samePage = "";
      }
      if (SWEIsAutomation)
         Top().SetBusy (true);
      frameObj.location = url;      
   }
}

// 12-EEI3FR: SWETargetGotoURL() issued from view bar should not fail due to swe
// count.  These changes are added in this function rather than directly in 
// SWETargetGotoURL() to prevent possible regression in 7.5.3.  This is only used in SI.
// For HI the same is done in JSSApplication_GotoView() via SWESelect().
function SWEViewBarTargetGotoURL(url, target, bLeaveApplet)
{
   url = url.replace(/SWEC=\d*/, "SWEC=" + ((SWEIsHighInteract) ?  App().GetSWECount() : Top().SWECount));
   SWETargetGotoURL(url, target, bLeaveApplet);
}

function SWEGotoURL(url, bLeaveApplet)
{ 
   SWETargetGotoURL(url, this, bLeaveApplet); 
}

function SWEFindFrame(topWin, frameName)
{
   if (frameName == "_self")
   {
      return this;
   }

   for (var i = 0; i < topWin.frames.length; i ++) 
   {
      var frameObj = topWin.frames[i];

      var theFrame = topWin.frames[frameName];

      if (theFrame != null)
         return theFrame;
            
      frameObj = SWEFindFrame (frameObj, frameName);

      if (frameObj != null)
          return frameObj;
   }

   return null;
}

function SWEConfirmNCall(msg, jsCall)
{
   var tf = SWEConfirm(msg);

   if (tf)
   {
      eval(jsCall);
   }
}

var   g_bInitialized = false;

// JAVASCRIPT FUNCTION -- DOES NOTHING (USED FOR THE HREF IN THE CALENDAR and calculator CALL)
function doNothing() {
}

function SWEJSSEvalJS (str)
{
   eval (str);
}

function SWEJSSClosePopup ()
{
   if (top.opener == null)
   {
      App().ClosePopup();
   }
}

function SWEJSSInvokeMethod (
                       appletVar,
                       method, 
                       viewId,
                       view, 
                       applet,                        
                       field,
                       row, 
                       rowIds,
                       reqRowId,
                       inputps,
                       targetView,
                       targetApplet,
                       mode,
                       showPopup,
                       height,
                       width,
                       reqCount,
                       confirmMsg,
                       keepCtx,
                       seqId,
                       resize)
{
   if(confirmMsg != null && confirmMsg != "" )
   {
      var tf = SWEConfirm(confirmMsg);
      if (!tf)
      {
         return;
      }
   }

   var pset = App().CreateObj ("JSSPropertySet");
   var bMethod = false;

   if (method != null)
   {
      pset.SetProperty ("SWEMethod", method);
      bMethod = true;
   }

   if (viewId != null)
      pset.SetProperty ("SWEVI", viewId);
   
   if (view != null)
      pset.SetProperty ("SWEView", view);

   if (applet != null)
   {
      pset.SetProperty ("SWEApplet",applet);
      if (bMethod)
         pset.SetProperty ("SWECLK", "1");
   }

   if (seqId != null)
      pset.SetProperty ("SWESeq", seqId);

   if (field != null)
      pset.SetProperty ("SWEField", field);

   if (row != null)
      pset.SetProperty ("SWER", row);

   if (rowIds != null)
      pset.SetProperty ("SWERowIds", rowIds);

   if (reqRowId != null)
      pset.SetProperty ("SWEReqRowId", reqRowId);

   if (inputps != null)
      pset.SetProperty ("SWEIPS", inputps);

   if (targetView != null)
      pset.SetProperty ("SWETargetView", targetView);

   if (targetApplet != null)
      pset.SetProperty ("SWETA", targetApplet);

   if (mode != null)
      pset.SetProperty ("SWEM", mode);

   if (showPopup != null)
      pset.SetProperty ("SWESP", showPopup);

   if (height != null)
      pset.SetProperty ("SWEH", height);

   if (width != null)
      pset.SetProperty ("SWEW", width);

   if (reqCount != null)
      pset.SetProperty ("SWEC", reqCount);

   if (keepCtx != null)
      pset.SetProperty ("SWEKeepContext", keepCtx);

   if (resize != null)
   {
      Top().SWEPopupResizeNoAuto = true;
      Top().SWEPopupHeight = height;
      Top().SWEPopupWidth = width;
   }

   var frame = SWEJSSFindAppletFrame (viewId, applet);
   var fullAppletVar = 'frame.' + appletVar;
   eval ('if (typeof frame != "undefined" && ' + fullAppletVar + ') ' + fullAppletVar + '.ButtonMethod (method, pset, (top.opener != null))');
}

//This function is only used for Calendar code, per Anil Mukundan's suggestion
//Please do not use this function for any other purpose.
//Contact Setiono Tandriono or Parthasarathy Ramachandran if you have any questions
//Please keep this function in sync with:
//       SSstring
//       CSSSWEFrame::CreateJSSInvokeMethodCall(CSSMapStringToString& jsArgs,
//                                              LPCTSTR               functionName)
//in framshow.cpp
//
function SWEJSSCalendarInvokeMethod (
                                      appletVar,
                                      method, 
                                      viewId,
                                      view, 
                                      applet, 
                                      field,
                                      row, 
                                      rowIds,
                                      reqRowId,
                                      inputps,
                                      targetView,
                                      targetApplet,
                                      mode,
                                      showPopup,
                                      height,
                                      width,
                                      reqCount,
                                      confirmMsg,
                                      keepCtx,
                                      rowId)
{
   if(confirmMsg != null && confirmMsg != "" )
   {
      var tf = SWEConfirm(confirmMsg);
      if (!tf)
      {
         return;
      }
   }

   var pset = App().CreateObj ("JSSPropertySet");

   if (method != null)
      pset.SetProperty ("SWEMethod", method);

   if (viewId != null)
      pset.SetProperty ("SWEVI", viewId);
   
   if (view != null)
      pset.SetProperty ("SWEView", view);

   if (applet != null)
      pset.SetProperty ("SWEApplet",applet);

   if (field != null)
      pset.SetProperty ("SWEField", field);

   if (row != null)
      pset.SetProperty ("SWER", row);

   if (rowIds != null)
      pset.SetProperty ("SWERowIds", rowIds);

   if (rowId != null)
      pset.SetProperty ("SWERowId", rowId);

   if (reqRowId != null)
      pset.SetProperty ("SWEReqRowId", reqRowId);

   if (inputps != null)
      pset.SetProperty ("SWEIPS", inputps);

   if (targetView != null)
      pset.SetProperty ("SWETargetView", targetView);

   if (targetApplet != null)
      pset.SetProperty ("SWETA", targetApplet);

   if (mode != null)
      pset.SetProperty ("SWEM", mode);

   if (showPopup != null)
      pset.SetProperty ("SWESP", showPopup);

   if (height != null)
      pset.SetProperty ("SWEH", height);

   if (width != null)
      pset.SetProperty ("SWEW", width);

   if (reqCount != null)
      pset.SetProperty ("SWEC", reqCount);

   if (keepCtx != null)
      pset.SetProperty ("SWEKeepContext", keepCtx);

   var frame = SWEJSSFindAppletFrame (viewId, applet);
   var fullAppletVar = 'frame.' + appletVar;
   eval ('if (typeof frame != "undefined" && ' + fullAppletVar + ') ' + fullAppletVar + '.ButtonMethod (method, pset, (top.opener != null))');
}



//
// Please contact Jay Gopalkrishnan before you use / modify this function.
// This is a temporary function that will go away with the view cache changes
//
function SWEJSSInvokeMethod_Temp (
                       appletVar,
                       method, 
                       viewId,
                       view, 
                       applet, 
                       field,
                       row, 
                       rowIds,
                       reqRowId,
                       inputps,
                       targetView,
                       targetApplet,
                       mode,
                       showPopup,
                       height,
                       width,
                       reqCount,
                       confirmMsg,
                       keepCtx)
{
   Top().SetBusy(true);

   if(confirmMsg != null && confirmMsg != "" )
   {
      var tf = SWEConfirm(confirmMsg);
      if (!tf)
      {
         Top().SetBusy(false);
         return;
      }
   }

   var pset = App().CreateObj ("JSSPropertySet");

   if (method != null)
      pset.SetProperty ("SWEMethod", method);

   if (viewId != null)
      pset.SetProperty ("SWEVI", viewId);
   
   if (view != null)
      pset.SetProperty ("SWEView", view);

   if (applet != null)
      pset.SetProperty ("SWEApplet",applet);

   if (field != null)
      pset.SetProperty ("SWEField", field);

   if (row != null)
      pset.SetProperty ("SWER", row);

   if (rowIds != null)
      pset.SetProperty ("SWERowIds", rowIds);

   if (reqRowId != null)
      pset.SetProperty ("SWEReqRowId", reqRowId);

   if (inputps != null)
      pset.SetProperty ("SWEIPS", inputps);

   if (targetView != null)
      pset.SetProperty ("SWETargetView", targetView);

   if (targetApplet != null)
      pset.SetProperty ("SWETA", targetApplet);

   if (mode != null)
      pset.SetProperty ("SWEM", mode);

   if (showPopup != null)
      pset.SetProperty ("SWESP", showPopup);

   if (height != null)
      pset.SetProperty ("SWEH", height);

   if (width != null)
      pset.SetProperty ("SWEW", width);

   if (reqCount != null)
      pset.SetProperty ("SWEC", reqCount);

   if (keepCtx != null)
      pset.SetProperty ("SWEKeepContext", keepCtx);

   SWEJSSClosePopup();

   var frame = SWEJSSFindAppletFrame (viewId, applet);
   var fullAppletVar = 'frame.' + appletVar;
   eval ('if (typeof frame != "undefined" && ' + fullAppletVar + ') ' + fullAppletVar + '.InvokeMethod (method, pset)');

   pset.axObj = null;

   Top().SetBusy(false);
}


// JAVASCRIPT FUNCTION -- Create Hidden Fields in HTML Forms (J.Huang) 
function c_h (id, inputNames, inputValues) 
{
   var size;
   var text;
   var elem;
	if (inputValues.length != inputNames.length)
	{
      SWEAlert ("error found in writting hidden inputs");
   }
	if (inputValues.length > inputNames.length)
	{
		size = inputNames.length;
	}
	else
	{
		size = inputValues.length;
	}
   
   text = "";
   for (var i = 0; i < size; i++) 
   {
       text += "<input type=\"hidden\" name='" + inputNames[i] + "' value='" + HtmlEncode(inputValues[i]) + "'>";
   }
   elem = document.getElementById(id);
   if (elem)
      elem.innerHTML = text;
}


function SWERefresh(targetFrame, url, targetWindow)
{
   if (url == null || url == "")
   {    
      if (SWEIsAutomation)
         Top().SetBusy (true);

      if (top.opener != null && top.opener.SWEDoRefresh != null)
         top.opener.SWEDoRefresh();      
      else if (this.SWEDoRefresh != null)
         SWEDoRefresh();            
   }
   else
   {
      if (targetWindow != "undefined" && targetWindow == "_self")
      {
         SWETargetGotoURL(url, top);
      }
      else if (top.opener != null && top.opener.SWEDoRefresh != null)
         SWETargetGotoURL(url, top.opener);
      else if (targetFrame != null && targetFrame != "")
      {
         var targetFrameObj = eval(targetFrame);
         SWETargetGotoURL(url, targetFrameObj);
      }
    }
}

function SWEJSS_gotoText(id)
{
	var select	= document.all[id + "sel"];
	var text	= document.all[id + "txt"];
	var button	= document.all[id + "btn"];
	
        if (select.style.display != "none")
        	text.value = select.options(select.options.selectedIndex).innerText;
	select.style.display = "none";
	text.style.display = "block";
	text.disabled = true;
        button.style.display = "block";
}

// Submit Form when ENTER key pressed on an Input. (For both IE and NS)
function SWESubmitOnEnter (e)
{
  var keyCode = e ? e.which : window.event.keyCode;
  var formObj = e ? e.target.form : window.event.srcElement.form;

  if (keyCode == 13)
  {
    if (typeof(formObj.SWETS) != 'undefined') //always append timestamp
    {
       var now  = new Date();
       formObj.SWETS.value = now.getTime();
    }

    formObj.submit();
  }
}

var g_sweobj = null;
var g_oInterval = null;
var g_swecmd1 = null;
var g_swecmd2 = null;

function RecurGetJSS ()
{
   eval (g_swecmd2);
   if (g_sweobj != null)
   {
      eval (g_swecmd1);
      clearInterval (g_oInterval);
   }
}

function GetJSSInfo (sweobj, cmd1, cmd2)
{
   g_swecmd1 = cmd1;
   g_swecmd2 = cmd2;  
   if (sweobj == null)
      g_oInterval = window.setInterval ("RecurGetJSS()", 3000);   
   else
      eval (cmd1);
}  


var shutdown = false;
function HandleEvent(event, lparam, rParam) 
{ 
  
   switch (event)
   {
      case "Disable":

         if (document.all)
         {
            if (lparam)
            {
               if (dvBusy.style.display!="block")
               {
                  dvBusy.style.width = document.body.clientWidth;
                  dvBusy.style.height = document.body.scrollHeight;

                  dvBusy.style.display="block";

                  if (Top().g_menuCursor != null)
                     dvBusy.style.cursor=Top().g_menuCursor;
                  else
                     dvBusy.style.cursor="wait";

                  if (Top().doBusyScroll)
                  {
                     dvScroll.scrollTop = 500;
                     dvScroll.scrollTop = 0;
                  }
               }
            }
            else
            {
               if (dvBusy.style.display!="none")
               {
                  dvBusy.style.display="none";

                  if (Top().g_menuCursor != null)
                     dvBusy.style.cursor=Top().g_menuCursor;
                  else
                     dvBusy.style.cursor="default";

                  if (Top().doBusyScroll)
                  {
                     dvScroll.scrollTop = 500;
                     dvScroll.scrollTop = 0;
                  }
               }
            }

          if (document.getElementById("cms_send"))
             GetAndSendcookie();
         }
         break;

      case "YieldToPopup":

         if (top == Top () && document.all && dvUIBlock) // for main window only
         {
            if (lparam)
            {
               if (dvUIBlock.style.display != "block")
               {
                  dvUIBlock.style.pixelWidth = document.body.scrollWidth;
                  dvUIBlock.style.pixelHeight = document.body.scrollHeight;
                  dvUIBlock.style.display = "block";
               }
            }
            else
            {
               if (dvUIBlock.style.display != "none")
               {
                  dvUIBlock.style.display = "none";
               }
            }
         }
         break;

      case "ShutDown":
         shutdown = true;
         break;

      case "SetScreen":
         //lparam is the screen name
         SWESetScreen(lparam);
         break;
      
      case "UserQuery":
         var elem = document.getElementById("s_pdq");
         if (elem)
         {
            if (elem.options[0].value != "")
            {
               var newOpt = document.createElement("OPTION");
               newOpt.text = "";
               newOpt.value = "";
               elem.options.add(newOpt,0);
            }
            elem.selectedIndex = 0;
         }
         break;

      default:
         break;
   }
}

//This section has to stay in sync with the server side
if (typeof swetop == "undefined") 
{
	var swetop = null;
	// find the topmost window, but make sure it's a valid reference
	var parentWindow;
	var currentWindow;
	if (typeof window.parent != "undefined")
	{
		parentWindow = window.parent;
		currentWindow = window;
		while (currentWindow.parent != currentWindow ) 
		{
			if (typeof parentWindow.swetop != "unknown" && typeof parentWindow.swetop != "undefined") 
			{
				swetop = parentWindow.swetop;
				break;
			}
			if (typeof currentWindow.parent != "undefined")  
			{
				currentWindow = parentWindow;
				parentWindow = parentWindow.parent;
			} 
			else 
				break;
		}
	}
	// find the "top" window" in case swetop was not yet found but is defined in the opener
        // 12-F90FGC Need to check for unknown for opener we don't have access to
	if (swetop == null && window.opener != null && typeof window.opener.swetop != "unknown")
		swetop = window.opener.swetop;
	
	if (swetop == null)
	   swetop = window;
}

function Top()
{
   return swetop;
}
   

function App() { return Top()._swe._sweapp.S_App; }

function SWESetScreen(screenTab)
{
   var objFrame = SWEFindFrame(top, "_swescrnbar");
   if (objFrame == null || objFrame != this)      return;
   st_scrn(screenTab.name, -1, screenTab);
}


function SWEPopupHide()
{
   if ((typeof SWEPopupResizeNoHide != "undefined" && 
       SWEPopupResizeNoHide ))
      return;
    
   
   var popWin  = Top().SWEPopupWin;
   
   if ((popWin == null) || 
       (!Top().SWEIsHighInteract && popWin.closed) ||
       (Top().SWEIsHighInteract && App().popup.bUnloaded))
       
      popWin = window;

   if (   (popWin != null)
        &&((!Top().SWEIsHighInteract && !popWin.closed)||(Top().SWEIsHighInteract && !App().popup.bUnloaded))
       )
   {
      var body    = document.body;
      var deltaX = 100 - body.offsetWidth;
      var deltaY = 100 - body.offsetHeight;
      
      popWin.moveTo(0, 10000);
      popWin.resizeBy(deltaX, deltaY);   
      if (Top().SWEIsHighInteract)
      {
         App().popup.visible = false;
      }
   }
}


function SWEExecuteCommand(formObj, action, command, bPopup, height, width, target)
{
   if (! g_bInitialized)
      return;

   if (action != null)
      formObj.action = action;

   if (command != null)
      formObj.SWECmd.value = command;

   if (bPopup == "true" || bPopup == "TRUE")
   {
      SWEShowPopup ("", parseInt(height), parseInt(width), "false");
   }

   if (target != null && target != "")
   {
      formObj.target = target;
   }

   if (SWEIsHighInteract)
      App().SubmitForm(formObj);
   else
   {
      if (SWEIsAutomation)
         Top().SetBusy (true);
      formObj.submit();
   }
}

function SWEPersonalizationDrillDown (viewName, appletName, fieldName, rowId, parentRowIds)
{
   SWEPersonalizationDrillDown_top (viewName, appletName, fieldName, rowId, parentRowIds, this);
}

function SWEPopupSize()
{
   var resized = false;

   var popWin  = Top().SWEPopupWin;
   
   if ((popWin == null) || (popWin.closed))
      popWin = window;

   if ((popWin != null) && (!popWin.closed))
   {
      var screenX = SWEGetAvailScreenWidth();
      var screenY = SWEGetAvailScreenHeight();
      if (typeof Top().SWEPopupResizeNoAuto != "undefined" && 
          Top().SWEPopupResizeNoAuto )
      {
         Top().SWEPopupResizeNoAuto = false;
         popWin.resizeTo(Top().SWEPopupWidth, Top().SWEPopupHeight);
         popWin.moveTo(Math.floor((screenX-Top().SWEPopupWidth) / 2), Math.floor((screenY-Top().SWEPopupHeight) / 2 ));
      }
      else 
      {
         var body    = document.body;
         var minX    = (body.minWidth != null) ? body.minWidth : 0;
         var bodyX   = (body.scrollWidth > minX) ? body.scrollWidth : minX;
         var bodyY   = body.scrollHeight;
         var maxX    = (bodyX < screenX) ? bodyX : screenX;
         var maxY    = (bodyY < screenY) ? bodyY : screenY;
         var deltaX  = maxX - body.clientWidth;
         var deltaY  = maxY - body.clientHeight;
   
         if (Math.abs(deltaX) > 4 || Math.abs(deltaY) > 4)
         {
            popWin.resizeBy(deltaX, deltaY);
            resized = true;
         }
         if (resized || popWin.screenTop > screenY || popWin.screenTop <0)
            popWin.moveTo(Math.floor((screenX-maxX) / 2), Math.floor((screenY-maxY) / 2 ));
      }
      if (Top().SWEIsHighInteract)
      {
         App().PopupAppletOnload(); 
         App().popup.visible = true;
	     popWin.focus();
      }
   }
}


function SWEPopupNNSize() 
{  
   var popWin  = Top().SWEPopupWin;
   if ((popWin == null) || (popWin.closed))
      popWin = window;

   popWin.scrollTo(10000,10000);

   setTimeout("NNResize()",10);
}

function SWESubmitForm (csFormObj, csObj, fieldName, rowId, csFrame)
{
   var height=480, width=640;
   var newWindow = false;
   var objFrame, obj, formObj;

   if (SWEIsAutomation)
      Top().SetBusy (true);

   if (typeof(csFrame) != 'undefined')
   {
      //objFrame = SWEFindFrame(top, csFrame);
      objFrame = csFrame;
      if (objFrame == null)
      {
         SWEAlert("Fail to submit form due to invalid objFrame!");
         return;
      }
      formObj = objFrame[csFormObj];
      obj = objFrame[csObj];
   }
   else
   {
      obj = csObj;
      formObj = csFormObj;
   }

   if (pendingChanges()&& 
       ((typeof(obj.SWEApplet) != 'undefined' && typeof(top.SWEApplets) != 'undefined' && typeof(top.SWEApplets[top.CurrentAppletIndex]) != 'undefined' && obj.SWEApplet != top.SWEApplets[top.CurrentAppletIndex].Name) || 
       (typeof(obj.SkipDataLossWarning) == 'undefined' && StopForSavingData())))
   {
      if (SWEIsAutomation)
        Top().SetBusy (false);
      return;
   }
  
   SWESubmitForm_top (obj, formObj, fieldName, rowId);
  
   if (typeof(obj.SWESPNH) != 'undefined')
      SWEPopupResizeNoHide = true;
      
   if (typeof(obj.SWEH) != 'undefined')
      height = obj.SWEH;

   if (typeof(obj.SWEW) != 'undefined')
      width = obj.SWEW;

   if (typeof(obj.SWESPNR) != 'undefined')
   {
      Top().SWEPopupResizeNoAuto = true;
      Top().SWEPopupHeight = height;
      Top().SWEPopupWidth = width;
   }

   if (typeof(obj.SWESP)  == 'TRUE' || obj.SWESP  == 'true')
   {
      if (formObj.SWETVI.value ==  Top().SWEHtmlPopupName &&
          formObj.SWETA.value == "" )
         newWindow= SWEDoShowPopup ("", height, width, "false", "false", "scrollbars");
      else
         newWindow= SWEDoShowPopup ("", height, width, "false");

      formObj.target = Top().SWEHtmlPopupName;

      if (typeof(formObj.SWESP) != 'undefined')
         formObj.SWESP.value = obj.SWESP;
   }

   if (typeof(top._samePage) != 'undefined' && top._samePage!="")
    {
       formObj.SWESPa.value = top._samePage;
       top._samePage = "";
    }
    else if (formObj.SWEPOC.value != "")
       top._samePage = "1";

    if (SWEIsHighInteract)
       App().SubmitForm(formObj);
    else
    {
       if (SWEIsAutomation)
         Top().SetBusy (true);
       formObj.submit();
    }

   if (SWEIsAutomation)
      Top().SetBusy (false);
}

function NNResize() 
{
   var popWin  = Top().SWEPopupWin;
   if ((popWin == null) || (popWin.closed))
      popWin = window;

   var screenX = SWEGetAvailScreenWidth();
   var screenY = SWEGetAvailScreenHeight(); 

   if (navigator.userAgent.indexOf("Mozilla/5.0") >= 0) 
   {
   // fix for defect 12-FTAGAQ that in Netscape 7.0x, sizeToContent() set wrong size
	if(navigator.userAgent.indexOf("Netscape/7.") < 0) // for Netscape 6.2
   		window.sizeToContent();
    else // for Netscape 7.0x
    {
      var offX = window.pageXOffset;
	// fix for defect 12-FTAGAQ
	// In NS 7.0x, window.pageYOffset + window.content.innerHeight != window.document.height
	// here we use the larger value of the two to ensure all content is visible
      var newY = window.pageYOffset + window.content.innerHeight; 
	  if(newY < window.document.height)
	  	newY = window.document.height;
	  	
      if (offX > screenX - window.outerWidth)
         offX = screenX - window.outerWidth; 
      if (newY > screenY)
         newY = screenY;

      window.resizeTo(offX+window.outerWidth, newY);
      //In MVG popup, after resizeTo(), pageYOffset is still not 0, resize again.
      window.resizeBy(0, window.pageYOffset);
    }
   }
   else
   {
      var offX = window.pageXOffset;
      var offY = window.pageYOffset; 

      if (offX > screenX - window.outerWidth)
         offX = screenX - window.outerWidth; 
      if (offY > screenY - window.outerHeight)
         offY = screenY - window.outerHeight;

      window.resizeBy(offX, offY);
   }
   popWin.moveTo(Math.floor((screenX-window.outerWidth) / 2), Math.floor((screenY-window.outerHeight) / 2 ));
}

function SweClickAddPopup()
{
  if (typeof(s_swe_filepop_add) != 'undefined')
  {
      s_swe_filepop_add.click();
  }
}

//Pointer to functions in the hidden frame
if (typeof (Top()._swescript) != "undefined")
{
	var hiddenFrame = Top()._swescript;
	c_d = hiddenFrame.c_d;
	EvalFrame = hiddenFrame.EvalFrame;
	HtmlEncode = hiddenFrame.HtmlEncode;
	pendingChanges = hiddenFrame.pendingChanges;
	ResetScreen = hiddenFrame.ResetScreen;
	SWEAlert = hiddenFrame.SWEAlert;
	SetAppCookie = hiddenFrame.SetAppCookie;
	SetScreen = hiddenFrame.SetScreen;
	StopForSavingData = hiddenFrame.StopForSavingData;
	SWEAppendArgsToURL = hiddenFrame.SWEAppendArgsToURL;
	SWEClosePopup = hiddenFrame.SWEClosePopup;
	SWEConfirm = hiddenFrame.SWEConfirm;
	SWEFormReset = hiddenFrame.SWEFormReset;
	SWEGetAvailScreenHeight = hiddenFrame.SWEGetAvailScreenHeight;
	SWEGetAvailScreenWidth = hiddenFrame.SWEGetAvailScreenWidth;
	SWEGetScreenHeight = hiddenFrame.SWEGetScreenHeight;
	SWEGetScreenWidth = hiddenFrame.SWEGetScreenWidth;
	SWEJSSEnterColInput = hiddenFrame.SWEJSSEnterColInput;
	SWEJSSEnterField = hiddenFrame.SWEJSSEnterField;
	SWEJSSFindAppletFrame = hiddenFrame.SWEJSSFindAppletFrame;
	SWEJSSGetAppletObj = hiddenFrame.SWEJSSGetAppletObj;
	SWEJSSGetAppletObjShadow = hiddenFrame.SWEJSSGetAppletObjShadow;
	SWEJSSLeaveColInput = hiddenFrame.SWEJSSLeaveColInput;
	SWEJSSLeaveField = hiddenFrame.SWEJSSLeaveField;
	SWEPersonalizationDrillDown_top = hiddenFrame.SWEPersonalizationDrillDown_top;
	SWEPersonalizationGotoScreen = hiddenFrame.SWEPersonalizationGotoScreen;
	SWEPersonalizationGotoview = hiddenFrame.SWEPersonalizationGotoview;
	SWEPopupGainFocus = hiddenFrame.SWEPopupGainFocus;
	SWEPopupMessage = hiddenFrame.SWEPopupMessage;
	SWEPosition = hiddenFrame.SWEPosition;
	SWEShowFeaturedPopup = hiddenFrame.SWEShowFeaturedPopup;
	SWESubmitForm_top = hiddenFrame.SWESubmitForm_top;
	SWEUnloadApp = hiddenFrame.SWEUnloadApp;
        SWEUnloadAppQueryPara = hiddenFrame.SWEUnloadAppQueryPara;
	SweValidateSubmit = hiddenFrame.SweValidateSubmit;
	trackChange = hiddenFrame.trackChange;
	URLEncode = hiddenFrame.URLEncode;
}


function SWEGotoPageRC(PageName,url, target, bLeaveApplet)
{ 
   var pset = top._swescript.CCFMiscUtil_CreatePropSet ();
   pset.SetProperty ("PageItem", PageName);
   App().FireRecorderEvent(App().GetName(), "GoToContainerPageItem", 0, 0, "", pset);
   
   SWETargetGotoURL(url, target, bLeaveApplet); 
}

