var contributorDialog;
function initContributorDialog(tableId) {
	contributorDialog = new YAHOO.widget.Dialog("contributorDialog", {fixedcenter: true, visible:false}); 
var handleCancel = function() {
	this.cancel();
}
var handleSubmit = function() {
	with (document.frm_contributorDialog) {
		if (existing_or_new[0].checked)
			if (person_select.value !="") 
				addContributorTableRow(tableId,role.value,role.options[role.selectedIndex].text,person_select.value,person_select.options[person_select.selectedIndex].text);
			else
				return alert(acs_lang_text.no_contributor_selected);
		else
			if (new_user_name.value !="" && new_user_email.value !="") {
				var sUrl = package_url + "/.utils/ajax/contributorCreate?name=" + new_user_name.value + "&email=" + new_user_email.value;
				var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, 
				  { success: function(o) {
							var response = eval(o.responseText);
							if (response.status == 'created')
								addContributorTableRow(tableId,role.value,role.options[role.selectedIndex].text,response.party_id,response.name);
							else
								alert(acs_lang_text.person_already_exists);
						} , 
							failure:function(o) {
							if (o.status == 400) {
								var response =  eval(o.responseText);
								var text = response.error;
							}
							else
								var text = o.status + ' ' + o.statusText;
							alert(acs_lang_text.problem_creating_contributor + ': ' + text)
								} 
					});
			}
			else
				return alert(acs_lang_text.you_have_to_provide_name_and_email);
	}
	contributorDialog.hide();
}
var myButtons = [ { text:acs_lang_text.add, handler:handleSubmit, isDefault:true },
				  { text:acs_lang_text.close, handler:handleCancel } ];
 contributorDialog.cfg.queueProperty("buttons", myButtons);

 contributorDialog.render();
}

function person_search(value) {
	var sUrl = package_url + ".utils/ajax/contributorOptions?search_string=" + value;
	var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, 
	 { success: function(o) {
			 var person_select = document.getElementById('person_select');
			 while (person_select.firstChild) 
				 person_select.removeChild(person_select.firstChild);
			 var options=eval(o.responseText);
			 for(i=0;i<options.length;i++) {
				 person_select.options[i] = new Option(options[i].name,options[i].person_id);
			 }
		 } , 
			 failure:function(o) {
			 alert(acs_lang_text.problem_looking_for_contributors +':'+o.status+' '+o.statusText)
				 } 
	 });
}

function deleteContributorTableRow(TableId, TableRowId) {
	var row = document.getElementById(TableId+'_'+TableRowId);
	row.parentNode.removeChild(row);
}

function addContributorTableRow(TableId,roleId,roleLabel,personId,personName) {
	var counter = ++document.getElementById(TableId + ".count").value;
	var contributorTable = document.getElementById(TableId);
	var newContributorTableRow = contributorTable.insertRow(contributorTable.rows.length);
	var oneRowId = TableId + "_" + counter; 

	newContributorTableRow.id = oneRowId;
	var cell1 = document.createElement("TD");
	var cell2 = document.createElement("TD");
	var cell3 = document.createElement("TD");

				
	cell1.innerHTML = "<input type='hidden' name='"+ oneRowId + ".role' value='" + roleId + "' id='" + oneRowId + ".role'><label for='"+ oneRowId + ".role'>" + roleLabel + "</label>";
	cell2.innerHTML = "<input type='hidden' name='"+ oneRowId +".user' value='" + personId + "' id='" + oneRowId + ".user'><label for='"+ oneRowId + ".user'>" + personName + "</label>";
	cell3.innerHTML = "<a href='#' onclick=\"deleteContributorTableRow('" + TableId + "'," + counter +");return false;\"><img src='/shared/images/Delete16.gif' border='0'></a>";
	
 	newContributorTableRow.appendChild(cell1);
 	newContributorTableRow.appendChild(cell2);
 	newContributorTableRow.appendChild(cell3);
	return true;
}

