/////////////////////////////////////////////////////////
// Interwindow communications

// used to remove an item from an array
Array.prototype.remove = function remove(iStart, iCount)
{
  if( !iCount )
    iCount = 1;

  if( (iStart > this.length) && ((iStart+iCount) >= this.length) )
    return;

  this.splice(iStart,iCount);
}

// Copies selections from a select box on the opening window to a
// select box in the current window.  Fore example, it will copy
// the vehicle list from the main.htm to the maintvehicle.htm.
function CopySelections(ReadSel, WriteSel, CurSelection)
{
  var RSel, WSel;
  var i, iCnt;

  if( typeof(window.opener.getelement) == 'undefined' )
    return false;

  RSel = window.opener.getelement(ReadSel);  // read select box is in the opening window
  WSel = getelement(WriteSel);

  if( !RSel || (typeof(RSel.options) == "undefined") || 
      !WSel || (typeof(WSel.options) == "undefined") )
    return false;

  WSel.options.length = 0; // remove all options
	
  iCnt =	RSel.options.length;

  for(i = 0; i < iCnt; i++)
  { 
    WSel.options[WSel.options.length] = new Option(RSel.options[i].text, RSel.options[i].value);
    if( (!CurSelection || (CurSelection.length == 0)) && (i == 0) )
      WSel.options[WSel.options.length - 1].selected = true;
    if( CurSelection && 
        ((CurSelection == RSel.options[i].value) || 
         (CurSelection == RSel.options[i].text)) )
      WSel.options[WSel.options.length - 1].selected = true;
  }

  return true;
}

// called to update accounting information in all open windows
// this function calls the main windows UpdateChildWindows function
function UpdateInterWindowData(PaymentType, HitCount, Balance, UpdateTime)
{
  if( window.opener && !window.opener.closed )
  {
    UpdateAccountInfo(window.opener, PaymentType, HitCount, Balance, UpdateTime)
    if( typeof(window.opener.UpdateChildWindows) != 'undefined' )
      window.opener.UpdateChildWindows(document.window, PaymentType, HitCount, Balance, UpdateTime);
    return true;
  }

  return false;
}

function UpdateChildWindows(Window, PaymentType, HitCount, Balance, UpdateTime)
{
  var i;

  for(i = 0; i < OpenWinCount; i++)
  {
    try
    {
      if( !OpenWins[i] || OpenWins[i].closed )
      {
        OpenWins.remove(i);
        i--;
        OpenWinCount--;
        continue;
      }

      if( OpenWins[i] != Window )
        UpdateAccountInfo(OpenWins[i], PaymentType, HitCount, Balance, UpdateTime);
    }
    catch(ex)
    {
      continue;
    }
  }
}

function UpdateAccountInfo(Window, PaymentType, HitCount, Balance, UpdateTime)
{
  if( typeof(Window.getelement) == 'undefined' )  // window does not have common.js loaded, can't continue
    return;

  var AcctPaymentType = Window.getelement('AcctPaymentType');
  var AcctHitCount = Window.getelement('AcctHitCount');
  var AcctBalance = Window.getelement('AcctBalance');
  var AcctLastUpdated = Window.getelement('AcctLastUpdated');

  if( PaymentType && AcctPaymentType )
    AcctPaymentType.innerHTML = PaymentType;
  
  if( HitCount && AcctHitCount )
    AcctHitCount.innerHTML = HitCount;
  
  if( Balance && AcctBalance )
    AcctBalance.innerHTML = Balance;

  if( UpdateTime && AcctLastUpdated )
    AcctLastUpdated.innerHTML = UpdateTime;
}


function ShowSignInPage()
{
  try
  {
    if (window.opener && !window.opener.closed &&
        (typeof (window.opener.SignOut) != 'undefined') && window.opener.SignOut())
    {
      window.close();
      return true;
    }
  }
  catch (err)
  {
  }
  
  return false;
}

function SignOut()
{
  if( typeof(SignOutLink) != "undefined" )
  {
    location.href = SignOutLink;
    return true;
  }

  return false;
}


