function request(url, oncomplete, param1, param2, postBody)
{
	var xmlHttp;
	try
	{
	// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				alert("Vaš preglednik ne podržava AJAX!");
				return false;
			}
		}
    }
    
	xmlHttp.onreadystatechange = function()
    {
		if(xmlHttp.readyState==4)
			oncomplete(xmlHttp.responseText, param1, param2);
	}

	if(postBody == 'undefined')
	{
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
	else
	{
		//xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlHttp.open("POST", url, true);
		xmlHttp.send(postBody);	
	}
}

function isSet(varname)
{
	if(typeof(window[varname]) != "undefined")
		return true;
	else
		return false;
}

function isNumeric(value)
{
	return typeof value != "boolean" && value !== null && !isNaN(+ value);
}

function getEl(id)
{
	return document.getElementById(id);
}

function setPercentBox(id, yes, no)
{
	font_yes = 10;
	font_no = 10;
	if(yes > 50)
		font_yes = 14;
	if(no > 50)
		font_no = 14;
	getEl(id).innerHTML =	"<span style='font-size: " + font_yes + "px;' class='true'>" + yes + "%</span>" +
 							" | " +
							"<span style='font-size: " + font_no + "px;' class='false'>" + no + "%</span>";
}

function VoteHandler(_url, _msg_id, _html0, _html1, _html2, _logged, _msg_not_logged)
{	
	this.url = _url;
	this.id_prefix = 'votebox_';
	this.msg_id = _msg_id;
	this.html0 = _html0;
	this.html1 = _html1;
	this.html2 = _html2;
	this.logged = _logged;
	this.msg_not_logged = _msg_not_logged;

	this.sendVote = function(id, answer)
	{
		if(!this.logged)
		{
			alert(this.msg_not_logged);
			return false;
		}
		// promijeni izgled boxa
		old_html = getEl('votebox_'+id).innerHTML;
		if(answer == '1')
			getEl('votebox_'+id).innerHTML = this.html1;
		else if(answer == '2')
			getEl('votebox_'+id).innerHTML = this.html2;

		request_url = this.url + id + '' + '?answer='+answer;
		request(request_url, this.handleVote, id, old_html);
	}

	this.handleVote = function(response, id, old_html)
	{
		response = response.split('|');

		if(isNumeric(response[0]) && isNumeric(response[1]))
		{
			getEl(this.msg_id).innerHTML = '<span style="font-weight:bold; color: #008800">' + response[2] + '</span>';
			setPercentBox('votebox_' + id + '_percent', response[0], response[1]);
		}
		else
		{
			alert(response[0]);
			getEl('votebox_'+id).innerHTML = old_html;
			//document.getElementById(this.msg_id).innerHTML = '<span style="font-weight:bold; color: #880000">' + response[0] + '</span>';
			//document.getElementById('votebox_'+id).innerHTML = this.html0;
		}
	}
}

function showHide(id)
{
	if(getEl(id).style.display=="none")
		getEl(id).style.display="block";
	else
		getEl(id).style.display="none";
}

function handle_send_res(response, id)
{
	getEl(send_msg_id+'_'+id).innerHTML = response;
	alert(response);
}

function send(id, url)
{
	getEl('send_'+id).style.display="none";
	to = getEl('send_to_'+id).value;
	subject = getEl('send_subject_'+id).value;
	body = getEl('send_body_'+id).value;
	
	postValues = 'data[to]='+to+'&data[subject]='+subject+'&data[body]='+body;
	
	idval = "send_msg_"+id;
	
	new	ajax(url,
		{
			method: 'post',
			postBody: postValues,
			onComplete:function(response)
			{
				status = response.responseText[0];
				msg = response.responseText.substring(1);
				if(status == '0')
					getEl(idval).innerHTML = '<span class="error">'+msg+'</span>';
				else
					getEl(idval).innerHTML = '<span class="success">'+msg+'</span>';
			}
		}
	);
	
	//request(url, handle_send_res, 0, 0, postBody);
}