function inputEnter(elem) {
	elem.style.color = '#000000';
	elem.value = '';
}

function inputExit(elem, val) {
	elem.style.color = '#ededed';
	elem.value = val;
}

function getPopup(url,width,height)
{
	fenster = window.open(url, "popup", "width="+width+",height="+height+",status=no,scrollbars=yes,resizable=no");
	fenster.focus();
}

function buildRow(row_count, hourly_rate) {
	var table_row = "";
	table_row = table_row + "\n";
	table_row = table_row + "	<tr>\n";
	table_row = table_row + "		<td width=\"10\">\n";
	table_row = table_row + "			<div id=\"pos_" + row_count + "\">" + (row_count + 1) + "</div>\n";
	table_row = table_row + "		</td>\n";
	table_row = table_row + "		<td width=\"120\">\n";
	table_row = table_row + "			<input type=\"text\" name=\"desc_" + row_count + "\" id=\"desc_" + row_count + "\" class=\"input_forms\" style=\"width:115px;\">\n";
	table_row = table_row + "		</td>\n";
	table_row = table_row + "		<td width=\"30\">\n";
	table_row = table_row + "			<input type=\"text\" name=\"amount_" + row_count + "\" id=\"amount_" + row_count + "\" class=\"input_forms\" style=\"width:30px;\" onChange=\"calcRow(" + row_count + ")\">\n";
	table_row = table_row + "		</td>\n";
	table_row = table_row + "		<td width=\"65\">\n";
	table_row = table_row + "			<select name=\"item_" + row_count + "\" id=\"item_" + row_count + "\" class=\"input_forms\" style=\"width:65px\" onChange=\"calcRow_" + row_count + "\">\n";
	table_row = table_row + "				<option value=\"S\" selected=\"selected\">Stunden</option>\n";
	table_row = table_row + "				<option value=\"P\">Pauschale</option>\n";
	table_row = table_row + "			</select>\n";
	table_row = table_row + "		</td>\n";
	table_row = table_row + "		<td width=\"60\">\n";
	table_row = table_row + "			<input type=\"text\" name=\"price_" + row_count + "\" id=\"price_" + row_count + "\" class=\"input_forms\" style=\"width:55px;\" onChange=\"calcRow(" + row_count + ")\" value=\"" + hourly_rate + "\">\n";
	table_row = table_row + "		</td>\n";
	table_row = table_row + "		<td width=\"60\" align=\"right\">\n";
	table_row = table_row + "			<div id=\"price_total_" + row_count + "\" style=\"width:50px;float:left;\">0</div>\n";
	table_row = table_row + "			<div style=\"width:10px;float:right;\">&euro;</div>\n";
	table_row = table_row + "		</td>\n";
	table_row = table_row + "		<td width=\"15\">\n";
	if (row_count > 0) {
		table_row = table_row + "			<a href=\"javascript:delRow(" + row_count + ")\"><img src=\"/images/icons/table_delete.png\" border=\"0\" alt=\"Zeile löschen\" title=\"Zeile löschen\"></a>\n";
	}
	else {
		table_row = table_row + "			&nbsp;\n";
	} 
	table_row = table_row + "		</td>\n";
	table_row = table_row + "	</tr></table>";
	
	return table_row;
}

function insertFirstRow(hourly_rate) {
	var row_count = 0;
	
	var new_row = buildRow(row_count, hourly_rate);
	
	// Add row
	document.getElementById("table_bill").innerHTML = (document.getElementById("table_bill").innerHTML).substr(0, (document.getElementById("table_bill").innerHTML).length - 8);
	document.getElementById("table_bill").innerHTML = document.getElementById("table_bill").innerHTML + new_row;
	document.getElementById("row_count").value = row_count;
}

function addRow(hourly_rate) {
	var row_count = parseInt(document.getElementById("row_count").value);
	var desc = new Array();
	var amount = new Array();
	var item = new Array();
	var price = new Array();
	var price_total = new Array();
	
	// save all inserted values
	for (var iCnt = 0; iCnt <= row_count; iCnt++) {
		desc[iCnt] = document.getElementById("desc_" + iCnt).value;
		amount[iCnt] = document.getElementById("amount_" + iCnt).value;
		item[iCnt] = document.getElementById("item_" + iCnt).value;
		price[iCnt] = document.getElementById("price_" + iCnt).value;
		price_total[iCnt] = document.getElementById("price_total_" + iCnt).value;
	}
	
	// count row 1 up
	row_count = row_count + 1;
	
	var new_row = buildRow(row_count, hourly_rate);
	
	// Add row
	document.getElementById("table_bill").innerHTML = (document.getElementById("table_bill").innerHTML).substr(0, (document.getElementById("table_bill").innerHTML).length - 8);
	document.getElementById("table_bill").innerHTML = document.getElementById("table_bill").innerHTML + new_row;
	document.getElementById("row_count").value = row_count;
	
	// set all rows to it's old values
	for (var iCnt = 0; iCnt <= (row_count-1); iCnt++) {
		document.getElementById("desc_" + iCnt).value			= desc[iCnt];
		document.getElementById("amount_" + iCnt).value			= amount[iCnt];
		document.getElementById("item_" + iCnt).value			= item[iCnt];
		document.getElementById("price_" + iCnt).value			= price[iCnt];
		document.getElementById("price_total_" + iCnt).value	= price_total[iCnt];
	}
}

function delRow(row_number) {
	var row_count = parseInt(document.getElementById("row_count").value);
	var desc = new Array();
	var amount = new Array();
	var item = new Array();
	var price = new Array();
	var price_total = new Array();
	
	// save all inserted values
	for (var iCnt = 0; iCnt <= row_count; iCnt++) {
		desc[iCnt] = document.getElementById("desc_" + iCnt).value;
		amount[iCnt] = document.getElementById("amount_" + iCnt).value;
		item[iCnt] = document.getElementById("item_" + iCnt).value;
		price[iCnt] = document.getElementById("price_" + iCnt).value;
		price_total[iCnt] = document.getElementById("price_total_" + iCnt).innerHTML;
	}
	
	
	// clear table
	document.getElementById("table_bill").innerHTML = "";
	document.getElementById("row_count").value = -1;
	
	// add old rows but not the deleted one
	var show_iCnt = -1;
	for (var iCnt = 0; iCnt <= row_count; iCnt++) {
		
		// if actual row is row, which has to be deleted
		if (iCnt == row_number) {
		}
		else {
			show_iCnt = show_iCnt + 1;
		}
		
		if (iCnt != row_number) {
			//alert("erzeuge " + iCnt + " von " + row_count + " mit Nr. " + show_iCnt);
			
			addRow(0);
			
			document.getElementById("desc_" + show_iCnt).value			= desc[iCnt];
			document.getElementById("amount_" + show_iCnt).value		= amount[iCnt];
			document.getElementById("item_" + show_iCnt).value			= item[iCnt];
			document.getElementById("price_" + show_iCnt).value			= price[iCnt];
			document.getElementById("price_total_" + show_iCnt).innerHTML= price_total[iCnt];
		}
	}
	calcTotal();
}

function calcRow(row_number) {
	var price_total = 0.0;
	
	var amount = parseFloat(document.getElementById("amount_" + row_number).value);
	var price = parseFloat(document.getElementById("price_" + row_number).value);
	
	price_total = amount * price;
	if (isNaN(price_total))
		price_total = 0;
	
	document.getElementById("price_total_" + row_number).innerHTML = price_total;
	
	calcTotal();
}

function calcTotal() {
	var row_count = parseInt(document.getElementById("row_count").value);
	var price_total = 0.0;
	
	// calculate total
	for (var iCnt = 0; iCnt <= row_count; iCnt++) {
		price_total = price_total + parseFloat(document.getElementById("price_total_" + iCnt).innerHTML);
	}
	
	if (isNaN(price_total))
		price_total = 0;
		
	document.getElementById("price_total").innerHTML = "Gesamt: " + price_total + " &euro;";
	document.getElementById("price").value = price_total;
}
