function splitContributors(contextPath, fieldName, fieldInput, labelButtonAdd, bshare, bintord)
{
	var values = j(fieldName).val();	
	j.ajax({url: contextPath+"/json/contributors", 
		dataType: "json", 
		async: false,
		contentType: "application/x-www-form-urlencoded;charset=UTF-8", 
		data: ({allcontributors: values}), 
		success: function(info) {
			if (info == null || info.allcontributors == '')
				return;
			j("#"+fieldInput.replace("dc_contributor_","dc_description_all")+"s_id").val(info.allcontributors);
			j("#"+fieldInput.replace("dc_contributor_","dc_description_all")+"s_id").change();
			var contributors = info.shareAuthorInfos;
			if (contributors != null && contributors.size() > 0)
			{
				j("#sharediv_"+fieldInput).remove("table#sharetable_"+fieldInput+" tbody"); 
				var rows =  j("#sharediv_"+fieldInput).find("table#sharetable_"+fieldInput);
				var html = "";
				for (var i = 0; i < contributors.size(); i++)
				{
					html += "<tr>";
					html += "<td>";
					var share = contributors[i].share;
					
					j.each((contributors[i].authors),function(index,value){
						if(contributors[i].authors.length>1) {													
						var fullname = 	value.fullname;
						var authority = value.authority;						
						if(index==0) {
							html += "<input type=\"hidden\" align=\"left\" size=\"100\" id=\""+fieldInput+"_"+(i+1)+"\" name=\""+fieldInput+"_"+(i+1)+"\" value=\""+ fullname +"\"/>";
							html += "<select style=\"width:629px\" id=\"select_"+fieldInput+"_"+(i+1)+"\" name=\"select_"+fieldInput+"_"+(i+1)+"\" onchange=\"fillContributor('"+fieldInput+"',"+(i+1)+",this.value)\">";
						}
						html += "<option value=\""+ index +"_"+ fullname +"_"+ authority +"_"+ share +"_"+(i+1)+"\">";
						html += value.display.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;");
						html += "</option>";
						if(index==contributors[i].length-1) {
							html += "</select>";
						}
						
						}
						else {
							html += "<input type=\"text\" readonly=\"readonly\" align=\"left\" size=\"100\" id=\""+fieldInput+"_"+(i+1)+"\" name=\""+fieldInput+"_"+(i+1)+"\" value=\""+contributors[i].authors[0].fullname+"\"/>";		
						}
						
					});

					html += "</td>";
					html += "<td>";					
					html += "<input type=\"hidden\" align=\"left\" size=\"10\" id=\""+fieldInput+"_authority_"+(i+1)+"\" name=\""+fieldInput+"_authority_"+(i+1)+"\" value=\""; 
					if((contributors[i].authors[0].authority)==null){
						html += "\">";		
					}
					else {
						html += contributors[i].authors[0].authority;
						html += "\">";
					}
								
					html += "</td>";
					
					if (bshare)
					{
						html += "<td>";					
						html += "<input type=\"text\" align=\"left\" size=\"3\" id=\""+fieldInput+"_share_"+(i+1)+"\" name=\""+fieldInput+"_share_"+(i+1)+"\" value=\"";
						if((contributors[i].share)==null){
							html += 0;
							html += "\">";		
						}
						else {
							html += contributors[i].share;
							html += "\">";
						}
						html += "</td>";
					}
					
					if (bintord)
					{
						html += "<td>";					
						html += "<input readonly=\"readonly\" type=\"text\" align=\"left\" size=\"3\" id=\""+fieldInput+"_intorder_"+(i+1)+"\" name=\""+fieldInput+"_intorder_"+(i+1)+"\" value=\""+(i+1)+"\">";					
						html += "</td>";
					}
					
					if(i==0) {
						html += "<td rowspan=\""+contributors.size()+"\">";					
						html += "<input type=\"submit\" name=\"submit_"+fieldInput+"_add\" value=\""+ labelButtonAdd +"\"/>";					
						html += "</td>";
					}
					html += "</tr>";
				}
				j("#sharetbody_"+fieldInput).html(html);
				j("#sharetbody_"+fieldInput).find('input[type="text"]').keydown(function(){return false;});
				//j("#sharetable").addClass("ui-state-highlight");
				j("#sharetable_"+fieldInput).effect('highlight',null,5000);
				if (j('#msgSplitFeature_'+fieldInput).size() == 0)
				{
					var alertMsg = j('<div class="ui-widget" id="msgSplitFeature_'+fieldInput+'"/>');
					alertMsg.html(
							j('<div class="ui-state-highlight ui-corner-all" style="margin-top: 20px; padding: 0 .7em;" />').
							html(
									j('<p/>').html('<span class="ui-icon ui-icon-info" style="float: left; margin-right: .3em;"></span>'+SUROA_I18N_SUBMIT_SPLITCONTRIBUTORS)
							)
						).insertBefore(j("#sharetable_"+fieldInput));
				}
				j.scrollTo((j('#msgSplitFeature_'+fieldInput)));
			}
		}
	});
}

function fillContributor(fieldInput,fieldInputIndex,valueOption) {

	var substr = valueOption.split('_');
	var indexOption = substr[0];
	var fullname = substr[1];
	var authority = substr[2]=="undefined"?"":substr[2];
	var share = substr[3]=="undefined"?0:substr[3];
	var intorder = substr[4]=="undefined"?"":substr[4];
	var dc_contributor_author = "#"+fieldInput+"_"+fieldInputIndex;
	j(dc_contributor_author).val(fullname);	
	var dc_contributor_author_authority = "#"+fieldInput+"_authority_"+fieldInputIndex;
	j(dc_contributor_author_authority).val(authority);
	var dc_contributor_author_share = "#"+fieldInput+"_share_"+fieldInputIndex;
	j(fieldInput+"_share_"+fieldInputIndex).val(share);
	var dc_contributor_author_intorder = "#"+fieldInput+"_intorder_"+fieldInputIndex;
	j(dc_contributor_author_intorder).val(intorder);
	
}


function groupContributors(fieldName) {
	
	var results = "";
	var rows = j("#sharediv_"+fieldName).find("table#sharetable_"+fieldName+" tbody tr");
	
	j.each(rows,function(index,value){
				
		var count = j(rows).find("td:first input[value!='']").length;
		var input = j(this).find("td:first input");
		
		if(input!=null && input.length>0) {
		if(j(input).val().length>0) {
			
			myregexp = new RegExp("^[\\w|\\W]+,\\s\\w");
			var mymatch = myregexp.exec(j(input).val());
			if(mymatch==null) {
				results += j(input).val();
			}
			else {		
				//double substitutions
				results += mymatch[0].toLowerCase().replace(/^(.)|\s(.)/g, function($1){ return $1.toUpperCase( ); }).replace(/-(.)|'(.)/g, function($1){ return $1.toUpperCase( ); });	
			}
			if(index!=count-1) {
				results += "; ";		
			}
		}
		}
	});
	j(fieldName).val(results);
	j(fieldName).change();
}

function recalculateShare(fieldName) {
	var rows = j("#sharediv_"+fieldName).find("table#sharetable_"+fieldName+" tbody tr");
	var count = j(rows).find("td:first input[value!='']").length;
	
	var data = j(rows).find("td:eq(2) input");
	var diffShare = 0;
	var intShare = Math.floor(100/count);
	diffShare = 100-(count*intShare);
	
	j.each(data,function(index,value) {
		var firstInput = j(value).parent().parent().children().get(0).firstChild;
		if(j(firstInput).val().length>0) {
			j(value).val(intShare);					
			if(index<diffShare) {			
				j(value).val(intShare + 1);	
			}
		}
		else {
			j(value).val("");
		}
	});
	
}

function recalculateIntOrder(fieldName) {
	var rows = j("#sharediv_"+fieldName).find("table#sharetable_"+fieldName+" tbody tr");
	var data = j(rows).find("td:eq(3) input");
	var counter = 1;
	j.each(data,function(index,value){		
		var firstInput = j(value).parent().parent().children().get(0).firstChild;
		if(j(firstInput).val().length>0) {			
			j(value).val(counter);
			counter++;
		}					
		else {
			j(value).val("");
		}
	});
}
