<!--

var newElementCount = 0 ; // used for unique IDs for div tags

// Default CSS classes for various dialog controls
var dialog_label = 'dialog_label' ;
var dialog_button = 'dialog_button' ;
var dialog_listbox = 'dialog_listbox' ;
var dialog_altlistbox = 'dialog_altlistbox' ;
var dialog_checkbox = 'dialog_checkbox' ;
var dialog_numeric = 'dialog_numeric' ;
var dialog_radiobtn = 'dialog_radiobtn' ;
var dialog_image = 'dialog_image' ;
var dialog_arrayedit = 'dialog_arrayedit' ;
var dialog_arraylist = 'dialog_arraylist' ;
var dialog_date = 'dialog_date' ;
var dialog_iframe = 'dialog_iframe' ;
var dialog_hotspot = 'dialog_hotspot' ;
var dialog_grid = 'dialog_grid' ;
var dialog_body = 'dialog_body' ;
var dialog_graph = 'dialog_graph' ;

// const
var WYSIWYG = 0 ;
var HYBRID  = 1 ;
var CSS     = 2

function GetNextElementID()
{
  return ( 'TTV' + ( newElementCount++ ) ) ;
}

var isIE = ( document . all ) ;
var sourceSvr = '' ;
var flatStyle = ";border-style:solid;border-color:#000000;border-width:1px" ;
var lastClickedID = null ;
var building = false ;
var aPopupReportObject = null ;
var dialogTimeout = 0 ;
var visualAlert = true ;
var INVALID_COLOR = '#999999' ;
var dialog_offset_x = 0 ;
var dialog_offset_y = 0 ;
var page_id = 0 ;

function PopupReportObject ( aText , aTitle )
{
  this . text = aText ;
  this . description = aTitle ;
}

// ===============================================================================

function AjaxInitialize ( aDefaults )
{
  var el = document.getElementById('loading_div') ;
  if ( el )
  {
    el.style.visibility = 'hidden' ;
    el.style.position = 'absolute' ;
  }

  if ( aDefaults )
    ApplyCSSdefaults ( aDefaults ) ;
  
  if ( typeof appSettings != 'undefined' && appSettings )
  {
  	if ( aDefaults )
  	  ;
  	else
      ApplyCSSdefaults ( appSettings.CSSdefaults ) ;
    document.title = appSettings . title ;
  }
  else
  {
  	appSettings = { 'controlStyling' : 0 }
  }
}

// ===============================================================================

function DlgControl ( aIndex , aType , controlID , objectName , isRequired , tabID , divID )
{
  this.index = aIndex ;    // index in the elements array
  this.type = aType ;      // Control type (num,lb,cb)
  this.id = controlID ;    // HTML control ID
  this.divID = divID ;     // HTML div element ID containing the control
  this.name = objectName ; // attribute name
  this.required = isRequired ; // is this item obligatory?
  this.tabID = tabID ;     // Parent tab ID (0 = main window)
  this.handled = false ;   // For obligatory checking, has this object passed OK?
  this.isCurrent = false ; // For radio buttons, grid holds current selection status
  this.valids = null ;     // valid values for list, cb and grids
  this.isValid = true ;    // Is this numeric valid?
  this.values = null ;     // values for the grid control
  this.enabled = true ;    // is the control enabled or not?
  this.visible = true ;    // visibility status (used mainly for tabsheets)
  this.sortInvalid = true ;// sortInvalid flag for LBs, combos, & grids
  this.showInvalid = true ;// showInvalid flag for LBs, combos, radios & grids
  this.allowInvalid = false ; // allowInvalid flag for LBs, CBs & Grids
  this.hasTitle = false ;  // Does the gris have a title?
  this.sortCol = -1 ;      // Do I sort this grid on this column?
  this.sortDir = 1 ;       // If I sort this grid, which direction do I go (1=asc, 2=desc)
  this.items = null ;      // stores the value array for LBs, combos, & grids  (used to retain original sortorder for an AllVal call)
  this.txtColor = '' ;     // Holds the designed text color for numeric edit boxes
  this.doneTrigram = false ; // Have we done a trigram search?
  this.confirmText = '' ;  // Confirmation text on exitbuttons
  this.text = '' ;         // holds the designed caption of a radio button
  this.multiSel = false ;  // Is this check box a multi-sel list attribute?
  this.errorDiv = '' ;     // the div id of the visual error indicator
  this.helpDiv = '' ;      // the div id of the visual help indicator 
}

var elements = new Array () ; // used to hold current dialog inputs

function ArrayPush ( array , newElement )
{
  // array.push ( newElement ) ;       !!!!! not supported on all browsers (IE5 and earlier, IE6 on ME)
  array [ array . length ] = newElement ;
  return ( array ) ;
}

// Find first control related to attribute based on Y,X position
function IsFirstControl ( objectName , ctrlID )
{
	var bestY = 999999 ;
	var bestX = 999999 ;
	var bestIsMe = false ;
	for (var c = 0 ; c < elements.length ; c++ )
    if ( elements[c].name == objectName )
    {
      var pel = document.getElementById(elements[c].divID) ;
      if ( pel != null )
      {          	
	      if ( pel.offsetTop < bestY )
	      {
	        bestY = pel.offsetTop ;
	        bestX = pel.offsetWidth ;
	        bestIsMe = ( pel.id == ctrlID ) ;//elements[c].divID
        } 
        else if ( ( pel.offsetTop == bestY ) && ( pel.offsetWidth < bestX ) )
	      {
	        bestX = pel.offsetWidth ;
	        bestIsMe = ( pel.id == ctrlID ) ;
        }
      }
    }
	return ( bestIsMe ) ;
}

function AddDlgControl ( aType , controlID , objectName , isRequired , tabID , aDiv , hlpTxt , parentDiv )
{
  var i = new DlgControl ( elements.length , aType , controlID , objectName , isRequired , tabID , aDiv.id ) ;
  elements = ArrayPush ( elements , i ) ;
  if ( hlpTxt )
  {
  	if ( IsFirstControl(objectName,aDiv.id) )
  	{
  	  if ( parentDiv )
  	    aDiv = parentDiv ;
      var e = document.getElementById ( aDiv.id ) ;
      if ( e )
      {
        var s = '<a href="javascript:DoHelp(\'' + hlpTxt + '\')"><img src="images/tiny.gif" width="16" height="16" border="0" /></a>' ;
        var hDiv = CreateElement ( e.offsetLeft+e.offsetWidth,e.offsetTop,16,16,s,null,null,'hidden',FindParent(0),'input_help' );
        i.helpDiv = hDiv.id ;
      }
    }
  }
  return ( i ) ;
}

function DoHelp ( hlpTxt )
{
  if ( hlpTxt.substr(0,4).toUpperCase() == 'SRC=' )
  {
    hlpTxt = hlpTxt.substr(4) ;
    hlpTxt = '<iframe width="100%" height="300px" src="' + hlpTxt + '" frameborder="0" scrolling="yes" />' ;
    ShowModal(hlpTxt,'Help');
  }
  else
    ShowModal('<br>' + hlpTxt + '<br><br>','Help');
}

function FindDlgControl ( objectName , firstIndex )
{
  var c ;
  if ( firstIndex == null )
    firstIndex = 0 ;
  for ( c = firstIndex ; c < elements . length ; c++ )
    if ( elements[c].name == objectName )
      return ( elements[c] ) ;
  return ( null ) ;
}

function FindDlgControlByID ( controlID )
{
  for ( c = 0 ; c < elements . length ; c++ )
    if ( elements[c].id == controlID )
      return ( elements[c] ) ;
  return ( null ) ;
}

function MakeVisible( documentElement )
{
  if ( documentElement != null )
    documentElement . style . visibility = 'visible' ;
}

function MakeInvisible( documentElement )
{
  if ( documentElement != null )
    documentElement . style . visibility = 'hidden' ;
}

// ===============================================================================

function TabPage ( sheetID , ctrlID , sheetIndex )
{
  this.id = sheetID ;          // Tab Page ID
  this.ctrlID = ctrlID ;       // Tab Control ID
  this.tabIndex = sheetIndex ; // tab index (for activePage property)
  this.active = false ;
}

var tabs = new Array () ; // used to hold current tab sheets

function AddTabPage ( sheetID , ctrlID , sheetIndex )
{
  var i = new TabPage ( sheetID , ctrlID , sheetIndex ) ;
  tabs = ArrayPush ( tabs , i ) ;
  return ( i ) ;
}

function FindTabPageByID ( tabID )
{
  for ( c = 0 ; c < tabs . length ; c++ )
    if ( tabs[c].id == tabID )
      return ( tabs[c] ) ;
  return ( null ) ;
}

function FindActiveTabPage()
{
  for ( c = 0 ; c < tabs . length ; c++ )
    if ( tabs[c].active )
      return( tabs[c].id ) ;
  return( -1 ) ;
}

function SetActiveTabPage( tabID )
{
  for ( c = 0 ; c < tabs . length ; c++ )
    if ( tabs[c].id == tabID )
      tabs[c].active = true ;
    else
      tabs[c].active = false ;
}

// ===============================================================================
// Date picker code

function findPos(obj, e ) {
  var pos = new Object;
  pos.left = 0;
  pos.top = 0;
  if (obj.offsetParent)	{
    while (obj.offsetParent) {
      if ( e == obj )
        break ;
      pos.left += obj.offsetLeft;
      pos.top  += obj.offsetTop;
      obj = obj.offsetParent;
    };
  }	else if (obj.x) {
    pos.left = obj.x;
    pos.top = obj.y;
  };
  return pos;
};

function parent(widget) {
  if (widget.parentElement) { return widget.parentElement };
  if (widget.parentNode) { return widget.parentNode };
  if (widget.parent) { return widget.parent };
  return;
};

var calendar_date_edit = null ;
var calendar_id = '' ;

function show_calendar_widget(widget,aID) {
  if (! document.getElementsByTagName) { return; };

  var date_edit = document.getElementById( 'calendar_widget' ) ;
  if ( date_edit != null )
  {
    document.body.removeChild(date_edit) ;
    return ;
  }

  // find edit widget
  date_edit = document.getElementById( aID ) ;
  if ( date_edit == null )
  {
    alert("No date edit field assigned to picker");
    return 0 ;
  }

  // load date from edit widget
  var date = date_edit.innerHTML;

  // find/create calendar widget
  var calendar_widget = document.getElementById('calendar_widget');
  if (! calendar_widget)
  {
    calendar_widget = document.createElement('div');
    calendar_widget.id = 'calendar_widget';
    calendar_widget.className = 'calendar_widget';
    document.body.appendChild(calendar_widget);
    var iframe = document.createElement('iframe');
    iframe.id = 'calendar_widget_iframe';
    iframe.name = iframe.id;
    iframe.style.border = 'none';
    iframe.width = '100%';
    iframe.height = '100%';

    iframe.src = 'calendar.html';

    calendar_widget.appendChild( iframe );
  };

  // find lower edge of edit widget:
  pos = findPos(date_edit);
  if (date_edit.offsetHeight) {
    pos.top += date_edit.offsetHeight;
  } else {
    pos.top += date_edit.clientHeight;
  };

  // move calendar widget
  if ( isIE ) {
    // IE
    calendar_widget.style.posTop = pos.top ;
    calendar_widget.style.posLeft = pos.left;
  } else {
    // Mozilla
    calendar_widget.style.top = pos.top + "px";
    calendar_widget.style.left = pos.left + "px";
  };
  calendar_widget.style.zIndex = 255 ;
  calendar_widget.style.position = 'absolute';
  calendar_widget.style.display = "block";
  // Now connect the current edit to the iframe:
  calendar_date_edit = date_edit ;
  calendar_id = aID ;
  MakeVisible( calendar_widget ) ;
};

// ===============================================================================

	function encodeForHTML (s)
	{
	  var result = "";

    if (s == null)
      return (result);

    for (c = 0; c < s.length; c++)
	  {
		  ch = s.charAt(c);
		  switch (ch)
		  {
        case '<':
			    result = result + "&lt;";
			    break;

		    case '>':
			    result = result + "&gt;";
			    break;

  		  default:
			    result = result + ch;
          break;
		  }
	  }
	  return (result);
	}

//================================================================================
function RetrieveValue ( element )
{
  var el = document.getElementById(element.id) ;
  if ( el != null )
    switch ( element.type )
    {
      case ( "num" ) : return ( el.value ) ;
      case ( "dte" ) : return ( EncodeDate( el.innerHTML ) ) ;
      case ( "txt" ) : return ( escape ( el.value ) ) ; // '<test'   
      case ( "lb" ) :
        if ( el.multiple )
        {
          var s = '' ;
          var c ;
          for ( c = el.options.length - 1 ; c >= 0 ; c-- )
            if ( el.options[c].selected )
            {
              if ( s.length > 0 )
                s += '^' ;
              s += el.options[c].value ;
            }
//          if ( sourceSvr != 'XR_AJAX.DLL' && s.length > 0 )
//            return ( '~' + s ) ;
          return ( s ) ;
        }
        else
        {
//          if ( sourceSvr != 'XR_AJAX.DLL' && el.value.length > 0 )
//            return ( '~' + el.value ) ;
          return ( el.value ) ;
        }
      case ( "cb" ) :
        if ( el.value == null || el.value == '0' )
          return ( '0' ) ;
        else
        {
//          if ( sourceSvr != 'XR_AJAX.DLL' && el.value.length > 0 )
//            return ( '~' + el.value ) ;
          return ( el.value ) ;
        }
      case ( "chb" ) :
        if ( el.checked )
        {
          if ( el.value == 0 )
          {
//            if ( sourceSvr != 'XR_AJAX.DLL' )
//              return ( '~1') ;
              return ( '1' ) ;
          }
          else if ( element.multiSel )
            return ( 'A[' + el.value ) ;
          else
            return ( el.value ) ;
        }
        else
        {
          if ( el.value == 0 )
          {
//            if ( sourceSvr != 'XR_AJAX.DLL' )
//              return ( '~2' ) ;
              return ( '2' ) ;
          }
          else if ( element.multiSel )
            return ( 'D[' + el.value ) ;
          else
            return ( '_NA_' ) ;
        }
      case ( "rd" ) :
        if ( el.checked )
        {
//          if ( sourceSvr != 'XR_AJAX.DLL' && el.value.length > 0 )
//            return ( '~' + el.value) ;
          return ( el.value ) ;
        }
        else
          return ( '_NA_' ) ;
      case ( "hot" ) :
      {
        if ( element.isCurrent )
          return( element.values ) ;
        else
          return( '_NA_' ) ;
      }
      case ( "alb" ) :
        if ( el.multiple )
        {
          var s = '' ;
          var c ;
          for ( c = el.options.length - 1 ; c >= 0 ; c-- )
            if ( el.options[c].selected )
            {
              if ( s.length > 0 )
                s += '^' ;
              s += el.options[c].value ;
            }
//          if ( sourceSvr != 'XR_AJAX.DLL' && s.length > 0 )
//            return ( '~' + s ) ;
          return ( s ) ;
        }
        else
        {
//          if ( sourceSvr != 'XR_AJAX.DLL' && el.value.length > 0 )
//            return ( '~' + el.value ) ;
          return ( el.value ) ;
        }
      case ( "acb" ) : return ( el.value ) ;
      case ( "grd" ) :
        if ( element.isCurrent == false )
          return ( '' ) ;
        else
        {
//          if ( sourceSvr != 'XR_AJAX.DLL' )
//            return ( '~' + element.isCurrent ) ;
            return ( element.isCurrent ) ;
        }
      case ( "hlb" ) : return ( el.value ) ;
    }
  return ( "" ) ;
}

function RetrieveElements ( lastClickedID )
{
  var c , ss , skipElement ;
  var aNames = new Array () ;
  var s = "" ;

  var lastClickedName = "" ;
  if ( lastClickedID != null )
  {
    var e = FindDlgControlByID ( lastClickedID ) ;
    if ( e != null )
      lastClickedName = e.name ;
  }

  for ( c = 0 ; c < elements.length ; c++ )
  {
    skipElement = false ;
    if ( lastClickedName != '' )
      if ( ( elements[c].name == lastClickedName ) && ( elements[c].id != lastClickedID ) )
        skipElement = true ;
    if ( ( ( elements[c].type == "num" ) ||
           ( elements[c].type == "dte" ) ||
           ( elements[c].type == "txt" ) ||
           ( elements[c].type == "lb" ) ||
           ( elements[c].type == "cb" ) ||
           ( elements[c].type == "chb" ) ||
           ( elements[c].type == "rd" ) ||
           ( elements[c].type == "alb" ) ||
           ( elements[c].type == "acb" ) ||
           ( elements[c].type == "hot" ) ||
           ( elements[c].type == "grd" ) ||
           ( elements[c].type == "hlb" ) ) &&
         ( ! skipElement ) )
    {
      ss = RetrieveValue ( elements[c] ) ;
      if ( ss != '_NA_' )
      {
        if ( s.length > 0 )
          s += '&' ;
        s += elements[c].name + '=' + ss ;
        aNames = ArrayPush ( aNames , elements[c].name ) ;
      }
    }
  }
  for ( c = 0 ; c < elements.length ; c++ )
    if ( ( elements[c].type == "num" ) ||
         ( elements[c].type == "dte" ) ||
         ( elements[c].type == "txt" ) ||
         ( elements[c].type == "lb" ) ||
         ( elements[c].type == "cb" ) ||
         ( elements[c].type == "chb" ) ||
         ( elements[c].type == "rd" ) ||
         ( elements[c].type == "alb" ) ||
         ( elements[c].type == "acb" ) ||
         ( elements[c].type == "hot" ) ||
         ( elements[c].type == "grd" ) )
    {
      var found = false ;
      for ( cc = 0 ; cc < aNames.length ; cc++ )
        if ( aNames[cc] == elements[c].name )
        {
          found = true ;
          break ;
        }
      if ( ! found )
      {
        if ( s.length > 0 )
          s += '&' ;
        s += elements[c].name + '=' ;
        aNames = ArrayPush ( aNames , elements[c].name ) ;
      }
    }
  return ( s ) ;
}

// ===============================================================================

function IsNumber ( s )
{
  var c ;
  var nums = "0123456789" ;
  var passedPoint = false ;
  for ( c = 0 ; c < s.length ; c++ )
    if ( nums.indexOf(s.charAt(c)) == -1 )
    {
      var ok = false ;
      if ( ( c == 0 ) && ( s.charAt(c) == "-" ) )
        ok = true ;
      else if ( ( ( s.charAt(c) == "." ) || ( s.charAt(c) == "," ) ) && ( ! passedPoint ) )
      {
        ok = true ;
        passedPoint = true ;
      }
      if ( ! ok )
        return ( false ) ;
    }
  return ( true ) ;
}

function getCursorIndex ( fld )
{
  if (typeof fld.selectionStart == 'undefined' && document.selection) // workround for ie8 
  {
	  fld.focus (); 
		var Sel = document. selection . createRange ( ) ; 
		Sel. moveStart ( 'character' , - fld.value.length ) ; 
		return (Sel.text.length); 
	}
	return fld.selectionStart;
}

function numberFormat(fld, e, decP)
{
  var key = '';
  var strCheck = '-0123456789';
  if (decP > 0)
    strCheck += '.' ;
  whichCode = (window.event) ? e.keyCode : e.which ;
  if (whichCode == 0 && e.keyCode > 0)
    whichCode = e.keyCode // Fix for FF tab
    
  switch (whichCode)
  {
  	case 08:    // BackSpace
  	case 09:    // Tab
  	case 13:    // Enter
  	  return (true);

  	case 35:    // #
  	  return ( e.charCode == 0 );  // End
  	  
  	case 36:    // Home , $
  	  return ( ! e.shiftKey ) ;
  	  
  	case 37:    // Left , %
  	  if ( e.charCode == 0 )
  	    return ( true ) ;
  	  return ( ! e.shiftKey );

  	case 39:    // right, '
  	  if ( e.charCode == 0 )
  	    return ( true ) ;
  	  return ( ! e.shiftKey && (e.charCode == 0) );

  	case 46:    // .
        if ( e.charCode == 0 )  // Del
  	      return (true);
  	  break;
	}
  key = String.fromCharCode(whichCode);  // Get key value from key code

  if (strCheck.indexOf(key) == -1) 
    return (false);  // Not a valid key

  if (key == '-' && ( getCursorIndex ( fld ) != 0 || fld.value.indexOf('-') != -1) )
    return (false);  // already have a '-'      
  else

  if (decP > 0)
  {
    var p = fld.value.indexOf('.') ;
    if ( p != -1 )
    {
      if (key == '.' )
        return (false);  // already have a '.'  
      if ( fld.selectionStart > p && fld.value.length - p > decP )
        return (false);  // already have correct '.'
	  }
  }
  return (true);
}

function ClickedInvalid ( aID )
{
  var el = FindDlgControlByID ( aID ) ;
  if ( el == null )
    return ( false ) ;
  if ( ( el.type != "lb" ) && ( el.type != "cb" ) )
    return ( false ) ;
  var pel = document.getElementById(el.id) ;
  if ( pel == null )
    return ( false ) ;
  if ( pel.selectedIndex == -1 )
    return ( false ) ;
  var cn = pel.options[pel.selectedIndex].className ;
  if ( cn != undefined )
//SA    if ( cn == 'invalidOption' )
    if ( cn == 'invalidOption' || cn == 'invalidSelection')  // SA Added 
      return ( true ) ;
}

function FocusElement ( element , pagePop )
{
  if ( pagePop == null )
    pagePop = true ;
  var el = document.getElementById(element.id) ;
  if ( el != null )
  {
    if ( element.tabID != 0 )
    {
      var pel = document.getElementById("TAB"+element.tabID) ;
      if ( pel != null )
        if ( pel.parentNode != null )
          if ( pel.style.visibility == "hidden" )
          {
            if ( pagePop )
              ShowPage ( pel.parentNode.id , element.tabID ) ;
            else
              return ( false ) ;
          }
    }

    if ( el . disabled )
      return( false ) ;
    if ( element.visible )
      el.focus() ;
    return ( true ) ;
  }
  return ( false ) ;
}

function ObligatoryError ( aElement , visualErr , isInvalidErr )
{
  var s = '' ;
  if ( visualErr )
  {
  	if ( ( aElement.type == "rd" ) || ( aElement.type == "chb" ) )
  	{
  		// first FIRST radio/checkbox
  		for ( var c = 0 ; c < elements.length ; c++ )
  		  if ( ( elements[c].type == aElement.type ) && 
  		       ( elements[c].attributeID == aElement.attributeID ) && 
  		       IsFirstControl(elements[c].name,elements[c].divID) )
  		  {
  		  	aElement = elements[c] ;
  		  	break ;
  		  }
  	}
    var aName = aElement.name ;      //nameObj.flatProps.description
    if ( isInvalidErr )
      s = 'Invalid value selected' ;
    else
      s = 'Please enter a value' ;
    if ( aName != '' )
	    s += ' for "' + aName + '"' ;
  }
//  var e = document.getElementById ( aElement.id ) ;
  var e = document.getElementById ( aElement.divID ) ;
  if ( e )
  {
    e2 = FindParent(0) ;
    var pos = findPos(e , e2);
    if (e.offsetWidth != 0)
      pos.left += e.offsetWidth;
    else
      pos.left += e.clientWidth;
//    var eDiv = CreateElement ( pos.left-16,pos.top-18,16,16,'',null,null,'visible',e2,'input_error' );
//    var eDiv = CreateElement ( pos.left+6,pos.top-30,16,16,'',null,null,'visible',e2,'input_error' );
    var eDiv = CreateElement ( pos.left+0,pos.top-18,16,16,'',null,null,'visible',e2,'input_error' );
    eDiv.title = s ;

    aElement.errorDiv = eDiv.id ;
  }
  FocusElement ( aElement ) ;
}

function ValidateElements ()
{
  var c , cc , el , v ;
  var s = "" ;
  for ( c = 0 ; c < elements.length ; c++ )
    elements[c].handled = false ;
  // remove any existing error markers
  var aBody = FindParent(0);

  for ( c = 0 ; c < elements.length ; c++ )
    if ( elements[c].errorDiv != '' )
    {
      var e = document.getElementById(elements[c].errorDiv) ;
      if ( e )
        aBody.removeChild(e) ;
      elements[c].errorDiv = '' ;
    }
  var validFlag = true ;
  for ( c = 0 ; c < elements.length ; c++ )
  {
    var previouslyHandled = false ;
    for ( cc = c-1 ; cc >= 0 ; cc-- )
      if ( elements[cc].name == elements[c].name )
        if ( elements[cc].handled )
          previouslyHandled = true ;
    var moreInputs = false ;
    for ( cc = c+1 ; cc < elements.length ; cc++ )
      if ( elements[cc].name == elements[c].name )
        moreInputs = true ;
    if ( previouslyHandled )
      elements[c].handled = true ;
    else
    {
      el = document.getElementById(elements[c].divID) ;
      if ( el != null )
        isHidden = ( ( el.style.visibility == "hidden" ) && ( ( el.tabID != 0 ) && ( el.visible == true ) ) );
      else
        isHidden = false ;
      if ( ! isHidden )
        switch ( elements[c].type )
        {
          case ( "num" ) :
            v = RetrieveValue ( elements[c] ) ;
            if ( ( v.length == 0 ) && ( elements[c].required ) && ( ! moreInputs ) )
            {
//              alert ( 'You must supply a value for "' + elements[c].name + '"' ) ;
//              FocusElement ( elements[c] ) ;
//              return ( false ) ;
              ObligatoryError ( elements[c] , visualAlert ) ;
              validFlag = false ;
            }
            if ( ! IsNumber ( v ) )
            {
//              alert ( '"' + v + '" is not a valid number' ) ;
//              FocusElement ( elements[c] ) ;
//              return ( false ) ;
              ObligatoryError ( elements[c] , visualAlert ) ;
              validFlag = false ;

            }
            elements[c].handled = true ;
            break ;
          case ( "dte" ) :
            v = RetrieveValue ( elements[c] ) ;
            if ( ( v.length == 0 || v.indexOf ('*') != -1 ) && ( elements[c].required ) && ( ! moreInputs ) )
            {
//              alert ( 'You must supply a value for "' + elements[c].name + '"' ) ;
//              FocusElement ( elements[c] ) ;
//              return ( false ) ;
              ObligatoryError ( elements[c] , visualAlert ) ;
              validFlag = false ;
            }
            elements[c].handled = true ;
            break ;
          case ( "txt" ) :
            v = RetrieveValue ( elements[c] ) ;
            if ( ( v.length == 0 ) && ( elements[c].required ) && ( ! moreInputs ) )
            {
//              alert ( 'You must supply a value for "' + elements[c].name + '"' ) ;
//              FocusElement ( elements[c] ) ;
//              return ( false ) ;
              ObligatoryError ( elements[c] , visualAlert ) ;
              validFlag = false ;

            }
            elements[c].handled = true ;
            break ;
          case ( "lb" ) :
            v = RetrieveValue ( elements[c] ) ;
            var el = document.getElementById(elements[c].id) ;
            var isMulti = false ;
            if ( el != null )
              isMulti = el.multiple ;
            if ( ( v.length == 0 ) && ( elements[c].required ) && ( ! moreInputs ) && ( ! isMulti ) )
            {
//              alert ( 'You must supply a value for "' + elements[c].name + '"' ) ;
//              FocusElement ( elements[c] ) ;
//              return ( false ) ;
              ObligatoryError ( elements[c] , visualAlert ) ;
              validFlag = false ;

            }
            if ( ClickedInvalid ( elements[c].id ) )
            {
//              alert ( 'Invalid value selected for "' + elements[c].name + '"' ) ;
//              FocusElement ( elements[c] ) ;
//              return ( false ) ;
              ObligatoryError ( elements[c] , visualAlert , true ) ;
              validFlag = false ;
            }
            elements[c].handled = true ;
            break ;
          case ( "hlb" ) :
            v = RetrieveValue ( elements[c] ) ;
            var el = document.getElementById(elements[c].id) ;
            if ( ( v.length == 0 ) && ( elements[c].required ) && ( ! moreInputs ) )
            {
//              alert ( 'You must supply a value for "' + elements[c].name + '"' ) ;
//              FocusElement ( elements[c] ) ;
//              return ( false ) ;
              ObligatoryError ( elements[c] , visualAlert ) ;
              validFlag = false ;

            }
            if ( ClickedInvalid ( elements[c].id ) )
            {
//              alert ( 'Invalid value selected for "' + elements[c].name + '"' ) ;
//              FocusElement ( elements[c] ) ;
//              return ( false ) ;
              ObligatoryError ( elements[c] , visualAlert ) ;
              validFlag = false ;
            }
            elements[c].handled = true ;
            break ;
          case ( "cb" ) :
            v = RetrieveValue ( elements[c] ) ;
            if ( ( v.length == 0 ) && ( elements[c].required ) && ( ! moreInputs ) )
            {
//              alert ( 'You must supply a value for "' + elements[c].name + '"' ) ;
//              FocusElement ( elements[c] ) ;
//              return ( false ) ;
              ObligatoryError ( elements[c] , visualAlert ) ;
              validFlag = false ;
            }
            if ( ClickedInvalid ( elements[c].id ) )
            {
//              alert ( 'Invalid value selected for "' + elements[c].name + '"' ) ;
//              FocusElement ( elements[c] ) ;
//              return ( false ) ;
              ObligatoryError ( elements[c] , visualAlert , true ) ;
              validFlag = false ;
            }
            elements[c].handled = true ;
            break ;
          case ( "rd" ) :
            v = RetrieveValue ( elements[c] ) ;
            if ( v == '_NA_' )
              v = '' ;
            if ( ( v.length == 0 ) && ( elements[c].required ) && ( ! moreInputs ) )
            {
//              alert ( 'You must supply a value for "' + elements[c].name + '"' ) ;
//              FocusElement ( elements[c] ) ;
//              return ( false ) ;
              ObligatoryError ( elements[c] , visualAlert ) ;
              validFlag = false ;
            }
            if ( v.length > 0 )
              elements[c].handled = true ;
            break ;
          case ( "hot" ) :
            v = RetrieveValue ( elements[c] ) ;
            if ( v == '_NA_' )
              v = '' ;
            if ( ( v.length == 0 ) && ( elements[c].required ) && ( ! moreInputs ) )
            {
//              alert ( 'You must supply a value for "' + elements[c].name + '"' ) ;
//              FocusElement ( elements[c] ) ;
//              return ( false ) ;
              ObligatoryError ( elements[c] , visualAlert ) ;
              validFlag = false ;
            }
            if ( v.length > 0 )
              elements[c].handled = true ;
            break ;
          case ( "alb" ) :
            v = RetrieveValue ( elements[c] ) ;
            var el = document.getElementById(elements[c].id) ;
            var isMulti = false ;
            if ( el != null )
              isMulti = el.multiple ;
            if ( ( v.length == 0 ) && ( elements[c].required ) && ( ! moreInputs ) && ( ! isMulti ) )
            {
//              alert ( 'You must supply a value for "' + elements[c].name + '"' ) ;
//              FocusElement ( elements[c] ) ;
//              return ( false ) ;
              ObligatoryError ( elements[c] , visualAlert , false ) ;   //this.name
              validFlag = false ;
            }
            elements[c].handled = true ;
            break ;
          case ( "acb" ) :
            v = RetrieveValue ( elements[c] ) ;
            if ( ( v.length == 0 ) && ( elements[c].required ) && ( ! moreInputs ) )
            {
//              alert ( 'You must supply a value for "' + elements[c].name + '"' ) ;
//              FocusElement ( elements[c] ) ;
//              return ( false ) ;
              ObligatoryError ( elements[c] , visualAlert , false ) ;
              validFlag = false ;
            }
            elements[c].handled = true ;
            break ;
          case ( "grd" ) :
            v = RetrieveValue ( elements[c] ) ;
            if ( ( v.length == 0 ) && ( elements[c].required ) && ( ! moreInputs ) )
            {
//              alert ( 'You must supply a value for "' + elements[c].name + '"' ) ;
//              return ( false ) ;
              ObligatoryError ( elements[c] , visualAlert ) ;
              validFlag = false ;
            }
            if ( v.length > 0 )
              elements[c].handled = true ;
            break ;
        }
    }
  }
//  return ( true ) ;
  return ( validFlag ) ;
}

function FocusFirstElement ()
{
  var c ;
  for ( c = 0 ; c < elements.length ; c++ )
    if ( ( elements[c].type == "num" ) ||
         ( elements[c].type == "txt" ) ||
         ( elements[c].type == "lb" ) ||
         ( elements[c].type == "cb" ) ||
         ( elements[c].type == "chk" ) ||
         ( elements[c].type == "rd" ) ||
         ( elements[c].type == "alb" ) ||
         ( elements[c].type == "acb" ) )
    {
      if ( FocusElement ( elements[c] , false ) )
        return ;
    }
}

// ===============================================================================

function DoTabs ()
{
  var c , cc , el ;
  var invalidTabs = new Array () ;
  for ( c = 0 ; c < elements.length ; c++ )
  {
    el = elements[c] ;
    if ( el.tabID != 0 )
    {
      var found = false ;
      for ( cc = 0 ; cc < invalidTabs . length ; cc++ )
        if ( invalidTabs[cc] == el.tabID )
        {
          found = true ;
          break ;
        }
      if ( ! found )
        switch ( el.type )
        {
          case ( "num" ) :
            v = RetrieveValue ( el ) ;
            if ( ( ( v.length == 0 ) && ( el . required ) )
                 || ( ! IsNumber ( v ) ) )
              invalidTabs = ArrayPush ( invalidTabs , el.tabID ) ;
            break ;
          case ( "dte" ) :
            v = RetrieveValue ( el ) ;
            if ( ( v.length == 0 ) && ( el . required ) )
              invalidTabs = ArrayPush ( invalidTabs , el.tabID ) ;
            break ;
          case ( "txt" ) :
            v = RetrieveValue ( el ) ;
            if ( ( v.length == 0 ) && ( el . required ) )
              invalidTabs = ArrayPush ( invalidTabs , el.tabID ) ;
            break ;
          case ( "lb" ) :
            v = RetrieveValue ( elements[c] ) ;
            var pel = document.getElementById(elements[c].id) ;
            var isMulti = false ;
            if ( el != null )
              isMulti = el.multiple ;
            if ( ( ( v.length == 0 ) && ( el . required ) )&& ( ! isMulti ) )
              invalidTabs = ArrayPush ( invalidTabs , el.tabID ) ;
            break ;
          case ( "cb" ) :
            v = RetrieveValue ( elements[c] ) ;
            if ( ( v.length == 0 ) && ( el . required ) )
              invalidTabs = ArrayPush ( invalidTabs , el.tabID ) ;
            break ;
          case ( "rd" ) :
            break ;
          case ( "alb" ) :
            v = RetrieveValue ( elements[c] ) ;
            var el = document.getElementById(elements[c].id) ;
            var isMulti = false ;
            if ( el != null )
              isMulti = el.multiple ;
            if ( ( ( v.length == 0 ) && ( el . required ) ) && ( ! isMulti ) )
              invalidTabs = ArrayPush ( invalidTabs , el.tabID ) ;
            break ;
          case ( "acb" ) :
            v = RetrieveValue ( elements[c] ) ;
            if ( ( v.length == 0 ) && ( el . required ) )
              invalidTabs = ArrayPush ( invalidTabs , el.tabID ) ;
            break ;
          case ( "grd" ) :
            v = RetrieveValue ( elements[c] ) ;
            if ( ( v.length == 0 ) && ( el . required ) )
              invalidTabs = ArrayPush ( invalidTabs , el.tabID ) ;
            break ;
        }
    }
    if ( el.type == "chb" )
    {
      var pel = document.getElementById(el.id) ;
      if ( pel != null )
      {
        var isInvalid = false ;
        var aArray = el.valids ;
        if ( pel.value == 0 ) // boolean
        {
          var trueValid = true ;
          var falseValid = true ;
          if ( aArray != null )
            for ( aStart = 0 ; aStart < aArray.length ; aStart++ )
            {
              if ( aArray[aStart] == 1 )
                trueValid = ( aArray[aStart+1] != 'i' ) ;
              else if ( aArray[aStart] == 2 )
                falseValid = ( aArray[aStart+1] != 'i' ) ;
            }

          // more logic required here!
          pel . disabled = false ;
          if ( ( trueValid ) && ( ! falseValid ) )
          {
            if ( ! pel.checked )
              isInvalid = true ;
            else
              pel . disabled = true ;
          }
          else if ( ( ! trueValid ) && ( falseValid ) )
          {
            if ( pel.checked )
              isInvalid = true ;
            else
              pel . disabled = true ;
          }
          else
            isInvalid = false ;

        }
        else
        {
          pel.disabled = false ;
          if ( aArray != null )
            while ( aStart <= aArray.length )
            {
              if ( aArray[aStart] == pel.value )
                if ( aArray[aStart+1] == 'i' )
                  isInvalid = true ;
              aStart += 2 ;
            }
        }
        var tel = document.getElementById('chb' + el.id ) ;
        if ( tel != null )
        {
          if ( isInvalid )
            tel . style . color = '#FF0000' ;
          else
          {
    	      if ( el.textColor && tel . style . color )
              if ( appSettings . controlStyling != CSS )
                tel . style . color = '#' + el . textColor ;
              else
                tel . style . color = '' ; // was null -- Dont work for IE!;

/*
            if ( el . textColor && appSettings . applyControlStyling )
              tel . style . color = '#' + el . textColor ;
*/              
          }
        }
      }
    }
  }
  for ( c = 0 ; c < invalidTabs.length ; c++ )
  {
    el = document.getElementById('TABIDENT'+invalidTabs[c]) ;
    if ( el != null )
      el.style.visibility = "visible" ;
  }
  for ( c = 0 ; c < tabs.length ; c++ )
  {
    var found = false ;
    for ( cc = 0 ; cc < invalidTabs.length ; cc++ )
      if ( invalidTabs[cc] == tabs[c].id )
      {
        found = true ;
        break ;
      }
    if ( ! found )
    {
      el = document.getElementById('TABIDENT'+tabs[c].id) ;
      if ( el != null )
        el.style.visibility = "hidden" ;
    }
  }
}

// ===============================================================================

function DisableElements ( aParent )
{
  var c ;
  for ( c = aParent.childNodes.length - 1 ; c >= 0  ; c-- )
    if ( aParent.childNodes[c].id )
      if ( ( aParent != document.getElementById("xrui") ) || ( aParent.childNodes[c].id.substr(0,3) == 'TTV' ) )
      {
        aParent.childNodes[c].disabled = true ;
        DisableElements ( aParent.childNodes[c] ) ;
      }
}

function ClearElements ()
{
//  var aBody = document.getElementsByTagName("body").item(0) ;
  var aBody = document.getElementById("xrui") ;
  for ( c = aBody.childNodes.length - 1 ; c >= 0  ; c-- )
    if ( aBody.childNodes[c].id )
      if ( aBody.childNodes[c].id.substr(0,3) == 'TTV' )
        aBody.removeChild(aBody.childNodes[c]) ;
  elements = new Array () ;
  tabs = new Array () ;
}

// ===============================================================================

function AttachElement ( aDiv , aParent )
{
  if ( aParent == null )
    aParent = document.getElementById("xrui") ;
  aParent.appendChild(aDiv);
}

function FindParent ( aID )
{
  if ( aID == 0 )
  {
    var el = document.getElementById("xrdlg") ;
    if ( ! el )
      el = document.getElementById("xrui") ;
    return ( el ) ;
  }
  else
    return ( document.getElementById('TAB'+aID) ) ;
}

// font-name|font-size
function ParamsToStyle ( aArray , aStart )
{
  var s = '' ;
  s += 'font-family:' + aArray[aStart++] + ';' ;
  var sz = aArray[aStart++] ;
  s += 'font-size:' + sz + 'px;' ;
  if ( aArray[aStart++] != 0 )
    s += 'font-weight:bold;' ;
  if ( aArray[aStart++] != 0 )
    s += 'font-style:italic;' ;
  return ( s ) ;
}

function CreateElement ( x , y , w , h , innerHTML , aColor , aAlign , aOverflow , aParent , className )
{
  var oDiv = document.createElement('div');
  oDiv.className = className ;
  oDiv.id = GetNextElementID() ;
  if ( x != null )
  {
    oDiv.style.position = 'absolute' ;
    if ( typeof(x) == 'string' )
      oDiv.style.left = x ;
    else
      oDiv.style.left = ( x + dialog_offset_x ) + 'px' ;
    if ( typeof(y) == 'string' )
      oDiv.style.top = y ;
    else
      oDiv.style.top = ( y + dialog_offset_y ) + 'px' ;
    oDiv.style.width = w + 'px' ;
    oDiv.style.height = h + 'px' ;
  }
  if ( aColor != null )
    oDiv.style.backgroundColor = '#' + aColor ;
  if ( aAlign != null )
    oDiv.style.textAlign = aAlign ;

  if ( aOverflow == null )
    aOverflow = 'hidden' ;
  else if ( aOverflow == 'scroll' )
  {
    oDiv.style.border = '1px' ;
    oDiv.style.borderStyle = 'solid' ;
  }
  oDiv.style.overflow = aOverflow ;
  var spn = null ;
  if ( innerHTML != '' )
  {
    spn = document.createElement('span');
    spn.id = GetNextElementID() ;
    spn.innerHTML = innerHTML ;
    oDiv.appendChild(spn);
  }
  AttachElement ( oDiv , aParent ) ;
  return ( oDiv ) ;
}

function CanExitFail()
{
  if ( dialogTimeout )
    self.setTimeout( 'DlgTimeout()', ( dialogTimeout * 1000 ) ) ;
}

function DlgTimeout()
{
  BtnClk('ok') ;
}

// x|y|w|h|centered|hasCaption|text|bgColor|bkgndImage|timeout|cssName
function SetWnd ( aArray )
{
  var aStart = 0 ;
  var x = aArray[aStart++] ;
  var y = aArray[aStart++] ;
  var w = aArray[aStart++] ;
  var h = aArray[aStart++] ;
  var centered = aArray[aStart++] ;
  var hasCaption = aArray[aStart++] ;
  var aText = aArray[aStart++] ;
  var aBGcolor = aArray[aStart++] ;
  var aImage = aArray[aStart++] ;
  var timeout = aArray[aStart++] ; //dialog time out is given in seconds
  var fStyle = aArray[aStart++] ;

  if (timeout == null)
    timeout = 0 ;
    
  dialogTimeout = timeout ;

  var cl = null ;
  var cssName = dialog_body ;
      
  if ( hasCaption )
  {
    var caption_height = 20 ;
    var dialog_offset = 4 ;
    s = '<span id="xrtitle_span">&nbsp;</span>' ;

    if ( centered )
      var bg = CreateElement ( '50%' , dialog_offset , w , h , '' , null , null , null , null , 'dialog_frame' ) ;
    else
      var bg = CreateElement ( 0 , dialog_offset , w , h , '' , null , null , null , null , 'dialog_frame' ) ;

    var t = CreateElement ( 0 , 0 , w , caption_height , s , null , null , null , bg , 'dialog_title' ) ;
    t.id = 'xrtitle' ;
    var dlg = CreateElement ( 0 , caption_height , w , h - caption_height , '' , cl , null , null , bg , cssName ) ;
    dlg.id = 'xrdlg' ;
    if ( centered )
      bg.style.marginLeft = '-' + ( w / 2 ) + 'px' ; 
  }
  else if ( fStyle != '' )
  {
    document.body.style.backgroundColor = '' ;
    document.body.className = fStyle ;
  }
  
  if ( hasCaption )
  {
    if ( aImage != '' )
      document.body.background = aImage ;
	  var pel = document.getElementById('xrtitle_span') ;
    pel.innerHTML = '&nbsp;' + aText ;
    if ( fStyle == '' )
    {
      pel = document.getElementById('xrdlg') ;
      if ( appSettings . controlStyling != CSS )
        pel.style.backgroundColor = '#' + aBGcolor ;
    }
  }
  else
  {
    if ( centered )
    {
	  var caption_height = 0 ;
	  var dialog_offset = 4 ;      
	  var bg = CreateElement ( '50%' , dialog_offset , w , h , '' , null , null , null , null , 'dialog_frame' ) ;
	  var dlg = CreateElement ( 0 , caption_height , w , h - caption_height , '' , null , null , null , bg , cssName ) ;
      dlg.id = 'xrdlg' ;
      bg.style.marginLeft = '-' + ( w / 2 ) + 'px' ; 
	}
  
//SA     document.title = aText ;
    if ( document.body.className == '' )
      document.body.style.backgroundColor = '#' + aBGcolor ;
  }

  if ( timeout != 0 )
    self.setTimeout( 'DlgTimeout()', ( timeout * 1000 ) ) ;
}

// id|x|y|w|h|aName|txtColor|bgColor|required|aStyle arr|tabID|cssName|helpText|dp|ctrlID
function CreateNumeric ( aType , aArray , aStart )
{
  var aID = "CTRL" + aArray[aStart++] ;
  var x = aArray[aStart++] ;
  var y = aArray[aStart++] ;
  var w = aArray[aStart++] ; // was + 4 Stephen 11/6/2009 ;
  var h = aArray[aStart++] ; // + 4 ;
  var aName = aArray[aStart++] ;
  var aTxtColor = aArray[aStart++] ;
  if ( aTxtColor.length > 0 )
    aTxtColor = 'color:#' + aTxtColor + ';' ;
  var aBGcolor = aArray[aStart++] ;
  if ( aBGcolor.length > 0 )
    aBGcolor = 'background-color:#' + aBGcolor + ';' ;
  var isRequired = aArray[aStart++] ;
  var aStyle = aArray[aStart++] ;
  var aTabID = aArray[aStart++] ;
  var cssName = aArray[aStart++] ;
  var helpText = aArray[aStart++] ;
  var dp = aArray[aStart++] ;
  var ctrlID = aArray[aStart++] ;
  
//  var aux = aName.split( '(' ) ;
//  if ( aux.length > 1 )
//    aName = aux[0] ;

  var fStyle = '' ;
  if ( appSettings . controlStyling == WYSIWYG )
    cssName = '' ;
  if ( cssName == '' )
  {
    if ( appSettings . controlStyling != CSS )
      fStyle = ParamsToStyle ( aStyle , 0 ) + aBGcolor + aTxtColor + ' text-align:right;';
    cssName = dialog_numeric ;
  }

  var f = 0 ; // width fix
  if ( isIE )
    f = 6 ;
  else
    f = 4 ;
//  f = 2 ;

  var s = '<input id="' + aID + '" type="text" onKeyPress="return (numberFormat(this, event, ' + dp + '))" onchange="ValChange(\'' + aID + '\');"' + 
          ' style="position:absolute; left:0px; top:0px; width:' + ( w - f ) + 'px;height:' + 
          ( h - 6 ) + 'px;' + fStyle + '" class="' + cssName + '"' ;
  s +=  ' />';

  var aDiv = CreateElement ( x , y , w , h , s , null , null , null , FindParent ( aTabID ) ) ;  // was w + 6 , h + 6 Stephen 11/6/2009 ;
  var i = AddDlgControl ( aType , aID , aName , isRequired , aTabID , aDiv , helpText ) ;
  i.xrCTRLid = ctrlID ;
//  if ( aTxtColor.length > 0 )
//    i . txtColor = aTxtColor.substring(6,13) ;
}

// id|x|y|w|h|aName|required|aStyle arr|tabID|cssName|helpText
function CreateDate ( aType , aArray , aStart )
{
  var aID = "CTRL" + aArray[aStart++] ;
  var x = aArray[aStart++] ;
  var y = aArray[aStart++] ;
  var w = aArray[aStart++] + 4 ; // Added 4 Stephen 11/6/2009 ;
  var h = aArray[aStart++] + 4 ;
  var aName = aArray[aStart++] ;
  var isRequired = aArray[aStart++] ;
  var aStyle = aArray[aStart++] ;
  var aTabID = aArray[aStart++] ;
  var cssName = aArray[aStart++] ;
  var helpText = aArray[aStart++] ;

  var fStyle = '' ;
  if ( appSettings . controlStyling == WYSIWYG )
    cssName = '' ;  
  if ( cssName == '' )
  {
    if ( appSettings . controlStyling != CSS )
      fStyle = ParamsToStyle ( aStyle , 0 ) ;
    cssName = dialog_date ;
  }

  if ( isIE )
    w -= 6 ;
  else
    w -= 4 ;
//  var s = '<input id="' + aID + '" type="text" onchange="javascript:ValChange(\'' + aID + '\');"  disabled="true" style="background-color:#FFFFFF;position:absolute; left:0px; top:0px; width:' + ( w - 28 ) + 'px;height:' + ( h - 6 ) + 'px;' + fStyle + flatStyle + '" />' +
  var s = '<span id="' + aID + '" style="position:absolute; left:0px; top:0px; width:' + ( w - 28 ) + 'px;height:' + ( h - 6 ) +
          'px;' + fStyle + '" class="' + cssName + '"></span>' +
          '<a href="#" onclick="javascript:if(isEnabled(\'' + aID + '\')) show_calendar_widget(this,\'' + aID + '\');" id="date_link_' + aID + '" class="calendar-link"><img src="images/datebtn.gif" style="position:absolute; left:' + ( w - 22 ) + 'px;top:0px;width:24px;height:24px;border:none;" /></a>' ;
  var aDiv = CreateElement ( x , y , w , h , s , null , null , null , FindParent ( aTabID ) ) ;
  AddDlgControl ( aType , aID , aName , isRequired , aTabID , aDiv , helpText ) ;
}

function TextAreaMaxLength (fld, e, aID, maxlen)
{
	if (maxlen > 0)
	{
    var pel = document.getElementById(aID);
    if ( pel )
      if (pel.value.length > maxlen) 
      {
        whichCode = (window.event) ? e.keyCode : e.which ;
        if (whichCode == 0 && e.keyCode > 0)
          whichCode = e.keyCode // Fix for FF tab
        switch (whichCode)
        {
  	      case 08:    // BackSpace
  	      case 09:    // Tab
//  	      case 13:    // Enter
  	        return (true);

        	case 35:    // #
  	        return ( e.charCode == 0 );  // End
  	  
  	      case 36:    // Home , $
  	        return ( ! e.shiftKey ) ;
  	  
        	case 37:    // Left , %
  	        if ( e.charCode == 0 )
  	          return ( true ) ;
  	        return ( ! e.shiftKey );

  	      case 39:    // right, '
  	        if ( e.charCode == 0 )
  	          return ( true ) ;
  	        return ( ! e.shiftKey && (e.charCode == 0) );

  	      case 46:    // .
            if ( e.charCode == 0 )  // Del
  	          return (true);
  	        break;
	      }
      	return (false) ;
      }
  }
  return (true);
}

// id|x|y|w|h|aName|txtColor|bgColor|isPassword|isMultiLine|required|aStyle arr|tabID|cssName|helpText|dp|ctrlID
function CreateText ( aType , aArray , aStart )
{
  var aID = "CTRL" + aArray[aStart++] ;
  var x = aArray[aStart++] ;
  var y = aArray[aStart++] ;
  var w = aArray[aStart++] ;
  var h = aArray[aStart++] ;
  var aName = aArray[aStart++] ;
  var aTxtColor = aArray[aStart++] ;
  if ( aTxtColor.length > 0 )
    aTxtColor = 'color:#' + aTxtColor + ';' ;
  var aBGcolor = aArray[aStart++] ;
  if ( aBGcolor.length > 0 )
    aBGcolor = 'background-color:#' + aBGcolor + ';' ;
  var isPassword = aArray[aStart++] ;
  var isMultiLine = aArray[aStart++] ;
  var isRequired = aArray[aStart++] ;
  var aStyle = aArray[aStart++] ;
  var aTabID = aArray[aStart++] ;
  var cssName = aArray[aStart++] ;
  var helpText = aArray[aStart++] ;
  var dp = aArray[aStart++] ;
  var ctrlID = aArray[aStart++] ;
  var maskStr = aArray[aStart++] ; 

//  var aux = aName.split( '(' ) ;
//  if ( aux.length > 1 )
//    aName = aux[0] ;

  var fStyle = '' ;
  if ( appSettings . controlStyling == WYSIWYG )
    cssName = '' ;  
  if ( cssName == '' )
  {
    if ( appSettings . controlStyling != CSS )
      fStyle = ParamsToStyle ( aStyle , 0 ) + aBGcolor + aTxtColor ;
    cssName = dialog_arrayedit ;
  }

  var f = 0 ; // width fix
  if ( isIE )
    f = 6 ;
  else
    f = 4 ;
  var ty = 'text' ;
  if ( isPassword )
    ty = 'password' ;
  var s = '';
  var overflow = null ;
  if ( dp > 0 )
    s = ' maxlength="' + dp +  '"' ;
  if ( ! isMultiLine )
    s = '<input id="' + aID + '"' + s + ' type="' + ty + '" onchange="javascript:ValChange(\'' + aID + '\');" style="position:absolute; left:0px; top:0px; width:' +
            ( w - f ) + 'px;height:' + ( h - 6 ) + 'px;' + fStyle + '" class="' + cssName + '" />' ; 
  else
  {
  	s = '<textarea id="' + aID + '"' + s + ' onchange="javascript:ValChange(\'' + aID + '\');"' +
  	    ' onKeyPress="return (TextAreaMaxLength(this, event,\'' + aID + '\',\'' + dp + '\'));"' + 
  	    ' style="position:absolute; left:0px; top:0px; width:' + ( w - f ) + 'px;height:' + ( h - 6 ) + 
  	    'px;' + fStyle + '" class="' + cssName + '"></textarea>' ;
  }          
  var aDiv = CreateElement ( x , y , w , h , s , null , null , overflow , FindParent ( aTabID ) ) ;  
  var i = AddDlgControl ( aType , aID , aName , isRequired , aTabID , aDiv , helpText ) ;    
  i.xrCTRLid = ctrlID ;

  if (maskStr != "")
  {
    oStringMask = new Mask(maskStr, "string");
    oStringMask.attach(document.getElementById(aID));
  }
}

// id|x|y|w|h|aText|aTextColor|aBGcolor|aAlignment|borderStyle|borderColor|aStyle arr|attachmentURL|tabID|cssName|showscroll
function CreateLabel ( aType , aArray , aStart )
{
  var aID = "CTRL" + aArray[aStart++] ;
  var x = aArray[aStart++] ;
  var y = aArray[aStart++] ;
  var w = aArray[aStart++] ;
  var h = aArray[aStart++] ;
  var aText = aArray[aStart++] ;
  var aTextColor = aArray[aStart++] ;
  var aBGcolor = aArray[aStart++] ;
  if ( appSettings . controlStyling == CSS || aBGcolor == 'NONE' )
    aBGcolor = null ;

  var aAlign = aArray[aStart++] ;
  var borderStyle = aArray[aStart++] ;
  var borderColor = '#' + aArray[aStart++] ;
  var aStyle = aArray[aStart++] ;
  var aURL = aArray[aStart++] ;
  if ( aURL . length != 0 )
    aText = '<a href="' + aURL + '">' + aText + '</a>' ;
  var aTabID = aArray[aStart++] ;
  var cssName = aArray[aStart++] ;
  var showscroll = aArray[aStart++] ;

  var s = '<span id="' + aID + '" style="position:absolute; left:0px; top:0px;' ;
  if ( aAlign != 'left' )
    s += 'width:' + w + 'px;' ;

  if ( appSettings . controlStyling == WYSIWYG )
    cssName = '' ;
  var fStyle = cssName ;
  if ( fStyle == '' )
  {
    if ( appSettings . controlStyling != CSS )
      s += ParamsToStyle ( aStyle , 0 ) + ';color:#' + aTextColor + '";' ;
    cssName = dialog_label ;
  }

 // s += '" class="' + cssName +

  s += '" >' + aText + '</span>' ;
   
  var aOverflow = null ;
  if ( showscroll )
    aOverflow = 'auto' ;

  var i = CreateElement ( x , y , w , h , s , aBGcolor , aAlign , aOverflow , FindParent ( aTabID ) , cssName ) ;

  if ( borderStyle > 0 && ( fStyle == '' ) )
  {
    switch ( borderStyle )
    {
      case 1 : // plain
        i.style.borderRightColor = borderColor ;
        i.style.borderLeftColor = borderColor ;
        i.style.borderTopColor = borderColor ;
        i.style.borderBottomColor = borderColor ;
        break;
          
      case 2 : // sunk
        i.style.borderRightColor = "#FFFFFF" ;
        i.style.borderLeftColor = "#808080" ;
        i.style.borderTopColor = "#808080" ;
        i.style.borderBottomColor = "#FFFFFF" ;
        break;
        
      case 3 :  // raised
        i.style.borderRightColor = "#808080" ;
        i.style.borderLeftColor = "#FFFFFF" ;
        i.style.borderTopColor = "#FFFFFF" ;
        i.style.borderBottomColor = "#808080" ;
        break;
  	}
  	i.style.borderRightStyle = "solid" ;
    i.style.borderRightWidth = "1px" ;

    i.style.borderLeftStyle = "solid" ;
    i.style.borderLeftWidth = "1px" ;

    i.style.borderTopStyle = "solid" ;
    i.style.borderTopWidth = "1px" ;

    i.style.borderBottomStyle = "solid" ;
    i.style.borderBottomWidth = "1px" ;
  }
  AddDlgControl ( aType , aID , "" , false , aTabID , i ) ;
}

// id|x|y|w|h|aFilename|isStretched|tabID|cssName
function CreateImage ( aType , aArray , aStart )
{
  var aID = "CTRL" + aArray[aStart++] ;
  var x = aArray[aStart++] ;
  var y = aArray[aStart++] ;
  var w = aArray[aStart++] ;
  var h = aArray[aStart++] ;
  var aFilename = aArray[aStart++] ;
  var isStretched = aArray[aStart++] ;
  var aTabID = aArray[aStart++] ;
  var cssName = aArray[aStart++] ;

  var s = '<img src="' + aFilename + '"' ;
  if ( isStretched )
    s += ' width="' + w + '" height="' + h + '"' ;

  if ( cssName == '' )
    cssName = dialog_image ;
  s += ' class="' + cssName +'"' ;

  s += ' />' ;
  var aDiv = CreateElement ( x , y , w , h , s , null , null , null , FindParent ( aTabID ) , cssName ) ;
  var i = AddDlgControl ( aType , aID , "" , false , aTabID , aDiv ) ;
  i . items = new Array( isStretched, w, h ) ;
}

// id|x|y|w|h|aCaption|aAction|isEnabled|aStyle arr|tabID|ConfirmationText|cssName|isArea
function CreateButton ( aType , aArray , aStart )
{
  var aID = "CTRL" + aArray[aStart++] ;
  var x = aArray[aStart++] ;
  var y = aArray[aStart++] ;
  var w = aArray[aStart++] ;
  var h = aArray[aStart++] ;
  var aCaption = aArray[aStart++] ;
  var aAction = aArray[aStart++] ;
  var isDisabled = '' ;
  if ( aArray[aStart++] == 0 )
    isDisabled = 'disabled="true" ' ;
  var aStyle = aArray[aStart++] ;
  var aTabID = aArray[aStart++] ;
  var confirmation = aArray[aStart++] ;
  var cssName = aArray[aStart++] ;
  var isArea = aArray[aStart++] ;

  var s ;

  if ( isIE )
    f = 4 ;
  else
    f = 0 ;

  if ( ! isArea )
  {
    var accel = '' ;
    var c = aCaption.indexOf('&');
    if ( ( c != -1 ) && ( c < aCaption.length-1 ) )
    {
      if ( aCaption.charAt(c+1) != '&' )
        accel = 'accesskey="' + aCaption.charAt(c+1) + '" ' ;
      aCaption = aCaption.substring(0,c) + aCaption.substring(c+1);
    }

    s = '<input id="' + aID + '" type="button" ' + accel + 'value="' + aCaption + '" onclick="javascript:if(isEnabled(\'' +
        aID + '\')) BtnClk(\'' + aAction + '\')" style="position:absolute; left:0px; top:0px; width:' + (w + f) +
        'px;height:' + h + 'px;' ;
  }
  else
    s = '<img src="images/tiny.gif" alt="' + aCaption + '" width=100% height=100% style="cursor:pointer;" onclick="javascript:if(isEnabled(\'' +
        aID + '\')) BtnClk(\'' + aAction + '\')' ;

  if ( appSettings . controlStyling == WYSIWYG )
    cssName = '' ;
  if ( cssName == '' )
  {
    if ( ! isArea && appSettings . controlStyling != CSS )
      s += ParamsToStyle ( aStyle , 0 ) ;
    cssName = dialog_button  ;
  }
  s += '" class="' + cssName +'"' ;

/*
  if ( aControl.tabno > 0 )
    s += ' tabindex=' + aControl.tabno ;
*/
  s += ' />' ;

  var aDiv = CreateElement ( x , y , w + f , h , s , null , null , null , FindParent ( aTabID ) ) ;
  var i = AddDlgControl ( aType , aID , "" , false , aTabID , aDiv ) ;
  i . confirmText = confirmation ;
}

function BuildLBcontent ( aID , items , addNoneItem ) {
  var s = "" ;
  if ( addNoneItem )
  ;
  for ( var c = 0 ; c < items.length ; c+=2 )
    s += '<option value="' + items[c] + '" ondblclick="if (CanDblClick( \'' + aID + '\', \'' + items[c] +  '\'))LBdblclk();">' + items[c+1] + '</option>' ;
  return s ;
}

// id|x|y|w|h|aName|items arr|required|multi-select|aStyle arr|tabID|sortInvalid|showInvalid|allowInvalid|cssName|isWebInput|helpText|currentSel|aTextColor
function CreateLB ( aType , aArray , aStart , isCreate )
{
  if ( ! isCreate ) {
    var el = aArray ;
    var s = BuildLBcontent ( el.id , el.items ) ;
    var sel = document.getElementById(el.id);
    if ( sel )
      sel.innerHTML = s ;
    return
  }
  var aID = "CTRL" + aArray[aStart++] ;
  var x = aArray[aStart++] ;
  var y = aArray[aStart++] ;
  var w = aArray[aStart++] ;     // was + 4 Stephen 11/6/2009 ;
  var h = aArray[aStart++] ;     // was + 4 Stephen 11/6/2009 ;
  var aName = aArray[aStart++] ;
  var items = aArray[aStart++] ;
  var isRequired = aArray[aStart++] ;
  var isMultiSel = aArray[aStart++] ;
  var aStyle = aArray[aStart++] ;
  var aTabID = aArray[aStart++] ;
  var sortInvalid = aArray[aStart++] ;
  var showInvalid = aArray[aStart++] ;
  var allowInvalid = aArray[aStart++] ;
  var cssName = aArray[aStart++] ;
  var isWebInput = aArray[aStart++] ;
  var helpText = aArray[aStart++] ;
  var selectedVal = aArray[aStart++] ; // multi-sel is comma delimd list
  var aTextColor = aArray[aStart++] ;

  var fStyle = '' ;
  if ( appSettings . controlStyling == WYSIWYG )
  {
    cssName = '' ;
    w += 4 ;  // Compensate for ctrl3d
    h += 4 ;  // Compensate for ctrl3d
  }
  if ( cssName == '' )
  {
    if ( appSettings . controlStyling != CSS )
      fStyle = ParamsToStyle ( aStyle , 0 ) + ';color:#' + aTextColor + ';overflow:visible;vertical-align:middle;';
    if ( isWebInput )
      cssName = dialog_altlistbox ;
    else
      cssName = dialog_listbox ;
  }

  var f = 0 ; // width fix
  if ( isIE )
    f = 2 ;
  else
    f = 4 ;

  var ms = '' ;
  var i = null ;
  if ( isWebInput )
  {
    if ( isMultiSel )
    {
      var aDiv = CreateElement ( x , y , w , h , '' , null , null , 'auto' , FindParent ( aTabID ) ) ; // was w-4 , h-4 Stephen 11/6/2009 ;
      for ( c = 0 ; c < items.length ; c+=2 )
      {
        var aID2 = aID + '_' + items[c] ;
        var s = '<div style="white-space:nowrap;vertical-align:middle;">' +
                '<input type="checkbox" id="' + aID2 + '" value="' + items[c] + '" onclick="javascript:if(isEnabled(\'' + aID2 + '\'))ValChange(\'' + aID2 + '\');" />' +
                '<span id="chb' + aID2 + '" onclick="javascript:if(isEnabled(\'' + aID2 + '\'))CBclick(\'' + aID2 + '\');" style="' + fStyle + '">' +
                items[c+1] +
                '</span>' +
                '</div>' ;

        var aDiv2 = CreateElement ( null , null , null , null , s , null , null , null , aDiv , cssName ) ;
        i = AddDlgControl ( "chb" , aID2 , aName , isRequired , aTabID , aDiv2 , helpText , aDiv ) ;
        i.multiSel = true ;
      }
    }
    else
    {// was w-4 , h-4 Stephen 11/6/2009 ;
      var aDiv = CreateElement ( x , y , w , h , '<input type="hidden" id="' + aID + '" value="' + selectedVal + '" />' , null , null , 'auto' , FindParent ( aTabID ) ) ;
      var i = AddDlgControl ( "hlb" , aID , aName , isRequired , aTabID , aDiv , helpText ) ;
      var selTxt = ' <span class="selected_marker">(Last selection)</span>' ;
      for ( c = 0 ; c < items.length ; c+=2 )
      {
        var s;
        if ( true ) // aObject . aValues [ c ] . valid
          s = 'a';
        else
          s = 'span' ;
        s = '<' + s + ' href="javascript:SetValueAndOK(\'' + aID + '\',' + items[c] + ');" style="' + fStyle + '">' + items[c+1] + '</' + s + '>' ;
        if ( parseInt ( selectedVal ) == parseInt ( items[c] ) )
          s += selTxt ;
        s = '<div style="' + fStyle + '">' + s + '</div>' ;
        CreateElement ( null , null , null , null , s , null , null , null , aDiv , cssName ) ;
      }
    }
  }
  else
  {
    if ( isMultiSel )
      ms = 'multiple="multiple" ' ;
    var s = '<select id="' + aID + '" size="' + items.length + '" ' + ms + ' onchange="javascript:if(isEnabled(\'' + aID +
            '\'))ValChange(\'' + aID + '\');" style="position:absolute; left:0px; top:0px; width:' + w  + 'px;height:' + h +
            'px;' + fStyle + '" class="' + cssName + '" onmousedown="LBmouseDown(\'' + aID + '\')" onmouseup="LBmouseUp(\'' + aID + '\')" >' ;
    s += BuildLBcontent ( aID , items ) ;
    s += '</select>' ;
    var aDiv = CreateElement ( x , y , w , h , s , null , null , null , FindParent ( aTabID ) ) ;
    i = AddDlgControl ( aType , aID , aName , isRequired , aTabID , aDiv , helpText ) ;
  }

  if ( i )
    if ( items )
    {
      i . sortInvalid = sortInvalid ;
      i . showInvalid = showInvalid ;
      i . allowInvalid = allowInvalid ;
      i . items = items ;
    }
}

// id|x|y|w|h|aName|items arr|required|aStyle arr|tabID|sortInvalid|ShowInvalid|allowInvalid|cssName|isWebInput|helpText|aTextColor
function CreateCB ( aType , aArray , aStart , isCreate )
{
  if ( ! isCreate ) {
    var el = aArray ;
    var s = BuildLBcontent ( el.id , el.items , true ) ;
    var sel = document.getElementById(el.id);
    if ( sel )
      sel.innerHTML = s ;
    return
  }
  var aID = "CTRL" + aArray[aStart++] ;
  var x = aArray[aStart++] ;
  var y = aArray[aStart++] ;
  var w = aArray[aStart++] ; // was + 4 Stephen 11/6/2009 ;
  var h = aArray[aStart++] ; // was + 4 Stephen 11/6/2009 ;
  var aName = aArray[aStart++] ;
  var items = aArray[aStart++] ;
  var isRequired = aArray[aStart++] ;
  var aStyle = aArray[aStart++] ;
  var aTabID = aArray[aStart++] ;
  var sortInvalid = aArray[aStart++] ;
  var showInvalid = aArray[aStart++] ;
  var allowInvalid = aArray[aStart++] ;
  var cssName = aArray[aStart++] ;
  var isWebInput = aArray[aStart++] ;
  var helpText = aArray[aStart++] ;
  var selectedVal = aArray[aStart++] ; // multi-sel is comma delimd list
  var aTextColor = aArray[aStart++] ;

  var fStyle = '' ;
  if ( appSettings . controlStyling == WYSIWYG )
    cssName = '' ;  
  if ( cssName == '' )
  {
   if ( appSettings . controlStyling != CSS )
      fStyle = ParamsToStyle ( aStyle , 0 ) + ';color:#' + aTextColor ;
    if ( isWebInput )
      cssName = dialog_altlistbox ; 
    else
      cssName = dialog_listbox ;
  }

  var f = 0 ; // width fix
  if ( isIE )
    f = 2 ;
  else
    f = 4 ;

  if ( isWebInput )
  {// was w-4 , h-4 Stephen 11/6/2009 ;
    var aDiv = CreateElement ( x , y , w , h , '<input type="hidden" id="' + aID + '" value="' + selectedVal + '" />' , null , null , 'auto' , FindParent ( aTabID ) ) ;
    var i = AddDlgControl ( "hlb" , aID , aName , isRequired , aTabID , aDiv , helpText ) ;
    var selTxt = ' <span class="selected_marker">(Last selection)</span>' ;
    for ( c = 0 ; c < items.length ; c+=2 )
    {
      var s;
      if ( true ) // aObject . aValues [ c ] . valid
        s = 'a';
      else
        s = 'span' ;
      s = '<' + s + ' href="javascript:SetValueAndOK(\'' + aID + '\',' + items[c] + ');" style="' + fStyle + '">' + items[c+1] + '</' + s + '>' ;
      if ( parseInt ( selectedVal ) == parseInt ( items[c] ) )
        s += selTxt ;
      s = '<div style="' + fStyle + '">' + s + '</div>' ;
      CreateElement ( null , null , null , null , s , null , null , null , aDiv , cssName ) ;
    }  	
  }
  else
  {
    var s = '<select id="' + aID + '" size="1" onChange="javascript:ValChange(\'' + aID +
          '\');" style="position:absolute; left:0px; top:0px; width:' +
          w + 'px;' + fStyle + '" class="' + cssName + '">' ;
    s += BuildLBcontent ( aID , items , true ) ;
    var aDiv = CreateElement ( x , y , w , h , s , null , null , null , FindParent ( aTabID ) ) ;
    var i = AddDlgControl ( aType , aID , aName , isRequired , aTabID , aDiv , helpText ) ;
  }
  if ( i )
  {
    i . sortInvalid = sortInvalid ;
    i . showInvalid = showInvalid ;
    i . allowInvalid = allowInvalid ;
    i . items = items ;
  }
}

// id|x|y|w|h|aText|aName|aValue|aTextColor|aBGcolor|aStyle arr|tabID|cssName|helpText
function CreateCheckBox ( aType , aArray , aStart )
{
  var aID = "CTRL" + aArray[aStart++] ;
  var x = aArray[aStart++] ;
  var y = aArray[aStart++] ;
  var w = aArray[aStart++] ;
  var h = aArray[aStart++] ; //+ 4  Stephen 11/6/2009
  var aText = aArray[aStart++] ;
  var aName = aArray[aStart++] ;
  var aValue = aArray[aStart++] ;
  var isMultiSel = aArray[aStart++] ;
  var aTextColor = aArray[aStart++] ;
  var aBGcolor = aArray[aStart++] ;
  if ( aBGcolor == 'NONE' || appSettings . controlStyling == CSS )
    aBGcolor = null ;
  var aStyle = aArray[aStart++] ;
  var aTabID = aArray[aStart++] ;
  var cssName = aArray[aStart++] ;
  var helpText = aArray[aStart++] ;
  var is

  var fStyle = '' ;
  if ( appSettings . controlStyling == WYSIWYG )
    cssName = '' ;  
  if ( cssName == '' )
  {
    if ( appSettings . controlStyling != CSS )
      fStyle = ParamsToStyle ( aStyle , 0 ) + ';color:#' + aTextColor + '; overflow:visible;vertical-align: middle ';
    cssName = dialog_checkbox ;
  }
  else
    aBGcolor = null ;

  var s = '<table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%" ><tr><td NOWRAP>' + // ' class="' + cName + '" >' +
          '<input type="checkbox" id="' + aID + '" value="' + aValue + '" onclick="javascript:if(isEnabled(\'' + aID + '\'))ValChange(\'' + aID + '\');" />' +
          '<span id="chb' + aID + '" onclick="javascript:if(isEnabled(\'' + aID + '\'))CBclick(\'' + aID + '\');" style="' + fStyle + ';cursor:default;">' +
          aText +
          '</span>' +
          '</td></tr></table>' ;
  var aDiv = CreateElement ( x , y , w , h , s , aBGcolor , null , null , FindParent ( aTabID ) , cssName ) ;
  var i = AddDlgControl ( aType , aID , aName , true , aTabID , aDiv , helpText ) ;
  i . textColor = aTextColor ;
  i . multiSel = isMultiSel ;
}

// id|x|y|w|h|aText|aName|aValue|aTextColor|aBGcolor|required|aStyle arr|attachmentURL|tabID|showInvalid|cssName|helpText
function CreateRadioButton ( aType , aArray , aStart )
{
  var aID = "CTRL" + aArray[aStart++] ;
  var x = aArray[aStart++] ;
  var y = aArray[aStart++] ;
  var w = aArray[aStart++] ; // was + 4 Stephen 11/6/2009
  var h = aArray[aStart++] ; // was + 4 Stephen 11/6/2009
  var aText = aArray[aStart++] ;
  var aName = aArray[aStart++] ;
  var aValue = aArray[aStart++] ;
  aID += '_' + aValue ;
  var aTextColor = aArray[aStart++] ;
  var aBGcolor = aArray[aStart++] ;
  if ( aBGcolor == 'NONE' || appSettings . controlStyling == CSS )
    aBGcolor = null ;
  var isRequired = aArray[aStart++] ;
  var aStyle = aArray[aStart++] ;
  var aURL = aArray[aStart++] ;
  if ( aURL . length != 0 )
    aText = '<a href="' + aURL + '">' + aText + '</a>' ;
  var aTabID = aArray[aStart++] ;
  var showInvalid = aArray[aStart++] ;
  var cssName = aArray[aStart++] ;
  var helpText = aArray[aStart++] ;

  var fStyle = '' ;
  if ( appSettings . controlStyling == WYSIWYG )
    cssName = '' ;  
  if ( cssName == '' )
  {
    if ( appSettings . controlStyling != CSS )
      fStyle = ParamsToStyle ( aStyle , 0 ) + ';color:#' + aTextColor + ';overflow:visible;vertical-align:top;'  ;
    cssName = dialog_radiobtn ;
  }
  else
    aBGcolor = null ;

  fStyle += 'cursor:pointer; ' ;  // " class="' + cssName ;

  var a = aText.split('|');
  var s = '<table border="0" cellpadding="0" cellspacing="0"><tr><td NOWRAP>' + // class="' + cName + '" >' +
          '<input style="cursor:pointer;" type="radio" id="' + aID + '" name="' + aName + '" value="' + aValue + '" onclick="javascript:if(isEnabled(\'' + aID + '\'))ValChange(\'' + aID + '\');" />' +
          '<span id="rt' + aID + '" onclick="javascript:if(isEnabled(\'' + aID + '\'))CBclick(\'' + aID + '\');" style="' + fStyle + '">' +
          a[0] +
          '</span>' +
          '</td></tr></table>' ;
  var aDiv = CreateElement ( x , y , w , h , s , aBGcolor , null , null , FindParent ( aTabID ) , cssName ) ;
  var i = AddDlgControl ( aType , aID , aName , isRequired , aTabID , aDiv , helpText ) ;
  i . showInvalid = showInvalid ;
  i . textColor = aTextColor ;
  i . text = aText ;
}

// id|x|y|w|h|selName|isMultiSel|required|value arr|aStyle arr|tabID|cssName|helpText
function CreateArrayLB ( aType , aArray , aStart )
{
  var aID = "CTRL" + aArray[aStart++] ;
  var x = aArray[aStart++] ;
  var y = aArray[aStart++] ;
  var w = aArray[aStart++] ;
  var h = aArray[aStart++] ;
  var aName = aArray[aStart++] ;
  var isMultiSel = aArray[aStart++] ;
  var isRequired = aArray[aStart++] ;
  var items = aArray[aStart++] ;
  var aStyle = aArray[aStart++] ;
  var aTabID = aArray[aStart++] ;
  var cssName = aArray[aStart++] ;
  var helpText = aArray[aStart++] ;
  
  var fStyle = '' ;
  if ( appSettings . controlStyling == WYSIWYG )
    cssName = '' ;  
  if ( cssName == '' )
  {
    if ( appSettings . controlStyling != CSS )
      fStyle = ParamsToStyle ( aStyle , 0 ) ;

    cssName = dialog_arraylist ;
  }
  var ms = '' ;
  if ( isMultiSel )
    ms = 'multiple="multiple" ' ;
  var s = '<select id="' + aID + '" size="' + items.length + '" ' + ms + ' onchange="javascript:ValChange(\'' + aID + '\');" style="position:absolute; left:0px; top:0px; width:' + w + 'px;height:' + h + 'px;' + fStyle + '" class="' + cssName + '" onmouseup="javascript:LBmouseUp(\'' + aID + '\')" ondblclick="javascript:LBdblclk()" >' ;
  var c ;
  for ( c = 0 ; c < items.length ; c++ )
    s += '<option value="' + (c+1) + '">' + items[c] + '</option>' ;
  s += '</select>' ;
  var aDiv = CreateElement ( x , y , w , h , s , null , null , null , FindParent ( aTabID ) ) ;
  AddDlgControl ( aType , aID , aName , isRequired , aTabID , aDiv , helpText ) ;
}
                        
// id|x|y|w|h|selName|required|value arr|aStyle arr|tabID|cssName|helpText
function CreateArrayCB ( aType , aArray , aStart )
{
  var aID = "CTRL" + aArray[aStart++] ;
  var x = aArray[aStart++] ;
  var y = aArray[aStart++] ;
  var w = aArray[aStart++] ;
  var h = aArray[aStart++] ;
  var aName = aArray[aStart++] ;
  var isMultiSel = aArray[aStart++] ;
  var isRequired = aArray[aStart++] ;
  var items = aArray[aStart++] ;
  var aStyle = aArray[aStart++] ;
  var aTabID = aArray[aStart++] ;
  var cssName = aArray[aStart++] ;
  var helpText = aArray[aStart++] ;
  
  var fStyle = '' ;
  if ( appSettings . controlStyling == WYSIWYG )
    cssName = '' ;  
  if ( cssName == '' )
  {
    if ( appSettings . controlStyling != CSS )
      fStyle = ParamsToStyle ( aStyle , 0 ) ;

    cssName = dialog_arraylist ;
  }
  var s = '<select id="' + aID + '" size="1" onchange="javascript:ValChange(\'' + aID + '\');" style="position:absolute; left:0px; top:0px; width:' + w + 'px;' + fStyle + '" class="' + cssName + '">' ;
  s += '<option value="0" class="noneValue"></option>' ;
  var c ;
  for ( c = 0 ; c < items.length ; c++ )
    s += '<option value="' + (c+1) + '">' + items[c] + '</option>' ;
  s += '</select>' ;
  var aDiv = CreateElement ( x , y , w , h , s , null , null , null , FindParent ( aTabID ) ) ;
  AddDlgControl ( aType , aID , aName , isRequired , aTabID , aDiv , helpText ) ;
}

function GetSortImage ( sortMode )
{
  var imageName
  var alt
  switch ( sortMode )
  {
    case 1 :
      imageName = 'images/asc.gif' ;
      alt = 'Ascending' ;
      break ;
    case 2 :
      imageName = 'images/desc.gif' ;
      alt = 'Descending' ;
      break ;
    default :
      imageName = 'images/tiny.gif' ;
      alt = '' ;
  }
  return ( '<img height="8" width="8" src="' + imageName + '" alt="' + alt + '" />' ) ;
}

//New Grid ---------------------------------------------------------------------

function TableRow ( id , dIndex , isSelected , isValid , cols )
{
  this.rowID = id ;
  this.designIndex = dIndex ;
  this.isSelected = isSelected ;
  this.isValid = isValid ;
  this.cols = cols ;  // [ value {, value } ]
}

// add a table row
function AddRow ( id , dIndex , isSelected , isValid , cols , tableRows )
{
  var r = new TableRow ( id , dIndex , isSelected , isValid , cols ) ;
  tableRows = ArrayPush ( tableRows , r ) ;
}

function ClearGridRowSelected( tableRows )
{
  for ( var c = 0 ; c <= tableRows . length - 1 ; c++ )
    tableRows[ c ] . isSelected = false ;
}

function SetGridRowSelected( id, tableRows )
{
  for ( var c = 0 ; c <= tableRows . length - 1 ; c++ )
    if ( tableRows[ c ] . rowID == id )
      tableRows[ c ] . isSelected = true ;
}

function IsGridRowSelected ( tableRows , id )
{
  for ( var c = 0 ; c <= tableRows . length - 1 ; c++ )
    if ( tableRows[ c ] . rowID == id )
      return ( tableRows[ c ] . isSelected ) ;
  return ( false ) ;
}

function ClearGridRowInvalids( tableRows )
{
  for ( var c = 0 ; c <= tableRows . length - 1 ; c++ )
    tableRows[ c ] . isValid = true ;
}

function SetGridRowInvalid( id, tableRows )
{
  for ( var c = 0 ; c <= tableRows . length - 1 ; c++ )
    if ( tableRows[ c ] . rowID == id )
      tableRows[ c ] . isValid = false ;
}

function IsGridRowValid ( tableRows , id )
{
  for ( var c = 0 ; c <= tableRows . length - 1 ; c++ )
    if ( tableRows[ c ] . rowID == id )
      return ( tableRows[ c ] . isValid ) ;
  return ( false ) ;
}

function GridRowClick ( ev )
{
  var s = ev.target ? ev.target.id : ev.srcElement.id;
  var data = s.split( '_' ) ;
  if ( data.length != 2 )
    return ;
  SetGridVal ( data [ 0 ] , data [ 1 ] ) ;
}

// populate a grid based on the 2D tableRows array in it.  Clear existing one
// http://msdn.microsoft.com/library/default.asp?url=/workshop/author/dhtml/reference/objects.asp
function GridPop( aElement )
{
  var aID = aElement . id ;
  table = document.getElementById( 'grd' + aID ) ;

  var c , cc ;
  var sortType = 'T' ;

  // Assign the sort images
  var ccc = 0 ;
  for ( c = 0 ; c < aElement . values [ 9 ] . length ; c+=2 )
  {
    var spn = document.getElementById( 'sortimg_' + aID + '_' + ccc ) ;
    if ( spn != null )
    {
      if ( aElement . sortCol == ccc )
      {
        cc = aElement . sortDir ;
        sortType = aElement . values [ 9 ] [ c + 1 ] ;
      }
      else
        cc = 0 ;
      spn . innerHTML = GetSortImage ( cc ) ;
    }
    ccc++ ;
  }

  // Clear Existing rows
  var st = 0 ;
  if ( aElement . hasTitle )
    st = 1 ;
  while ( table . rows . length > st )
    table . deleteRow ( table . rows . length - 1 ) ;

  // Start building table rows
  var rows = aElement . items ;

  if ( aElement . sortCol != -1 )
  {
    rows = SortValues ( aElement , rows , aElement . sortCol , aElement . sortDir , sortType ) ; // Sort the rows based on sortCol , sortDir
  }
  if ( aElement . sortInvalid )
    rows = SortInvalidValues ( aElement , rows ) ; // Sort the rows based on sortInvalid & isValid

  // FF will allow write access to table.innerHTML, IE won't  
  var tableSpan = document.getElementById(aElement . id) ;
  var tc = tableSpan.innerHTML ;
  var p = tc.indexOf ( '</tbody>' ) ;
  if ( p == -1 )
    p = tc.indexOf ( '</TBODY>' ) ;
  if ( p == -1 )
    p = tc.indexOf ( '</table>' ) ;
  if ( p == -1 )
    p = tc.indexOf ( '</TABLE>' ) ;
  if ( p == -1 )
    return ;
  tc = tc.substr ( 0 , p ) ;

  for ( c = 0 ; c < rows . length ; c++ )
  {
    var aRow = rows [ c ] ;
    if ( ( aElement . showInvalid ) || ( aRow . isValid ) )
    {
      // ----
        var r = '<tr id="' + aID + '_' + aRow . rowID + '" onclick="SetGridVal(\'' + aID + '\',' + aRow . rowID + ')">' ;
      // ----
      /* ----
        var newRow ;
        if ( isIE )
        {
          //IE has no insertRow Method ;-(
          newRow = table.tBodies[0].appendChild(document.createElement('tr'));
        }
        else
          newRow = table.insertRow(c+st) ;
        newRow . id = aID + '_' + aRow . rowID ;
        if ( isIE )
          newRow.attachEvent('onclick',GridRowClick);
        else
          newRow . onclick = GridRowClick ;
      ---- */

      // now Insert the cells
      for ( cc = 0 ; cc < aRow . cols . length ; cc++ )
      {
        // image hack
        var s = aRow . cols [ cc ] ;
        var i ;
        var selImg = '' ;
        var invImg = '' ;
        var selInvImg = '' ;
        var doneSelectedImage = false ;
        if ( s.indexOf ( 'IMG:' ) == 0 )
        {
          // string prop format = IMG:<selectable>,<selected>,<invalid>,<selectedInvalid>
          s = s.substring(4) ;
          i = s.indexOf ( ',' ) ;
          if ( i >= 0 )
          {
            selImg = s.substring ( i + 1 ) ;
            s = s.substring ( 0 , i ) ;
            //
            i = selImg.indexOf ( ',' ) ;
            if ( i >= 0 )
            {
              invImg = selImg.substring ( i + 1 ) ;
              selImg = selImg.substring ( 0 , i ) ;
              //
              i = invImg.indexOf ( ',' ) ;
              if ( i >= 0 )
              {
                selInvImg = invImg.substring ( i + 1 ) ;
                invImg = invImg.substring ( 0 , i ) ;
              }
            }
          }
          if ( aRow . isSelected )
          {
            if ( ( !aRow . isValid ) && ( aElement.allowInvalid ) )
            {
              if ( selInvImg.length > 0 )
              {
                s = selInvImg ;
                doneSelectedImage = true ;
              }
            }
            else
            {
              if ( selImg.length > 0 )
              {
                s = selImg ;
                doneSelectedImage = true ;
              }
            }
          }
          else if ( ! aRow . isValid )
          {
            if ( invImg.length > 0 )
            {
              s = invImg ;
              doneSelectedImage = true ;
            }
          }
          s = '<img src="' + s + '" onclick="javascript:SetGridVal(\'' + aID + '\',' + aRow.rowID + ');">' ;
        }

        /* ----
          newCell = newRow.insertCell(cc) ;
          newCell . innerHTML = s ;
          newCell . id = aID + '_' + aRow . rowID ;
        ---- */

        var cn = null ;
        if ( ( aRow . cols . length == 1 ) && ( doneSelectedImage ) )
          ;
        else if ( aRow . isSelected )
        {
          if ( ( !aRow . isValid ) && ( aElement.allowInvalid ) )
            cn = 'gridSelectInvalid' ;
          else
            cn = 'gridSelect' ;
        }
        else if ( ! aRow . isValid )
          cn = 'invalidOption' ;

        /* ----
          newCell . className = cn ;
        ---- */
        // ----
          r += '<td id="' + aID + '_' + aRow . rowID + '" class="' + cn + '">' + s + '</td>' ;
        // ----
      }
      // ----
        r += '</tr>' ;
        tc += r ;
      // ----
    }
  }
  // ----
    tc += '</tbody></table>' ;
    tableSpan . innerHTML = tc ;
  // ----
}

//------------------------------------------------------------------------------

// id|x|y|w|h|aName|aBGcolor|show title|prop arr|item arr|required|defSort|defSOrtDir|aStyle arr|tabID|sortInvalid|showInvalid|allowInvalid|cssName|helpText
function CreateGrid ( aType , aArray , aStart , isCreate )
{
  var aDesignArr = aArray ;
  var aID = "CTRL" + aArray[aStart++] ;
  var x = aArray[aStart++] ;
  var y = aArray[aStart++] ;
  var w = aArray[aStart++] ;
  var h = aArray[aStart++] ;
  var aName = aArray[aStart++] ;
  var aBGcolor = aArray[aStart++] ;
  var hasTitle = aArray[aStart++] ;
  var props = aArray[aStart++] ;
  var items = aArray[aStart++] ;    // [ [ ID { , value } ] { , [ ID { , value } ] } ]
  var isRequired = aArray[aStart++] ;
  var defSort = aArray[aStart++] ;
  var defSortDir = aArray[aStart++] ; // 1=ascending 2=descending
  var aStyle = aArray[aStart++] ;
  var aTabID = aArray[aStart++] ;
  var sortInvalid = aArray[aStart++] ;
  var showInvalid = aArray[aStart++] ;
  var allowInvalid = aArray[aStart++] ;
  var cssName = aArray[aStart++] ;
  var helpText = aArray[aStart++] ;

  var fStyle = '' ;
  var border = false ;
  if ( aBGcolor == 'NONE' || appSettings . controlStyling == CSS )
    aBGcolor = null ;
  
  if ( appSettings . controlStyling == WYSIWYG )
    cssName = '' ;  
  if ( cssName == '' )
  {
    if ( appSettings . controlStyling != CSS )
    {
      border = true ;
      fStyle = ParamsToStyle ( aStyle , 0 ) ; 
      if ( aBGcolor.length > 0 )
        fStyle += ';background-color:#' + aBGcolor ;
    }
    else
    aBGcolor = null ;
    
    cssName = dialog_grid ;
  }

  var sortCol = -1 ;
  var sortDir = 1 ;

  var tableRows = new Array () ;
  var i ;
  for ( c = 0 ; c < items . length ; c++ )
  {
    i = items[c] ;
    rowID = i[0] ;    // get the rowID
    i.shift() ;       // remove the first item
    AddRow( rowID, c, false, true, i, tableRows ) ;
  }

  var ccc ;

  if ( isCreate )
  {
    if ( defSort != '' )
    {
      ccc = 0 ;
      for ( c = 0 ; c < props.length ; c+=2 )
      {
        if ( props[ccc] == defSort )
        {
          sortCol = ccc ;
          sortDir = defSortDir ;
        }
        ccc++ ;
      }
    }

    var s = '<table id="grd' + aID + '" width="100%" border="0" cellspacing="1" cellpadding="0" style="' + fStyle + ';">' ;
    var c ;
    if ( hasTitle )
    {
      s += '<tr>' ;
      var sortImage ;
      ccc = 0 ;
      for ( c = 0 ; c < props.length ; c+=2 )
      {
        if ( props[ccc] == defSort )
          sortImage = GetSortImage ( defSortDir ) ;
        else
          sortImage = GetSortImage ( 0 ) ;
        sortImage = '<span id="sortimg_' + aID + '_' + ccc + '">' + sortImage + '</span>' ;
        s += '<td class="gridColHead" onclick="javascript:ReSortValues(\'' + aID + '\',' + ccc + ');">' + props[c] + '&nbsp;&nbsp;' + sortImage + '</td>' ;
        ccc++ ;
      }
      s += '</tr>' ;
    }
    s += '</table>' ;

    var aDiv = CreateElement ( x , y , w , h , s , aBGcolor , null , 'auto' , FindParent ( aTabID ) ) ; 
    if ( aDiv.childNodes.length == 1 )
      aDiv.childNodes[0].id = aID ; // hack the span id to be the object ID
    
    if ( border )
    {
      aDiv.style.border = '1px' ;
      aDiv.style.borderStyle = 'solid' ;
    }

    i = AddDlgControl ( aType , aID , aName , isRequired , aTabID , aDiv , helpText ) ;
    i . sortCol = sortCol ;
    i . sortDir = sortDir ;
  }
  else
  {
    el = FindDlgControlByID ( aID ) ;
    if ( el != null )
      el . isCurrent = false ;
    i = el ;
  }

  i . values = aDesignArr ;
  i . sortInvalid = sortInvalid ;
  i . showInvalid = showInvalid ;
  i . allowInvalid = allowInvalid ;
  i . hasTitle = hasTitle ;
  i . items = tableRows ;

  if ( ! building )
    GridPop ( i ) ;
}

// id|x|y|w|h|aParentID|activePageIdx|page arr|cssName
function CreateTabSheet( aType , aArray , aStart )
{
  var ctrlID = aArray [ aStart++ ] ;
  var aID = "CTRL" + ctrlID ;
  var x = aArray [ aStart++ ] ;
  var y = aArray [ aStart++ ] ;
  var w = aArray [ aStart++ ] ; // was + 4 Stephen 11/6/2009 ;
  var h = aArray [ aStart++ ] ; // was + 4 Stephen 11/6/2009 ;
  var aParentID = aArray[aStart++] ;
  var activePage = aArray [ aStart++ ] ;
  var items = aArray [ aStart++ ] ;
  var cssName = aArray [ aStart++ ] ;

  var aDiv = CreateElement ( x , y , w , h , '' , null , null , null , FindParent ( aParentID ) ) ;
  var dID = aDiv.id ;

  var tID = GetNextElementID() ;
  var tHeight = 20 ;
  var s = '<table id="' + tID + '" border="0" cellspacing="0" cellPadding="2"><tr>' +
          '<td class="tabIndent"><img src="images/tiny.gif" width="1" height="1"></td>' ;
  var c ;
  for ( c = 0 ; c < items . length ; c++ )
    s += '<td NOWRAP id="HEAD_' + dID + '_' + items[c][0] + '" class="tabDeselected" onclick="javascript:ShowPage(\'' + dID + '\',' + items[c][0] + ');">' +
         items[c][1] +
         '<img id="TABIDENT'+items[c][0]+'" style="visibility:hidden" src="images/desc.gif" width="8" height="8">' +
         '</td>' ;
  s += '<td class="tabVoid" width="100%"><img src="images/tiny.gif" width="1" height="1"></td>' +
       '</tr>' +
       '</table>' ;

  var tDiv = CreateElement ( 0 , 0 , w , tHeight , s , null , null , null , aDiv ) ;
  var el = document.getElementById(tID) ;
  if ( el != null )
  {
    tHeight = el.clientHeight ;
    tDiv.style.height = tHeight+'px' ;
  }

  for ( c = 0 ; c < items . length ; c++ )
  {
  	var i = CreateElement ( 0 , tHeight , w - 6 , h - tHeight - 4 , '' , null , null , 'auto' , aDiv , 'tabPage' ) ;
    i.id = 'TAB' + items[c][0] ;
    AddTabPage ( items[c][0] , ctrlID , items[c][5] ) ;
  }

  if ( ( activePage >= 1 ) && ( activePage <= items.length ) )
    ShowPage ( dID , items[activePage-1][0] ) ;
}

// id|x|y|w|h|aName|aValue|required|hotspotType|tabID|cssName
function CreateHotspot( aType , aArray , aStart )
{
  var aID = "CTRL" + aArray [ aStart++ ] ;
  var x = aArray [ aStart++ ] ;
  var y = aArray [ aStart++ ] ;
  var w = aArray [ aStart++ ] + 4;
  var h = aArray [ aStart++ ] + 4;
  var aName = aArray[aStart++] ;
  var aValue = aArray[aStart++] ;
  var isRequired = aArray[aStart++] ;
  var hotspotType = aArray[aStart++] ;
  var aTabID = aArray[aStart++] ;
  var cssName = aArray[aStart++] ;

  if ( appSettings . controlStyling == WYSIWYG )
    cssName = '' ;  

  if ( cssName == '' )
    cssName = dialog_hotspot ;

//  var s = '<div id="' + aID + '" style="position:absolute; height:' + ( h - 6 ) + 'px; width:' + ( w - 6 )+
//          'px; cursor:pointer; z-index:254;" class="' + cssName + '" onclick="javascript:if(isEnabled(\'' + aID + 
//          '\')){HotspotClick(\'' + aID + '\');ValChange(\'' + aID + '\');}" /></div>' ;
  var s = '<div id="' + aID + '" style="position:absolute; height:' + ( h - 4 ) + 'px; width:' + ( w - 4 )+  // was (h - 6) (w - 6) Stephen 11/6/2009
          'px; cursor:pointer; z-index:254;" class="' + cssName + '" /><img id="' + aID + 
          '_img" src="images/tiny.gif" width=100% height=100% onclick="javascript:if(isEnabled(\'' + aID + 
          '\')){HotspotClick(\'' + aID + '\');ValChange(\'' + aID + '\');}" /></div>' ;

  var aDiv = CreateElement ( x , y , w , h , s , null , null , null , FindParent ( aTabID ) ) ;
  var item = AddDlgControl ( aType , aID , aName , isRequired , aTabID , aDiv ) ;
  item.values = aValue ; // store the value associated with the hotspot in this values field
  item.valids = hotspotType ; // hold the hotspot type in the unused valids field
}

// <nItems>{,<itemID>,<itemText>}
function SearchItem( id, text )
{
  this . id = id ;
  this . text = text ;
  this . score = 100 ;
}

function CalcHeight( searchObj )
{
  var sz = 0 ;
  for ( var c = 0 ; c < searchObj.values.length ; c++ )
    if ( searchObj.values[c].score > 0 )
      sz++ ;
  return( sz * 20 ) ;  // assuming 20 is about the right height for a row
}

function BuildSearchOptions ( searchObj )
{
  var searchControl = document.getElementById(searchObj.id);
  while ( searchControl.rows.length > 0 )
    searchControl.deleteRow(0);
  var c , s , ss, cl , rw ;
  var sss = '' ;
  var searchStr = searchObj.txtColor ;
  var boldVal = searchStr.toLowerCase() ;
  for ( c = 0 ; c < searchObj.values.length ; c++ )
    if ( ( searchObj.values[c].score > 0 ) || ( ! searchObj . doneTrigram ) )
    {
      ss = searchObj.values[c].text ;
      if ( searchObj . doneTrigram )
      {
        ss = ss.toLowerCase();
        ss = ss.replace ( boldVal , "<B>"+searchStr+"</B>" );
      }

      if ( sourceSvr != "XR_AJAX.DLL" )
        sss = searchObj . id . slice( 4 ) + ',';

      s = '<a href="javascript:Perform( \'sol,' + sss + searchObj.values[c].id + '\');" class="srch">' + ss + '</a>' ;
      if ( searchObj . doneTrigram )
        s += '  (' + searchObj.values[c].score + '%)' ;
      rw = searchControl.insertRow(searchControl.rows.length) ;
      cl = rw.insertCell(rw.cells.length) ;
      cl.innerHTML = s ;
    } ;
  searchControl . style . height = CalcHeight( searchObj ) ;
}

function ResetSearch ( ctrlID )
{
  var searchObj = FindDlgControlByID ( ctrlID )
  searchObj . doneTrigram = false ;
  BuildSearchOptions ( searchObj ) ;
}

// Perform a trigram search on the search items
function SSearch ( searchStr )
{
  var c ;
  var searchObj ;
  for ( c = 0 ; c < elements . length ; c++ )
    if ( elements[c].type == 'ssrch' )
      searchObj = elements[c] ;

  if ( searchStr . length > 0 )
  {
    searchObj . doneTrigram = true ;
    searchObj . txtColor = searchStr ;
    CalcSearchTriGrams ( searchObj, searchStr ) ;
    BuildSearchOptions ( searchObj ) ;
  }
  else
    ResetSearch ( searchObj.id ) ;
}

// Key has been pressed in the super search text box
function SSearchTextPress ( event )
{
  var key = event.keyCode ;
  if ( key == 0 )
    key = event.which;
  if ( key == 13 )
    SSearch(document.getElementById('sstext').value);
  else
  {
    /*
    var searchObj = FindDlgControlByID ( 'CTRL_1' )
    if ( searchObj.doneTrigram )
    {
      BuildSearchOptions( searchObj, true ) ;
      searchObj.doneTrigram = false ;
    }
    */
  }
}

function CreateSuperSearch( aType, aArray, aStart )
{
  var aID = "CTRL" + aArray[ aStart++ ] ;
  var x = 10 ;
  var y = 10 ;
  var w = 375 ;
  var h = 400 ;
  var aParentID = 0 ;
  var nItems = aArray[ aStart++ ] ;
  var s = aArray[ aStart++ ] ;
  var sitems = new Array () ;
  var item ;
  var items = s.split( ',' ) ;
  var ss = ''

  if ( items . length > 1 )
    for( c = 0; c < items . length; c+=2 )
      sitems = ArrayPush ( sitems , new SearchItem( items[c], items[c+1] ) ) ;
  ss += '<span style="width:100%; font-family: arial; font-weight: bold;">Search&nbsp;' +
         '<input type="text" id="sstext" size="27" onkeypress="javascript:SSearchTextPress(event)" /> ' +
         '<input type="button" value="Find" onclick="javascript:SSearch(document.getElementById(\'sstext\').value);" />' +
         '<input type="button" value="Reset" onclick="javascript:ResetSearch(\'' + aID + '\');" />' +
         '</span><br /><br />' ;

  h = ( items.length / 2 ) * 20 ;  // based on CalcHeight function

  ss += '<div style="overflow: auto;"><table id="' + aID + '" class="tablelikelist" style="width:' + w + 'px;height:' + h + 'px;" >' +
        '</table></div>' ;

  ss += '<input id="btn_' + aID + '" type="button" value="Continue" onclick="javascript:Perform(\'ok\')" style="position:relative; left:140px; top:10px;" />' ;
  var aDiv = CreateElement( x, y, w, h + 200 , ss, null , null , null , null , FindParent ( aParentID ) ) ;
  var item = AddDlgControl ( aType , aID , '' , false , 0 , aDiv ) ;
  item . values = sitems ;
  item . doneTrigram = false ;
  BuildSearchOptions ( item ) ;
}
// id|x|y|w|h|aURL|tabID|cssName|showScrollBars
function CreateIFrame ( aType , aArray , aStart , isCreate )
{
  var aID = "CTRL" + aArray[aStart++] ;
  var x = aArray[aStart++] ;
  var y = aArray[aStart++] ;
  var w = aArray[aStart++] ;
  var h = aArray[aStart++] ;
  var aURL = aArray[aStart++] ;
  var aTabID = aArray[aStart++] ;
  var cssName = aArray[aStart++] ;
  var showScrolls = aArray[aStart++] ;

  var sb = '' ;
  if ( ! showScrolls )
    sb = ' scrolling="no"' ;

  if ( cssName == '' )
    cssName = dialog_iframe ;

  var i = CreateElement ( x , y , w , h , '<iframe src="" id="' + aID + '" name="' + aID + '"' + sb + ' frameborder="0" style="width:' + (w-2) + 'px;height:' + (h-2) + 'px" class="' + cssName + '">&nbsp;</iframe>' , null , null , null , FindParent ( aTabID ) ) ;
  i = AddDlgControl ( aType , aID , "" , false , aTabID , i ) ;
}

// id|x|y|w|h|attName|xAxisName|tabID|minY|maxY|minYthresh|maxYthresh|showXaxis|cssName|endValue|bgColor
function CreateGraph ( aType , aArray , aStart , isCreate )
{
  var aID = "CTRL" + aArray[aStart++] ;
  var x = aArray[aStart++] ;
  var y = aArray[aStart++] ;
  var w = aArray[aStart++] ;
  var h = aArray[aStart++] ;
  var attName = aArray[aStart++] ;
  var xAxisName = aArray[aStart++] ;
  var aTabID = aArray[aStart++] ;
  var minY = aArray[aStart++] ;
  var maxY = aArray[aStart++] ;
  var minYthresh = aArray[aStart++] ;
  var maxYthresh = aArray[aStart++] ;
  var showXaxis = aArray[aStart++] ;
  var cssName = aArray[aStart++] ;
  var endValue = aArray[aStart++] ;
  var bgColor = aArray[aStart++] ;

  if ( cssName == '' )
    cssName = dialog_graph ;

  var d = CreateElement ( x , y , w , h , '' , null , null , null , FindParent ( aTabID ) ) ;
  i = AddDlgControl ( aType , aID , attName , false , aTabID , d ) ;

  // Add extra properties (wrapped into an object) to the control (i) object
  i.graph_data = { "xaxis" : xAxisName ,
                   "miny" : minY ,
                   "maxy" : maxY ,
                   "minythresh" : minYthresh ,
                   "maxythresh" : maxYthresh ,
                   "showxaxis" : showXaxis ,
                   "endvalue" : endValue ,
                   "bgcolor" : bgColor } ;

  // Add the graph now!!!
  swfobject.embedSWF('graph/open-flash-chart.swf', d.id, w, h, '9.0.0', 'expressInstall.swf', {"get-data":"chart_data", "id":aID});
}

// Callback from flash graph to get the data!
function chart_data(id)
{
  var el = FindDlgControlByID(id) ;
  // use the el.graph_data object to set bkgnd color etc...
  var data = '{ "elements": [ { "type": "bar", "values": [  ] } ] , "title": { "text": "" } }' ;
  return ( data );
}

// <ctrl type>{|<control param>}
function MakeItm ( aArray )
{
  var aStart = 0 ;
  var aType = aArray[aStart++] ;
  switch ( aType )
  {
    case ( "num" ) : CreateNumeric ( aType , aArray , aStart ) ; break ;
    case ( "dte" ) : CreateDate ( aType , aArray , aStart ) ; break ;
    case ( "txt" ) : CreateText ( aType , aArray , aStart ) ; break ;
    case ( "lbl" ) : CreateLabel ( aType , aArray , aStart ) ; break ;
    case ( "img" ) : CreateImage ( aType , aArray , aStart ) ; break ;
    case ( "btn" ) : CreateButton ( aType , aArray , aStart ) ; break ;
    case ( "lb" ) : CreateLB ( aType , aArray , aStart , true ) ; break ;
    case ( "cb" ) : CreateCB ( aType , aArray , aStart , true ) ; break ;
    case ( "chb" ) : CreateCheckBox ( aType , aArray , aStart ) ; break ;
    case ( "rd" ) : CreateRadioButton ( aType , aArray , aStart ) ; break ;
    case ( "alb" ) : CreateArrayLB ( aType , aArray , aStart ) ; break ;
    case ( "acb" ) : CreateArrayCB ( aType , aArray , aStart ) ; break ;
    case ( "grd" ) : CreateGrid ( aType , aArray , aStart , true ) ; break ;
    case ( "tab" ) : CreateTabSheet ( aType , aArray , aStart ) ; break ;
    case ( "hot" ) : CreateHotspot ( aType , aArray , aStart ) ; break ;
    case ( "ssrch" ) : CreateSuperSearch( aType, aArray, aStart ) ; break ;
    case ( "ifrm" ) : CreateIFrame ( aType, aArray, aStart ) ; break ;
    case ( "gph" ) : CreateGraph ( aType, aArray, aStart ) ; break ;    
  }
}

// ====================================================================

function ReSortValues ( aID , sortColIdx )
{
  var el = FindDlgControlByID( aID ) ;
  if ( el != null )
  {
    var isSameCol = ( el . sortCol == sortColIdx ) ;
    el . sortCol = sortColIdx ;
    if ( isSameCol )
    {
      if ( el.sortDir == 1 )
        el . sortDir = 2 ;
      else
        el . sortDir = 1 ;
    }
    else
      el . sortDir = 1 ; // ascending
    GridPop ( el ) ;
  }
}

function Swap( items, idx1, idx2 )
{
  var tmp = items[idx1] ;
  items[idx1] = items[idx2] ;
  items[idx2] = tmp ;
  return ( items ) ;
}

function SortValues( el, rows, sortColIdx, sortMode, sortType )
{
  if ( sortMode == 0 )
  {
    return(rows) ;
  }
  else
  {
    for ( var c = 0 ; c < rows.length ; c++)
    {
      for ( var cc = c ; cc < rows.length ; cc++ )
      {
        s1 = rows[c].cols[sortColIdx] ;
        s2 = rows[cc].cols[sortColIdx] ;
        if ( sortType == 'I' )
        {
          s1 = parseInt ( s1 ) ;
          s2 = parseInt ( s2 ) ;
        }
        else if ( sortType == 'F' )
        {
          s1 = parseFloat ( s1 ) ;
          s2 = parseFloat ( s2 ) ;
        }
        if ( sortMode == 2 )
        {
          if ( s1 < s2 )
          {
            rows = Swap( rows, c, cc ) ;
          }
        } else
        {
          if ( s1 > s2 )
          {
            rows = Swap( rows, c, cc ) ;
          }
        }
      }
    }
    return(rows) ;
  }
}

function SortInvalidValues ( el , rows )
{
  for ( var c = 0 ; c < rows.length ; c++)
    for ( var cc = c ; cc < rows.length ; cc++ )
      if ( ! rows [ c ] . isValid && ( rows [ cc ] . isValid ) )
        rows = Swap ( rows , c , cc ) ;
  return ( rows ) ;
}

// ===============================================================================

function ShowPage ( divID , pageID )
{
  var el = document.getElementById(divID) ;
  if ( el != null )
  {
    var c ;
    for ( c = el.childNodes.length - 1 ; c >= 0  ; c-- )
    {
      if ( el.childNodes[c].id.substr(0,3) == 'TAB' )
      {
        var n = el.childNodes[c].id.substring(3,el.childNodes[c].id.length) ;
        var el2 = document.getElementById('HEAD_'+divID+'_'+n) ;
        if ( n == pageID.toString() )
        {
          //scan through controls and handle the visibility
          var cc ;
          for ( cc = 0 ; cc < elements.length ; cc ++ )
          {
            if ( ( elements[cc].tabID == n ) && ( elements[cc].visible ) )
            {
              var pel = document.getElementById( elements[cc].divID ) ;
              if ( pel != null )
                pel.style.visibility = 'visible' ;
            }
            if ( ( elements[cc].tabID == n ) && ( ! elements[cc].visible ) )
            {
              var pel = document.getElementById( elements[cc].divID ) ;
              if ( pel != null )
                pel.style.visibility = 'hidden' ;
            }
          }
          //

          el.childNodes[c].style.visibility = 'visible' ;
          if ( el2 != null )
            el2.className = 'tabSelected' ;

          //set the active page id
          SetActiveTabPage( pageID ) ;
        }
        else
        {
          //scan through controls and handle the visibility
          var cc ;
          for ( cc = 0 ; cc < elements.length ; cc ++ )
          {
            if ( elements[cc].tabID == n )
            {
              var pel = document.getElementById( elements[cc].divID ) ;
              if ( pel != null )
                pel.style.visibility = 'hidden' ;
            }
          }
          el.childNodes[c].style.visibility = 'hidden' ;
          if ( el2 != null )
            el2.className = 'tabDeselected' ;
        }
      }
    }
  }
  var ti = FindTabPageByID ( pageID ) ;
  Perform ( 'tabchg,' + ti.ctrlID + '|' + ti.tabIndex /* ti.id */ ) ;
}

// ===============================================================================

function PopulateNumeric ( aElement , aValue )
{
  var pel = document.getElementById(aElement.id) ;
  if ( pel != null )
    if ( pel.value != aValue )
      pel.value = aValue ;
}

function DecodeDate( aValue )  // YYYY-MM-DD -> DD-MM-YYYY
{
  if ( aValue != '' )
  {
    var mydate=aValue.split( '-' ) ;
    return( mydate[2] + '/' + mydate[1] + '/' + mydate[0] ) ;
  }
  else
    return( '' ) ;
}

function EncodeDate( aValue ) // DD-MM-YYYY -> YYYY-DD-MM
{
  if ( aValue != '' )
  {
    var mydate=aValue.split( '/' ) ;
    return( mydate[2] + '-' + mydate[1] + '-' + mydate[0] ) ;
  }
  else
    return( '' ) ;
}

function PopulateDate ( aElement , aValue )
{
  var pel = document.getElementById(aElement.id) ;
  if ( pel != null )
  {
    if ( aValue.length != 0 )
      pel.innerHTML = DecodeDate( aValue ) ;
    else
      pel.innerHTML = '**/**/****' ; // was ''
  }
}

function PopulateText ( aElement , aValue )
{
  var pel = document.getElementById(aElement.id) ;
  if ( pel != null )
    if ( pel.value != aValue )
      pel.value = aValue ;
}

function PopulateLB ( aElement , aValue )
{
  aElement.isCurrent = aValue ;
  var pel = document.getElementById(aElement.id) ;
  var c , cc ;
  if ( pel != null )
  {
    if ( pel.multiple )
    {
      var aValues = aValue.split ( ',' ) ;
      for ( c = pel.options.length - 1 ; c >= 0 ; c-- )
      {
        var found = false ;
        for ( cc = 0 ; ( cc < aValues.length ) && ( ! found ) ; )
          if ( pel.options[c].value == aValues[cc] )
            found = true ;
          else
            cc++ ;
        pel.options[c].selected = found ;
      }
    }
    else
    {
    	var scroll = pel.scrollTop ;
      for ( c = pel.options.length - 1 ; c >= 0 ; c-- )
      {
        if ( pel.options[c].value == aValue )
        {
          if ( pel.options[c].value.length == 0 )
            pel.selectedIndex = -1
          else
            pel.selectedIndex = c ;
          pel.scrollTop = scroll ;
          return ;
        }
      }
//      for ( c = 0 ; c <= pel.options.length - 1 ; c++ )
//        if ( pel.options[c].className == 'invalidSelection' )
//          pel.options[c].className = 'invalidOption' ;
      pel.selectedIndex = -1 ;
      pel.scrollTop = scroll ;
    }
  }
}

function PopulateCB ( aElement , aValue )
{
  PopulateLB ( aElement , aValue ) ;
}

function PopulateCheckBox ( aElement , aValue )
{
  var pel = document.getElementById(aElement.id) ;
  if ( pel != null )
  {
    if ( aElement.multiSel )
    {
      var aValues = aValue.split ( ',' ) ;
      var found = false ;
      for ( var cc = 0 ; ( cc < aValues.length ) && ( ! found ) ; cc++ )
        if ( pel.value == aValues[cc] )
          found = true ;
      pel.checked = found ;
    }
    else
    {
      if ( pel.value == 0 )
        pel.checked = ( aValue == '1' ) ;
      else
        pel.checked = ( aValue == pel.value ) ;
    }
  }
}

function PopulateRadioButton ( aElement , aValue )
{
  var pel = document.getElementById(aElement.id) ;
  if ( pel != null )
  {
    pel.checked = ( aValue == pel.value ) ;
    aElement.isCurrent = pel.checked ;
  }
}

function PopulateHotspotOld( aElement, aValue )
{
/*
 0 == hsInvertNo
 1 == hsBorderNo
 2 == hsInvertYes
 3 == hsBorderYes
*/
  if ( aValue != '' )
  {
    var pel = document.getElementById( aElement.id )
    if ( pel != null )
    {
      if ( aElement.valids != 0 )
        pel.style.border = ( aValue == aElement.values ) ? '1px solid blue' : '' ;
//      pel.style.border = ( aValue == aElement.values ) ? '1px solid blue' : '' ;
      aElement.isCurrent = ( aValue == aElement.values ) ;
    }
  }
}

function PopulateHotspot( aElement, aValue )
{
/*
  showborder  = aElement.valids && 1
  showoutline = aElement.valids && 2
*/
  var pel = document.getElementById( aElement.id )
  if ( pel != null )
  {
    var showborder  = ( aElement.valids & 1 ) ;
    var showoutline = ( aElement.valids & 2 ) ;
  	
  	aElement.isCurrent = ( aValue == aElement.values ) ;
    if ( aElement.isCurrent )
    {		
    	pel.style.border = (showborder) ? '4px ridge white':(showoutline)? '1px dashed white':'' ;
      
      if ( ! isIE )
      {
        pel.style.width = (showborder) ? pel.style.width - 8 : pel.style.width ; 
        pel.style.height = (showborder) ? pel.style.height - 8 : pel.style.height ;
      }
      if ( !showborder )
      {
        pel.style.filter = 'alpha(opacity=25)' ;
        pel.style.opacity = 0.25 ;
        pel.style.backgroundColor = '#000000' ;
      }
    }
    else
    {
      pel.style.border = (showoutline) ? '1px dashed blue' : '' ;
      if ( ! isIE )
      {
//        pel.style.width = aElement.aDefinition.width ;
//        pel.style.height = aElement.aDefinition.height ;  
      }
      if ( !showborder )
      {    	
        if ( ! isIE || ( pel.style.filter == 'alpha(opacity=25)' ) )
          pel.style.filter = 'alpha(opacity=100)' ;
        pel.style.opacity = 1.00 ;
        pel.style.backgroundColor = '' ;
      }
    }
/*
    aElement.isCurrent = ( aValue == aElement.aDefinition.value ) ;
    // Dynamic Enabled
    if (aElement.aObject.flatProps)
    {
      pel.style.cursor = ( aElement.aObject.flatProps.isenabled ) ? 'pointer' : 'default' ;
      aElement.enabled = aElement.aObject.flatProps.isenabled ;  
    }          
*/
  }
}

function PopulateArrayLB ( aElement , aValue )
{
  aElement.isCurrent = aValue ;
  var pel = document.getElementById(aElement.id) ;
  var c ;
  if ( pel != null )
    for ( c = pel.options.length - 1 ; c >= 0 ; c-- )
      if ( pel.options[c].value == parseInt (aValue) )
      {
        pel.selectedIndex = c ;
        return ;
      }
  pel.selectedIndex = -1 ;
}

function PopulateArrayCB ( aElement , aValue )
{
  PopulateArrayLB ( aElement , aValue ) ;
}

function PopulateGrid ( aElement , aValue )
{
  aElement.isCurrent = aValue ;
  ClearGridRowSelected ( aElement . items ) ;
  SetGridRowSelected ( aValue , aElement . items ) ;
//SA GridPop() call is required for the rebuild of preselected value!!    if ( ! building )
  GridPop ( aElement ) ;
}


function findSWF(movieName)
{
  if (navigator.appName.indexOf("Microsoft")!= -1)
  {
    return window[movieName];
  } else {
    return document[movieName];
  }
}

function PopulateGraph ( aElement , aValue )
{
  var tmp = findSWF(aElement.divID);
  var s = '' ;
  for ( var c = 0 ; c < aValue.ydata.length ; c++ )
  {
    if ( s.length > 0 )
      s += ',' ;
    s += aValue.ydata[c] ;
  }
  // also look at aValue.xdata array for the x axis labels
  var data = '{ "elements": [ { "type": "bar", "values": [' + s + '] } ] }' ;
  tmp.load( data );
}

// <attName>|<attValue>{|<attName>|<attValue>}
function PopItm ( aArray )
{
  var aStart = 0 ;

  var aName = aArray[aStart++] ;

//  var aux = aName.split( '(' ) ;
//  if ( aux.length > 1 )
//    aName = aux[0] ;

  var aValue = aArray[aStart++] ;
  var i = 0 ;
  do
  {
    var el = FindDlgControl ( aName , i ) ;
    if ( el != null )
    {
      switch ( el . type )
      {
        case ( "num" ) : PopulateNumeric ( el , aValue ) ; break ;
        case ( "dte" ) : PopulateDate ( el , aValue ) ; break ;
        case ( "txt" ) : PopulateText ( el , aValue ) ; break ;
        case ( "lb" ) : PopulateLB ( el , aValue ) ; break ;
        case ( "cb" ) : PopulateCB ( el , aValue ) ; break ;
        case ( "chb" ) : PopulateCheckBox ( el , aValue ) ; break ;
        case ( "rd" ) : PopulateRadioButton ( el , aValue ) ; break ;
        case ( "alb" ) : PopulateArrayLB ( el , aValue ) ; break ;
        case ( "acb" ) : PopulateArrayCB ( el , aValue ) ; break ;
        case ( "grd" ) : PopulateGrid ( el , aValue ) ; break ;
        case ( "hot" ) : PopulateHotspot ( el , aValue ) ; break ;
        case ( "gph" ) : PopulateGraph ( el , aValue ) ; break ;
      }
      i = el.index + 1 ;
    }
  } while ( el != null ) ;
}

function PopulateLabel ( aElement , aText )
{
  var pel = document.getElementById(aElement.id) ;
  if ( pel != null )
    if ( pel.innerHTML != aText )
      pel.innerHTML = aText ;
}

// <labelID>|<labelText>{|<labelID>|<labelText>}
function PopLbl ( aArray )
{
  var aStart = 0 ;
  var aID = "CTRL" + aArray[aStart++] ;
  var aText = aArray[aStart++] ;
  var el = FindDlgControlByID ( aID ) ;
  if ( el != null )
    if ( el . type == "lbl" )
      PopulateLabel ( el , aText ) ;
}

// Change the src URL of the iFrame
function PopulateIFrame ( frameID , aText )
{
  // TODO : 09-03-09 : Untested
  var aID = "CTRL" + frameID ;
  var el = FindDlgControlByID ( aID ) ;
  if ( el != null )
    if ( el . type == "ifrm" )
    {
      var pel = document.getElementById(el.id) ;
      if ( pel != null )
      {
        var same = true ;
        var s = pel.src.toLowerCase() ;
        var aText2 = aText.toLowerCase() ;
        if ( s.substr(0,aText2.length) == aText2 )
        {
          s = s.substr(aText2.length) ;
          if ( ( s != '' ) && ( s != '/' ) )
            same = false ;
        }
        else
          same = false ;
        if ( ! same )
          pel.src = aText ;
      }
    }
}

// Change the image path of an image
function ChgImg ( imageID , imagePath )
{
  var aID = "CTRL" + imageID ;
  var el = FindDlgControlByID ( aID ) ;
  if ( el != null )
    if ( el . type == "img" )
    {
      var pel = document.getElementById(el.divID) ;
      if ( pel != null )
      {
        // TTV 09-02-06
        // John : items array holds the isStretched/w/h properties from the create :-)
//        imagePath = imagePath.replace(/\\/g,"/");
        var s = '<img src="' + imagePath + '"' ;
        if ( ( el.items != null ) && ( el.items[0] ) )
          s += ' width="' + el.items[1] + '" height="' + el.items[2] + '"' ;
        s += '>' ;
        pel.innerHTML = s ;
      }
    }
}

// <attName>|<gridItemArr>
function FillItm ( aArray )
{
  var aStart = 0 ;
  var aName = aArray [ aStart++ ] ;
  var i = 0 ;
  do
  {
    var el = FindDlgControl ( aName , i ) ;
    if ( el != null )
    {
      if ( el . type == "grd" )
      {
        el.values[10] = aArray [ aStart ] ;
        CreateGrid ( el . type , el.values , 1 , false ) ;
      } 
      else if ( el . type == "lb" )
      {
        for ( var c = 1 ; c < aArray[aStart].length ; c++ )   // Ignore 1st element as it is blank for CB deselection
        {
          el.items[(c-1)*2] = aArray[aStart][c][0] ;
          el.items[(c-1)*2+1] = aArray[aStart][c][1] ;
        }
        el.items.length = ( aArray[aStart].length - 1 )* 2 ;
        CreateLB ( "" , el , 0 , false ) ;
      }
      else if ( el . type == "cb" )
      {
        for ( var c = 0 ; c < aArray[aStart].length ; c++ ) 
        {
          el.items[c*2] = aArray[aStart][c][0] ;
          el.items[c*2+1] = aArray[aStart][c][1] ;
        }
        el.items.length = aArray[aStart].length * 2 ;
        CreateCB ( "" , el , 0 , false ) ;
      }
      
      i = el.index + 1 ;
    }
  } while ( el != null ) ;
}

function DebugElement( el )
{
  if ( el != null )
   alert( 'index : ' + el.index + '\n' +
          'type : ' + el.type + '\n' +
          'id : ' + el.id + '\n' +
          'divID : ' + el.divID + '\n' +
          'name : ' + el.name + '\n' +
          'required : ' + el.required + '\n' +
          'tabID : ' + el.tabID + '\n' +
          'handled : ' + el.handled + '\n' +
          'isCurrent : ' + el.isCurrent + '\n' +
          'valids : ' + el.valids + '\n' +
          'values : ' + el.values + '\n' +
          'enabled : ' + el.enabled + '\n' +
          'showInvalid : ' + el.showInvalid + '\n' +
          'sortInvalid : ' + el.showInvalid + '\n' +
          'allowInvalid : ' + el.allowInvalid + '\n' ) ;
  else
    alert( 'el = null' ) ;
}

// <aID>{|<aID>}
function ShItms ( aArray , isVisible )
{
  var c ;
  for ( c = 0 ; c < aArray . length ; c++ )
  {
    var el = FindDlgControlByID ( "CTRL" + aArray [ c ] ) ;
    if ( el != null )
    {
      el.visible = isVisible ;
      var pel = document.getElementById(el.divID) ;
      if ( pel != null )
      {
        if ( ( isVisible ) )
          pel.style.visibility = "visible" ;
        else
          pel.style.visibility = "hidden" ;

        pel = document.getElementById(el.helpDiv) ;
        if ( pel != null )
        {
          if ( ( isVisible ) )
            pel.style.visibility = "visible" ;
          else
            pel.style.visibility = "hidden" ;
	      }
      }
    }
  }
  // force a refresh as ShItms is usually called after the ShowPage.  Need this to initialise the controls on the page
  var activeTab = FindActiveTabPage() ;
  if ( activeTab != -1 )
  {
    pel = document.getElementById("TAB" + activeTab ) ;
    if ( pel != null )
      if ( pel.parentNode != null )
        ShowPage( pel.parentNode.id , activeTab ) ;
  }
}

// ===============================================================================

function isEnabled( aID )
{
 var el = FindDlgControlByID ( aID ) ;
 var isit = true ;
 if ( el != null )
 {
   if ( el.type == "lb" )
   {
   } else
   // Check Radio button invalids to see if they're clickable.
   if ( el.type == "rd" )
   {
     //Check the valids before returning
     var IDs = el.id.split( "_" ) ;
     if ( ( IDs.length > 1 ) && ( el.valids != null ) )
     {
       var valueID = IDs[1] ;
       for ( var c = 1 ; c < el.valids.length ; c+=2 )
       {
         if ( valueID == el.valids[ c ] )
           return( ( el.valids[c+1] == "v" ) && ( el.enabled ) ) ;
       }
       return(el.enabled);
     }
   } else if ( el . type == "chb" )
   {
     return ( true ) ; // 21-03-06
     if ( el . valids != null )
     {
       var checkedIndex = 4 ;
       var uncheckedIndex = 2 ;
       pel = document.getElementById( aID ) ;
       if ( pel != null )
         if( pel.checked )
           return( ( el.valids[ checkedIndex ] == "v" ) && ( el.enabled ) ) ;
         else
           return( ( el.valids[ uncheckedIndex ] == "v" ) && ( el.enabled ) ) ;
     }
   }
   return ( el.enabled ) ;
 }
 return( isit ) ;
}

function EnableNumeric ( el , isEnabled )
{
  var pel = document.getElementById(el.id) ;
  if ( pel != null )
    pel.disabled = ! isEnabled ;
  el.enabled = isEnabled ;
}

function EnableDate( el, isEnabled )
{
  var pel = document.getElementById(el.id);
  if ( pel != null )
    pel.disabled = !isEnabled ;
  el.enabled = isEnabled ;
}

function EnableLB( el, isEnabled )
{
  var pel = document.getElementById(el.id) ;
  if ( pel != null )
    pel.disabled = ! isEnabled ;
  el.enabled = isEnabled ;
}

function EnableCB( el, isEnabled )
{
  var pel = document.getElementById(el.id) ;
  if ( pel != null )
    pel.disabled = ! isEnabled ;
  el.enabled = isEnabled ;
}

function EnableCheckBox( el, isEnabled )
{
  var pel = document.getElementById(el.id) ;
  if ( pel != null )
    pel.disabled = ! isEnabled ;

  var tel = document.getElementById('chb' + el.id ) ;
  if ( tel != null )
    if ( pel.disabled )
      tel . style . color = INVALID_COLOR ;
    else
    {
    	if ( el.textColor && tel . style . color )
        if ( appSettings . controlStyling != CSS )
          tel . style . color = '#' + el . textColor ;
        else
          tel . style . color = '' ; // was null -- Dont work for IE!;
    }
  el.enabled = isEnabled ;
}

function EnableRadioButton( el, isEnabled )
{
  var pel = document.getElementById(el.id) ;
  if ( pel != null )
    pel.disabled = ! isEnabled ;

  var tel = document.getElementById('rt' + el.id ) ;
  if ( tel != null )
  {
    var a = el.text.split('|');
    if ( pel.disabled )
    {
      tel . style . color = INVALID_COLOR ;
      if ( a.length > 0 )
        tel . innerHTML = a[1] ;
    }
    else
    {
    	if ( el.textColor && tel . style . color )
        if ( appSettings . controlStyling != CSS )
          tel . style . color = '#' + el . textColor ;
        else
          tel . style . color = '' ; // was null -- Dont work for IE!;

      if ( a.length > 0 )
        tel . innerHTML = a[0] ;
    }
  }

  el.enabled = isEnabled ;
}

function EnableHotspot( el, isEnabled )
{
  var pel = document.getElementById(el.id) ;
  if ( pel != null )
    pel.style.cursor = ( isEnabled ) ? 'pointer' : 'default' ;
  el.enabled = isEnabled ;
}

function EnableButton( el, isEnabled )
{
  var pel = document.getElementById(el.id);
  if ( pel != null )
    pel.disabled = !isEnabled ;
}

function EnableGrid( el, isEnabled )
{
}

// <aID>{|<aID>}
function EnItms ( aArray , isEnabled )
{
 var c ;
 for ( c = 0 ; c < aArray . length ; c++ )
 {
   var el = FindDlgControlByID ( "CTRL" + aArray [ c ] ) ;
   if ( el != null )
   {
     switch ( el . type )
     {
       case ( "num" ) : EnableNumeric ( el , isEnabled ) ; break ;
       case ( "dte" ) : EnableDate ( el , isEnabled ) ; break ;
       case ( "lb" ) : EnableLB ( el , isEnabled ) ; break ;
       case ( "cb" ) : EnableCB ( el , isEnabled ) ; break ;
       case ( "chb" ) : EnableCheckBox ( el , isEnabled ) ; break ;
       case ( "rd" ) : EnableRadioButton ( el , isEnabled ) ; break ;
       case ( "hot" ) : EnableHotspot ( el , isEnabled ) ; break ;
       case ( "btn" ) : EnableButton ( el , isEnabled ) ; break ;
       case ( "grd" ) : EnableGrid ( el , isEnabled ) ; break ;
     }
   }
 }
}

// ===============================================================================

// Modify a parameter (at the moment only obligatory) of a specified control
// <aID>|<paramID>|<value>{|<aID>|<paramID>|<value>}
function ModItms ( aArray )
{
  var c ;
  for ( c = 0 ; c < aArray . length ; c+=3 )
  {
    var el = FindDlgControlByID ( "CTRL" + aArray [ c ] ) ;
    if ( el != null )
    {
      switch ( aArray [ c + 1 ] )
      {
        case ( "ALLOWBLANK" ) :
          el . required = ( aArray [ c + 2 ] == "FALSE" ) ;
          break ;
      }
    }
  }
}

// ===============================================================================

//<report text>|<report title>|<isPopUp>|<timeout>
function MakeRep ( aArray )
{
  var aStart = 0 ;
  var aText = aArray[aStart++] ;
  var aTitle = aArray[aStart++] ;
  var isPopUp = aArray[aStart++] ;
  var timeout = aArray[aStart++] ;
  //
  SetWnd ( new Array ( 0,0,0,0,0,0, aTitle , "FFFFFF" , "" , 0 , "" ) ) ;
  if ( isPopUp == '1'  && ! aPopupReportObject )
  {
    aPopupReportObject = new PopupReportObject ( aText , aTitle ) ;

    var popupReportWindow = window.open("report.html","repWindow","width=400,height=300,toolbar=0");
    if ( popupReportWindow )
    {
      popupReportWindow.focus();
      Perform ( 'ok' ) ;
      return ;
    }
    else
      popupReportWindow = null ;
  }
  aText += '<br><input type="button" value="OK" id="' + GetNextElementID() + '" onclick="javascript:Perform(\'ok\');" />' ;
  var oDiv = document.createElement('div');
  oDiv.id = GetNextElementID() ;
  var spn = document.createElement('span');
  spn.innerHTML = aText ;
  oDiv.appendChild(spn);
  AttachElement ( oDiv ) ;
  if ( timeout )
    self.setTimeout( 'Perform("ok")' , ( timeout * 1000 ) ) ;
}

// ===============================================================================

function ShowDebug ( aText , doOK , aTitle , aButtonTxt , aHTML )
{
//  Handle aHTML = false here
  ShowStatus ( "" ) ;
  if (aTitle && aTitle.length > 0 )
    ShowModal ( aText , aTitle , aButtonTxt , DebugOK ) ;
  else
  {
    alert ( aText ) ;
    if ( doOK )
    Perform ( 'ok' ) ;
  }
}


function DebugOK() {
  Perform('ok');
}

// ===============================================================================

function EndOfInference ( data )
{
  ShowStatus ( "" ) ;
  SetWnd ( new Array ( 0,0,0,0,0,0, "End of Inference" , "FFFFFF" , "", 0, "") ) ;
  ClearElements ( ) ;
  SetAudit ( null ) ;
  SetBackBookmarkID ( 0 ) ; 
  BuildEndHTML(data);
}

function MaxUserHtml( data )
{
  var el = document.getElementById("eoi");
  if ( el != null )
    el.innerHTML = '<h2>Server has hit the licenced maximum users limit(' + data + ')</h2>' +
                   '<p><strong>Please wait a while and try again</strong></p>' ;
}

function redir( aUrl )
{
  window.location = aUrl ;
}

// ===============================================================================
function SortVal( el, pel, aArray ) // sorts valids and invalids
{
  if ( el . sortInvalid )
  {
    // before reseting, pull off the selected values ;
    var sels = new Array() ;
    for ( c = 0 ; c < pel.options.length/*SA - 1*/ ; c++ )
      if ( pel . options[ c ] . selected )
        sels = ArrayPush ( sels , pel.options[c].value ) ;

    //reset
    if ( el . type == "cb" )
      pel.options.length = 1 ;  // allow for the blank line to deselect
    else
      pel.options.length = 0 ;

    //Store the selected index
    var found = false ; // for some reason the option.selected can be true but selectedIndex can still be -1 :-/ need to filter here
    var oldSel = pel.selectedIndex ;

    //re-add only the valids first
    var c, cc ;
    var ast ;
    for ( c = 0 ; c < el . items . length - 1 ; c+=2 )
    {
      ast = 1 ;
      for ( ast = 1 ; ast <= aArray.length - 1 ; ast+=2 )
      {
        if ( el.items[c] == aArray[ast] )
        {
          if ( aArray[ast+1] == 'v' )
          {
            var aOpt = document . createElement( 'option' ) ;
            aOpt . value = el . items[ c ] ;
            aOpt . text = el . items[ c + 1 ] ;
            aOpt . ondblclick = 'function(){if (CanDblClick( \'' + el.id + '\', \'' + el.items[c] +  '\'))LBdblclk();}' ;
            aOpt . className = undefined ;
            pel.options.add( aOpt ) ;
            for ( cc = 0 ; cc <= sels.length - 1 ; cc++ )
              if ( aOpt . value == sels[ cc ] )
              {
                found = true ;
                pel.options[pel.options.length-1].selected = true ;  // can't add to the aOpt as IE will clear it ;-(
              }
          }
        }
      }
    }
    // now the invalids
    for ( c = 0 ; c < el . items . length - 1 ; c+=2 )
    {
      ast = 1 ;
      for ( ast = 1 ; ast <= aArray.length - 1 ; ast+=2 )
        if ( el.items[c] == aArray[ast] )
          if ( aArray[ast+1] == 'i' )
          {
            var aOpt = document . createElement( 'option' ) ;
            aOpt . value = el . items[ c ] ;
            aOpt . text = el . items[ c + 1 ] ;
            aOpt . ondblclick = 'function(){if (CanDblClick( \'' + el.id + '\', \'' + el.items[c] +  '\'))LBdblclk();}' ;
            aOpt . className = 'invalidOption' ;
            pel.options.add( aOpt ) ;
            for ( cc = 0 ; cc < sels.length ; cc++ )
              if ( aOpt . value == sels[ cc ] )
              {
                found = true ;
                pel.options[pel.options.length-1].selected = true ;  // can't add to the aOpt as IE will clear it ;-(
                pel.options[pel.options.length-1].text += ' [Invalid]';
                pel.options[pel.options.length-1].className = 'invalidSelection';
              }
          }
    }
    
    //re-store the selection only if -1
    if ( ( oldSel == -1 ) && ( !found ) )
      pel.selectedIndex = -1 ;
  }
}

// <attName>{,<attValue>,v|i}
function SetVal ( aArray )
{
  var aStart = 0 ;
  var aName = aArray[aStart++] ;
  var rStart = aStart ;
  var ctrlCnt = 0 ;
  do
  {
    var el = FindDlgControl ( aName , ctrlCnt ) ;
    if ( el != null )
    {
      el.valids = aArray ;
      aStart = rStart ;
      if ( ( el.type == "lb" ) || ( el.type == "cb" ) )
      {
        var pel = document.getElementById(el.id) ;
        if ( pel != null )
        {
          while ( aStart <= aArray.length )
          {
            var c ;
            for ( c = pel.options.length - 1 ; c >= 0 ; c-- )
            {
              if ( el.tabID == 0 )
                pel.options[c].style.visibility = 'visible' ;
              if ( pel.options[c].value == aArray[aStart] )
              {
                if ( aArray[aStart+1] == 'i' )
                {
                  if ( pel.options[c].selected )
                    pel.options[c].className = 'invalidSelection';
                  else
                    pel.options[c].className = 'invalidOption' ;
                } else
                  pel.options[c].className = undefined ;
              }
            }
            aStart += 2 ;
          }

          if ( el . sortInvalid ) // sort the lb elements based on class (need to respect natural order)
            SortVal( el, pel, aArray ) ;

          if ( ! el . showInvalid )
          {
            // before reseting, pull off the selected values ;
            var sels = new Array() ;
            for ( c = 0 ; c < pel.options.length - 1 ; c++ )
              if ( pel . options[ c ] . selected )
                sels = ArrayPush ( sels , pel.options[c].value ) ;

            //reset
            pel.options.length = 0 ;
            //re-add only the valids
            var cc ;
            var ast ;
            for ( c = 0 ; c < el . items . length - 1 ; c+=2 )
            {
              ast = 1 ;
              for ( ast = 1 ; ast <= aArray.length - 1 ; ast+=2 )
              {
                if ( el.items[c] == aArray[ast] )
                {
                  if ( aArray[ast+1] == 'v' )
                  {
                    var aOpt = document . createElement( 'option' ) ;
                    aOpt . value = el . items[ c ] ;
                    aOpt . text = el . items[ c + 1 ] ;
                    for ( cc = 0 ; cc <= sels.length - 1 ; cc++ )
                      if ( aOpt . value == sels[ cc ] )
                        aOpt . selected = true ;
                    aOpt . ondblclick = 'function(){if (CanDblClick( \'' + el.id + '\', \'' + el.items[c] +  '\'))LBdblclk();}' ;
                    aOpt . className = undefined ;
                    pel.options.add( aOpt ) ;
                  }
                }
              }
            }
          }
        }
      }
      else if ( el.type == "rd" )
      {
        var pel = document.getElementById(el.id) ;
        if ( pel != null )
        {
          while ( aStart <= aArray.length )
          {
            if ( pel.value == aArray[aStart] )
            {
              pel.disabled = ( aArray[aStart+1] == 'i' ) ;
              if ( ( pel.disabled ) && ( ! el.showInvalid ) )
              {
                if ( el.tabID == 0 )
                  MakeInvisible( pel ) ;
              }
              // now get the text element and set that to grey if need be
              var tel = document.getElementById('rt' + el.id ) ;
              if ( tel != null )
              {
                var a = el.text.split('|');
                if ( pel . disabled )
                {
                  tel . style . color = INVALID_COLOR ;
                  if ( ( !el.showInvalid ) && ( el.tabID == 0 ) )
                    MakeInvisible( tel ) ;
                  else if ( a.length > 0 )
                    tel . innerHTML = a[0] ;
                }
                else
                {
     	            if ( el.textColor && tel . style . color )
                    if ( appSettings . controlStyling != CSS )
                      tel . style . color = '#' + el . textColor ;
                    else
                      tel . style . color = '' ; // was null -- Dont work for IE!;
/*                	                	
                  if ( tel . style . color )
                  {
                    if ( appSettings . applyControlStyling )
                      tel . style . color = '#' + el . textColor ;
                    else
                      tel . style . color = null ;
                  }
*/
                  if ( a.length > 0 )
                    tel . innerHTML = a[0] ;
                }
              }
              aStart = aArray.length+1 ;
            }
            else
              aStart += 2 ;
          }
        }
      }
      else if ( el.type == "hot" )
      {
        var pel = document.getElementById(el.id) ;
        if ( pel != null )
        {
          while ( aStart <= aArray.length )
          {
            if ( el.values == aArray[aStart] )
            {
              pel.disabled = ( aArray[aStart+1] == 'i' ) ;
              aStart = aArray.length+1 ;
            }
            else
              aStart += 2 ;
          }
        }
      }
      else if ( el.type == "chb" )
      {
        // Moved from here to the DoTabs routine
      }
      else if ( el.type == "grd" )
      {
        ClearGridRowInvalids ( el . items ) ;
        while ( aStart <= aArray.length )
        {
          if ( aArray[aStart+1] == 'i' )
            SetGridRowInvalid ( aArray[aStart] , el . items ) ;
          aStart += 2 ;
        }
        GridPop ( el ) ;
      }
      else if ( el.type == "num" )
      {
        el.isValid = ( aArray[aStart] != 'i' ) ;
        aStart++ ;
        var pel = document.getElementById(el.id) ;
        if ( pel != null )
        {
          if ( ! el.isValid )
            pel.style.color = '#FF0000' ;
          else if ( el.txtColor.length > 0 )
            pel.style.color = el.txtColor ;
          else if ( appSettings . controlStyling != CSS )
            pel.style.color = '#000000' ;
        }
      }
      ctrlCnt = el.index + 1 ;
    }
  } while ( el != null ) ;
}

function AllVal ( attName )
{
 var c , pel ;
 var ctrlCnt = 0 ;
 do
 {
   var el = FindDlgControl ( attName , ctrlCnt ) ;
   if ( el != null )
   {
     el.valids = null ;
     if ( ( el.type == "lb" ) || ( el.type == "cb" ) )
     {
       pel = document.getElementById(el.id) ;
       var selectedValue = '' ;
       if ( pel != null )
       {
         var sv = '' ;
         for ( c = pel.options.length - 1 ; c >= 0 ; c-- )
         {
           if ( pel.options[c].selected )
           {
             selectedValue = pel.options[c].value ;
             sv = pel.options[c].value ;
           }
           pel.options[c].className = undefined ;
         }

         if ( el . sortInvalid )
         {
           pel . options . length = 0 ;
           var idx = -1 ;
           var optObj ;
           // re-jig back to original sort order
           var i = 0;
           var arr = new Array (el.items.length / 2);
           for ( c = 0 ; c < el.items.length ; c+=2 )
           {
             optObj = document.createElement('option');
             optObj.text = el . items[c+1] ;
             optObj.value = el . items[c] ;
             if ( ( sv.length != 0) && ( sv == el.items[c] ) )
             {
               idx = Math.floor( ( c / 2 ) ) ;
               optObj.selected = true ;
             }
             optObj.ondblclick = 'function(){if (CanDblClick( \'' + el.id + '\', \'' + el.items[c] +  '\'))LBdblclk();}' ;
             optObj.className = 'validOption' ; // undefined ;
             pel.options[i++] = optObj;
           }
           pel.selectedIndex = idx ;
         }
       }
     }
     else if ( el.type == "rd" )
     {
       pel = document.getElementById(el.id) ;
       if ( pel != null )
       {
         pel.disabled = false ;
         if ( ( ! el . showInvalid ) && ( el . tabID == 0 ) )
           MakeVisible( pel ) ;
       }
       var tel = document.getElementById('rt' + el.id ) ;
       if ( tel != null )
       {
         if ( pel . disabled )
           tel . style . color = INVALID_COLOR ;
         else
         {
    	     if ( el.textColor && tel . style . color )
             if ( appSettings . controlStyling != CSS )
               tel . style . color = '#' + el . textColor ;
             else             
               tel . style . color = '' ; // was null -- Dont work for IE!;

/*
           if ( tel . style . color )
           {
             if ( appSettings . applyControlStyling )
               tel . style . color = '#' + el . textColor ;
             else
               tel . style . color = null ;
           }
*/
           if ( ( !el . showInvalid ) && ( el . tabID == 0 ) )
             MakeVisible( tel ) ;
         }
       }
     }
     else if ( el.type == "chb" )
     {
       pel = document.getElementById(el.id) ;
       if ( pel != null )
         pel.disabled = false ;

       var tel = document.getElementById('chb' + el.id ) ;
       if ( tel != null )
       {
         if ( pel . disabled )
           tel . style . color = INVALID_COLOR ;
         else
         {
    	     if ( el.textColor && tel . style . color )
             if ( appSettings . controlStyling != CSS )
               tel . style . color = '#' + el . textColor ;
             else
               tel . style . color = '' ; // was null -- Dont work for IE!;
/*
           if ( tel . style . color )
           {
             if ( appSettings . applyControlStyling )
               tel . style . color = '#' + el . textColor ;
             else
               tel . style . color = null ;
           }
*/
         }
       }
     }
     else if ( el.type == "grd" )
     {
       ClearGridRowInvalids ( el.items ) ;
       GridPop ( el ) ;
     }
     ctrlCnt = el.index + 1 ;
   }
 } while ( el != null ) ;
}

// ===============================================================================

// aData = [ { aName , aValue } ]
function SetAudit ( aTitle , aData )
{
//  var aBody = document.getElementsByTagName("body").item(0) ;
  var aBody = FindParent(0) ; 
  var el = document.getElementById("XRaudit");
  if ( aData == null )
  {
    if ( el != null )
      aBody.removeChild(el);
  }
  else if ( aData . length > 0 )
  {
    var w = aBody.clientWidth ;
    var h = aBody.clientHeight ;
    if ( el == null )
    {
      var el = document.createElement('div');
      el.id = 'XRaudit' ;
      el.style.position = 'absolute' ;
      el.style.left = '0px' ;
      el.style.top = ( h - 100 ) + 'px' ;
      el.style.width = w + 'px' ;
      el.style.height = '100px' ;
      el.style.overflow = 'scroll' ;
      AttachElement ( el ) ;
    }
    var s = '<table width="100%" cellspacing="0" cellpadding="0">' +
            '<tr><td class="audittitle">' + aTitle + '</td><td class="audittitle" style="border-left:1px solid white">Click below to change answer</td></tr>' ;
    var n = '' ;
    var v = '' ;
    var f = 0 ;
    var cl = '' ;
    var c = aData . length - 3 ;
    var rn = aData . length / 3 ;
    while ( c >= 0 )
    {
      n = aData [ c ] ;
      v = aData [ c+1 ] ;
      f = aData [ c+2 ] ;
      c -= 3 ;
      if ( f == 0 )
        cl = 'auditrow' ;
      else
        cl = 'auditrowtss' ;
      s += '<tr><td class="' + cl + '">' + n + '</td><td class="auditrow" style="border-left:1px solid black"><a href="javascript:Reanswer(' + rn + ')">' + v + '</a></td></tr>' ;
      rn-- ;
    }
    s += '</table>' ;
    el.innerHTML = s ;
  }
}

function Reanswer ( rowNumber )
{
  SetAudit ( null ) ;
  Perform ( 'prevans,' + rowNumber ) ;
}

// ===============================================================================

function ShowStatus ( aStatus )
{
  var aBody = document.getElementsByTagName("body").item(0) ;
  //var aBody = document.getElementById("xrui") ;
  var w = aBody.clientWidth ;
  var el = document.getElementById("XRstatus");
  if ( el != null )
    aBody.removeChild(el);
  if ( aStatus.length > 0 )
  {
    var el = document.createElement('div');
    el.id = 'XRstatus' ;
    el.style.position = 'absolute' ;
    el.style.left = '0px' ;
    el.style.top = '0px' ;
    el.style.width = w + 'px' ;
    el.style.height = '20px' ;
    el.innerHTML = '<div align="right"><table><tr><td bgcolor="#000080"><font color="#FFFFFF">' + aStatus + '</font></td></tr></table></div>' ;
    AttachElement ( el ) ;
  }
}

// ===============================================================================

var replay = "" ;

function SetReplay ( aReplayStr )
{
  replay = aReplayStr ;
}

function DisableClick()
{
  if ( event.button == 1 ) alert('Please wait until "Loading" has finished before clicking.') ;
}

// ===============================================================================

var performing = false ;
var performStack = new Array () ;

function loadXMLDoc(url,params)
{
//  ShowStatus ( "Loading" ) ;
  if (window.XMLHttpRequest)
  {
    req = new XMLHttpRequest();
    req.onreadystatechange = processReqChange;
    req.open("POST", url, true);
    req.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" ) ; //Servlet need
    //alert(params);
    req.send(params);
  }
  else if (window.ActiveXObject)
  {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    if (req)
    {
      document.onmousedown=DisableClick;
      req.onreadystatechange = processReqChange;
      req.open("POST", url, true);
      req.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" ) ; //Servlet need
      req.send(params);
    }
  }
}

function processReqChange()
{
  if ( req . readyState == 4 )
  {
    if ( req . status == 200 )
    {
//      eval ( req.responseText ) ;
      var s = req.responseText.split('\u0000');  // This is required for none IE .net Web Apps
      eval (s[0]) ;
      ShowStatus ( "" ) ;
      if ( performStack . length > 0 )
        Perform2 ( performStack.shift() ) ;
      else
        performing = false ;
      if ( window.ActiveXObject ) document.onmousedown=null;
      return ( true ) ;
    }
    else
    {
      if ( window.ActiveXObject) document.onmousedown=null;
      ShowStatus ( "Error" ) ;
      performStack . length = 0 ;
      performing = false ;
      return ( false ) ;
    }
  }
}

// ===============================================================================

function Perform2 ( action , extraValues )
{
  var p = "" ;
  if ( action.length > 0 )
    p = "action=" + action + "&replay=" + replay + "&cur=" + page_id + "&" + RetrieveElements(lastClickedID) ;
  if ( extraValues != null )
    if ( extraValues.length > 0 )
      p += "&" + extraValues ;
  if ( ( sourceSvr.length == 0 ) || ( !Boolean( sourceSvr ) ) )
    sourceSvr = "XR_AJAX.DLL";
  lastClickedID = null ;
  loadXMLDoc ( sourceSvr , p ) ;
}

function Perform ( action , extraValues , srcSvr )
{
  if ( ( srcSvr != null ) && ( srcSvr.length != 0 ) )
    sourceSvr = srcSvr ;
  if ( performing )
    performStack = ArrayPush ( performStack , action ) ;
  else
  {
    performing = true ;
    Perform2 ( action , extraValues ) ;
  }
}

function PopulateWithPrevious ( aID )
{
  var el = FindDlgControlByID ( aID ) ;
  if ( el != null )
  {
    switch ( el . type )
    {
      case ( "lb" ) : PopulateLB ( el , el.isCurrent ) ; break ;
      case ( "cb" ) : PopulateCB ( el , el.isCurrent ) ; break ;
      case ( "grd" ) : PopulateGrid ( el , el.isCurrent ) ; break ;
    }
  }
}

var doneChange = false ;

function ValChange ( aID )
{
  var el = FindDlgControlByID ( aID ) ;
  if ( el != null )
  {
    var aName = el.name ;
    if ( el.type == 'rd' )
    {
      if ( el.isCurrent )
      {
        var pel = document.getElementById(aID);
        if ( pel != null )
          pel.checked = false ;
      }
    }
    doneChange = true ;
    if ( ClickedInvalid ( aID ) )
    {
      if ( !el . allowInvalid )
      {
        PopulateWithPrevious ( aID ) ;
        return ;
      } else
      {
        if ( el.valids != null )
          SetVal ( el.valids ) ;
      }
    }
    lastClickedID = aID ;
    ShowStatus ( "" ) ;

    if ( ( ( el.type == 'txt' ) || ( el.type == 'num' ) ) && el.xrCTRLid )
      aName += ',' + el.xrCTRLid ;

    Perform ( 'refresh,' + aName ) ;
  }
}

var lastLBscroll = 0 ;

function LBmouseDown ( aID )
{
  var el = document.getElementById(aID);
  if ( el != null )
    lastLBscroll = el.scrollTop ;
}

function LBmouseUp ( aID )
{
  var el = document.getElementById(aID);
  if ( el != null )
  {
    doneChange = false ;
    window.setTimeout('LBmouseUp2("' + aID + '")',50);
  }
}

function LBmouseUp2 ( aID )
{
  if ( ! doneChange )
  {
    var el = document.getElementById(aID);
    if ( el != null )
    {
      var e = FindDlgControlByID( aID ) ;
      if ( e != null )
        if ( ( e.type == 'lb' ) && (el.scrollTop != lastLBscroll) )
          return ;
    	
      el.selectedIndex = -1 ;
      var e = FindDlgControlByID ( aID ) ;
      if ( e != null )
        ValChange ( aID ) ;
    }
  }
}

function CBclick ( aID )
{
  var el = FindDlgControlByID ( aID ) ;
  if ( el != null )
  {
    var pel = document.getElementById(aID);
    if ( pel != null )
    {
      pel.checked = ! pel.checked ;
      ValChange ( aID ) ;
    }
  }
}

function HotspotClick ( aID )
{
  var el = FindDlgControlByID ( aID ) ;
  if ( el != null )
  {
    for ( var c = 0 ; c < elements.length ; c ++ )
    {
      if ( elements[c].id == aID )
      {
        var pel = document.getElementById( aID )
        if ( pel != null )
        {
//          pel.style.filter = ( elements[c].valids == 2 ) ? 'invert' : '' ;
          pel.style.border = ( elements[c].valids != 3 ) ? '1px solid blue' : '' ;
//          pel.style.border = '1px solid blue' ;
        }

        el.isCurrent = true ;
      } else if ( elements[c].name == el.name )
      {
        var pel = document.getElementById( elements[c].id )
        if ( pel != null )
          pel.style.border = '' ;
        elements[c].isCurrent = false ;
      }
    }
  }
}

function CanDblClick( elementID, aValue )
{
  var el = FindDlgControlByID( elementID ) ;
  if ( el != null )
  {
    if ( el.type == 'lb' )
    {
      var pel = document.getElementById( elementID ) ;
      if ( pel != null )
      {
        for ( var c = 0 ; c <= pel.options.length - 1; c++ )
        {
          if ( ( pel.options[c].value == aValue ) && ( pel.options[c].className == 'invalidOption' ) )
          {
            return( false ) ;
          }
        }
      }
    }
  }
  return( true ) ;
}

function LBdblclk ( )
{
  doneChange = true ;
  BtnClk ( "ok" ) ;
}

function SetGridVal ( elementID , valueID )
{
  var el = FindDlgControlByID ( elementID ) ;
  if ( el != null )
  {
    if ( ! IsGridRowValid ( el . items , valueID ) && ! el . allowInvalid )
      return ;
    ClearGridRowSelected ( el . items ) ;
    if ( el.isCurrent == valueID )
      el.isCurrent = false ; // de-select click
    else
    {
      SetGridRowSelected ( valueID , el . items ) ;
      el.isCurrent = valueID ;
    }
    GridPop ( el ) ;
  }
  ValChange ( elementID ) ;
}

function SetValueAndOK ( elementID , valID )
{
  var el = document.getElementById( elementID ) ;
  if ( el != null )
    el.value = valID ;
  BtnClk ( "ok" ) ;
}

function BtnClk ( aParam )
{
  var c = aParam.indexOf ( "|" ) ;
  var aValue = "" ;
  if ( c != -1 )
  {
    aValue = aParam.substring ( c + 1 , aParam.length ) ;
    aParam = aParam.substring ( 0 , c ) ;
  }
  switch ( aParam )
  {
    case ( "mac" ) :
      Perform ( 'macro,' + aValue ) ;
//      Perform ( 'canexit' ) ;
      break ;
    case ( "ok" ) :
      Perform ( 'canexit' ) ;
      break ;
    case ( "continue" ) :
      if ( aValue == "nochk" )
        Perform ( 'ok' ) ;
      else if ( ValidateElements() )
        Perform ( 'ok' ) ;
      break ;
    case ( "exit" ) :
      if ( aValue.length == 0 )
        Perform( 'exit' ) ;
      else if ( confirm( aValue ) )
        Perform ( 'exit' ) ;
      break ;
    case ( "back" ) :
      Perform ( 'back' ) ;
      break ;
    case ( "url" ) :
      c = aValue.indexOf ( "|" ) ;
      if ( c != -1 )
        aValue = aValue.substring ( 0 , c ) ;
      var popupWindow = window.open(aValue,"urlButton","resizable=1,width=400,height=300");
      if ( popupWindow )
        popupWindow.focus();
      break;
  }
}

// Back Protection =========================================================================

var lastHash = 0 ;
var timerID = 0 ;

function GetBookmarkID ( )
{
  var url = '' ;
  if ( isIE )
    url = document.getElementById('backFrame').contentWindow.document.body.innerHTML ;
  else
    url = window . location . href ;
  if ( url . indexOf ( '#' ) > -1 )
  {
    var url_elements = url . split ( '#' ) ;
    return url_elements [ url_elements . length - 1 ] ;
  }
  else
  {
    return 0 ;
  }
}

// Called by the server to set the bookmark ID of the page (incremented at the end of each dialog/report passed)
function SetBackBookmarkID ( aID )
{
  var url = '' ;
  if ( isIE )
    url = document.getElementById('backFrame').contentWindow.document.body.innerHTML ;
  else
    url = window . location . href ;
  if ( url . indexOf ( '#' ) > -1 )
  {
    var url_elements = url . split ( '#' ) ;
    url = url_elements [ 0 ] ;
  }
  url += '#' + aID ;
  if ( timerID != 0 )
  {
    clearTimeout ( timerID ) ;
    timerID = 0 ;
  }
  if ( isIE )
  {
    var doc = document.getElementById('backFrame').contentWindow.document;
    if ( url != doc.body.innerHTML )
    {
      doc.open();
      doc.write(url);
      doc.close();
    }
    /*
    url = window . location . href ;
    if ( url . indexOf ( '#' ) > -1 )
    {
      var url_elements = url . split ( '#' ) ;
      url = url_elements [ 0 ] ;
    }
    url += '#' + aID ;
    window . location . href = url ;
    */
  }
  else
    window . location . href = url ;
  lastHash = aID ;
  timerID = setTimeout ( "CheckForPageChange()" , 200 ) ;
}

function CheckForPageChange ( )
{
  if ( GetBookmarkID ( ) == lastHash )
    timerID = setTimeout ( "CheckForPageChange()" , 200 ) ;
  else
    Perform ( 'HASHBACK,' + GetBookmarkID ( ) ) ;
}


// ---- New Calls for Scraping (working progress) ---
// ===============================================================================

var attValueRet = '';

// Called in response to a "GetValue" action
function GotValue ( attName , attValue )
{
//  alert ('YES YSE YES GotValue ---- attName - ' + attName + '   is set to - ' + attValue);
  attValueRet = attValue;
}

// Jump to a specified question number
function SetAdHocPage ( adhocPage )
{
  var lframe = window.backFrame ;
  if (lframe != null)
  {
    var loc = window.backFrame.document.location ;
//    lframe.document.eval (adhocPage) ;
    alert ('location - ' + 'http://localhost/ajaxpreview/' + adhocPage);
    lframe.document.location = 'http://localhost/ajaxpreview/' + adhocPage ;
  }
  else
    alert ('lframe == null')
}

function GetAnswerValueText ( aName )
{
  GetServerValueText ( aName );
  alert ('Dummy alert () is needed In GetAnswerValueText ()');
  return (attValueRet);
}

function wait(msecs)
{
  var start = new Date().getTime();
  var cur = start
  while(cur - start < msecs)
    cur = new Date().getTime();
}

function GetServerValueText ( aName )
{
  attValueRet = '';
  Perform ( 'getValue,' + aName );
//  for ( c = 0 ; c < 5 ; c++ )
//  {
//    if (attValueRet.length > 0)
//    {
//      alert ('In GetAnswerValueText () aName - ' + aName + '   attValueRet = ' + attValueRet);
//      break;
//    }
//    alert ('In GetAnswerValueText () aName - ' + aName + '   calling WAIT ()');
//    wait(500);    
//  }
}

function AddReplayAnswerByName ( aName , vName , visible )
{
	if ( visible )
    alert ('noaction , ' + aName + '     to    ' + vName);
  Perform ( 'noaction,' + aName + '=' + vName )
}

// Ad Hoc (user) page in the flow. Finish page
function NextPage ( )
{
  alert ('Ad Hoc (user) page in the flow' );
}

// ---- New Calls for Scarping (working progress) ---
// ===============================================================================


// ============= UI Callbacks from popup windows ==============

function PopulatePopupReport ( )
{
  if ( aPopupReportObject )
  {
    var o = aPopupReportObject ;
    aPopupReportObject = null ;
    return ( { "text":o.text,
               "description":o.description } ) ;
  }
  return ( null ) ;
}

function ApplyCSSdefaults ( styles )
{
  if ( styles )
    for ( var i in styles )
      if ( styles[i] != '' )
        eval ( i + "='" + styles[i] + "'" ) ;
}

function PadUp ( n , w )
{
  var s = new String ( n ) ;
  while ( s.length < w )
    s = '0' + s ;
  return ( s ) ;
}

function strftime(format,dateSelected,monthSelected,yearSelected) {
  var result = format.toLowerCase();
  result = result.replace( /yyyy/, yearSelected);
  result = result.replace( /mm/, PadUp ( monthSelected + 1 , 2 ) );
  result = result.replace( /m/, PadUp ( monthSelected + 1 , 2 ) );
  result = result.replace( /dd/, PadUp ( dateSelected , 2 ) );
  result = result.replace( /d/, PadUp ( dateSelected , 2 ) );
  return result;
};

function AppendCSS ( aFile )
{
  if ( aFile != '' )
  {
    var headID = document.getElementsByTagName("head")[0];         
    var cssNode = document.createElement('link');
    cssNode.type = 'text/css';
    cssNode.rel = 'stylesheet';
    cssNode.href = "assets/" + aFile;
    cssNode.media = 'screen';
    headID.appendChild(cssNode);
  }	
}

-->

