var preWorkExpIndex = 1;

function autoFocus() {

	var size = document.getElementById("size").value;
	var comapny = document.getElementById("comapny").value;
	var tyer_type = document.getElementById("tyer_type").value;
	var r_n = document.getElementById("r_n").value;
	var pattren = document.getElementById("pattren").value;
	var cut_ok = document.getElementById("cut_ok").value;
	var tyer_no = document.getElementById("tyer_no").value;
	var purchase_price = document.getElementById("purchase_price").value;
	var sale_price = document.getElementById("sale_price").value;
	var quantity = document.getElementById("quantity").value;
	var dealer_id_fk = document.getElementById("dealer_id_fk").value;
	var purchase_date = document.getElementById("purchase_date").value;

	if (tyer_no == "") {

		alert("Insert tyer_no value");

		document.getElementById("size").value = "";
		document.getElementById("comapny").value = "";
		document.getElementById("tyer_type").value = "";
		document.getElementById("r_n").value = "";
		document.getElementById("pattren").value = "";
		document.getElementById("cut_ok").value = "";
		document.getElementById("tyer_no").value = "";
		document.getElementById("purchase_price").value = "0.0";
		document.getElementById("sale_price").value = "0.0";
		document.getElementById("quantity").value = "0.0";
		document.getElementById("dealer_id_fk").value = "0";
		document.getElementById("purchase_date").value = "";

		// 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_type = document.getElementById("tyer_type").value;
		var r_n = document.getElementById("r_n").value;
		var pattren = document.getElementById("pattren").value;
		var cut_ok = document.getElementById("cut_ok").value;
		var tyer_no = document.getElementById("tyer_no").value;
		var purchase_price = document.getElementById("purchase_price").value;
		var sale_price = document.getElementById("sale_price").value;
		var rubber = document.getElementById("rubber").value;

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

		var dealer_id_fk = document.getElementById("dealer_id_fk").value;

		var purchase_date = document.getElementById("purchase_date").value;

		var newCell = newRow.insertCell(0);
		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(1);

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

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

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

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

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

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

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

		var newCell = newRow.insertCell(8);
		newCell.innerHTML = sale_price + '<input id="sale_price'
				+ preWorkExpIndex + '" type="hidden" name="Sale_price" value="'
				+ sale_price + '"><input id="purchase_price' + preWorkExpIndex
				+ '" type="hidden" name="Purchase_price" value="'
				+ purchase_price + '"><input id="dealer_id_fk'
				+ preWorkExpIndex
				+ '" type="hidden" name="Dealer_id_fk" value="' + dealer_id_fk
				+ '"><input id="purchase_date' + preWorkExpIndex
				+ '" type="hidden" name="Purchase_date" value="'
				+ purchase_date + '">';
		newCell.setAttribute("align", "center");

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

		newCell = newRow.insertCell(10);
		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_type").value = "";
		document.getElementById("r_n").value = "";
		document.getElementById("pattren").value = "";
		document.getElementById("cut_ok").value = "";
		document.getElementById("tyer_no").value = "";
		document.getElementById("purchase_price").value = "0.0";
		document.getElementById("sale_price").value = "0.0";
		document.getElementById("quantity").value = "1";
		document.getElementById("rubber").value = "";
		document.getElementById("tyer_no").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_type = document.getElementById("tyer_type" + j).value;
		var r_n = document.getElementById("r_n" + j).value;
		var pattren = document.getElementById("pattren" + j).value;
		var cut_ok = document.getElementById("cut_ok" + j).value;
		var tyer_no = document.getElementById("tyer_no" + j).value;
		var purchase_price = document.getElementById("purchase_price" + j).value;
		var sale_price = document.getElementById("sale_price" + j).value;
		var quantity = document.getElementById("quantity" + j).value;
		var rubber = document.getElementById("rubber" + j).value;

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

		var newCell = newRow.insertCell(0);
		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(1);

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

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

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

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

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

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

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

		var newCell = newRow.insertCell(8);
		newCell.innerHTML = sale_price + '<input id="sale_price' + index
				+ '" type="hidden" name="Sale_price" value="' + sale_price
				+ '"><input id="purchase_price' + index
				+ '" type="hidden" name="Purchase_price" value="'
				+ purchase_price + '"><input id="dealer_id_fk' + index
				+ '" type="hidden" name="Dealer_id_fk" value="' + dealer_id_fk
				+ '"><input id="purchase_date' + index
				+ '" type="hidden" name="Purchase_date" value="'
				+ purchase_date + '">';
		newCell.setAttribute("align", "center");

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

		newCell = newRow.insertCell(10);
		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 purchase_pricearr = document.getElementsByName('Sale_price');

	var total_purchase_price = 0.0;

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

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

	// alert(tot_amount);

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

}

function findDuplicateCode() {
	var arr_tyer_no = document.getElementsByName("Tyer_no");
	var ret = 0;

	for (var i = 1; i <= arr_tyer_no.length; i++) {

		if (document.getElementById("tyer_no").value.toUpperCase() == document
				.getElementById("tyer_no" + i).value.toUpperCase()) {

			ret = 1;
			alert("Same Code");
			document.getElementById('tyer_no').value = "";
			document.getElementById('tyer_no').focus();
		}
	}
	return ret;
}

function TyersizeWiseItemDetail() {

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

	if (tyer_no != "" && tyer_no != null) {

		var check = findDuplicateCode();

		if (check == 0) {

			$.ajax({
				url : 'Ajex.ItemInfoByTyerNo.jsp',
				data : 'tyer_no=' + tyer_no,
				type : 'post',
				success : function(msg) {
					// alert(msg);
					$('#abcd').html(msg);

					document.getElementById("size").value = document
							.getElementById("size_val").value;
					document.getElementById("comapny").value = document
							.getElementById("comapny_val").value;

					document.getElementById("tyer_type").value = document
							.getElementById("tyer_type_val").value;

					document.getElementById("r_n").value = document
							.getElementById("r_n_val").value;
					document.getElementById("pattren").value = document
							.getElementById("pattren_val").value;
					document.getElementById("cut_ok").value = document
							.getElementById("cut_ok_val").value;
					document.getElementById("purchase_price").value = document
							.getElementById("purchase_price_val").value;

					document.getElementById("purchase_date").value = document
							.getElementById("purchase_date_val").value;
					document.getElementById("dealer_id_fk").value = document
							.getElementById("dealer_id_fk_val").value;

					document.getElementById("rubber").value = document
							.getElementById("rubber_val").value;

					/* var a = document.getElementById("item_stock_qty").value; */

					// alert(a);
					/*
					 * document.getElementById("Item_total_qty").value =
					 * document .getElementById("item_stock_qty").value;
					 */

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

				}
			});

		} else {
		}

	} else {
		// alert("Model is Incorrect");

		document.getElementById('tyer_no').value = "";
		document.getElementById('tyer_no').focus();
	}

}

function onlyNumberWithFloat(htmlEvent, allowChar) {
	var keynum;
	if (window.event) // IE
	{
		keynum = htmlEvent.keyCode;
	} else if (htmlEvent.which) // Netscape/Firefox/Opera
	{
		keynum = htmlEvent.which;
	}
	// alert(keynum);
	// alert(allowChar.charCodeAt(0));
	var code = '';
	if (allowChar != undefined) {
		code = allowChar.charCodeAt(0);
	}
	if (keynum < 46 || keynum > 57 || keynum == 47) {
		if (keynum != 8 && keynum != code) {
			return false;
		}
	}
}

function onlyNumber(htmlEvent, allowChar) {
	var keynum;
	if (window.event) // IE
	{
		keynum = htmlEvent.keyCode;
	} else if (htmlEvent.which) // Netscape/Firefox/Opera
	{
		keynum = htmlEvent.which;
	}
	// alert(keynum);
	// alert(allowChar.charCodeAt(0));
	var code = '';
	if (allowChar != undefined) {
		code = allowChar.charCodeAt(0);
	}
	if (keynum < 48 || keynum > 57) {
		if (keynum != 8 && keynum != code) {
			return false;
		}
	}
}

function getCustomerInfoByName()

{
	var name = document.getElementById("customer_name").value;

	$.ajax({

		url : "Ajex_customer_Info_by_id.jsp",
		data : "name=" + name,
		type : "post",
		success : function(msg) {
			$("#abcd").html(msg);

			document.getElementById("customer_id_fk").value = document
					.getElementById("customer_id_fk_val").value;

			document.getElementById("cust_name").value = document
					.getElementById("customer_name_val").value;

			document.getElementById("mobile_no").value = document
					.getElementById("mobile_no_val").value;

			document.getElementById("address").value = document
					.getElementById("address_val").value;

		}
	});
}

function showHideDD(flag) {
	if (flag == 'DD' || flag == 'Cheque' || flag == 'Online') {
		showHideTemplateByCheckBox(true, 'ddDiv');
		showHideTemplateByCheckBox(false, 'ddDiv_cash');
		showHideTemplateByCheckBox(false, 'ddReturnDiv_cash');
	} else {
		showHideTemplateByCheckBox(false, 'ddDiv');
		showHideTemplateByCheckBox(true, 'ddDiv_cash');
		showHideTemplateByCheckBox(true, 'ddReturnDiv_cash');
	}
	document.getElementById("ddno").value = "";
	document.getElementById("bankname").value = "";
	document.getElementById("ddDate").value = "";
	document.getElementById("dd_amount").value = "0";
	returnAmt(0);
}

function showHideTemplateByCheckBox(flag, htmlObj) {
	// alert(htmlObj);
	var htmlObj1 = document.getElementById(htmlObj);
	if (htmlObj1 != undefined) {
		if (flag) {
			htmlObj1.style.display = "inline";
		} else {
			htmlObj1.style.display = "none";
		}
	}
}
