
var IsMSIE   = navigator.userAgent.toLowerCase().indexOf( "msie" ) > -1 ? true : false;
var IsSafari = navigator.userAgent.toLowerCase().indexOf( "safari" ) > -1 ? true : false;
var Tabs     = 2;

// preload images
var Image1 = new Image();
Image1.src = "Images/2tabs_tab1.gif";

var Image2 = new Image();
Image2.src = "Images/2tabs_tab2.gif";

var Image3 = new Image();
Image3.src = "Images/3tabs_tab1.gif";

var Image4 = new Image();
Image4.src = "Images/3tabs_tab2.gif";

var Image5 = new Image();
Image5.src = "Images/3tabs_tab3.gif";

function HideStatusWindows()
{
  var Frame = document.getElementById( 'OneMomentScreen' );
  if ( Frame )
    Frame.style.display = 'none';

}    

function HideAllTabs()
{
  document.getElementById( 'Tab1' ).style.display              = 'none';
  document.getElementById( 'Tab2' ).style.display              = 'none';
  
  if ( document.getElementById( 'Tab3' ) )
  document.getElementById( 'Tab3' ).style.display              = 'none';

  document.getElementById( 'ContextHelpFrame' ).style.zIndex   = 0;
  document.getElementById( 'MenuFrame' ).style.zIndex          = 0;
  document.getElementById( 'ReportsAdminFrame' ).style.zIndex  = 0;
  

}

function ShowOneMoment()
{
  var Frame = document.getElementById( 'OneMomentScreen' );
  if ( Frame )
    Frame.style.display = "";
}        

function ShowHelpFrame()
{
  HideAllTabs();

  //document.getElementById( 'ContextHelpFrame' ).style.display  = '';
  document.getElementById( 'ContextHelpFrame' ).style.zIndex  = 1;
  document.getElementById( 'Tab2' ).style.display              = '';
  document.getElementById( 'TabsImage' ).src                   = "Images/" + Tabs + "tabs_Tab2.gif";

  ContextHelpFrame.focus();
}    

function ShowReportMenuFrame()
{
  HideAllTabs();

  //document.getElementById( 'MenuFrame' ).style.display         = '';
  document.getElementById( 'MenuFrame' ).style.zIndex          = 1;
  document.getElementById( 'Tab1' ).style.display              = '';
  document.getElementById( 'TabsImage' ).src                   = "Images/" + Tabs + "tabs_Tab1.gif";

  MenuFrame.focus();
}

function ShowAdminFrame()
{
  HideAllTabs();

  //document.getElementById( 'MenuFrame' ).style.display         = '';
  document.getElementById( 'ReportsAdminFrame' ).style.zIndex  = 1;
  document.getElementById( 'Tab3' ).style.display              = '';
  document.getElementById( 'TabsImage' ).src                   = "Images/" + Tabs + "tabs_Tab3.gif";
  ReportsAdminFrame.focus();
}

function SetContextHelp( Report )
{
  var URL = GetFrameLocation( 'ContextHelpFrame' );
  if ( URL.indexOf( "_" + Report + "." ) < 0 )
  {
    if ( Report.indexOf( "CRM_" ) > -1 )
    {
      Report = Report.substring( 0, Report.indexOf( "_" ) );
    }
    SetFrameLocation( 'ContextHelpFrame', "Docs/ContextHelp/ContextHelp_" + Report + ".aspx" );
  }
  
}

function CenterIframe( FrameName )
{
    var FrameObj    = document.getElementById( FrameName );
    var FrameWidth  = MN( FrameObj.style.width );
    var FrameHeight = MN( FrameObj.style.height );
    
    var ClientWidth = document.body.clientWidth
    var ClientHeight = document.body.clientHeight
    var Left = ( ClientWidth / 2 ) - ( FrameWidth / 2 );
    var Top  = ( ClientHeight / 2 ) - ( FrameHeight / 2 );
    
    FrameObj.style.left = Left;
    FrameObj.style.top  = Top;
}    

function HideAllFrames()
{
  try
  {
      HideExportFrame();
      HideEmailReportFrame();
      HideUniversalFrame();
      HideAltFrame();
  }
  catch( ex )
  {
  }
}

function ShowExportFrame( Report, Modes )
{
  HideAllFrames();
  IEhideApplets( "parent.top.ContentFrame." );
  parent.top.ContentFrame.HideDropDownLists();
  SetFrameLocation( "ExportFrame", "Export.aspx?Report=" + Report + "&LastURL=" + escape( parent.top.ContentFrame.document.location.href ) );
  CenterIframe( "ExportFrame" );
}

function HideExportFrame()
{
  parent.top.document.getElementById( 'ExportFrame' ).style.display = 'none';
  IEshowApplets( "parent.top.ContentFrame." );
  parent.top.ContentFrame.ShowDropDownLists();

}

function ShowEmailReportFrame( Report )
{
  HideAllFrames();
  IEhideApplets( "parent.top.ContentFrame." );
  var Params = parent.top.ContentFrame.document.location.search;
  SetFrameLocation( 'EmailReportFrame', "EmailReport.aspx" + Params );
  CenterIframe( "EmailReportFrame" );
}

function HideEmailReportFrame()
{
  parent.top.document.getElementById( 'EmailReportFrame' ).style.display = 'none';
  IEshowApplets( "parent.top.ContentFrame." );

}

function GetPrintURL()
{
  var URL = parent.top.ContentFrame.document.location.href;
  if ( URL.indexOf( "?" ) == -1 )
    URL += "?";
    
  URL = RemoveParam( URL, "UsingWidth" );
  URL += "&UsingWidth=650"  
  URL += "&Print=true"
  
  return URL;
}

function ShowUniversalFrame( URL, Width, Height )
{
  HideAllFrames();
  IEhideApplets( "parent.top.ContentFrame." );
  SetFrameLocation( "UniversalFrame", URL );
  var Frame = document.getElementById('UniversalFrame');
  Frame.style.width  = Width;
  Frame.style.height = Height;
  CenterIframe( "UniversalFrame" );
}

function HideUniversalFrame()
{
  parent.top.document.getElementById( 'UniversalFrame' ).style.display = 'none';
  IEshowApplets( "parent.top.ContentFrame." );
}

var ForceMaximize = false;
var Max_CalendarHeight = 0;
var CurrentCalendarHeight = 0;
var OldContentHeight  = 0;
var Default_NewCalendarHeight = 64;
var Calendar_State = 0; // 0 == Maximized, 1 == Minimized

function isYearGraphMinimized()
{
  if( Calendar_State == 1)
    return true;
    
  return false;
}
function RefreshYearGraph()
{
    if ( parent.top.CalendarFrame.ShowOneMomentScreen )
      parent.top.CalendarFrame.ShowOneMomentScreen();
    
    var CalendarLeft = -1;
    if ( parent.top.CalendarFrame && parent.top.CalendarFrame.CalendarImage )
    {
      CalendarLeft = MN( parent.top.CalendarFrame.CalendarImage.style.left );
    }
  
    parent.top.CalendarFrame.document.location = "CalendarGraph.aspx?CalendarLeft=" + CalendarLeft;	//just have to reset it to that point
}

function VerifyCalendarState()
{
      if( isYearGraphMinimized() )
      {
          if ( ForceMaximize )
          {
              MaximizeYearGraph();
              ForceMaximize = false;
          }
          else
            ShowCalendarDateSummary();
      }        
}

function MinimizeYearGraph( NewCalendarHeight )
{
  if ( NewCalendarHeight == null )
   NewCalendarHeight = Default_NewCalendarHeight;
   
  if ( isYearGraphMinimized() && CurrentCalendarHeight == NewCalendarHeight )
    return;
    
  parent.top.CalendarFrame.document.getElementById('Maximize').style.display = '';
  parent.top.CalendarFrame.document.getElementById('Minimize').style.display = 'none';
  

  var CalendarFrame     = document.getElementById( 'CalendarFrame' );

  CurrentCalendarHeight = NewCalendarHeight;
  LastSetContentHeight +=  ( MN( CalendarFrame.style.height ) - NewCalendarHeight )
  
  var a = new parent.top.Accelimation(CalendarFrame.style, "height", NewCalendarHeight, 500, 1);
  var b = new parent.top.Accelimation(ContentFrameObj.style, "height", LastSetContentHeight, 500, 1);

  a.onend = function() {   
                           ShowCalendarDateSummary();
                           b.start();
                           return false;
                       }
  a.start();
  
  Calendar_State = 1;
}

function ShowCalendarDateSummary()
{
    parent.top.CalendarFrame.document.getElementById('CalendarGraphTable').style.display = 'none';
    parent.top.CalendarFrame.document.getElementById('CalendarSummaryTable').style.display = '';
}

function HideYearGraph()
{
    MinimizeYearGraph( 0 );
}
function MaximizeYearGraph()
{
  if(!isYearGraphMinimized())
     return false;
     
  parent.top.CalendarFrame.document.getElementById('Maximize').style.display = 'none';
  parent.top.CalendarFrame.document.getElementById('Minimize').style.display = '';
  parent.top.CalendarFrame.document.getElementById('CalendarGraphTable').style.display = '';
  parent.top.CalendarFrame.document.getElementById('CalendarSummaryTable').style.display = 'none';

  var CalendarFrame   = document.getElementById( 'CalendarFrame' );
  
  LastSetContentHeight -=  ( Max_CalendarHeight - CurrentCalendarHeight )
  CurrentCalendarHeight = Max_CalendarHeight;
  
  var a = new parent.top.Accelimation(ContentFrameObj.style, "height", LastSetContentHeight, 500, 1);
  var b = new parent.top.Accelimation(CalendarFrame.style, "height", Max_CalendarHeight, 500, 1);

  a.onend = function() {   b.start();
                           return false;
                       }
  
  a.start();
 
  Calendar_State = 0;
}

function ShowDialog( Icon, Title, Message, OkText, OKAction, CancelText, CancelAction, Width, Height, Mode )
{
    IEhideApplets( "parent.top.ContentFrame." );
    IEhideApplets
    Icon          = escape( Icon );
    Title         = escape( Title );
    Message       = escape( Message );
    OkText        = escape( OkText );
    OKAction      = escape( OKAction );
    CancelText    = escape( CancelText );
    CancelAction  = escape( CancelAction );
    var URL       = "DMDialog.aspx?Icon=" + Icon + "&Title=" + Title + "&Message=" + Message + "&OkText=" + OkText + "&OKAction=" + OKAction + "&CancelText=" + CancelText + "&CancelAction=" + CancelAction + "&Mode=" + Mode;

    SetFrameLocation( "DialogBox", URL );
    
    var Frame = parent.top.document.getElementById( 'DialogBox' );
    Frame.style.height = Height;
    Frame.style.width  = Width;
    CenterIframe( "DialogBox" );
}

function SetFrameLocation( FrameName, Location )
{
  var Frame = parent.top.document.getElementById( FrameName );
  
  if ( IsSafari )
    Frame.style.display = ''; // Safari requires the frame to have a display.
  
  Frame.src = Location;
}

function GetFrameLocation( FrameName )
{
  return parent.top.document.getElementById( FrameName ).src;
}


var LastSetContentHeight = 0;
function SetSizes()
{
  var AvailableHeight = GetPageHeight();
  
  AvailableHeight -= MN( CalendarFrameObj.style.height );
  AvailableHeight -= MN( ReportSettingsFrameObj.style.height );
  AvailableHeight -= 22;

  ContentFrameObj.style.height = AvailableHeight;
  LastSetContentHeight         = AvailableHeight;
  
  if ( CalendarFrame && CalendarFrame.document )
  {
    var Table = CalendarFrame.document.getElementById('tblMainTable');
    if ( Table )
      Table.style.width = ContentFrameObj.offsetWidth;
  }
  
  // only record the maximized height, if it's not minimized
  if ( ! isYearGraphMinimized() )
    Max_CalendarHeight     = MN( CalendarFrameObj.style.height );
  
}


var Arial15PixelBoldWidth  = new Array( 4, 4, 7, 8, 8, 13, 11, 4, 5, 5, 6, 9, 4, 5, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 4, 4, 9, 9, 9, 9, 15, 9, 11, 11, 11, 10, 9, 12, 11, 4, 8, 11, 9, 13, 11, 12, 10, 12, 11, 10, 10, 11, 9, 13, 10, 10, 8, 5, 4, 5, 9, 8, 5, 8, 9, 8, 9, 9, 5, 9, 9, 4, 4, 8, 4, 12, 9, 9, 9, 9, 6, 8, 5, 9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, 6, 6, 6, 6, 6, 6, 3, 3, 6, 6, 6, 6, 10, 7, 7, 7, 7, 7, 6, 8, 7, 3, 6, 7, 6, 9, 7, 8, 7, 8, 7, 7, 5, 7, 7, 9, 7, 7, 6, 3, 3, 3, 6, 6, 3, 6, 6, 6, 6, 6, 3, 6, 6, 3, 4, 6, 3, 9, 6, 6, 6, 6, 4, 6, 3, 6, 7, 9, 6, 5, 5, 4, 3, 4, 6 );


var Arial9PixelWidth       = new Array( 3, 3, 3, 5, 5, 8, 6, 2, 3, 3, 4, 5, 3, 3, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 5, 5, 5, 5, 9, 6, 6, 7, 7, 6, 6, 7, 7, 3, 5, 6, 5, 7, 7, 7, 6, 7, 7, 6, 5, 7, 6, 9, 5, 7, 6, 3, 3, 3, 3, 5, 3, 5, 5, 5, 5, 5, 4, 5, 5, 2, 2, 5, 2, 8, 5, 5, 5, 5, 3, 5, 3, 5, 6, 6, 5, 6, 4, 3, 3, 3, 5 );
var Arial9PixelBoldWidth   = new Array( 3, 3, 4, 5, 5, 8, 6, 2, 3, 3, 4, 5, 3, 3, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 3, 3, 5, 5, 5, 5, 9, 5, 7, 7, 7, 6, 6, 7, 7, 3, 5, 7, 6, 7, 7, 7, 6, 7, 7, 6, 5, 7, 6, 9, 6, 5, 6, 3, 3, 3, 5, 5, 3, 5, 6, 5, 6, 5, 3, 6, 6, 3, 3, 5, 3, 8, 6, 6, 6, 6, 4, 5, 3, 6, 5, 7, 5, 5, 5, 4, 3, 4, 5 );

function GetStringWidth( Text, CharWidthArray )
{
  var Length     = Text.length
  var TotalWidth = 0;
  var CharCode;
  
  for (var i = 0; i < Length; i++ )
  {
    CharCode = Text.charCodeAt( i );
    
    if ( CharCode > 126 )
    {
        CharCode = 64;
    }
    TotalWidth += CharWidthArray[ CharCode - 32 ];
  }
  
  return TotalWidth;

}
