$(document).ready(
  function ()
  {
    $('#searchNewsForm').submit(
      function ()
      {
        var jKeyWord = $('#newsKeyWord');

        if (jKeyWord.val() == tDefaultKeyWord)
        {
          alert(tPleaseEnterKeyWord);

          jKeyWord[0].focus();

          return false;
        }

        $('#page_div_main').html($('#loadingPleaseWait').html());

        return true;
      }
    );

    $('#newsKeyWord').focus(
      function ()
      {
        if (this.value == tDefaultKeyWord)
          this.value = '';
      }
    ).blur(
      function ()
      {
        if (this.value == '')
          this.value = tDefaultKeyWord;
      }
    );

    $('#searchProductForm').submit(
      function ()
      {
        var jKeyWord = $('#productKeyWord');

        if (jKeyWord.val() == tDefaultKeyWord)
        {
          alert(tPleaseEnterKeyWord);

          jKeyWord[0].focus();

          return false;
        }

        $('#page_div_main').html($('#loadingPleaseWait').html());

        return true;
      }
    );

    $('#productKeyWord').focus(
      function ()
      {
        if (this.value == tDefaultKeyWord)
          this.value = '';
      }
    ).blur(
      function ()
      {
        if (this.value == '')
          this.value = tDefaultKeyWord;
      }
    );

    $('#quickOrderForm').submit(
      function ()
      {
        var jKeyWord = $('#quickKeyWord');

        if (jKeyWord.val() == tArticleNumber)
        {
          alert(tPleaseEnterQuick);

          jKeyWord[0].focus();

          return false;
        }

        $('#page_div_main').html($('#loadingPleaseWait').html());

        return true;
      }
    );

    $('#quickKeyWord').focus(
      function ()
      {
        if (this.value == tArticleNumber)
          this.value = '';
      }
    ).blur(
      function ()
      {
        if (this.value == '')
          this.value = tArticleNumber;
      }
    );

    $('#newPopUpClose').click(
      function ()
      {
        if ($(this).hasClass('inactive'))
          return;

        closeModal();
      }
    );

    $('#menu A').click(
      function ()
      {
        if (this.href.substr(0,11) != 'javascript:')
          $('#page_div_main').html($('#loadingPleaseWait').html());
      }
    );
  }
);

var recordIsModified        = false;
var stickyRecordIsModified  = false;

function modifyRecord ()
{
  if (!recordIsModified)
  {
    recordIsModified        = true;
    stickyRecordIsModified  = true;
    oa_setTitle('newView',oa_getTitle('newView') + ' *');
  }
}

function unmodifyRecord ()
{
  if (recordIsModified)
  {
    recordIsModified  = false;
    var oldTitle      = oa_getTitle('newView');

    if (oldTitle !== null)
      oa_setTitle('newView',oldTitle.substr(0,oldTitle.length - 2));
  }
}

function closeNewView ()
{
  oa_closePopupByCode('newView');

  if (refreshURL)
    $('#page_div_main').html($('#loadingPleaseWait').html()).load(refreshURL);
}

function saveTenderModifications (doChecks)
{
  if (doChecks && recordCanSave && !recordIsNew && !checkForms())
    return false;

  if (recordIsModified)
  {
    if (doChecks && !checkForms())
      return false;

    saveTender($('#tenderSave'),closeNewView);

    return false;
  }

  if (stickyRecordIsModified && refreshURL)
    $('#page_div_main').html($('#loadingPleaseWait').html()).load(refreshURL);

  return true;
}

function createTender (title)
{
  var jWindow = $(window);
  var width   = 787,
      height  = jWindow.height() - 70,
      padding = 10;
  var jView   = oa_openPopup(
                  {
                    'code':     'newView',
                    'title':    title,
                    'modal':    true,
                    'width':    width,
                    'height':   height,
                    'left':     (jWindow.width() - (padding + width + padding)) / 2,
                    'top':      (jWindow.height() - (padding + height + padding)) / 2 - 20,
                    'padding':  padding,
                    'onClose':
                      function ()
                      {
                        saveTenderModifications(false);
                        return true;
                      }
                  }
                );

  oa_setPopupContentFromURL(jView,'/tenderCreate.php',{});
}

function modifyTender (tenderId,title)
{
  var jWindow = $(window);
  var width   = 787,
      height  = jWindow.height() - 70,
      padding = 10;
  var jView   = oa_openPopup(
                  {
                    'code':     'newView',
                    'title':    title,
                    'modal':    true,
                    'width':    width,
                    'height':   height,
                    'left':     (jWindow.width() - (padding + width + padding)) / 2,
                    'top':      (jWindow.height() - (padding + height + padding)) / 2 - 20,
                    'padding':  padding,
                    'onClose':
                      function ()
                      {
                        saveTenderModifications(false);
                        return true;
                      }
                  }
                );

  oa_setPopupContentFromURL(jView,'/tenderModify.php',{'id':tenderId});
}

function saveOrderModifications (doChecks)
{
  if (doChecks && recordCanSave && !recordIsNew && !checkForms())
    return false;

  if (recordIsModified)
  {
    if (doChecks && !checkForms())
      return false;

    saveOrder($('#orderSave'),closeNewView);

    return false;
  }

  if (stickyRecordIsModified && refreshURL)
    $('#page_div_main').html($('#loadingPleaseWait').html()).load(refreshURL);

  return true;
}

function createOrder (title)
{
  var jWindow = $(window);
  var width   = 787,
      height  = jWindow.height() - 70,
      padding = 10;
  var jView   = oa_openPopup(
                  {
                    'code':     'newView',
                    'title':    title,
                    'modal':    true,
                    'width':    width,
                    'height':   height,
                    'left':     (jWindow.width() - (padding + width + padding)) / 2,
                    'top':      (jWindow.height() - (padding + height + padding)) / 2 - 20,
                    'padding':  padding,
                    'onClose':
                      function ()
                      {
                        saveOrderModifications(false);
                        return true;
                      }
                  }
                );

  oa_setPopupContentFromURL(jView,'/orderCreate.php',{});
}

function modifyOrder (orderId,title)
{
  var jWindow = $(window);
  var width   = 787,
      height  = jWindow.height() - 70,
      padding = 10;
  var jView   = oa_openPopup(
                  {
                    'code':     'newView',
                    'title':    title,
                    'modal':    true,
                    'width':    width,
                    'height':   height,
                    'left':     (jWindow.width() - (padding + width + padding)) / 2,
                    'top':      (jWindow.height() - (padding + height + padding)) / 2 - 20,
                    'padding':  padding,
                    'onClose':
                      function ()
                      {
                        saveOrderModifications(false);
                        return true;
                      }
                  }
                );

  oa_setPopupContentFromURL(jView,'/orderModify.php',{'id':orderId});
}

function saveFavoriteModifications (doChecks)
{
  if (doChecks && recordCanSave && !recordIsNew && !checkForms())
    return false;

  if (recordIsModified)
  {
    if (doChecks && !checkForms())
      return false;

    saveFavorite($('#favoriteSave'),closeNewView);

    return false;
  }

  if (stickyRecordIsModified && refreshURL)
    $('#page_div_main').html($('#loadingPleaseWait').html()).load(refreshURL);

  return true;
}

function createFavorite (title)
{
  var jWindow = $(window);
  var width   = 787,
      height  = jWindow.height() - 70,
      padding = 10;
  var jView   = oa_openPopup(
                  {
                    'code':     'newView',
                    'title':    title,
                    'modal':    true,
                    'width':    width,
                    'height':   height,
                    'left':     (jWindow.width() - (padding + width + padding)) / 2,
                    'top':      (jWindow.height() - (padding + height + padding)) / 2 - 20,
                    'padding':  padding,
                    'onClose':
                      function ()
                      {
                        saveFavoriteModifications(false);
                        return true;
                      }
                  }
                );

  oa_setPopupContentFromURL(jView,'/favoriteCreate.php',{});
}

function modifyFavorite (favoriteId,title)
{
  var jWindow = $(window);
  var width   = 787,
      height  = jWindow.height() - 70,
      padding = 10;
  var jView   = oa_openPopup(
                  {
                    'code':     'newView',
                    'title':    title,
                    'modal':    true,
                    'width':    width,
                    'height':   height,
                    'left':     (jWindow.width() - (padding + width + padding)) / 2,
                    'top':      (jWindow.height() - (padding + height + padding)) / 2 - 20,
                    'padding':  padding,
                    'onClose':
                      function ()
                      {
                        saveFavoriteModifications(false);
                        return true;
                      }
                  }
                );

  oa_setPopupContentFromURL(jView,'/favoriteModify.php',{'id':favoriteId});
}
