String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function validateArticleForm()
{
	var titleBox = document.getElementById('news_title');
	var contentBox = document.getElementById('news_content');
	var categoryBox = document.getElementById('news_category');
	var idBox = document.getElementById('news_id');
	
	if(titleBox==null || contentBox==null || categoryBox==null || idBox==null)
		return false;
		
	var errors = "";
	
	if(titleBox.value.trim().length==0)
		errors += "You must enter a title.\n";
	if(contentBox.value.trim().length==0)
		errors += "You must enter some content.\n";
	if(categoryBox.value==-1)
		errors += "You must select a category.\n";
		
	if(errors=="")
		return true;
		
	alert("You did not fill out the form properly:\n\n" + errors);
	return false;
}

function validatePeopleForm()
{
	var nameBox = document.getElementById('people_name');
	var emailBox = document.getElementById('people_email');
	var instituteBox = document.getElementById('people_institution');
	var typeBox = document.getElementById('people_type');
	var idBox = document.getElementById('people_id');
	
	if(nameBox==null || emailBox==null || instituteBox==null || typeBox==null || idBox==null)
		return false;
		
	var errors = "";
	
	if(nameBox.value.trim().length==0)
		errors += "You must enter a name.\n";
	if(emailBox.value.trim().length==0)
		errors += "You must enter an email address.\n";
	if(instituteBox.value.trim().length==0)
		errors += "You must enter an institute.\n";
	if(typeBox.selectedIndex<0)
		errors += "You must select a category.\n";
		
	if(errors=="")
		return true;
		
	alert("You did not fill out the form properly:\n\n" + errors);
	return false;
}

function validatePublicationForm()
{
	var authorsBox = document.getElementById('news_authors');
	var yearBox = document.getElementById('news_year');
	var titleBox = document.getElementById('news_title');
	var journalBox = document.getElementById('news_journalname');
	var volumeBox = document.getElementById('news_volume');
	var pageBox = document.getElementById('news_pagenumber');
	
	if(authorsBox==null || yearBox==null || titleBox==null || journalBox==null || volumeBox==null || pageBox==null)
		return false;
		
	var errors = "";
	
	if(authorsBox.value.trim().length==0)
		errors += "You must enter the author(s).\n";
	if(yearBox.value.trim().length==0)
		errors += "You must enter the year.\n";
	if(titleBox.value.trim().length==0)
		errors += "You must enter the title.\n";
	if(journalBox.value.trim().length==0)
		errors += "You must enter the journal name.\n";
	if(volumeBox.value.trim().length==0)
		errors += "You must enter the volume.\n";
	if(pageBox.value.trim().length==0)
		errors += "You must enter the page number.\n";
		
	if(errors=="")
		return true;
		
	alert('You did not fill out the form properly:\n\n' + errors);
	return false;
}

function validateMediaCoverageForm()
{
	var dateBox = document.getElementById('item_date');
	var contentBox = document.getElementById('item_content');
	var typeBox = document.getElementById('item_type');
	
	if(dateBox==null || contentBox==null || typeBox==null)
		return false;
		
	var errors = "";
	
	if(dateBox.value.trim().length==0)
		errors += "You must pick the date.\n";
	if(contentBox.value.trim().length==0)
		errors += "You must enter the content.\n";
	
	if(errors=="")
		return true;
		
	alert('You did not fill out the form properly:\n\n' + errors);
	return false;
}

function doPublication()
{
	var contentBox = document.getElementById('news_content');
	var authorsBox = document.getElementById('news_authors');
	var yearBox = document.getElementById('news_year');
	var titleBox = document.getElementById('news_title');
	var journalBox = document.getElementById('news_journalname');
	var volumeBox = document.getElementById('news_volume');
	var pageBox = document.getElementById('news_pagenumber');
	
	if(contentBox==null)
		return;
		
	contentBox.value += authorsBox==null ? "\n" : authorsBox.value + "\n";
	contentBox.value += yearBox==null ? "\n" : yearBox.value + "\n";
	contentBox.value += journalBox==null ? "\n" : journalBox.value + "\n";
	contentBox.value += volumeBox==null ? "\n" : volumeBox.value + "\n";
	contentBox.value += pageBox==null ? "\n" : pageBox.value + "\n";
}