var agt = navigator.userAgent.toLowerCase();
var is_ie	= (agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1);
var is_gecko = navigator.product == "Gecko";
var is_opera  = (agt.indexOf("opera") != -1);
var is_mac	  = (agt.indexOf("mac") != -1);
var is_mac_ie = (is_ie && is_mac);
var is_win_ie = (is_ie && !is_mac);

function add_event(control, type, fn, use_capture)
{
 if (control.addEventListener)
 {
   control.addEventListener(type, fn, use_capture);
   return true;
 }
 else if (control.attachEvent)
 {
   var r = control.attachEvent("on" + type, fn);
   return r;
 }
}

function get_query_item(page_href, item_name)
{
  arr = get_query_items(page_href);

  if(arr[item_name])
    return arr[item_name];
  else
    return null;
}


function get_query_items(uri)
{
  query_items = new Array();

  arr = uri.split('?');
  if(!arr[1])
    return query_items;

  query = arr[1];

  arr = query.split('&');

  for(index in arr)
  {
    if(arr[index])
    {
      key_value = arr[index].split('=');
      if(!key_value[1])
        continue;

      query_items[key_value[0]] = key_value[1];
    }
  }

  return query_items;
}

var current_hint_x = -1, current_hint_y = -1;
var current_hint_id = null;
var enable_hint_moving = true;

add_event(document, 'mousemove', hint_mouse_move_handler);

function hint_mouse_move_handler(e)
{
  if (is_gecko)
  {
    current_hint_x = e.pageX;
    current_hint_y = e.pageY;
  }
  else
  {
    e = window.event;
    current_hint_x = e.x + document.body.scrollLeft;
    current_hint_y = e.y + document.body.scrollTop;
  }

  if(enable_hint_moving)
    on_hint_mouse_move();
}

function on_hint_mouse_move()
{
  if(current_hint_id)
  	move_hint(current_hint_id, current_hint_x+10, current_hint_y+10);
}

function show_hint(id)
{
 	document.getElementById(id).style.visibility = "visible";
 	document.getElementById(id).style.display = '';
	
	if (document.getElementById(id+'_active') && document.getElementById(id+'_idle')){
	 	document.getElementById(id + '_active').style.visibility = "visible";
	 	document.getElementById(id + '_active').style.display = 'block';
	 	document.getElementById(id + '_idle').style.visibility = "hidden";
	 	document.getElementById(id + '_idle').style.display = 'none';
	}

}

function hide_hint(id)
{
  document.getElementById(id).style.visibility = "hidden";

	if (document.getElementById(id+'_active') && document.getElementById(id+'_idle')){
	 	document.getElementById(id + '_active').style.visibility = "hidden";
	 	document.getElementById(id + '_active').style.display = 'none';
	 	document.getElementById(id + '_idle').style.visibility = "visible";
	 	document.getElementById(id + '_idle').style.display = 'block';
	}
}

function move_hint(id,x,y)
{
  h = document.getElementById(id);
  h.style.left = x;
  h.style.top = y;
}

function start_hint(id)
{
  if(current_hint_id)
  	hide_hint(current_hint_id);

  current_hint_id = id;

  move_hint(current_hint_id, current_hint_x+10, current_hint_y+10);
  show_hint(current_hint_id);
}

function stop_hint()
{
  if(current_hint_id)
    hide_hint(current_hint_id);
  current_hint_id = null;
}

function toggle_hint(id)
{
  if(current_hint_id)
  {
    enable_hint_moving = true;

    if(current_hint_id == id)
    {
    	stop_hint();
    	return;
    }
    stop_hint();
  }
  enable_hint_moving = false;

  start_hint(id);
}
function confirmLink()
{
    if (typeof(window.opera) != 'undefined') {
        return true;
    }

    var is_confirmed = confirm('Вы уверены?');
    return is_confirmed;
}

function checkTextareaLen(field,maxLen){
	
	var str=document.getElementById(field).value;
	len=str.length;
	if(len>maxLen){
		document.getElementById(field).value=document.getElementById(field).value.substring(0,maxLen);
	}
	if ((maxLen-len)>0){
		document.getElementById('maxlen_'+field).innerHTML=(maxLen-len);
	}else{
		document.getElementById('maxlen_'+field).innerHTML=0;
	}


}