var win = null;

function CenterWindow(myurl, myname, w, h, scroll, status) {
	if(win == '[object]') win.close();
	status = 1;
	
	if (screen.width <= w + 20) {
		// размер выдаваемого окна больше чем размер экрана пользователя
		scroll = 1;
		w = screen.width - 20;
	}
	
	if (screen.height <= h + 50) {
		// размер выдаваемого окна больше чем размер экрана пользователя
		scroll = 1;
		h = screen.height - 50;
	}
	
	
	// -10 и -50 - поправка для скроллинга и для нижней полосы с меню "Старт"
	LeftPosition = (screen.width) ? (screen.width-w - 10)/2: 0;
	TopPosition = (screen.height) ? (screen.height-h - 50)/2 : 0;
	settings = 'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',toolbar=0,location=0,resizeable=0,menubar=0,directories=0,dependent=1,status='+status;
	win = window.open(myurl,myname,settings)
	return win;
}
function showDialog(myurl, null_reserved, w, h) {
	window.showModalDialog(myurl, window, "dialogHeight:"+h+"px; dialogWidth:"+w+"px; edge: Raised; center: Yes; help: No; resizable: No; status: No; scroll: 0; ");
}
// Центрирует диалоговое окно
function centerDialog() {
	var height = document.body.scrollHeight + 55;
	window.dialogHeight = height + 'px';
	var l = 
	window.dialogLeft = ((screen.width - parseInt(window.dialogWidth)) / 2) + 'px';
	window.dialogTop = ((screen.height - parseInt(window.dialogHeight)) / 2) + 'px';
}
function showImage(url) {
	if (win == '[object]') win.close();
	LeftPosition = (screen.width) ? (screen.width-640 - 10)/2: 0;
	TopPosition = (screen.height) ? (screen.height-480 - 50)/2 : 0;
	win = window.open('/tools/cms/site/image6.php?url='+url,'image','height=480,width=640,top='+TopPosition+',left='+LeftPosition+',scrollbars=1,toolbar=0,location=0,menubar=0,directories=0,dependent=1,status=1')
}
function resizeImageDialog(img) {
	var img = document.getElementById(img);
	var w = img.width + 50;
	var h = img.height + 100;
	resizeDialog(w,h);
}
function resizeDialog(w,h) {
	var mv = 80;
	var mh = 50;
	w = (screen.width <= w + mh) ? screen.width - mh : w;
	h = (screen.height <= h + mv) ? screen.height - mv : h;
	w = (w < 100) ? 100 : w;
	h = (h < 100) ? 100 : h;
	
//	if (h>640) h=640;
	LeftPosition = (screen.width) ? (screen.width - w) / 2 : 0;
	TopPosition = (screen.height) ? (screen.height - h) / 2 : 0;
	window.resizeTo(w,h);
	window.moveTo(LeftPosition,TopPosition);
}
function setCookie(cookieName,cookieValue,nDays) {
	var today = new Date();
	var expire = new Date();
	if (nDays==null || nDays==0) nDays=1;
	expire.setTime(today.getTime() + 3600000*24*nDays);
	document.cookie = cookieName+"="+escape(cookieValue)+ ";expires="+expire.toGMTString();
}
function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
	    begin = dc.indexOf(prefix);
	    if (begin != 0) return null;
	} else {
	    begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
	    end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Определяет внутреннюю ширину и высоту окна браузера
 */
function windowWidth() {
 return (window.innerWidth)?window.innerWidth:((document.all)?document.body.offsetWidth:null);
}
function windowHeight() {
	return (window.innerHeight)?window.innerHeight:((document.all)?document.body.offsetHeight:null);
}
/**
 * Ставит слой в центр экрана
 */
/*function centerDiv(layer_id) {
	if (byId(layer_id)) {
		var left = (windowWidth()-byId(layer_id).clientWidth)/2;
		var top = (windowHeight()-byId(layer_id).clientHeight)/2 + document.body.scrollTop;
		byId(layer_id).style.left = (left >= 0) ? left : 0;
		byId(layer_id).style.top =  (top >= 0) ? top : 0;
		byId(layer_id).style.top =  400;
	}
}*/

function centerDiv(layer_id) {
	/*(if (byId(layer_id)) {
		var left = (windowWidth()-byId(layer_id).clientWidth)/2;
		var top = (windowHeight()-byId(layer_id).clientHeight)/2;
		{
			byId(layer_id).style.position = 'fixed';
			byId(layer_id).style.top = '300px';
		}
	}*/
	ds = getDocumentSize(document);
	byId('ajaxPreloader').style.left = (ds.width / 2 - 100) + "px";
}

/**
 * Алиас для функции document.getElementById()
 */
function byId(id) {
	return document.getElementById(id);
}

function getDocumentSize(doc) { // getting a real size document with scrolling etc.
  var r = { width: 0, height: 0 };
 
  var width1=0, width2=0, width3=0, width4=0, maxWidth=0;
  var height1=0, height2=0, height3=0, height4=0, maxHeight=0;
  
  
  if (doc.width) maxWidth = doc.width; 
  if (doc.body) { 
	if (doc.body.scrollWidth) width1 = doc.body.scrollWidth;
	if (doc.body.offsetWidth) width2 = doc.body.offsetWidth; 
  }
  if (doc.documentElement) {
	width3 = doc.documentElement.scrollWidth; 
	width4 = doc.documentElement.clientWidth;
  } 
  
  maxWidth = Math.max(Math.max(Math.max(width1, width2), Math.max(width3, width4)),maxWidth);
  
  if (doc.height) maxHeight = doc.height;
  if (doc.body) {
	if (doc.body.scrollHeight) height1 = doc.body.scrollHeight;
	if (doc.body.offsetHeight) height2 = doc.body.offsetHeight;
  }
  if (doc.documentElement) {
	height3 = doc.documentElement.scrollHeight;
	height4 = doc.documentElement.clientHeight;
  }
  maxHeight = Math.max(Math.max(Math.max(height1, height2), Math.max(height3, height4)),maxHeight);
  
  r.width = maxWidth;
  r.height = maxHeight;
  
  return r;
}