var hlasovano = 0;
var ajaxAnkety = new AJAX();

function getHlas( nazevForm ) {
	if( hlasovano )
	{
		var f;
		if( f = getObj( nazevForm ) ) {
			var m;
			for( var i = 1; ( m = getObj( 'an' + i )) != null; i ++ )
				m.disabled = true;
			if( f.fACT != null ) {
				f.fACT.disabled = true;
				f.fACT.style.display = 'none';
			}
		}
	}
}

ajaxAnkety.onLoad = function( ) {
	var l, mess, blok, blok_in;

	blok_in = this.getXML();

	if( typeof( blok_in ) == 'undefined' ) return ;
	if( typeof( blok_in.firstChild ) == 'undefined' ) return ;
	if( blok_in.firstChild.nodeName != 'ankety' ) return ;

	blok_in = blok_in.firstChild.firstChild;
	if( typeof( blok_in ) == 'undefined' ) return ;
	if( typeof( blok_in.data ) == 'undefined' ) return ;

	blok_in = blok_in.data;

	l = blok_in.indexOf( '|' );
	mess = blok_in.substr( 0, l );

	if( mess != 'O.K.') {
		alert( mess );
	} else {
		l = blok_in.indexOf( '<div id="hpanketa"', l );
		var odx = blok_in.indexOf( '>', l ) + 1;
		var dox = blok_in.lastIndexOf( '<' );
		if( blok = getObj( 'hpanketa' ) ) blok.innerHTML = blok_in.substring( odx, dox );
	}
	getHlas( 'hpank' );
}

function hlasuj( idAnkety, nazevForm )
{
	var f = getObj( nazevForm );
	var odp;
	if( hlasovano )
	{
		alert( 'Nelze hlasovat znovu!' );
		return;
	}
	var check = false;
	for( var i = 0; i< f.length; i++ ) {
		if( f[i].type == 'radio' && f[i].checked ) {
			check = true;
			odp = f[i].value;
		}
	}
	if( ! check ) 
	{
		alert( "Zadejte Vaši odpověď" );
		return;
	}

	var params = 'idAnkety=' + idAnkety;
	params+= '&odpoved=' + odp;

	ajaxAnkety.post( '/admin/ankety/hlasovani.php', params );
	hlasovano = 1;
	getHlas( nazevForm );
}

function pridejMoznostAnkety( ) {
	var table = getObj( "moznosti" );
	if( table == null ) return ;
	
	var radek = table.insertRow( table.rows.length - 1 );
	
	anketa_moznosti ++;
	anketa_max ++;

	radek.id = 'moznost_' + anketa_max;
	radek.insertCell( 0 ).innerHTML = '<input type="text" id="mnadpis_' + anketa_max + '" name="mnadpis[]" class="validate[required]" width="200px" />';
	radek.insertCell( 1 ).innerHTML = '<input type="text" id="mpocet_' + anketa_max + '" name="mpocet[]" value="0" class="validate[optional,custom[onlyNumber]]" width="200px" onkeypress="return numeric( event );" />';
	radek.insertCell( 2 ).innerHTML = '<input type="button" id="msmaz_' + anketa_max + '" onclick="smazMoznostAnkety( ' + anketa_max + ' );" value="Odebrat" />';
}

function smazMoznostAnkety( num ) {
	var radek = getObj( "moznost_" + num );
	if( radek == null ) return ;

	radek.parentNode.removeChild( radek );
}

