function addLoadEvent(func) 
{
  var oldonload = window.onload;
  
  if (typeof window.onload != 'function') 
  {
    window.onload = func;
	}
	else
	{
    window.onload = function() 
    {
      if (oldonload) { oldonload(); }
      func();
    };
  }
}

function addScrollEvent(func)
{
  var oldonscroll = window.onscroll;
  
  if (typeof window.onscroll != 'function') 
  {
    window.onscroll = func;
	}
	else
	{
    window.onscroll = function() 
    {
      if (oldonscroll) { oldonscroll(); }
      func();
    };
  }
}

function byID(id) 
{
	return document.getElementById(id);
}

function show(id) 
{
	if (byID(id)) { byID(id).style.display = ''; }
}

function hide(id) 
{
	if (byID(id)) { byID(id).style.display = 'none'; }
}

function enable(id) 
{
	if (byID(id)) { byID(id).disabled = false; }
}

function disable(id) 
{
	if (byID(id)) { byID(id).disabled = true; }
}

function newopt(val,text,selected)
{
  newOption = document.createElement('option');
	newOption.value = val;
	newOption.innerHTML = text;
	newOption.selected = selected;
	return newOption;
}

function clearSelect(id)
{
  disable(id);
  var sel = byID(id);
  var nodes = sel.childNodes;
  var len = nodes.length;
  for (var i=len-1; i>=0; i--) { sel.removeChild(nodes[i]); } 
  enable(id);
}

function setCookie(name, value, days)
{
  if (days) 
  {
    var date = new Date();
    date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    var expires = '; expires=' + date.toGMTString();
  } else { var expires = ''; }  
	document.cookie = name + '=' + value + expires + '; path=/';
}

function getCookie(name)
{
	name = name + '=';
	list = document.cookie.split(';');
	for (i = 0; i < list.length; i++)
	{
		c = list[i];
		while (c.charAt(0) == ' ') c = c.substring (1, c.length);
		if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
	}
	return false;
}

function trim(str)
{
  ok = false;
  while (!ok)
  {
    //ind_prob = str.indexOf(' ');
    t_char = str.substring(0,1);
    if (t_char == ' ') { str = str.substring(1); }
    else { ok = true; }
  }
  ok = false;
  while (!ok)
  {
    l = str.length;
    t_char = str.substring(l-1,l);
    if (t_char == ' ') { str = str.substring(0,l-1); }
    else { ok = true; }
  }
  return str;
}

function getMouseXY(e) {
   try {
      x = event.clientX + document.body.scrollLeft;
      y = event.clientY + document.body.scrollTop;
   } catch(except) {
      x = e.clientX + window.scrollX;
      y = e.clientY +  window.scrollY;
   }
   var out = new Array(x,y);
   return out;
}

function getWindowSize()
{
  if (window.innerHeight)
  {
    h = window.innerHeight;
    w = window.innerWidth;
  } 
  else
  {
    w = document.documentElement.clientWidth; // ширина окна пользователя
    h = document.documentElement.clientHeight; // высота окна пользователя
  }
  var ret = new Array(w,h);
  return ret;
}

function getWindowScroll()
{
  if (document.all)
  {
    scrollX = document.documentElement.scrollLeft;
    scrollY = document.documentElement.scrollTop;
  }
  else
  {
    scrollX = window.pageXOffset; // высота прокрутки документа
    scrollY = window.pageYOffset; // ширина прокрутки документа
  }
  var ret = new Array(scrollX,scrollY);
  return ret;
}

function displayObjectPr(object)
{
  var proprty_message = "";
  for (var property_name in object)
  {
    proprty_message += object + "." + property_name + " = " + object[property_name] + "\n<br />";
  }
  document.write(proprty_message);
}

function ch_color(id)
{
  var colorArr = new Array('#FF0000','#00FF00','#0000FF','#000000');
  var el = byID(id)
  if (el)
  {
    var curent_j = el.style.current_color;
    if (curent_j) { j = parseInt(curent_j); } else { j= 0; }
    l = colorArr.length - 1;
    if (j>l) { j = 0; }
    el.style.color = colorArr[j];
    el.style.current_color = j+1;
  }
  setTimeout("ch_color('"+id+"')",1000);
}

function submitForm(idForm)
{
  var elForm = byID(idForm);
  if (elForm)
  {
    elForm.submit();
    return true;
  } else { return false; }
}

function preloadimg(src)
{
  im = new Image(); 
  im.src = src;
}

function rand(min,max)
{
	var randomNum = Math.random() * (max-min);
	return(Math.round(randomNum) + min);
}

function reloadImg(obj)
{
  if (obj.src)
  {
    srcPath = obj.src;
    if (srcPath.indexOf('?') > -1)
    {
      srcPath = srcPath.substr(0,srcPath.indexOf('?'))
    }
    rnd = Math.random()*10;
    obj.src = srcPath + '?' + rnd;
  }
}

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

function getParamOfLocationHref(paramName)
{
  var tmpstr = window.location.search.replace('?','');
	paramName = paramName + '=';
	list = tmpstr.split('&');
	//alert(list);
	for (i=0; i<list.length; i++)
	{
		c = list[i];
		while (c.charAt(0) == ' ') c = c.substring (1, c.length);
		if (c.indexOf(paramName) == 0) return c.substring(paramName.length, c.length);
	}
	return false;
}

// ++++ CH_COLOR_2 ++++
var arr_blink = new Array();
var arr_blink_stac = new Array();
var arr16 = new Array('0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F');
var arrColor = new Array('#800000','#FF0000','#008000','#00FF00','#0000C0','#FFFF00','#00C0C0','#C000C0','#FF00FF','#FF8000','#008080','#8000FF');
function get_element_for_ch_color(classname)
{
  var i = arr_blink.length;
  $(document).find('.' + classname).each(function() {
    //alert($(this).html());
    arr_blink[i] = $(this);
    i++;
  });
}

function get_element_for_ch_color_stac(classname)
{
  //alert(classname);
  var i = arr_blink_stac.length;
  $(document).find('.' + classname).each(function() {
    //alert($(this).html());
    arr_blink_stac[i] = $(this);
    i++;
  });
}

function ch_color_v2()
{
  if (arr_blink.length > 0)
  {
    var clr = '#' + arr16[rand(0,15)] + arr16[rand(0,15)] + arr16[rand(0,15)] + arr16[rand(0,15)] + arr16[rand(0,15)] + arr16[rand(0,15)];
    for (i=0;i<arr_blink.length;i++)
    {                                                    
      arr_blink[i].css('color',clr);
    }
  }
  if (arr_blink_stac.length > 0)
  {
    //var clr_stac = arrColor[rand(0,arrColor.length-1)];
    for (i=0;i<arr_blink_stac.length;i++)
    {
      var clr_stac = arrColor[rand(0,arrColor.length-1)];
      arr_blink_stac[i].css('color',clr_stac);
    }
  }
  setTimeout("ch_color_v2()",1000);
}
// ---- CH_COLOR ----


function checkCorrectEmail(email)
{
  var tmp = email.match(/^[0-9a-z._-]+@[0-9a-z._-]+[.][0-9a-z]{2,7}$/i);
  if (tmp == null) { return false; }
  return true;	
}

function f_focus(id)
{
  if ($('#'+id).val() == $('#'+id).attr('defval')) 
  {
    $('#'+id).val(''); 
    //$('#'+id).css('color','#000000');
  }
}

function f_blur(id)
{
  if (trim($('#'+id).val()) == '') 
  {
    $('#'+id).val($('#'+id).attr('defval'));
    //$('#'+id).css('color','#808080');
  }
}

function addBookmark_v1(title, url) 
{
  if (!title) { title = "Бесплатное Порево"; }
  if (!url) { url = "http://besplatnoe-porevo.com/"; }
  if (document.all) { window.external.addFavorite(url, title); }
   else if (window.sidebar) { window.sidebar.addPanel(title, url, ""); }
    else if (window.opera && window.print) { return true; }
}

function addBookmark_v2(a)
{
/*
  if (!url) url = location.href;
  if (!title) title = document.title;
*/
 var url = window.document.location;
 var title = window.document.title;

  //Gecko
  if ((typeof window.sidebar == "object") && (typeof window.sidebar.addPanel == "function")) window.sidebar.addPanel(title, url, "");
  //IE4+
  else if (typeof window.external == "object") window.external.AddFavorite(url, title);
  //Opera7+
  else if (window.opera && document.createElement)
  {
//    var a = document.createElement('a');
    if (!a) return false; //IF Opera 6
    a.setAttribute('rel','sidebar');
    a.setAttribute('href',url);
    a.setAttribute('title',title);
  }
  else return false;
  return true;
}

function reloadCaptcha(id)
{
  var obj = byID(id);
  if (obj)
  {
    srcPath = obj.src;
    if (srcPath.indexOf('?') > -1)
    {
      srcPath = srcPath.substr(0,srcPath.indexOf('?'))
    }
    rnd = Math.random()*10;
    obj.src = srcPath + '?' + rnd;
  }
}

function defvalinp()
{
  $(document).find('input').each(function(){
    if ($(this).attr('defval')) 
    {
      //alert($(this).attr('defval'));
      $(this).val($(this).attr('defval'));
      $(this).addClass('defvalclass');
      
      $(this).focus(function(){
        if ($(this).val() == $(this).attr('defval')) 
        {
          $(this).val(''); 
          $(this).removeClass('defvalclass');
        }
        else
        {
          //$(this).addClass('defvalclass');
        }
      });
      
      $(this).blur(function(){
        if (($(this).val() == '') || ($(this).val() == $(this).attr('defval')))
        {
          $(this).val($(this).attr('defval'));
          $(this).addClass('defvalclass');
        }
        else
        {
          $(this).removeClass('defvalclass');
        }
      });
    }
  });
}

function scrollDiv(divId,deltaScroll,xy)
{
  if (xy == 'y')
  {
    curscrl = parseInt($('#'+divId).scrollTop());
    scrl = curscrl+deltaScroll;
    $('#'+divId).animate({scrollTop: scrl+'px'},1000);
  }
  if (xy == 'x')
  {
    curscrl = parseInt($('#'+divId).scrollLeft());
    scrl = curscrl+deltaScroll;
    $('#'+divId).animate({scrollLeft: scrl+'px'},1000);
  }
}

