/*global jQuery:false */
(function( $ ) {
"use strict";

function fill_column(data) {
	if(data.objects.length > 0) {
		var obj = data.objects[0];
		var col = window.bika.ar_copy_from_col;
		$("#ar_"+col+"_Contact").val(obj.Contact);
		$("#ar_"+col+"_Contact_uid").val(obj.Contact_uid);
		// $("#ar_"+col+"_CCContact").val(obj.CCContact);
		// $("#ar_"+col+"_CCContact_uid").val(obj.CCContact_uid);
		$("#ar_"+col+"_CCEmails").val(obj.CCEmails);
		$("#ar_"+col+"_Client").val(obj.Client);
		$("#ar_"+col+"_Client_uid").val(obj.Client_uid);
		$("#ar_"+col+"_SubGroup").val(obj.SubGroup);
		$("#ar_"+col+"_SubGroup_uid").val(obj.SubGroup_uid);
		$("#ar_"+col+"_Template").val(obj.Template);
		$("#ar_"+col+"_Template_uid").val(obj.Template_uid);
		$("#ar_"+col+"_Profile").val(obj.Profile);
		$("#ar_"+col+"_Profile_uid").val(obj.Profile_uid);
		$("#ar_"+col+"_SampleType").val(obj.SampleType);
		$("#ar_"+col+"_SampleType_uid").val(obj.SampleType_uid);
		$("#ar_"+col+"_SamplePoint").val(obj.SamplePoint);
		$("#ar_"+col+"_SamplePoint_uid").val(obj.SamplePoint_uid);
		$("#ar_"+col+"_ClientOrderNumber").val(obj.ClientOrderNumber);
		$("#ar_"+col+"_ClientReference").val(obj.ClientReference);
		$("#ar_"+col+"_ClientSampleID").val(obj.ClientSampleID);

		var services = {};
		var specs = {};
		var cat_uid, service_uid;

		for (var i = obj.Analyses.length - 1; i >= 0; i--) {
			var analysis = obj.Analyses[i];
			cat_uid = analysis.CategoryUID;
			service_uid = analysis.ServiceUID;
			if(!(analysis.CategoryUID in services)){
				services[analysis.CategoryUID] = [];
			}
			services[analysis.CategoryUID].push(service_uid);
			specs[service_uid] = analysis.specification;
		}

		for(cat_uid in services){
			if(!services.hasOwnProperty(cat_uid)){ continue; }
			var service_uids = services[cat_uid];
			window.toggleCat("lab", cat_uid, col, [], true);
			for (cat_uid = 0; cat_uid < service_uids.length; cat_uid++) {
				service_uid = service_uids[cat_uid];
				$("[column="+col+"]").filter("#"+service_uid).click();
				var spec = specs[service_uid];
				if(spec){
					$("[name^='ar."+col+".min']").filter("[uid='"+service_uid+"']").val(spec.min);
					$("[name^='ar."+col+".max']").filter("[uid='"+service_uid+"']").val(spec.max);
					$("[name^='ar."+col+".error']").filter("[uid='"+service_uid+"']").val(spec.error);
				}
			}
		}
	}
}

$(document).ready(function(){
	var copy_from = window.location.href.split("copy_from=");
	if(copy_from.length > 1){
		copy_from = copy_from[1].split("&")[0];
		copy_from = copy_from.split(",");
		for (var column = 0; column < copy_from.length; column++) {
			window.bika.ar_copy_from_col = column;
			$.ajaxSetup({async:false});
			window.bika.lims.jsonapi_read({
				catalog_name: "uid_catalog",
				UID: copy_from[column]
			}, fill_column);
			$.ajaxSetup({async:true});
		}
	}

});
}(jQuery));

@@ -1310,16 +1310,25 @@
 ice_uid;
+%0A%09%09var i;
 %0A%0A%09%09for 
@@ -1328,20 +1328,16 @@
 %0A%09%09for (
-var 
 i = obj.
@@ -1840,18 +1840,28 @@
 d, col, 
-%5B%5D
+service_uids
 , true);
@@ -1873,28 +1873,16 @@
 or (
-cat_uid = 0; cat_uid
+i = 0; i
  %3C s
@@ -1901,23 +1901,17 @@
 length; 
-cat_uid
+i
 ++) %7B%0A%09%09
@@ -1943,22 +1943,19 @@
 ids%5B
-cat_uid
+i
 %5D;%0A%09%09%09%09
+// 
 $(%22%5B
@@ -2004,16 +2004,40 @@
 click();
+ // toggleCat does this.
 %0A%09%09%09%09var

Allow ar_add.js to handle service selection when copying ARs.