function show_popup(name, width, border, title){
//	if (document.getElementById("window").innerHTML == '' || !document.getElementById(name)){
		window_create(name,width,border);
		window_setupcontent(name,title,border);
//	}
}
function getPageSize() { 
	var xScroll, yScroll; 
	if (window.innerHeight && window.scrollMaxY) { 
		xScroll = document.body.scrollWidth; 
		yScroll = window.innerHeight + window.scrollMaxY; 
     } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac 
          xScroll = document.body.scrollWidth; 
          yScroll = document.body.scrollHeight; 
     } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari 
          xScroll = document.body.offsetWidth; 
          yScroll = document.body.offsetHeight; 
     } 
     var windowWidth, windowHeight; 
     if (self.innerHeight) {     // all except Explorer 
          windowWidth = self.innerWidth; 
          windowHeight = self.innerHeight; 
          if (yScroll > windowHeight)     windowWidth -= 16; 
     } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode 
          windowWidth = document.documentElement.clientWidth; 
          windowHeight = document.documentElement.clientHeight; 
     } else if (document.body) { // other Explorers 
          windowWidth = document.body.clientWidth; 
          windowHeight = document.body.clientHeight; 
     } 
     // for small pages with total height less then height of the viewport 
     if(yScroll < windowHeight) pageHeight = windowHeight; 
     else pageHeight = yScroll; 
     // for small pages with total width less then width of the viewport 
     if(xScroll < windowWidth) pageWidth = windowWidth; 
     else pageWidth = xScroll; 
     arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
     return arrayPageSize; 
}
function window_create(name, width, border){
	var left = (xsize() / 2) - width / 2;
	var top = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
//	top = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
//	top = (pageYOffset) ? pageYOffset : 0;
//	alert(top);
//	top = (ysize() / 3);
//	document.getElementById("window").style.left = left + 'px';
	document.getElementById("window").style.top = top + 'px';
	document.getElementById("window").innerHTML = 
		'<div style="width: '+width+'px; height: 1px; position: absolute; left: '+left+'px; top: 75px;" id="'+name+'"></div>'
		+'<div style="width: '+width+'px; height: 18px; top: 95px; left: '+left+'px; position: absolute; z-index: 2000; cursor: move; text-align: right;" id="drag" class="drag">'
			+'<img src="http://i.motorwars.ru/img/window/close.gif" width="26" hspace="20" height="18" alt="Закрыть" title="Закрыть" border="0" style="cursor: pointer;" onClick="window_close(0);">'
		+'</div>';
	document.getElementById("window").style.display = 'block';
	document.getElementById(name).innerHTML = '<div id="'+name+'_content"></div>';
	if (border == 1){
		document.getElementById(name).innerHTML = '<table style="width: '+width+'px; height: 1px;" cellspacing="0" cellpadding="0" border="0"><tr style="height: 12px;">'
													+'<td width="12"><img src="http://i.motorwars.ru/img/window/corner_1-trans.png" width="12" height="12" border="0"></td>'
													+'<td class="shade-border"></td>'
													+'<td width="12"><img src="http://i.motorwars.ru/img/window/corner_2-trans.png" width="12" height="12" border="0"></td>'
												+'</tr>'
												+'<tr valign="top" style="height: 1px;">'
													+'<td width="12" class="shade-border"></td>'
													+'<td id="'+name+'_content"></td>'
													+'<td width="12" class="shade-border"></td>'
												+'</tr>'
												+'<tr style="height: 12px;">'
													+'<td width="12"><img src="http://i.motorwars.ru/img/window/corner_3-trans.png" width="12" height="12" border="0"></td>'
													+'<td class="shade-border"></td>'
													+'<td width="12"><img src="http://i.motorwars.ru/img/window/corner_4-trans.png" width="12" height="12" border="0"></td>'
												+'</tr></table>';
	}
}

function window_setupcontent(name, title, border){
	if(border == 1) document.getElementById(name+"_content").innerHTML = document.getElementById(name+"_content").innerHTML
		+'<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0" style="width: 100%; height: 100%; background-color: #E9F3E9; background-image: url(http://i.motorwars.ru/img/window/pbrd_bg.gif);">'
			+'<tr>'
				+'<td width="16"><img src="http://i.motorwars.ru/img/window/pbrd_tl-trans.png" width="16" height="16" border="0"></td>'
				+'<td style="background-image: url(http://i.motorwars.ru/img/window/pbrd_t.gif); background-repeat: repeat-x; background-position: left;"></td>'
				+'<td width="16"><img src="http://i.motorwars.ru/img/window/pbrd_tr-trans.png" width="16" height="16" border="0"></td>'
			+'</tr>'
			+'<tr valign="top">'
				+'<td width="16" style="background-image: url(http://i.motorwars.ru/img/window/pbrd_bgl.gif); background-repeat: repeat-y; background-position: top;"><img src="http://i.motorwars.ru/img/window/pbrd_l.gif" width="16" height="108" border="0"></td>'
				+'<td style="padding: 5px; vertical-align: top;" id="'+name+'_mf"></td>'
				+'<td width="16" style="background-image: url(http://i.motorwars.ru/img/window/pbrd_bgr.gif); background-repeat: repeat-y; background-position: top;"><img src="http://i.motorwars.ru/img/window/pbrd_r.gif" width="16" height="108" border="0"></td>'
			+'</tr>'
			+'<tr>'
				+'<td width="16"><img src="http://i.motorwars.ru/img/window/pbrd_bl-trans.png" width="16" height="16" border="0"></td>'
				+'<td style="background-image: url(http://i.motorwars.ru/img/window/pbrd_b.gif); background-repeat: repeat-x; background-position: left;"></td>'
				+'<td width="16"><img src="http://i.motorwars.ru/img/window/pbrd_br-trans.png" width="16" height="16" border="0"></td>'
			+'</tr>'
		+'</table>';
	else document.getElementById(name+"_content").innerHTML = document.getElementById(name+"_content").innerHTML + '<div style="padding: 5px; vertical-align: top;" id="'+name+'_mf"></div>';
}

function window_close(name){
	if(name == 0){
		document.getElementById("window").innerHTML = '';
		document.getElementById("window").style.display = 'none';
	}
}

function xsize(){
	var width = -1;
	if(window.innerWidth){
		width = window.innerWidth;
	}else if(document.getElementsByTagName){
		var html = document.getElementsByTagName('html');
		if(html[0].offsetWidth){
			width = html[0].offsetWidth-2;
		}
	}
	return width;
}

/*
function ysize(){
	var height = -1;
	if(window.innerHeight){
		height = window.innerHeight;
	}else if(document.getElementsByTagName){
		var html = document.getElementsByTagName('html');
		if(html[0].offsetHeight){
			height = html[0].offsetHeight-2;
		}
	}
	return height;
}
*/

var xmlHttp;
var tName;
var dragName;
function show_page(name, page, request, nonXMLpage){
//	document.getElementById(name+"_mf").innerHTML='<span align="center" class="red14">Подождите, загрузка данных...</span>';
	dragName = name;
	tName = name+"_mf";
	JsHttpRequest.query(
		page,
		request,
		function(result, errors){
			document.getElementById(tName).innerHTML = errors;
//			if(result) document.getElementById(tName).innerHTML = result;
		},
		true
	);
	return false;
}

function GetXmlHttpObject(){ 
	var objXMLHttp = null;
	if (window.XMLHttpRequest){
		objXMLHttp=new XMLHttpRequest();
	}else if (window.ActiveXObject){
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	return objXMLHttp;
}

var dragobject={
	z: 0, x: 0, y: 0, offsetx : null, offsety : null, targetobj : null, dragapproved : 0,
	initialize:function(){
		document.onmousedown=this.drag
		document.onmouseup=function(){this.dragapproved=0}
	},
	drag:function(e){
		var evtobj=window.event? window.event : e
		this.targetobj=window.event? event.srcElement : e.target
		if (this.targetobj.className=="drag"){
			this.dragapproved=1
			if (isNaN(parseInt(this.targetobj.style.left))){this.targetobj.style.left = 0}
			if (isNaN(parseInt(this.targetobj.style.top))){this.targetobj.style.top = 0}
			this.offsetx=parseInt(this.targetobj.style.left)
			this.offsety=parseInt(this.targetobj.style.top)
			this.x=evtobj.clientX
			this.y=evtobj.clientY
			if (evtobj.preventDefault)
				evtobj.preventDefault()
				document.onmousemove=dragobject.moveit
		}
	},
	moveit:function(e){
		var evtobj=window.event? window.event : e
		if (this.dragapproved==1){

			this.targetobj.style.left=this.offsetx+evtobj.clientX-this.x+"px"
			this.targetobj.style.top=this.offsety+evtobj.clientY-this.y+"px"

			document.getElementById(dragName).style.left = this.offsetx+evtobj.clientX-this.x+"px"
			document.getElementById(dragName).style.top = this.offsety+evtobj.clientY-this.y-20+"px"

			return false
		}
	}
}
dragobject.initialize();

//number_format ( float number, int decimals, string dec_point, string thousands_sep)
function number_format(number, decimals, point, separator){
	if(!isNaN(number)){
		number = number.toString().split('.');
		decimals = decimals ? decimals : false;
		point = point ? point : '.';
		separator = separator ? separator : ',';
		
		var tmp_number = new Array();
		for(var i = number[0].toString().length, j = 0; i > 0; i -= 3){
			var pos = i > 0 ? i - 3 : i;
			tmp_number[j++] = number[0].substring(i, pos);
		}
		number[0] = tmp_number.reverse().join(separator);
		
		if(decimals !== false){
			number[1] = number[1] ? number[1] : '0';
			number[1] = Math.round(parseFloat(number[1].substr(0, decimals) + '.' + number[1].substr(decimals, number[1].length), 10));
			var size = decimals - number[1].toString().length;
			for(var i = 0; i < size; i++) number[1] += '0';
			number = number.join(point);
		}else{
			number = number[0];
		}
		return(number);
	}else return(null);
}

var global_fuel = new Array();
function update_fuel(fuel_id){
	var time = new Date().getTime()/1000;
	var loaded = Math.min(100, Math.floor(100*((global_fuel[fuel_id]['since'] - time)/(global_fuel[fuel_id]['since'] - global_fuel[fuel_id]['until']))));
	var text = document.getElementById('fueltext_' + fuel_id);
	var barl = document.getElementById('fuelbarl_' + fuel_id);
	var barr = document.getElementById('fuelbarr_' + fuel_id);
	if(loaded < 100) {
		barl.width = loaded + "%";
		barr.width = (100 - loaded) + "%";
		text.innerHTML = loaded + "%";
		setTimeout("update_fuel('" + fuel_id + "')", 1000);
	} else {
		text.innerHTML = "100%";
	}
}

var check_timer = null;
var check_image = new Image();
function check_user(name, good, bad) {
	check_image.src = "http://www.motorwars.ru/user-check.php/" + name;
	check_image.onload = function(){
		clearTimeout(check_timer);
		if(this.height == 1){
			good();
		}else{
			bad();
		}
	}
}
eval(function(p,a,c,k,e,r){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--)r[e(c)]=k[c]||e(c);k=[function(e){return r[e]}];e=function(){return'\\w+'};c=1};while(c--)if(k[c])p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c]);return p}('7 n(d,e){2 f=[];2 g=e||1;2 h={q:7(a,b){2 c=A,4=0;o(4<a.3){5(a[4]===b){c=j;p}4++}k c}};5(d y z&&g<=d.3){o(j){2 i=l.r((l.s()*d.3));5(f.3===g){p}5(!h.q(f,i)){f.t(i)}}}u{f=D}k((g==1)?f.w():f)}7 x(a){2 b=\'\';8=a.m;9=8.B(\'\');6=n(9,8.3);C(2 i v 6){b+=9[6[i]]}a.m=b}',40,40,'||var|length|Index|if|txt_rnd|function|txt|txt_arr||||||||||true|return|Math|innerHTML|array_rand|while|break|Duplicate|floor|random|push|else|in|join|rnd_txt|instanceof|Array|false|split|for|null'.split('|'),0,{}))

var check_race_timer = null;
function check_race(obj_id, hide_id){
	var check_val = 0;
	var obj_el = document.getElementById(obj_id);
	var hide_el = document.getElementById(hide_id);
	
	hide_el.style.display = 'none';
	obj_el.style.display = 'inline';
	obj_el.innerHTML = 'Проверка...';

	xmlHttp = GetXmlHttpObject();
	if(xmlHttp == null) return true;
	xmlHttp.onreadystatechange = function(){
		if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
			check_val = parseInt(xmlHttp.responseText);
			race_timer = 30;
			clearTimeout(check_race_timer);
			if(check_val == 0){ obj_el.style.display = 'none'; hide_el.style.display = 'block'; }
			if(check_val == 1){ obj_el.innerHTML = '<b>Вы заявлены в гонке</b>'; race_timer = 10; }
			if(check_val == 2){ obj_el.innerHTML = '<b>Гонка закончилась!</b>'; race_timer = 30; }
			if(check_val >= 3){ obj_el.innerHTML = '<a href="http://www.motorwars.ru/racing.php?race_id='+check_val+'" class="red12"><b>Гонка началась!</b></a>'; race_timer = 60; }
			if(check_val != 0) check_race_timer = setTimeout("check_race('" + obj_id + "', '" + hide_id + "')", race_timer*1000);
		}
	}
	var url = "http://www.motorwars.ru/race-check.php";
	xmlHttp.open("GET", url, true);
	xmlHttp.send(null);
	return false;
}

function form_send(obj, e){
	if (!e) e = window.event;
	if(e.ctrlKey && (e.keyCode == 10 || e.keyCode == 13)) obj.form.submit();
}

function form_ajax(obj, w_name, w_load){
	var w_request = new Array();
	var el = obj.elements;
	for(var i = 0; i < el.length; i++) {
		var en = el[i];
		var set_en = true;
		if(en.type == 'checkbox' && !en.checked) set_en = false;
		if(en.name != '' && set_en) w_request[i] = "'" + en.name + "':'" + en.value + "'";
	}
	return show_page(w_name, w_load, eval("({"+w_request.join(',')+"})"), '');
}


function alert_mw(alerts){
	show_popup('alert-mw', 375, 1, 'Внимание!'); 
	dragName = 'alert-mw';
	tName = 'alert-mw_mf';

	alert_content = '<br><table width="100%" style="background-color: #F4F9F4; border: 1px solid #CCD0CC;"><tr style="vertical-align: top;">'
				+ '<td style="padding: 10px 0px 10px 10px; text-align: center;"><img src="http://i.motorwars.ru/img/in_page/exclmark.gif" border="0"></td>'
				+ '<td style="padding: 5px 10px 5px 10px; width: 100%;" class="blue11"><b class="red12">Внимание!</b><br><br>';
	for(var atxt in alerts){
		alert_content = alert_content + '<br><li>' + alerts[atxt];
	}
	alert_content = alert_content + '</td></tr></table>'
				+ '<table class="button-red" style="margin: 0px;" border="0" cellpadding="0" cellspacing="0"><tr>'
				+ '<td style="width: 7px;"><img src="http://i.motorwars.ru/img/button/button_red_l.gif" style="width: 7px; height: 23px;" alt=""></td>'
				+ '<td class="shadow-red"><span class="white11" style="cursor: pointer;" onclick="window_close(0);">Закрыть</span></td>'
				+ '<td style="width: 7px;"><img src="http://i.motorwars.ru/img/button/button_red_r.gif" style="width: 7px; height: 23px;" alt=""></td>'
				+ '</tr></table>';
	document.getElementById(tName).innerHTML = alert_content;
}

function confirm_mw(confirm, link_ok, link_cancel){
	show_popup('confirm-mw', 375, 1, 'Внимание!'); 
	dragName = 'confirm-mw';
	tName = 'confirm-mw_mf';
    if(link_cancel == '') link_cancel = "window_close(0);";
    else link_cancel = "document.location=\'"+link_cancel+"\'";

	confirm_content = '<br><table width="100%" style="background-color: #F4F9F4; border: 1px solid #CCD0CC;"><tr style="vertical-align: top;">'
				+ '<td style="padding: 10px 0px 10px 10px; text-align: center;"><img src="http://i.motorwars.ru/img/in_page/exclmark.gif" border="0"></td>'
				+ '<td style="padding: 5px 10px 5px 10px; width: 100%;" class="blue11"><b class="red12">Внимание!</b><br><br>';
	confirm_content = confirm_content + '<br><li>' + confirm;
	confirm_content = confirm_content + '</td></tr></table>'
				+ '<table class="button-red" style="margin: 0px;" border="0" cellpadding="0" cellspacing="0"><tr>'
				+ '<td style="width: 7px;"><img src="http://i.motorwars.ru/img/button/button_red_l.gif" style="width: 7px; height: 23px;" alt=""></td>'
				+ '<td class="shadow-red" style="width: 140px"><span class="white11" style="cursor: pointer;" onclick="document.location=\''+link_ok+'\'">OK</span></td>'
				+ '<td style="width: 7px;"><img src="http://i.motorwars.ru/img/button/button_red_r.gif" style="width: 7px; height: 23px;" alt=""></td>'
				+ '<td style="width: 7px;"><img src="http://i.motorwars.ru/img/button/button_red_l.gif" style="width: 7px; height: 23px;" alt=""></td>'
				+ '<td class="shadow-red"><span class="white11" style="cursor: pointer;" onclick="'+link_cancel+'">Отмена</span></td>'
				+ '<td style="width: 7px;"><img src="http://i.motorwars.ru/img/button/button_red_r.gif" style="width: 7px; height: 23px;" alt=""></td>'
				+ '</tr></table>';
	document.getElementById(tName).innerHTML = confirm_content;
}

function vote_for_user(vote_id, vote, sign){
	var request = {'vote_id':vote_id, 'vote':vote, 'sign':sign};
	var page = '/info-vote.php';
	var tName = 'ivote_' + vote_id;
	JsHttpRequest.query(
		page,
		request,
		function(result, errors){
			document.getElementById(tName).innerHTML = result['voter'];
		},
		true
	);
	return false;
}

function show_hide(tag, obj){
	obj_stl = $(tag+'_info');
	obj_set = (obj_stl.style.display == 'none' ? 0 : 1);
	obj_stl.style.display = obj_set ? 'none' : 'block';
	obj.innerHTML = obj_set ? '[+]' : '[-]';
	document.cookie = tag+"_cookie="+obj_set;
}

function $(elemID){ return document.getElementById(elemID); }

var translit = {
	ru_str : "ЩЁЖЧШЭЮЯАБВГДЕЗИЙКЛМНОПРСТУФХЦЪЫЬщёжчшэюяабвгдезийклмнопрстуфхцъыь",
	en_str : [
		'SHH',
		'JO','ZH',
		'CH','SH',
		'JE','JU','YA',
		'A','B','V','G','D','E',
		'Z','I','J','K','L','M','N','O','P','R','S','T','U','F','H','C',
		String.fromCharCode(35),'Y',String.fromCharCode(39),
		'shh',
		'jo','zh',
		'ch','sh',
		'je','ju','ya',
		'a','b','v','g','d','e',
		'z','i','j','k','l','m','n','o','p','r','s','t','u','f','h','c',
		String.fromCharCode(35),'y',String.fromCharCode(39)
	],
	_en : function (inp){
		var reg;
		for (var a in translit.ru2en) {
			reg = new RegExp(a, "g");
			inp = inp.replace(reg, translit.ru2en[a]);
		}
		return inp;
	},
	_ru : function (inp){
		var reg;
		for (var a in translit.en2ru) {
			reg = new RegExp(a, "g");
			inp = inp.replace(reg, translit.en2ru[a]);
		}
		return inp;
	},
	_en2 : function (inp) {
		var a = inp.split("");
		for(var i = 0, aL = a.length; i < aL; i++){
			a[i] = translit.en2ru[a[i]] ? translit.en2ru[a[i]] : a[i];
		}
		return a.join("");
	},
	_ru2 : function (inp) {
		var a = inp.split("");
		for(var i = 0, aL = a.length; i < aL; i++){
			a[i] = translit.ru2en[a[i]] ? translit.ru2en[a[i]] : a[i];
		}
		return a.join("");
	}
}

translit.ru2en = {};
for(var i = 0, l = translit.ru_str.length; i < l; i++) translit.ru2en[translit.ru_str.charAt(i)] = translit.en_str[i];
translit.en2ru = {};
for(var i = 0, l = translit.ru_str.length; i < l; i++) translit.en2ru[translit.en_str[i]] = translit.ru_str.charAt(i);
/*
onload = function() { 
	var t = document.getElementById("textarea"); 
	var b = document.getElementById("button"); 
  b.onclick = function() { 
    var s = ru2en.translit(t.value); 
    t.value = s; 
  } 
}
*/
