var itemsTable = 'itemsTable';
var items = new Array();
var displayArray = new Array();
var searchedProducts = new Array();
var ajax_url = "index.php?module=EcmStockDocInsideIns&action=javahelper&to_pdf=1";

function FillTable(data, allReadOnly) {
	html = '';
	// loop throw data rows
	$
			.each(
					data,
					function(row_index, row) {
						html += '<tr>';
						// and insert columns
						$
								.each(
										columns,
										function(col_index, column) {
											// row must have code and name
											if (!row.product_code
													|| row.product_code == ''
													|| !row.name
													|| row.name == '')
												return; // return in each =
											// continue in php for
											// loop
											// special types
											if (column.name == 'number') {
												html += '<td><input class="inputs" style="text-align: center;" type="text" id="number_'
														+ row_index
														+ '" name="number_'
														+ row_index
														+ '" readonly value="'
														+ (row_index + 1)
														+ '"/></td>';
											} else if (column.name == 'name') {
												html += '<td><textarea style="width: 100%; height: 100%;" id="name_'
														+ row_index
														+ '" name="name_'
														+ row_index + '"';
												if (allReadOnly)
													html += ' readonly';
												html += '>' + row.name
														+ '</textarea></td>';
											} else if (column.name == 'options') {
												if (allReadOnly)
													html += '<td></tr>';
												else {
													html += '<td>';
													// move up
													html += '<a onClick="moveUpRow('
															+ row_index
															+ ')" target="_blank"><img style="cursor:pointer;" src="modules/EcmStockDocInsideIns/images/moverowup.gif"/></a>';
													html += '&nbsp;';
													// move down
													html += '<a onClick="moveDownRow('
															+ row_index
															+ ')" target="_blank"><img style="cursor:pointer;" src="modules/EcmStockDocInsideIns/images/moverowdown.gif"/></a>';
													html += '<br>';
													// delete row
													html += '<a onClick="deleteRow('
															+ row_index
															+ ')" target="_blank"><img style="cursor:pointer;" src="modules/EcmStockDocInsideIns/images/deleterow.gif"/></a>';
													html += '&nbsp;';
													// get components
													html += '<a onClick="getComponents('
															+ row_index
															+ ')" target="_blank"><img style="cursor:pointer;" src="modules/EcmStockDocInsideIns/images/get_components.gif"/></a>';
													html += '</td>';
												}
											}
											// other types
											else {
												html += '<td>';
												$
														.each(
																column.content,
																function(
																		cell_index,
																		cell) {
																	var cellname = column.name
																			+ cell.name;
																	if (cellname == 'product_link') {
																		html += '<a href="index.php?module=EcmProducts&action=DetailView&record='
																				+ row.product_id
																				+ '" target="_blank">'
																				+ row.product_code
																				+ '</a>';
																	} else if (cellname == 'price_start_div'
																			&& !allReadOnly) {
																		html += '<br><img style="cursor: pointer;" src="modules/EcmQuotes/images/search.gif" onClick="if ($(\'#price_start_div_'
																				+ row_index
																				+ '\').css(\'display\')==\'none\') getPricesInfo('
																				+ row_index
																				+ '); else $(\'#price_start_div_'
																				+ row_index
																				+ '\').hide(\'slow\')"/>';
																		html += '<div id="'
																				+ cellname
																				+ '_'
																				+ row_index
																				+ '" style="display:none;float:right;text-align:right;border: 1px #cccccc solid;padding:3px;"></div>';
																	} else {
																		if (cell.label
																				&& cell.label != '')
																			html += '<p>'
																					+ cell.label
																					+ '</p>';
																		html += '<input class="inputs" type="'
																				+ cell.type
																				+ '" name="'
																				+ cellname
																				+ '_'
																				+ row_index
																				+ '" id="'
																				+ cellname
																				+ '_'
																				+ row_index
																				+ '"';
																		if (cell.readonly
																				|| allReadOnly)
																			html += ' readonly ';
																		if (column.align)
																			html += ' style="text-align: '
																					+ column.align
																					+ ';"';
																		if (cell.precision) precision = cell.precision; else precision = 2;
																		if (cell.onChange
																				|| cell.isNumber) {
																			if (cellname == 'quantity')
																				precision = 4;
																			else
																				precision = 2;
																			html += ' onChange="$(this).val(FormatNumber($(this).val(),'+precision+'));'
																					+ cell.onChange
																					+ '" onClick="$(this).select();"';
																			
																		}
																		else if (cell.onChange
																				|| !cell.isNumber)
																			html += ' onChange="'
																					+ cell.onChange
																					+ '" ';
																		if (row[cellname]
																				&& cell.isNumber)
																			html += ' value="'
																					+ FormatNumber(row[cellname], precision)
																					+ '" ';
																		else if (row[cellname])
																			html += ' value="'
																					+ row[cellname]
																					+ '" ';
																		else {
																			if (cell.isNumber)
																				html += ' value="0,00" ';
																			else
																				html += ' value="" ';
																		}
																		html += '>';
																	}
																});
												html += '</td>';
											}

										});
						html += '</tr>';

					});

	$('#' + itemsTable + '_T > tbody').html(html);
	if (allReadOnly)
		DrawDetailSummary();
	else
		checkProducts();
}

function deleteRow(index) {
	items.splice(index, 1);
	FillTable(items);
}

function moveUpRow(index) {
	if (index == 0)
		return; // Can't go upper.. :(
	var new_index = index - 1;
	var old_index = index;
	if (new_index >= items.length) {
		var k = new_index - items.length;
		while ((k--) + 1) {
			items.push(undefined);
		}
	}
	items.splice(new_index, 0, items.splice(old_index, 1)[0]);
	FillTable(items);
}

function moveDownRow(index) {
	if (index == items.length - 1)
		return; // Can't go lower.. :(
	var new_index = index + 1;
	var old_index = index;
	if (new_index >= items.length) {
		var k = new_index - items.length;
		while ((k--) + 1) {
			items.push(undefined);
		}
	}
	items.splice(new_index, 0, items.splice(old_index, 1)[0]);
	FillTable(items);
}

function FormatNumber(number, precision) {
	if (number < 0)
		number = 0;
	
	var precision = precision || 2;
	// make string..
	number = number + '';
	number = number.replace(',', '.');
	// round
	number = toFixed(number, precision);
	// add 1000 sep
	var tmp = number.split(".");
	var c = '';
	for (var i = tmp[0].length; i != -1; i--) {
		c += tmp[0].charAt(i);
		if ((tmp[0].length - i) == 0 || i == 0)
			continue;
		if ((tmp[0].length - i) % 3 == 0)
			c += '.';
	}
	// reverse c
	c = c.split("").reverse().join("");

	return c + ',' + tmp[1];
}

function UnformatNumber(number) {
	// make string..
	number = number + '';
	// remove 1000 sep
	number = number.replace('.', '');
	// change ',' to '.'
	number = number.replace(',', '.');

	return parseFloat(number);
}

// round with precision
function toFixed(value, precision) {
	var precision = precision || 0, neg = value < 0, power = Math.pow(10,
			precision), value = Math.round(value * power), integral = String((neg ? Math.ceil
			: Math.floor)(value / power)), fraction = String((neg ? -value
			: value)
			% power), padding = new Array(Math.max(precision - fraction.length,
			0) + 1).join('0');

	return precision ? integral + '.' + padding + fraction : integral;
}

// draw table
function DrawHeaders() {
	var html = '<link rel="stylesheet" type="text/css" href="modules/EcmQuotes/MyTable.css" />';
	html += '<div style="width:100%;border: 1px solid rgb(48,192,255);background-color:white;height: 250px;overflow:auto;" id="'
			+ itemsTable + 'DIV">';
	html += '<table class="positions" style="width:100%;" id="' + itemsTable
			+ '_T">';
	html += '<thead id="head">';
	html += '<tr id="tr">';
	// draw columns headers
	$.each(columns, function(index, column) {
		html += '<td width="' + column.width + '%">' + column.label + '</td>';
	});
	html += '</tr></thead><tbody></tbody></table>';
	html += '</div><br>';
	// totals table
	html += '<table width="100%"" cellpadding="0" cellspacing="0" border="0">';
	html += '<tr>';
	html += '<td width="55%" class="dataLabel" valign="top">';
	html += '&nbsp;';
	html += '</td>                                                                               <!--color:#b3b9cf;-->';
	html += '<td width="40%" class="dataField" style="text-align: left;">';
	html += '<br>';
	html += '<table  id="result_table" cellpadding="0" cellspacing="0" style="width:100%; height:100%; border: 1px solid rgb(48,192,255);">';
	html += '</table>';
	html += '</td>';
	html += '<td width="5%" class="dataField" style="text-align: left;">&nbsp;</td>';
	html += '</tr>';
	html += '</table>';
	// sort? why not!
	/*
	 * html += 'Sortowanie:&nbsp;'; html += '<select id="sort_field"
	 * onChange="sortTable();"'; html += '<option value=""></option>'; html += '<option
	 * value="name">Nazwa</option>'; html += '<option value="code">Kod</option>';
	 * html += '<option value="category">Kategoria</option>'; html += '<option
	 * value="category">Kategoria</option>'; html += '<option
	 * value="price">Cena po<br>upuście</option>'; html += '<option
	 * value="total">Wartość</option>'; html += '</select>';
	 */
	$('#' + itemsTable).html(html);
}
function searchProducts() {
	var searchKey = $('#searchProductsInput').val();
	if (searchKey.length < 2 && searchKey != '%') {
		return;
	}
	$("#searchResultDiv").html(
			'<img src="modules/EcmQuotes/images/loading.gif"/>');
	setTimeout(function() {
		if (searchKey == $('#searchProductsInput').val()) {
			var params = {
				job : 'searchProducts',
				searchKey : searchKey,
				searchCategory : $("#productSearchCategory :selected").val(),
				searchStock : $("#productSearchStock :selected").val(),
				searchSort : $("#productSearchSort :selected").val(),
				searchStockId : $("#stock :selected").val(),
			};

			$.ajax({
				type : "POST",
				url : ajax_url,
				dataType : "json",
				success : function(data) {
					if (data != '-1')
						if (data.length == 0)
							$('#searchResultDiv').html(
									'MOD.LBL_SEARCH_NO_RESULT');
						else
							createSearchResult(data);
				},
				data : params
			});
		}
	}, 1000);
}

function createSearchResult(data) {
	var html = '<table style="text-align: center;" cellpadding="0" cellspacing="0" width="100%" border="0" class="list view">';
	html += '<theader><tr>';
	html += '<th  style="width: 15pt; margin: auto auto;">&nbsp</th>';
	html += '<th style="width: 100pt; text-align: left;">Kod</th>';
	html += '<th style="width: 300pt; text-align: left;">Nazwa</th>';
	if ($("#productSearchStock :selected").val() != '1')
		html += '<th  style="width: 30pt; text-align: right;">Stan</th>';
	html += '<th  style="width: 15pt; margin: auto auto;">&nbsp</th>';

	html += '</tr></thead>';
	var counter;
	$
			.each(
					data,
					function(index, value) {
						if(counter%2==0){
							html += '<tr style="height: 15pt;" class="oddListRowS1">';
						} else {
							html += '<tr style="height: 15pt;" class="evenListRowS1">';
						}
						
						html += '<td "><input id="prod_'
								+ value.id + '" type="checkbox"/></td>';
						html += '<td style="text-align:left" onClick="$(\'#prod_'
								+ value.id
								+ '\').prop(\'checked\', true);">'
								+ value.code + '</td>';
						html += '<td style="text-align:left" onClick="unsetAllCheckboxes(); $(\'#prod_'
								+ value.id
								+ '\').prop(\'checked\', true); return addProducts();">'
								+ value.name + '</td>';
						if ($("#productSearchStock :selected").val() != '1')
							html += '<td style="border: 0.5px dashed black;text-align:right;">'
									+ value.stock_state + '</td>';
						html += '<td>';
						html += '<img style="cursor: pointer;" src="modules/EcmQuotes/images/search.gif" onClick="if ($(\'#search_info_div_'
								+ value.id
								+ '\').css(\'display\')==\'none\') getSearchInfo(\''
								+ value.id
								+ '\',\''
								+ value.code
								+ '\'); else $(\'#search_info_div_'
								+ value.id
								+ '\').hide(\'slow\')"/>';
						html += '<div id="'
								+ 'search_info_div_'
								+ value.id
								+ '" style="display:none;float:right;text-align:right;border: 1px #cccccc solid;padding:3px;"></div>';
						html += '</td>';
						html += '</tr>';
						searchedProducts.push(value.id);
						counter++;
					});
	// add button
	html += '</table><br>';
	html += 'Cena:&nbsp<input type="text" id="searchInputPrice" value=""/>&nbspIlość:&nbsp<input type="text" id="searchInputQty" value=""/>';
	html += '<br><br><input type="button" value="Dodaj wiele" onClick="return addProducts();"/>';
	$('#searchResultDiv').html(html);
}

function unsetAllCheckboxes() {
	$("#searchResultDiv input[type=checkbox]").each(function() {
		$(this).prop("checked", false);
	});
}

function addProducts() {
	$(".loading_panel").css("display", "block");
	var products = new Array();
	$.each(searchedProducts, function(index, value) {
		if ($('#prod_' + value).is(':checked'))
			products[value] = true;
	});
	for ( var key in products) {
		var params = {
			job : 'getProduct',
			id : key,
			stockId : $("#stock_id :selected").val(),
		};
		$.ajax({
			type : "POST",
			url : ajax_url,
			dataType : "json",
			async : false,
			success : function(data) {
				if (data != '-1') {
					var p = new Object();
					p.product_id = data.id;
					p.name = data.name;
					p.product_code = data.code;
					p.price = data.price;
					p.unit_name = data.unit_name;
					p.unit_id = data.unit_id;
					p.stock_state = data.stock_state;
					p.product_category_id = data.product_category_id;
					if ($("#searchInputQty").val() != ''
							&& !isNaN(parseFloat(UnformatNumber($(
									"#searchInputQty").val()))))
						p.quantity = $("#searchInputQty").val();
					items.push(p);
				}
			},
			data : params
		});
	}
	FillTable(items);
	// clear results
	$('#searchResultDiv').html('');
	searchedProducts = new Array();
	$('#searchProductsInput').val('');

	$(".loading_panel").css("display", "none");
}

function getItems(editview) {
	var record = '';
	if ($("#duplicate").val() == true)
		record = $("input[name='return_id']").val();
	else
		record = $("input[name='record']").val();
	if (isNaN(editview))
		editview = true;
	var params = {
		job : 'getItems',
		record : record,
	};
	$.ajax({
		type : "POST",
		url : ajax_url,
		dataType : "json",
		success : function(data) {
			items = data;
			if (editview)
				FillTable(items, false);
			else
				FillTable(items, true);
			$(".loading_panel").css("display", "none");
		},
		data : params
	});
}

function DrawDetailSummary() {
	$("#result_table").html('');
	html = '';
	html += '<tr id="subtotal_tr"> ';
	html += '<td class="positionsLabel" style="border-top:0px;">Suma dokumentu</td>';
	html += '<td class="positionsField" style="border-top:0px;"><input type="text" style="border:0px;font-weight:900;width:100%;text-align:right;" readonly="readonly" name="t_netto" id="t_netto" value=\''
			+FormatNumber($("#total_netto").val()) + '\'></td>';
	html += '</tr>';

	html += '<tr id="discount_tr"> ';
	html += '<td class="positionsLabel">Suma operacji: wejście</td>';
	html += '<td class="positionsField"><input type="text" readonly="readonly" style="border:0px;font-weight:900;width:100%;text-align:right;" name="disc" id="disc" value=\''
			+ $("#operations_in").val() + '\'></td>';
	html += '</tr>';
	html += '<tr id="total_tr"> ';
	html += '<td class="positionsLabel"><a href="index.php?module=EcmStockOperations&custom_parent_id='
			+ $("input[name=record]").val()
			+ '" target="blank">Podgląd operacji magazynowych</a></td>';
	html += '<td class="positionsField">&nbsp;</td>';
	html += '</tr>';

	$("#result_table").html(html);
}

function generateNumber() {
	var params = {
		job : 'generateNumber',
		stock: $("#stock_id").val(),
	};
	$.ajax({
		type : "POST",
		url : ajax_url,
		dataType : "json",
		success : function(data) {
			if (data == '-1') {
				// try loading again
				generateNumber();
			} else {
				$("#document_no").val(data.document_no);
				$("#number").val(data.number);
				$(".loading_panel").css("display", "none");
			}
		},
		data : params
	});
}
function getPricesInfo(index) {
	$("#price_start_div_" + index).show('slow');
	$("#price_start_div_" + index).html(
			'<img src="modules/EcmQuotes/images/loading.gif"/>');

	var params = {
		job : 'getPricesInfo',
		product_id : $("#product_id_" + index).val(),
		pricebook_id : $("#pricebook_id :selected").val(),
		account_id : $("#parent_id").val(),
	};
	$
			.ajax({
				type : "POST",
				url : ajax_url,
				dataType : "json",
				async : false,
				success : function(data) {
					html = '<ul style="font-size: 7pt;">';
					$
							.each(
									data,
									function(key, value) {
										if (value.name == 'pricebook') {
											html += '<li style="cursor:pointer;" onClick="$(\'#price_start_'
													+ index
													+ '\').val(\''
													+ FormatNumber(value.price)
													+ '\'); $(this).closest(\'div\').hide(\'slow\');calculateRow('
													+ index
													+ ')"><u>'
													+ $(
															"#pricebook_id :selected")
															.html()
													+ ': '
													+ FormatNumber(value.price)
													+ '</u></li>';
										} else if (value.name == $(
												"#ecmprice_name").val()) {
											html += '<li style="cursor:pointer;" onClick="$(\'#price_start_'
													+ index
													+ '\').val('
													+ value.price
													+ '); $(this).closest(\'div\').hide(\'slow\');calculateRow('
													+ index
													+ ');"><b>'
													+ value.name
													+ ': '
													+ FormatNumber(value.price)
													+ '</b></li>';
										} else {
										if(value.visible=='1'){
											html += '<li style="cursor:pointer;" onClick="$(\'#price_start_'
													+ index
													+ '\').val('
													+ value.price
													+ '); $(this).closest(\'div\').hide(\'slow\');calculateRow('
													+ index
													+ ');">'
													+ value.name
													+ ': ';
													if(value.module=='EcmPrices'){
													html+='<font style="color:#7FFF00;">'+FormatNumber(value.price)+'</font>';
													}
													if(value.module=='EcmInvoiceOuts'){
													html+='<font style="color:#FF3333;">'+FormatNumber(value.price)+'</font>';
													}
													html+ '</li>';
											}
										}
									});
					html += '</ul>';
				},
				data : params
			});
	$("#price_start_div_" + index).html(html);
}
function getCategoriesList() {
	var params = {
		job : 'getCategoriesList'
	};

	$.ajax({
		type : "POST",
		url : ajax_url,
		dataType : "json",
		success : function(data) {
			var html = '<option value=""></option>';
			$.each(data, function(index, value) {
				html += '<option value="' + value.id + '">' + value.name
						+ '</option>';
				$("#productSearchCategory").html(html);
			});
		},
		data : params
	});
}

function refreshStock(index) {
	var params = {
		job : 'getStockState',
		id : $("#product_id_" + index).val(),
		stockId : $("#stock_id").val(),
	};

	$.ajax({
		type : "POST",
		url : ajax_url,
		dataType : "json",
		async : false,
		success : function(data) {
			$("#stock_state_" + index).val(FormatNumber(data));
		},
		data : params
	});
}

function checkProducts() {
	var count = $('#' + itemsTable + '_T tr').length - 1; // -1 - thead row
	var error = false; // hope :)
	var total_netto=0;
	for (var index = 0; index != count; index++) {
		refreshStock(index);
		var qty = UnformatNumber($("#quantity_" + index).val());
		var cat_id = $("#product_category_id_" + index).val();
		// refresh items array
		items[index].quantity = qty;
		items[index].price = UnformatNumber($("#price_" + index).val());
		items[index].price_total=items[index].quantity*items[index].price;
		$("#price_total_" + index).val(items[index].price_total);
		total_netto+=items[index].price_total;
		var stock = UnformatNumber($("#stock_state_" + index).val());
	}
	$("#total_netto").val(total_netto);
	DrawDetailSummary();
	if (error)
		return false;
	else
		return true;
}

function getComponents(index) {
	$(".loading_panel").css("display", "block");
	var product_id = items[index].product_id;
	var qty = items[index].quantity;
	//var price = items[index].price;

	var params = {
		job : 'getComponents',
		product_id : product_id,
	};
	$.ajax({
		type : "POST",
		url : ajax_url,
		dataType : "json",
		async : false,
		success : function(data) {
			if (data.length > 0) {
				items.splice(index, 1);
				$.each(data, function(index, value) {
					var p = new Object();
					p.product_id = value.id;
					p.name = value.name;
					p.product_code = value.code;
					p.price = value.price;
					p.unit_name = value.unit_name;
					p.unit_id = value.unit_id;
					p.quantity = qty * parseInt(value.quantity);
					p.product_category_id = value.product_category_id;
					
					if (p.product_category_id=='d7f876b0-1a3d-43a1-7c9b-511ba40df3d1')
						return;
					items.push(p);
				});
			};
		},
		data : params
	});
	FillTable(items);
	$(".loading_panel").css("display", "none");
}

function getSearchInfo2(product_id, product_code) {
	$("#search_info_div2_" + product_id).show().draggable();
	var html = '<img style="cursor: pointer;" align="left" src="themes/Sugar/images/close_inline.gif" onClick="$(\'#search_info_div2_'
								+ product_id
								+ '\').hide(\'slow\')"/>';
	 html += '<a href="index.php?module=EcmProducts&action=DetailView&record='
			+ product_id + '" target="_blank">' + product_code + '</a><br><br>';
	 html +='<table><tr><td style="text-align:left;">Magazyn</td><td>Ilość</td></tr>';
	var params = {
		job : 'getStockArray',
		product_id : product_id,
	};
	$.ajax({
		type : "POST",
		url : ajax_url,
		dataType : "json",
		async : false,
		success : function(data) {
			$.each(data, function(key, value) {
					html += '<tr><td style="text-align:left;">'+key + '</td><td>' + FormatNumber(value)
							+ '</td></tr>';
			});
		},
		data : params
	});
	html += '</table>';

	$("#search_info_div2_" + product_id).html(html);
}

function getSearchInfo(product_id, product_code) {
	$("#search_info_div_" + product_id).show().draggable();
	var html = '<img style="cursor: pointer;" align="left" src="themes/Sugar/images/close_inline.gif" onClick="$(\'#search_info_div_'
								+ product_id
								+ '\').hide(\'slow\')"/>';
	 html += '<a href="index.php?module=EcmProducts&action=DetailView&record='
			+ product_id + '" target="_blank">' + product_code + '</a><br><br>';
	// get prices info
	 html +='<table><tr><td style="text-align:left;">Nazwa</td><td>Ilość</td><td>Cena</td><td>Data dok</td></tr>';
	var params = {
		job : 'getPricesInfo',
		product_id : product_id,
		pricebook_id : $("#pricebook_id :selected").val(),
		account_id : $("#parent_id").val(),
	};
	$.ajax({
		type : "POST",
		url : ajax_url,
		dataType : "json",
		async : false,
		success : function(data) {
			$.each(data, function(key, value) {
				if (value.name == 'pricebook')
					html += $("#pricebook_id :selected").html() + ': '
							+ FormatNumber(value.price) + '<br>';
				if (value.name == $("#ecmprice_name").val())
					html += '<b>' + value.name + ': '
							+ FormatNumber(value.price) + '</b><br>';
				else

					if(value.module=='EcmStockDocIns'){
						html+='<tr><td style="text-align:left;">'+value.name + '</td><td>'+value.quantity+'</td><td> <font style="color:#7FFF00;text-align:right;">'+FormatNumber(value.price)+'</font></td><td>'+value.register_date+'</td></tr>';
					}
					//html += value.name + ': ' + FormatNumber(value.price)
						//	+ '<br>';
			});
		},
		data : params
	});
	html+='</tr></table>';

	$("#search_info_div_" + product_id).html(html);
}

function getSearchInfo3(product_id, product_code) {
	$("#search_info_div3_" + product_id).show().draggable();
	var html = '<img style="cursor: pointer;" align="left" src="themes/Sugar/images/close_inline.gif" onClick="$(\'#search_info_div3_'
								+ product_id
								+ '\').hide(\'slow\')"/>';
	 html += '<a href="index.php?module=EcmProducts&action=DetailView&record='
			+ product_id + '" target="_blank">' + product_code + '</a><br><br>';
	// get prices info
	 html +='<table><tr><td style="text-align:left;">Nazwa</td><td>Cena</td></tr>';
	var params = {
		job : 'getPricesInfo',
		product_id : product_id,
		pricebook_id : $("#pricebook_id :selected").val(),
		account_id : $("#parent_id").val(),
	};
	$.ajax({
		type : "POST",
		url : ajax_url,
		dataType : "json",
		async : false,
		success : function(data) {
			$.each(data, function(key, value) {
				if (value.name == 'pricebook')
					html += $("#pricebook_id :selected").html() + ': '
							+ FormatNumber(value.price) + '<br>';
				if (value.name == $("#ecmprice_name").val())
					html += '<b>' + value.name + ': '
							+ FormatNumber(value.price) + '</b><br>';
				else

					if(value.module=='EcmPrices'){
						html+='<tr><td style="text-align:left;">'+value.name + ':</td><td> <font style="color:#7FFF00;text-align:right;">'+FormatNumber(value.price)+'</font></td></tr>';
					}
					if(value.module=='EcmInvoiceOuts'){
						html+='<tr><td "text-align:left;">'+value.name + ':</td><td> <font style="color:#FF3333;text-align:right;">'+FormatNumber(value.price)+'</font></td></tr>';
					}
					//html += value.name + ': ' + FormatNumber(value.price)
						//	+ '<br>';
			});
		},
		data : params
	});
	html+='</table>';

	$("#search_info_div3_" + product_id).html(html);
}
// sortable rows in main table
$("#" + itemsTable + " tbody").sortable();