function EmailTitlePageFunction() {
 postPaperToServer(true);
}

function SaveTitlePageFunction(){
  postPaperToServer(false);
}

function postPaperToServer(completed){
  $$('input[type=text]').each(function(element){
    element.writeAttribute('value', element.value);
    element.writeAttribute('type', "text");  // <----- New code 
  });

  var ptitle = $('title').value;
  $$('textarea').each(function(element){
    element.update(element.value);
   });
  $$('input[type=checkbox]').each(function(element){
    if(element.checked)
      element.writeAttribute('checked', true);
    else
      element.writeAttribute('checked', false);
  });
  $$('input[type=radio]').each(function(element){
    if(element.checked)
      element.writeAttribute('checked', true);
    else
      element.writeAttribute('checked', false);
  });
  
  var all_params = new Hash({ 'authors': nextIndex, 'radio_counter': radioCounter, 'edited_index': editedIndex, 'paper_completed': false});
  if(paperID != -1){
    all_params = all_params.merge({paper_id: paperID});
  }
  for(var i = 0; i < nextIndex; i++){
    if($("Author_"+i+"_email").value == ""){
      alert($("Author_"+i+"_name").value + " does not have an email address. Will not save.");
      return;
    }
    all_params.set("Author_"+i+"_name", $("Author_"+i+"_name").value);
    all_params.set("Author_"+i+"_email", $("Author_"+i+"_email").value);
    all_params.set("Author_"+i+"_academic_title", $("Author_"+i+"_academic_title").value);
  }
  //I would like to use the paper[body], but the proxy doesn't like that very much
  all_params = all_params.merge({'paper_body': escape($('editor-form-body').innerHTML), 'paper_title': ptitle, 'format': 'js'});

  //var url = 'http://paper-saver.local/papers';
  //var url = 'http://paper-saver.local/proxy.php?proxy_url=http://aa_editor.alturndev.com/papers';

  var url = 'http://www.aaeditor.org/proxy.php?proxy_url=http://alturndev.com/papers';
  new Ajax.Request(url, {
    parameters: all_params,
    onCreate: function() {
	$('TitlePageErrors').innerHTML = "<span class='TitlePageBeingSaved'>This may take up to one minute. You can continue your work while your data are being saved. You will see a message after it is finished.</span>";
	$('TitlePageErrors').style.display = "block";
	$('TitlePageErrors').style.border = "none";
	if (IE6){
		window.location.hash="TopMark";
		}
    },
    onSuccess: function(transport){
        var real_response = eval('(' + transport.responseText + ')');
        $('permalink').value = real_response.path;
        paperID = real_response.id;
        var alert_text = "";
        if(real_response.names.length > 0){
          alert_text += "Emails have been sent to: "+real_response.names.join(", ")+".\n\n";
          alert_text += "Your title page has been saved at \n"+real_response.path+". This is included in the email."
		if (completed == true) {
			var corIndex = findCorrespondingAuthor();
			var OtherEmails = "";
			$('Corresponding_Author_Email').value = $('Author_' + corIndex + '_email').value;
			$('Corresponding_Author_Name').value = $('Author_' + corIndex + '_name').value + ", " + $('Author_' + corIndex + '_degree').value;
			for (i = 0; i < nextIndex; i++) {
				if (i !== corIndex) {
					OtherEmails += $('Author_' + i + '_email').value + " ";
				}
			}
			OtherEmails += "editor@anesthesia-analgesia.org";
			$('Other_Authors_Email').value = OtherEmails;
			$('Manuscript_Name').value = $('title').value;
			$('TitlePageHTML').value = $('PaperTitlePage').innerHTML;
			$('TitlePageURL').value = real_response.path;
			var answer = confirm('Your title page has been saved. Emails have been sent to you and your coauthors.\nPlease select "OK" to exit the program, or "Cancel" to return to your work.')
			if (answer) {
				document.EmailForm.submit();
			}
		}
        } else {
          alert_text += "Warning: No emails were sent, because no authors were set.\n\n";
          alert_text += "Your url, which you should copy down, is:\n"+real_response.path;
        }
		$('TitlePageErrors').innerHTML = "<span class='TitlePageBeingSaved'>"+alert_text+"</span>";
		$('TitlePageErrors').style.display = "block";
		$('TitlePageErrors').style.border = "none";
		if (IE6){
			window.location.hash="TopMark";
			}
    },
        onFailure: function(transport){
        alert("There was an error saving your page. Sorry I can't be more specific.");
    }
  });
}

