// Window Properties JavaScript Functions
// File:    windowProperties.js
// Author:  Clinton Farleigh
// WEBCAT:  2.2
// Version: 1.3
// History:
//    17 Sep 2001  V1.0: Initial creation...CJF
//    26 Sep 2001  Added writeFrames function - to write out
//                 Frame information for the parent...CJF
//    15 Nov 2001  Added percent for location on screen...CJF
//    19 Dec 2001  Rewrote writeFrame to handle compatibility with Netscape
//		   Added several helper functions
//		   (writeSingleFrame(), openFrameSet(), closeFrameSet())...CJF
//    17 Jan 2002  Added functions and array to keep track of child window...CJF
//	  21 May 2002  Added null check to trimString function...CJF
//    21 May 2002  Added getWindowName (_wp_getWindowName) accessor method...CJF
//	  27 Sep 2002  

//  ==============================================
//  PROPERTIES :
//  Numerical :
//  mnScreenX - X Coordinate of the left hand corner of the window.
//  mnScreenY - Y Coordinate of the left hand corner of the window.
//  mnWidth   - Width of the window.
//  mnHeight  - Height of the window.
//  mnControlFramePercent - Percent of window that control takes up.
//
//  Textual :
//  msControl - The target location (Name of frame) of the control menu.
//
//  Boolean :
//  mbDependant - *** not sure what this is *** (MenuContent.xsl)
//  mbDirectories - Indicates whether personal (directories) bar is displayed.
//  mbLocation - Indicates whether location bar is displayed.
//  mbMenuBar - Indicates whether menu bar is displayed.
//  mbResizable - Indicates whether window is resizable.
//  mbScrollbars - Indicates whether window has scrollbars.
//  mbStatus - Indicates whether window has status bar.
//  mbToolbar - Indicates whether window has toolbar.
//  msWindowName - Name of the last window opened using this windowProperties object.

// Global variables.
var currentWPObject = null ; // Current windowProperties object - used for frame writing.

var childWindows = new Array( ) ; // Array of child windows - used to control closure of 
								  // child windows.

// Default Properties (Global)
// Order :
// mnScreenX,mnScreenY,mnWidth,mnHeight,mnControlFramePercent,msControl,mbDependant,mbDirectories
// mbLocation,mbMenuBar,mbResizable,mbScrollbars,mbStatus,mbToolBar
var EntryProp =  [ 0, 3, 95, 80, 30, 'L', 0, 0, 0, 1, 1, 1, 0, 0 ] ;
var BrowseProp = [ 0, 3, 95, 80, 18, 'L', 0, 0, 0, 1, 1, 1, 0, 0 ] ;
var SearchProp = [ 0, 3, 95, 80, 30, 'L', 0, 0, 0, 1, 1, 1, 0, 0 ] ;
var MenuProp =   [ 0, 3, 90, 80, 30, 'L', 0, 0, 0, 1, 1, 1, 0, 0 ] ;
var DetailProp = [ 3, 0, 95, 80, 30, 'L', 0, 0, 0, 1, 1, 1, 0, 0 ] ;
var NavigationProp = [ 0, 5, 95, 80, 30, 'L', 0, 0, 0, 1, 1, 1, 0, 0 ] ;
var OtherProp =  [ 3, 0, 95, 80, 30, 'L', 0, 0, 0, 1, 1, 1, 0, 0 ] ;
var HelpProp =  [ 25, 25, 50, 50, 30, 'T', 0, 0, 0, 1, 1, 1, 0, 0 ] ;

//===============================================================
//  trimString - Helper function to remove whitespaces from string.
//===============================================================
function trimString( str ) 
{
  if ( !str ) return "" ;
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}


//===============================================================
//  addChildWindow - Helper function to keep track of windows
//					 opened by the current window. (ADD)
//===============================================================
function addChildWindow( inChildWH )
{
	// Add child to the array (Array is in frame of all but menu).
	var aWin = self ;
	if ( self.parent && self.parent.typeID != "Menu" )
	{
		aWin = self.parent ;
	}
	aWin.childWindows[ aWin.childWindows.length ] = inChildWH ;
}

//===============================================================
//  removeChildWindow - Helper function to keep track of windows 
//						opened by the current window. (REMOVE)
//===============================================================
function removeChildWindow( inChildWH ) {
	var aWin = self ;
	if ( self.parent && self.parent.typeID != "Menu" ) {
		aWin = self.parent ;
	}
	for ( var aiCtr = 0 ; aiCtr < aWin.childWindows.length ; aiCtr++ ) {
		
		if ( inChildWH == aWin.childWindows[ aiCtr ] ) {
			aWin.childWindows[ aiCtr ] = null ; // *** temporarily use this until IE5.0 no longer supported ***
			return ;
		}
	}
	return ; // assume that is current window.
}

//===============================================================
//  closeWindow - closes child window.
//===============================================================
function closeWindow( inClose ) {
	// Close all children in the list.
	for ( aiCtr = 0 ; aiCtr < childWindows.length ; aiCtr++ ) {
		var aChild = childWindows[ aiCtr ] ;
		if ( aChild != null && aChild && !aChild.closed ) {
			if ( aChild.closeWindow ) {
				// Recursively call this function on children until all is closed.
				aChild.closeWindow( true ) ;
			}
			else {
				aChild.close( ) ;		
			}
		}
	}
	// Remove self from the children of opener.
	if ( self && self.opener && self.opener.removeChildWindow ) {
		self.opener.removeChildWindow( self ) ;
	}
	// Close the current window if required.
	if ( self && inClose == true ) { 
		self.close( ) ;
	}
}

//===============================================================
//  writeFrame( ) - Write out frame html content of the parent
//                  page.  Content is customized based on
//                  configuration.
//===============================================================
function writeFrame( inControlName,
                     inControlURL,
                     inContentName,
                     inContentURL,
                     inFrameBorder,
                     inOptions,
                     inWebpage ) {
    // Inner frame variables.
	var aInnerControl = "" ;
	var aInnerSize = "" ;
    var aInnerFrame1 = "" ;
    var aInnerFrame1Src = "" ;
    var aInnerFrame2 = "" ;
    var aInnerFrame2Src = "" ;
    var aOptions = null ;
    if ( inOptions != "" && inOptions != null )
    {
	    aOptions = inOptions.split( "|" ) ;
	    aInnerFrame1    = aOptions[0] ; // Name of the first inner frame.
	    aInnerFrame1Src = aOptions[1] ; // Source of the first inner frame
	    aInnerFrame2    = aOptions[2] ; // Name of the second inner frame.
	    aInnerFrame2Src = aOptions[3] ;	// Source of the second inner frame.
	    for ( var aCtr = 4 ; aCtr < aOptions.length ; aCtr++ )
	    {
	        if ( aOptions[ aCtr ] == 'POS' )
	        {
	          aCtr++ ;
	          aInnerControl = aOptions[aCtr] ;
	        }
	        else if ( aOptions[ aCtr ] == 'SIZE' )
	        {
	          aCtr++ ;
	          aInnerSize = aOptions[aCtr] ;
	        }
	    }
    }
    // Local variables for the outer frame.
    var an1Percent = null ;
    var an2Percent = null ;
    var aName1  = null ;
    var aName2 = null ;
    var aURL1 = null ;
    var aURL2 = null ;
    var aFramePos = null ;
    // Local variables for the inner frame.
    var anInner1Percent = null ;
    var anInner2Percent = null ;
    var aInnerName1  = null ;
    var aInnerName2 = null ;
    var aInnerURL1 = null ;
    var aInnerURL2 = null ;
    var aInnerFramePos = null ;
    if ( aOptions != null )
    {
	    // Determine way the control and content frames are positioned.
	    if ( aInnerControl.charAt(0) == 'R' || aInnerControl.charAt(0) == 'B' )
	    {
	        anInner1Percent = 100 - aInnerSize ;
	        anInner2Percent = aInnerSize ;
	        aInnerName1 = aInnerFrame2 ;
	        aInnerName2 = aInnerFrame1  ;
	        aInnerURL1  = aInnerFrame2Src   ;
	        aInnerURL2  = aInnerFrame1Src   ;
	    }
	    else
	    {
	        anInner1Percent = aInnerSize ;
	        anInner2Percent = 100 - aInnerSize ;
	        aInnerName2 = aInnerFrame2 ;
	        aInnerName1 = aInnerFrame1 ;
	        aInnerURL2  = aInnerFrame2Src   ;
	        aInnerURL1  = aInnerFrame1Src   ;
	    }
    }
    if ( this.msControlPos.charAt(0) == 'R' || this.msControlPos.charAt(0) == 'B' )
    {
        an1Percent = 100 - this.mnControlFramePercent ;
        an2Percent = this.mnControlFramePercent ;
        aName1 = inContentName ;
        aName2 = inControlName  ;
        aURL1  = inContentURL   ;
        aURL2  = inControlURL   ;
    }
    else
    {
        an1Percent = this.mnControlFramePercent ;
        an2Percent = 100 - this.mnControlFramePercent ;
        aName2 = inContentName ;
        aName1 = inControlName  ;
        aURL2  = inContentURL   ;
        aURL1  = inControlURL   ;
    }
    aFramePos = ( this.msControlPos.charAt(0) == "T" || this.msControlPos.charAt(0) == "B" ) ? "ROWS" : "COLS" ;
    aInnerFramePos = ( aInnerControl.charAt(0) == "T" || aInnerControl.charAt(0) == "B" ) ? "ROWS" : "COLS" ;
    inWebpage.document.open( ) ;
	openFrameSet( inWebpage, aFramePos, an1Percent, an2Percent, inFrameBorder  ) ;
    if ( anInner1Percent != null )
    {
    	if ( aInnerControl.charAt(0) == 'T' || aInnerControl.charAt(0) == 'L' )
    	{
    		writeSingleFrame( inWebpage, aName1, aURL1 ) ;
    		openFrameSet( inWebpage, aInnerFramePos, anInner1Percent, anInner2Percent, 0 ) ;
     		writeSingleFrame( inWebpage, aInnerName1, aInnerURL1 ) ;
     		writeSingleFrame( inWebpage, aInnerName2, aInnerURL2 ) ;
    		closeFrameSet( inWebpage ) ;
    	}
    	else
    	{
    		openFrameSet( inWebpage, aFramePos, anInner1Percent, anInner2Percent, 0 ) ;
     		writeSingleFrame( inWebpage, aInnerName1, aInnerURL1 ) ;
     		writeSingleFrame( inWebpage, aInnerName2, aInnerURL2 ) ;
    		closeFrameSet( inWebpage ) ;
    		writeSingleFrame( inWebpage, aName1, aURL1 ) ;
    	}
   	}
   	else
   	{
   		writeSingleFrame( inWebpage, aName1, aURL1 ) ;
   		writeSingleFrame( inWebpage, aName2, aURL2 ) ;
   	}
	closeFrameSet( inWebpage ) ;
    inWebpage.document.close( ) ;
}

//===============================================================
//  writeSingleFrame( ) - Write out frames with given names and source
//					 to document specified.
//===============================================================
function writeSingleFrame( inWebpage, inName, inURL )
{
    inWebpage.document.writeln( "<FRAME NAME=\"" +
                               inName +
                               "\" SRC=\"" +
                               inURL +
                               "\"></FRAME>" ) ;
}

//===============================================================
//  writeFrames( ) - Write frameset tag to the document.
//===============================================================
function openFrameSet( inWebpage, inFramePos, in1Percent, in2Percent, inFrameBorder ) {
    inWebpage.document.writeln( "<FRAMESET ONRESIZE=\"top.nnReload( );\" " + // ONUNLOAD=\"top.closeWindows();\"
                              inFramePos +
                              "=\"" +
                              in1Percent +
                              "%" +
                              ", " +
                              in2Percent +
                              "%\"" ) ;
    if ( inFrameBorder == 0 )
    {
        inWebpage.document.write( " BORDER=\"" +
                                  inFrameBorder +
                                  "\"" ) ;
    }
    inWebpage.document.write( " >" ) ;
}

//===============================================================
//  closeFrameSet( ) - Write closing frameset tag to the document
//===============================================================
function closeFrameSet( inWebpage ) {
	inWebpage.document.writeln( "</FRAMESET>" ) ;
}

function _wp_getWindowName( ) {
	return this.msWindowName ;
}

//===============================================================
//  getWindowProperties( ) - Construct window features on the fly.
//===============================================================
function getWindowProperties( ) {
  // Make any modifications to properties here.
  var anWidth = ( this.mnWidth / 100 ) * screen.availWidth ;
  var anHeight = ( this.mnHeight / 100 ) * screen.availHeight ;
  var anX = ( this.mnScreenX / 100 ) * screen.availWidth  ;
  var anY = ( this.mnScreenY / 100 ) * screen.availHeight ;
  var asProperties = "width=" + anWidth +
                     ",height=" + anHeight +
                     ",screenX=" + anX +
                     ",screenY=" + anY +
                     ",left=" + anX +
                     ",top=" + anY ;
  asProperties += ",dependent=" ;
  asProperties += ( this.mbDependent == 1 ) ? "yes" : "no" ;
  asProperties += ",personalbar=" ;
  asProperties += ( this.mbDirectories == 1 ) ? "yes" : "no" ;
  asProperties += ",location=" ;
  asProperties += ( this.mbLocation == 1 ) ? "yes" : "no" ;
  asProperties += ",menubar=" ;
  asProperties += ( this.mbMenuBar == 1 ) ? "yes" : "no" ;
  asProperties += ",resizable=" ;
  asProperties += ( this.mbResizable == 1 ) ? "yes" : "no" ;
  asProperties += ",scrollbars=" ;
  asProperties += ( this.mbScrollbars == 1 ) ? "yes" : "no" ;
  asProperties += ",status=" ;
  asProperties += ( this.mbStatus == 1 ) ? "yes" : "no" ;
  asProperties += ",toolbar=" ;
  asProperties += ( this.mbToolBar == 1 ) ? "yes" : "no" ;

  return asProperties ;

}

//===============================================================
//  openNewWindow( ) - open a new window with properties
//  Assumptions : First frame in inFrames is always the control frame.
//                If inFrames=="" then do not include frames. (eg. detail)
//                If there are frames, inURL , inFrames
//                are | delimited. ( eg. "http://abc.com|http://test.com"
//                and "ControlFrame|BrowseData".
//===============================================================
function openNewWindow( inName, inURL ) {
    var asProperties = this.getWindowProperties( ) ;
    currentWPObject = this ;
    //alert( this.mnScreenX + " "
    //      + this.mnScreenY + " "
    //      + this.mnWidth + " "
    //      + this.mnHeight + " "
    //      + this.mnControlFramePercent
    //      + this.msControl + " "
    //      + this.mbDependent + " "
    //      + this.mbDirectories + " "
    //      + this.mbLocation + " "
    //      + this.mbMenuBar + " "
    //      + this.mbResizable + " " +
    //      + this.mbScrollbars + " " +
    //      + this.mbStatus + " " +
    //      + this.mbToolBar ) ;
    var temp = top.name.split('_');
    var ctr = 0 ;
    if (inName == temp[0]) {
        ctr = parseInt(temp[1],10) + 1;
    } else {
        ctr = 1;
    }
    var today = new Date( ) ;
    // Create a unique window name.
    var wName = inName + '_' + ctr + '_' + today.getTime() ;
	this.msWindowName = wName ;
    var aWinHandle = window.open( inURL , wName, asProperties ) ;
	// Add the child window to the current window's list.
	//alert( "adding child window (OPEN)" ) ;
	addChildWindow( aWinHandle ) ;
    //aWinHandle.focus( ) ; // *** Took out temporarily ***
    this.mWindowOpened = aWinHandle ;
    return aWinHandle ;
}


//===============================================================
//  getDefault( ) - Returns default property for type of screen.
//===============================================================
function getDefault( inProp, inType ) {
  // takes in the type of the window and displays defaults based
  // on this type. (inType = entry, browse, search, menu)
  var aProp = null ;
  inProp = inProp ;
  switch( inType )
  {
        case "Entry" :
            aProp = EntryProp[ inProp ] ;
            break ;
        case "Browse" :
            aProp = BrowseProp[ inProp ] ;
            break ;
        case "Search" :
            aProp = SearchProp[ inProp ] ;
            break ;
        case "Menu" :
            aProp = MenuProp[ inProp ] ;
            break ;
        case "Detail" :
            aProp = DetailProp[ inProp ] ;
            break ;
        case "Navigation" :
            aProp = NavigationProp[ inProp ] ;
            break ;
		case "Help" :
			aProp = HelpProp[ inProp ] ;
			break ;
        default :
            aProp = OtherProp[ inProp ] ;
            break ;
  }
  return aProp;
}

//===============================================================
//  windowProperties( ) - Constructor.
//===============================================================

function windowProperties( inScreenX,
                           inScreenY,
                           inWidth,
                           inHeight,
                           inControlFramePercent,
                           inControl,
                           inDependent,
                           inDirectories,
                           inLocation,
                           inMenuBar,
                           inResizable,
                           inScrollbars,
                           inStatus,
                           inToolBar,
                           inType ) {
  // Properties of object.
  this.msType = ( inType != "" && inType != null ) ? inType : "Other" ;
  this.mnScreenX = ( trimString( inScreenX ) != "" && inScreenX != null ) ? inScreenX : getDefault( 0 , inType ) ;
  this.mnScreenY = ( trimString( inScreenY ) != "" && inScreenY != null ) ? inScreenY : getDefault( 1 , inType ) ;
  this.mnWidth =   ( trimString( inWidth ) != "" && inWidth != null ) ? inWidth : getDefault( 2, inType ) ;
  this.mnHeight =  ( trimString( inHeight ) != "" && inHeight != null ) ? inHeight : getDefault( 3, inType ) ;
  this.mnControlFramePercent = ( trimString( inControlFramePercent ) != "" && inControlFramePercent != null ) ? inControlFramePercent : getDefault( 4, inType ) ;
  this.msControl = ( trimString( inControl ) != "" && inControl != null ) ? inControl : getDefault( 5, inType ) ;
  this.mbDependent = ( trimString( inDependent ) != "" && inDependent != null ) ? inDependent : getDefault( 6, inType ) ;
  this.mbDirectories = ( trimString( inDirectories ) != "" && inDirectories != null ) ? inDependent : getDefault( 7, inType ) ;
  this.mbLocation = ( trimString( inLocation ) != "" && inLocation != null ) ? inLocation : getDefault( 8, inType ) ;
  this.mbMenuBar =  ( trimString( inMenuBar ) != "" && inMenuBar != null ) ? inMenuBar : getDefault( 9, inType ) ;
  this.mbResizable = ( trimString( inResizable ) != "" && inResizable != null ) ? inResizable : getDefault( 10, inType ) ;
  this.mbScrollbars = ( trimString( inScrollbars ) != "" && inScrollbars != null ) ? inScrollbars : getDefault( 11, inType ) ;
  this.mbStatus = ( trimString( inStatus ) != "" && inStatus != null ) ? inStatus : getDefault( 12, inType ) ;
  this.mbToolBar = ( trimString( inToolBar ) != "" && inToolBar != null ) ? inToolBar : getDefault( 13, inType ) ;
  // Methods of object.
  this.openNewWindow = openNewWindow ;
  this.getWindowProperties = getWindowProperties ;
  this.writeFrame = writeFrame ;
  this.getWindowName = _wp_getWindowName ;
}

//===============================================================
//  windowProperties( ) - Constructor for string.
//===============================================================
function windowProperties( inWindowInfo ) {
  // Property declarations
  var inType = '' ;
  var inScreenX = '' ;
  var inScreenY = '' ;
  var inWidth = '' ;
  var inHeight = '' ;
  var inControlFramePercent = '' ;
  var inControl = '' ;
  var inDependent = '' ;
  var inDirectories = '' ;
  var inLocation = '' ;
  var inMenuBar = '' ;
  var inResizable = '' ;
  var inScrollbars = '' ;
  var inStatus = '' ;
  var inToolBar = '' ;
  // Split string
  var aWinArray = inWindowInfo.split( "|" ) ;
  if ( aWinArray.length >= 14 ) {
	  inType = aWinArray[14] ;
	  inScreenX = aWinArray[9] ;
	  inScreenY = aWinArray[10] ;
	  inWidth = aWinArray[11] ;
	  inHeight = aWinArray[8] ;
	  inControlFramePercent = aWinArray[13] ;
	  inControl = aWinArray[12] ;
	  inDependent = aWinArray[0] ;
	  inDirectories = aWinArray[1] ;
	  inLocation = aWinArray[2] ;
	  inMenuBar = aWinArray[3] ;
	  inResizable = aWinArray[4] ;
	  inScrollbars = aWinArray[5] ;
	  inStatus = aWinArray[6] ;
	  inToolBar = aWinArray[7] ;
  } // else ignore the input string
  // Properties of object.
  this.msType = ( inType != "" && inType != null ) ? inType : "Other" ;
  this.mnScreenX = ( trimString( inScreenX ) != "" && inScreenX != null ) ? inScreenX : getDefault( 0 , inType ) ;
  this.mnScreenY = ( trimString( inScreenY ) != "" && inScreenY != null ) ? inScreenY : getDefault( 1 , inType ) ;
  this.mnWidth =   ( trimString( inWidth ) != "" && inWidth != null ) ? inWidth : getDefault( 2, inType ) ;
  this.mnHeight =  ( trimString( inHeight ) != "" && inHeight != null ) ? inHeight : getDefault( 3, inType ) ;
  this.mnControlFramePercent = ( trimString( inControlFramePercent ) != "" && inControlFramePercent != null ) ? inControlFramePercent : getDefault( 4, inType ) ;
  this.msControlPos = ( trimString( inControl ) != "" && inControl != null ) ? inControl : getDefault( 5, inType ) ;
  this.mbDependent = ( trimString( inDependent ) != "" && inDependent != null ) ? inDependent : getDefault( 6, inType ) ;
  this.mbDirectories = ( trimString( inDirectories ) != "" && inDirectories != null ) ? inDependent : getDefault( 7, inType ) ;
  this.mbLocation = ( trimString( inLocation ) != "" && inLocation != null ) ? inLocation : getDefault( 8, inType ) ;
  this.mbMenuBar =  ( trimString( inMenuBar ) != "" && inMenuBar != null ) ? inMenuBar : getDefault( 9, inType ) ;
  this.mbResizable = ( trimString( inResizable ) != "" && inResizable != null ) ? inResizable : getDefault( 10, inType ) ;
  this.mbScrollbars = ( trimString( inScrollbars ) != "" && inScrollbars != null ) ? inScrollbars : getDefault( 11, inType ) ;
  this.mbStatus = ( trimString( inStatus ) != "" && inStatus != null ) ? inStatus : getDefault( 12, inType ) ;
  this.mbToolBar = ( trimString( inToolBar ) != "" && inToolBar != null ) ? inToolBar : getDefault( 13, inType ) ;
  this.msWindowName = "" ;
  // Methods of object.
  this.openNewWindow = openNewWindow ;
  this.getWindowProperties = getWindowProperties ;
  this.writeFrame = writeFrame ;
  this.getWindowName = _wp_getWindowName ;
}
