jQuery(function($){
	/* Navigation Highlighting */
	
	// determine which page and show relevant menu options
	var pathname = window.location.pathname; // getUrl
	var length = pathname.length; //get length of the url string
	pathname = pathname.substring(8,length) //cut a new string using 2 values above.
	// We now should have the unique identifier as a string which we can use to search the navigation to find the link to the page we are on
	
	var link = $('#main_nav a[href$="'+pathname+'"]') //find the link which links to the page we are on
	link.addClass("selected"); // highlight the link
	
	
	
	/* Table Zebra Striping */
	$("table.stripeMe tr:nth-child(odd)").addClass("odd")
	
});

var iAuthorIndexCount = -1; // -1 indicates this value has not been set.

var publicationTypeIdJournalPaper = 1; // 
var publicationTypeIdConferencePaper = 2; // 
var publicationTypeIdMagazineArticle = 9; //

var publicationStateIdPublished= 4;
var publicationStateIdPublishedApproved= 5;

/**
*  Updates the highest used index value for the author fields.
*/
function updateAuthorIndexCount(a_iInitialMaxIndex)
{
	if(iAuthorIndexCount == -1)
	{		
		// First time this function is called, set iAuthorIndexCount to the value of the m_iHighestUsedIndex.
		iAuthorIndexCount = a_iInitialMaxIndex;
	}
}

/**
* Adds a single author row to additional_author_rows div if iAuthorIndexCount
* is < maxAthors.
*/
function addAuthor(maxAuthors)
{
	if(iAuthorIndexCount < maxAuthors)
	{
		// Store all field values
		// (since setting innerHTML causes values be dropped from existing fields in Div)
		var titles = new Array();
		var firstNames = new Array();
		var lastNames = new Array();
		var initials = new Array();
		var affiliations = new Array();
		var fromDiamond = new Array();
		
		var parents = new Array();
		var parentTitles = new Array();
		var parentFirstNames = new Array();
		var parentLastNames = new Array();
		var parentInitials = new Array();
		var parentAffiliations = new Array();
		var parentEmails = new Array();
		var parentFedIds = new Array();
		for(var i=0; i<iAuthorIndexCount; i++)
		{
			titles[i] = document.getElementById('publicationAuthors[' + i + '].title').value;
			firstNames[i] = document.getElementById('publicationAuthors[' + i + '].firstName').value;
			lastNames[i] = document.getElementById('publicationAuthors[' + i + '].lastName').value;
			initials[i] = document.getElementById('publicationAuthors[' + i + '].initials').value;
			affiliations[i] = document.getElementById('publicationAuthors[' + i + '].affiliations').value;
			fromDiamond[i] = document.getElementById('publicationAuthors[' + i + '].fromDiamond').checked;
			parents[i] = document.getElementById('publicationAuthors[' + i + '].author').value;
			parentTitles[i] = document.getElementById('publicationAuthors[' + i + '].author.title').value;
			parentFirstNames[i] = document.getElementById('publicationAuthors[' + i + '].author.firstName').value;
			parentLastNames[i] = document.getElementById('publicationAuthors[' + i + '].author.lastName').value;
			parentInitials[i] = document.getElementById('publicationAuthors[' + i + '].author.initials').value;
			parentAffiliations[i] = document.getElementById('publicationAuthors[' + i + '].author.affiliations').value;
			parentEmails[i] = document.getElementById('publicationAuthors[' + i + '].author.email').value;
			parentFedIds[i] = document.getElementById('publicationAuthors[' + i + '].author.fedId').value;			
		}
		
		// new input fields for author, template HTML code should exist
		// in hidden div called new_author_HTML
		document.getElementById("additional_author_rows").innerHTML += document.getElementById("new_author_HTML").innerHTML.replace(/__AuthorIndex__/g, "[" + iAuthorIndexCount + "]");


		initAuthorFieldForAC(iAuthorIndexCount);

	
		// Replace fields
		for(var i=0; i<iAuthorIndexCount; i++)
		{
			document.getElementById('publicationAuthors[' + i + '].title').value = titles[i];
			document.getElementById('publicationAuthors[' + i + '].firstName').value = firstNames[i];
			document.getElementById('publicationAuthors[' + i + '].lastName').value = lastNames[i];
			document.getElementById('publicationAuthors[' + i + '].initials').value = initials[i];
			document.getElementById('publicationAuthors[' + i + '].affiliations').value = affiliations[i];
			document.getElementById('publicationAuthors[' + i + '].fromDiamond').checked = fromDiamond[i];
			document.getElementById('publicationAuthors[' + i + '].author').value = parents[i];
			document.getElementById('publicationAuthors[' + i + '].author.title').value = parentTitles[i];
			document.getElementById('publicationAuthors[' + i + '].author.firstName').value = parentFirstNames[i];
			document.getElementById('publicationAuthors[' + i + '].author.lastName').value = parentLastNames[i];
			document.getElementById('publicationAuthors[' + i + '].author.initials').value = parentInitials[i];
			document.getElementById('publicationAuthors[' + i + '].author.affiliations').value = parentAffiliations[i];
			document.getElementById('publicationAuthors[' + i + '].author.email').value = parentEmails[i];
			document.getElementById('publicationAuthors[' + i + '].author.fedId').value = parentFedIds[i];
		}
		
		iAuthorIndexCount++;
		document.getElementById('numberOfAuthors').value = iAuthorIndexCount;
	}
	
	// Enforce author limit
	if(iAuthorIndexCount >= maxAuthors)
	{
		document.getElementById("addauth").innerHTML = "<strong>Maximum of "+maxAuthors+" authors has been reached.</strong>";
	}
}

function toggleSyncrotronFields()
{
	if(document.getElementById('usesSyncrotronDataYes') && document.getElementById('usesSyncrotronDataYes').checked)
	{					
		if(document.getElementById('fromDiamondQuestion'))
		{
			document.getElementById('fromDiamondQuestion').style.display = 'block';									
		}
	}
	else
	{
		if(document.getElementById('fromDiamondQuestion'))
		{
			document.getElementById('fromDiamondQuestion').style.display = 'none';
		}
	}
	
	
	if(document.getElementById('fromDiamondYes') && document.getElementById('fromDiamondYes').checked)
	{	
		if(document.getElementById('beamlines'))
		{
			document.getElementById('beamlines').style.display = 'block';			
		}
	}
	else
	{	
		if(document.getElementById('beamlines'))
		{		
			document.getElementById('beamlines').style.display = 'none';
		}
	}	
	
	if(document.getElementById('fromDiamondYes') && document.getElementById('fromDiamondYes').checked)
	{	
		if(document.getElementById('otherSyncrotronsQuestion'))
		{
			document.getElementById('otherSyncrotronsQuestion').style.display = 'block';			
		}
	}
	else
	{	
		if(document.getElementById('otherSyncrotronsQuestion'))
		{		
			document.getElementById('otherSyncrotronsQuestion').style.display = 'none';
		}
	}	
	
	
	if(document.getElementById('usesSyncrotronDataYes') && !document.getElementById('usesSyncrotronDataYes').checked)
	{	
		if(document.getElementById('technical_disciplines'))
		{				
			document.getElementById('technical_disciplines').style.display = 'block';									
		}
	}
	else
	{
		if(document.getElementById('technical_disciplines'))
		{
			document.getElementById('technical_disciplines').style.display = 'none';
		}
	}			
		
	if(document.getElementById('usesSyncrotronDataYes') && document.getElementById('usesSyncrotronDataYes').checked && document.getElementById('fromDiamondYes') && !document.getElementById('fromDiamondYes').checked)
	{	
		if(document.getElementById('otherSyncrotronQuestion'))
		{				
			document.getElementById('otherSyncrotronQuestion').style.display = 'block';									
		}
	}
	else
	{
		if(document.getElementById('otherSyncrotronQuestion'))
		{
			document.getElementById('otherSyncrotronQuestion').style.display = 'none';
		}
	}			
}

/**
* This is called when publication type is selected. If the publication type is journal
* then we make sure the journal title field is enabled and mark it as required.
*/
function toggleJournalFields(selPublicationTypeSelect, fldJournalFieldElemIds, divJournalTitleRequiredId,fldConferenceFieldElemIds,divConferenceTitleRequiredId,fldMagazineFieldElemIds,divMagazineTitleRequiredId)
{
	if(selPublicationTypeSelect.value==publicationTypeIdJournalPaper)
	{
		// => PublicationType = Journal
		disabled = '';
		requiredLabelToggle(divJournalTitleRequiredId, true);
		document.getElementById('journal_options').style.display = 'block';
		document.getElementById('journal_options').style.position = 'relative';
		document.getElementById('journal_options').style.left = '0';
	}
	else
	{
		disabled = 'disabled';
		requiredLabelToggle(divJournalTitleRequiredId, false);
		document.getElementById('journal_options').style.display = 'none';
		document.getElementById('journal_options').style.position = 'absolute';
		document.getElementById('journal_options').style.left = '-9999em';
	}
	
	for(var x=0; x<fldJournalFieldElemIds.length; x++)
	{
		elem = document.getElementById(fldJournalFieldElemIds[x]);
		elem.disabled = disabled;
		elem.value = "";		
	}
	
	if(selPublicationTypeSelect.value==publicationTypeIdConferencePaper)
	{
		// => PublicationType = Conference Paper
		disabled = '';
		requiredLabelToggle(divConferenceTitleRequiredId, true);
		document.getElementById('conference_options').style.display = 'block';
		document.getElementById('conference_options').style.position = 'relative';
		document.getElementById('conference_options').style.left = '0';
	}
	else
	{
		disabled = 'disabled';
		requiredLabelToggle(divConferenceTitleRequiredId, false);
		document.getElementById('conference_options').style.display = 'none';
		document.getElementById('conference_options').style.position = 'absolute';
		document.getElementById('conference_options').style.left = '-9999em';
	}
	
	for(var x=0; x<fldConferenceFieldElemIds.length; x++)
	{
		elem = document.getElementById(fldConferenceFieldElemIds[x]);
		elem.disabled = disabled;
		if(fldConferenceFieldElemIds[x] == 'peerReviewedYes')
		{
			elem.checked = false;
		}
		else if (fldConferenceFieldElemIds[x] == 'peerReviewedNo')
		{
			elem.checked = true;
		}
		else
		{
			elem.value = "";		
		}
	}
	
	
	if(selPublicationTypeSelect.value==publicationTypeIdMagazineArticle)
	{
		// => PublicationType = Magazine Article
		disabled = '';
		requiredLabelToggle(divMagazineTitleRequiredId, true);
		document.getElementById('magazine_options').style.display = 'block';
		document.getElementById('magazine_options').style.position = 'relative';
		document.getElementById('magazine_options').style.left = '0';
	}
	else
	{
		disabled = 'disabled';
		requiredLabelToggle(divMagazineTitleRequiredId, false);
		document.getElementById('magazine_options').style.display = 'none';
		document.getElementById('magazine_options').style.position = 'absolute';
		document.getElementById('magazine_options').style.left = '-9999em';
	}
	
	for(var x=0; x<fldMagazineFieldElemIds.length; x++)
	{
		elem = document.getElementById(fldMagazineFieldElemIds[x]);
		elem.disabled = disabled;		
		elem.value = "";	
	}
}


/**
* This is called when publication state is selected. If the publication type published or published approved
* then we make sure the date published fields are enabled and mark them as required.
*/
function toggleStateFields(selPublicationStateSelect, fldStateFieldsElemIds, divPublishDateRequiredId)
{
	if(selPublicationStateSelect.value==publicationStateIdPublished || selPublicationStateSelect.value==publicationStateIdPublishedApproved)
	{
		// => PublicationType = Journal
		disabled = false;
		requiredLabelToggle(divPublishDateRequiredId, true);
	}
	else
	{
		disabled = true;
		requiredLabelToggle(divPublishDateRequiredId, false);
	}
	
	for(var y=0; y<fldStateFieldsElemIds.length; y++)
	{
		elem = document.getElementById(fldStateFieldsElemIds[y]);
		elem.disabled = disabled;
		if(disabled)
		{
			elem.value = "";		
		}
	}
}

/**
* This function marks fields as required or unrequired fields by
* printing or removing a * from divRequiredDivId if bRequired is true or false.
*/
function requiredLabelToggle(divRequiredDivId, bRequired)
{
	divRequiredDiv = document.getElementById(divRequiredDivId);
	if(bRequired)
	{
		divRequiredDiv.innerHTML = "*";
	}
	else
	{
		divRequiredDiv.innerHTML = "";
	}
}

DOMAssistant.DOMReady(function() {
	if ($$('journal_options')) {
		$$('journal_options').setStyle({'display': 'none', 'position': 'absolute', 'left': '-9999em'});
	}
	
	if ($$('conference_options')) {
		$$('conference_options').setStyle({'display': 'none', 'position': 'absolute', 'left': '-9999em'});
	}
	
	if ($$('magazine_options')) {
		$$('magazine_options').setStyle({'display': 'none', 'position': 'absolute', 'left': '-9999em'});
	}
	
	// create hide/show for search form on search results page
  	if ($$('searchFormWrapper')) {
	  	$$('searchFormWrapper').addClass('hide');
	  	
	  	var searchLink = document.createElement('a');
	  	searchLink.id = "searchLink";
	  	var headingText = $$('formHeading').firstChild.nodeValue;
	  	var linkText = document.createTextNode(headingText);
	  	searchLink.appendChild(linkText);
	  	$$('formHeading').innerHTML = '';
	  	$$('formHeading').appendChild(searchLink);
	  	
	  	$$('searchLink').addEvent('click', function () {
	  		if ($$('searchFormWrapper').hasClass('hide')) {
	  			$$('searchFormWrapper').removeClass('hide').addClass('show');
	  			$$('searchLink').setStyle({'background': '#F2F2F2 url(images/up_arrow.gif) no-repeat center right'});
	  			$$('searchLink').firstChild.nodeValue = 'Hide Search Form';
	  		} else {
	  			$$('searchFormWrapper').removeClass('show').addClass('hide');
	  			$$('searchLink').setStyle({'background': '#F2F2F2 url(images/down_arrow.gif) no-repeat center right'});
	  			$$('searchLink').firstChild.nodeValue = headingText;
	  		}
	  	});
	  	
	}
	if ($$('publicationType')) {
		if ($$('publicationType').value == publicationTypeIdJournalPaper) {
			$$('journal_options').setStyle({'display': 'block', 'position': 'relative', 'left': 0});
		}
	}
	
	if ($$('publicationType')) {
		if ($$('publicationType').value == publicationTypeIdConferencePaper) {
			$$('conference_options').setStyle({'display': 'block', 'position': 'relative', 'left': 0});
		}
	}
	
	
	if ($$('publicationType')) {
		if ($$('publicationType').value == publicationTypeIdMagazineArticle) {
			$$('magazine_options').setStyle({'display': 'block', 'position': 'relative', 'left': 0});
		}
	}
	
	toggleSyncrotronFields();
	
  	
});

DOMAssistant.DOMLoad.setErrorHandling(function (e) {
//alert(e);
}); 

function setThisIsMe(a_sFedIdInputFieldId, a_sFedId, thisid)
{
	
	$('.isthismewrapper input.radio').each(function () {
		if (thisid.indexOf('userIsAuthorInList') != -1) {
			if ($(this).id.indexOf('userIsNotAuthorInList') != -1) {
				$(this).checked = 'checked';
			}
			if ($(this).id.indexOf('userIsAuthorInList') != -1) {
				$(this).checked = '';
			}
			$$(thisid).checked = 'checked';
		}
	});
		

	// First remove fedId from all authors
	for(var i=0; i<iAuthorIndexCount; i++)
	{
		document.getElementById('authors[' + i + '].fedId').value = "";
	}
	
	// Set a_sFedId for the selected author, if an author is selected
	if(a_sFedIdInputFieldId != null)
	{
		document.getElementById(a_sFedIdInputFieldId).value = a_sFedId;
	}
}
function checkmoveup(elm) {
	var idArr = elm.id.split('[');
	var authorIndex = idArr[1].replace(']', '');
	if (document.getElementById('author_row[' + (authorIndex -1 ) + ']')) {
		return trues;
	}
	else {
		alert('Cannot move author up');
		return false;
	}
}

function moveup(elm) {
	var idArr = elm.id.split('[');
	var authorIndex = idArr[1].replace(']', '');
	if (document.getElementById('author_row[' + (authorIndex -1 ) + ']')) {
		moveAuthor(authorIndex, (authorIndex -1));
	}
	else {
		alert('Cannot move author up');
	}
}

function checkmovedown(elm) {
	
	var idArr = elm.id.split('[');
	var authorIndex = idArr[1].replace(']', '');
	var tempIndex = authorIndex;
	var targetIndex = parseFloat(tempIndex) + parseFloat(1);
	if (document.getElementById('author_row[' + targetIndex + ']')) {
		return true;
	}
	else {
		alert('Cannot move author down');
		return false;
	}	
}

function movedown(elm) {
	
	var idArr = elm.id.split('[');
	var authorIndex = idArr[1].replace(']', '');
	var tempIndex = authorIndex;
	var targetIndex = parseFloat(tempIndex) + parseFloat(1);
	if (document.getElementById('author_row[' + targetIndex + ']')) {
		moveAuthor(authorIndex, targetIndex);
	}
	else {
		alert('Cannot move author down');
	}	
}

function moveAuthor(currIndex, targetIndex) {
	var currAuthorIds = new Array();
	var currAuthorVals = new Array();
	var targetAuthorIds = new Array();
	var targetAuthorVals = new Array();
	
	currAuthorIds[0] = "publicationAuthors[" + currIndex + "].title";
	currAuthorIds[1] = "publicationAuthors[" + currIndex + "].firstName";
	currAuthorIds[2] = "publicationAuthors[" + currIndex + "].lastName";
	currAuthorIds[3] = "publicationAuthors[" + currIndex + "].initials";
	currAuthorIds[4] = "publicationAuthors[" + currIndex + "].affiliations";
	currAuthorIds[5] = "publicationAuthors[" + currIndex + "].author";
	currAuthorIds[6] = "publicationAuthors[" + currIndex + "].author.title";
	currAuthorIds[7] = "publicationAuthors[" + currIndex + "].author.firstName";
	currAuthorIds[8] = "publicationAuthors[" + currIndex + "].author.lastName";
	currAuthorIds[9] = "publicationAuthors[" + currIndex + "].author.initials";
	currAuthorIds[10] = "publicationAuthors[" + currIndex + "].author.affiliations";
	currAuthorIds[11] = "publicationAuthors[" + currIndex + "].author.email";
	currAuthorIds[12] = "publicationAuthors[" + currIndex + "].author.fedId";
	
		
	currAuthorVals[0] =	document.getElementById('publicationAuthors[' + currIndex + '].title').value;
	currAuthorVals[1] =	document.getElementById('publicationAuthors[' + currIndex + '].firstName').value;
	currAuthorVals[2] =	document.getElementById('publicationAuthors[' + currIndex + '].lastName').value;
	currAuthorVals[3] =	document.getElementById('publicationAuthors[' + currIndex + '].initials').value;
	currAuthorVals[4] =	document.getElementById('publicationAuthors[' + currIndex + '].affiliations').value;
	currAuthorVals[5] =	document.getElementById('publicationAuthors[' + currIndex + '].author').value;
	currAuthorVals[6] =	document.getElementById('publicationAuthors[' + currIndex + '].author.title').value;
	currAuthorVals[7] =	document.getElementById('publicationAuthors[' + currIndex + '].author.firstName').value;
	currAuthorVals[8] =	document.getElementById('publicationAuthors[' + currIndex + '].author.lastName').value;
	currAuthorVals[9] =	document.getElementById('publicationAuthors[' + currIndex + '].author.initials').value;
	currAuthorVals[10] =	document.getElementById('publicationAuthors[' + currIndex + '].author.affiliations').value;
	currAuthorVals[11] =	document.getElementById('publicationAuthors[' + currIndex + '].author.email').value;
	currAuthorVals[12] =	document.getElementById('publicationAuthors[' + currIndex + '].author.fedId').value;			

	targetAuthorIds[0] = "publicationAuthors[" + targetIndex + "].title";
	targetAuthorIds[1] = "publicationAuthors[" + targetIndex + "].firstName";
	targetAuthorIds[2] = "publicationAuthors[" + targetIndex + "].lastName";
	targetAuthorIds[3] = "publicationAuthors[" + targetIndex + "].initials";
	targetAuthorIds[4] = "publicationAuthors[" + targetIndex + "].affiliations";
	targetAuthorIds[5] = "publicationAuthors[" + targetIndex + "].author";
	targetAuthorIds[6] = "publicationAuthors[" + targetIndex + "].author.title";
	targetAuthorIds[7] = "publicationAuthors[" + targetIndex + "].author.firstName";
	targetAuthorIds[8] = "publicationAuthors[" + targetIndex + "].author.lastName";
	targetAuthorIds[9] = "publicationAuthors[" + targetIndex + "].author.initials";
	targetAuthorIds[10] = "publicationAuthors[" + targetIndex + "].author.affiliations";
	targetAuthorIds[11] = "publicationAuthors[" + targetIndex + "].author.email";
	targetAuthorIds[12] = "publicationAuthors[" + targetIndex + "].author.fedId";
		
	targetAuthorVals[0] =	document.getElementById('publicationAuthors[' + targetIndex + '].title').value;
	targetAuthorVals[1] =	document.getElementById('publicationAuthors[' + targetIndex + '].firstName').value;
	targetAuthorVals[2] =	document.getElementById('publicationAuthors[' + targetIndex + '].lastName').value;
	targetAuthorVals[3] =	document.getElementById('publicationAuthors[' + targetIndex + '].initials').value;
	targetAuthorVals[4] =	document.getElementById('publicationAuthors[' + targetIndex + '].affiliations').value;
	targetAuthorVals[5] =	document.getElementById('publicationAuthors[' + targetIndex + '].author').value;
	targetAuthorVals[6] =	document.getElementById('publicationAuthors[' + targetIndex + '].author.title').value;
	targetAuthorVals[7] =	document.getElementById('publicationAuthors[' + targetIndex + '].author.firstName').value;
	targetAuthorVals[8] =	document.getElementById('publicationAuthors[' + targetIndex + '].author.lastName').value;
	targetAuthorVals[9] =	document.getElementById('publicationAuthors[' + targetIndex + '].author.initials').value;
	targetAuthorVals[10] =	document.getElementById('publicationAuthors[' + targetIndex + '].author.affiliations').value;
	targetAuthorVals[11] =	document.getElementById('publicationAuthors[' + targetIndex + '].author.email').value;
	targetAuthorVals[12] =	document.getElementById('publicationAuthors[' + targetIndex + '].author.fedId').value;			
	
	for (var x=0; x<currAuthorIds.length; x++) {
		document.getElementById(targetAuthorIds[x]).value = currAuthorVals[x];
		document.getElementById(currAuthorIds[x]).value = targetAuthorVals[x];
	}	
	
	var fromDiamond = document.getElementById('publicationAuthors[' + currIndex + '].fromDiamond').checked;
	document.getElementById('publicationAuthors[' + currIndex + '].fromDiamond').checked = document.getElementById('publicationAuthors[' + targetIndex + '].fromDiamond').checked;
	document.getElementById('publicationAuthors[' + targetIndex + '].fromDiamond').checked = fromDiamond;
}


function clearAuthor(elm) 
{
	var idArr = elm.id.split('[');
	var targetIndex = idArr[1].replace(']', '');
	
	document.getElementById('publicationAuthors[' + targetIndex + '].title').value = "";
	document.getElementById('publicationAuthors[' + targetIndex + '].firstName').value = "";
	document.getElementById('publicationAuthors[' + targetIndex + '].lastName').value = "";
	document.getElementById('publicationAuthors[' + targetIndex + '].initials').value = "";
	document.getElementById('publicationAuthors[' + targetIndex + '].affiliations').value = "";
	document.getElementById('publicationAuthors[' + targetIndex + '].fromDiamond').checked = "";
	document.getElementById('publicationAuthors[' + targetIndex + '].author').value = "";
	document.getElementById('publicationAuthors[' + targetIndex + '].author.title').value = "";
	document.getElementById('publicationAuthors[' + targetIndex + '].author.firstName').value = "";
	document.getElementById('publicationAuthors[' + targetIndex + '].author.lastName').value = "";
	document.getElementById('publicationAuthors[' + targetIndex + '].author.initials').value = "";
	document.getElementById('publicationAuthors[' + targetIndex + '].author.affiliations').value = "";
	document.getElementById('publicationAuthors[' + targetIndex + '].author.email').value = "";
	document.getElementById('publicationAuthors[' + targetIndex + '].author.fedId').value = "";
	
}

function deleteAuthor(elm) {
	var idArr = elm.id.split('[');
	var authorIndex = idArr[1].replace(']', '');
	for (var z=authorIndex; z<iAuthorIndexCount; z++) {
		var idx = z;
		var nextIdx = parseFloat(z) + parseFloat(1);
		if (document.getElementById('author_row[' + nextIdx + ']')) {
			moveAuthor(idx, nextIdx);
		}
		
	}
	var rowToRemove = document.getElementById('author_row[' + (iAuthorIndexCount-1) + ']');
	if ( rowToRemove.hasChildNodes() )
	{
	    while ( rowToRemove.childNodes.length >= 1 )
	    {
	        rowToRemove.removeChild( rowToRemove.firstChild );       
	    } 
	}
	
	rowToRemove.parentNode.removeChild(rowToRemove);
	
	iAuthorIndexCount--;
	document.getElementById('numberOfAuthors').value = iAuthorIndexCount;
}

function fromDiamondToggle(elm) 
{
	var idArr = elm.id.split('[');
	var targetIndex = idArr[1].replace('].fromDiamond', '');
	if(document.getElementById('publicationAuthors[' + targetIndex + '].fromDiamond').checked)
	{
		if(document.getElementById('publicationAuthors[' + targetIndex + '].affiliations').value.indexOf('Diamond Light Source') == -1)
		{
			if(document.getElementById('publicationAuthors[' + targetIndex + '].affiliations').value)
			{
				document.getElementById('publicationAuthors[' + targetIndex + '].affiliations').value += ", Diamond Light Source";
			}
			else
			{
			document.getElementById('publicationAuthors[' + targetIndex + '].affiliations').value = "Diamond Light Source";
			}
		}
	}
}

function popup(url) {
	var newwindow = window.open(url,'Help','height=500,width=540');
	
	/*if (window.focus) {
		newwindow.focus();  // Breaks IE
	}*/
	
	return false;
}


/*
 * Toggles all select boxes that are children of the given target element.
 *  
 * @param targetId
 */
function toggleSelectAll(targetId) {
	var target = $(targetId); 
	
	if (target.hasClass('selectall')) {
		$('input', target).each(function() {
			this.checked = false;
		});
		target.removeClass('selectall');
	} else {
		$('input', target).each(function() {
			this.checked = true;
		});
		target.addClass('selectall');
	}
};

