var preWorkExpIndex = 1;

function autoFocus() {

	var weight = document.getElementById("weight").value;
	var sale_price = document.getElementById("sale_price").value;
	var quantity = document.getElementById("quantity").value;
	var amount = document.getElementById("amount").value;

	if (weight == "") {

		alert("Insert weight value");

		document.getElementById("size").value = "";
		document.getElementById("comapny").value = "";
		document.getElementById("weight").value = "";
		document.getElementById("sale_price").value = "0.0";
		document.getElementById("quantity").value = "0.0";

		// document.getElementById("icode").focus();

	} else if (sale_price == "" || sale_price < 0 || isNaN(sale_price)) {
		alert("Insert purchase price ");
		document.getElementById("sale_price").value = "0";
		document.getElementById("sale_price").focus();
	} else if (quantity == "" || quantity <= 0 || isNaN(quantity)) {
		alert("Insert qty value");
		document.getElementById("quantity").value = "0";
		document.getElementById("quantity").focus();
	} else {

		// setAmount();

		var applicationTable = document.getElementById("tableScroll");

		var newRow = applicationTable.insertRow(preWorkExpIndex - 1);

		// alert(preWorkExpIndex);

		var size = document.getElementById("size").value;
		var comapny = document.getElementById("comapny").value;
		var weight = document.getElementById("weight").value;
		var sale_price = document.getElementById("sale_price").value;
		var quantity = document.getElementById("quantity").value;
	
		
		

		var newCell = newRow.insertCell(0);

		newCell.innerHTML = size.toUpperCase() + '<input id="size'
				+ preWorkExpIndex + '" type="hidden" name="Size" value="'
				+ size + '">';
		newCell.setAttribute("align", "center");

		var newCell = newRow.insertCell(1);
		newCell.innerHTML = comapny + '<input id="comapny' + preWorkExpIndex
				+ '" type="hidden" name="Comapny" value="' + comapny + '">';
		newCell.setAttribute("align", "center");
		var newCell = newRow.insertCell(2);
		newCell.innerHTML = weight + '<input id="weight' + preWorkExpIndex
				+ '" type="hidden" name="Weight" value="' + weight + '">';
		newCell.setAttribute("align", "center");

		var newCell = newRow.insertCell(3);
		newCell.innerHTML = sale_price + '<input id="sale_price'
				+ preWorkExpIndex + '" type="hidden" name="Sale_price" value="'
				+ sale_price + '">';
		newCell.setAttribute("align", "center");

		var newCell = newRow.insertCell(4);
		newCell.innerHTML = quantity + '<input id="quantity' + preWorkExpIndex
				+ '" type="hidden" name="Quantity" value="' + quantity + '">';
		newCell.setAttribute("align", "center");
		
		var newCell = newRow.insertCell(5);
		newCell.innerHTML = amount + '<input id="amount' + preWorkExpIndex
				+ '" type="hidden" name="Amount" value="' + amount + '">';
		newCell.setAttribute("align", "center");

		newCell = newRow.insertCell(6);
		newCell.innerHTML = "<img src='img/close.gif' onclick='deleteRow(\""
				+ applicationTable.id
				+ "\",this.parentNode.parentNode.rowIndex); '/>";

		document.getElementById("size").value = "";
		document.getElementById("comapny").value = "";
		document.getElementById("weight").value = "";
		document.getElementById("sale_price").value = "0.0";
		document.getElementById("quantity").value = "0";
		document.getElementById("amount").value = "0";
		

		document.getElementById("size").focus();

		preWorkExpIndex++;

		findTotal();
		findTotalQty();

	}
}

function deleteRow(tableObjId, i, btnId) {

	var btnObj = document.getElementById(btnId);
	var tblObj = document.getElementById(tableObjId);
	preWorkExpIndex--;

	tblObj.deleteRow(i - 1);

	if (btnObj != null) {
		if (tblObj.rows.length <= 7) {
			btnObj.disabled = "";
		}
	}

	findTotal();
	findTotalQty();

	manageRow(i);
}

function manageRow(rowNo) {

	var applicationTable = document.getElementById("tableScroll");
	var tblObj = document.getElementById(applicationTable.id);

	for (var index = rowNo; index <= tblObj.rows.length; index++) {
		j = index + 1;

		var size = document.getElementById("size" + j).value;
		var comapny = document.getElementById("comapny" + j).value;
		var weight = document.getElementById("weight" + j).value;
		var sale_price = document.getElementById("sale_price" + j).value;
		var quantity = document.getElementById("quantity" + j).value;

		var newRow = applicationTable.insertRow(index - 1);
		// alert(index);

		var newCell = newRow.insertCell(0);

		newCell.innerHTML = size.toUpperCase() + '<input id="size' + index
				+ '" type="hidden" name="Size" value="' + size + '">';
		newCell.setAttribute("align", "center");

		var newCell = newRow.insertCell(1);
		newCell.innerHTML = comapny + '<input id="comapny' + index
				+ '" type="hidden" name="Comapny" value="' + comapny + '">';
		newCell.setAttribute("align", "center");
		var newCell = newRow.insertCell(2);
		newCell.innerHTML = weight + '<input id="weight' + index
				+ '" type="hidden" name="Weight" value="' + weight + '">';
		newCell.setAttribute("align", "center");

		var newCell = newRow.insertCell(3);
		newCell.innerHTML = sale_price + '<input id="sale_price' + index
				+ '" type="hidden" name="Sale_price" value="' + sale_price
				+ '">';
		newCell.setAttribute("align", "center");

		var newCell = newRow.insertCell(4);
		newCell.innerHTML = quantity + '<input id="quantity' + index
				+ '" type="hidden" name="Quantity" value="' + quantity + '">';
		newCell.setAttribute("align", "center");

		newCell = newRow.insertCell(5);
		newCell.innerHTML = "<img src='img/close.gif' onclick='deleteRow(\""
				+ applicationTable.id
				+ "\",this.parentNode.parentNode.rowIndex); '/>";

		tblObj.deleteRow(index);
	}
}

function findTotalQty() {
	var arr = document.getElementsByName('Quantity');
	var qty = 0;
	for (var i = 0; i < arr.length; i++) {
		if (parseInt(arr[i].value)) {
			qty += parseInt(arr[i].value);

			document.getElementById('TotalQty').value = qty;

			document.getElementById('TotalQtyDiv').innerHTML = qty;

		}
	}
}

function findTotal() {

	var amountearr = document.getElementsByName('Amount');

	var total_sale_price = 0.0;

	for (var i = 0; i < amountearr.length; i++) {
		if (parseFloat(amountearr[i].value))
			total_sale_price = total_sale_price
					+ parseFloat(amountearr[i].value);
	}

	var tot_amount = parseFloat(total_sale_price).toFixed(2);

	// alert(tot_amount);

	document.getElementById('total_amount').value = parseFloat(tot_amount)
			.toFixed(2);

}
