window.onload = function() 
{
	var obj = findClass('check_options','input');
	for(var z=0; z < obj.length; z++)
		{
			if(obj[z].checked)
			{
				obj[z].checked = false;
			}
		}
	if(document.getElementById('other'))
	{
		document.getElementById('other_text').style.display = 'none';
		document.getElementById('other').value = '';
	}
}

function load_url_main(args,class_block,ii)
{
var my_url = 'main_update.php';
if(class_block == 'add_plus')
{my_url = 'add_plus.php';}
// пишем http_request
	var current_page_name=location.href;

	if(current_page_name.indexOf('www.posobie.info')>=0)
	{
		var url="http://www.posobie.info"; 
	}
	else
	{
		if(current_page_name.indexOf('test.posobie.info')>=0)
		{
			var url="http://test.posobie.info"; 
		}
		else
		{
			var url="http://posobie.info"; 
		}
	}

	url+="/"+my_url;

	var http_request = false;
		if (window.XMLHttpRequest) 
		{
 			// Mozilla, Safari, ...
			http_request = new XMLHttpRequest();
			if (http_request.overrideMimeType) 
			{
				http_request.overrideMimeType('text/plain;charset=windows-1251');
			}
		} 
			else if (window.ActiveXObject) 
		{ 
			
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
		}
			if (!http_request) 
		{
			alert('Не вышло :( Невозможно создать экземпляр класса XMLHTTP ');
			return false;
		}
		http_request.onreadystatechange = function() { do_something_main(http_request,class_block,ii); };
		http_request.open('POST', url, true);
		http_request.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); // анти-кэш для IE
		http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');


//args += '&s='+encodeURIComponent(s_info);
http_request.send(args);

}
function do_something_main(http_request,class_block,ii)
{
	var browser=navigator.appName;
	if (http_request.readyState == 4)
	{
		if (http_request.status == 200)
		{
// ii - это номер редактируемого дива
var back_text = http_request.responseText;

var pattern1 = /\r\n|\r|\n/g;
var new_pattern1 = back_text.replace(pattern1,"");
var btl = new_pattern1.length;


			if(btl < '4' && class_block != 'interview' && class_block != 'add_plus' ) // Hacking attempt
			{
			window.location.reload();
			http_request.responseText = ''; // чтобы не пугать публику еденицей
			}
			if(class_block == 'interview')
			{
				if(new_pattern1 == 'interview')
				{
					if(document.getElementById('interview_block'))
					{
document.getElementById('interview_block').innerHTML = '<div class="rounded-corner-container" style="background:#F9FDFF; margin-top:10px">																											<div class="rounded-corner-1" style="background:#FFCEB7;"> </div>														<div class="rounded-corner-2" style="background:#FFCEB7;"> </div>															<div class="rounded-corner-3" style="background:#FFCEB7;"> </div>														<div class="rounded-corner-4" style="background:#FFCEB7;"> </div>														</div>																														<p class="genmed_b" style="font-size:15px; background:#FFCEB7; margin:0; padding:10px;"><b>Спасибо, Ваш голос принят!</b></p>																											<div class="rounded-corner-container" style="background:#F9FDFF;">														<div class="rounded-corner-4" style="background:#FFCEB7;"> </div>															<div class="rounded-corner-3" style="background:#FFCEB7;"> </div>															<div class="rounded-corner-2" style="background:#FFCEB7;"> </div>															<div class="rounded-corner-1" style="background:#FFCEB7;"> </div>														</div>';
					}
				}
			}
		}
		else
		{
			alert('С запросом возникла проблема.');
		}
	}
}

function send_results(ip, session_time)
{
	var some_checked = '';
	var subject = 'interview';
	var args = 'type='+subject+'&ip='+encodeURIComponent(ip)+'&session_time='+encodeURIComponent(session_time);
	var obj = findClass('check_options','input');
	for(var z=0; z < obj.length; z++)
		{
			if(obj[z].checked)
			{
				some_checked = '1';
				args += '&option'+(z+1)+'='+encodeURIComponent(obj[z].value);
			}
		}
	if(document.getElementById('other'))
	{
		args += '&other'+'='+encodeURIComponent(document.getElementById('other').value);
	}
	if(some_checked == '')
	{
		alert('Вы должны указать один из пунктов');	
	}
	else
	{
		load_url_main(args,subject,'1');
	}
}

function get_request(url,mode,position)
{
	var current_page_name=location.href;

	if(current_page_name.indexOf('www.posobie.info')>=0)
	{
		var url="http://www.posobie.info/"+url; 
	}
	else
	{
		if(current_page_name.indexOf('test.posobie.info')>=0)
		{
			var url="http://test.posobie.info/"+url; 
		}
		else
		{
			var url="http://posobie.info/"+url; 
		}
	}

	var http_request = false;

	if (window.XMLHttpRequest) 
	{
		// Mozilla, Safari, ...
		http_request = new XMLHttpRequest();

		if (http_request.overrideMimeType) 
		{
			http_request.overrideMimeType('text/xml');
			// Читайте ниже об этой строке
		}
	} 
	else if (window.ActiveXObject) 
	{ 
		http_request = new ActiveXObject("Msxml2.XMLHTTP");
	}

	if (!http_request) 
	{
		alert('Не вышло :( Невозможно создать экземпляр класса XMLHTTP1');
		return false;
	}
	http_request.open('GET', url, true);
	http_request.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
	http_request.send(null);
	if(mode == 'add_plus')
	{
		http_request.onreadystatechange = function() { add_plus_1(http_request,position); };
	}
}

function XmlHttp() {
	if (window.XMLHttpRequest) {
		try {
			return new XMLHttpRequest();
		} catch (e){}
	} else if (window.ActiveXObject) {
		try {
			return new ActiveXObject('Msxml2.XMLHTTP');
		} catch (e){
			try {
				return new ActiveXObject('Microsoft.XMLHTTP');
			} catch (e){}
		}
	}
	return null;
}

function ajax(url, param, callback, key) 
{
	if (document.getElementById('s_info'))
	{
		var sid = document.getElementById('s_info').innerHTML;
	}
	else
	{
		alert('ERROR: no sid');
		exit();
	}

	param += '&s=' + sid;

	if (url == null)
	{
		url = '../functions_ajax.php';
	}

	var param = encodeURI(param);

	var xml = XmlHttp()

	xml.open('POST', url, true);
	xml.onreadystatechange = function(e) { 
		if (xml.readyState == 4 && xml.status == 200) 
		{
			callback(xml.responseText, key); 
		}
	}

	xml.setRequestHeader("Accept-Charset", "windows-1251");
	xml.setRequestHeader("Accept-Language","ru, en");
	xml.setRequestHeader("Connection", "close");
	xml.setRequestHeader("Content-length", param.length);
	xml.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xml.send(param);
}

function ismaxlength(obj){
   var mlength=obj.getAttribute ? parseInt(obj.getAttribute("maxlength")) : ""
   if (obj.getAttribute && obj.value.length>mlength)
   obj.value=obj.value.substring(0,mlength)
}

function object(e)
{
	var obj;

	if (window.event) 
	{
		obj = window.event.srcElement;
	}
	else
	{
		obj = e.currentTarget;
	}

	return obj;
}

function error()
{
	alert('Вы не авторизованны! Введите ник и пароль, чтобы зайти в личный кабинет');
	window.location.reload();
}


function select_page_send(select, mode)
{
	if(mode == 'change_nick')
	{
		var url = '&type=show_nick_history&mode=' + mode + '&start=' + select + '&user_id=' + user_id;
		ajax('kabinet_not_logged_in.php', url, after_show_nick_history);
	}
	else
	{
		var url = '&mode=' + mode + '&start=' + select + '&s=' + document.getElementById('s_info').innerHTML;
		ajax('functions_ajax.php', url, block_back);
	}

}

function assembly(type, titul)
{
	var close = '<div style="padding-top:2px;text-align: right;"><span style="cursor:pointer;margin-right:5px;" onClick="block_off()" class="genmed_b">Закрыть</span></div>';	
	var top	= '<div style="margin-top:20px;font-size:20px;" class="genmed_b">' + titul + '</div>';	
	var nbsp = '<table id="body_tb" style="width:100%;"><tr><td>&nbsp;</tr></td></table>';

	var row = '<div align="center" style="margin-left:40px;margin-right:40px;">' + top + nbsp + '<table  align="center">';

	if (type == 'home')
	{
		for (i = 0; i < 5; i++)
		{
			row = row + '<tr><td align="center" id="img_' + i + '"></td><td id="info_' + i + '"></td><td id="img_' + (i + 5) + '"></td><td id="info_' + (i + 5) + '"></td></tr>';
		}
	}
	else if (type = 'row')
	{
		for (i = 0; i < 10; i++)
		{
			row = row + '<tr id="tr_' + i + '"><td style="font-size:10px;" id="row_' + i + '"></td><tr>';
		}
	}

	row = row + '</table>';

	if (type == 'home')
	{
		row = row + '<div id="line_gray" style="border-bottom:1px solid gray;">&nbsp;</div>';
	}

	row = row + '<div style="margin-bottom:20px;">&nbsp;</div><div style="*left:40px;margin-left:2px;padding:5px;position:absolute;bottom:0;" align="left" id="page"></div></div>';

	row = close + row;

	return row;
}

function assembly_detected()
{
	if (document.getElementById('img_0'))
	{
		return 'home';
	}
	else if (document.getElementById('tr_0'))
	{
		return 'row';
	}
	else
	{
		exit();	
	}
}

HEIGHT_OVERLAY = 0;
WIDTH = {'guest':650,'topic':700,'album':420,'journal':610,'delete':448};

function block_send(mode, type, titul)
{
	add_oo()
	opacity()

	document.getElementById('overlay').innerHTML = assembly(type, titul);

	overlay_width(mode)

	var url = 'mode=' + mode + '&start=1&s=' + document.getElementById('s_info').innerHTML;
	ajax('functions_ajax.php', encodeURI(url), block_back)
}

function overlay_width(mode)
{
	if (isNaN(parseInt(mode)))
	{
		WIDTH_PX = WIDTH[mode] + 'px';
	}
	else
	{
		WIDTH_PX = mode + 'px';
	}

	document.getElementById('overlay').style.display = 'none';
	document.getElementById('overlay').style.width = WIDTH_PX;
	document.getElementById('overlay').style.minWidth = WIDTH_PX;
	document.getElementById('overlay').style.maxWidth = WIDTH_PX;
}

function opacity()
{
	if (document.getElementById('opacity').className == 'opacity_on')
	{
		document.getElementById('opacity').className = 'opacity_off';
	}
	else
	{
		document.getElementById('opacity').className = 'opacity_on';
		document.getElementById('opacity').style.height = document.body.scrollHeight + 'px';
	}
}

function block_off()
{
	HEIGHT_PX = '0px';
	document.getElementById('overlay').innerHTML = '';
	document.getElementById('overlay').className = 'overlay_off';
	opacity()
	document.getElementById('overlay').style.height = '';
	HEIGHT_OVERLAY = 0;
}

function block_back(e)
{
	if (e != 'Hacking attempt')
	{
		var back = eval( '(' + e + ')' );

		type = assembly_detected();

		if (type == 'home')
		{
			var name = new Array('img_','info_');
		}
		else if (type = 'row')
		{
			var name = new Array('row_');
		}
		else
		{
			alert('Ошибка получения имён структуры блока');
		}

		for (j = 0;j < name.length; j++)
		{
			for (i = 0; i < 10; i++)
			{
				if (back[name[j] + i])
				{
					if (type == 'home')
					{
						document.getElementById(name[j] + i).style.display = '';
						document.getElementById(name[j] + i).innerHTML = back[name[j] + i];
					}
					else if (type == 'row')
					{
						document.getElementById('tr_' + i).style.display = '';	
						document.getElementById('row_' + i).style.borderBottomStyle = back['style_borderstyle_' + i];
						document.getElementById('row_' + i).style.borderBottomColor = back['style_color_' + i];
						document.getElementById('row_' + i).style.borderBottomWidth = back['style_px_' + i];
						document.getElementById(name[j] + i).innerHTML = back[name[j] + i];
					}	
					else
					{
						alert('Ошибка заполнения таблицы.');
					}
				}
				else
				{
					if (type == 'home')
					{
						document.getElementById(name[j] + i).innerHTML = '';
						document.getElementById(name[j] + i).style.display = 'none';
					}
					else if (type = 'row')
					{
						if (document.getElementById('tr_' + i))
						{
							document.getElementById('tr_' + i).style.display = 'none';	
							document.getElementById('row_' + i).innerHTML = '';
						}
					}
				}
			}
		}

		if (document.getElementById('line_gray'))
		{
			document.getElementById('line_gray').innerHTML = '';
			document.getElementById('line_gray').style.borderBottomColor = 'white';	
		}

		if (back['page'])
		{
			document.getElementById('page').innerHTML = back['page'];
		}

		overlay_size()

		if (back['page'])
		{
			var br = navigator.userAgent.toLowerCase().indexOf('msie');

			if (br != -1) // EPIC IE
			{
				var IE = 70;
			}
			else
			{	
				var IE = 0;
			}

			document.getElementById('page').style.marginLeft = '2px';
			document.getElementById('page').style.width = (document.getElementById('body_tb').clientWidth - (document.getElementById("page").style.paddingLeft.replace('px', '') * 2) - 4) - IE + 'px';
			document.getElementById('page').style.borderTopStyle = 'solid';
			document.getElementById('page').style.borderTopColor = 'gray';
			document.getElementById('page').style.borderTopWidth = '1px';
		}

		overlay_on('white')	
	}
	else
	{
		error()
	}
}

function overlay_on(color)
{
	document.getElementById('overlay').className = 'overlay_on';

	if (color)
	{
		document.getElementById('overlay').style.background = color;
	}
}

function overlay_size(reserve)
{	
	var	RESERVE = 0;

	if (reserve != false)
	{
		RESERVE = 50;
	}

	document.getElementById('overlay').style.display = '';

	document.getElementById('overlay').style.left = (document.body.clientWidth - document.getElementById('overlay').clientWidth) / 2 + 'px';

	if (HEIGHT_OVERLAY == 0)
	{
		HEIGHT_OVERLAY = document.getElementById('overlay').clientHeight;
		document.getElementById('overlay').style.height = (HEIGHT_OVERLAY + RESERVE) + 'px';
	}
	else
	{
		document.getElementById('overlay').style.height = (HEIGHT_OVERLAY + RESERVE) + 'px';
	}

	if ((document.documentElement.clientHeight - document.getElementById('overlay').clientHeight) > 0)
	{
		document.getElementById('overlay').style.top = document.getElementsByTagName( 'html' )[0].scrollTop + ((document.documentElement.clientHeight - document.getElementById('overlay').clientHeight) / 2) + "px";
	}
	else
	{
		document.getElementById('overlay').style.top = (document.getElementsByTagName( 'html' )[0].scrollTop + 10) + 'px';	
	}
}

function swipe(side)
{
	//if (!number)	//	для лимитированного скрола
	//{
	//	number = alignment();
	//}

	var obj = findClass('img_blocks','div');

	if (side == 'right')
	{
		for(i = (obj.length - 1); i > 0; i--)
		{
			if (obj[i].style.display != 'block' && obj[i - 1].style.display != 'none')
			{
				obj[i].style.display = 'block';

				if (document.getElementById('left_arrow') && document.getElementById('left_arrow').style.display != 'block')
				{
					document.getElementById('left_arrow').style.display = 'block';
				}

				if (!obj[i + 1] && document.getElementById('right_arrow') && document.getElementById('right_arrow').style.display != 'none')
				{
					document.getElementById('right_arrow').style.display = 'none';
				}

				for(i = 0; i < obj.length; i++)
				{
					if (obj[i].style.display != 'none')
					{
						obj[i].style.display = 'none';
						exit();	
					}
				}
				
				alert('FATAL ERROR: RIGHT');
			}	
		}
	}
	else if (side == 'left')
	{
		for(i = 0; i < obj.length; i++)
		{
			if (obj[i].style.display != 'block' && obj[i + 1].style.display != 'none')
			{
				obj[i].style.display = 'block';

				if (!obj[i - 1] && document.getElementById('left_arrow') && document.getElementById('left_arrow').style.display != 'none')
				{
					document.getElementById('left_arrow').style.display = 'none';
				}

				for(i = (obj.length - 1); i > 0; i--)
				{
					if (obj[i].style.display != 'none' && obj[i - 1].style.display != 'none')
					{
						obj[i].style.display = 'none';

						if (obj[i + 1] && obj[i + 1].style.display != 'block' && document.getElementById('right_arrow') && document.getElementById('right_arrow').style.display != 'block')
						{
							document.getElementById('right_arrow').style.display = 'block';
						}

						exit();	
					}
				}

				alert('FATAL ERROR: LEFT');
			}
		}
	}
}

function alignment(align)
{
	var TAPE = document.getElementById('tape').clientWidth;
	var IMG_NUMBER = 0;
	var IMG_WIDTH = 0;
	var IMG_MAX_NUMBER = 0;
	var IMG_MAX_WIDTH = 0;
	var MARGIN = 0;	
	var MARGIN_END = 0;

	if (document.getElementById('img_0'))
	{
		MARGIN = Number(document.getElementById('img_0').style.marginLeft.replace('px', '')) + Number(document.getElementById('img_0').style.marginRight.replace('px', ''));
	}

	while(document.getElementById('img_' + IMG_NUMBER))
	{
		IMG_WIDTH = IMG_WIDTH + document.getElementById('img_' + IMG_NUMBER).clientWidth + MARGIN;
	
		if (IMG_WIDTH <= TAPE) //> 
		{
			IMG_MAX_NUMBER++;
			IMG_MAX_WIDTH = IMG_WIDTH;
		}
		else
		{
			break;
		}

		IMG_NUMBER++
	}	

	if (align == 'center')
	{
		while(document.getElementById('img_' + IMG_NUMBER))
		{
			document.getElementById('img_' + IMG_NUMBER).style.display = 'none';

			if (document.getElementById('right_arrow') && document.getElementById('right_arrow').style.display != 'block')
			{
				document.getElementById('right_arrow').style.display = 'block';
			}

			IMG_NUMBER++
		}

		MARGIN_END = ((((TAPE - IMG_MAX_WIDTH) + (IMG_MAX_NUMBER * MARGIN)) / IMG_MAX_NUMBER) / 2) + 'px';
		IMG_NUMBER = 0;
	
		while(document.getElementById('img_' + IMG_NUMBER) && IMG_MAX_NUMBER + 1 > IMG_NUMBER)
		{
			document.getElementById('img_' + IMG_NUMBER).style.marginRight = MARGIN_END;
			document.getElementById('img_' + IMG_NUMBER).style.marginLeft = MARGIN_END;
			IMG_NUMBER++
		}
	}
	else
	{
		return IMG_MAX_NUMBER;
	}
}

function add_oo()
{
	if (!document.getElementById('opacity') && !document.getElementById('overlay'))
	{
		var div = document.createElement("div")

		div.innerHTML +='<div id="opacity" style="overflow-x: hidden; overflow-y: hidden; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; position: fixed; display: block; *position: absolute;"></div><div id="overlay" align="left" style="margin: 0px; overflow: hidden; display:block; position: absolute;"></div>';

		document.body.insertBefore(div, document.body.firstChild);
	}
}


