/*
  widgetParent.js
  Description: Methods for widgets that are called by Online and Desktop 
*/

/*
 *
 * Widget Prep Functions
 *
 */
 
function tsGetWindowSize()
{
  var res = new Array();
  if( typeof( window.innerWidth ) == 'number' ) 
  {
    //Non-IE
    res[0] = window.innerWidth;
    res[1] = window.innerHeight;
  } 
  else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
  {
    //IE 6+ in 'standards compliant mode'
    res[0] = document.documentElement.clientWidth;
    res[1] = document.documentElement.clientHeight;
  } 
  else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
  {
    //IE 4 compatible
    res[0] = document.body.clientWidth;
    res[1] = document.body.clientHeight;
  }
  return res;
}

function tsGetScrollXY() 
{
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) 
  {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  }
  else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) 
  {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } 
  else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) 
  {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function tsGetCoordinates( x, y, h, w )
{
  var winInfo = tsGetWindowSize();
  var scrInfo = tsGetScrollXY();

  // shift position according to scrolling
  x += scrInfo[0];
  y += scrInfo[1];

  if( x + w + 10 > ( winInfo[0] + scrInfo[0] ) )
    x = ( winInfo[0] + scrInfo[0] ) - w - 10;

  if( y + h + 10 > ( winInfo[1] + scrInfo[1] ) )
    y = ( winInfo[1] + scrInfo[1] ) - h - 10;

  return [ x, y ];
}

// calendar opener
var tsCalTargets = new Array();
var tsCalForm = "";
var tsCalCallback = null;
var cur_sTokenName = null;

function tsOpenCalendar()
{
  // open this where the mouse is
  var x = 0;
  var y = 0;

  if( arguments.length < 6 )
  {
    return;
  } 
  
  evt = arguments[0];
  tsCalTargets = arguments[1].split( "," );
  tsCalForm = arguments[2];
  inDate = arguments[3];
  inFormat = arguments[4];
  timeShow = arguments[5];

  //Start of DESKTOP specific
  if( arguments.length == 7 )
    tsCalCallback = arguments[6];
  //End of DESKTOP specific
  //Start of ONLINE specific
  else if (arguments.length == 8)
  {
    sTokenName = arguments[6];
    sToken = arguments[7];
  }
  //End of ONLINE specific

  var h = 350;
  var w = 350;

  if( timeShow != "" && timeShow == "false" )
  {
    h = 300;
    w = 300;
  }

  if( evt )
  {
    x = evt.clientX;
    y = evt.clientY;
  }
  else
  {
    x = parseInt( document.body.clientWidth / 2 - w / 2 );
    y = parseInt( document.body.clientHeight / 2 - h / 2);
  }

  winInfo = tsGetCoordinates( x, y, h, w );
  x = winInfo[0];
  y = winInfo[1];

  var url = CommonPath + "/Widgets/calendar.asp?tsFocusForm=" + encodeURIComponent( tsCalForm )+ "&" + encodeURIComponent(sTokenName) + "=" + encodeURIComponent(sToken);

  if( inDate != "" )
  {
    url += "&tsWidgetDate=" + encodeURIComponent( inDate );
  }

  if( inFormat != "" )
  {
    url += "&tsDateFormat=" + encodeURIComponent( inFormat );
  }

  if( timeShow != "" )
  {
    url += "&tsShowTime=" + encodeURIComponent( timeShow );
  }

  openWidget( "calendar", url, { h: h, w: w, x: x, y: y } );
}

function rightTrim(sString) 
{
	while (sString.substring(sString.length-1, sString.length) == ' ')
	{
	sString = sString.substring(0,sString.length-1);
	}
	return sString;
}

function tsApplyCalendar( formattedDate, plainDate )
{
  for( var x = 0; x < tsCalTargets.length; x++ )
  {
   formattedDate = rightTrim(formattedDate);
    var sDash = formattedDate.charAt(formattedDate.length-1);
    if (sDash == "-")
		formattedDate = formattedDate.substring(0,formattedDate.length-1);  
    document.getElementById( tsCalTargets[x] ).value = formattedDate;
    var srcImg = document.getElementById( "cal::" + tsCalTargets[x] );
    if( srcImg )
      srcImg.setAttribute( "datestr", plainDate );
  }

  if( tsCalCallback != null )
    tsCalCallback();
}

// for duration primitives
var tsDurTargets = new Array();
var tsDurForm = "";
function tsOpenDuration()
{
  // open this where the mouse is
  var x = 0;
  var y = 0;

  evt = arguments[0];
  tsDurTargets = arguments[1].split( "," );
  tsDurForm = arguments[2];
  //Start of ONLINE only 
  if (arguments.length > 3)
  {
    sTokenName = arguments[3];
    sToken = arguments[4];
  }
  //End of ONLINE
  var h = 290;
  var w = 175;

  if( evt )
  {
    x = evt.clientX;
    y = evt.clientY;
  }
  else
  {
    x = parseInt( document.body.clientWidth / 2 - w / 2 );
    y = parseInt( document.body.clientHeight / 2 - h / 2);
  }

  winInfo = tsGetCoordinates( x, y, h, w );
  x = winInfo[0];
  y = winInfo[1];

  var url = CommonPath + "/Widgets/duration.asp?tsFocusForm=" + encodeURIComponent( tsDurForm ) + "&" + encodeURIComponent(sTokenName) + "=" + encodeURIComponent(sToken);

  if( tsDurTargets.length == 1 )
  {
    if( document.getElementById( tsDurTargets[0] ) && document.getElementById( tsDurTargets[0] ).value != "" )
    {
      url += "&tsCurrentValue=" + encodeURIComponent( document.getElementById( tsDurTargets[0] ).value );
    }
  }

  openWidget( "duration", url, { h: h, w: w, x: x, y: y } );
}

function tsApplyDuration( duration )
{
  for( var x = 0; x < tsDurTargets.length; x++ )
  {
    document.getElementById( tsDurTargets[x] ).value = duration;
  }
}

var tsPerfSelectCallback = null;
function tsOpenPerformanceSelector( series, callback )
{
  if( callback )
    tsPerfSelectCallback = callback;
  else
    tsPerfSelectCallback = null;

  var url = CommonPath + "/Widgets/perfselect.asp?" + encodeURIComponent(sTokenName) + "=" + encodeURIComponent(sToken);
  for( x = 0; x < series.length; x++ )
  {
      url += "&BOset::WOSperformances::Query::Clause::1::value=" + encodeURIComponent(series[x]);
  }

  openWidget( "performanceselect", url, { h: 300, w: 300 } );
}

function tsApplyPerformanceSelector( perfs )
{
  closeWidget( "performanceselect" );
  if( tsPerfSelectCallback )
    tsPerfSelectCallback( perfs );
}

function tsOpenColour( evt, ID )
{

  var h = 300;
  var w = 420;

  if( evt )
  {
    x = evt.clientX;
    y = evt.clientY;
  }
  else
  {
    x = parseInt( document.body.clientWidth / 2 - w / 2 );
    y = parseInt( document.body.clientHeight / 2 - h / 2);
  }

  winInfo = tsGetCoordinates( x, y, h, w );
  x = winInfo[0];
  y = winInfo[1];

  var url = CommonPath + "/Widgets/colour.asp?FocusID=" + encodeURIComponent( ID );
  url += "&FocusColour=" + encodeURIComponent( document.getElementById( ID ).value ) + "&" + encodeURIComponent(sTokenName) + "=" + encodeURIComponent(sToken);

  openWidget( "colour", url, { h: h, w: w, x: x, y: y } );

}

function tsScreenSummary( objectHandle, objectType, objectMethod )
{
  scrHt = screen.availHeight;
  scrWd = screen.availWidth;
  if( scrHt > 768 ) {
    scrHt = 768;
  }
  if( scrWd > 1024 ) {
    scrWd = 1024;
  }
  var x = 0;
  var y = 0;
  if( x + scrWd > screen.availWidth ) {
    x = x - ( screen.availWidth - x + scrWd );
  }
  if( y + scrHt > screen.availHeight ) {
    y = y - ( screen.availHeight - y + scrHt );
  }

  var url = CommonPath + "/Widgets/summary_result.asp?objectHandle=" + encodeURIComponent(objectHandle);
  url += "&objectType=" + encodeURIComponent(objectType) + "&objectMethod=" + encodeURIComponent(objectMethod) + "&" + encodeURIComponent(sTokenName) + "=" + encodeURIComponent(sToken);

  winArgs = "height=" + scrHt + ",width=" + scrWd + ",scrollbars=1,resizable=1,toolbar=yes,top=" + y + ",left=" + x + ",status=1,dependent=1";
  winName = window.open( url, "Reports", winArgs );
  if( window.opener )
  {
    window.opener.openWindows[ "Reports" ] = winName;
  }
  winName.focus();
}

function tsDownloadSummary( objectHandle, objectType, objectMethod, template, templates, downloadFileName )
{
  // close the widget
  closeWidget( "summarywidget" );
  // process the download into an hidden iframe
  var url = CommonPath + "/Widgets/summary_result.asp?objectHandle=" + encodeURIComponent(objectHandle);
  url += "&objectType=" + encodeURIComponent(objectType) + "&objectMethod=" + encodeURIComponent(objectMethod) + "&target=download";
  url += "&templates=" + encodeURIComponent(templates) + "&template=" + encodeURIComponent(template) + "&downloadFileName=" + encodeURIComponent(downloadFileName) + "&" + encodeURIComponent(sTokenName) + "=" + encodeURIComponent(sToken);
  
  if (document.getElementById( "BOcustBio::Request::from_customer::0" ) != undefined)
      url += "&from_customer=" + encodeURIComponent(document.getElementById( "BOcustBio::Request::from_customer::0" ).value);
  widget = document.createElement( "iframe" );
  widget.style.height = "0px";
  widget.style.width = "0px";
  document.body.appendChild( widget );
  widget.src = url;

}

function tsDownloadExtract( objectHandle, objectType, objectMethod, template, templates, downloadFileName, tracking, generateCorrespondence )
{
  // close the widget
  closeWidget( "extractwidget" );
  // process the download into an hidden iframe
  var url = CommonPath + "/Widgets/extract_result.asp?objectHandle=" + encodeURIComponent(objectHandle);
  url += "&objectType=" + encodeURIComponent(objectType) + "&objectMethod=" + encodeURIComponent(objectMethod) + "&target=download";
  url += "&templates=" + encodeURIComponent(templates) + "&template=" + encodeURIComponent(template) + "&downloadFileName=" + encodeURIComponent(downloadFileName) + "&" + encodeURIComponent(sTokenName) + "=" + encodeURIComponent(sToken);
  url += "&tracking=" + encodeURIComponent(tracking) + "&generateCorrespondence=" + encodeURIComponent(generateCorrespondence);
  widget = document.createElement( "iframe" );
  widget.style.height = "0px";
  widget.style.width = "0px";
  document.body.appendChild( widget );
  widget.src = url;

}

function tsOpenExtract( titleKey, objectHandle, objectType, objectMethod, template, showTemplate, templates, downloadFileName, srcForm, menuName, sTokenName, sToken )
{
  var sendForm = srcForm ? document.forms[ srcForm ] : document.forms[0];
  var btnName = menuName ? menuName : "tsExtract";

  // hold onto original target and action
  var srcTarget = sendForm.target;
  var srcAction = sendForm.action;
  var oHdl = document.createElement( "INPUT" );
  var oMth = document.createElement( "INPUT" );
  var oTyp = document.createElement( "INPUT" );
  var oKey = document.createElement( "INPUT" );
  var oTmp = document.createElement( "INPUT" );
  var oFname = document.createElement("INPUT");
  var oTemplate = document.createElement("INPUT");
  var oFormat = document.createElement( "INPUT" );
  var oGenCorrespondence = document.createElement( "INPUT" );
  var oTracking = document.createElement( "INPUT" );
  
  oHdl.type = "hidden";
  oHdl.name = "objectHandle";
  oHdl.id = "objectHandle";
  oHdl.value = objectHandle;
  
  oMth.type = "hidden";
  oMth.name = "objectMethod";
  oMth.id = "objectMethod";
  oMth.value = objectMethod;

  oTyp.type = "hidden";
  oTyp.name = "objectType";
  oTyp.id = "objectType";
  oTyp.value = objectType;

  oKey.type = "hidden";
  oKey.name = "titleKey";
  oKey.id = "titleKey";
  oKey.value = titleKey;
  
  oGenCorrespondence.type = "hidden";
  oGenCorrespondence.name = "generateCorrespondence";
  oGenCorrespondence.id = "generateCorrespondence";

  oTracking.type = "hidden";
  oTracking.name = "tracking";
  oTracking.id = "tracking";

  
  var tracking;
  var generateCorrespondence;
  
  if (document.getElementById('BOparam::' + objectHandle + '::extract::tracking') != undefined)
    oTracking.value = document.getElementById('BOparam::' + objectHandle + '::extract::tracking').value;  

  if (document.getElementById('BOparam::' + objectHandle + '::extract::generateCorrespondence') != undefined)
    oGenCorrespondence.value = document.getElementById('BOparam::' + objectHandle + '::extract::generateCorrespondence').value;    
  
  if( !showTemplate )
  {
      oTmp.type = "hidden";
      oTmp.name = "noTemplate";
      oTmp.id = "noTemplate";
      oTmp.value = 1;

      oTemplate.type = "hidden";
      oTemplate.name = "template";
      oTemplate.id = "template";
      oTemplate.value = template;
   }
  
  oFname.type = "hidden";
  oFname.name = "downloadFileName";
  oFname.value = downloadFileName;
  oFname.id = "downloadFileName";
  
  oFormat.type = "hidden";
  oFormat.name = "templates";
  oFormat.value = templates;  
  oFormat.id = "templates";

  sendForm.appendChild( oHdl );
  sendForm.appendChild( oMth );
  sendForm.appendChild( oTyp );
  sendForm.appendChild( oKey );
  sendForm.appendChild( oFname );
  sendForm.appendChild( oFormat );  
  sendForm.appendChild( oTracking);  
  sendForm.appendChild( oGenCorrespondence );  

  if( !showTemplate )
  {
    sendForm.appendChild( oTmp );
    sendForm.appendChild( oTemplate );
  }
  if( (titleKey == "" && templates == "") || (titleKey == "Screen" && templates != "") )
  {
    // close the widget
    closeWidget( "extractwidget" );
  
    var url = CommonPath + "/Widgets/extract_result.asp";
    scrHt = screen.availHeight;
    scrWd = screen.availWidth;
    if( scrHt > 768 ) {
      scrHt = 768;
    }
    if( scrWd > 1024 ) {
      scrWd = 1024;
    }
    var x = 0;
    var y = 0;
    if( x + scrWd > screen.availWidth ) {
      x = x - ( screen.availWidth - x + scrWd );
    }
    if( y + scrHt > screen.availHeight ) {
      y = y - ( screen.availHeight - y + scrHt );
    }

    winArgs = "height=" + scrHt + ",width=" + scrWd + ",scrollbars=1,resizable=1,toolbar=yes,top=" + y + ",left=" + x + ",status=1,dependent=1";
    winName = window.open( "", "Extracts", winArgs );
    if( window.opener )
      window.opener.openWindows[ "Extracts" ] = winName;

    sendForm.target = "Extracts";
    sendForm.action = url;
    sendForm.submit();

    winName.focus();

  }
  else
  {
    tsShowButtonMenu( document.getElementById( btnName + "Menu"  ), btnName );
    var url = CommonPath + "/Widgets/extract.asp";
    
    sendForm.target = "extractwidget";
    sendForm.action = url;
    sendForm.submit();
    // display our widget
    openWidgets[ "extractwidget" ] = true;
    var widget = document.getElementById( "Widget::extractwidget" );
    var shadow = document.getElementById( "Shadow::extractwidget" );
    shadow.style.zIndex = widgetIdx++;
    widget.style.zIndex = widgetIdx++;

    var left = document.body.clientWidth / 2 - 150 + document.body.scrollLeft;
    var top = document.body.clientHeight / 2 - 150 + document.body.scrollTop;
  
    widget.style.left = left;
    widget.style.top = top;
  
    shadow.style.left = left + 10;
    shadow.style.top = top + 10;

    shadow.style.display = "inline";
    widget.style.display = "block";
  }

  try
  {
    var elem=document.getElementById('objectHandle');
    elem.parentNode.removeChild(elem)  
    elem=document.getElementById('objectMethod');
    elem.parentNode.removeChild(elem)  
    elem=document.getElementById('objectType');
    elem.parentNode.removeChild(elem)  
    elem=document.getElementById('titleKey');
    elem.parentNode.removeChild(elem)  
    elem=document.getElementById('downloadFileName');
    elem.parentNode.removeChild(elem)  
    elem=document.getElementById('templates');
    elem.parentNode.removeChild(elem)  
    elem=document.getElementById('generateCorrespondence');
    elem.parentNode.removeChild(elem)  
    elem=document.getElementById('tracking');
    elem.parentNode.removeChild(elem)  
    
    if( !showTemplate )
    {
        elem=document.getElementById('noTemplate');
        elem.parentNode.removeChild(elem)  
        elem=document.getElementById('template');
        elem.parentNode.removeChild(elem)  
    }
  }
  catch( e )
  {
    // nada
  }

  sendForm.target = srcTarget;
  sendForm.action = srcAction;
  
}

function tsOpenSummary( titleKey, objectHandle, objectType, objectMethod, template, showTemplate, templates, downloadFileName, srcForm, menuName, sTokenName, sToken, fromWidget )
{
  var sendForm = srcForm ? document.forms[ srcForm ] : document.forms[0];
  var btnName = menuName ? menuName : "tsSummary";
  if (fromWidget == undefined) fromWidget = false;
  // hold onto original target and action
  var srcTarget = sendForm.target;
  var srcAction = sendForm.action;

  if (document.getElementById("objectHandle") && !fromWidget)
  {
    document.getElementById("objectHandle").value = objectHandle;
  }
  else
  {
    var oHdl = document.createElement( "input" );
    oHdl.type = "hidden";
    oHdl.name = "objectHandle";
    oHdl.id = "objectHandle";
    oHdl.value = objectHandle;
    sendForm.appendChild(oHdl);
  }
  
  if (document.getElementById("objectSrcForm") && !fromWidget)
  {
    document.getElementById("objectSrcForm").value = srcForm;
  }
  else
  {
    var oSrcForm = document.createElement( "input" );
    oSrcForm.type = "hidden";
    oSrcForm.name = "objectSrcForm";
    oSrcForm.id = "objectSrcForm";
    oSrcForm.value = srcForm;
    sendForm.appendChild( oSrcForm );
  }

  if (document.getElementById("objectMethod") && !fromWidget)
  {
    document.getElementById("objectMethod").value = objectMethod;
  }
  else
  {
    var oMth = document.createElement( "input" );
    oMth.type = "hidden";
    oMth.name = "objectMethod";
    oMth.id = "objectMethod";
    oMth.value = objectMethod;
    sendForm.appendChild( oMth );
  }

  if (document.getElementById("objectType") && !fromWidget)
  {
    document.getElementById("objectType").value = objectType;
  }
  else
  {
    var oTyp = document.createElement( "input" );
    oTyp.type = "hidden";
    oTyp.name = "objectType";
    oTyp.id = "objectType";
    oTyp.value = objectType;
    sendForm.appendChild( oTyp );
  }

  if (document.getElementById("titleKey") && !fromWidget)
  {
    document.getElementById("titleKey").value = titleKey;
  }
  else
  {
    var oKey = document.createElement( "input" );
    oKey.type = "hidden";
    oKey.name = "titleKey";
    oKey.id = "titleKey";
    oKey.value = titleKey;
    sendForm.appendChild( oKey );
  }

  if (document.getElementById("downloadFileName") && !fromWidget)
  {
    document.getElementById("downloadFileName").value = downloadFileName;
  }
  else
  {
    var oFname = document.createElement( "input" );
    oFname.type = "hidden";
    oFname.name = "downloadFileName";
    oFname.id = "downloadFileName";
    oFname.value = downloadFileName;
    sendForm.appendChild( oFname );
  }

  if (document.getElementById("templates") && !fromWidget)
  {
    document.getElementById("templates").value = templates;
  }
  else
  {
    var oFormat = document.createElement( "input" );
    oFormat.type = "hidden";
    oFormat.name = "templates";
    oFormat.id = "templates";
    oFormat.value = templates;
    sendForm.appendChild( oFormat );  
  }

  if( !showTemplate )
  {
    if (document.getElementById("noTemplate") && !fromWidget)
    {
      document.getElementById("noTemplate").value = 1;
    }
    else
    {
      var oTmp = document.createElement( "input" );
      oTmp.type = "hidden";
      oTmp.name = "noTemplate";
      oTmp.id = "noTemplate";
      oTmp.value = 1;
      sendForm.appendChild( oTmp );
    }

    if (document.getElementById("template") && !fromWidget)
    {
      document.getElementById("template").value = template;
    }
    else
    {
      var oTemplate = document.createElement( "input" );
      oTemplate.type = "hidden";
      oTemplate.name = "template";
      oTemplate.id = "template";
      oTemplate.value = template;
      sendForm.appendChild( oTemplate );      
    }
  }

  if( (titleKey == "" && templates == "") || (titleKey == "Screen" && templates != "") )
  {
    // close the widget
    closeWidget( "summarywidget" );
  
    var url;
    if (document.getElementById( "BOcustBio::Request::from_customer::0" ) != undefined)
      url = CommonPath + "/Widgets/summary_result.asp?from_customer=" + encodeURIComponent(document.getElementById( "BOcustBio::Request::from_customer::0" ).value);
    else
	  	url = CommonPath + "/Widgets/summary_result.asp";
	  
    scrHt = screen.availHeight;
    scrWd = screen.availWidth;
    if( scrHt > 768 ) {
      scrHt = 768;
    }
    if( scrWd > 1024 ) {
      scrWd = 1024;
    }
    var x = 0;
    var y = 0;
    if( x + scrWd > screen.availWidth ) {
      x = x - ( screen.availWidth - x + scrWd );
    }
    if( y + scrHt > screen.availHeight ) {
      y = y - ( screen.availHeight - y + scrHt );
    }

    winArgs = "height=" + scrHt + ",width=" + scrWd + ",scrollbars=1,resizable=1,toolbar=yes,top=" + y + ",left=" + x + ",status=1,dependent=1";
    winName = window.open( "", "Reports", winArgs );
    if( window.opener )
      window.opener.openWindows[ "Reports" ] = winName;

    sendForm.target = "Reports";
    sendForm.action = url;
    sendForm.submit();

    winName.focus();

  }
  else
  {

    tsShowButtonMenu( document.getElementById( btnName + "Menu"  ), btnName );
    var url;
    if (document.getElementById( "BOcustBio::Request::from_customer::0" ) != undefined)
      url = CommonPath + "/Widgets/summary.asp?from_customer=" + document.getElementById( "BOcustBio::Request::from_customer::0" ).value;
    else
	    url = CommonPath + "/Widgets/summary.asp";

    sendForm.target = "summarywidget";
    sendForm.action = url;
    sendForm.submit();

    // display our widget
    openWidgets[ "summarywidget" ] = true;
    var widget = document.getElementById( "Widget::summarywidget" );
    var shadow = document.getElementById( "Shadow::summarywidget" );

    shadow.style.zIndex = widgetIdx++;
    widget.style.zIndex = widgetIdx++;

    var left = document.body.clientWidth / 2 - 150 + document.body.scrollLeft;
    var top = document.body.clientHeight / 2 - 150 + document.body.scrollTop;
  
    widget.style.left = left;
    widget.style.top = top;
  
    shadow.style.left = left + 10;
    shadow.style.top = top + 10;

    shadow.style.display = "inline";
    widget.style.display = "block";
  }

    var parentElem = oMth.parentElement;
    
    var elem=document.getElementById('objectHandle');
    elem.parentNode.removeChild(elem)  
    elem=document.getElementById('objectMethod');
    elem.parentNode.removeChild(elem)  
    elem=document.getElementById('objectSrcForm');
    elem.parentNode.removeChild(elem)  
    elem=document.getElementById('objectType');
    elem.parentNode.removeChild(elem)  
    elem=document.getElementById('titleKey');
    elem.parentNode.removeChild(elem)  
    elem=document.getElementById('downloadFileName');
    elem.parentNode.removeChild(elem)  
    elem=document.getElementById('templates');
    elem.parentNode.removeChild(elem)  

    
    if( !showTemplate )
    {
        elem=document.getElementById('noTemplate');
        elem.parentNode.removeChild(elem)  
        elem=document.getElementById('template');
        elem.parentNode.removeChild(elem)  
    }

  sendForm.target = srcTarget;
  sendForm.action = srcAction;
}

function DoAction( inValue )
{
  try
  {
    window.event.cancelBubble = true;
  }
  catch( e )
  {
    // ignore, mozilla seems to not like this
  }

  switch( inValue )
  {
    case "logout" :
      LogOut();
      break;
    case "password" :
      openWidget( "password", CommonPath + "/Widgets/changePass.asp", { h: 225, w: 400 } );
      break;
    case "override" :
      openWidget( "override", CommonPath + "/Widgets/initOverride.asp", { h: 225, w: 400 } );
      break;
    case "role" :
      openWidget( "role", CommonPath + "/Widgets/changeRole.asp", { h: 255, w: 450 } );
      break;
    case "myaudienceview" :
      if( window.WindowPop != undefined )
      {
        WindowPop( 'myaudienceview' );
      }
      else
      {
        tabWindowPop( 'myaudienceview' );
      }
      break;
    case "about" :
      openWidget( "version", CommonPath + "/Widgets/version.asp", { h: 500, w: 700 } );
      break;
    default :
      break;  }
}

// calls the event for the key being pressed
function GetKey( evt )
{
  key = 0;

  try
  {
    if( window.event.srcElement.type != undefined )
    {
      return;
    }
  }
  catch( e )
  {
    if( evt.target.type != undefined )
    {
      return;
    }
  }

  try
  {
    key = window.event.keyCode;
  }
  catch( e )
  {
    try
    {
      // mozilla event handler
      key = evt.which;
    }
    catch( e )
    {
      // nada
      return;
    }
  }
  switch( key )
  {
    case 65:
      DoAction( "about" );
      break;
    case 76:
      DoAction( "logout" );
      break;
    case 77:
      DoAction( "myaudienceview" );
      break;
    case 79:
      DoAction( "override" );
      break;
    case 80:
      DoAction( "password" );
      break;
    case 83:
      DoAction( "role" );
      break;
    default:
      break;
  }
}

// Widget functions
var tsConfirmFunction = null;
var openWidgets = new Array();
var widgetIdx = 0;

function AValert( msg)
{
  var url = CommonPath + "/Widgets/alert.asp?message=" + encodeURIComponent(msg) + "&" + encodeURIComponent(sTokenName) + "=" + encodeURIComponent(sToken);
  openWidget( "alert", url, { h: 220, w: 300 } );
}

function AVconfirm( msg, func)
{
  tsConfirmFunction = func;
  var url = CommonPath + "/Widgets/confirm.asp?message=" + encodeURIComponent(msg) + "&" + encodeURIComponent(sTokenName) + "=" + encodeURIComponent(sToken);
  openWidget( "confirm", url, { h: 220, w: 300 } );
}

function confirmWidget( val )
{
  closeWidget( "confirm" );
  tsConfirmFunction( val )
}

function AVinvoice( invoice, mkey, key, sTokenName, sToken )
{
  tsShowButtonMenu( document.getElementById( mkey ), key );

  openWidget( "invoice", CommonPath + "/Widgets/invoice.asp?" + encodeURIComponent(sTokenName) + "=" + encodeURIComponent(sToken) + "&BOparam::BOorder::invoice::target=" + encodeURIComponent(invoice), { h: 225, w: 450 } );
}

// Mass add result nodes in basic form
function AVBIResult( hdl, bo_type )
{
  var url = CommonPath + "/Widgets/bi_results.asp?handle=" + encodeURIComponent(hdl) + "&type=" + encodeURIComponent(bo_type) + "&" + encodeURIComponent(sTokenName) + "=" + encodeURIComponent(sToken);
  openWidget( "bi_result", url, { h: 225, w: 800 } );
}

// Edit a BI Node or add a new one
function AVBIEdit( hdl, bo_type, path )
{
  var url = CommonPath + "/Widgets/bi_edit.asp?handle=" + encodeURIComponent(hdl) + "&type=" + encodeURIComponent(bo_type) + "&" + encodeURIComponent(sTokenName) + "=" + encodeURIComponent(sToken);
  if( path != null )
    url += "&path=" + encodeURIComponent(path);

  openWidget( "bi_edit", url, { h: 225, w: 450 } );
}

// Edit a BI Node or add a new one
function AVbuildLink( contentName, showBaseURL )
{
  var url = CommonPath + "/Widgets/linkBuilder.asp?contentName=" + encodeURIComponent(contentName) + "&" + encodeURIComponent(sTokenName) + "=" + encodeURIComponent(sToken);
  if( showBaseURL == true )
  {
    url += "&showBaseURL=true";
  }

  openWidget( "linkBuilder", url, { h: 225, w: 600 } );
}

function AVBIClose( handle, delList )
{
  for( name in delList )
  {
    cBox = document.getElementById( "Enable::Terminate::" + handle + "::Query::ResultMember::" + delList[name] );
    if( cBox )
      cBox.checked = true;
  }
  document.forms[0].submit();
}

function OpenGUIDWidget( imgID, hdl, url )
{
  var args = new Array();
  if( arguments.length == 4 )
  {
    args = arguments[3];
  }
  var img = document.getElementById( imgID );
  var fieldName = imgID.substr( imgID.indexOf( "::" ) + 2 );

  if( img.lastHandle != hdl )
  {
    // toggle menu highlights
    var oldpick = document.getElementById( "MenuItem::" + fieldName + "::" + img.lastHandle );
    if( oldpick )
      oldpick.className = "dr_menuitem";

    var newpick = document.getElementById( "MenuItem::" + fieldName + "::" + hdl );
    if( newpick )
      newpick.className = "dr_menuitem_select";

  }

  if( args[ "type" ] != undefined )
  {

    // reset the desired icon behaviour
    img.setAttribute("lastType", args[ "type" ]);
    img.setAttribute("lastHandle", hdl);
    img.setAttribute("lastURL", url);
  }
  else
  {
    // we've clicked the icon, is it still the same type?
    var attr = img.getAttribute("lastHandle");
    if( hdl != attr )
    {
      hdl = img.getAttribute("lastHandle");
      url = img.getAttribute("lastURL");
    }
  }

  if( img.style.cursor == "not-allowed" )
  {
    // activate the icon for general use
    img.style.cursor = "";
    tsSetEventListener(img, "mousedown", GUIDiconDown);
    tsSetEventListener(img, "mouseout", GUIDiconUp);
    tsSetEventListener(img, "mouseup", GUIDiconUp);
    tsSetEventListener(img, "click", GUIDiconSelect);
  }

  if( document.getElementById( "menu::" + fieldName ).style.display != "none" )
    tsShowButtonMenu( document.getElementById( fieldName + "::Menu" ), fieldName );

  openWidget( hdl, url, args );

}

// helper for dynamic guid pickers
function GUIDiconUp( event )
{
  if( event.srcElement )
    event.srcElement.className = 'iconup';
  else
    event.target.className = 'iconup';
}

// helper for dynamic guid pickers
function GUIDiconDown( event )
{
  if( event.srcElement )
    event.srcElement.className = 'icondown';
  else
    event.target.className = 'icondown';
}

// helper for dynamic guid pickers
function GUIDiconSelect( event )
{
  var obj = event.srcElement ? event.srcElement : event.target;
  OpenGUIDWidget( obj.id, obj.lastHandle, obj.lastURL, { h: 700, w: 800 } );
}
//End of DESKTOP specific only

// big list widget
function openListWidget( elem, handle, botype, prim, basename )
{
  url = CommonPath + "/Widgets/biglist.asp?handle=" + encodeURIComponent( handle );
  url += "&botype=" + encodeURIComponent( botype ) + "&element=" + encodeURIComponent( elem );
  url += "&primitive=" + encodeURIComponent( prim );
  url += "&basename=" + encodeURIComponent( basename );
  url += "&" + encodeURIComponent(sTokenName) + "=" + encodeURIComponent(sToken);

  if( arguments.length == 6 )
    url += "&nomulti=" + encodeURIComponent(arguments[5]);

  openWidget( prim, url, { h: 600, w: 600 } );
}

function openWidget( hdl, url )
{
  var widget, shadow;

  var args = new Array();
  if( arguments.length == 3 )
  {
    args = arguments[2];
  }
  
  if( openWidgets[ hdl ] == undefined )
  {
    widget = document.createElement( "iframe" );
    shadow = document.createElement( "div" );
    widget.id = "Widget::" + hdl;
    widget.name = "Widget::" + hdl;
    widget.className = "widget";
    widget.scrolling = "no";
    widget.border = 0;
    widget.frameborder = 0;
    widget.style.visibility = "hidden";   
    widget.src = url;
    shadow.id = "Shadow::" + hdl;
    shadow.className = "drop";
    shadow.style.visibility = "hidden";
    openWidgets[ hdl ] = true;

    document.getElementsByTagName( "body" )[0].appendChild( shadow );
    document.getElementsByTagName( "body" )[0].appendChild( widget );

  }
  else
  {
    widget = document.getElementById( "Widget::" + hdl );
    widget.src = url;
    shadow = document.getElementById( "Shadow::" + hdl );
  }

  var ht = "300px";
  var hdiv = 150;
  if( args[ "h" ] != undefined )
  {
    ht = args[ "h" ] + "px";
    hdiv = Math.round( args[ "h" ] / 2 );
  }
  
  var wt = "300px";
  var wdiv = 150;
  if( args[ "w" ] != undefined )
  {
    wt = args[ "w" ] + "px";
    wdiv = Math.round( args[ "w" ] / 2 );
  }
  
  widget.style.height = ht;
  widget.style.width = wt;
  shadow.style.height = ht;
  shadow.style.width = wt;

  // center this on the window for now
  var left = 0;
  var top = 0;

  if( args[ "x" ] == undefined )
  {
    left = document.body.clientWidth / 2 - wdiv + document.body.scrollLeft;
  }
  else
  {
    left = args[ "x" ];
  }

  if( args[ "y" ] == undefined )
  {
    top = document.body.clientHeight / 2 - hdiv + document.body.scrollTop;
  }
  else
  {
    top = args[ "y" ];
  }
  
  // Ensure the left of the dialog is displayed in the client area.
  if (left < document.body.scrollLeft)
  {
    left = document.body.scrollLeft;
  }
  
  // Ensure the top of the dialog is displayed in the client area.
  if (top < document.body.scrollTop)
  {
    top = document.body.scrollTop;
  }


  widget.style.left = left + "px";
  widget.style.top = top + "px";

  shadow.style.left = (left + 10) + "px";
  shadow.style.top = (top + 10) + "px";

  widget.style.visibility = "visible";
  shadow.style.visibility = "visible";

  shadow.style.zIndex = widgetIdx++;
  widget.style.zIndex = widgetIdx++;

//Start ONLINE
  shadow.style.position = "absolute";
  widget.style.position = "absolute";
//End ONLINE

  return true;
}

function openFormWidget( hdl )
{
  var widget, shadow;
  if( openWidgets[ hdl ] == undefined )
  {    
    shadow = document.createElement( "div" );
        
    // Fix for IE bug when trying to dynamically post a form to an iframe created in DOM
    var widget = document.createElement( "span" );
    widget.innerHTML = "<iframe id='Widget::" + hdl + "' name='Widget::" + hdl + "' class='widget' width='100%' height='92%' scrolling='no' style='visibility: hidden' frameBorder='0'>";

    shadow.id = "Shadow::" + hdl;
    shadow.className = "drop";
    shadow.style.visibility = "hidden";
    openWidgets[ hdl ] = true;
    document.getElementsByTagName( "body" )[0].appendChild( shadow );
    document.getElementsByTagName( "body" )[0].appendChild( widget );
  }
  else
  {
    shadow = document.getElementById( "Shadow::" + hdl );
  }
  widget = document.getElementById( "Widget::" + hdl );

  var args = new Array();
  if( arguments.length == 2 )
  {
    args = arguments[1];
  }
  var ht = "300px";
  var hdiv = 150;
  if( args[ "h" ] != undefined )
  {
    ht = args[ "h" ] + "px";
    hdiv = Math.round( args[ "h" ] / 2 );
  }

  var wt = "300px";
  var wdiv = 150;
  if( args[ "w" ] != undefined )
  {
    wt = args[ "w" ] + "px";
    wdiv = Math.round( args[ "w" ] / 2 );
  }

  widget.style.height = ht;
  widget.style.width = wt;
  shadow.style.height = ht;
  shadow.style.width = wt;

  // center this on the window for now
  var left = 0;
  var top = 0;

  if( args[ "x" ] == undefined )
  {
    left = document.body.clientWidth / 2 - wdiv + document.body.scrollLeft;
  }
  else
  {
    left = args[ "x" ];
  }

  if( args[ "y" ] == undefined )
  {
    top = document.body.clientHeight / 2 - hdiv + document.body.scrollTop;
  }
  else
  {
    top = args[ "y" ];
  }

  widget.style.left = left;
  widget.style.top = top;

  shadow.style.left = left + 10;
  shadow.style.top = top + 10;

  shadow.style.zIndex = widgetIdx++;
  widget.style.zIndex = widgetIdx++;

  widget.style.visibility = "visible";
  shadow.style.visibility = "visible";

  return true;
}
//End of DESKTOP specific only

function closeWidget( hdl, inMsg )
{
    elemW = document.getElementById( "Widget::" + hdl );
    elemS = document.getElementById( "Shadow::" + hdl );
	
    if(elemW && elemS)
    {
        if( hdl == "summarywidget" ||  hdl == "extractwidget")
        {
            elemW.style.display = "none";
	    elemS.style.display = "none";
        }
        else
        {
	    elemW.style.visibility = "hidden";
	    elemS.style.visibility = "hidden";
        }
        window.focus();
	
        if( inMsg != null && inMsg != "" )
        {
	    AValert( inMsg );
        }
    }
}

function resizeWidget( hdl, ht, wd )
{
  var widget = document.getElementById( "Widget::" + hdl );
  var shadow = document.getElementById( "Shadow::" + hdl );

  if( widget && shadow )
  {
    widget.style.height = ht + "px";
    shadow.style.height = ht + "px";
    widget.style.width = wd + "px";
    shadow.style.width = wd + "px";
  }
}

function applyWidget( hdl, inMsg, frmIdx )
{
  closeWidget( hdl, inMsg );

  var formTarget = 0;
  if( frmIdx != null )
  {
    formTarget = frmIdx;
  }

  var notifyElem = document.createElement( "INPUT" );
  notifyElem.type = "hidden";
  notifyElem.name = "tsAttachNotify";
  notifyElem.value = "1";
  document.forms[ formTarget ].appendChild( notifyElem );

  if( ++submitCount == 1 )
    document.forms[ formTarget ].submit();
}


/*
 *  Widget drag functions
 */

var widgetdragger = null;
var shadowdragger = null;

var widgetdragX = 0;
var widgetdragY = 0;
var widgetstartX = 0;
var widgetstartY = 0;

function startWidgetDrag( hdl, mouseX, mouseY )
{
  widgetdragger = document.getElementById( 'Widget::' + hdl );
  shadowdragger = document.getElementById( 'Shadow::' + hdl );

  if( shadowdragger )
    shadowdragger.style.zIndex = widgetIdx++;

  widgetdragger.style.zIndex = widgetIdx++;

  widgetdragX = parseInt( widgetdragger.style.left );
  widgetdragY = parseInt( widgetdragger.style.top );
  widgetstartX = mouseX;
  widgetstartY = mouseY;
}

function stopWidgetDrag()
{
  widgetdragger = null;
  shadowdragger = null;
}

function moveWidget( offsetX, offsetY )
{
  if( ( widgetdragX + offsetX < 0 ) || ( widgetdragY + offsetY ) < 0 )
  {
    return;
  }

  if( widgetdragger != null )
  {
    try
    {
      widgetdragger.style.left = ( widgetdragX + offsetX ) + "px";
      widgetdragger.style.top  = ( widgetdragY + offsetY ) + "px";

      if( shadowdragger )
      {
        shadowdragger.style.left = ( widgetdragX + offsetX + 10 ) + "px";
        shadowdragger.style.top  = ( widgetdragY + offsetY + 10 ) + "px";
      }
    }
    catch( e )
    {
    }
  }
}

