d = document;

function getXmlHttp(){
	var xmlhttp;
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
	}
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

function sendRequest(func,vars,url,method){
	var url = url || 'default.php';
	var method = method || 'POST';
	eval(func + '(null,vars)'); 
	var xmlhttp = getXmlHttp();
	xmlhttp.open(method, url, true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200) {
				eval(func + '(xmlhttp,vars)');
			}
		}
	};
	var params = '';
	for(var i in vars) params += '&' + i + '=' + encodeURIComponent(vars[i]);
	params = 'target=' + func + params;
	xmlhttp.send(params);
}

function vote(req,vars){
	if(req == null){
		d.getElementById("vm_" + vars.id).innerHTML = '<img src="/i/loader.gif" width="16" height="16">';
	}
	else{
		d.getElementById("vm_" + vars.id).innerHTML = '';
		eval('var res = ' + req.responseText + ';');
		if(res != null){
			d.getElementById("vm_" + vars.id).innerHTML = res.m;
			if(res.c == 0){
				var yes = d.getElementById("yc" + "_" + vars.id);
				var no = d.getElementById("nc" + "_" + vars.id);			
				if(vars.type == 'yes') yes.innerHTML = parseInt(yes.innerHTML) + 1;
				else no.innerHTML = parseInt(no.innerHTML) + 1;
				setBounds(vars.id,yes,no);
			}
		}
	}
}

function inc(obj){
	sendRequest('vote',{id: obj.id,type: 'yes'},'/vote.ajax.php');
}
function dec(obj){
	sendRequest('vote',{id: obj.id,type: 'no'},'/vote.ajax.php');
}

function setBounds(id,yes,no){
	var sum = parseInt(yes.innerHTML) + parseInt(no.innerHTML);
	var pr = (sum / 100);
	d.getElementById("ybg" + "_" + id).style.width = parseInt(yes.innerHTML/pr) + '%';
	d.getElementById("nbg" + "_" + id).style.width = parseInt(no.innerHTML/pr) + '%';
}

function countCheck(obj,count){
			var counter = d.getElementById(obj.id + "_count");
			if((count - obj.value.length) < 0) obj.value = obj.value.substr(0,count);
			counter.value = 'Осталось символов ' + (count - obj.value.length);
		}