function showHide (param)  {
  if (document.getElementById(param).style.display == 'none')
  document.getElementById(param).style.display = '';
  else document.getElementById(param).style.display = 'none';
  return true;
}

function setCookie(name, value) {
  var expdate = new Date();
  expdate.setTime(expdate.getTime() + (1000*60*60*24*365));
  var argv = setCookie.arguments;
  var argc = setCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : expdate;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;

  document.cookie = name + "=" + escape(value) +
    ((expires == null) ? "" : ("; expires=" +
      expires.toGMTString())) +
    ((path == null) ? "" : ("; path=" + path)) +
    ((domain == null) ? "" : ("; domain=" + domain)) +
    ((secure == true) ? "; secure" : "");
}

function getCookie(name) {
  var p = name + "=";
  var si = document.cookie.indexOf(p);
  if (si == -1) return null;
  var ei = document.cookie.indexOf(";", si + p.length);
  if (ei == -1) ei = document.cookie.length;
  return unescape(document.cookie.substring(si + p.length, ei));
}

function doLoad(URL, data) {
  data.is_ajax = true;
  if (URL == null || URL == undefined || URL == false || URL == '') URL = location.href;
  $.ajax({
    "cache": false,
    "data": (data),
    "dataType": "json",
    "type": "post",
    "url": (URL),
    "success": function(data, textStatus) {
      $.each(data, function(k, val) {
        if (k != 'js') {
          $(k).html(val).decHTMLifEnc();
          $(k).val(val);
        }
      });
      if (data.js) eval(data.js);
    }
  });
}

function doLoadForm(form) {
  form.ajaxSubmit({
    "success": function (data) {
      $.each(data, function(k, val) {
        if (k != 'js') $(k).html(val).decHTMLifEnc();
      });
      if (data.js) eval(data.js);
    },
    "dataType": "json",
    "type": "post",
    "data": {"is_ajax": "1"}
  });
}

$(document).ready(function() {
  // добавляем окошко загрузки
  $('body').append('<div id="loading">Загрузка...</div>');
  $('body').append('<pre id="debug"></div>');
  // показать при загрузке ajax-контента
  $("#loading").bind("ajaxSend", function(){
    $(this).fadeIn('fast');
  }).bind("ajaxComplete", function(){
    $(this).fadeOut('fast');
  });
  //datepicker
  $(".date").datepicker({
    "dateFormat": "yy-mm-dd",
    "changeMonth": true,
    "changeYear": true,
    "firstDay": 1
  });
});

function getAbsolutePos(el) {
  var r = { x: el.offsetLeft, y: el.offsetTop };
  if (el.offsetParent) {
    var tmp = getAbsolutePos(el.offsetParent);
    r.x += tmp.x;
    r.y += tmp.y;
  }
  return r;
}

function getMousePos(event) { 
  var x = y = 0; 
  if (document.attachEvent != null) { // Internet Explorer & Opera 
    x = window.event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft); 
    y = window.event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); 
  } else if (!document.attachEvent && document.addEventListener) { // Gecko 
    x = event.clientX + window.scrollX; 
    y = event.clientY + window.scrollY; 
  } else { 
  } 
  return {x:x, y:y};
}

// по окончанию набора в поле
var timeouts = new Array();

function onInputEnd(e, callback, timeout, timeouted) {
  if (!timeout) timeout = 1000;
  if (!timeouted) {
    if (timeouts[e.target.id]) clearTimeout(timeouts[e.target.id]);
    timeouts[e.target.id] = setTimeout(function() { onInputEnd(e, callback, timeout, true) }, timeout);
  } else {
    callback();
  }
}

// скрывать уведомления по прошествию 3 сек
$(function() {
  var last_err_inf_cont = null;
  var err_inf_counter = 0;
  setInterval(function() {
    if (($('#err_inf').html() != last_err_inf_cont || err_inf_counter > 0)
    && $('#err_inf > div:visible').length > 0) err_inf_counter++;
    if (err_inf_counter > 3) {
      $('#err_inf > div').fadeOut(200);
      err_inf_counter = 0;
    }
    last_err_inf_cont = $('#err_inf').html();
  }, 1000);
});


document.onkeydown = documentOnKeyDown;
function documentOnKeyDown(e) {
  if (!e) e = window.event;
  var k = e.keyCode;
  if (k == 27) {
    if ($('.popupWndBg:visible').length > 0) $('.popupWndBg, .popupWndCont').fadeOut(200);
  }
}
