$().ready(function() {

	//AUTOCOMPLETE CLIENT DETAILS
	 $( "#suggest_client" ).autocomplete({
		source: function( request, response ) {
			$.ajax({
			url: "inc/json.php",
			dataType: "json",
				data: {
					tip: "client",
					name: request.term
				},
				success: function( data ) {
					response( $.map( data.clients, function( item ) {
						return {
							label: item.name +", "+item.location,
							cemail: item.email,
							cphone: item.phone,
							clocation: item.location,
							caddress: item.address,
							value: item.name
						}
					}));
				},
				  error: function (xhr, ajaxOptions, thrownError) {
					alert(xhr.status);
					alert(thrownError);
				  }
			});
		},
		minLength: 2,
		select: function( event, ui ) {
			$('#email').val(ui.item.cemail);
			$('#phone').val(ui.item.cphone);
			$('#location').val(ui.item.clocation);
			$('#address').val(ui.item.caddress);
		},
		open: function() {$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );},
		close: function() {$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );}
	});
	
	//AUTOCOMPLETE PAYMENT METHOD
	
	 $( "#pmethod" ).autocomplete({
		source: function( request, response ) {
			$.ajax({
			url: "inc/json.php",
			dataType: "json",
				data: {
					tip: "payment",
					name: request.term
				},
				success: function( data ) {
					response( $.map( data.pmethods, function( item ) {
						return {
							label: item.meth,
							value: item.meth
						}
					}));
				}
			});
		},
		minLength: 2,
		select: function( event, ui ) {
			
		},
		open: function() {$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );},
		close: function() {$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );}
	});
	
	
	
	//AUTOCOMPLETE PRODUCTS
	
	 $( "#prod_title" ).autocomplete({
		source: function( request, response ) {
			$.ajax({
			url: "inc/json.php",
			dataType: "json",
				data: {
					tip: "products",
					name: request.term
				},
				success: function( data ) {
					response( $.map( data.products, function( item ) {
						return {
							label: item.title,
							proddesc: item.description,
							prodprice: item.price,
							value: item.title
						}
					}));
				}
			});
		},
		minLength: 2,
		select: function( event, ui ) {
			$('#prod_description').val(ui.item.proddesc);
			$('#prod_price').val(ui.item.prodprice);
		},
		open: function() {$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );},
		close: function() {$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );}
	});	
	
});