var ua='';

var sCat = 0;
var newMes = 0;

var ctrlPress = false;

var opacity = 1;
var fadeObj = null;
var tmpHeight = 1;

var scroll_step=15;
var scroll_action = null;

var dragItem=new Object();

var mouseX=0;
var mouseY=0;

var uAgents = new Array(RegExp("Opera", "i"), RegExp("Gecko", "i"), RegExp("MSIE", "i"));
if (window.navigator.userAgent.match(uAgents[0])) { ua = 'Opera'; }
else if (window.navigator.userAgent.match(uAgents[1])) { ua = 'Netscape';getCoord(Event);document.captureEvents(Event.MOUSEMOVE);document.onmousemove=getCoord;}
else if (window.navigator.userAgent.match(uAgents[2])) { ua = 'IE'; }
else { ua = 'Unk'; }

function timeOut() {
	window.setTimeout("history.back(-1);", 2000);
}

function get(id, type) {
	if(!type) return document.getElementById(id);
	else return document.getElementByName(id);
}

function mouseSpy(e){ 
    e = e || window.event 
  
    if (e.pageX == null && e.clientX != null ) {  
        var html = document.documentElement 
        var body = document.body 
      
        e.pageX = e.clientX + (html && html.scrollLeft || body && body.scrollLeft || 0) - (html.clientLeft || 0) 
        e.pageY = e.clientY + (html && html.scrollTop || body && body.scrollTop || 0) - (html.clientTop || 0) 
    }
    mouseX = e.pageX 
    mouseY = e.pageY 
}

document.onmousemove=mouseSpy;

function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

function insertBB(form, element, aTag, eTag) {
	var input = document.forms[form].elements[element];
	//var input = document.getElementById(element);
	
	var scrollTop=input.scrollTop;
	
	input.focus();
	
	if(typeof document.selection != 'undefined') {
		var range = document.selection.createRange();
		var insText = range.text;
		range.text = aTag + insText + eTag;
		range = document.selection.createRange();
		if (insText.length == 0) {
			range.move('character', aTag.length + insText.length + eTag.length);
		} else {
			range.moveStart('character', aTag.length + insText.length + eTag.length);      
		}
		
		input.scrollTo(0, scrollTop);
	} else if(typeof input.selectionStart != 'undefined') {
		var start = input.selectionStart;
		var end = input.selectionEnd;
		var insText = input.value.substring(start, end);
		
		input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
		
		var pos;
		if (insText.length == 0) {
			pos = start + aTag.length + insText.length + eTag.length;
		} else {
			pos = start + aTag.length + insText.length + eTag.length;
		}
		
		input.selectionStart = pos;
		input.selectionEnd = pos;
	}
}

function formColors(field_id, type)
{
	var field=document.getElementById('fld_'+field_id);
	var checked_field=document.getElementById('chk_'+field_id);
	var default_color=document.getElementById('clr_'+field_id).value;
	checked=checked_field.value;
	
	if(type == "over") {
		if(checked != 1) field.style['background']="#ffce9d";
	} else if(type == "out") {
		if(checked != 1) field.style['background']=default_color;
	} else if(type == "click") {
		if(checked != 1) {
			field.style['background']="#ffce9d";
			checked_field.value='1';
		} else {
			field.style['background']="#ffce9d";
			checked_field.value='0';
		}
	}
}

function getCoord( event ) {
	NetX = event.pageX;
	NetY = event.pageY;
}


function showDiv(div, dW, dH, fade) {
	if(ua == 'Opera' || ua == 'IE') {
		var x=event["clientX"];
		var y=event["clientY"];
		
		y+=document.body.scrollTop;
	} else {
		var x=NetX;
		var y=NetY;
	}
	
x=mouseX;y=mouseY;

	var x2=x;
	var y2=y;
	
	var menu_height=dH;
	var menu_width=dW;

	if( (y+menu_height+15) > (document.body["clientHeight"]+document.body.scrollTop) ) y-=menu_height+10;
	if( (x+menu_width+15) > (document.body["clientWidth"]) ) x-=menu_width+10;
	
	if(x+10 < 0) {
		var zadel=document.body["clientWidth"]-menu_width-10-x2;
		x=x2-10+zadel;
	}
	if(y+10 < 0) {
		var zadel=document.body["clientHeight"]+document.body.scrollTop-menu_height-10-y2;
		y=y2-10+zadel;
	}
	
	var block=document.getElementById(div);
	
	block.style['left'] = x+10;
	block.style['top'] = y+10;
	block.style['visibility']='visible';
	
	if(fade == true) {
		opacity = 1;
		fadeObj = div;
		
		goFade();
	}
}

function goFade() {
	var obj = document.getElementById(fadeObj);
	
	obj.style['opacity']="0."+opacity;
	obj.style['-moz-opacity']="0."+opacity;
	obj.style['filter']="alpha(opacity=" + opacity + "0)";
	
	if(opacity < 9) {
		setTimeout(goFade,10);
		opacity++;
	}
}

function hideDiv(div) {
	var block=document.getElementById(div);
    block.style['visibility']='hidden';
}

function captureDiv(div, _event, _break) {
	var el=document.getElementById(div);
	
	dragItem.element=el;
	dragItem.position_x=parseInt(el.style['left']);
	dragItem.position_y=parseInt(el.style['top']);
	
	dragItem.width=parseInt(dragItem.element.style['width']);
	dragItem.height=parseInt(dragItem.element.style['height']);
	
	if(ua == 'Opera' || ua == 'IE') {
		var x=event["clientX"];
		var y=event["clientY"];
		
		document.attachEvent("onmousemove", moveDiv);
		if(!_break) document.attachEvent("onmouseup", dropDiv);
		
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	} else {
		var x=NetX;
		var y=NetY;
		
		document.addEventListener("mousemove", moveDiv, true);
		if(!_break) document.addEventListener("mouseup", dropDiv, true);
		
		_event.preventDefault();
	}
	
	dragItem.mouse_x=x;
	dragItem.mouse_y=y;
	
	dragItem.distance_x=dragItem.mouse_x-dragItem.position_x;
	dragItem.distance_y=dragItem.mouse_y-dragItem.position_y;
}

function moveDiv() {
	if(dragItem.element) {
		if(ua == 'Opera' || ua == 'IE') {
			var x=event["clientX"];
			var y=event["clientY"];
		
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		} else {
			var x=NetX;
			var y=NetY;
		}
		
		borderSize=15;
		
		newX=(x-dragItem.distance_x);
		newY=(y-dragItem.distance_y)
		
		posX=newX+dragItem.width;
		posY=newY+dragItem.height;
		
		rightBorder=document.body["clientWidth"]-borderSize;
		bottomBorder=document.body["clientHeight"]+document.body.scrollTop-borderSize;
		
		if( posX >= rightBorder ) newX=rightBorder-dragItem.width;
		if( posY >= bottomBorder ) newY=bottomBorder-dragItem.height;
		
		if( newX <= borderSize ) newX=borderSize;
		if( newY <= borderSize ) newY=borderSize;
		
		dragItem.element.style['left'] = newX;
		dragItem.element.style['top'] = newY;
	}
}

function dropDiv(div) {
	dragItem.element=false;
	
	if(ua == 'Opera' || ua == 'IE') {
		document.detachEvent("onmousemove", moveDiv);
		document.detachEvent("onmouseup", dropDiv);
	} else {
		document.removeEventListener("mousemove", moveDiv, true);
		document.removeEventListener("mouseup", dropDiv, true);
	}
}

function fullScreen(div) {
	var block=document.getElementById(div);
	
	block.style['visibility']="hidden";
	
	window.scroll(0,0);
	
	block.style['left']=5;
	block.style['top']=5;
	
	block.style['width']=document.body.clientWidth-10;
	block.style['height']=document.body.clientHeight-10;
	
	block.style['visibility']="visible";
}

function restoreSize(div, w, h) {
	var block=document.getElementById(div);
	
	block.style['width']=w;
	block.style['height']=h;
}

function showDivResize(div, height, openOnly) {
	var tmp=parseInt(document.getElementById(div).style['height']);
	
	if(tmp != height || tmp == 0) {
		document.getElementById(div).style['visibility']="visible";
		tmpHeight=1;
		resizeDiv(div, height);
	} else {
		tmpHeight=height;
		if(!openOnly) resizeDivB(div);
	}
}

function resizeDiv(div, height) {
	var obj=document.getElementById(div);
	
	if(tmpHeight < height) {
		setTimeout("resizeDiv('"+div+"', '"+height+"');",10);
		
		if(tmpHeight+5 > height) tmpHeight=height;
		else tmpHeight+=5;
	}
	
	obj.style['height']=tmpHeight;
}

function resizeDivB(div) {
	var obj=document.getElementById(div);
	
	if(tmpHeight > 1) {
		setTimeout("resizeDivB('"+div+"');",10);
		
		if(tmpHeight-5 <= 1) {
			tmpHeight=1;
			document.getElementById(div).style['visibility']="hidden";
		} else {
			tmpHeight-=5;
		}
	}
	
	obj.style['height']=tmpHeight;
}

function ScrollAction(action) {
	scroll_action=action;
}

function ScrollTo(obj) {
	el=document.getElementById(obj);
	el_pos=el.offsetTop;
	sc_pos=document.body.scrollTop;
	
	if(sc_pos > el_pos) {
		new_pos=sc_pos-scroll_step;
		if(new_pos < el_pos) {
			new_pos=el_pos;
			if(scroll_action != null) eval("setTimeout(\""+scroll_action+"\", 200);");
			scroll_action=null;
		}
		
		window.scroll(0, new_pos);
		
		setTimeout("ScrollTo('"+obj+"')", 10);
	} else {
		if(scroll_action != null) eval("setTimeout('"+scroll_action+"', 200);");
		scroll_action=null;
	}
}

function go(url) {
	location.href=url;
}

function keyUp(e) {
	var key = e? e.which: window.event.keyCode;
	if(key == 17) ctrlPress=false;
}

function keyDown(e) {
	var key = e? e.which: window.event.keyCode;
	if(key == 17) ctrlPress=true;

	if(ctrlPress && (key == 69 || key == 73)) {

		if (document.getSelection) {
			var str = document.getSelection();
		} else if (document.selection && document.selection.createRange) {
			var range = document.selection.createRange();
			var str = range.text;
		}
		get('errorText').value=str;
		showDiv('errorReport', 350,150);
	}
}

document.onkeyup = keyUp;
document.onkeydown = keyDown

if (document.layers) {
	document.captureEvents(Event.KEYUP);
	document.captureEvents(Event.KEYDOWN);
}

function LoadCalendar(parms) {
	if(!parms) parms="";
	//$$('calendarPlace').innerHTML="<div class='small' align=center>загрузка календаря... <img src='images/load.gif' align=absmiddle></div>";
	GetXML('a=calendar'+parms, 'LoadCalendarRes');
}

function LoadCalendarRes(res) {
	get('calendarPlace').innerHTML=res;
}