var do_throws_count = 0;

function throw_dices( )
{

	var throwtext = '';
	var throwcode = '';

	if( do_throws_count > 9 )
	{
		alert( 'Nie mozna wykonac wiecej niz 9 rzutow.' );
		return false;
	}
	if( parseInt( document.dices.dices_count.value ) < 1 || parseInt( document.dices.dices_size.value ) < 1 || isNaN( parseInt( document.dices.dices_count.value ) ) || isNaN( parseInt( document.dices.dices_size.value ) ) )
	{
		alert( 'Wykryto niedozwolone wartosci.' );
		return false;
	}

	throwtext = throwtext + 'Rzut ' + parseInt( document.dices.dices_count.value ) + ( ( parseInt( document.dices.dices_count.value ) != 1 ) ? ' kosciami ' : ' koscia ' );
	throwtext = throwtext + parseInt( document.dices.dices_size.value ) + ( ( parseInt( document.dices.dices_count.value ) != 1 ) ? '-sciennymi' : '-scienna' ) ;

	throwtext = throwtext + ( ( document.dices.modifier.value != 0 && parseInt( document.dices.modifier_value.value ) > 0 ) ? ' (modyfikator: ' + document.dices.modifier.value + parseInt( document.dices.modifier_value.value ) + ')' : '' );
	document.dices.throws.value = document.dices.throws.value + ( ( document.dices.throws.value != '' ) ? '\n' : '' ) + throwtext;

	throwcode = throwcode + '(' + parseInt( document.dices.dices_count.value ) + 'd' + parseInt( document.dices.dices_size.value ) + ')';
	throwcode = throwcode + ( ( document.dices.modifier.value != 0 && parseInt( document.dices.modifier_value.value ) > 0 ) ? document.dices.modifier.value + parseInt( document.dices.modifier_value.value ) : '' );

	document.dices.dices_throws.value = document.dices.dices_throws.value + ( ( document.dices.dices_throws.value != '' ) ?  ',' : '' ) + throwcode;

	document.dices.dices_count.value = '';
	document.dices.dices_size.value = '';
	document.dices.modifier.value = '';
	document.dices.modifier_value.value = '';

	do_throws_count++;

}



RPGrequest = false;
try
{
	RPGrequest = new XMLHttpRequest();
}
catch (e)
{
	try
	{
		RPGrequest = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try
		{
			RPGrequest = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e)
		{
			RPGrequest = false;
		}
	}
}

function setpopuppos( )
{
	var windowWidth = document.body.clientWidth;
	var popupWidth = document.getElementById( 'rpgpopup' ).clientWidth;
	document.getElementById( 'rpgpopup' ).style.left = parseInt( ( windowWidth - popupWidth ) / 2 ) + 'px';
	document.getElementById( 'rpgpopup' ).style.top = '120px';
}

onresize = setpopuppos;

function rpgpopup( vars )
{
	if( !document.getElementById( 'rpgpopup' ) )
	{
		return false;
	}
	document.getElementById('rpgpopup').style.display = 'block';
	document.getElementById('rpgpopup').innerHTML = 'Connecting...';
	RPGrequest.open("GET", 'rpgengine.php?do=generate&' + vars + '&time=' + new Date( ), true);
	RPGrequest.onreadystatechange = generateBody;
	RPGrequest.send(null);
	setpopuppos( );
}

function generateBody( )
{
	if ( RPGrequest.readyState == 1 )
	{
		document.getElementById('rpgpopup').innerHTML = '<div style="font-weight:bold;font-size:10px;font-family:verdana;color:#454545;background-color:white;border:1px solid;padding:25px;text-align:center"><img src="rpgimages/progress.gif" alt="" border="0" align="absbottom"/><br /><br />Loading object...</div>';
	}
	if ( RPGrequest.readyState == 4 )
	{
		var xmlDoc = RPGrequest.responseText;
		if ( xmlDoc.length > 0 )
		{
			document.getElementById('rpgpopup').innerHTML = xmlDoc;
		}
		else
		{
			document.getElementById('rpgpopup').style.display = 'none';
		}
	}
}
