var effectTime = 300;

getData = function (id, url) {
//   if (!$('#'+id)) return false;
  $('#spinner').slideDown('slow');
  var target = (id) ? $('#'+id) : '';
  if (target && target.attr('display') == 'block') target.animate({'opacity': 'toggle'}, effectTime);
  var responseText = $.ajax({
      url: 'exec/'+url,
      async: false
    }).responseText;
  if (target) target.html(responseText);
//   .evalScripts();
  if (target && target.attr('opacity') == 0.0) target.animate({'opacity': 'toggle'}, effectTime);
  $('#spinner').slideUp('slow');
  return (target) ? true : responseText;
}
slideView = function (listID, viewID, url) {
  if (!$('#'+viewID)) return false;
  if (url) getData(viewID, url);
  if ($('#'+listID)) $('#'+listID).slideUp(effectTime);
  $('#'+viewID).slideDown(effectTime);
}

systemConfirm = function (alertID) {
  $('#confirmBox').html( $('#'+alertID).html() );
  $('#systemAlertOverlay').css({'height':$(document).height()+'px', 'opacity': 0.6});
  $('#systemAlertOverlay').fadeIn(effectTime);
//   $('#systemAlertOverlay').animate({'opacity': 0.5,'display': 'block'}, effectTime);
  var h = ($(document).height()-$('#confirmBoxBox').height())/2+'px';
  $('#systemConfirm').css({'top': h});
  $('#systemConfirm').fadeIn(effectTime);
}
var systemAlert = function (txt) {
  $('#alertMsg').html(txt);
  $('#systemAlertOverlay').css({'height':$(document).height()+'px', 'opacity': 0.6});
  $('#systemAlertOverlay').fadeIn(effectTime);
  var h = ($(document).height()-$('#alertBox').height())/2+'px';
  $('#systemAlert').css({'top': h});
  $('#systemAlert').fadeIn(effectTime);
}
systemAlertHide = function () {
  $('#systemAlertOverlay').fadeOut(effectTime);
  $('#systemAlert').fadeOut(effectTime);
}
systemConfirmHide = function () {
  $('#systemAlertOverlay').fadeOut(effectTime);
  $('#systemConfirm').fadeOut(effectTime);
}
function usrMsg (_txt) {
  $('#usrMsg').html(_txt);
  $('#usrMsg').slideDown(effectTime);
  setTimeout("$('#usrMsg').slideUp(effectTime);", 4000);
}
saveForm = function (formID, respTarget) {
  var oForm = $('#'+formID);
  oForm.attr('disabled', true);
  var sUrl; 
  if(!(sUrl = 'exec/'+oForm.attr('action')))
      sUrl = location.href;
  if (oForm.attr('method').toLowerCase() == 'get')
  $.get(sUrl, oForm.serialize(), function(sResponse) {
    $('#'+respTarget).html(sResponse);
  });
  else
  $.post(sUrl, oForm.serialize(), function(sResponse) {
    $('#'+respTarget).html(sResponse);
  });
  return false;
}
Go = function (url) {
  location.href = url;
}
checkForm = function (formID) {
var result = '';
var oForm = (formID) ? '#'+formID+' .req' : '.req';
  $(oForm).each(function () {
    if (!$(this).val() && !$(this).html()) {
      result += "<li>"+$('#l_'+this.id).html()+"</li>";
      $('#l_'+this.id).css('font-weight', 'bold');
    }
  });
    if (result) {
        systemAlert('<h2 class="small">Proszę uzupełnić brakujące dane:</h2><ul class="aList">'+result+'</ul>');
        return false;
    }
    return true;
  }
tooltip = function() {
 var xOffset = 40;
 var yOffset = 20;
  $('.tTip').each(function () {
    var tTipID = 't'+$(this).attr('id');
    $(this).hover(function(e) {
      $('#'+tTipID).addClass('tip');
      $('#'+tTipID)
        .css('top',(e.pageY + yOffset) + 'px')
        .css('left',(e.pageX + xOffset) + 'px')
        .fadeIn('fast');		
      },
      function() {
        $('#'+tTipID).fadeOut('fast');
    });
    $(this).mousemove(function(e) {
      if (e.pageX + xOffset + $('#'+tTipID).width() < $(window).width()-20)
      $('#'+tTipID)
        .css('left',(e.pageX + xOffset) + 'px');
      if (e.pageY + yOffset + $('#'+tTipID).height() < $(window).height()-20)
      $('#'+tTipID)
        .css('top',(e.pageY + yOffset) + 'px')
    });
  });
}
function checkEmail(str) {
  var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i ;
  if (filter.test(str))
    return true;
  else return false;
  }

goNextPage = function (direction, pageNo) {
  var _link = location.pathname+'?';
  var urlterms = location.search.replace('?', '').split('&');
  
//          alert(location.search);
  var page = 0;
  var Direction = (direction) ? -1 : 1;
  
  for (i=0 ; i < urlterms.length;i++) {
          _var = urlterms[i].split('=');
  //         alert(urlterms[i]);
          if (_var[0] == 'Page') {
                  page = (pageNo) ? pageNo : parseInt(_var[1])+parseInt(Direction);
                  urlterms[i] = '';
                  continue;
                  }
          else if (urlterms[i] != '') _link += urlterms[i]+'&';
          }
          
          if (page > 0) _link += 'Page='+page;
          else { 
            if (pageNo) _link += 'Page='+pageNo;
            else _link += 'Page=2';
          }
  Go(_link);
}

