﻿
  window.onerror = function( )
  {
    if ( !window.bErrorDispatched )
    {
      window.bErrorDispatched = true;
      window.status = 'Si è verificato un errore javascript: si prega di contattare lo staff di Giovani.it.';
    }

    return true;
  }


  if ( !window.onload )
  {
    window.onload = function()
    {
      if ( document.getElementById('GF_LoginCheking') )
      {
        document.getElementById('GF_LoginCheking').style.display = 'none';
    
        if ( window.oDynamicXSS != null && oDynamicXSS.oUser != null )
        {
          document.getElementById('GF_LoginUserName').innerHTML = oDynamicXSS.oUser.strUserName != null ? oDynamicXSS.oUser.strUserName : 'N/D';
          document.getElementById('GF_LoginLogged').style.display = '';
        }
    
        else
          document.getElementById('GF_LoginAnonymous').style.display = '';


        if ( window.oDynamicXSS != null && oDynamicXSS.iOnlineUsers != null )
          document.getElementById('GF_CommunityOnlineUsers').innerHTML = oDynamicXSS.iOnlineUsers;
      }
    }
  }
  

  function GF_CleanString( strString )
  {
    return strString.replace( /^[ \t\r\n\v\f]+|[ \t\r\n\v\f]+$/g, '' ).replace( /[ \t\r\n\v\f]+/g, ' ' );
  }


  function GF_ShowZoomedImage( uriThumbnailImage )
  {
    document.getElementById( 'GF_LoadingBackground' ).className = 'loading';
    document.getElementById( 'GF_LoadingForeground' ).className = 'loader';

    var hZoomedImage = new Image( );

    hZoomedImage.onerror = onabort = function( )
    {
      alert( 'Siamo spiacenti, ma non è stato possibile caricare l\'immagine.' );
    }

    hZoomedImage.onload = function( )
    {
      if ( window.hZoomedImageWnd && !window.hZoomedImageWnd.closed )
        hZoomedImageWnd.close( );

      window.hZoomedImageWnd = window.open( hZoomedImage.src, 'GF_ZoomerWnd', 'scrollbars=no,resizable=yes,status=no,location=no,toolbar=no,menubar=no,directories=no,width=' + (hZoomedImage.width+15) + ',height=' + (hZoomedImage.height+15) );

      document.getElementById( 'GF_LoadingBackground' ).className = 'hidden';
      document.getElementById( 'GF_LoadingForeground' ).className = 'hidden';

      if ( window.hZoomedImageWnd )
        window.hZoomedImageWnd.focus && window.hZoomedImageWnd.focus( );

      else
        alert( 'Un popup blocker ha impedito l\'apertura dell\'immagine ingrandita.' );
    }

    hZoomedImage.src = uriThumbnailImage;
    return true;
  }



  function GF_TextAreaSetMaxLength( hTextArea, iMaxLength )
  {
    if ( hTextArea.value.length > iMaxLength )
    {
      hTextArea.value = hTextArea.value.substring( 0, iMaxLength );
      alert( 'Attenzione: il testo è limitato a ' + iMaxLength + ' caratteri.' );

      hTextArea.focus( );
      return false;
    }

    return true;
  }


  function GF_MakeFastCallRequest( strUrl, strPostData, fCallBack )
  {
    var hHttpRequest = null;

    if( window.XMLHttpRequest )
    {
      try
      {
        hHttpRequest = new XMLHttpRequest( );
      }

      catch( oErr )
      {
        hHttpRequest = null;
      }
    }

    else if( window.ActiveXObject )
    {
      var aStrMsxhtml = new Array( 'Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0', 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP' );

      for ( var i = 0; i < aStrMsxhtml.length && !hHttpRequest; i++ )
      {
        try
        {
          hHttpRequest = new ActiveXObject( aStrMsxhtml[i] );
        }

        catch( oErr )
        {
          hHttpRequest = null;
        }
      }
    }

    if ( hHttpRequest != null )
    {
      hHttpRequest.onreadystatechange = function( )
      {
        if ( hHttpRequest.readyState == 4 )
          fCallBack(hHttpRequest.status == 200 && hHttpRequest.responseXML ? hHttpRequest.responseXML : null );
      }

      hHttpRequest.open( strPostData == null ? 'GET' : 'POST', strUrl, true );

      if ( strPostData != null )
      {
        hHttpRequest.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' );
        hHttpRequest.setRequestHeader( 'Content-Length', strPostData.length );
      }

      hHttpRequest.setRequestHeader( 'If-Modified-Since', 'Thu, 1 Jan 1970 00:00:00 GMT' );
      hHttpRequest.send( strPostData );

      return true;
    }

    fCallBack( null );
    return false;
  }



  function GF_ShowAdvancedSearch( hForm, strSearchElement, strSimpleSearchText, strAdvancedSearchText )
  {
    if ( document.getElementById( strSearchElement ).className == 'topNascosto' )
    {
      document.getElementById( strSearchElement + 'Enabler' ).innerHTML = strSimpleSearchText;
      document.getElementById( strSearchElement ).className = '';
    }

    else
    {
      document.getElementById( strSearchElement + 'Enabler' ).innerHTML = strAdvancedSearchText;
      document.getElementById( strSearchElement ).className = 'topNascosto';

      for ( var i = 0; i < hForm.elements.length; i++ )
      {
        if ( hForm.elements[i].type.toLowerCase() == 'select-one' )
          hForm.elements[i].selectedIndex = 0;
      }
    }

    return true;
  }


  function GF_HiLightStars( hForm, hVote, iStars )
  {
    for ( var i = 0; i < hForm.elements[ hVote.name ].length; i++ )
      hForm.elements[ hVote.name ][ i ].className = i < iStars ? 'stella2' : 'stella';

    return true;
  }


  function GF_ExpandText( strMainElement, hClickElement, strExpand, strCompress )
  {
    var hContainer = document.getElementById( strMainElement + 'Container' );
    var hBlender = document.getElementById( strMainElement + 'Blender' );

    if ( hContainer.className == 'contenuto' )
    {
      hContainer.className = 'contenuto2';
      hBlender.className = 'trans2';
      
      hClickElement.innerHTML = strCompress;
    }

    else
    {
      hContainer.className = 'contenuto';
      hBlender.className = 'trans';

      hClickElement.innerHTML = strExpand;
    }

    return true;
  }



  function GF_AnswerComment( iRootCommentId )
  {
    var aDivs = document.getElementsByTagName( 'div' );

    for ( var i = 0; i < aDivs.length; i++ )
    {
      if ( aDivs[i].className == 'risposta' )
      {
        if ( aDivs[i].id == 'GF_AnswerForm_iCommentId_' + iRootCommentId )
        {
          if ( aDivs[i].style.display == 'none' )
            aDivs[i].style.display = '';

          else
            aDivs[i].style.display = 'none';

        }

        else if ( aDivs[i].id != 'GF_AnswerForm_Root' )
          aDivs[i].style.display = 'none';
      }
    }

    return true;
  }



  function GF_LimitCommentLength( hTextArea, iMaxLength )
  {
    if ( hTextArea.value.length > iMaxLength )
    {
      hTextArea.value = hTextArea.value.substring( 0, iMaxLength );

      alert( 'Attezione: il testo del commento è limitato a ' + iMaxLength + ' caratteri.' );
      return false;
    }

    return true;
  }




  function GF_ToggleAnswers( iRootCommentId )
  {
    var hComment = document.getElementById( 'GF_Answers_iCommentId_' + iRootCommentId );
    var hToggler = document.getElementById( 'GF_AnswerToogler_iCommentId_' + iRootCommentId );

    if ( hComment.style.display == 'none' )
    {
      hComment.style.display = '';
      hToggler.className = 'comp';
    }

    else
    {
      hComment.style.display = 'none';
      hToggler.className = 'esp';
    }

    return true;
  }



  function GF_CheckSearchFilm( hForm, strTitle, iGenreId, iNationalityId, iSearchMethod, iPage )
  {
    hForm.elements[ strTitle ].value = GF_CleanString( hForm.elements[ strTitle ].value );

    if ( hForm.elements[ strTitle ].value != '' )
      return true;


    alert( 'Si prega di inserire il titolo del film da cercare.')
    return false;
  }


  function GF_CheckSearchCharacter( hForm, strName, iSearchMethod, iPage )
  {
    hForm.elements[ strName ].value = GF_CleanString( hForm.elements[ strName ].value );

    if ( hForm.elements[ strName ].value != '' )
      return true;


    alert( 'Si prega di inserire il nome del personaggio da cercare.')
    return false;
  }



  function GF_CheckComment( hForm, strAuthor, iAuthorMaxLen, uriAuthor, iUriAuthorMaxLen, reValidUriAuthor, strTitle, iTitleMaxLen, strPreference, strComment, iCommentMaxLen )
  {
    if ( hForm )
    {
      var strErrors = '';

      hForm.elements[ strAuthor ].value = GF_CleanString( hForm.elements[ strAuthor ].value );
      hForm.elements[ uriAuthor ].value = GF_CleanString( hForm.elements[ uriAuthor ].value );
      hForm.elements[ strTitle ].value = GF_CleanString( hForm.elements[ strTitle ].value );
      hForm.elements[ strComment ].value = GF_CleanString( hForm.elements[ strComment ].value );


      if ( hForm.elements[ strAuthor ].value == '' )
        strErrors += '  - inserisci il tuo nome\n';
      else if ( hForm.elements[strAuthor].value.length > iAuthorMaxLen )
        strErrors += '  - il nome che hai inserito risulta troppo lungo\n';


      if ( hForm.elements[ uriAuthor ].value != '' && !/^http(s)?:\/\//i.test(hForm.elements[ uriAuthor ].value) )
        hForm.elements[ uriAuthor ].value = 'http://' + hForm.elements[ uriAuthor ].value.substring( 0, iUriAuthorMaxLen );
      if ( hForm.elements[ uriAuthor ].value.length > iUriAuthorMaxLen )
        strErrors += '  - la home page che hai inserito è troppo lunga\n';
      else if ( hForm.elements[ uriAuthor ].value != '' && !reValidUriAuthor.test(hForm.elements[ uriAuthor ].value) )
        strErrors += '  - la home page che hai inserito non è valida\n';


      if ( hForm.elements[ strTitle ].value == '' )
        strErrors += '  - inserisci il titolo della risposta\n';
      else if ( hForm.elements[ strTitle ].value.length > iTitleMaxLen )
        strErrors += '  - il titolo della risposta che hai inserito è troppo lungo\n';


      if ( !hForm.elements[strPreference][0].checked && !hForm.elements[strPreference][1].checked && !hForm.elements[strPreference][2].checked )
        strErrors += '  - non hai espresso alcun giudizio\n';


      if ( hForm.elements[ strComment ].value == '' )
        strErrors += '  - inserisci il commento\n';
      else if ( hForm.elements[strComment].value.length > iCommentMaxLen )
        strErrors += '  - il commento che hai inserito è troppo lungo\n';


      if ( strErrors == '' )
        return true;

      else
        alert( 'I seguenti campi non sono stati riempiti in modo corretto:\n' + strErrors + '\nSei pregato di correggerli.' );
    }


    return false;
  }




  function GF_VoteFilm( hForm, strFastCall, strMode, strFilmId, strVote, iVote )
  {
    if ( !window.bServerRequest )
    {
      window.bServerRequest = true;

      document.getElementById( 'GF_LoadingBackground' ).className = 'loading';
      document.getElementById( 'GF_LoadingForeground' ).className = 'loader';

      setTimeout( function(){ GF_MakeFastCallRequest( hForm.action, strFastCall + '=' + strFastCall + '&' + strMode + '=' + encodeURIComponent(hForm.elements[strMode].value) + '&' + strFilmId + '=' + encodeURIComponent(hForm.elements[strFilmId].value) + '&' + strVote + '=' + encodeURIComponent(iVote),
      function( xmlResponse )
      {
        window.bServerRequest = false;

        document.getElementById( 'GF_LoadingBackground' ).className = 'hidden';
        document.getElementById( 'GF_LoadingForeground' ).className = 'hidden';

        if ( xmlResponse )
        {
          var hRoot = xmlResponse.documentElement;
          var iCode = hRoot.getElementsByTagName('code')[0].firstChild.nodeValue;
          var strMessage = hRoot.getElementsByTagName('message')[0].firstChild.nodeValue;


          if ( iCode == 0 && hRoot.getElementsByTagName('data').length > 0 )
          {
            var hData = hRoot.getElementsByTagName('data')[0];

            var iVotes = hData.getElementsByTagName('iVotes')[0].firstChild.nodeValue;
            var iAverageVote = hData.getElementsByTagName('iAverageVote')[0].firstChild.nodeValue;


            document.getElementById( 'GF_FilmVotesContainer').style.display = '';

            document.getElementById( 'GF_FilmVotes').innerHTML = iVotes;
            document.getElementById( 'GF_FilmAverageVote').innerHTML = iAverageVote;
            document.getElementById( 'GF_FilmVoteForm' ).innerHTML = '<div class="bgStelle"><div style="width: ' + Math.round( parseFloat(iAverageVote)/10*170 ) + 'px;"></div></div>';
          }

          else
            alert( strMessage );
        }

        else
          alert( 'Errore: il server ha risposto in modo inatteso.' );

      }
      ) }, 1500 );

    }

    return false;
  }




  function GF_VoteReview( hForm, strFastCall, strMode, strFilmId, strVote, iVote )
  {
    if ( !window.bServerRequest )
    {
      window.bServerRequest = true;

      document.getElementById( 'GF_LoadingBackground' ).className = 'loading';
      document.getElementById( 'GF_LoadingForeground' ).className = 'loader';

      setTimeout( function(){ GF_MakeFastCallRequest( hForm.action, strFastCall + '=' + strFastCall + '&' + strMode + '=' + encodeURIComponent(hForm.elements[strMode].value) + '&' + strFilmId + '=' + encodeURIComponent(hForm.elements[strFilmId].value) + '&' + strVote + '=' + encodeURIComponent(iVote),
      function( xmlResponse )
      {
        window.bServerRequest = false;

        document.getElementById( 'GF_LoadingBackground' ).className = 'hidden';
        document.getElementById( 'GF_LoadingForeground' ).className = 'hidden';

        if ( xmlResponse )
        {
          var hRoot = xmlResponse.documentElement;
          var iCode = hRoot.getElementsByTagName('code')[0].firstChild.nodeValue;
          var strMessage = hRoot.getElementsByTagName('message')[0].firstChild.nodeValue;

          if ( iCode == 0 && hRoot.getElementsByTagName('data').length > 0 )
          {
            var hData = hRoot.getElementsByTagName('data')[0];

            var iVotes = hData.getElementsByTagName('iVotes')[0].firstChild.nodeValue;
            var iAverageVote = hData.getElementsByTagName('iAverageVote')[0].firstChild.nodeValue;


            document.getElementById( 'GF_ReviewVotesContainer').style.display = '';

            document.getElementById( 'GF_ReviewVotes').innerHTML = iVotes;
            document.getElementById( 'GF_ReviewAverageVote').innerHTML = iAverageVote;

            setTimeout( "document.getElementById( 'GF_ReviewVoteForm' ).style.display = 'none';", 5000 );
          }

          document.getElementById( 'GF_ReviewVoteForm' ).innerHTML = strMessage;
        }

        else
          alert( 'Errore: il server ha risposto in modo inatteso.' );

      }
      ) }, 1500 );

    }

    return false;
  }




  function GF_VoteCharacter( hForm, strFastCall, strMode, strCharacterId, strVote, iVote )
  {
    if ( !window.bServerRequest )
    {
      window.bServerRequest = true;

      document.getElementById( 'GF_LoadingBackground' ).className = 'loading';
      document.getElementById( 'GF_LoadingForeground' ).className = 'loader';

      setTimeout( function(){ GF_MakeFastCallRequest( hForm.action, strFastCall + '=' + strFastCall + '&' + strMode + '=' + encodeURIComponent(hForm.elements[strMode].value) + '&' + strCharacterId + '=' + encodeURIComponent(hForm.elements[strCharacterId].value) + '&' + strVote + '=' + encodeURIComponent(iVote),
      function( xmlResponse )
      {
        window.bServerRequest = false;

        document.getElementById( 'GF_LoadingBackground' ).className = 'hidden';
        document.getElementById( 'GF_LoadingForeground' ).className = 'hidden';

        if ( xmlResponse )
        {
          var hRoot = xmlResponse.documentElement;
          var iCode = hRoot.getElementsByTagName('code')[0].firstChild.nodeValue;
          var strMessage = hRoot.getElementsByTagName('message')[0].firstChild.nodeValue;

          if ( iCode == 0 && hRoot.getElementsByTagName('data').length > 0 )
          {
            var hData = hRoot.getElementsByTagName('data')[0];

            var iVotes = hData.getElementsByTagName('iVotes')[0].firstChild.nodeValue;
            var iAverageVote = hData.getElementsByTagName('iAverageVote')[0].firstChild.nodeValue;


            document.getElementById( 'GF_CharacterVotesContainer').style.display = '';

            document.getElementById( 'GF_CharacterVotes').innerHTML = iVotes;
            document.getElementById( 'GF_CharacterAverageVote').innerHTML = iAverageVote;
            document.getElementById( 'GF_CharacterVoteForm' ).innerHTML = '<div class="bgStelle"><div style="width: ' + Math.round( parseFloat(iAverageVote)/10*170 ) + 'px;"></div></div>';
          }

          else
            alert( strMessage );
        }

        else
          alert( 'Errore: il server ha risposto in modo inatteso.' );

      }
      ) }, 1500 );

    }

    return false;
  }
