var min = 60; //liczba minut w godzinie
var day = 22; //liczba roboczodni w miesiacu
var year = 11.5; //liczba dni w roku (a może liczba tygodni?)
var rows = 3; //liczba wierszy w pierwszej tabeli
var id = 3;		//id największego elementu
var max2 = 10; //liczba wierszy w 2. tabeli
var hours_week = day * 8; //liczba godzin w tygodniu


/**
 * Coś się zmieniło, należy przeliczyć dane
 */
function go_change(table_id)
{
	count_employers(table_id);
	count_hours_day(table_id);
	count_hours_month(table_id);
	count_hours_year(table_id);
	summary(table_id);
	get_values(table_id);
	main_summary();
}

/**
 * Zmiana nagłówka tabeli zależnej
 */
function change_title (table_id)
{
	//zmiana tytułu tabeli zależnej
	document.getElementById('title_'+table_id).innerHTML = document.getElementById(table_id+'_1').value;
}

/**
 * Policzenie liczby pracowników dokonujących nadużyć
 */
function count_employers(table_id)
{
	var employers = 0;	//pracownicy ogółem
	var percent = 0;	//procent dokonujących nadużyć
	var i = max2;
	var j = 1;
	employers = myGetFloat(document.getElementById(table_id+"_2").value);

	//petelka po naduzyciach
	for ( ; j <= i; j+=1)
	{
		percent = myGetFloat(document.getElementById(table_id+"_"+j+"_3").value);
		var x = round2((employers * percent / 100),4);
		if (isNaN(x))
			x = 0;
		document.getElementById(table_id+"_"+j+"_4").innerHTML = format(formatValue(x));
	}
}

function count_hours_day(table_id)
{
	var employers = 0;	//pracownicy
	var time = 0;	//czas nadużyć w minutach
	var i = max2;
	var j = 1;
	
	//petelka po wierszach
	for ( ; j <= i; j+=1)
	{
		time = myGetFloat(document.getElementById(table_id+"_"+j+"_2").value);
		people = myGetFloat(document.getElementById(table_id+"_"+j+"_4").innerHTML);
		document.getElementById(table_id+"_"+j+"_5").innerHTML = format(formatValue(round2((people * time) / min,4)));
	}
}

function count_hours_month(table_id)
{
	var i = max2;
	var j = 1;
	
	//petelka po wierszach
	for ( ; j <= i; j+=1)
		document.getElementById(table_id+"_"+j+"_6").innerHTML = format(formatValue(myGetFloat(document.getElementById(table_id+"_"+j+"_5").innerHTML) * day,4));
}

function count_hours_year(table_id)
{
	var i = max2;
	var j = 1;
	
	//petelka po wierszach
	for ( ; j <= i; j+=1)
		document.getElementById(table_id+"_"+j+"_7").innerHTML = format(formatValue(myGetFloat(document.getElementById(table_id+"_"+j+"_6").innerHTML) * year,4));
}

function summary(table_id)
{
	var sum_day = 0;
	var sum_month = 0;
	var sum_year = 0;
	var i = max2;
	var j = 1;
	//petelka po wierszach
	for ( ; j <= i; j+=1)
	{
		sum_day += myGetFloat(document.getElementById(table_id+"_"+j+"_5").innerHTML);
		sum_month += myGetFloat(document.getElementById(table_id+"_"+j+"_6").innerHTML);
		sum_year += myGetFloat(document.getElementById(table_id+"_"+j+"_7").innerHTML);
	}
	
	document.getElementById(table_id+"_sum_day").innerHTML = format(formatValue(sum_day));
	document.getElementById(table_id+"_sum_month").innerHTML = format(formatValue(sum_month));
	document.getElementById(table_id+"_sum_year").innerHTML = format(formatValue(sum_year));
}

/**
 * Przepisanie wartości z zależnej tabeli do tabeli głównej
 */
function get_values(table_id)
{
	//godziny
	document.getElementById(table_id+"_4").innerHTML = format(formatValue(document.getElementById(table_id+"_sum_day").innerHTML));
	//wartość wynagrodzenia
	var srednie = myGetFloat(document.getElementById(table_id+"_3").value);
	var hours = myGetFloat(document.getElementById(table_id+"_4").innerHTML);
	var x = round2((srednie * hours / hours_week),2);
	if (isNaN(x))
		x = 0;
	document.getElementById(table_id+"_5").innerHTML = format(formatValue(x));
	
	//ilosc godzin w miesiacu
	document.getElementById(table_id+"_6").innerHTML = format(formatValue(document.getElementById(table_id+"_sum_month").innerHTML));
	//wartość wynagrodzenia miesiąc
	hours = myGetFloat(document.getElementById(table_id+"_6").innerHTML);
	x = round2((srednie * hours / hours_week),2);
	if (isNaN(x))
		x = 0;
	document.getElementById(table_id+"_7").innerHTML = format(formatValue(x));
	//ilosc godzin w roku
	document.getElementById(table_id+"_8").innerHTML = format(formatValue(document.getElementById(table_id+"_sum_year").innerHTML));
	//wartość wynagrodzenia rok
	hours = myGetFloat(document.getElementById(table_id+"_8").innerHTML);
	x = round2((srednie * hours / hours_week),2);
	if (isNaN(x))
		x = 0;
	document.getElementById(table_id+"_9").innerHTML = format(formatValue(x));
}

function main_summary()
{
	var sum1 = 0;
	var sum2 = 0;
	var sum3 = 0;
	var sum4 = 0;
	var sum5 = 0;
	var sum6 = 0;
	var i = id;
	var j = 1;

	//petelka po wierszach
	for ( ; j <= i; j+=1)
	{
		//czy dana tabela wchodzi do sumy?
		if (isset(document.getElementById(j+"_4")))
		{
			sum1 += myGetFloat(document.getElementById(j+"_4").innerHTML);
			sum2 += myGetFloat(document.getElementById(j+"_5").innerHTML);
			sum3 += myGetFloat(document.getElementById(j+"_6").innerHTML);
			sum4 += myGetFloat(document.getElementById(j+"_7").innerHTML);
			sum5 += myGetFloat(document.getElementById(j+"_8").innerHTML);
			sum6 += myGetFloat(document.getElementById(j+"_9").innerHTML);
		}
	}

	document.getElementById("day").innerHTML = format(formatValue(round2(sum1,2)));
	document.getElementById("day_payment").innerHTML = format(formatValue(round2(sum2,2)));
	document.getElementById("month").innerHTML = format(formatValue(round2(sum3,2)));
	document.getElementById("month_payment").innerHTML = format(formatValue(round2(sum4,2)));
	document.getElementById("year").innerHTML = format(formatValue(round2(sum5,2)));
	document.getElementById("year_payment").innerHTML = format(formatValue(round2(sum6,2)));
}

function show(table_id)
{
	//zgaszenie wszystkich innych
	var i = 1;
	for (;i <= id; i++)
	{
		if (document.getElementById("table_"+i))
			document.getElementById("table_"+i).style.display = "none";
			
		if (document.getElementById("img_"+i))
			document.getElementById("img_"+i).src = "img/dalej.gif";
			
		if (document.getElementById("oblicz_"+i))
			document.getElementById("oblicz_"+i).style.background = "white";
	}
	if (document.getElementById("table_"+table_id).innerHTML != "")
		document.getElementById("table_"+table_id).style.display = "block";
		
	if (document.getElementById("img_"+table_id))
			document.getElementById("img_"+table_id).src = "img/dalej2.gif";
	if (document.getElementById("oblicz_"+table_id))
			document.getElementById("oblicz_"+table_id).style.background = "#ec7e04";
}




function round2(number,precision) 
{
	number = number * potega(10,precision);
	var res = Math.round(number) / potega(10,precision);
	return res;
}

function potega (liczba, do_potegi)
{

	if (do_potegi == 0)
		return 1;

	var temp = liczba;
	for (i = 0; i < do_potegi - 1; i++)
	{
		liczba = liczba * temp;
	}
	return liczba;
}

function add_row()
{
	//pobranie tabeli
	var table = document.getElementById("content_1");
	
	rows = rows + 1;
	id = id + 1;
	
	//wstawienie nowego wiersza
	var row = table.insertRow(rows+2);
	row.id = "row_"+id;
	
	//zerowa komórka
	var cell = row.insertCell(0);
	cell.className = "white";
    var text = '<a href="javascript: del_row('+id+');" ><img src="img/minus.gif" alt="Usuń wiersz" onmouseover="return overlib(\'Usuń wiersz.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" ></a>';
	cell.innerHTML = text;
	
	//pierwsza komórka
	var cell = row.insertCell(1);
	cell.className = "white";
    var text = '<input type="text" value="Dział '+id+'" onmouseover="return overlib(\'Wprowadź nazwę działu.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" id="'+id+'_1" name="'+id+'_1" onchange="javascript: change_title('+id+');">';
	cell.innerHTML = text;

	//druga komórka
	var cell2 = row.insertCell(2);
	cell2.className = "white";
	cell2.innerHTML = '<input onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" style="text-align: center;" type="text" id="'+id+'_2" name="'+id+'_2" onkeypress="return onlydec(event);" onchange="javascript: go_change('+id+');" maxlength="3">';

	//trzecia komórka
	var cell3 = row.insertCell(3);
	cell3.className = "white";
	cell3.innerHTML = '<input onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" style="text-align: center;" type="text" id="'+id+'_3" name="'+id+'_3" style="width: 80px;" onkeypress="return onlydec(event);" onchange="javascript: go_change('+id+');" maxlength="5">';
	
	var cell_extra = row.insertCell(4);
	cell_extra.className = "white";
	cell_extra.setAttribute('id','oblicz_'+id);
	cell_extra.innerHTML = '<a href="javascript: show('+id+');" onmouseover="return overlib(\'Doprecyzuj dane w tabeli szczegółowej.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();"><img src="img/dalej.gif" id="img_'+id+'" alt="Doprecyzuj dane w tabeli szczegółowej." ></a>';
	
	//czwarta komórka
	var cell4 = row.insertCell(5);
	cell4.className = "light3";
	cell4.setAttribute('id',id+"_4");
	//cell4.style.setProperty("color","#696969","");
	cell4.style.color = '#696969';
	cell4.innerHTML = '-';
	
	
	//piąta komórka
	var cell5 = row.insertCell(6);
	cell5.className = "light2";
	cell5.setAttribute('id',id+"_5");
	cell5.innerHTML = '-';
	
	//szósta komórka
	var cell6 = row.insertCell(7);
	cell6.className = "light3";
	cell6.setAttribute('id',id+"_6");
	cell6.style.color = '#696969';
	cell6.innerHTML = '-';

	//siódma komórka
	var cell7 = row.insertCell(8);
	cell7.className = "light2";
	cell7.setAttribute('id',id+"_7");
	cell7.innerHTML = '-';
	
	//ósma komórka
	var cell8 = row.insertCell(9);
	cell8.className = "light3";
	cell8.setAttribute('id',id+"_8");
	cell8.style.color = '#696969';
	cell8.innerHTML = '-';

	//dziewiąta komórka
	var cell9 = row.insertCell(10);
	cell9.className = "light2";
	cell9.setAttribute('id',id+"_9");
	cell9.innerHTML = '-';
	
	//dodanie tabeli zależnej
	add_table2();
	show(0);
}

function del_row(i)
{
	//pobranie tabeli
	var table = document.getElementById("content_1");
	
	//usunięcie wiersza
	//pętla po wierszach, jeśli id się zgadza, to delete
	var table_rows = table.rows;
	var l = 0;
	
	for (var max = table_rows.length; l < max; l++)
	{
			if (table_rows[l].id == "row_"+i)
			{
				table.deleteRow(l);
				rows = rows - 1;
				max -= 1;
				//usunięcie tabeli zależnej
				document.getElementById("table_"+i).innerHTML = "";
			}
	}
	
	main_summary();
	show(0);
}

//dodanie nowego wiersza w tabeli 1 i dodanie tabeli zależnej
function add_table2()
{
	var content = document.getElementById("content2").innerHTML;

	content += '<!-- Tabela '+id+' -->';
	content += '<div id="table_'+id+'">';
	content += '<h3 id="title_'+id+'"><a href="javascript: show(0)"><img src="img/up.gif" title="Schowaj" style="border: 0px;" ></a>&nbsp;Dział '+id+'</h3>';
	content += '<p class="produkty" style="width: 720px; padding: 10px; text-align: justify;">W tej tabeli możesz doprecyzować swoje dane wpisując odpowiednie wartości w polach oznaczonych kolorem białym. Tabela wypełniona jest domyślnymi danymi statystycznymi, które możesz nadpisać. Po wprowadzeniu własnych danych kliknij, aby zaktualizować wyliczenia sumaryczne w obu tabelach.</p>';
	content += '<table class="roi" cellpadding="0" cellspacing="0" style="margin-left: 20px;">';
	content += '<tr><td  class="dark" >Rodzaj nadużycia</td><td  class="light" >przeciętny czas nadużycia (min.)</td><td  class="dark"  >odsetek pracowników dokonujących nadużyć (%) dla wybranej jednostki organizacyjnej</td>																			<td  class="light" >odsetek pracowników dokonujących nadużyć (osoby) dla wybranej jednostki organizacyjnej</td><td  class="dark" >ilość godzin łącznie w ciągu dnia</td><td  class="light"  >ilość godzin łącznie w ciągu m-ca</td><td  class="dark" >ilość godzin łącznie w ciągu roku</td></tr>';
	content += '<tr style="min-height: 10px; height: 15px;"><td class="light2" id="'+id+'_1_1">Spóźnienia</td>																																																<td class="white"><input type="text" value="15" onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" style="text-align: center;" dir="rtl" id="'+id+'_1_2" name="'+id+'_1_2"  onkeypress="return onlydec(event);" onchange="javascript: go_change('+id+');"  maxlength="3" ></td>	<td class="white"><input type="text" onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" value="15" style="text-align: center;" dir="rtl" id="'+id+'_1_3" onkeypress="return onlydec(event);" onchange="javascript: go_change('+id+');"  maxlength="3" ></td> <td class="light2" id="'+id+'_1_4" >-</td><td class="light3" id="'+id+'_1_5">-</td><td class="light2" id="'+id+'_1_6">-</td><td class="light3" id="'+id+'_1_7">-</td></tr>';
	content += '<tr style="min-height: 10px; height: 15px;"><td class="light3" id="'+id+'_2_1">Wcześniejsze wyjścia z pracy</td>																																											<td class="white"><input type="text" value="15" onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" style="text-align: center;" dir="rtl" id="'+id+'_2_2" name="'+id+'_2_2"  onkeypress="return onlydec(event);" onchange="javascript: go_change('+id+');"  maxlength="3" ></td>	<td class="white"><input type="text" onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" value="15" style="text-align: center;" dir="rtl" id="'+id+'_2_3" onkeypress="return onlydec(event);" onchange="javascript: go_change('+id+');"  maxlength="3" ></td> <td class="light2" id="'+id+'_2_4" >-</td><td class="light3" id="'+id+'_2_5">-</td><td class="light2" id="'+id+'_2_6">-</td><td class="light3" id="'+id+'_2_7">-</td></tr>';	
	content += '<tr style="min-height: 10px; height: 15px;"><td class="light2" id="'+id+'_3_1">Przedłużające się przerwy socjalne</td>																																										<td class="white"><input type="text" value="15" onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" style="text-align: center;" dir="rtl" id="'+id+'_3_2"   onkeypress="return onlydec(event);" onchange="javascript: go_change('+id+');"  maxlength="3" ></td>				 	<td class="white"><input type="text" onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" value="25" style="text-align: center;" dir="rtl" id="'+id+'_3_3" onkeypress="return onlydec(event);" onchange="javascript: go_change('+id+');"  maxlength="3" ></td> <td class="light2" id="'+id+'_3_4" >-</td><td class="light3" id="'+id+'_3_5">-</td><td class="light2" id="'+id+'_3_6">-</td><td class="light3" id="'+id+'_3_7">-</td></tr>';
	content += '<tr style="min-height: 10px; height: 15px;"><td class="light3" id="'+id+'_4_1">Nieautoryzowane wyjścia w czasie godzin pracy np. do sklepu</td> 																																			<td class="white"><input type="text" value="30" onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" style="text-align: center;" dir="rtl" id="'+id+'_4_2"   onkeypress="return onlydec(event);" onchange="javascript: go_change('+id+');"  maxlength="3" ></td>					<td class="white"><input type="text" onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" value="5" style="text-align: center;" dir="rtl" id="'+id+'_4_3" onkeypress="return onlydec(event);" onchange="javascript: go_change('+id+');"  maxlength="3" ></td><td class="light2" id="'+id+'_4_4" >-</td><td class="light3" id="'+id+'_4_5">-</td><td class="light2" id="'+id+'_4_6">-</td><td class="light3" id="'+id+'_4_7">-</td></tr>';
	content += '<tr style="min-height: 10px; height: 15px;"><td class="light2" id="'+id+'_5_1">Przerwy na papierosa</td>																																													<td class="white"><input type="text" value="25" onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" style="text-align: center;" dir="rtl" id="'+id+'_5_2"   onkeypress="return onlydec(event);" onchange="javascript: go_change('+id+');"  maxlength="3" ></td>					<td class="white"><input type="text" onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" value="30" style="text-align: center;" dir="rtl" id="'+id+'_5_3" onkeypress="return onlydec(event);" onchange="javascript: go_change('+id+');"  maxlength="3" ></td> <td class="light2" id="'+id+'_5_4" >-</td><td class="light3" id="'+id+'_5_5">-</td><td class="light2" id="'+id+'_5_6">-</td><td class="light3" id="'+id+'_5_7">-</td></tr>';
	content += '<tr style="min-height: 10px; height: 15px;"><td class="white"><input type="text" value="inne" id="'+id+'_6_1" onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();"></td>		<td class="white"><input type="text" value="0"  onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" style="text-align: center;" dir="rtl" id="'+id+'_6_2"   onkeypress="return onlydec(event);" onchange="javascript: go_change('+id+');"  maxlength="3" ></td>					<td class="white"><input type="text" onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" value="0" style="text-align: center;" dir="rtl" id="'+id+'_6_3" onkeypress="return onlydec(event);" onchange="javascript: go_change('+id+');"  maxlength="3" ></td>										    <td class="light2" id="'+id+'_6_4" >-</td><td class="light3" id="'+id+'_6_5">-</td><td class="light2" id="'+id+'_6_6">-</td><td class="light3" id="'+id+'_6_7">-</td></tr>';
	content += '<tr style="min-height: 10px; height: 15px;"><td class="white"><input type="text" value="inne" id="'+id+'_7_1" onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();"></td>		<td class="white"><input type="text" value="0"  onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" style="text-align: center;" dir="rtl" id="'+id+'_7_2"   onkeypress="return onlydec(event);" onchange="javascript: go_change('+id+');"  maxlength="3" ></td>					<td class="white"><input type="text" onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" value="0" style="text-align: center;" dir="rtl" id="'+id+'_7_3" onkeypress="return onlydec(event);" onchange="javascript: go_change('+id+');"  maxlength="3" ></td>											<td class="light2" id="'+id+'_7_4" >-</td><td class="light3" id="'+id+'_7_5">-</td><td class="light2" id="'+id+'_7_6">-</td><td class="light3" id="'+id+'_7_7">-</td></tr>';
	content += '<tr style="min-height: 10px; height: 15px;"><td class="white"><input type="text" value="inne" id="'+id+'_8_1" onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();"></td>		<td class="white"><input type="text" value="0"  onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" style="text-align: center;" dir="rtl" id="'+id+'_8_2"   onkeypress="return onlydec(event);" onchange="javascript: go_change('+id+');"  maxlength="3" ></td>					<td class="white"><input type="text" onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" value="0" style="text-align: center;" dir="rtl" id="'+id+'_8_3" onkeypress="return onlydec(event);" onchange="javascript: go_change('+id+');"  maxlength="3" ></td>											<td class="light2" id="'+id+'_8_4" >-</td><td class="light3" id="'+id+'_8_5">-</td><td class="light2" id="'+id+'_8_6">-</td><td class="light3" id="'+id+'_8_7">-</td></tr>';
	content += '<tr style="min-height: 10px; height: 15px;"><td class="white"><input type="text" value="inne" id="'+id+'_9_1" onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();"></td>		<td class="white"><input type="text" value="0"  onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" style="text-align: center;" dir="rtl" id="'+id+'_9_2"   onkeypress="return onlydec(event);" onchange="javascript: go_change('+id+');"  maxlength="3" ></td>					<td class="white"><input type="text" onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" value="0" style="text-align: center;" dir="rtl" id="'+id+'_9_3" onkeypress="return onlydec(event);" onchange="javascript: go_change('+id+');"  maxlength="3" ></td>											<td class="light2" id="'+id+'_9_4" >-</td><td class="light3" id="'+id+'_9_5">-</td><td class="light2" id="'+id+'_9_6">-</td><td class="light3" id="'+id+'_9_7">-</td></tr>';
	content += '<tr style="min-height: 10px; height: 15px;"><td class="white"><input type="text" value="inne" id="'+id+'_10_1" onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();"></td>		<td class="white"><input type="text" value="0"  onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" style="text-align: center;" dir="rtl" id="'+id+'_10_2"  onkeypress="return onlydec(event);" onchange="javascript: go_change('+id+');"  maxlength="3" ></td>					<td class="white"><input type="text" onmouseover="return overlib(\'Możesz zmienić dane.\', WIDTH, -1, FGCOLOR, \'#FFFFFF\', BGCOLOR, \'#000000\', TEXTCOLOR, \'#000000\');" onmouseout="return nd();" value="0" style="text-align: center;" dir="rtl" id="'+id+'_10_3" onkeypress="return onlydec(event);" onchange="javascript: go_change('+id+');"  maxlength="3" ></td>											<td class="light2" id="'+id+'_10_4" >-</td><td class="light3" id="'+id+'_10_5">-</td><td class="light2" id="'+id+'_10_6">-</td><td class="light3" id="'+id+'_10_7">-</td></tr>';
	content += '<tr><td colspan="4" class="orange_light" style="color: black; font-size: 12px;" >ŁĄCZNA ILOŚĆ GODZIN dla jednostki organizacyjnej</td><td class="orange_dark" style="color: black; font-size: 12px;" id="'+id+'_sum_day">-</td><td class="orange_light" style="color: black; font-size: 12px;" id="'+id+'_sum_month">-</td><td class="orange_dark" style="color: black; font-size: 12px;" id="'+id+'_sum_year">-</td></tr>';
	content += '</table></div>';
	document.getElementById("content2").innerHTML = content;
}

function myGetFloat(tmp)
{
	var tmp1 = tmp.toString();
	var tmp2 = tmp1.replace("&nbsp;","");
	tmp = parseFloat(tmp2);

	if (isNaN(tmp))
		return 0;
	return tmp;
}

function isset () {
    var a=arguments, l=a.length, i=0;
    
    if (l===0) {
        throw new Error('Empty isset'); 
    }
    
    while (i!==l) {
        if (typeof(a[i])=='undefined' || a[i]===null) { 
            return false; 
        } else { 
            i++; 
        }
    }
    return true;
}

function formatValue(val)
{
	val = val.toString();
	val = parseFloat(val.replace("&nbsp;",""));
	
	if (val < 0)
		val = 0;
		
	if (val == 0)
		return "-";
		
	if (isNaN(val))
		return "-";
	return val;
}


function onlydec(event)
{
	if (event.srcElement) 
		kc = event.keyCode;
	else 
		kc = event.which;

	if ((kc <= 47 || kc > 57) && kc != 8 && kc != 0 && kc != 46)
		return false;

	return true;
}

function format(val)
{
	if (val == "-")
		return val;
		
	val = round2(parseFloat(val),2);
	val = val.toString();
	
	var values = val.split(".");

	//wyszukanie modulo 3
	var pos1 = values[0].length%3;
	var max = Math.floor(values[0].length/3);

	var res = "";
	if (pos1 > 0)
	{
		res = values[0].substr(0,pos1);
		if (max > 0)
			res += "&nbsp;";
	}
	
	for( var i = 1; i <= max ; i+=1)
	{
		res += values[0].substr(pos1+(i-1)*3,3);
		if (i < max)
			res += "&nbsp;";
	}
	
	if (!isset(values[1]))
		res+=".00";
	else if (values[1].length == 1)
		res+="."+values[1]+"0";
	else
		res+= "."+values[1];
	
	return res;
}

function my_print(html)
{
	//przygotowanie formularza do wysłania
	if (html == true)
	{
		var win = window.open('index.php?p=roi', 'okno','status=yes, width=1024, height=756, resizable=1, scrollbars=1');
		var form = '<form name="_form_roi" id="_form_roi" action="index.php?p=roi" METHOD="post" style="display: none;">';
		form += '<input type="hidden" name="print" value="true">';
	}
	else
	{
		var form = '<form name="_form_roi" id="_form_roi" action="index.php?p=roi" METHOD="post" style="display: none;">';
		//sprawdzenie, czy jest email
		if (isset(document.getElementById("email")) && document.getElementById("email").value == '')
		{	
			alert("Proszę wypełnić pole Adres e-mail");
			return;
		}
		form += '<input type="hidden" name="send" value="true">';
		form += '<input type="hidden" name="email" value="'+document.getElementById("email").value+'">';
	}
	
	form += '<input type="hidden" name="confirm">';
	
	//załączenie informacji dotyczących pierwszej tabeli
	var i = id;
	var j = 1;
	var k = max2; //liczba wierszy w drugiej tabeli
	var l = 1;

	//petelka po wierszach pierwszej tabeli
	for ( ; j <= i; j+=1)
	{
		//czy dana tabela wchodzi do sumy?
		if (isset(document.getElementById(j+"_1")))
		{
			form += '<input type="hidden" name="'+j+'_1" value="'+document.getElementById(j+"_1").value+'">';
			form += '<input type="hidden" name="'+j+'_2" value="'+document.getElementById(j+"_2").value+'">';
			form += '<input type="hidden" name="'+j+'_3" value="'+document.getElementById(j+"_3").value+'">';
			form += '<input type="hidden" name="'+j+'_4" value="'+document.getElementById(j+"_4").innerHTML+'">';
			form += '<input type="hidden" name="'+j+'_5" value="'+document.getElementById(j+"_5").innerHTML+'">';
			form += '<input type="hidden" name="'+j+'_6" value="'+document.getElementById(j+"_6").innerHTML+'">';
			form += '<input type="hidden" name="'+j+'_7" value="'+document.getElementById(j+"_7").innerHTML+'">';
			form += '<input type="hidden" name="'+j+'_8" value="'+document.getElementById(j+"_8").innerHTML+'">';
			form += '<input type="hidden" name="'+j+'_9" value="'+document.getElementById(j+"_9").innerHTML+'">';
			
			//pobranie danych z tabeli zależnej
			for ( l=1 ; l <= k; l++)
			{ 
				if ( l > 5)
					form += '<input type="hidden" name="'+j+'_'+l+'_1" value="'+document.getElementById(j+'_'+l+'_1').value+'">';
				else
					form += '<input type="hidden" name="'+j+'_'+l+'_1" value="'+document.getElementById(j+'_'+l+'_1').innerHTML+'">';
					
				form += '<input type="hidden" name="'+j+'_'+l+'_2" value="'+document.getElementById(j+'_'+l+'_2').value+'">';
				form += '<input type="hidden" name="'+j+'_'+l+'_3" value="'+document.getElementById(j+'_'+l+'_3').value+'">';
				
				form += '<input type="hidden" name="'+j+'_'+l+'_4" value="'+document.getElementById(j+'_'+l+'_4').innerHTML+'">';
				form += '<input type="hidden" name="'+j+'_'+l+'_5" value="'+document.getElementById(j+'_'+l+'_5').innerHTML+'">';
				form += '<input type="hidden" name="'+j+'_'+l+'_6" value="'+document.getElementById(j+'_'+l+'_6').innerHTML+'">';
				form += '<input type="hidden" name="'+j+'_'+l+'_7" value="'+document.getElementById(j+'_'+l+'_7').innerHTML+'">';
			}
			
			//pobranie podsumowania tabeli zależnej
			form += '<input type="hidden" name="'+j+'_sum_day" value="'+document.getElementById(j+'_sum_day').innerHTML+'">';
			form += '<input type="hidden" name="'+j+'_sum_month" value="'+document.getElementById(j+'_sum_month').innerHTML+'">';
			form += '<input type="hidden" name="'+j+'_sum_year" value="'+document.getElementById(j+'_sum_year').innerHTML+'">';
		}
		
		//pobranie podsumowania tabeli głównej
		form += '<input type="hidden" name="day" value="'+document.getElementById('day').innerHTML+'">';
		form += '<input type="hidden" name="day_payment" value="'+document.getElementById('day_payment').innerHTML+'">';
		form += '<input type="hidden" name="month" value="'+document.getElementById('month').innerHTML+'">';
		form += '<input type="hidden" name="month_payment" value="'+document.getElementById('month_payment').innerHTML+'">';
		form += '<input type="hidden" name="year" value="'+document.getElementById('year').innerHTML+'">';
		form += '<input type="hidden" name="year_payment" value="'+document.getElementById('year_payment').innerHTML+'">';
		
		//wyślanie liczby wierszy w pierwszej tabeli
		form += '<input type="hidden" name="max" value="'+id+'">';
	}

	if (html == true)
	{
		win.alert("Nastąpi automatyczne wywołanie drukowania.");

		//podpięcie tam gdzie trzeba
		win.document.getElementById('my_form').innerHTML = form;

		//wysłanie formularza	
		win.document.forms['_form_roi'].submit();
	} else
	{
		//podpięcie tam gdzie trzeba
		document.getElementById('my_form').innerHTML = form;

		//wysłanie formularza	
		document.forms['_form_roi'].submit();
	}
}


function get_form()
{
	//pobranie danych i przygotowanie formularza
	var form = '<form name="_form_roi" id="_form_roi" action="index.php?p=roi" METHOD="post" style="display: none;" >';

	form += '<input type="hidden" name="send" value="true">';
	form += '<input type="hidden" name="confirm">';
	
	form += '<input type="hidden" name="title" value="'+document.getElementById('title').value+'">';
	form += '<input type="hidden" name="email" value="'+document.getElementById('email').value+'">';
	
	
	//załączenie informacji dotyczących pierwszej tabeli
	var i = id;
	var j = 1;
	var k = max2; //liczba wierszy w drugiej tabeli
	var l = 1;

	//petelka po wierszach pierwszej tabeli
	for ( ; j <= i; j+=1)
	{
		//czy dana tabela wchodzi do sumy?
		if (isset(document.getElementById(j+"_1")))
		{
			form += '<input type="hidden" name="'+j+'_1" value="'+document.getElementById(j+"_1").value+'">';
			form += '<input type="hidden" name="'+j+'_2" value="'+document.getElementById(j+"_2").value+'">';
			form += '<input type="hidden" name="'+j+'_3" value="'+document.getElementById(j+"_3").value+'">';
			form += '<input type="hidden" name="'+j+'_4" value="'+document.getElementById(j+"_4").innerHTML+'">';
			form += '<input type="hidden" name="'+j+'_5" value="'+document.getElementById(j+"_5").innerHTML+'">';
			form += '<input type="hidden" name="'+j+'_6" value="'+document.getElementById(j+"_6").innerHTML+'">';
			form += '<input type="hidden" name="'+j+'_7" value="'+document.getElementById(j+"_7").innerHTML+'">';
			form += '<input type="hidden" name="'+j+'_8" value="'+document.getElementById(j+"_8").innerHTML+'">';
			form += '<input type="hidden" name="'+j+'_9" value="'+document.getElementById(j+"_9").innerHTML+'">';
			
			//pobranie danych z tabeli zależnej
			for ( l=1 ; l <= k; l++)
			{ 
				if ( l > 5)
					form += '<input type="hidden" name="'+j+'_'+l+'_1" value="'+document.getElementById(j+'_'+l+'_1').value+'">';
				else
					form += '<input type="hidden" name="'+j+'_'+l+'_1" value="'+document.getElementById(j+'_'+l+'_1').innerHTML+'">';
					
				form += '<input type="hidden" name="'+j+'_'+l+'_2" value="'+document.getElementById(j+'_'+l+'_2').value+'">';
				form += '<input type="hidden" name="'+j+'_'+l+'_3" value="'+document.getElementById(j+'_'+l+'_3').value+'">';
				
				form += '<input type="hidden" name="'+j+'_'+l+'_4" value="'+document.getElementById(j+'_'+l+'_4').innerHTML+'">';
				form += '<input type="hidden" name="'+j+'_'+l+'_5" value="'+document.getElementById(j+'_'+l+'_5').innerHTML+'">';
				form += '<input type="hidden" name="'+j+'_'+l+'_6" value="'+document.getElementById(j+'_'+l+'_6').innerHTML+'">';
				form += '<input type="hidden" name="'+j+'_'+l+'_7" value="'+document.getElementById(j+'_'+l+'_7').innerHTML+'">';
			}
			
			//pobranie podsumowania tabeli zależnej
			form += '<input type="hidden" name="'+j+'_sum_day" value="'+document.getElementById(j+'_sum_day').innerHTML+'">';
			form += '<input type="hidden" name="'+j+'_sum_month" value="'+document.getElementById(j+'_sum_month').innerHTML+'">';
			form += '<input type="hidden" name="'+j+'_sum_year" value="'+document.getElementById(j+'_sum_year').innerHTML+'">';
		}
	}
	//pobranie podsumowania tabeli głównej
	form += '<input type="hidden" name="day" value="'+document.getElementById('day').innerHTML+'">';
	form += '<input type="hidden" name="day_payment" value="'+document.getElementById('day_payment').innerHTML+'">';
	form += '<input type="hidden" name="month" value="'+document.getElementById('month').innerHTML+'">';
	form += '<input type="hidden" name="month_payment" value="'+document.getElementById('month_payment').innerHTML+'">';
	form += '<input type="hidden" name="year" value="'+document.getElementById('year').innerHTML+'">';
	form += '<input type="hidden" name="year_payment" value="'+document.getElementById('year_payment').innerHTML+'">';
		
	//wyślanie liczby wierszy w pierwszej tabeli
	form += '<input type="hidden" name="max" value="'+id+'">';
	return form;
}

function get_form_data()
{
	//pobranie danych i przygotowanie formularza
	var form = 'send=true&confirm=true&title='+document.getElementById('title').value;
	   form += '&email='+document.getElementById('email').value;
	
	//załączenie informacji dotyczących pierwszej tabeli
	var i = id;
	var j = 1;
	var k = max2; //liczba wierszy w drugiej tabeli
	var l = 1;

	//petelka po wierszach pierwszej tabeli
	for ( ; j <= i; j+=1)
	{
		//czy dana tabela wchodzi do sumy?
		if (isset(document.getElementById(j+"_1")))
		{
			form += '&'+j+'_1='+document.getElementById(j+"_1").value;
			form += '&'+j+'_2='+document.getElementById(j+"_2").value;
			form += '&'+j+'_3='+document.getElementById(j+"_3").value;
			form += '&'+j+'_4='+myGetFloat(document.getElementById(j+"_4").innerHTML);
			form += '&'+j+'_5='+myGetFloat(document.getElementById(j+"_5").innerHTML);
			form += '&'+j+'_6='+myGetFloat(document.getElementById(j+"_6").innerHTML);
			form += '&'+j+'_7='+myGetFloat(document.getElementById(j+"_7").innerHTML);
			form += '&'+j+'_8='+myGetFloat(document.getElementById(j+"_8").innerHTML);
			form += '&'+j+'_9='+myGetFloat(document.getElementById(j+"_9").innerHTML);
			
			//pobranie danych z tabeli zależnej
			for ( l=1 ; l <= k; l++)
			{ 
				if ( l > 5)
					form += '&'+j+'_'+l+'_1='+document.getElementById(j+'_'+l+'_1').value;
				else
					form += '&'+j+'_'+l+'_1='+myGetFloat(document.getElementById(j+'_'+l+'_1').innerHTML);
					
				form += '&'+j+'_'+l+'_2='+document.getElementById(j+'_'+l+'_2').value;
				form += '&'+j+'_'+l+'_3='+document.getElementById(j+'_'+l+'_3').value;
				form += '&'+j+'_'+l+'_4='+myGetFloat(document.getElementById(j+'_'+l+'_4').innerHTML);
				form += '&'+j+'_'+l+'_5='+myGetFloat(document.getElementById(j+'_'+l+'_5').innerHTML);
				form += '&'+j+'_'+l+'_6='+myGetFloat(document.getElementById(j+'_'+l+'_6').innerHTML);
				form += '&'+j+'_'+l+'_7='+myGetFloat(document.getElementById(j+'_'+l+'_7').innerHTML);
			}
			
			//pobranie podsumowania tabeli zależnej
			form += '&'+j+'_sum_day='+myGetFloat(document.getElementById(j+'_sum_day').innerHTML);
			form += '&'+j+'_sum_month='+myGetFloat(document.getElementById(j+'_sum_month').innerHTML);
			form += '&'+j+'_sum_year='+myGetFloat(document.getElementById(j+'_sum_year').innerHTML);
		}
	}
	//pobranie podsumowania tabeli głównej
	form += '&day='+myGetFloat(document.getElementById('day').innerHTML);
	form += '&day_payment='+myGetFloat(document.getElementById('day_payment').innerHTML);
	form += '&month='+myGetFloat(document.getElementById('month').innerHTML);
	form += '&month_payment='+myGetFloat(document.getElementById('month_payment').innerHTML);
	form += '&year='+myGetFloat(document.getElementById('year').innerHTML);
	form += '&year_payment='+myGetFloat(document.getElementById('year_payment').innerHTML);
		
	//wyślanie liczby wierszy w pierwszej tabeli
	form += '&max='+id;
	return form;
}

function send_mail()
{
	//sprawdzenie, czy jest email
	if (isset(document.getElementById("email")) && document.getElementById("email").value == '')
	{	
		alert("Proszę wypełnić pole Adres e-mail.");
		return;
	}

	var form = get_form();
	document.getElementById('my_form').innerHTML = form;

	document.getElementById('_form_roi').submit();
	
	document.getElementById('okno1').style.display = 'none';
}

/************************************************************
 *  			AJAX - WYSYŁANIE E-MAILI					*
 ************************************************************/

//stworzenie obiektu połączenia i ustawienie go na false
var XMLHttpRequestObject = false;

function getData(dataSource)
{
	if (isset(document.getElementById("email")) && document.getElementById("email").value == '')
	{	
		alert("Proszę wypełnić pole Adres e-mail.");
		return;
	}

	if (window.XMLHttpRequest)
	{
		XMLHttpRequestObject = new XMLHttpRequest();
	} else
	{
		XMLHttpRequestObject = new ActiveObject("Microsoft.XMLHTTP");
	}
	
	if(XMLHttpRequestObject)
	{
		var form = get_form_data();

		var obj = document.getElementById('result');
		//otwarcie połączenia
		XMLHttpRequestObject.open("POST", dataSource);
		
		//ustawienie nagłówka
		XMLHttpRequestObject.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		
		//ustawienie funkcji przechwycającej dane z serwera
		XMLHttpRequestObject.onreadystatechange = function()
		{
			//jeśli pobieranie zakończone i pobieranie zakończone sukcesem
			if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200)
			{
				obj.innerHTML = XMLHttpRequestObject.responseText;
			} elseif (XMLHttpRequestObject.readyState != 4)
			{
				obj.innerHTML = "<h3>Błąd przetwarzania danych</h3>";
			}
		}
		obj.innerHTML = "<h3>Przetwarzanie danych...</h3>";
		XMLHttpRequestObject.send(form);
	}
}

function send_again()
{
	document.getElementById('result').innerHTML = '<div style="float: left; width: 90px; line-height: 25px;">Tytuł kalkulacji:&nbsp;<br/>Adres email</div><div style="float: right;"><input type="text" name="title" id="title" value=""  style="width: 220px; border: 1px solid orange; margin-right: 18px;"></div><div style="float: right;"><input type="text" name="email" id="email" value=""  style="width: 220px; border: 1px solid orange; margin-top: 10px;">&nbsp;<img src="img/mail.gif" alt="Wyślij" title="Wyślij" onclick="javascript: getData(\'index.php?p=send_mail\');" /></div>';
}

function wyczysc()
{
	document.location.href = "index.php?p=roi";
}
	





















