// Идея этого скрипта была позаимствована у Яндекса.
// В конце концов: код открыт; комментариев, содержащих запрет на использование, тоже.
// А учиться у профессионалов - это исключительно правильно.
// Исходный код его находится по адресу  http://img.yandex.ru/i/fp.js  (11.11.2003).

// К слову сказать, код существенно до- и переработан.

macstyleFormVisible = 0;
waitFormVisible = 0;
stayAlertWin = 0;



// =========================================
//     КОНСТРУКТОР ОБЪЕКТА         floatWindow
// =========================================

function floatWindow(id){
	this.id = (id ? id : 'fw' + Math.round(Math.random()*100000));
	this.text = '';
	this.zIndex = 100;
	this.stayOnTop = false;
	this.title = '';
	this.withTitleBar = true;
	this.transparency = use_transparency;
	this.buttons = new Array();
	this.butType = 'butNone';
	if(!isDefined(this.width)) this.width = 300;
	if(!isDefined(this.width)) this.height = 200;
}

floatWindow.prototype.create = function(existing_div){
	var documentBody = document.getElementsByTagName('BODY')[0];
	
	var new_div = (!isDefined(this.div));
	if(new_div) this.div = document.createElement('div');
	with(this.div){
		id = this.id+'_div';
		className = 'floatWin';
		style.zIndex = this.zIndex+1;
	}
	if(new_div) documentBody.appendChild(this.div);
	
	if(this.withTitleBar){
		new_div = (!isDefined(this.titlebar));
		if(new_div) this.titlebar = new_DIV();
		with(this.titlebar){
			id = this.id+'_titlebar';
			className = 'floatWinTitleBar';
			style.zIndex = this.zIndex+3;
		}
		if(new_div) this.div.appendChild(this.titlebar);
		this.titlebar.height = xHeight(this.titlebar) ? xHeight(this.titlebar) : 20;
		this.titlebar.win = this.div;
	}
	
	new_div = (!isDefined(this.closebut));
	if(new_div) this.closebut = new_DIV();
	with(this.closebut){
		id = this.id+'_closebut';
		className = 'floatWinCloseBut';
		style.zIndex = this.zIndex+4;
		innerHTML = '<img src='+htmlrootpath+'/images/close_but.gif width=14 height=14 border=0 vspace=0 hspace=0 />';
	}
	if(new_div) this.div.appendChild(this.closebut);
	this.closebut.win = this;
	this.closebut.onmouseover = function(){
		xBackground(this, null, htmlrootpath+'/images/close_but_bg.gif');
	}
	this.closebut.onmouseout = function(){
		xBackground(this, null, '');
	}
	this.closebut.onclick = function(){
		xBackground(this, null, '');
		this.win.hide();
	}

	new_div = (!isDefined(this.cont));
	if(new_div) this.cont = new_DIV()
	with(this.cont){
		id = this.id+'_item';
		className = 'floatWinWorkfield';
		style.zIndex = this.zIndex+2;
	}
	if(new_div) this.div.appendChild(this.cont);
	
	new_div = (!isDefined(this.itemCell));
	if(new_div) {
	
		var tbody1,tbody2,tr1,tr2,tr3,tr4,td1,td2,td3;
		
		tbody1 = new_TBODY();
		
		tr1 = new_TR(tbody1);
		tr1.height = 10;
		td1 = new_TDwWIDTH(tr1, 15);
		with(td1) {height = 10; innerHTML='<div><spacer /></div>';}
		td2 = new_TDwWIDTH(tr1, this.width);
		with(td2) {height = 10; innerHTML='<div><spacer /></div>';}
		td3 = new_TDwWIDTH(tr1, 15);
		with(td3) {height = 10; innerHTML='<div><spacer /></div>';}
		
		tr2 = new_TR(tbody1);
		tr2.height = 10;
		td1 = new_TDwWIDTH(tr2, 15);
		with(td1) {height = 10; className = 'topL'; innerHTML='<div><spacer /></div>';}
		td2 = new_TDwWIDTH(tr2, this.width);
		with(td2) {height = 10; className = 'top'; innerHTML='<div><spacer /></div>';}
		td3 = new_TDwWIDTH(tr2, 15);
		with(td3) {height = 10; className = 'topR'; innerHTML='<div><spacer /></div>';}
	
		tr3 = new_TR(tbody1);
		tr3.height = this.height;
		td1 = new_TDwWIDTH(tr3, 15);
		with(td1) {className = 'mdlL'; innerHTML='<div><spacer /></div>';}
		td2 = new_TDwWIDTH(tr3, this.width);
		this.itemCell = td2;
		td3 = new_TDwWIDTH(tr3, 15);
		with(td3) {className = 'mdlR'; innerHTML='<div><spacer /></div>';}
	
		tr4 = new_TR(tbody1);
		tr4.height = 20;
		td1 = new_TDwWIDTH(tr4, this.width + 30);
		td1.colSpan = 3;
		td1.height = 20;
	
		var tbody2 = new_TBODY();
		td1.appendChild(new_TABLEw100(tbody2));
		
		tr1 = new_TR(tbody2);
		tr1.height = 20;
		td1 = new_TDwWIDTH(tr1, 50);
		with(td1){height = 20; className = 'btmL'; innerHTML='<div><spacer /></div>';}
		td2 = new_TDwWIDTH(tr1, this.width + 30 - 100);
		with(td2){height = 20; className = 'btm'; innerHTML='<div><spacer /></div>';}
		td3 = new_TDwWIDTH(tr1, 50);
		with(td3){height = 20; className = 'btmR'; innerHTML='<div><spacer /></div>';}
	
		this.cont.appendChild(new_TABLEw100(tbody1));
	} 

	new_div = (!isDefined(this.item));
	if(new_div) this.item = new_DIV();
	this.item.className = 'floatWinItemContent';
	with(this.itemCell){
		width = this.width;
		height = this.height;
		vAlign = 'top';
		// nowrap = 'nowrap';
		className = 'floatWinItem';
	}
	if(new_div) this.itemCell.appendChild(this.item);

	xResizeTo(this.cont, this.width + 30, this.height + 40);

	if(this.butType!='' || isDefined(this.buttons)){
		var butText = 'OK';
		var butAction = '';
		switch(this.butType) {
			case('butOK'):{
				butText = 'OK';
				break;
			}
			case('butClose'):{
				butText = 'Закрыть';
				break;
			}
			case('butNone'):{
				butText = '';
				break;
			}
		}
		if(butText!='' || isDefined(this.buttons)){
			
			if(isDefined(this.butsbar))
				this.div.removeChild(this.butsbar);
			this.butsbar = new_DIV();
			with(this.butsbar){
				id = this.id+'_buttonsbar';
				className = 'floatWinButtonsBar';
				style.zIndex = this.zIndex+3;
				align = 'center';
				style.paddingTop = 8;
			}
			if(butText!='')
				this.defaultButton = new_BUTTON(this.butsbar, butText, new Function('if(!this.stayOnTop) hideFloatWindow(\''+this.id+'\'); return false'));
			for(var i=0;i<this.buttons.length;i++){
				this.but = new_BUTTON(this.butsbar, this.buttons[i].title, new Function(this.buttons[i].action+'return false'));
				this.but.style.margin = '0px 5px 0px 5px';
				if(this.buttons[i].def) this.defaultButton = this.but;
			}
			this.div.appendChild(this.butsbar);
		}
	}

	if(isMSIE && this.transparency) {
		new_div = (!isDefined(this.divPNG));
		if(new_div) this.divPNG = new_DIV()
		with(this.divPNG){
			id = this.id+'_divPNG';
			className = 'floatWinPNG';
			style.zIndex = this.zIndex;
		}
		if(new_div) documentBody.appendChild(this.divPNG);
	}
	return false;

}
	
floatWindow.prototype.refresh = function(){
	this.create();
}

floatWindow.prototype.show = function(){
	if((isOpera && isOpera7) || isDOM){

		var titleheight = (isDefined(this.titlebar)?this.titlebar.height:0);
		
		/*
		if(isMSIE && this.transparency){
			this.divPNG.style.display = 'block';
			xResizeTo(this.divPNG, this.width, this.height);
			xMoveTo(this.divPNG, l + 15, t + 20);
		}
		*/
		
		if(isDefined(this.titlebar)){
			xResizeTo(this.titlebar, this.width, titleheight);
			xMoveTo(this.titlebar, 15, 20);
			this.titlebar.innerHTML = this.title;
		}

		xMoveTo(this.closebut, 15 + this.width - titleheight + 3, 20 + 3);

		if(isDefined(this.butsbar)){
			xResizeTo(this.butsbar, this.width, 40);
			xMoveTo(this.butsbar, 15, titleheight + this.height - 40);
		}
		
		xMoveTo(this.cont, 0, 0);
		xResizeTo(this.cont, this.width + 30, this.height + 40);
		
		xResizeTo(this.itemCell, this.width, this.height);
		xResizeTo(this.item, this.width, this.height - titleheight - (isDefined(this.butsbar)?40:0));
		xMoveTo(this.item, 0, titleheight);
		
		this.item.innerHTML = this.text;
		
		if(isMozilla)
			xMoveTo(this.div, xScrollTop(), -2000);
		this.moveOnTop();
		
		xResizeTo(this.div, this.width + 30, this.height + 40);
		var clW = parseInt(xClientWidth());
		var clH = parseInt(xClientHeight());
		var layW = this.width + 30;
		var layH = this.height + 40;
		var l = (layW<=clW) ? parseInt(xScrollLeft() + (clW - layW)/2) : 0;
		var t = (layH<=clH) ? parseInt(xScrollTop() + (clH - layH)/2) : 0;
		xMoveTo(this.div, l, t);

		this.div.style.display = 'block';

		// if(isDefined(this.defaultButton)) this.defaultButton.focus();

		this.width = (layW - 30);
		this.height = (layH - 40);

	}
	return false;
}

floatWindow.prototype.hide = function(){
	hideFloatWindow(this.id);
	this.stayOnTop = false;
	return false;
}

function hideFloatWindow(id){
	xDisplay(id+'_div', 'none');
	if(isMSIE) xDisplay(id+'_divPNG', 'none');
	return false;
}

floatWindow.prototype.moveOnTop = function(){
	var windows = xGetElementsByClassName('floatWin');
	var z = highZ = 0;
	for(var i=0;i<windows.length;i++){
		z = xZIndex(windows[i]);
		if(highZ<z) highZ=z;
	}
	xZIndex(this.div, highZ+10);
	return false;
}

floatWindow.prototype.die = function(){
	var documentBody = document.getElementsByTagName('BODY')[0];
	documentBody.removeChild(this.div);
	if(isMSIE) documentBody.removeChild(this.divPNG);
	return false;
	
	// не забывать что надо еще удалить сам объект  this  посредством присваивания его к  null  после вызова этого метода.
}

floatWindow.prototype.repos = function(){
	var obj = this.div;
	var obj_png = this.divPNG;
	var clW = parseInt(xClientWidth());
	var clH = parseInt(xClientHeight());
	var layW = parseInt(xWidth(obj));
	var layH = parseInt(xHeight(obj));
	var l = (layW<=clW) ? parseInt(xScrollLeft() + (clW - layW)/2) : 0;
	var t = (layH<=clH) ? parseInt(xScrollTop() + (clH - layH)/2) : 0;
	if(obj){
		if(layW<=clW)
			xLeft(obj, l);
		if(layH<=clH)
			xTop(obj, t);
	}
	if(isMSIE && obj_png){
		if(layW<=clW)
			xLeft(obj_png, l + 15);
		if(layH<=clH)
			xTop(obj_png, t + 20);
	}
}

function myOnDragStart(ele, mx, my){
}
function myOnDrag(obj, mdx, mdy){
	if(isDefined(obj.win)) xMoveTo(obj.win, xLeft(obj.win) + mdx, xTop(obj.win) + mdy);
}
function myOnDragEnd(ele, mx, my){
}




// =========================================

// основные типы окна

function  showAlert(text, butType) {
	if(!alertWindow){
		alertWindow = new floatWindow();
		alertWindow.create();
	}
	var oldButType = alertWindow.butType;
	alertWindow.text = text;
	if(butType && butType!='') alertWindow.butType = butType;
	alertWindow.show();
	alertWindow.butType = oldButType;
}

function showLogin(ini_login, ini_password, referer) {

	var str = htmlrootpath+"/index.htm";
	if(referer)
		var ref = referer;
	else 
		var ref = document.location.href.replace(/^http:\/\//, '').replace(/^[^\/]+/, '');
	var sLs = '' +
		'<form name=auth action=' + ref + ' method=post>' +
		'<input type=hidden name=referer value="'+ref+'"><input type=hidden name=log_action value="commit">' +
		'<table width=100% border=0 cellspacing=5 cellpadding=0>' +
		'<tr height=60 align=center valign=middle nowrap="nowrap">' +
			'<td width=99% style="font-size:8pt; padding-bottom:10px" colspan=2>'+welcome_text()+'</td>' +
		'</tr>' +
		'<tr height=20 valign=middle>' +
			'<td align=right style="font-size:8pt; width:1%; border-right:6px #007ACC solid;padding-right:5px">'+unlanguage('Логин###Log in')+':</td><td style="padding-left:5px"><input name=login type=text id=authFormLogin value="'+(ini_login?ini_login:'')+'" size=20 class=inputs onKeyDown="if(event.keyCode==13)return checkAuth();" style="width:100%;height:20px" /></td>' +
		'</tr>' +
		'<tr height=20 valign=middle>' +
			'<td align=right style="font-size:8pt; width:1%; border-right:6px #007ACC solid;padding-right:5px">'+unlanguage('Пароль###Password')+':</td><td style="padding-left:5px"><input name=password type=password id=authFormPassword value="'+(ini_password?ini_password:'')+'" size=20 class=inputs onKeyDown="if(event.keyCode==13)return checkAuth();" style="width:100%;" /></td>' +
		'</tr>' +
		'<tr align=center valign=middle>' +
			'<td colspan=2 style="font-size:8pt;"><a href=./ onClick=\'var s = "";' +
				'if(document.getElementById("authFormLogin").value.match(/\\W/g)!=null) {' +
					's += "<li>недопустимые символы в поле <b>Логин<'+'/b>.";' +
				'}' +
				'if(s!="") {' +
					'errorAlert("ошибка:<ul>"+s+"<ul>")' +
				'} else {' +
					'document.location.href = "'+htmlrootpath+'/registration/email_password.htm?login="+document.getElementById("authFormLogin").value;' +
				'}' +
				'return false\'>'+unlanguage('Забыли пароль###Forget your password')+'?<'+'/a>' +
			'</td>' +
		'</tr>' +
		'</table>' +
		'</form>';

	if(!loginWindow){
		loginWindow = new floatWindow();
		loginWindow.create();
	}
	loginWindow.text = sLs;
	loginWindow.width = 350;
	loginWindow.zIndex = 600;
	loginWindow.buttons = Array({title:'".unlanguage("Войти###Enter")."', action:'loginWindow.die();'},{title:'".unlanguage("Закрыть###Close")."', action:'loginWindow.die();'});
	loginWindow.show();
	document.getElementById('authFormLogin').focus();

	return false;
}




// Сопутствующие 


function showConfirm(text,yesAction,noAction){
	var sc_text="<table width=100% border=0 cellspacing=0 cellpadding=0><form name=confirm>" +
	"<tr valign=middle>" +
		"<td>"+text+"</td>" +
	"</tr>" +
	"<tr height=40 align=center valign=middle>" +
		"<td style=`padding-top:5px`>" +
			"<input type=`button` value=`Да` onClick=`"+yesAction.replace(/;$/,"")+((yesAction.replace(/;$/,"")=="")?"":";")+"if(!(stayAlertWin)) alertWindow.hide();return false`>" +
			"&nbsp;&nbsp;" +
			"<input type=`button` value=`Нет` onClick=`"+noAction.replace(/;$/,"")+((noAction.replace(/;$/,"")=="")?"":";")+"if(!(stayAlertWin)) alertWindow.hide();return false`>" +
		"</td>" +
	"</tr>" +
	"</form></table>";
	showAlert(sc_text,"butNone");
	return true;
}

function showConfirm2(text,okAction,cancelAction){
	var sc_text="<table width=100% border=0 cellspacing=0 cellpadding=0><form name=confirm2>" +
	"<tr valign=middle>" +
		"<td>"+text+"</td>" +
	"</tr>" +
	"<tr height=40 align=center valign=middle>" +
		"<td style=\"padding-top:5px\">" +
			"<input type='button' value='OK' onClick='"+okAction.replace(/;$/,"")+((okAction.replace(/;$/,"")=="")?"":";")+"confirmWindow.hide();return false'>" +
			"&nbsp;&nbsp;" +
			"<input type='button' value='Отмена' onClick='"+cancelAction.replace(/;$/,"")+((cancelAction.replace(/;$/,"")=="")?"":";")+"confirmWindow.hide();return false'>" +
		"</td>" +
	"</tr>" +
	"</form></table>";


	var tbl,tbody1,form,tr1,tr2,td1;
	
	tbody1 = new_TBODY();
	
	// form = new_FORM(tbody1, './', 'multipart/form-data', 'multipart/form-data', 'get', '', 'confirmFrm');
	// form.id = 'confirmFrm';

	tr1 = new_TR(tbody1);
	td1 = new_TDwWIDTH(tr1, '100%');
	td1.innerHTML = text;
	
	tr2 = new_TR(tbody1);
	tr2.height = 40;
	td1 = new_TDwWIDTH(tr2, '100%');
	with(td1) {
		height = 40;
		innerHTML = ''+
			'<input type="button" value="OK" onClick="'+okAction.replace(/;$/,"")+((okAction.replace(/;$/,"")=="")?"":";")+'confirmWindow.hide();return false">' +
			'&nbsp;&nbsp;' +
			'<input type="button" value="Отмена" onClick="'+cancelAction.replace(/;$/,"")+((cancelAction.replace(/;$/,"")=="")?"":";")+'confirmWindow.hide();return false">' +
			'';
	}

	tbl = new_TABLEw100(tbody1);
	
	confirmWindow.zIndex = alertWindow.zIndex+10;
	// confirmWindow.text = tbl.outerHTML;
	confirmWindow.show();
	confirmWindow.item.innerHTML = '';
	confirmWindow.item.appendChild(tbl);
}

function showConfirm(text,okAction,cancelAction){
	/*
	var sc_text="<table width=100% border=0 cellspacing=0 cellpadding=0><form name=confirm>" +
	"<tr valign=middle>" +
		"<td>"+text+"</td>" +
	"</tr>" +
	"<tr height=40 align=center valign=middle>" +
		"<td style=`padding-top:5px`>" +
			"<input type=`button` value=`Да` onClick=`"+yesAction.replace(/;$/,"")+((yesAction.replace(/;$/,"")=="")?"":";")+"if(!(stayAlertWin)) alertWindow.hide();return false`>" +
			"&nbsp;&nbsp;" +
			"<input type=`button` value=`Нет` onClick=`"+noAction.replace(/;$/,"")+((noAction.replace(/;$/,"")=="")?"":";")+"if(!(stayAlertWin)) alertWindow.hide();return false`>" +
		"</td>" +
	"</tr>" +
	"</form></table>";
	showAlert(sc_text,"butNone");
	return true;
	*/
	confirmWindow.text = text;
	confirmWindow.buttons[0].action = okAction.replace(/;$/,"")+((okAction.replace(/;$/,"")=="")?"":";")+'confirmWindow.hide();';
	confirmWindow.buttons[1].action = cancelAction.replace(/;$/,"")+((cancelAction.replace(/;$/,"")=="")?"":";")+'confirmWindow.hide();';
	confirmWindow.refresh();
	confirmWindow.show();
}

function showConfirm2(text,yesAction,noAction){
	
	var yesAct = yesAction.replace(/;$/,"")+((yesAction.replace(/;$/,"")=="")?"":";")+'confirmWindow2.hide();';
	var noAct = noAction.replace(/;$/,"")+((noAction.replace(/;$/,"")=="")?"":";")+'confirmWindow2.hide();';
	confirmWindow2.text = text;
	// confirmWindow2.buttons = Array({title:'".unlanguage("Да###Yes")."', action:yesAct},{title:'".unlanguage("Нет###No")."', action:noAct});
	confirmWindow2.buttons = Array({title:'Да', action:yesAct},{title:'Нет', action:noAct});
	confirmWindow2.refresh();
	confirmWindow2.show();

}

function showPrompt(text, okAction, cancelAction, defaultValue){
	/*
	var sc_text="<table width=100% border=0 cellspacing=0 cellpadding=0>" +
	"<form name=prompt_frm onSubmit='"+okAction.replace(/;$/, "").replace(/'/g, "\'")+((okAction.replace(/;$/,"")=="")?"":";")+"if(!stayAlertWin) alertWindow.hide(); return false'>" +
	"<tr valign=middle>" +
		"<td>"+text+"</td>" +
	"</tr>" +
	"<tr valign=middle>" +
		"<td width=100%><input type=text name=win_prompt_text id=win_prompt_text value='"+(defaultValue?defaultValue:"")+"' style='width:100%'></td>" +
	"</tr>" +
	"<tr height=40 align=center valign=middle>" +
		"<td style='padding-top:5px'>" +
			"<input type=submit value='OK'>" +
			"&nbsp;&nbsp;" +
			"<input type=button value='Отмена' onClick='"+cancelAction.replace(/;$/,"").replace(/'/g, "\'")+((cancelAction.replace(/;$/,"")=="")?"":";")+"if(!stayAlertWin) alertWindow.hide(); return false'>" +
		"</td>" +
	"</tr>" +
	"</form>" +
	"</table>";
	showAlert(sc_text, "butNone");
	*/
	var sp_text = '<table width=100% border=0 cellspacing=0 cellpadding=0>' +
	'<tr valign=middle>' +
		'<td>'+text+'</td>' +
	'</tr>' +
	'<tr valign=middle>' +
		'<td width=100%><input type=text name=win_prompt_text id=win_prompt_text value="'+(defaultValue?defaultValue:'')+'" style="width:100%"></td>' +
	'</tr>' +
	'</table>';
	var okAct = okAction.replace(/;$/,"")+((okAction.replace(/;$/,"")=="")?"":";")+'promptWindow.hide();';
	var cancelAct = cancelAction.replace(/;$/,"")+((cancelAction.replace(/;$/,"")=="")?"":";")+'promptWindow.hide();';
	promptWindow.text = sp_text;
	promptWindow.buttons = Array({title:'OK', action:okAct},{title:'Отмена', action:cancelAct});
	promptWindow.refresh();
	promptWindow.show();
}


function waitAlert(str){
	if (!waitFormVisible) showAlert("<center>"+((str!="")?str:"Подождите, осуществляется загрузка...")+"<br /><br /><img src="+htmlrootpath+"/images/progress_bar.gif width=154 height=18 border=0 style='margin-bottom:20px'></center>","butNone");
	waitFormVisible = 1;
	return false;
}

function errorAlert(str){
	showAlert("<font color=red>"+((str!="")?str:"Ошибка системы... Обратитесь к <a href=mailto:ewgeniy@onegin.com>администратору</a> с сообщением о случившемся.")+"</font>","butOK");
	return false;
}

function hideAlert(){
	stayAlertWin = 0;
	waitFormVisible = 0;
	alertWindow.hide();
}

function hideFloatedWin(name){
	if(document.getElementById(name+'-form'))
		document.getElementById(name+'-form').style.display = "none";
	if(document.getElementById(name+'-form-png'))
		document.getElementById(name+'-form-png').style.display = "none";
	return false;
}

function register_text(){
	return "<a href='"+htmlrootpath+"/registration/index.htm'>"+unlanguage('Зарегистрироваться###Register')+"</a>";
}

function welcome_text(){
	return "<div style='text-align:left'>" +
		unlanguage("Введите Ваш логин и пароль для входа в систему###Type your login and password to access the system") + ".<br />" +
		"</div>";
}

function reposFloatedWin(name){
	var obj = document.getElementById(name + '-form');
	var obj_png = document.getElementById(name + '-form-png');
	var clW = parseInt((isMSIE ? document.body.clientWidth : document.clientWidth));
	var clH = parseInt((isMSIE ? document.body.clientHeight : document.clientHeight));
	var layW = parseInt((isMSIE ? obj.offsetWidth : obj.scrollWidth));
	var layH = parseInt((isMSIE ? obj.offsetHeight : obj.scrollHeight));
	if(obj){
		if(layW<=clW)
			obj.style.left = (isMSIE ? document.body.scrollLeft : document.body.scrollLeft) + (clW - layW)/2;
		if(layH<=clH)
			obj.style.top = (isMSIE ? document.body.scrollTop : document.body.scrollTop) + (clH - layH)/2;
	}
	if(isMSIE && obj_png){
		if(layW<=clW)
			obj_png.style.left = parseInt(obj.style.left) + 15;
		if(layH<=clH)
			obj_png.style.top = parseInt(obj.style.top) + 20;
	}
}

