var http_request = false;

function makeRequest(page, func) {

	http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
			// See note below about this line
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		return false;
	}
	http_request.onreadystatechange = func;
	http_request.open('GET', page, true);
	http_request.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=windows-1251');
	http_request.send(null);
}

function result_func() {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var resultSCRIPT = typeof BinaryToString != 'undefined' ? BinaryToString(http_request.responseBody+"") : http_request.responseText;
			eval(resultSCRIPT);
		} else alert('Во время исполнения запроса возникла ошибка. #1.'+http_request.status);
	}
}

function opinion_vote(vote,opinion_id) {
	makeRequest('/vedusers/social.php?cvotemode=vote&vote='+vote+'&opinion_id='+opinion_id,result_func);
	return false;
}

function after_opinion_vote(opinion_id,opinion_voices_sum,positive_voices,user_voices) {
	if (user_voices==null) {
		user_voices=positive_voices;
		positive_voices=null;
	}

	var sumhtml = (opinion_voices_sum || positive_voices) ? (opinion_voices_sum > 0 ? '+' : '') + opinion_voices_sum : '';
	var title = positive_voices!=null ? ('+' + positive_voices + ' -' + (positive_voices - opinion_voices_sum) + ' = ') : '';
	title += (opinion_voices_sum > 0 ? '+' : '') + opinion_voices_sum;
	var voicesclass = null;
	if (opinion_voices_sum > 0) voicesclass='up';
	else if (opinion_voices_sum < 0) voicesclass='down';

	var c=document.getElementById('v'+opinion_id);
	if (c) {
		c.innerHTML = sumhtml;
		if (voicesclass) c.className = voicesclass;
		else c.className = null;
		c.title=title;
	}

	c=document.getElementById('bv'+opinion_id);
	if (c) {
		c.innerHTML = sumhtml;
		if (voicesclass) c.className = voicesclass;
		else c.className = null;
		c.title=title;
	}

	c=document.getElementById('voices_left');
	if (c) c.innerHTML=user_voices;

	if (user_voices) {
		var onevotehtml="<a href='#' onclick=\"alert('За один отзыв можно проголосовать только один раз'); return false;\" class=vote_none>&nbsp;</a>";

		var arspan=document.getElementById('ar'+opinion_id);
		if (arspan) arspan.innerHTML=onevotehtml;

		arspan=document.getElementById('bar'+opinion_id);
		if (arspan) arspan.innerHTML=onevotehtml;
	} else no_more_voices();
}

function no_more_voices() {
	var novoicehtml="<a href='#' onclick=\"alert('У Вас не осталось голосов'); return false;\" class=vote_none>&nbsp;</a>";

	var arspans=document.getElementsByTagName('span');
	for(var i=0;i<arspans.length;i++) if (arspans[i].getAttribute( 'name' ) == 'vote_arrows') arspans[i].innerHTML=novoicehtml;
}

// доп. функция для скрытых отзывов
function show_opinion(oid) {
        $('ocontent_'+oid).show();
        $('oshowlink_'+oid).hide();
}
