// JavaScript Document
  function toggleCheckBoxes ( all, boxId, countBoxes )
  {
    var stillOneChecked = false;

    if ( all && $( boxId + 0 ).checked )
    {
      for ( i=1; i<countBoxes; i++ )
      {
         $( boxId + i ).checked = false;
      }
    }

    if ( !all )
    {
      $( boxId + 0 ).checked = false;
    }

    oneIsChecked = false;

    for ( i=0; i<countBoxes; i++ )
    {
      if ( $( boxId + i ).checked == true )
      {
        oneIsChecked = true;
      }
    }
    if ( !oneIsChecked )
    {
      $( boxId + 0 ).checked = true;
    }

  }

  function toggleElementClass ( header, open, close )
  {
    if ( !open )
    {
      open = 'open';
    }

    if ( !close )
    {
      close = 'close-head';
    }

    if ( $( header ).hasClassName( close ) )
    {
      $( header ).removeClassName( close );
      $( header ).addClassName( open );
    }
    else if ( $( header ).hasClassName( open ) )
    {
      $( header ).removeClassName( open );
      $( header ).addClassName( close );
    }
  }

  function showOnce( id )
  {
    try
    {
      if ( $( id ).style.display == 'none' )
      {
        Effect.BlindDown( id );
      }
    }
    catch ( e ) { }
  }

  function alignDivPopup ( id ) {

    var x, y;

    if ( self.innerHeight ) // all except Explorer
    {
      x = self.innerWidth;
      y = self.innerHeight;
      yOffset = self.pageYOffset;
    }
    else if ( document.documentElement && document.documentElement.clientHeight )
      // Explorer 6 Strict Mode
    {
      x = document.documentElement.clientWidth;
      y = document.documentElement.clientHeight;
      yOffset = document.documentElement.scrollTop;
    }
    else if ( document.body ) // other Explorers
    {
      x = document.body.clientWidth;
      y = document.body.clientHeight;
      yOffset = document.body.scrollTop;
    }

    var halfWindowWidth = Math.round( x / 2 );
    var halfWindowHeight = Math.round( y / 2 );
    var halfPopupWidth = 100;
    var halfPopupHeight = 75;

    var alignLeft = halfWindowWidth - halfPopupWidth;
    var alignTop = halfWindowHeight - halfPopupHeight + yOffset;

    $( id ).style.left = alignLeft + "px";
    $( id ).style.top = alignTop + "px";
  }

  function addEmo( myId, text )
  {
    try
    {
      var txtarea = $( myId );
      text = ' ' + text + ' ';

      if ( txtarea.createTextRange && txtarea.caretPos )
      {
        var caretPos = txtarea.caretPos;
        caretPos.text = caretPos.text.charAt( caretPos.text.length - 1 ) == ' ' ? text + ' ' : text;
        txtarea.focus();
      }
      else
      {
        txtarea.value += text;
        txtarea.focus();
      }
    }
    catch ( e ) {}
  }

function flashUpdate()
{
  try
  {
    updateProfile();
    updateHighscores();
    updatePlayersCounts();
    updatePlayers();
    updateBattlesCounts();
    updateBattles();
  }
  catch ( e ) {}
}

function updateProfile()
{
  try
  {
    new Ajax.Updater( 'profilePanelScore',
                      '/players/getScore',
                      { asynchronous: true,
                        evalScripts: true,
                        onComplete: function(request, json)
                          {  },
                        requestHeaders: ['X-Update', 'profilePanelScore'] }
                     );
  }
  catch ( e ) {}
}

function updateHighscores()
{
  try
  {
    new Ajax.Updater( 'highScCon',
                      '/games/get_highscores',
                      { asynchronous: true,
                        evalScripts: true,
                        onComplete: function(request, json)
                          {},
                        requestHeaders: ['X-Update', 'highScCon'] }
                     );
  }
  catch ( e ) {}
}

function updatePlayersCounts()
{
  try
  {
    new Ajax.Updater( 'onlinePlayerCounter',
                      '/players/getCountPlayersOnline',
                      { asynchronous: true,
                        evalScripts: true,
                        onComplete: function(request, json)
                          {},
                        requestHeaders: ['X-Update', 'onlinePlayerCounter'] }
                     );
  }
  catch ( e ) {}
}

function updatePlayers()
{
  try
  {
    new Ajax.Updater( 'playerCon',
                      '/players/teaser',
                      { asynchronous: true,
                        evalScripts: true,
                        onComplete: function(request, json)
                          {},
                        requestHeaders: ['X-Update', 'playerCon'] }
                     );
  }
  catch ( e ) {}
}

function updateBattlesCounts()
{
  try
  {
    new Ajax.Updater( 'openBattleCounter',
                      '/players/setBattlesTotal/xxx/new',
                      { asynchronous: true,
                        evalScripts: true,
                        onComplete: function(request, json)
                          {},
                        requestHeaders: ['X-Update', 'openBattleCounter'] }
                     );
  }
  catch ( e ) {}
}

function updateBattles()
{
  try
  {
    new Ajax.Updater( 'battleCon',
                      '/players/setBattles/xxx/limit:6',
                      { asynchronous: true,
                        evalScripts: true,
                        onComplete: function(request, json)
                          {},
                        requestHeaders: ['X-Update', 'battleCon'] }
                     );
  }
  catch ( e ) {}
}

function sendRequestAndInactivateButton( button, message )
{
  try
  {
    href = $( button ).getAttribute( 'href' );
    if ( href != '#' )
    {
      $( button ).hide();
      $( button ).setAttribute( 'href', '#' );
      location.href = href;
    }
  }
  catch ( e ) {}
}

function preloadHovers( baseUrl )
  {
    try
    {
      //alert('preload Hovers called');
      var imgPath = "css/img/";

      var hoverArr = new Array(
        baseUrl + imgPath + "link_down_over.png",
        baseUrl + imgPath + "link_extern_over.png",
        baseUrl + imgPath + "link_intern_over.png",
        baseUrl + imgPath + "arrow-dome2-close.gif",
        baseUrl + imgPath + "arrow-dome2-close.gif",
        baseUrl + imgPath + "arrow-dome-close.gif",
        baseUrl + imgPath + "arrow-dome-open.gif",
        baseUrl + imgPath + "slider-next.gif",
        baseUrl + imgPath + "slider-next-state2.gif",
        baseUrl + imgPath + "slider-prev.gif",
        baseUrl + imgPath + "slider-prev-state2.gif",
        baseUrl + imgPath + "btn/btn-02-left-active.png",
        baseUrl + imgPath + "btn/btn-02-right-active.png",
        baseUrl + imgPath + "btn/btn-02-left-active.gif",
        baseUrl + imgPath + "btn/btn-02-right-active.gif",
        baseUrl + imgPath + "btn/btn-03-left-active.png",
        baseUrl + imgPath + "btn/btn-03-right-active.png",
        baseUrl + imgPath + "btn/btn-03-left-active.gif",
        baseUrl + imgPath + "btn/btn-03-right-active.gif"
      );

      preload = new Array();

      for ( i = 0; i < hoverArr.length; i++ )
      {
        preload[i]= new Image();
        preload[i].src = hoverArr[i];
      }
    }
    catch ( e ) {}
  }

  function addInput( target )
  {
    try
    {
      var arr = Element.getElementsByClassName( 'inputFriend', 'friends-label-input' );
      var searchBarCount = arr.length;

      if ( target  ) {

        searchBarCount++;

        var node  = arr.last();
        var clone = node.cloneNode( true );

        clone.innerHTML = clone.innerHTML.replace( /\s\d+:/, " " + searchBarCount + ":" );

        $( 'inputFriend' ).appendChild( clone );
      }
    }
    catch ( e ) {}
  }

  function validate_form( alertText1, alertText2 )
  {
    try {
      var arr = Element.getElementsByClassName( 'inputFriend', 'friends-input' );
      var arrEmpty = true;

      for ( i = 0; i < arr.length; i++ )
      {
        if ( arr[i].value != '' )
        {
          arrEmpty = false;
          break;
        }
      }

      if ( arrEmpty )
      {
        alert( alertText1 );

        return false;
      }

      for ( i = 0; i < arr.length; i++ )
      {
        if ( arr[i].value != '' && !validate_email( arr[i] ) )
        {
          arr[i].style.borderColor = "red";
          arr[i].focus();

          alert( alertText2 );

          return false;
        }

        arr[i].style.borderColor = "#C2A29E";
      }
    }
    catch ( e ) {}

    return true;
  }

  function validate_email( field )
  {
    try {
      with ( field )
      {
        if ( /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test( value ) )
        {
          return true;
        }
      }
    }
    catch ( e ) {}

    return false;
  }