var macstyleFormVisible = 0;
var waitFormVisible = 0;
var stayAlertWin = 0;
var waitWin = null;


// =========================================
//     КОНСТРУКТОР ОБЪЕКТА         fadedWindow
// =========================================

function fadedWindow(id){
	this.id = (id ? id : 'fw' + Math.round(Math.random()*100000));
	this.zIndex = 3000;
	this.stayOnTop = false;
	this.text = '';
	this.title = '';
	this.footer = '';
	this.withTitleBar = true;
	this.notitle = false;
	this.nofooter = false;
	this.title_className = '';
	this.title_div = null;
	this.content_div = null;
	this.footer_div = null;
	this.transparency = false;
	this.buttons = new Array();
	this.butType = 'butNone';
	if(!isDefined(this.width)) this.width = 300;
	if(!isDefined(this.height)) this.height = 200;
	this.div = null;
}

fadedWindow.prototype.create = function(isrefresh){
	// if(!xGetElementById(this.div)){
		if(!this.div){
			this.div = new_DIV();
			var documentBody = document.getElementsByTagName('BODY')[0];
			documentBody.appendChild(this.div);
			this.div.id = this.id;
			if(!this.iframe){
				this.iframe = document.createElement('IFRAME');
				this.iframe.id = this.id + '_iframe';
			}
			this.iframe.className = 'fwIframe';
			this.div.appendChild(this.iframe);
		}
		xZIndex(this.div, this.zIndex);
		xWidth(this.div, xClientWidth());
		xHeight(this.div, xClientHeight());
		xHide(this.div);
		xDisplay(this.div, 'block');
		this.div.className = 'fw';

		if(!this.sub_div){
			this.sub_div = new_DIV();
			this.div.appendChild(this.sub_div);
			this.sub_div.id = this.id+'_sub_div';
		}
		this.sub_div.className = 'fw_sub_div';
		// if(isMozilla) this.sub_div.style.position = 'fixed';
		xZIndex(this.sub_div, this.zIndex);

		if(!this.main_div){
			this.main_div = new_DIV();
			this.div.appendChild(this.main_div);
			this.main_div.id = this.id+'_main_div';
		}
		this.main_div.className = 'fw_main_div';
		// if(isMozilla) this.main_div.style.position = 'fixed';
		xWidth(this.main_div, this.width);
		xHeight(this.main_div, this.height);
		// this.main_div.style.marginLeft = '-'+(this.width/2)+'px';
		// this.main_div.style.marginLeft = '-'+(this.height/2)+'px';
		xMoveTo(this.main_div, (xClientWidth()-this.width)/2, (xClientHeight()-this.height)/2);
		xZIndex(this.main_div, this.zIndex+1);
		
		if(this.notitle!=true){
			if(!this.title_div){
				this.title_div = new_DIV();
				this.main_div.appendChild(this.title_div);
				this.title_div.id = this.id+'_title_div';
			}
			this.title_div.win = this.div;
			this.title_div.obj = this;
			this.title_div.className = ((this.title_className!='')?this.title_className:'fw_title_div');
			xWidth(this.title_div, this.width);
			xZIndex(this.title_div, this.zIndex+2);
			if(!this.titlebar){
				this.titlebar = new_DIV();
				this.title_div.appendChild(this.titlebar);
				this.titlebar.id = this.id+'_title';
			}
			this.titlebar.className = 'fw_title_content';
			xInnerHtml(this.titlebar, this.title);
			xMoveTo(this.title_div, xPageX(this.main_div), xPageY(this.main_div));

			if(!this.closebut){
				this.closebut = new_DIV();
				this.title_div.appendChild(this.closebut);
				this.closebut.id = this.id+'_closebut';
			}
			this.closebut.className = 'fwCloseBut';
			xZIndex(this.closebut, this.zIndex+3);
			xInnerHtml(this.closebut, '<img src='+htmlrootpath+'/images/close_but.gif width=14 height=14 border=0 vspace=0 hspace=0 />');
			xMoveTo(this.closebut, this.width-17, 3);
			this.closebut.obj = 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.obj.hide();
			}
		}

		if(!this.content_div){
			this.content_div = new_DIV();
			this.main_div.appendChild(this.content_div);
			this.content_div.id = this.id+'_content_div';
		}
		this.content_div.className = 'fw_content_div';
		xWidth(this.content_div, this.width);
		xZIndex(this.title_div, this.zIndex+2);
		if(!this.content){
			this.content = new_DIV();
			this.content_div.appendChild(this.content);
			this.content.id = this.id+'_content';
		}
		this.content.className = 'fw_content';
		xInnerHtml(this.content, this.text);
		xMoveTo(this.content_div, xPageX(this.main_div), xPageY(this.main_div)+((this.notitle!=false)?xHeight(this.withTitleBar):0));
		
		if(this.nofooter!=true){
			if(!this.footer_div){
				this.footer_div = new_DIV();
				this.main_div.appendChild(this.footer_div);
				this.footer_div.id = this.id+'_footer_div';
			}
			this.footer_div.className = 'fw_footer_div';
			xWidth(this.footer_div, this.width);
			xZIndex(this.title_div, this.zIndex+2);
			xMoveTo(this.footer_div, xPageX(this.main_div), xPageY(this.main_div)+this.height-xHeight(this.footer_div));
			if(!this.butsbar){
				this.butsbar = new_DIV();
				this.footer_div.appendChild(this.butsbar);
				this.butsbar.id = this.id+'_buttons';
			}
			this.butsbar.className = 'fw_footer_content';

			if((this.butType && this.butType!='butNone') || isDefined(this.buttons)){
				this.refresh_buttons();
			}

		}

		xHeight(this.content_div, this.height-((this.notitle!=true)?xHeight(this.titlebar):0)-((this.nofooter!=true)?xHeight(this.footer_div):0));
		
		xShow(this.div);
		if(!isrefresh) xDisplay(this.div, 'none');
		
	// }

}
	
fadedWindow.prototype.show = function(){
	hideFlashes();
	xDisplay(this.div, 'block');
	if(this.defaultButton) this.defaultButton.focus();
}

fadedWindow.prototype.hide = function(){
	revertFlashes();
	xDisplay(this.div, 'none');
}

fadedWindow.prototype.refresh = function(){
	var curr_state = xDisplay(this.div);
	this.create(true);
	xDisplay(this.div, ((curr_state && curr_state!='')?curr_state:'block'));
}

fadedWindow.prototype.resizeTo = function(w, h){
	this.width = w;
	this.height = h;
	this.refresh();
}

fadedWindow.prototype.setText = function(text){
	this.text = text;
	xInnerHtml(this.content, this.text);
}

fadedWindow.prototype.refresh_buttons = function(){
	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)){
			
			while(this.butsbar.hasChildNodes())
				this.butsbar.removeChild(this.butsbar.firstChild);
			if(butText!=''){
				this.defaultButton = new_BUTTON(this.butsbar, butText, function(){this.obj.hide(); return false});
				this.defaultButton.obj = this;
				this.defaultButton.className = 'fwButton';
			}
			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.obj = this;
				this.but.className = 'fwButton';
				if(this.buttons[i].def) this.defaultButton = this.but;
			}

		}
	}
}

function hidefadedWindow(id){
	xDisplay(id+'_div', 'none');
	if(isMSIE) xDisplay(id+'_divPNG', 'none');
	return false;
}

fadedWindow.prototype.moveOnTop = function(){
	var windows = xGetElementsByClassName('fadedWin');
	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;
}

fadedWindow.prototype.die = function(){
	var documentBody = document.getElementsByTagName('BODY')[0];
	documentBody.removeChild(this.div);
	if(isMSIE) documentBody.removeChild(this.divPNG);
	return false;
	
	// не забывать что надо еще удалить сам объект  this  посредством присваивания его к  null  после вызова этого метода.
}

fadedWindow.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 fadedWindow();
		alertWindow.create();
	}
	if(xDef(butType) && butType!='') var oldButType = alertWindow.butType;
	alertWindow.setText(text);
	if(xDef(butType) && butType!='') alertWindow.butType = butType;
	alertWindow.show();
	if(xDef(butType) && butType!='') 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 valign=top>' +
			'<td style="height:40px; font-size:12pt; font-family:Geneva CY, Arial, sans-serif; color:#D0D0D0; font-weight:bold; padding-bottom:15px" colspan=2>'+unlanguage('Вход###Members entrance')+'</td>' +
		'</tr>' +
		'<tr height=20 valign=middle>' +
			'<td align=right style="font-size:8pt; width:1%;color:white; border-right:6px #007ACC solid">'+unlanguage('Логин###Log in')+':</td><td><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; margin:0 5px 0 5px" /></td>' +
		'</tr>' +
		'<tr height=20 valign=middle>' +
			'<td align=right style="font-size:8pt; width:1%;color:white; border-right:6px #007ACC solid">'+unlanguage('Пароль###Password')+':</td><td><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%; margin:0 5px 0 5px" /></td>' +
		'</tr>' +
		'<tr valign=middle>' +
			'<td colspan=2 style="font-size:8pt;color:white; padding-top:30px">' +
				welcome_text() + '<br />' +
				unlanguage('Забыли пароль? Воспользуйтесь ###Forgot your password? Use the ') + 
				'<a href=./ style="color:white" 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('сервисом получения пароля###password request service')+'.<'+'/a>' +
			'</td>' +
		'</tr>' +
		'</table>' +
		'</form>';

	if(!loginWindow){
		loginWindow = new fadedWindow();
		loginWindow.width = 350;
		loginWindow.height = 400;
		loginWindow.create();
		loginWindow.buttons = Array({title:unlanguage("Войти###Enter"), action:'checkAuth();this.obj.hide();'},{title:unlanguage("Закрыть###Close"), action:'this.obj.hide();'});
	}
	xBackground(loginWindow.main_div, '#007ACC');
	xBackground(loginWindow.footer_div, '#007ACC');
	loginWindow.text = sLs;
	loginWindow.refresh();
	xHeight(loginWindow.footer_div, 40);
	loginWindow.show();
	xGetElementById('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 register_text(){
	return "<a href='"+htmlrootpath+"/registration/index.htm'>"+unlanguage('Зарегистрироваться###Register')+"</a>";
}

function welcome_text(){
	return "<div style='text-align:left'>" +
		unlanguage(
			"Авторизуйтесь, пожалуйста. После авторизации у Вас появится возможность полноценной работы с сайтом.<br />"+
			"Если Вы еще не имеете имени пользователя и пароля, "+
			"<a style='color:white' href='"+htmlrootpath+"/registration/index.htm'>зарегистрируйтесь</a>"+
			" прямо сейчас."+
			"###"+
			"If you are already a registered user, please sign in now to be able to use the website features in their full capacity. "+
			"If you don't have a username and password yet, please, take a moment to fill out your <a style='color:white' href='"+htmlrootpath+"/registration/index.htm'>registration form</a>."
		)+
	"</div>";
}

function WaitWin(){
	return new fadedWindow();
}
