var ajax_url = "index.php?module=EcmPurchaseOrders&action=javahelper&to_pdf=1";


//get parent info use ajax
function getParentInfo(parent_id, parent_type) {
	var params = {
			id : parent_id,
			type : parent_type,
			job : 'getParentInfo'
	};
		
	$.ajax({
			type: "POST",
			url: ajax_url,
			dataType : "json",
			success: function(data) {
				if (data == '-1') {
					alert('Błąd');
					$("#parent_name").val('');
					$("#parent_id").val('');
				} else {
					setParentInfo(data);
				}
			},
			data: params
	});
}

//insert parent info in fields
function setParentInfo(data) {
	//pricebooks
	createPricebookSelect(data.pricebooks);
	if (data.ecmprice_id && data.ecmprice_name) {
		$("#ecmprice_id").val(data.ecmprice_id);
		$("#ecmprice_name").val(data.ecmprice_name);
	}
	if ($("#new_number").val() == false) return; //if edit exists load only pricebooks
	if (data.name)
		$("#parent_name_copy").val(data.name);
	if (data.parent_nip)
		$("#parent_nip").val(data.parent_nip);
	if (data.parent_address_street)
		$("#parent_address_street").val(data.parent_address_street);
	if (data.parent_address_city)
		$("#parent_address_city").val(data.parent_address_city);
	if (data.parent_address_postalcode)
		$("#parent_address_postalcode").val(data.parent_address_postalcode);
	if (data.parent_address_country)
		$("#parent_address_country").val(data.parent_address_country);
	if (data.invoice_type && data.invoice_type!='K')
		$("#no_tax").attr('checked', true);
	if (data.ecmpaymentcondition_id && data.ecmpaymentcondition_name) {
		$("#ecmpaymentcondition_id").val(data.ecmpaymentcondition_id);
		$("#ecmpaymentcondition_name").val(data.ecmpaymentcondition_name);
	}
	if (data.ecmdeliverycondition_id && data.ecmdeliverycondition_name) {
		$("#ecmdeliverycondition_id").val(data.ecmdeliverycondition_id);
		$("#ecmdeliverycondition_name").val(data.ecmdeliverycondition_name);
	}
	if (data.currency_id) 
		$("#currency_id option[value=\""+data.currency_id+"\"]").attr('selected', 'selected');
	//hide loading
	$(".loading_panel").css("display", "none");
}

function createPricebookSelect(pricebooks) {
	var s = $("<select id=\"pricebook_id\"/>");
	var opt = '';
	$.each(pricebooks, function(index, value) {
		opt+='<option value="'+value.id+'">'+value.name+'</option>';
	});
	s.html(opt);
	$("#pricebooks").append(s);
}

function generateNumber() {
	var params = {
			job : 'generateNumber'
	};
		
	$.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 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
	});		
}
