function getCity(city_id) {
	var url = '/ajax/city/' + city_id + '/';
	var ajax = new Ajax.Updater({ 
		success: 'city-info' }, url, { method: 'get', onFailure: reportCityError
	});
}

function reportCityError(request) {
	$('city-info').update('Ошибка');
}


function IWantToVote(name, city) {
	if (name != 'Имя' && city != 'Город') {
		$text = '<input type="hidden" id="name" name="name" value="' + name + '" />' +
		'<input type="hidden" id="city" name="city" value="' + city + '" />' +
		'<div class="top"></div><div class="bg"><div id="captcha_block"><h2><strong>Введите цифры:</strong></h2><fieldset class="captcha"><img id="captcha_pic" src="/_get_loader.php?script=captcha&rand=' + Math.random() + '" + height="24" hspace="5" width="125" ><p>Пожалуйста, введите цифры, указанные на картинке.</p></fieldset><fieldset class="input"><input type="text" maxlength="4" id="captcha" value="" class="txt" /><input type="image" src="i/btn/confirm.gif" value="Подтвердить" onclick="javascript: vote(document.getElementById(\'name\').value, document.getElementById(\'city\').value, document.getElementById(\'captcha\').value); return false;" /></fieldset></div></div><div class="bot"></div>';
		$('vote-form').addClassName('active');
		$('vote-form').update($text);		
	}
}

/*
function IWantToVote(name, city) {
	if (name != 'Имя' && city != 'Город') {
		$text = '<input type="hidden" id="name" name="name" value="' + name + '" />' +
		'<input type="hidden" id="city" name="city" value="' + city + '" />' +
		'<div class="top"></div><div class="bg"><h2><strong>Введите цифры:</strong></h2><fieldset class="captcha"><img id="captcha_pic" src="/zaputina_captha2.php&rand=' + Math.round(100+Math.random()) + '" + height="24" hspace="5" width="125" ><p>Пожалуйста, введите цифры, указанные на картинке.</p></fieldset><fieldset class="input"><input type="text" maxlength="4" id="captcha" value="" class="txt" /><input type="image" src="i/btn/confirm.gif" value="Подтвердить" onclick="javascript: vote(document.getElementById(\'name\').value, document.getElementById(\'city\').value, document.getElementById(\'captcha\').value); return false;" /></fieldset>		</div><div class="bot"></div>';
		$('vote-form').update($text);
	}
}*/


function IWantToVote_(name, city) {
	if (name != 'Имя' && city != 'Город') {
		$text = '<input type="hidden" id="name" name="name" value="' + name + '" />' +
		'<input type="hidden" id="city" name="city" value="' + city + '" />' +
		'<div class="top"></div><div class="bg"><h2><strong>Ваш e-mail:</strong></h2><fieldset class="captcha"><p>Необязательное поле.</p></fieldset><fieldset class="input"><input type="text" maxlength="15" id="email" value="" name="email" class="txt" /></div>' +
		'<div class="bg"><h2><strong>Введите цифры:</strong></h2><fieldset class="captcha"><img id="captcha_pic" src="/_get_loader.php?script=captcha&rand=' + Math.random() + '" + height="24" hspace="5" width="125" ><p>Пожалуйста, введите цифры, указанные на картинке.</p></fieldset><fieldset class="input"><input type="text" maxlength="4" id="captcha" value="" class="txt" /><input type="image" src="i/btn/confirm.gif" value="Подтвердить" onclick="javascript: vote(document.getElementById(\'name\').value, document.getElementById(\'city\').value, document.getElementById(\'captcha\').value, document.getElementById(\'email\').value); return false;" /></fieldset>		</div><div class="bot"></div>';
		$('vote-form').update($text);
	}
}

function vote(name, city, captcha)  {
	if (captcha) {
	var url = '/ajax/vote/name/' + encodeURIComponent(name) + '/city/' + encodeURIComponent(city) + '/captcha/' + captcha + '/';
	
	var ajax = new Ajax.Request(url, { method: 'get', onComplete: voteResponse });
		
	}
}


function _vote(name, city, captcha, email)  {
	if (captcha) {
	var url = '/ajax/vote/name/' + encodeURIComponent(name) + '/city/' + encodeURIComponent(city) + '/email/' + encodeURIComponent(email) + '/captcha/' + captcha + '/';
	
	var ajax = new Ajax.Request(url, { method: 'get', onComplete: voteResponse });
		
	}
}

function voteResponse(oReq) {

	switch(oReq.responseText) {
		case '0': // captcha error
			document.getElementById('captcha_pic').src = '/_get_loader.php?script=captcha&rand=' + Math.random();
			break;
			
		case '1': // ok
			$('vote-form').remove();
			incrementTotalVotes();
			$('home').addClassName('all');
			break;
		
		case '2': // already voted
			document.getElementById('captcha_block').innerHTML='<h2 class="done">Голос не принят. С этого компьютера уже отправляли голос в поддержку президента Путина.</h2>';
			break;
			
	}
}

function incrementTotalVotes() {
	var new_total_votes = parseInt($('total-votes').innerHTML)+1;
	$('total-votes').update(new_total_votes);	
}

/*
* we need win1251, not that utf stuff
*/
function encodeURIComponent(str){
    var trans = [];
    for (var i = 0x410; i <= 0x44F; i++) trans[i] = i - 0x350; // А-Яа-я
    trans[0x401] = 0xA8; // Ё
    trans[0x451] = 0xB8; // ё
    var ret = [];
    for (var i = 0; i < str.length; i++){
         var n = str.charCodeAt(i);
         if (typeof trans[n] != 'undefined') n = trans[n];
         if (n <= 0xFF) ret.push(n);
    }
    return escape(String.fromCharCode.apply(null, ret)).replace(/\+/g, '%2B'); // +
}

nn4 = document.layers;
ie4 = document.all;
nn6 = document.getElementById && !ie4;

function show_add_photo(w, h) { 

	var day= new Date(); 
	var id = day.getTime(); 
	var ww = parseInt(w);
	var wh = parseInt(h);

	if (nn4 || ie4 || nn6){
		posX = Math.round((screen.width - ww) / 2);
		posY = Math.round((screen.height - wh) / 2);
		posCode = (nn4 || nn6) ? "screenX=" + posX + ",screenY=" + posY : "left=" + posX + ",top=" + posY;
	}else{
		posCode = "";
	}
	
 	var params = 'width='+ww+',height='+wh+',status=no,toolbar=no,menubar=no,scrollbars=no,'+posCode; 
 	
 	var msg='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' +
 	'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru" id="popup">' +
 	'<head><meta http-equiv="Content-Type" content="text/html; Charset=Windows-1251" /><title>За Путина!</title><link rel="stylesheet" type="text/css" media="all" href="s/style.css" /><!--[if lt IE 8]><link rel="stylesheet" type="text/css" media="all" href="s/ie.css" /><![endif]-->' +
	'</head><body><form action="/_post_loader.php" method="post" enctype="multipart/form-data"><fieldset>' +
	'<input type="hidden" name="script" value="zaputina_save_photos" />' +
	'<label for="name">Имя:</label><input type="text" id="name" name="name" />' +
	'</fieldset><fieldset>' +
	'<label for="city">Город:</label><input type="text" id="city" name="city" />' +
	'</fieldset><fieldset>' +
	'<label for="homepage">Страница в сети:</label><input type="text" id="homepage" name="homepage" value="http://" />' +
	'</fieldset><fieldset>' +
	'<label for="photo">Фотка:</label><input type="file" id="photo" name="photo" />' +
	'</fieldset><fieldset>' +
	'<input type="submit" value="Загрузить" />' +
	'</fieldset></form></body></html>';

	var win = open('',id,params); 
	win.document.write(msg); 
	win.document.close(); 
}


function show_in_blog(w, h) { 

	var day= new Date(); 
	var id = day.getTime(); 
	var ww = parseInt(w);
	var wh = parseInt(h);

	if (nn4 || ie4 || nn6){
		posX = Math.round((screen.width - ww) / 2);
		posY = Math.round((screen.height - wh) / 2);
		posCode = (nn4 || nn6) ? "screenX=" + posX + ",screenY=" + posY : "left=" + posX + ",top=" + posY;
	}else{
		posCode = "";
	}
	
 	var params = 'width='+ww+',height='+wh+',status=no,toolbar=no,menubar=no,scrollbars=no,'+posCode; 
 	
 	var msg='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' +
 	'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru" id="popup">' +
 	'<head><meta http-equiv="Content-Type" content="text/html; Charset=Windows-1251" /><title>За Путина!</title><link rel="stylesheet" type="text/css" media="all" href="s/style.css" /><!--[if lt IE 8]><link rel="stylesheet" type="text/css" media="all" href="s/ie.css" /><![endif]-->' +
	'</head><body>' +
	'Код для вставки в блог:<br><textarea cols=35 rows=7><object width="770" height="310"><param name="allowScriptAccess" value="always" /><param name="allowFullScreen" value="true" /><param name="movie" value="http://www.zaputina.ru/inblog.swf" /><param name="menu" value="false" /><param name="quality" value="high" /><param name="scale" value="noscale" /><param name="bgcolor" value="#000000" /><embed src="http://www.zaputina.ru/inblog.swf" menu="false" quality="high"  scale="noscale" bgcolor="#000000" width="770" height="310" name="flv_player"  align="middle" allowScriptAccess="always" allowFullScreen="true"  type="application/x-shockwave-flash" /></object></textarea>' +
	'' +
	'</body></html>';

	var win = open('',id,params); 
	win.document.write(msg); 
	win.document.close(); 
}

function send_to_friend(w, h) { 

	var day= new Date(); 
	var id = day.getTime(); 
	var ww = parseInt(w);
	var wh = parseInt(h);

	if (nn4 || ie4 || nn6){
		posX = Math.round((screen.width - ww) / 2);
		posY = Math.round((screen.height - wh) / 2);
		posCode = (nn4 || nn6) ? "screenX=" + posX + ",screenY=" + posY : "left=" + posX + ",top=" + posY;
	}else{
		posCode = "";
	}
	
 	var params = 'width='+ww+',height='+wh+',status=no,toolbar=no,menubar=no,scrollbars=no,'+posCode; 
 	

	var win = open('/ajax/friend/',id,params); 
	win.document.close(); 
}


function show_pchess(w, h) { 

	var day= new Date(); 
	var id = day.getTime(); 
	var ww = parseInt(w);
	var wh = parseInt(h);

	if (nn4 || ie4 || nn6){
		posX = Math.round((screen.width - ww) / 2);
		posY = Math.round((screen.height - wh) / 2);
		posCode = (nn4 || nn6) ? "screenX=" + posX + ",screenY=" + posY : "left=" + posX + ",top=" + posY;
	}else{
		posCode = "";
	}
	
 	var params = 'width='+ww+',height='+wh+',status=no,toolbar=no,menubar=no,scrollbars=no,'+posCode; 
 	

	var win = open('http://putin-chess.ru/dniputinchess0.html',id,params); 
}

function show_pchess_help(w, h) { 

	var day= new Date(); 
	var id = day.getTime(); 
	var ww = parseInt(w);
	var wh = parseInt(h);

	if (nn4 || ie4 || nn6){
		posX = Math.round((screen.width - ww) / 2);
		posY = Math.round((screen.height - wh) / 2);
		posCode = (nn4 || nn6) ? "screenX=" + posX + ",screenY=" + posY : "left=" + posX + ",top=" + posY;
	}else{
		posCode = "";
	}
	
 	var params = 'width='+ww+',height='+wh+',status=no,toolbar=no,menubar=no,scrollbars=yes,'+posCode; 
 	

	var win = open('http://putin-chess.ru/vo.php',id,params); 
}

function show_add_anecdot(w, h) { 

	var day= new Date(); 
	var id = day.getTime(); 
	var ww = parseInt(w);
	var wh = parseInt(h);

	if (nn4 || ie4 || nn6){
		posX = Math.round((screen.width - ww) / 2);
		posY = Math.round((screen.height - wh) / 2);
		posCode = (nn4 || nn6) ? "screenX=" + posX + ",screenY=" + posY : "left=" + posX + ",top=" + posY;
	}else{
		posCode = "";
	}
	
 	var params = 'width='+ww+',height='+wh+',status=no,toolbar=no,menubar=no,scrollbars=no,'+posCode; 
 	
 	var msg='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' +
 	'<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru" id="popup">' +
 	'<head><meta http-equiv="Content-Type" content="text/html; Charset=Windows-1251" /><title>За Путина!</title><link rel="stylesheet" type="text/css" media="all" href="s/style.css" /><!--[if lt IE 8]><link rel="stylesheet" type="text/css" media="all" href="s/ie.css" /><![endif]-->' +
	'</head><body><form action="/_post_loader.php" method="post" enctype="multipart/form-data"><fieldset>' +
	'<input type="hidden" name="script" value="zaputina_save_anecdot" />' +
	'<label for="name">Имя:</label><input type="text" id="name" name="name" />' +
	'</fieldset><fieldset>' +
	'<label for="city">Город:</label><input type="text" id="city" name="city" />' +
	'</fieldset><fieldset>' +
	'<label for="anecdot">Анекдот:</label><textarea id="anecdot" name="anecdot" rows="12" cols="26"></textarea>' +
	'</fieldset><fieldset>' +
	'<input type="submit" value="Отправить" />' +
	'</fieldset></form></body></html>';

	var win = open('',id,params); 
	win.document.write(msg); 
	win.document.close(); 
}

window.onload = function update_votes() {
	new Ajax.PeriodicalUpdater('total-votes', '/votescached.txt', {
  		method: 'get', frequency: 3
	});
}