// =============================================================
//
// Copyright (c) 2000-2003 GE Smallworld. All Rights Reserved.
//
// =============================================================


// ------- Layers ----------------  
function createLayer(ID, windowRef, positioning) {
   /* This method inserts a new div tag into the body
      element of the HTML page specified in windowRef.
      Note that they layers by default will be hidden
      and thus must be made visible by the calling function.
      
      Parameters:
         ID: string - The id of the layer
         windowRef: window - The window which contains
            the HTML document.  If it is not set then
            the current window is assumed.
         positioning: string - The positioning of the layer.
            Allowed values are static, absolute and relative.
            If not specified this will default to absolute.
   */

   // get positioning
   if (!positioning)
      positioning = 'absolute';

   // get windowRef
   if (!windowRef)
      windowRef = window;
      
   // get document object
   aDocument = windowRef.document;
   
   // create a new div tag
   aLayerElement = aDocument.createElement('div');
   
   // set its id
   aLayerElement.setAttribute('id', ID);
   
   // now get style object
   aLayerStyle = new xbStyle(aLayerElement, positioning);

   // set visibility
   aLayerStyle.setVisibility('hidden');
      
   // get body element
   aBodyElement = aDocument.body;

   // add the layer to the body element
   aBodyElement.appendChild(aLayerElement);
   
   // now return the element
   return aLayerElement;
}


function getBatchForm(aFrame) {
   // This method retrieves a reference to the specified form.
   //
   // Parameters
   //    aFrame: The frame object which contains the form.
   //
   // Return: HTML element that represents the form.

	return getForm(aFrame, '', 'BatchForm');
}


function getForm (aFrame, aLayerName, aFormName) {
   // This method retrieves a reference to the specified form.
   //
   // Parameters
   //    aFrame: The frame object which contains the form.
   //    aLayerName:  The layer which contains the form.  This parameter
   //                 is deprecated and no longer used.
   //    aFormName: The name or id of the form element.
   //
   // Return: HTML element that represents the form.
   
   var aForm = xbGetElementById(aFormName, aFrame.window);

   if (aForm != undefined) {
      return aForm;
   }
   else {
      var aCollection = xbGetElementsByName(aFormName, aFrame.window);

      if (aCollection != undefined &&
          aCollection.length == 1) {
         return aCollection.item(0);
      }
   }
   
   return undefined;
}


function getChecked(radioButtonCollection) {
   /* Returns the checked value from a list of radio
      buttons.
      
      Parameters: radio - Reference to a collection
         of radio buttons.  A collection of radio buttons
         is defined as all radio buttons in a form that
         have the same name.
         
      Return: value - The value stored in the value attribute
            of the radio button that is checked.
   */

   if (radioButtonCollection==null) {
      return null;
   }

   for (i=0; i<radioButtonCollection.length; i++){
      if (radioButtonCollection[i].checked)  {
         return radioButtonCollection[i].value;
      }
   }			
}

function getSelected(selectInput) {
   /* Returns the selected value from a list.
      This method is needed because IE
      and Netscape deal with this differently.
      
      Parameters
         selectInput - Reference to a select input
         element.
         
      Return: value - The value stored in the value attribute
            of the selected item.
   */
   
   if (selectInput==null) {
      return null;
   }
   else if (navigator.family == 'ie4') {
      return selectInput.value;
   }
   else {
 		for (i =0; i< selectInput.length; i++){
			if (selectInput[i].selected)  {
            return selectInput[i].value;
         }
		}			
	}
}

function encodexml(sText) {
   /* This function escapes a string so that it is
      correctly represented in xml.
      
      Parameters:
         sText: string - The string to escape.
         
      Return: string - The escaped string.
   */

   if (sText == undefined) 
      return "";
      
	if (sText.indexOf("&") != -1)
		sText = stringReplace(sText, "&", "&amp;");
      
	if (sText.indexOf("<") != -1)
		sText = stringReplace(sText, "<", "&lt;");

	if (sText.indexOf(">") != -1)
		sText = stringReplace(sText, ">", "&gt;");

//	if (sText.indexOf("'") != -1)
//		sText = stringReplace(sText, "'", "&apos;");
//	if (sText.indexOf('"') != -1)
//		sText = stringReplace(sText, '"', "&quot;");
	return sText;
}

function stringReplace(originalString, findText, replaceText) {
   /* This function replaces all occurrences of a substring
      in the given string with the specfied value.
      
      Parameters:
         originalString: string - The string to modify.
         findText: string - The substring to replace.
         replaceText: string - The new value for the substring.
         
      Return: string - The escaped string.
   */

	var pos = 0;
	var lastpos = 0;
	var len = findText.length;
	pos = originalString.indexOf(findText);
	// check to make sure that we aren't repeating the
	// same replace. This is needed when replacing the 
	// findText with a superset (ie: & with &amp;)
	while ((pos != -1) && (pos != lastpos)) {
		preString = originalString.substring(0, pos);
        postString = originalString.substring(pos + len, originalString.length);
		originalString = preString + replaceText + postString;
		lastpos = pos;
		pos = originalString.indexOf(findText);
	}
	return originalString;
}


function getCurrentWinWidth(windowRef) {
   // Returns the width of a window.
   //
   // Parameters
   //    windowRef: window - The window which contains
   //    the HTML document.  If it is not set then
   //    the current window is assumed.
   //
   // Returns: integer - The width of the window.

   if (!windowRef)
      windowRef = window;

   switch (navigator.family) {
      case 'nn4':
         return(windowRef.innerWidth);
      case 'gecko':
         return(windowRef.innerWidth);
      case 'ie4':
         return(windowRef.document.body.clientWidth);
   }
}

function getCurrentWinHeight(windowRef) { 
   // Returns the height of a window.
   //
   // Parameters
   //    windowRef: window - The window which contains
   //    the HTML document.  If it is not set then
   //    the current window is assumed.
   //
   // Returns: integer - The width of the window.

   if (!windowRef)
      windowRef = window;

   switch (navigator.family) {
      case 'nn4':
         return(windowRef.innerHeight);
      case 'gecko':
         return(windowRef.innerHeight);
      case 'ie4':
         return(windowRef.document.body.clientHeight);
   }
}


function addFormField (aDocument, aForm, aInputType, aFieldName, aFieldValue) {
   // Adds an element to a form.
   //
   // Parameters
   //    aDocument: document - The document that will contain the new element.
   //    aForm - The form that will contain the new element.
   //    aInputType - The type of input element to create.
   //    aFieldName - The name of the new field.
   //    aFieldValue - The value of the new field.
   //
   // Returns: none

   if (xbGetElementById) {
      var input = aDocument.createElement('INPUT');

      if (aDocument.all) { // what follows should work 
                          // with NN6 but doesn't in M14
        input.type = aInputType;
        input.name = aFieldName;
        input.value = aFieldValue;
      }
      else if (xbGetElementById) { // so here is the
                                          // NN6 workaround
        input.setAttribute('type', aInputType);
        input.setAttribute('name', aFieldName);
        input.setAttribute('value', aFieldValue);
      }
      
      // now add the element to the form      
      aForm.appendChild(input);
   }
}


