// url na ktorej ziskame data
//var survey_url_request = "http://shugo/ofkajaxhradok.sk/request/survey.request.php";

// xmlHTTP objekct
var survey_xml_http = createXmlHttpRequestObject();

// objekty
var survey_update_interval = 5000;
var survey_cache = new Array();

var survey_group_edited = false;
var survey_group_opened = false;
var survey_opened = false;

// inicializacna funcia pre survey ajax
function survey_init()
{
}

function survey_vote(id_survey, id_option, id_user)
{
	/*if(! selftest_status())
	{
		survey_cache = new Array();
		return;
	}*/
	
	if(id_survey != "" && id_option != "")
	{
		var params = 'action=vote&id_option='+htmlspecialchars(id_option)+'&id_user='+htmlspecialchars(id_user)+'&id_survey='+htmlspecialchars(id_survey);
		survey_cache.push(params);
		survey_request();
	}
	else
	{
		report_error('Fatal error: Nebol zadaný identifikátor ankety, možnosti v ankete alebo identifikátor hlasujúceho používateľa.' ,false);
	}
}

function survey_request()
{
	// osetrenie proti nefunkcnemu pripojeniu
	/*if(! selftest_status())
	{
		survey_cache = new Array();
		return;
	}*/

	//loading_init();

	if(survey_xml_http)
	{
		try
		{
			if(survey_xml_http.readyState == 4 || survey_xml_http.readyState == 0)
			{
				var params = "";

				if(survey_cache.length > 0) params = survey_cache.shift();
				else return;

				survey_xml_http.open("POST", survey_url_request, true);
				survey_xml_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				survey_xml_http.onreadystatechange = survey_handle_response;

				survey_xml_http.send(params);


			}
			else
			{
				setTimeout("survey_request();", survey_update_interval);
			}
		}
		catch(e)
		{
			report_errror(e.toString(), true);
		}
	}
}



function survey_handle_response(){
	if(survey_xml_http.readyState == 4){
		if(survey_xml_http.status == 200){
			try{
				survey_read_response();
			}
			catch(e)
			{
				if(e.length == 0)
					;//selftest_start(survey_url_request);
				else{
					report_error(e.toString(), true);
				}
			}
		}
		else{
			;//selftest_start(survey_url_request);
		}
	}
}

function survey_read_response()
{
	// precitanie odpovede a zistenie ci obsahuje chybove hlasky
	var response = survey_xml_http.responseText;
	if( response.indexOf("shgmysqlerror") >= 0)
	{
		throw(response);
	}
	if( response.indexOf("ERRNO") >= 0 || response.indexOf("error:") >= 0 || response.length == 0)
	{
		if(response.length == 0)
			;//selftest_start(survey_url_request);
		else
			throw(response);

		return;
	}

	response = survey_xml_http.responseXML.documentElement;

	// precitanie chyby
	var error_messages = response.getElementsByTagName('error');
	if(error_messages && error_messages.length)
	{
		alert(error_messages.item(0).firstChild.data);
		//report_error(error_messages.item(0).firstChild.data, true);
		return;
	}
	
	// nacitanie aktualnych vysledkov hlasovania
	var options = response.getElementsByTagName('option');
	for(var i = 0; i < options.length; i++)
	{
		var id 		= options.item(i).getElementsByTagName('id');
		var votes	= options.item(i).getElementsByTagName('votes');
		var percent	= options.item(i).getElementsByTagName('percent');
		
		if(id.length && votes.length && percent.length)
		{
			id		= id.item(0).firstChild ? id.item(0).firstChild.data : false;
			votes	= votes.item(0).firstChild ? votes.item(0).firstChild.data : false;
			percent	= percent.item(0).firstChild ? percent.item(0).firstChild.data : false;
			
			if(id && votes && percent)
			{
				survey_set_votes('survey_o'+id, 10, percent, votes);
			}
		}
	}
	
	// nacitanie informacii i pouzivanej sablone
	/*var template = response.getElementsByTagName('use_template');
	var location = response.getElementsByTagName('location');
	var data     = response.getElementsByTagName('data');

	if(template.length && location.length && data.length)
	{
		location = location.item(0).firstChild ? location.item(0).firstChild.data : false;
		template = template.item(0).firstChild ? template.item(0).firstChild.data : false;
		data = data.item(0);

		if(location && template && data)
		{
			template_todo_add(location, template, data);
		}
	}*/
}

