var preWorkExpIndex = 1;

function autoFocus() {

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

	if (tyer_no == "") {

		alert("Insert tyer_no value");

		document.getElementById("size").value = "";
		document.getElementById("comapny").value = "";
		document.getElementById("tyer_no").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 tyer_no = document.getElementById("tyer_no").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 = tyer_no + '<input id="tyer_no' + preWorkExpIndex
				+ '" type="hidden" name="Tyer_no" value="' + tyer_no + '">';
		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");

		newCell = newRow.insertCell(5);
		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("tyer_no").value = "";
		document.getElementById("sale_price").value = "0.0";
		document.getElementById("quantity").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 tyer_no = document.getElementById("tyer_no" + 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 = tyer_no + '<input id="tyer_no' + index
				+ '" type="hidden" name="Tyer_no" value="' + tyer_no + '">';
		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 sale_pricearr = document.getElementsByName('Sale_price');

	var total_sale_price = 0.0;

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

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

	// alert(tot_amount);

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

}
