var httpRequest, httpRTimer;
function poll_result(this_form){
	//getting params
	var ind = this_form.id;
	var numb = 0;

	var param = '';
	while (t_ob('poll_'+ind+'_'+numb)){
		if (t_ob('poll_'+ind+'_'+numb).checked == true)
			param += '&poll[]=' + t_ob('poll_'+ind+'_'+numb).value;
		numb++;
	}
	
	if (param){
		param = 'poll_id=' + ind + param;
		
		//checking if connect is possible
		if (window.XMLHttpRequest){
			httpRequest = new XMLHttpRequest();
			if (httpRequest.overrideMimeType)
				httpRequest.overrideMimeType('text/html');
		}else if (window.ActiveXObject){
			try{
				httpRequest = new ActiveXObject('Msxml2.XMLHTTP');
			}catch(e){
				try{
					httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
				}catch(e){}
			}
		}
		
		//sending POST results to poll_ajax.php
		if (httpRequest){
			httpRequest.open("POST", "/poll/poll_ajax.php", true);
			httpRequest.onreadystatechange = get_responce;
			httpRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
			httpRequest.setRequestHeader("Content-length", param.length);
			httpRequest.setRequestHeader("Connection", "close");
			httpRequest.send(param);

			//disabling submit button
			for (var i=0;i<this_form.childNodes.length;i++){
				if (this_form.childNodes[i].tagName && this_form.childNodes[i].tagName.toUpperCase() == 'INPUT'){
					if (this_form.childNodes[i].type.toUpperCase() == 'SUBMIT'){
						this_form.childNodes[i].value = 'Подождите...';
						this_form.childNodes[i].disabled = true;
					}
				}
			}

			httpRTimer = window.setTimeout('httpRequest.abort();', 10000); //timer
		}else{
			this_form.submit();
		}
	}
}

function poll_results(text, hits, maxv, sumv){
	if (!maxv) maxv = 1;
	if (!sumv) sumv = 1;
	var precent = Math.round(hits/sumv*1000)/10; //precent
	var width = Math.round(100-hits/maxv*85); //width
	return '<div class=perc><div>'+precent+'% ('+hits+')</div>'+text+'</div><div class=dark><div class=light style="width:'+width+'%;"></div></div>';
}

function get_responce(){
	if (httpRequest.readyState == 4){
		clearTimeout(httpRTimer); //timer
		if (httpRequest.status == 200){
			var resp_text = httpRequest.responseText;
			if (resp_text && resp_text.indexOf(':') != -1){

				resp_text = resp_text.split(':');

				var maxval = 0;
				var sumval = 0;
				
				for (var i=0;i<resp_text.length-1;i++){
					resp_text[i] = parseInt(resp_text[i]);
					if (resp_text[i] > maxval) maxval = resp_text[i];
					sumval = sumval + resp_text[i];
				}
				
				var now_poll = resp_text[resp_text.length-1];
				var cur_title = eval('poll_title_'+now_poll);
				var cur_texts = eval('poll_texts_'+now_poll);
				var div_text = '<h4>'+cur_title+'</h4>';
				for (var i=0;i<cur_texts.length;i++){
					div_text += poll_results(cur_texts[i], resp_text[i], maxval, sumval);
				}
				div_text += '<p>Спасибо за участие!</p>';
				t_ob('poll_'+now_poll).innerHTML = div_text;
			}
		}
	}
}
