function TestingEachFunction(){
	var html = "";
	var X = 0;
	for (property in $$("#paper")) {
		html += property + ": " + $$("#paper")[property] + "<br>\r\n";
	}
	html += $$("#paper").each(function(element){
		html += element;
	}, X);
	html += X;
	$('PaperTitlePage').innerHTML = html;
}

function CreateTitlePage(){
	if (nextIndex == 0) return;
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
//	var html = '<div id="TopPaper" class="PaperBody">';
	var html='';
	var valid = validData();
	if (nextIndex >= 0) {
		if (valid === "") {
			html += createPaperTitle();
			html += createAuthors();
			html += createInstitution();
			html += createShortTitle();
			html += createFunding();
			html += createCorrespondence();
			html += createLWW();
			html += createEdOffice();
			html += createStatisticalReview();
			html += "Title Page Created on "+Today;
			$('PaperTitlePage').innerHTML = html;
			$('PaperTitlePage').style.display = "block";

			$('TitlePageErrors').innerHTML = "<span class='TitlePageOK'>Scroll to bottom to view title page.</span>";
			$('TitlePageErrors').style.display = "block";
			$('TitlePageErrors').style.border = "none";
			$('EmailPage').style.display = "block";
			$('SavePage').style.display = "none";
			if (IE6){
				window.location.hash="TopMark";
				}
		}
		else {
			html += valid;
			$('PaperTitlePage').innerHTML = "";
			$('PaperTitlePage').style.display = "none";
			$('TitlePageErrors').innerHTML = html;
			$('TitlePageErrors').style.display = "block";
			$('TitlePageErrors').style.border = "#FF0000 double";
			$('TitlePageErrors').style.fontSize="12px"
			$('TitlePageErrors').style.color="#FF0000"
			$('EmailPage').style.display = "none";
			if (nextIndex > 0 && filter.test($("Author_0_email").value)) {
				$('SavePage').style.display = "block";
			} else {
				$('SavePage').style.display = "none";
			}
		}
	}
}

function CreateDummy(text){
	$('PaperTitlePage').innerHTML = "Dummy called from "+text+" at nextIndex: "+nextIndex;
}

function createPaperTitle(){
	return "<h2 id='PaperTitle'>" + $('title').value + "</h2>";
}

function createAuthors(){
	var html = "";
	var i;
	var affiliationHTML;
	var currentAffil;
	var AuthorshipHTML;
	var AuthorshipRole;
	var AuthorN;
	var conflictHTML;
	var conflictList;
	var conflictN;
	var rowIndex;
	var attestHTML;
	var attestList;

	for (i=0; i < nextIndex; i++){
		var AuthorHTML = "";
		AuthorHTML += $('Author_' + i + '_name').value;
		AuthorHTML += ", " + $('Author_' + i + '_degree').value;
	    //JUST USE NUMBERS, current affiliation goes in parenthesis after affiliation where work was done
		AuthorHTML += "<sup>"+(i+1)+"</sup>";
		AuthorHTML += "<br>\r\n";
//		html += AuthorHTML;
	}
	html += "<ol style='margin-bottom: 0px'>";
	for (i=0; i < nextIndex; i++){
			
			affiliationHTML = "<li><b>";
			affiliationHTML += $('Author_' + i + '_name').value;
			affiliationHTML += ", " + $('Author_' + i + '_degree').value+"</b>";
			
			affiliationHTML +="<ul>";
			affiliationHTML += "<li type='disc'>Title: "
			affiliationHTML += $('Author_' + i + '_academic_title').value;
			affiliationHTML += "</li>";
			
			affiliationHTML += "<li type='disc'>Affiliation: ";
			affiliationHTML += $('Author_' + i + '_affiliation_where_work_was_done').value;
			currentAffil = $('Author_' + i + '_current_affiliation_if_different').value;
			if (currentAffil !== "")
				affiliationHTML += " (current affiliation: "+currentAffil+")";
			affiliationHTML += "</li>";

			affiliationHTML += "<li type='disc'>Email: ";
			affiliationHTML += $('Author_' + i + '_email').value;
			affiliationHTML += "</li>";

			html += affiliationHTML;

			/* Create Authorship role sentence */
			AuthorshipHTML = "<li type='disc'>Role: This author helped ";
			AuthorshipRole = new Array();
			AuthorN = 0;
			
		    if ($("Author_"+i+"_study_design").checked) 
				{
				AuthorshipRole.push("design the study");
				AuthorN++;
				}
		    if ($("Author_"+i+"_conduct_of_study").checked) 
				{
				AuthorshipRole.push("conduct the study");
				AuthorN++;
				}
		    if ($("Author_"+i+"_data_analysis").checked) 
				{
				AuthorshipRole.push("analyze the data");
				AuthorN++;
				}
		    if ($("Author_"+i+"_manuscript_preparation").checked) 
				{
				AuthorshipRole.push("write the manuscript");
				AuthorN++;
				}
			if ($('Author_'+i+'_other').value !== "")
				{
				AuthorshipRole.push($('Author_'+i+'_other').value);
				AuthorN++;
				}
			if (AuthorN === 0)
				{
				AuthorshipRole.push("with no identified aspect of this manuscript");
				}
			AuthorshipHTML += AuthorshipRole.toSentence();
			AuthorshipHTML += ".</li>";
			html += AuthorshipHTML;

			/* Create Conflict of Interest Sentence */
			conflictHTML = "<li type='disc'>Conflicts: This author ";
			if ($("Author_" + i + "_COI_no").checked === true) {
				conflictHTML += "reported no conflicts of interest";
			}
			else {
				conflictList = new Array();
				conflictN = 0;
				for (rowIndex = 0; rowIndex <= 5; rowIndex++) {
					if ($("Author_" + i + "_company_" + rowIndex).value !== "") {
						company = $("Author_" + i + "_company_" + rowIndex).value;
						if ($("Author_" + i + "_salary_" + rowIndex).checked === true) {
							conflictList.push("worked for " + company);
							conflictN++;
						}
						if ($("Author_" + i + "_honoraria_" + rowIndex).checked) {
							conflictList.push("received honoraria from " + company);
							conflictN++;
						}
						if ($("Author_" + i + "_consulting_" + rowIndex).checked) {
							conflictList.push("consulted for " + company);
							conflictN++;
						}
						if ($("Author_" + i + "_royalties_" + rowIndex).checked) {
							conflictList.push("received royalties from " + company);
							conflictN++;
						}
						if ($("Author_" + i + "_equity_" + rowIndex).checked) {
							conflictList.push("an equity interest in " + company);
							conflictN++;
						}
						if ($("Author_" + i + "_funded_research_" + rowIndex).checked) {
							conflictList.push("received research funding from " + company);
							conflictN++;
						}
						if ($("Author_" + i + "_other_" + rowIndex).checked) {
							conflictList.push('reported an "other" conflict of interest with ' + company);
							conflictN++;
						}
					}
				}
				
				conflictHTML += conflictList.toSentence();
                if ($("Author_" + i + "_COI_free_response").value !=="")
					{
						if (conflictN == 0)
							conflictHTML = "<li type='disc'>Conflicts: "+$("Author_" + i + "_COI_free_response").value;
						else
							conflictHTML = conflictHTML+". "+$("Author_" + i + "_COI_free_response").value;
					}								
			}
			conflictHTML += ".</li>";
	        html += conflictHTML;

			/* Create Attestation Sentence */
			attestHTML = "<li type='disc'>Attestation: This author ";
			attestList = new Array();
			
		    if ($("Author_"+i+"_have_seen_the_original_study_data?Yes").checked) 
				{
				attestList.push("has seen the original study data");
				}
		    if ($("Author_"+i+"_reviewed_the_data_analysis?Yes").checked) 
				{
				attestList.push("reviewed the analysis of the data");
				}
		    if ($("Author_"+i+"_approved_the_final_manuscript?Yes").checked) 
				{
				attestList.push("approved the final manuscript");
				}
		    if ($("Author_"+i+"_approved_the_final_manuscript?No").checked) 
				{
				attestList.push("<b>not</b> approved the final manuscript");
				}
		    if ($("Author_"+i+"_are_the_archival_author?Yes").checked===true) 
				{
				attestList.push("is the author responsible for archiving the study files");
				}

			attestHTML += attestList.toSentence();
			attestHTML += ".</li></ul></li><br>\r\n";
			html += attestHTML;
	}
	html += "</ol>"
	return html;
}

function createInstitution(){
	return "<b>Institution:</b> " + $('institution').value + "<br>\r\n";
}

function createCorrespondence(){
	var corIndex = findCorrespondingAuthor();
	var html = "<p>";
	html += "<b>Corresponding Author:</b><br/>";
	html += $('Author_'+corIndex+'_name').value;
	html += ", " + $('Author_'+corIndex+'_degree').value + "<br/>";
	html += $('Author_'+corIndex+'_affiliation_where_work_was_done').value + "<br/>";
	html += $('Author_'+corIndex+'_address').value.replace("\r\n", "<br/>") + "<br/>";
	html += "Phone: " + $('Author_'+corIndex+'_phone').value + " / ";
	html += "Fax: "+ $('Author_'+corIndex+'_fax').value  + "<br/>";
	html += "Email: " + "<a href=mailto:"+$('Author_'+corIndex+'_email').value + ">" + $('Author_'+corIndex+'_email').value + "</a>";
	html += "</p>";
	return html;
}

function createShortTitle(){
	return "<b>Short Title: </b>" + $('short_title').value+"<br>\r\n";
}

function createFunding(){
	var html = "Not Specified";
	data = $('funding').value;
	if(data !== ""){
		html = "<b>Funding: </b>" + data;
	}
	return html;
}

function createLWW(thisFormData){
	var html = "<p>Information for LWW regarding depositing manuscript into PubMed Central:";
	var flag = 0;
	if ($('NIHCheckBox').checked) {
		html += "<br>\r\nThis research was funded by National Institutes of Health";
		if ($('NIHText').value !== "")
			html +=", grant number "+$('NIHText').value;
		html += ".";
		flag = 1;
	}
	if ($('HHMICheckBox').checked) {
		html += "<br>\r\nThis research was funded by Howard Hughes Medical Institute";
		if ($('HHMIText').value !== "")
			html +=", grant number "+$('HHMIText').value;
		html += ".";
		flag = 1;
	}
	if ($('MRCCheckBox').checked) {
		html += "<br>\r\nThis research was funded by Medical Research Council";
		if ($('MRCText').value !== "")
			html +=", grant number "+$('MRCText').value;
		html += ".";
		flag = 1;
	}
	if ($('WTCheckBox').checked) {
		html += "<br>\r\nThis research was funded by Welcome Trust";
		if ($('WTText').value !== "")
			html +=", grant number "+$('WTText').value;
		html += ".";
		flag = 1;
	}
	if (flag === 0) {
			html += "<br>\r\nThis manuscript does <u>not</u> need to be deposited to PubMed Central.";
	}
	html += "</p>";
	return html;
}

function createEdOffice(){
	var html = "<p>For Editorial Office:"
	if ($('MSNumberText').value !== "") html += "<br>\r\n<b>Manuscript number:</b> "+$('MSNumberText').value;
	if ($('EditorText').value !== "") 	html += "<br>\r\n<b>Requesting Section Editor:</b> "+$('EditorText').value;
	if ($('MeetingText').value !== "") {
		html += "<br>\r\nThis report was previously presented, in part, at the " + $('MeetingText').value;
		if ($('NewsText').value !== "") html += ", which was the subject of an article in " + $('NewsText').value;
		html += ".";
		}
	
	html += "<br>\r\nSubmitted as ";
	if ($('Research_ReportCheckBox').checked         ) html += "a Research Report.";
	if ($('Case_ReportCheckBox').checked) {
		html += "a Case Report. ";
		if ($("TrulyExceptionalRadioYes").checked) 
			html += 'The author states that this case report meets the Journal requirement of being <b><i>"truly exceptional"</i></b>. ';
		else
			html += 'The author states that this case report does <u>not</u> meet the Journal requirement of being <b><i>"truly exceptional"</i></b>. ';
		if ($("ClaimEfficacyRadioYes").checked) 
			html += 'This case report claims that a new drug, device, or anesthetic technique is likely to be effective in a group of patients. ';
		html += createConsent();
	}
	if ($('Echo_RoundsCheckBox').checked) {
		html += "an Echo Rounds.";
		html += createConsent();
	}
	if ($('Brief_ReportCheckBox').checked            ) html += "a Brief Report.";
	if ($('Technical_CommunicationCheckBox').checked ) html += "a Technical Communication.";
	if ($('ReviewCheckBox').checked                  ) html += "a Review Article.";
	if ($('Medical_IntelligenceCheckBox').checked    ) html += "a Medical Intelligence Report.";
	if ($('Special_ArticleCheckBox').checked         ) html += "a Special Article.";
	if ($('Pro/Con/Core_ReviewCheckBox').checked     ) html += "a Pro/Con/Core Review.";
	if ($('Book_or_Multimedia_ReviewCheckBox').checked) html += "a Book or Multimedia Review.";
	if ($('Meeting_ReportCheckBox').checked           ) html += "a Meeting Report.";
	if ($('Focused_ReviewCheckBox').checked           ) html += "a Focused Review.";
	if ($('CommentaryCheckBox').checked               ) html += "a Commentary.";
	if ($('Letter_to_the_EditorCheckBox').checked) {
		html += "a Letter to the Editor.";
		html += createConsent();
	}
	if ($('EditorialCheckBox').checked                ) html += "an Editorial.";
	if ($('The_Open_MindCheckBox').checked            ) html += "an Open Mind.";
	html += "</p>"
	return html;
	}

function createConsent(){
	var html = "";
		if ($("ConsentNecessaryRadioYes").checked) {
		html += "The author states that the report describes the care of one or more patients. ";
		if ($("ConsentTypeRadioPatient").checked) 
			html += "The patient consented to publication of the report. This is described in the report. ";
		if ($("ConsentTypeRadioFamily").checked) 
			html += "A family member, next of kin, or legal representative of the patient consented to publication. This is described in the report. ";
		if ($("ConsentTypeRadioIRB").checked) 
			html += "The responsible Institutional Review Board gave permission to publish this report, as described in the report. ";
		}
	return html;
}

function createStatisticalReview(){
	var html = "";
	
	if ($("ProspectiveRadioYes" ).checked) { 
		html += "This report describes a prospective randomized clinical trial. The author states that every item in the CONSORT checklist is "
		if ($("ConsortIncludedRadioYes" ).checked) html += "included ";
		else html += "<u>not</u> included ";
		html += "in the report.<br>\r\n"
	}

	if ($("ObservationalRadioYes").checked) {
		html += "This report describes an observational clinical study. ";
	}

	if ($("Observational1RadioYes" ).checked) { 
		html += "This study includes hundreds or thousands of subjects. ";
		if ($("Observational2RadioYes").checked) {
			html += "The author states that the study reached a conclusion about clinical merit ";
			html += "based on small p-value(s) without consideration of the confidence interval or attributable risk.";
		}
		html += "<br>\r\n"
	}

	if ($("Observational3RadioYes" ).checked) {
		html += "This report describes cohort observational clinical study. The author states that the report ";
		if ($("Observational4RadioYes" ).checked) { 
			html += "includes ";
		} else {
			html += "does <u>not</u> include ";
		}
		html += "every item in the STROBE checklist for cohort observational clinical studies.<br>\r\n";
	}

	if ($("Observational5RadioYes" ).checked) {
		html += "This report describes a case control observational clinical study. The author states that the report "
		if ($("Observational6RadioYes" ).checked) { 
			html += "includes ";
		} else {
			html += "does <u>not</u> include ";
		}
		html += "every item in the STROBE checklist for case control observational clinical studies.<br>\r\n";
	}
		
	if ($("Observational7RadioYes" ).checked) {
		html += "This report describes a cross sectional observational study. The author states that the report ";
		if ($("Observational8RadioYes" ).checked) {
			html += "includes ";
			} else {
			html += "does <u>not</u> include ";
			}
		html += "every item in the STROBE checklist for cross sectional observational studies.<br>\r\n"
	}

	if ($("MetaRegressionRadioYes").checked) {
		html += "This report utilizes meta-regression (as is often done to analyze AIMS and time data). ";
		if ($("MetaRegression1RadioYes").checked) {
			html += "The author performed a meta-regression without complete study data but with an ";
			html += "independent variable that was not the basis for randomization.";
		}
		html += "<br>\r\n";
	}

	if ($("MetaAnalysisRadioYes").checked) {
		html += "This report is a meta-analysis. <br>\r\n";
		}

	if ($("CostRadioYes" ).checked) {
		html += "This report has a primary or secondary end-point that is either cost ";
		html += "or a surrogate for cost (e.g., time).<br>\r\n";
	}

	if ($("PropensityRadioYes" ).checked) {
		html += "This report includes an independent variable that was not randomized, ";
		html += "represents a clinical decision or consequence (e.g., choice of drug dose ";
		html += "or duration of surgery), and was not analyzed using propensity scores or ";
		html += "other appropriate methodology.<br>\r\n";
	}

	if ($("NegativeRadioYes" ).checked) {
		html += "This report suggests conclusion(s) of equality or absence of difference ";
		html += "without performing formal test(s) of equivalence or comparing confidence ";
		html += "interval widths to an external standard.<br>\r\n";
	}
	
	if ($("MixedEffectsRadioYes" ).checked) {
		html += "This report includes a dependent variable that may differ among providers ";
		html += "(e.g., physician) and was not analyzed by stratification, mixed models, etc.<br>\r\n"
		}
	if (html !== "") {
		html = "<p>For Statistical Reviewers <span style='color : #347C17;'>(GREEN CROWN)</span>:<br>\r\n" + html + "</p>"
	}
	return html;
}

function findCorrespondingAuthor(){
	for(var i = 0; i < nextIndex; i++){
		if($('Author_'+i+'_Corresponding_Author').checked)
			return i;
	}
	return -1;
}

//http://code.google.com/p/active-support-for-javascript/
Array.prototype.toSentence = function() {
        var options = Object.extend({ 
                "connector": "and", 
                "skip_last_comma": false 
        }, arguments[0] || {});

        switch (this.length) {
                case 0:  return "";
                case 1:  return this.first().toString();
                case 2:  return this[0] + " " + options.connector + " " + this[1];
                default: return this.slice(0, -1).join(', ') + (options.skip_last_comma ? ' ' : ', ') + options.connector + " " + this[this.length-1];
        }
}

function Capitalize(word){
	var tmpChar = word.substring(0,1).toUpperCase();
	var postString = word.substring(1,word.length);
	return tmpChar + postString;
}

