// url na ktorej ziskame data
var profile_url_request = "request/profile.request.php";

// xmlHTTP objekct
var profile_xml_http = createXmlHttpRequestObject();

// objekty
var profile_update_interval = 5000;
var profile_cache = new Array();

var profile_group_edited = false;
var profile_group_opened = false;
var profile_opened = false;

// inicializacna funcia pre profile ajax
function profile_init()
{
}

function profile_groups()
{
	// osetrenie proti nefunkcnemu pripojeniu
	if(! selftest_status())
	{
		profile_cache = new Array();
		return;
	}

	var params = 'action=group';
	profile_cache.push(params);
	profile_request();
}

function profile_group()
{
	// osetrenie proti nefunkcnemu pripojeniu
	if(! selftest_status())
	{
		profile_cache = new Array();
		return;
	}

	if(profile_group_opened)
	{
		var params = 'action=group&id_group='+profile_group_opened;
		profile_cache.push(params);
		profile_request();
	}
}

function profile_groupadd()
{
	// osetrenie proti nefunkcnemu pripojeniu
	if(! selftest_status())
	{
		profile_cache = new Array();
		return;
	}

	var groupname = document.getElementById('t_profile_groupname');
	if(groupname)
	{
		groupname.setAttribute('disabled','true');
		document.getElementById('t_profile_console').innerHTML = 'overujem...';
		document.getElementById('t_profile_console').className = 'alert2';

		var params = 'action=groupadd&name='+groupname.value;
		profile_cache.push(params);
		profile_request();
	}
}

function profile_groupadd_enter(e)
{
	var e = e ? e : window.event;

	var code = (e.charCode) ? e.charCode :
         ((e.keyCode) ? e.keyCode :
         ((e.which) ? e.which : 0));

	if (e.type == "keydown")
	{
		if(code == 13)
		{
			profile_groupadd();
		}
	}
}

function profile_add()
{
	// osetrenie proti nefunkcnemu pripojeniu
	if(! selftest_status())
	{
		profile_cache = new Array();
		return;
	}

	if(! profile_group_opened)
	{
		report_error('profile.ajax.js: Nie je možné pridávať profily, pretože nie je zvolená žiadna skupina', true);
		return;
	}

	var name = document.getElementById('t_profile_name');
	if(name)
	{
		name.setAttribute('disabled','true');
		document.getElementById('t_profile_console').innerHTML = 'overujem...';
		document.getElementById('t_profile_console').className = 'alert2';

		var params = 'action=add&id_group=' + profile_group_opened + '&name='+name.value;
		profile_cache.push(params);
		profile_request();
	}
}

function profile_add_enter(e)
{
	var e = e ? e : window.event;

	var code = (e.charCode) ? e.charCode :
         ((e.keyCode) ? e.keyCode :
         ((e.which) ? e.which : 0));

	if (e.type == "keydown")
	{
		if(code == 13)
		{
			profile_add();
		}
	}
}

function profile_groupaction(e)
{
	// osetrenie proti nefunkcnemu pripojeniu
	if(! selftest_status())
	{
		profile_cache = new Array();
		return;
	}

	var e = e ? e : window.event;

	var target = event_get_target(e);

	if(target.id.indexOf('groupdel') == 0)
	{
		if(! showCaution('Chcete naozaj vymazať skupinu a všetky profily v nej?\n\nAkcia je nenávratná')) return;
		var id = target.id;
		id = id.replace('groupdel', '');

		params = 'action=groupdel&id='+id;
		if(profile_group_opened) params = params + '&id_group='+profile_group_opened;
		profile_cache.push(params);
		profile_request();
	}
	else if(target.id.indexOf('groupedit') == 0)
	{
		var id = target.id;
		id = id.replace('groupedit', '');

		params = 'action=group&id='+id;
		if(profile_group_opened) params = params + '&id_group='+profile_group_opened;
		profile_cache.push(params);
		profile_request();
	}
	else if(target.id.indexOf('groupvis') == 0)
	{
		var id = target.id;
		id = id.replace('groupvis', '');

		params = 'action=visibility&id='+id;
		if(profile_group_opened) params = params + '&id_group='+profile_group_opened;
		profile_cache.push(params);
		profile_request();
	}
	else if(target.id.indexOf('groupopen') == 0)
	{
		var id = target.id;
		id = id.replace('groupopen', '');

		params = 'action=groupopen&id_group='+id;
		profile_cache.push(params);
		profile_request();
	}
	else if(target.id.indexOf('profileopen') == 0)
	{
		var id = target.id;
		id = id.replace('profileopen', '');

		params = 'action=view&id='+id;
		profile_cache.push(params);
		profile_request();
	}

	return;
}

function profile_groupsetposition(id, id_after)
{
	// osetrenie proti nefunkcnemu pripojeniu
	if(! selftest_status())
	{
		profile_cache = new Array();
		return;
	}

	if(id_after.indexOf('movable') >= 0) id_after = id_after.replace('movable', '');
	else if(id_after != "") return;

	if(id.indexOf('movable') >= 0)
	{
		id = id.replace('movable', '');

		var params = 'action=groupsetposition&id=' + id + '&id_after=' + id_after;
		if(profile_group_opened) params = params + '&id_group='+profile_group_opened;
		profile_cache.push(params);
		profile_request();
	}
}

function profile_groupedit()
{
	// osetrenie proti nefunkcnemu pripojeniu
	if(! selftest_status())
	{
		profile_cache = new Array();
		return;
	}

	if(profile_group_edited)
	{
		var name = document.getElementById('t_profile_groupeditname');
		if(name)
		{
			name = name.value;
			var params = 'action=groupedit&id=' + profile_group_edited + '&name=' + name;
			if(profile_group_opened) params = params + '&id_group='+profile_group_opened;
			profile_cache.push(params);
			profile_request();
		}
	}
}

function profile_groupedit_enter(e)
{
	var e = e ? e : window.event;

	var code = (e.charCode) ? e.charCode :
         ((e.keyCode) ? e.keyCode :
         ((e.which) ? e.which : 0));

	if (e.type == "keydown")
	{
		if(code == 13)
		{
			profile_groupedit();
		}
	}
}

function profile_view(e)
{
	// osetrenie proti nefunkcnemu pripojeniu
	if(! selftest_status())
	{
		profile_cache = new Array();
		return;
	}

	var e = e ? e : window.event;
	var target = event_get_target(e);

	if(target.id.indexOf('profileview') == 0)
	{
		var id = target.id;
		id = id.replace('profileview', '');

		var params = 'action=view&id=' + encodeURIComponent(id);
		profile_cache.push(params);
		profile_request();
	}
}

function profile_settings(e)
{
	// osetrenie proti nefunkcnemu pripojeniu
	if(! selftest_status())
	{
		profile_cache = new Array();
		return;
	}

	var e = e ? e : window.event;
	var target = event_get_target(e);

	if(target.id.indexOf('profilesettings') == 0)
	{
		var id = target.id;
		id = id.replace('profilesettings', '');

		var params = 'action=settings&id=' + encodeURIComponent(id);
		profile_cache.push(params);
		profile_request();
	}
}
function profile_settingsset(e)
{
	// osetrenie proti nefunkcnemu pripojeniu
	if(! selftest_status())
	{
		profile_cache = new Array();
		return;
	}

	var e = e ? e : window.event;
	var target = event_get_target(e);

	var category = document.getElementById('t_profile_category');
	var name     = document.getElementById('t_profile_name');
	var birth    = document.getElementById('t_profile_birth');
	var sport    = document.getElementById('t_profile_sport');
	var text = document.getElementById('t_profile_about');

	if(target.id.indexOf('profilesettingsset') == 0 && category && name && birth && sport && text)
	{
		var id = target.id;
		id = id.replace('profilesettingsset', '');

		var params = 'action=settingsset&id=' + encodeURIComponent(id)
									 + '&category=' + encodeURIComponent(category.value)
									 + '&name=' + encodeURIComponent(name.value)
									 + '&birth=' + encodeURIComponent(birth.value)
									 + '&sport=' + encodeURIComponent(sport.value)
								     + '&text=' + encodeURIComponent(text.value);
		profile_cache.push(params);
		profile_request();
	}
}

function profile_text(e)
{
	// osetrenie proti nefunkcnemu pripojeniu
	if(! selftest_status())
	{
		profile_cache = new Array();
		return;
	}

	var e = e ? e : window.event;
	var target = event_get_target(e);
	if(target.id.indexOf('profiletext') == 0)
	{
		var id = target.id;
		id = id.replace('profiletext', '');

		var params = 'action=text&id=' + id;
		profile_cache.push(params);
		profile_request();
	}
}
function profile_textset(e)
{
	// osetrenie proti nefunkcnemu pripojeniu
	if(! selftest_status())
	{
		profile_cache = new Array();
		return;
	}

	var e = e ? e : window.event;
	var target = event_get_target(e);

	var text = document.getElementById('t_profile_about2');
	if(target.id.indexOf('profiletextset') == 0 && text)
	{
		var id = target.id;
		id = id.replace('profiletextset', '');

		var params = 'action=textset&id=' + encodeURIComponent(id) + '&text=' + encodeURIComponent(text.value);
		profile_cache.push(params);
		profile_request();
	}
}

function profile_photo(e)
{
	// osetrenie proti nefunkcnemu pripojeniu
	if(! selftest_status())
	{
		profile_cache = new Array();
		return;
	}

	var e = e ? e : window.event;
	var target = event_get_target(e);
	if(target.id.indexOf('profilephoto') == 0)
	{
		var id = target.id;
		id = id.replace('profilephoto', '');

		var params = 'action=photo&id=' + id;
		profile_cache.push(params);
		profile_request();
	}
}

function profile_request()
{
	// osetrenie proti nefunkcnemu pripojeniu
	if(! selftest_status())
	{
		profile_cache = new Array();
		return;
	}

	loading_init();

	if(profile_xml_http)
	{
		try
		{
			if(profile_xml_http.readyState == 4 || profile_xml_http.readyState == 0)
			{
				var params = "";

				if(profile_cache.length > 0) params = profile_cache.shift();
				else return;

				profile_xml_http.open("POST", profile_url_request, true);
				profile_xml_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				profile_xml_http.onreadystatechange = profile_handle_response;

				profile_xml_http.send(params);
			}
			else
			{
				setTimeout("profile_request();", profile_update_interval);
			}
		}
		catch(e)
		{
			report_errror(e.toString(), true);
		}
	}
}



function profile_handle_response(){
	if(profile_xml_http.readyState == 4){
		if(profile_xml_http.status == 200){
			try{
				profile_read_response();
			}
			catch(e)
			{
				if(e.length == 0)
					selftest_start(profile_url_request);
				else{
					report_error(e.toString(), true);
				}
			}
		}
		else{
			selftest_start(profile_url_request);
		}
	}
}

function profile_read_response()
{
	// precitanie odpovede a zistenie ci obsahuje chybove hlasky
	var response = profile_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(profile_url_request);
		else
			throw(response);

		return;
	}

	response = profile_xml_http.responseXML.documentElement;

	// precitanie chyby
	var error_messages = response.getElementsByTagName('error');
	if(error_messages && error_messages.length)
	{
		report_error(error_messages.item(0).firstChild.data, true);
		return;
	}

	var load_template = response.getElementsByTagName('load_template');
	for(var i = 0; i < load_template.length; i++)
	{
		var template = load_template.item(i).firstChild ? load_template.item(i).firstChild.data : false;
		if(template)
		{
			template_load(template);
		}
	}


	// 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);
		}
	}
}

function chars_remaining(id, max_count, id_where)
{
	var elem = document.getElementById(id);
	var elem_where = document.getElementById(id_where);
	if(elem && elem_where)
	{
		var remains = max_count - elem.value.length;
		var text = "";
		var chars = "";
		if(remains >= 0)
		{
			if(remains > 4 || remains == 0) chars = " znakov";
			else if(remains <= 4 && remains > 1) chars = " znaky";
			else chars = " znak";
			elem_where.className = 'form_hint';
			text = "ostáva " + remains + chars;
		}
		else
		{
			remains = -remains;
			if(remains > 4 || remains == 0) chars = " znakov";
			else if(remains <= 4 && remains > 1) chars = " znaky";
			else chars = " znak";
			elem_where.className = 'alert2';
			text = "presiahli ste max dĺžku o " + remains + chars;
		}

		elem_where.innerHTML = text;
	}
}