﻿var isCCS, isIE6, isNN7;

// Watch Dog
var hWatchDog            = null;
var bWatchDogStarted     = false;

//****************************************************************************
// Method to determine the type of browser
function initDHTMLAPI()
{
  if (document.images)
  {
    isCCS = (document.body && document.body.style) ? true : false;
    isIE6 = (isCCS && document.all) ? true : false;
    isNN7 = (isIE6) ? false : true;
  }
}

//*****************************************************************************
// When the watchdog thread times out we need to reset everything
function WatchDog()
{
  hWatchDog = null;
  FinishAjaxCall();
}

//*****************************************************************************
// Provides the current state of the application
function IsWaitState()
{
  return bWatchDogStarted;
}

//*****************************************************************************
// Called to change the cursor on the form
function SetCursor(sCursor)
{
  try
  {
    if (document.getElementById('RunReport') != null)
      document.getElementById('RunReport').style.cursor = sCursor;
  }
  
  catch (e)
  {
  }
}

//*****************************************************************************
// Called when a ajax call is made
function StartAjaxCall()
{
  var bStart = false;
  
  try
  {
    if (bWatchDogStarted == false)
    {
      bWatchDogStarted = true;
      
      // Show the wait message
      document.getElementById('ProcessingMessageImage').innerHTML = '<img src="../Images/LoadingNewRotateSmall.gif" alt="Loading Data" />';
      document.getElementById('ProcessingMessage').style.display = '';
      
      // Change everything to the wait cursor
      SetCursor('wait');
      
      // Start the watchdog thread for 15 seconds
      hWatchDog = setTimeout("WatchDog()", 15000);
      
      // Flag that we started
      bStart = true;
    }
  }
  
  catch (e)
  {
  }
  
  return bStart;
}

//*****************************************************************************
// Called when a ajax call is finished
function FinishAjaxCall()
{
  try
  {
    // Reset the watchdog flag
    bWatchDogStarted = false;
    
    // Stop the watchdog
    if (hWatchDog != null)
    {
      clearTimeout(hWatchDog);
      hWatchDog = null;
    }
    
    // Reset the cursor
    SetCursor('pointer');
    
    // Hide the wait message
    document.getElementById('ProcessingMessageImage').innerHTML = '';
    document.getElementById('ProcessingMessage').style.display  = 'none';
  }
  
  catch (e)
  {
  }
}

//*****************************************************************************
// Function to open a window with the calculator report
function openNote()
{ 
  var agt=navigator.userAgent.toLowerCase(); 
  var is_aol   = (agt.indexOf("aol") != -1);

  leftpos=0;
  if (screen)
  {
    leftpos=screen.width/2 - 300;
  } 

  if (is_aol)
  {
    var s = " ";
    s=document.calculator.sJavaScriptReport('HTML');
      document.write(s);
  }
  else
  {
    var OpenWindow=window.open("","newwin","toolbar=yes,menubar=yes,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width=600,height=380,left="+leftpos+",top=20");
    var s = " ";
    s=document.calculator.sJavaScriptReport('HTML');
    OpenWindow.document.write(s);
    OpenWindow.document.close(); 
    OpenWindow.focus();
  }
}

//*****************************************************************************
// Called when the calculator list changes
function OnCalculatorListChange()
{
  try
  {
    // Call the server for the list of calculators
    if (StartAjaxCall())
    {
      // Get the new list
      if (document.getElementById('CalculatorList').value != "Select One")
        Homekeys.Calculators.Calculators.RetrieveCalculator(document.getElementById('CalculatorList').value, RetrieveCalculator_CallBack);
      else
      {
        document.getElementById('CalculatorApplet').innerHTML = 'These calculators require the latest version of Sun\'s<br /><a href="http://www.java.com/en/download/manual.jsp" target="_blank">Java Vurtual Machine</a>.&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://www.java.com/en/download/help/testvm.xml" target="_blank">Not Sure?</a>';
        FinishAjaxCall();
      }
    }
  }
  
  catch (e)
  {
    FinishAjaxCall();
  }
}

//*****************************************************************************
// Called to capture the calculator code
function RetrieveCalculator_CallBack(sResponse)
{
  try
  {
    if (sResponse.value != null)
    {
      document.getElementById('CalculatorApplet').innerHTML = sResponse.value;
    }
    else
      document.getElementById('CalculatorApplet').innerHTML = 'These calculators require the latest version of Sun\'s<br /><a href="http://www.java.com/en/download/manual.jsp" target="_blank">Java Vurtual Machine</a>.&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://www.java.com/en/download/help/testvm.xml" target="_blank">Not Sure?</a>';
  }
  
  catch (e)
  {
    document.getElementById('CalculatorApplet').innerHTML = 'These calculators require the latest version of Sun\'s<br /><a href="http://www.java.com/en/download/manual.jsp" target="_blank">Java Vurtual Machine</a>.&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://www.java.com/en/download/help/testvm.xml" target="_blank">Not Sure?</a>';
  }
  
  FinishAjaxCall();
}
