
/////////////////////// Top of Page Attestations ////////////////////////

function createTopDiv(){
	$('GFABox').checked=false;
	$('RoleBox').checked=false;
	$('AttestationBox').checked=false;
	$('ConflictBox').checked=false;
}

function toggleGFADiv() {
	$('AttestGuideForAuthorsDiv').style.display = "none";
	$('AttestRoleDiv').style.display = "block";
}

function toggleRoleDiv() {
	$('AttestRoleDiv').style.display = "none";
	$('AttestAttestationDiv').style.display = "block";
}

function toggleAttestationDiv() {
	$('AttestAttestationDiv').style.display = "none";
	$('AttestConflictDiv').style.display = "block";
}

function toggleConflictDiv() {
	$('InitialCheckBoxesDiv').style.display = "none";
	$('TitlePageFormDiv').style.display = "block";
}

////////////////////////////////// Define MS Types and Events /////////////////////////////

function defineMSTypes() {
	var MSTypeHTML ="";
    MSTypeArray.each(function(MSType){
	    name = MSType.replace(/ /g, "_").replace(/\(/g, "").replace(/\)/g, "");
		MSTypeHTML += '<div id="'+name+'Wrap" class="MSTypeWrap">';
		MSTypeHTML += '<input style="margin:3px 2px 0 1px;" type="checkbox" name="'+name+'" id="'+name+'CheckBox" onclick="toggleMSType(\''+name+'\')">';
		MSTypeHTML += '&nbsp;'+MSType+'</div>';

		});
	$('MSTypeDiv').innerHTML=MSTypeHTML;
	Uncheck();
}

function toggleMSType(thisMSType){
 		var MSTypeText = thisMSType.replace(/_/g, " ");
		if ($(thisMSType + 'CheckBox').checked) {
		MSTypeArray.each(function(MSType){
		    name = MSType.replace(/ /g, "_").replace(/\(/g, "").replace(/\)/g, "");
			if (thisMSType != name) {
				$(name + 'Wrap').style.display = "none";
			}
		});

		$("MSTypeHeader").innerHTML="<b>&nbsp;&nbsp;Unclick " + MSTypeText +" to restore the list of manuscript types.</b>";

		$(thisMSType + 'Div').style.display = "block";
		if (thisMSType == "Research_Report" ||
		thisMSType == "Brief_Report" ||
		thisMSType == "Technical_Communication") {
			toggleAttestations("block");
			if (thisMSType != "Technical_Communication") 
				$('StatisticalQuestionsDiv').style.display = "block";
		}
		if (thisMSType == "Case_Report" ||
		thisMSType == "Echo_Rounds" ||
		thisMSType == "Letter_to_the_Editor") {
			$('ConsentQuestionsDiv').style.display = "block";
		}
		if (thisMSType == "Case_Report") {
			$('CaseReportQuestionsDiv').style.display = "block";
		}
	}
	else {
        $("MSTypeHeader").innerHTML="<b>&nbsp;&nbsp;Please indicate the kind of manuscript you are submitting:</b>";
		MSTypeArray.each(function(MSType){
		    name = MSType.replace(/ /g, "_").replace(/\(/g, "").replace(/\)/g, "");
			$(name + 'Wrap').style.display = "block";
		});
		$(thisMSType + 'Div').style.display = "none";
		toggleAttestations("none");
		$('StatisticalQuestionsDiv').style.display="none";
		$('ConsentQuestionsDiv').style.display = "none";
		$('CaseReportQuestionsDiv').style.display = "none";
		Uncheck();
	}
	CreateTitlePage();
}

function Uncheck(){
	$("ConsentNecessaryRadioYes").checked = false
	$("ConsentNecessaryRadioNo").checked = false
	$("ConsentTypeRadioPatient").checked = false
	$("ConsentTypeRadioFamily").checked = false
	$("ConsentTypeRadioIRB").checked = false
	$("PatientConsentIncludedRadioYes").checked = false
	$("PatientConsentIncludedRadioNo").checked = false
	$("FamilyConsentIncludedRadioYes").checked = false
	$("FamilyConsentIncludedRadioNo").checked = false
	$("IRBConsentIncludedRadioYes").checked = false
	$("IRBConsentIncludedRadioNo").checked = false
	$("TrulyExceptionalRadioYes").checked = false
	$("TrulyExceptionalRadioNo").checked = false
	$("ClaimEfficacyRadioYes").checked = false
	$("ClaimEfficacyRadioNo").checked = false
	$("ProspectiveRadioYes").checked = false
	$("ProspectiveRadioNo").checked = false
	$("ConsortIncludedRadioYes").checked = false
	$("ConsortIncludedRadioNo").checked = false
	$("ObservationalRadioYes").checked = false
	$("ObservationalRadioNo").checked = false
	$("Observational1RadioYes").checked = false
	$("Observational1RadioNo").checked = false
	$("Observational2RadioYes").checked = false
	$("Observational2RadioNo").checked = false
	$("Observational3RadioYes").checked = false
	$("Observational3RadioNo").checked = false
	$("Observational4RadioYes").checked = false
	$("Observational4RadioNo").checked = false
	$("Observational5RadioYes").checked = false
	$("Observational5RadioNo").checked = false
	$("Observational6RadioYes").checked = false
	$("Observational6RadioNo").checked = false
	$("Observational7RadioYes").checked = false
	$("Observational7RadioNo").checked = false
	$("Observational8RadioYes").checked = false
	$("Observational8RadioNo").checked = false
	$("MetaRegressionRadioYes").checked = false
	$("MetaRegressionRadioNo").checked = false
	$("MetaRegression1RadioYes").checked = false
	$("MetaRegression1RadioNo").checked = false
	$("MetaAnalysisRadioYes").checked = false
	$("MetaAnalysisRadioNo").checked = false
	$("CostRadioYes").checked = false
	$("CostRadioNo").checked = false
	$("PropensityRadioYes").checked = false
	$("PropensityRadioNo").checked = false
	$("NegativeRadioYes").checked = false
	$("NegativeRadioNo").checked = false
	$("MixedEffectsRadioYes").checked = false
	$("MixedEffectsRadioNo").checked = false

	$("ConsentQuestionsDiv").style.display="none";
	$("explainIncludeConsentDiv").style.display="none";
	$("ConsentTypeDiv").style.display="none";
	$("PatientConsentsDiv").style.display="none";
	$("NoPatientConsentDiv").style.display="none";
	$("FamilyConsentsDiv").style.display="none";
	$("NoFamilyConsentDiv").style.display="none";
	$("IRBConsentsDiv").style.display="none";
	$("NoIRBConsentDiv").style.display="none";
	$("StatisticalQuestionsDiv").style.display="none";
	$("explainProspectiveDiv").style.display="none";
	$("ConsortDiv").style.display="none";
	$("NoConsortDiv").style.display="none";
	$("Observational1Div").style.display="none";
	$("CohortDiv").style.display="none";
	$("explainCohortDiv").style.display="none";
	$("CaseControlDiv").style.display="none";
	$("explainCaseControlDiv").style.display="none";
	$("CrossSectionalDiv").style.display="none";
	$("explainCrossSectionalDiv").style.display="none";
	$("MetaRegressionDiv").style.display="none";
	$("explainMetaRegressionDiv").style.display="none";
	$("explainMetaAnalysisDiv").style.display="none";
	$("explainCostDiv").style.display="none";
	$("explainClinicalDiv").style.display="none";
	$("explainNegativeStudyDiv").style.display="none";
	$("explainMixedEffectsDiv").style.display="none";
	$("NegativeStudyWarningDiv").style.display="none";
	$("MixedEffectsWarningDiv").style.display="none";
}

function toggleAttestations(vis){
	$$('.AttestStudyDiv').each(function(X){
		$(X).style.display=vis;
		});
}

function TurnOnPatientConsents(){
	$('PatientConsentsDiv').style.display = "block";
	$('FamilyConsentsDiv').style.display = "none";
	$('IRBConsentsDiv').style.display = "none";
	CreateTitlePage();
}

function TurnOnFamilyConsents(){
	$('PatientConsentsDiv').style.display = "none";
	$('FamilyConsentsDiv').style.display = "block";
	$('IRBConsentsDiv').style.display = "none";
	CreateTitlePage();
}

function TurnOnIRBConsents(){
	$('PatientConsentsDiv').style.display = "none";
	$('FamilyConsentsDiv').style.display = "none";
	$('IRBConsentsDiv').style.display = "block";
	CreateTitlePage();
}

function TurnOffConsentTypeDiv() {
	$("ConsentTypeRadio1").checked= false; 
	$("ConsentTypeRadio2").checked= false; 
	$("ConsentTypeRadio3").checked= false;
	$("PatientConsentIncludedRadioYes").checked= false;
	$("PatientConsentIncludedRadioNo").checked= false;
	$("FamilyConsentIncludedRadioYes").checked= false;
	$("FamilyConsentIncludedRadioNo").checked= false;
	$("IRBConsentIncludedRadioYes").checked= false;
	$("IRBConsentIncludedRadioNo").checked= false;

	$('ConsentTypeDiv').style.display = "none";
	$('PatientConsentsDiv').style.display = "none";
	$('FamilyConsentsDiv').style.display = "none";
	$('IRBConsentsDiv').style.display = "none";
	$('NoPatientConsentDiv').style.display = "none";
	$('NoFamilyConsentDiv').style.display = "none";
	$('NoIRBConsentDiv').style.display = "none";
}

/////////////////////// Define Button Functions ////////////////////////

function NewAuthorButtonFunction(){
//	Close existing author, if any
	if (editedIndex != -99)
		saveAuthorButtonFunction();

//	Double check that all authors are off
	for (var i = 0; i < nextIndex; i++) {
	    $("Author_data_" + i).hide();
	}

    $("NewAuthorForm").innerHTML = createForm();

	if ($('Research_ReportCheckBox').checked || $('Brief_ReportCheckBox').checked || $("Technical_CommunicationCheckBox").checked) {
		toggleAttestations("block");
		}
    $$("#NewAuthorForm.Corresponding_div").invoke('hide');
    $("NewAuthorForm").show();
    $('NewAuthorButton').hide();
    $('AddAuthorButton1').show();
    $('AddAuthorButton2').show();
    $('CancelAuthorButton1').show();
    $('CancelAuthorButton2').show();
	$("AuthorInfoDiv").style.border="1px solid #CCAA33";
	window.location.hash="NewAuthorMark";
}

function addAuthorButtonFunction(){
	// Events that follow completion of the Author form and clicking "Add Author"
	
	$('Author_999_Click').style.display="none";
    addAuthorToAuthors();
    nextIndex++;
    showUpDown();
    showNewAuthorButton();
	CreateTitlePage();
}

function saveAuthorButtonFunction(){
	var index = editedIndex;
    $('Authors_Author_name_' + index).innerHTML = AuthorName(index);

//	turn off all authors
	for (var i = 0; i < nextIndex; i++) {
	    $("Author_data_" + i).hide();
	}

	$("Author_" + index + "_explainCOIBox").style.display = "none";
	editedIndex = -99;
    showNewAuthorButton();
    CreateTitlePage();
}

function cancelAuthorButtonFunction() {
		// Appears if adding an Author, and then cancelling without saving
        $('NewAuthorForm').innerHTML = "";
        showNewAuthorButton();
		CreateTitlePage();
}

//function ShowTitlePageFunction() {
//		CreateTitlePage();
//		$('SavePage').style.display = "none";
//		$('EmailPage').style.display = "block";
//		window.location.hash="TitlePage";
//}

function NoCopy(){
//	 ShowTitlePageFunction();
// 	alert("To paste the Title Page into your manuscript, please use the Email Title Page button, and then copy from the e-mail into your manuscript. Please DO NOT copy the Title Page for your manuscript directly from your browser.")
}

function LoseTitlePageErrors() {
	if ($('TitlePageErrors').style.fontSize=="12px") {
			$('TitlePageErrors').style.fontSize="18px"
			$('TitlePageErrors').style.color="#000000"
			} else {
			$('TitlePageErrors').style.fontSize="12px"
			$('TitlePageErrors').style.color="#FF0000"
		}
}

////////////////////////////////// Define Author-Apecific Events /////////////////////////////
function AuthorMoveup(e){
	// cross browswer compatability
	try {
		var x = e.id;
	}
	catch (error) {
		var x = event.srcElement.id;
	}
	x = x.replace("Author_","");
    var workingAuthorID = parseInt(x);
    //first get the person id
    var changeAuthor = workingAuthorID - 1;
    moveAuthor(workingAuthorID, changeAuthor);
}

function AuthorMovedown(e){
	// cross browswer compatability
	try {
		var x = e.id;
	}
	catch (error) {
		var x = event.srcElement.id;
	}
	x = x.replace("Author_","");
    var workingAuthorID = parseInt(x);
    //first get the person id
    var changeAuthor = workingAuthorID + 1;
    moveAuthor(workingAuthorID, changeAuthor);
}

function AuthorEdit(e) {
	// cross browswer compatability
	try {
		var x = e.id;
	}
	catch (error) {
		var x = event.srcElement.id;
	}
	x = x.replace("Author_","");
	var index =parseInt(x);
	if (index == editedIndex){
		alert("Already editing "+AuthorName(index));
		return;
	}
	if (editedIndex != -99)
		saveAuthorButtonFunction();

//	turn off all authors, just to be sure
	for (var i = 0; i < nextIndex; i++) {
	    $("Author_data_" + i).hide();
	}

 	editedIndex = index;			
	$('NewAuthorForm').innerHTML = "";
	$("AuthorInfoDiv").style.border="1px solid #CCAA33";
    $("Author_data_" + index).show();

    $('NewAuthorButton').hide();
    $('AddAuthorButton1').hide();
    $('AddAuthorButton2').hide();
    $('CancelAuthorButton1').hide();
    $('CancelAuthorButton2').hide();
    $("SaveAuthorButton1").value = "Completed Changes to " + AuthorName(index);
    $("SaveAuthorButton2").value = "Completed Changes to " + AuthorName(index);
    $("SaveAuthorButton1").show();
    $("SaveAuthorButton2").show();
	window.location.hash="NewAuthorMark";
//	clearTitlePage();
}

function AuthorDelete(e) {
	// cross browswer compatability
	try {
		var x = e.id;
	}
	catch (error) {
		var x = event.srcElement.id;
	}
	x = x.replace("Author_","");
    var index = parseInt(x);
    var answer = confirm("Are you sure you want to delete " + AuthorName(index) + "?");
    if (answer) {
		if (editedIndex != -99) saveAuthorButtonFunction();
        for (i = index; i < nextIndex - 1; i++) {
            moveAuthor(i, i + 1);
		    $('Authors_Author_name_' + index).innerHTML = AuthorName(i);
			}
        nextIndex--;
        $$('#save_Author_' + nextIndex).invoke("remove");
        $('Author_' + nextIndex).remove();
        $('Author_data_' + nextIndex).remove();
        showUpDown();
        CreateTitlePage();
    }
}

////////////////////////////////////////////////////////////////////////////
function toggleThis(targetID, box){
    var vis = (box.checked) ? "block" : "none";
 	$(targetID).style.display = vis;
	CreateTitlePage();
}

function toggleBox(targetID){
    if ($(targetID).style.display == "block") 
		$(targetID).style.display = "none";
	else  
		$(targetID).style.display = "block";
	CreateTitlePage();
}

function toggleAuthorCOIExplanation(e){
	// cross browswer compatability
	try {
		var x = e.id;
	}
	catch (error) {
		var x = event.srcElement.id;
	}
	x = x.replace("Author_","");
	var index =parseInt(x);

	var targetID="Author_"+index+"_explainCOIBox";
    if ($(targetID).style.display == "block") 
		$(targetID).style.display = "none";
	else  
		$(targetID).style.display = "block";
}

function TurnOn(targetID){
	$(targetID).style.display = "block";
	CreateTitlePage();
}

function TurnOff(targetID){
	$(targetID).style.display = "none";
	CreateTitlePage();
}

function TurnOnMeetingDiv(){
	$("MeetingDiv").style.display = "block";
	$("YesMeetingDiv").style.display = "block";
	CreateTitlePage();
}

function TurnOffMeetingDiv(){
	$("MeetingDiv").style.display = "none";
	$("YesMeetingDiv").style.display = "none";
	CreateTitlePage();
}

function toggleCorrespondingAuthor(box){
	// Get index of current Author
	var x = box.id;
	x = x.replace("Author_", "");
	var index = parseInt(x);

	// if the box is being deselected, then just delete the corresponding author
	if (box.checked == false) {
		if (index < 999) {
			var priorName = AuthorName(index).replace(" (Corresponding Author)", "");
			var answer = confirm("Are you sure you want to remove " + priorName + " as Corresponding Author?");
			if (answer) {
				$("Corresponding_Author_" + index + "_div").style.display = "none";
				$('Authors_Author_name_' + index).innerHTML = AuthorName(index);
			    $("SaveAuthorButton1").value = "Completed Changes to " + AuthorName(index);
   				$("SaveAuthorButton2").value = "Completed Changes to " + AuthorName(index);
				CreateTitlePage();
				return;
			}
			else {
				box.checked = true;
				return;
			}
		}
		$("Corresponding_Author_" + index + "_div").style.display = "none";
	    $("SaveAuthorButton1").value = "Completed Changes to " + AuthorName(index);
	    $("SaveAuthorButton2").value = "Completed Changes to " + AuthorName(index);
		CreateTitlePage();
		return;
	}

	// Otherwise, get prior Corresponding Author, if any
	var priorIndex = -99;
	for (var i = 0; i < nextIndex; i++) {
		if (i != index && $("Author_" + i + "_Corresponding_Author").checked) {
			priorIndex = i;
			i = nextIndex; // force break
		}
	}

	// If there is no prior Corresponding Author, just turn on the box
	if (priorIndex == -99) {
		$("Corresponding_Author_" + index + "_div").style.display = "block";
		if (index < 999) $('Authors_Author_name_' + index).innerHTML = AuthorName(index);
	    $("SaveAuthorButton1").value = "Completed Changes to " + AuthorName(index);
	    $("SaveAuthorButton2").value = "Completed Changes to " + AuthorName(index);
		CreateTitlePage();
		return;
	}

	// If there is a prior Corresponding Author, need to verify change
	var priorName = AuthorName(priorIndex).replace(" (Corresponding Author)", "");
	var answer = confirm("This will remove remove " + priorName + " as Corresponding Author.\nDo you want to proceed?");
	if (answer) {
	// OK, need to undo prior
			$("Author_" + priorIndex + "_Corresponding_Author").checked = false;
			$('Authors_Author_name_' + priorIndex).innerHTML = AuthorName(priorIndex);
			$("Corresponding_Author_" + priorIndex + "_div").style.display = "none";
			box.checked = true;
			$("Corresponding_Author_" + index + "_div").style.display = "block";
			if (index < 999) $('Authors_Author_name_' + index).innerHTML = AuthorName(index);
		}
	else box.checked=false;
    $("SaveAuthorButton1").value = "Completed Changes to " + AuthorName(index);
    $("SaveAuthorButton2").value = "Completed Changes to " + AuthorName(index);
	CreateTitlePage();
}

function toggleCOIyes(e){
	// cross browswer compatability
	try {
		var x = e.id;
	}
	catch (error) {
		var x = event.srcElement.id;
	}
	x = x.replace("Author_","");
	var index =parseInt(x);
    if ($("Author_"+index+"_COI_yes").checked) {
		$("Author_"+index+"_COIDetailsDiv").style.display = "block";
		$("Author_"+index+"_COI_no").checked = false;
	}
	else {
		$("Author_"+index+"_COIDetailsDiv").style.display = "none";
		$("Author_"+index+"_COI_no").checked = true;
	}
	CreateTitlePage();
}

function toggleCOIno(e){
	// cross browswer compatability
	try {
		var x = e.id;
	}
	catch (error) {
		var x = event.srcElement.id;
	}
	x = x.replace("Author_","");
	var index =parseInt(x);
    if ($("Author_"+index+"_COI_no").checked) {
		$("Author_"+index+"_COIDetailsDiv").style.display = "none";
		$("Author_"+index+"_COI_yes").checked = false;
	}
	else {
		$("Author_"+index+"_COIDetailsDiv").style.display = "block";
		$("Author_"+index+"_COI_yes").checked = true;
	}
	CreateTitlePage();
}

function clearTitlePage(){
    $("PaperTitlePage").style.display = "none";
}

function AuthorName(index){
    var x = $('Author_' + index + '_name').value;
	if (x == "") x = "Author "+(index+1);
    if ($("Author_" + index + "_Corresponding_Author").checked) {
		x += " (Corresponding Author)";
	}
	return x;
}

function AuthorName999(index){
    var x = $('Author_999_name').value;
	if (x == "") x = "Author "+(index+1);
    if ($("Author_999_Corresponding_Author").checked) {
        x += " (Corresponding Author)";
    }
	return x;
}

function moveAuthor(workingAuthor, changeAuthor){
    var i;
//	clearTitlePage();
	// Update Author you want to move to -1
    updateAuthor(workingAuthor, -1);
	// Reassign change Author ID to working Author ID
    updateAuthor(changeAuthor, workingAuthor);
	// Reassign original workingn Author to change Author
    updateAuthor(-1, changeAuthor);
    for (i = 0; i < nextIndex; i++) {
        $('AuthorDiv').insert($('Author_' + i));
    }
    for (i = 0; i < nextIndex; i++) {
        $('AuthorInfoDiv').insert($('Author_data_' + i));
    }
    $('Authors_Author_name_' + workingAuthor).innerHTML = AuthorName(workingAuthor);
    $('Authors_Author_name_' + changeAuthor).innerHTML = AuthorName(changeAuthor);

    showUpDown();
    CreateTitlePage();
}

function updateAuthor(AuthorID, newID){
    var authorReg = new RegExp("^Author_" + AuthorID);
    var nameReg = new RegExp("^Author_" + AuthorID);
    var spanReg = new RegExp("^Authors_Author_name_" + AuthorID);
    var dataReg = new RegExp("^Author_data_" + AuthorID);
    var saveReg = new RegExp("save_Author_" + AuthorID, "g");
    var corrReg = new RegExp("^Corresponding_Author_" + AuthorID);
    var mainElement = $("Author_" + AuthorID);
    var CorrespondingAuthor = $('Corresponding_Author_' + AuthorID + '_div');
    CorrespondingAuthor.id = CorrespondingAuthor.id.replace(corrReg, "Corresponding_Author_" + newID);
	
	mainElement.descendants().each(function(element){
			if (element.id != "" && spanReg.test(element.id)) {
				element.id = element.id.replace(spanReg, "Authors_Author_name_" + newID);
			}
			if (element.id != "" && authorReg.test(element.id)) {
				element.id = element.id.replace(authorReg, "Author_" + newID);
			}
		});


    var dataElement = $("Author_data_" + AuthorID);
    dataElement.descendants().each(function(element){
        if (authorReg.test(element.id)) {
				element.id = element.id.replace(authorReg, "Author_" + newID);
        }
        var forAttr = element.readAttribute('for');
        if (nameReg.test(forAttr)) {
            element.writeAttribute("for", forAttr.replace(nameReg, "Author_" + newID));
        }
        var nameAttr = element.readAttribute('name');
        if (nameReg.test(nameAttr)) {
            element.writeAttribute("name", nameAttr.replace(nameReg, "Author_" + newID));
          }
    });

    var saveButton = $("save_Author_" + AuthorID);
    if (saveButton !== null) {
        //	saveButton.setAttribute('rel', saveButton.getAttribute('rel').replace(AuthorID, newID));
       saveButton.id = saveButton.id.replace(saveReg, "save_Author_" + newID);
    }
    mainElement.id = mainElement.id.replace(authorReg, "Author_" + newID);
    dataElement.id = dataElement.id.replace(dataReg, "Author_data_" + newID);
}

function showUpDown(){
    $$(".AuthorUp").invoke('show');
    $$(".AuthorDown").invoke('show');
    $$("#Author_0 .AuthorUp").invoke('hide');
    $$("#Author_" + (nextIndex - 1) + " .AuthorDown").invoke('hide');
}
// Function that displays the Author box

function addAuthorToAuthors(){
	var index=999;
	// This sets up "mainElement" in updateAuthor above
    var Author = "<div id='Author_999' class='Author' style='display:none; clear:left;'>";
    Author += "<span class=AuthorMove>";
    Author += "<a tabindex='-1' id='Author_999_moveup'   class='AuthorUp'   href='javascript:void(0)' onclick='AuthorMoveup(this)'  ><img src='/Authors/images/arrow_up.png'   title='Move up'   alt='Move up'  /></a>";
    Author += "<a tabindex='-1' id='Author_999_movedown' class='AuthorDown' href='javascript:void(0)' onclick='AuthorMovedown(this)'><img src='/Authors/images/arrow_down.png' title='Move down' alt='Move down'/></a>";
    Author += "</span>";
    Author += "<a tabindex='-1' id='Author_999_edit'   class='AuthorAction' href='javascript:void(0)' onclick='AuthorEdit(this)'  >EDIT</a>";
    Author += "<a tabindex='-1' id='Author_999_delete' class='AuthorAction' href='javascript:void(0)' onclick='AuthorDelete(this)'>DELETE</a>";
    Author += "<span id='Authors_Author_name_999'>" + AuthorName999(nextIndex);
    Author += "</span>";
    Author += "</div>";

	// This adds mainElement to the page
    $('AuthorDiv').insert(Author);
    $("Author_999").show();

    $('NewAuthorForm').writeAttribute('id', 'Author_data_999');
    $('Author_data_999').hide();
    Element.insert('Author_data_999', {
        after: '<div id="NewAuthorForm"/>'
    });
    $('NewAuthorForm').writeAttribute('style', 'display:none');

	// Turn off COI box in case
	$("Author_999_explainCOIBox").style.display = "none";

	// Now set the correct Author index
	updateAuthor(999,nextIndex);
}

function showNewAuthorButton(){
    $('NewAuthorButton').show();
    $('AddAuthorButton1').hide();
    $('AddAuthorButton2').hide();
    $('CancelAuthorButton1').hide();
    $('CancelAuthorButton2').hide();
	$('SaveAuthorButton1').hide();
	$('SaveAuthorButton2').hide();
	$("AuthorInfoDiv").style.border="#FFFFFF";
}

function createForm(){
	index=999;
    //basic text fields
    var form = '<div id="InnerAuthorForm" class="NoBorder">';
    form += createNameField('name', index);
    form += createTextField('degree', index);
    form += createTextField('academic title', index);
    form += createTextField('affiliation where work was done', index);
    form += createTextField('current affiliation (if different)', index);
    form += createTextField('email', index);

    //Corresponding Author
	form +='<div class="AuthorDiv">'
	form += '<label for="Author_999_[Corresponding_Author]" class="AuthorLabel">Corresponding Author</label>'
	form += '<input type="checkbox" name="Author_999_[Corresponding_Author]"'
	form += 'id="Author_999_Corresponding_Author" onclick="toggleCorrespondingAuthor(this)" class="AuthorCheckBox"></div>';
 
    form += "<div class='Corresponding_div' id='Corresponding_Author_" + index + "_div' style='display:none'>";
    form += createTextArea('address', index);
    form += createTextField('phone', index);
    form += createTextField('fax', index);
    form += "</div>";

    //Authorship
    form += '<div id="Author_999_Role" ';
	form += 'class="AuthorDiv"><br>Please enter this author\'s role in the study';
	form += ' (check all that apply)</div>';
    form += createCheckBox('study design', index);
    form += createCheckBox('conduct of study', index);
    form += createCheckBox('data analysis', index);
    form += createCheckBox('manuscript preparation', index);
    form += createTextField('other', index);
    
	//Attestations
    form += '<div class="AuthorDiv"><br>Will you publically attest that you:</div>';
 	form += '<div class="YesNoHeader" style="margin: 0 0 0 232px;">Yes&nbsp;&nbsp;No</div>';
    form += createSpecialCheckBox('have seen the original study data?', index, 1);
    form += createSpecialCheckBox('reviewed the data analysis?', index, 1);
	form += createSpecialCheckBox('are the archival author?', index, 1);
    form += createSpecialCheckBox('approved the final manuscript?', index, 0);

    //conflicts of interest
    form += '<div class="AuthorDiv" id="Author_999_COI"><br>Conflict of Interest disclosures are required of all authors</div>';
	form += '<div id="Author_999_explainCOIBox" class="explainCOIBox" style="display:none;">'
	form += '<br>'+$("InnerAttestConflictDiv").innerHTML+'</div>';
	
	form += '<div class="AuthorDiv">';
	form += '<div class="AuthorLabel">';
	form += '<a id="Author_999_showCOI" class="AuthorAction" href="javascript:void(0)" onclick="toggleAuthorCOIExplanation(this)">Conflict of Interest Explanation</a>';
	form += '</div>';
	form += '</div>';

    form += '<div class="AuthorDiv">';
	form += '<label for="Author_999_[conflicts_of_interest_no]" class="AuthorLabel">';
	form += 'I have no conflicts of interest</label>';
	form += '<input type="checkbox" name="Author_999_[conflicts_of_interest_no]" value="unconflicted"';
	form += 'id="Author_999_COI_no" onclick="toggleCOIno(this)" class="AuthorCheckBox">&nbsp;';
    form += '</div>'	

    form += '<div class="AuthorDiv" >';
	form += '<label for="Author_999_[conflicts_of_interest_yes]" class="AuthorLabel">';
	form += 'I have conflicts of interest</label>';
	form += '<input type="checkbox" name="Author_999_[conflicts_of_interest_yes]" value="conflicted"';
	form += 'id="Author_999_COI_yes" onclick="toggleCOIyes(this)" class="AuthorCheckBox">&nbsp;';
    form += '</div>'

	form += '<div id="Author_999_COIDetailsDiv" class="COIACCME" style="display:none;">';
	form += 'Please provide the following information about any relevant financial relationships with ';
	form += 'a commercial interest (i.e., conflicts of interest) occurring with the past 12 months, ';
	form += 'including the name of the commercial interest and the nature of the benefit to you or your ';
	form += 'spouse/partner.  <br><br>I have the following relevant financial interests to disclose:'
	form += createConflictOfInterestTable(index);
	form += '<label for="Author_999_[conflicts_of_interest_free]" class="">You also may describe your conflicts of interest below</label>'
	form += "<textarea id='Author_999_COI_free_response' class='TopYellowBox' name='Author_999_[conflicts_of_interest_free]'></textarea>"
    form += "</div>";
    form += '<div id="Author_999_Click" class="AuthorDiv"><br>Click Add This Author once you have entered the required data for this author.<br><br></div>';
    form += "</div>";
    return form;
}

function createNameField(name, index){
    var label_name = name.capitalize();
    name = name.replace(/ /g, "_").replace(/\(/g, "").replace(/\)/g, "");
	var html = '<div class="AuthorDiv">'
	html += '<label for="Author_' + index + '_[' + name + ']" class="AuthorLabel">' + label_name + '</label>'
	html += '<input type="text" name="Author_' + index + '_[' + name + ']" id="Author_' + index + '_' + name + '" onblur="CreateTitlePage();window.location.hash=\'AuthorMark\';" class="AuthorYellowBox"/>'
	html += '</div>';
    return html;
	}

function createTextField(name, index){
    var label_name = name.capitalize();
    name = name.replace(/ /g, "_").replace(/\(/g, "").replace(/\)/g, "");
	var html = '<div class="AuthorDiv">'
	html += '<label for="Author_' + index + '_[' + name + ']" class="AuthorLabel">' + label_name + '</label>'
	html += '<input type="text" name="Author_' + index + '_[' + name + ']" id="Author_' + index + '_' + name + '" onblur="CreateTitlePage()" class="AuthorYellowBox"/>'
	html += '</div>';
    return html;
	}

function createCheckBox(name, index){
    var label_name = name.capitalize();
    name = name.replace(/ /g, "_").replace(/\(/g, "").replace(/\)/g, "");
    var html = '<div class="AuthorDiv">';
	html += '<label for="Author_' + index + '_[' + name + ']" class="AuthorLabel">' + label_name + '</label>';
	html += '<input type="checkbox" name="Author_' + index + '_[' + name + ']" id="Author_' + index + '_' + name + '" onclick="CreateTitlePage()" class="AuthorCheckBox">';
	html += '</div>';
    return html;
}

function createSpecialCheckBox(name, index, flag){
    var label_name = name.capitalize();
	if (flag == 1) 	var html = '<div class="AttestStudyDiv">';
	else var html = '<div class="AttestMSDiv">';
    name = name.replace(/ /g, "_").replace(/\(/g, "").replace(/\)/g, "");
	radioName = "Radio"+radioCounter;
    html += '<label id="Author_' + index + '_' + name+'"';
	html += 'for="'+radioName+'" class="AuthorLabel">';
	html += label_name + '</label>';
	html += '<div class="AuthorRadio">';
	html += '<input type="radio" style="float:left;" name="'+radioName+'" id="Author_' + index + '_' + name + 'Yes" onclick="CreateTitlePage()" >';
	html += '<input type="radio" style="float:left;" name="'+radioName+'" id="Author_' + index + '_' + name + 'No" onclick="CreateTitlePage()" >';
	html += '</div></div>';	
	radioCounter += 1;
	return html;
	}

function createNOCOICheckBox(name, index){
    var label_name = name.capitalize();
    name = name.replace(/ /g, "_").replace(/\(/g, "").replace(/\)/g, "");

    var html = '<div class="AuthorDiv">';
	html += '<label for="Author_' + index + '_[' + name + ']" class="NOCOIlabel">' + label_name + '</label>';
	html += '<input type="checkbox" name="Author_' + index + '_[' + name + ']" id="Author_' + index + '_' + name + '"/ class="NOCOICheckBox" >';
	html += '</div>';
	return html;
	}

function createTextArea(name, index){
    var label_name = name.capitalize();
    name = name.replace(/ /g, "_").replace(/\(/g, "").replace(/\)/g, "");

    var html = '<div class="AuthorDiv">';
	html += '<label for="Author_' + index + '_[' + name + ']" class="AuthorLabel">' + label_name + '</label>';
	html += '<textarea name="Author_' + index + '_[' + name + ']" id="Author_' + index + '_' + name + '" class="AuthorYellowBox" onblur="CreateTitlePage()" >';
	html += '</textarea></div>';
	return html;
	}

function createConflictOfInterestTable(index){
    var table = "<table class='conflict_of_interest_table' id='Author_" + index + "_conflict_of_interest_table'>";
    table += createConflictOfInterestHeadings(headingArray);
    for (var rowIndex = 0; rowIndex <= 5; rowIndex++) {
        table += createConflictOfInterestRow(index, rowIndex);
    }
    return table + "</table>";
}

function createConflictOfInterestHeadings(){
    var headings = "<tr>";
    headings += '<th class="COIHeader">Company</th>';
    headingArray.each(function(heading, index){
        headings += '<th class="COIHeader" center>' + heading.capitalize() + '</th>';
    });
    return headings + "</tr>";
}

function createConflictOfInterestRow(index, rowIndex){
    var row = '<tr align="center">';
    row += createConflictOfInterestRowInput("company", index, rowIndex);
    headingArray.each(function(heading){
        row += createConflictOfInterestRowCheckBox(heading, index, rowIndex);
    });
    return row + "</tr>";
}

function createConflictOfInterestRowInput(name, index, rowIndex){
    name = name.replace(/ /g, "_");
    var html = '<td>';
	html += '<input type="text" name="Author_' + index + '_[' + name + '][' + rowIndex + ']"';
	html += ' id="Author_' + index + '_' + name + '_' + rowIndex + '" rel="' + rowIndex + '"';
	html += ' class="COIYellowBox"/ onblur="CreateTitlePage()" >';
	html += '</td>';
	return html;
	}

function createConflictOfInterestRowCheckBox(name, index, rowIndex){
    name = name.replace(/ /g, "_");
    var html = '<td>';
	html += '<input type="checkbox" name="Author_' + index + '_[' + name + '][' + rowIndex + ']"';
	html += ' id="Author_' + index + '_' + name + '_' + rowIndex + '" rel="' + rowIndex + '"';
	html += ' class="' + name + '" onclick="CreateTitlePage()" ></td>';
	return html;
	}

function trim(s) {
	s = s.replace(/(^\s*)|(\s*$)/gi,"");
	s = s.replace(/[ ]{2,}/gi," ");
	s = s.replace(/\n /,"\n");
	s = s.replace(/\.$/,"");
	return s;
}
