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

function  showAlert(text,butType) {
	if( isOpera &&  !isOpera7 ) {
		return true;
	} else if (isDOM) {
		var butText = 'OK';
		var butAction = '';
		switch(butType) {
			case('butOK'):{
				butText = 'OK';
				break;
			}
			case('butClose'):{
				butText = 'Закрыть';
				break;
			}
			case('butNone'):{
				butText = '';
				break;
			}
		}
		if (butText!='') { butText = '<tr height=40 valign=middle align=center><td style="padding-top:5px"><input id=alertDefaultButton type="button" value="'+butText+'" onClick=\''+butAction+'hideFloatedWin("alert")\' /></td></tr>';}
		document.getElementById('alert-form').innerHTML = ''+
			'<table border=0 cellspacing=0 cellpadding=0 width=100% align=center>'+
			'<tr height=10>' +
				'<td width=15 height=10 nowrap=nowrap></td>' +
				'<td width=100%></td>' +
				'<td width=15 height=10 nowrap=nowrap></td>' +
			'</tr>' +
			'<tr valign=bottom height=10>' +
				'<td class=topL width=15 height=10 nowrap=nowrap></td>' +
				'<td class=top width=100%></td>' +
				'<td class=topR width=15 height=10 nowrap=nowrap></td>' +
			'</tr>' +
			'<tr valign=top width=100%>' +
				'<td class=mdlL></td>' +
				'<td class=alertForm align=center>'+
					'<table width=100% border=0 cellspacing=5 cellpadding=0>'+
					'<tr valign=middle>'+
						'<td width=100% style="padding:10px 10px 0px 10px">'+
						text +
						'</td>'+
					'</tr>'+
					butText +
					'</table>'+
				'</td>'+
				'<td class=mdlR></td>'+
			'</tr>'+
			'<tr height=20>'+
				'<td colspan=3>'+
					'<table width=100% border=0 cellspacing=0 cellpadding=0>'+
					'<tr>'+
						'<td class=btmL><div style="width:50px;height:20px"><spacer /></div></td>'+
						'<td class=btm width=99%><div><spacer /></div></td>'+
						'<td class=btmR><div style="width:50px;height:20px"><spacer /></div></td>'+
					'</tr>'+
					'</table>'+
				'</td>'+
			'</tr>'+
			'</table>';
		document.getElementById('alert-form').style.display = "block";
		document.getElementById('alert-form').style.top =		document.body.scrollTop + (document.body.clientHeight-document.getElementById('alert-form').offsetHeight)/2;
		document.getElementById('alert-form').style.left =		document.body.scrollLeft + (document.body.clientWidth-document.getElementById('alert-form').offsetWidth)/2;
		document.getElementById('alert-form-png').style.display = "block";
		document.getElementById('alert-form-png').style.width = parseInt(document.getElementById('alert-form').offsetWidth) - 30;
		document.getElementById('alert-form-png').style.height = parseInt(document.getElementById('alert-form').offsetHeight) - 40;
		document.getElementById('alert-form-png').style.left = parseInt(document.getElementById('alert-form').style.left) + 15;
		document.getElementById('alert-form-png').style.top = parseInt(document.getElementById('alert-form').style.top) + 20;
		if(document.getElementById('alertDefaultButton'))
			document.getElementById('alertDefaultButton').focus();
		return false;
	}
}

function showConfirm(text,yesAction,noAction){
	var sc_text="<table width=100% border=0 cellspacing=0 cellpadding=0>" +
	"<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))hideAlert();return false`>" +
			"&nbsp;&nbsp;" +
			"<input type=`button` value=`Нет` onClick=`"+noAction.replace(/;$/,"")+((noAction.replace(/;$/,"")=="")?"":";")+"if(!(stayAlertWin))hideAlert();return false`>" +
		"</td>" +
	"</tr>" +
	"</table>";
	showAlert(sc_text,"butNone");
	return true;
}

function showConfirm2(text,okAction,cancelAction){
	var sc_text="<table width=100% border=0 cellspacing=0 cellpadding=0>" +
	"<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(/;$/,"")=="")?"":";")+"if(!(stayAlertWin))hideAlert();return false`>" +
			"&nbsp;&nbsp;" +
			"<input type=`button` value=`Отмена` onClick=`"+cancelAction.replace(/;$/,"")+((cancelAction.replace(/;$/,"")=="")?"":";")+"if(!(stayAlertWin))hideAlert();return false`>" +
		"</td>" +
	"</tr>" +
	"</table>";
	showAlert(sc_text,"butNone");
}

function showPrompt(text,okAction,cancelAction){
	var sc_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=`` style=`width:100%`></td>" +
	"</tr>" +
	"<tr height=40 align=center valign=middle>" +
		"<td style=`padding-top:5px`>" +
			"<input type=`button` value=`OK` onClick=`"+okAction.replace(/;$/,"")+((okAction.replace(/;$/,"")=="")?"":";")+"if(!(stayAlertWin))hideAlert();return false`>" +
			"&nbsp;&nbsp;" +
			"<input type=`button` value=`Отмена` onClick=`"+cancelAction.replace(/;$/,"")+((cancelAction.replace(/;$/,"")=="")?"":";")+"if(!(stayAlertWin))hideAlert();return false`>" +
		"</td>" +
	"</tr>" +
	"</table>";
	showAlert(sc_text,"butNone");
}

function showLogin(ini_login, ini_password, referer) {
	if( isOpera &&  !isOpera7 ) {
		return true;
	} else if (isDOM) {
		// var str = document.location.href.replace(/action=[a-zA-z_]+&?/, "").replace(/\?$/,"");
		var str = htmlrootpath+"/index.htm";
		if(referer)
			var ref = referer;
		else 
			var ref = document.location;
		sLs = '' +
			'<form name=auth action=' + str + ' method=post><input type=hidden name=referer value="'+ref+'"><input type=hidden name=log_action value="commit">' +
			'<table border=0 cellspacing=0 cellpadding=0 width=100% align=center>' +
			'<tr height=10>' +
				'<td width=15 height=10 nowrap=nowrap></td>' +
				'<td width=100%></td>' +
				'<td width=15 height=10 nowrap=nowrap></td>' +
			'</tr>' +
			'<tr height=10>' +
				'<td class=topL width=15 height=10 nowrap=nowrap></td>' +
				'<td class=top width=100%></td>' +
				'<td class=topR width=15 height=10 nowrap=nowrap></td>' +
			'</tr>' +
			'<tr valign=top height=150>' +
				'<td class=mdlL></td>' +
				'<td class=loginForm align=center>' +
					'<table width=100% border=0 cellspacing=5 cellpadding=0>' +
					'<tr height=40 align=center valign=middle nowrap="nowrap">' +
						'<td width=99% style="padding-bottom:10px">'+register_text()+'</td>' +
					'</tr>' +
					'<tr height=20 valign=middle>' +
						'<td>Логин:<br><input name=login type=text id=authFormLogin value="'+(ini_login?ini_login:'')+'" size=20 style="width: 100%;height:20px" /></td>' +
					'</tr>' +
					'<tr height=20 valign=middle>' +
						'<td>Пароль:<br><input name=password type=password id=authFormPassword value="'+(ini_password?ini_password:'')+'" size=20 style="width: 100%;" /></td>' +
					'</tr>' +
					'<tr height=30 align=center valign=middle>' +
						'<td><a href=./ onClick=\'var s = "";' +
							'if(document.auth.login.value=="") {' +
								's += "<li>поле <b>Логин</b> пустое.";' +
							'} else {' +
								'if(document.auth.login.value.match(/\\W/g)!=null) {' +
									's += "<li>недопустимые символы в поле <b>Логин</b>.";' +
								'}' +
							'}' +
							'if(s!="") {' +
								'errorAlert("ошибка:"+s)' +
							'} else {' +
								'document.location.href = "'+htmlrootpath+'/registration/email_password.htm?login="+document.auth.login.value;' +
							'}' +
							'return false\'>Забыли пароль?</a>' +
						'</td>' +
					'</tr>' +
					'<tr height=40 align=center valign=middle>' +
						'<td style="padding-top:5px">' +
							'<input type=button value="Войти" style="margin-right:9px"  onClick=\'var s = "";' +
								'if(this.form.login.value=="") {' +
									's += "<li>поле <b>Логин</b> пустое.";' +
								'} else {' +
									'if(this.form.login.value.match(/\\W/g)!=null) {' +
										's += "<li>недопустимые символы в поле <b>Логин</b>.";' +
									'}' +
								'}' +
								'if(this.form.password.value=="") {' +
									's += "<li>поле <b>Пароль</b> пустое.";' +
								'}' +
								'if(s!="") {' +
									'errorAlert("ошибка:"+s);return false' +
								'} else this.form.submit()\' />' +
							'<input type="button" value="Закрыть" onClick=\'hideFloatedWin("login")\' />' +
						'</td>' +
					'</tr>' +
					'</table>' +
				'</td>' +
				'<td class=mdlR></td>' +
			'</tr>' +
			'<tr height=20>' +
				'<td colspan=3>' +
					'<table width=100% border=0 cellspacing=0 cellpadding=0>' +
					'<tr>' +
						'<td class=btmL><div style="width:50px;height:20px"><spacer /></div></td>' +
						'<td class=btm width=99%><div><spacer /></div></td>' +
						'<td class=btmR><div style="width:50px;height:20px"><spacer /></div></td>' +
					'</tr>' +
					'</table>' +
				'</td>' +
			'</tr>' +
			'</table>' +
			'</form>';
		// alert(sLs);
		document.getElementById('login-form').innerHTML = sLs;
		document.getElementById('login-form').style.display = "block";
		document.getElementById('login-form').style.top = document.body.scrollTop + (document.body.clientHeight-document.getElementById('login-form').offsetHeight)/2;
		document.getElementById('login-form').style.left = document.body.scrollLeft + (document.body.clientWidth-document.getElementById('login-form').offsetWidth)/2;
		document.getElementById('login-form-png').style.display = "block";
		document.getElementById('login-form-png').style.width = parseInt(document.getElementById('login-form').offsetWidth) - 30;
		document.getElementById('login-form-png').style.height = parseInt(document.getElementById('login-form').offsetHeight) - 40;
		document.getElementById('login-form-png').style.left = parseInt(document.getElementById('login-form').style.left) + 15;
		document.getElementById('login-form-png').style.top = parseInt(document.getElementById('login-form').style.top) + 20;
		if(document.getElementById('authFormLogin').value!=""){
			document.getElementById('authFormPassword').focus();
		} else{
			document.getElementById('authFormLogin').focus();
		}
		return false;
	}
	return false;
}

function waitAlert(str){
	// showAlert("<center>"+((str!="")?str:"Подождите, осуществляется загрузка...")+"<br><br><img src=/i/progress_bar.gif width=200 height=60 border=0></center>","butNone");
	return false;
}

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

function hideAlert(){
	hideFloatedWin('alert');
}

function hideLogin(){
	hideFloatedWin('login');
}

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'>Зарегистрироваться</a>";
}

function reposFloatedWin(name){
	if(document.getElementById(name+'-form')){
		document.getElementById(name+'-form').style.top =		document.body.scrollTop + (document.body.clientHeight-document.getElementById(name+'-form').offsetHeight)/2;
		document.getElementById(name+'-form').style.left =		document.body.scrollLeft + (document.body.clientWidth-document.getElementById(name+'-form').offsetWidth)/2;
	}
	if(document.getElementById(name+'-form-png')){
		document.getElementById(name+'-form-png').style.left = parseInt(document.getElementById(name+'-form').style.left) + 15;
		document.getElementById(name+'-form-png').style.top = parseInt(document.getElementById(name+'-form').style.top) + 20;
	}
}

