// JavaScript Document

var newImg = new Image();
var captchaId = 'captcha';

function toggleElements( elShow, elHide, classShow, classHide )
{
  try
  {
    if ( classShow )
    {
      jQuery( classShow ).hide();
    }

    if ( classHide )
    {
      jQuery( classHide ).show();
    }

    if ( elShow )
    {
      jQuery( elShow ).show();
    }

    if ( elHide )
    {
      jQuery( elHide ).hide();
    }
  }
  catch ( e ) {}
}

function refreshCaptcha( id, nr )
{
  try
  {
    if ( id )
    {
      captchaId = id;
    }

    if ( nr )
    {
      nr = parseInt( nr );
    }
    else
    {
      nr = '';
    }

    var key = Math.round( Math.random() * 32000 );
    newImg.src = "/img/captcha" + nr + ".php?" + key;
    this.setTimeout( 'changeCaptcha()', 1000 );
  }
  catch ( e ) {}
}

function changeCaptcha()
{
  try
  {
    // TODO: replace with jQuery-call
    document.getElementById( captchaId ).src = newImg.src;
  }
  catch ( e ) {}
}

function setFormAction( el, formaction )
{
  try
  {
//    alert( formaction );
    jQuery( el ).parents( 'form' )
      .attr( 'action', formaction )
      .removeClass( 'validate' )
      .unbind( 'submit' );
  }
  catch ( e ) {
//    alert( e );
  }
}

function addEmo( myId, myText )
{
  try
  {
    var txtarea = document.getElementById( myId );
    myText = ' ' + myText + ' ';

    // IE support
    if ( document.selection )
    {
      txtarea.focus();
      sel = document.selection.createRange();
      sel.text = myText;
    }
    // MOZILLA/NETSCAPE support
    else if ( typeof( txtarea.selectionStart ) == 'number' )
    {
      var startPos = txtarea.selectionStart;
      var endPos = txtarea.selectionEnd;
      txtarea.value = txtarea.value.substring( 0, startPos )
                    + myText
                    + txtarea.value.substring( endPos, txtarea.value.length );
      txtarea.focus();
    }
    else if ( txtarea.createTextRange && txtarea.caretPos )
    {
      var caretPos = txtarea.caretPos;
      caretPos.text = caretPos.text.charAt( caretPos.text.length - 1 ) == ' ' ? myText + ' ' : myText;
      txtarea.focus();
    }
    else
    {
      txtarea.focus();
      txtarea.value += myText;
    }
  }
  catch ( e ) {}
}

function toggleCheckBoxes ( all, boxObj )
{
  try
  {
    var stillOneChecked = false;
    var oneIsChecked = false;
    var allIsChecked = false;

    jQuery( '.search-where' ).each(
      function( i )
      {
        if ( this.checked )
        {
          oneIsChecked = true;

          if ( this.id == 'SearchAll' )
          {
            allIsChecked = true;
          }

          return false;
        }
      }
    )

    if ( all )
    {
      if ( allIsChecked )
      {
        jQuery( '.search-where' ).each(
          function( i )
          {
            if ( this.id != 'SearchAll' )
            {
              this.checked = false;
            }
          }
        )
      }
    }
    else
    {
      jQuery( 'input#SearchAll' ).attr( 'checked', false );
    }

    if ( !oneIsChecked )
    {
      jQuery( 'input#SearchAll' ).attr( 'checked', true );
    }
  }
  catch ( e ) {}
}

function alignDivPopup ( id ) {

  try
  {
    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;

    position = 'left: ' + alignLeft + 'px; top: ' + alignTop + 'px;';

    jQuery( id ).attr( 'style', position );
  }
  catch ( e ) {}
}

function ajaxLoader( id )
{
  try
  {
    alignDivPopup( id );
    jQuery( id ).show();
  }
  catch ( e ) {}
}

function submitForm( elem )
{
  try
  {
    var frm = jQuery( elem ).parents( 'form' );

    if ( frm )
    {
      frm.submit();
    }
  }
  catch ( e ) {}

  return false;
}

jQuery( document ).ready( function()
{
  if ( jQuery().elastic )
  {
    jQuery( 'textarea.elastic' ).elastic();
  }

  if ( jQuery().validate )
  {
    jQuery( 'form.validate' ).each( function()
    {
      jQuery( this ).validate( {
        errorPlacement: function( error, elem )
        {
          try
          {
            var elemNext = elem.next();

            if ( elemNext.hasClass( 'tooltip' ) ||
                 ( elem.is( 'input[type=checkbox]' ) && elemNext.is( 'label' ) ) )
            {
              error.insertAfter( elemNext );
            }
//            else if ( elemNext.is( 'select' ) )
//            {
//            }
            else
            {
              if ( elem.is( 'textarea' ) )
              {
                error.addClass( 'error-textarea' );
              }

              error.insertAfter( elem );
            }
          } catch ( e ) {}
        }
      } );
    } );
  }
} );

