var POST = 0;
var MODIFY = 1;

function modify_check()
{
	if(title_check() && folder_check(MODIFY))
		return true;
	else
		return false;
}

function submit_check()
{
	if(title_check() && folder_check(POST) && document_check() && set_icon(POST))
		return true;
	else
		return false;
}
function title_check()
{
	// check that a title has been added
	if (this.document.forms[0].title.value == "")
	{
		alert("You must enter a title for the document.");
		return false;
	}
	else
		return true;
}
function pub_check()
{
	if(title_check() && pub_folder_check(POST)&& document_check() && set_icon(POST))
		return true;
	else
		return false;
}
function link_check()
{
	if(title_check() && valid_link(POST) && folder_check())
		return true;
	else
		return false;
}
function valid_link(action)
{
	if (action == POST)
		this.document.forms[0].action = "/links/output.chtml"; 
	else if (action == MODIFY)
		this.document.forms[0].action = "/links/modify-output.chtml"; 
				
	if ((this.document.forms[0].document_link.value != "") && (this.document.forms[0].document_link.value != "http://"))
	{
		this.document.forms[0].document_link.name = "document";
		this.document.forms[0].type.value = "Link";
		return true;
	}
	else
	{
		alert("Please enter a link");
		return false;
	}
}
	
function folder_check(action)
{
	this_value = this.document.forms[0].folder.value;
	// check that a valid folder has been selected
	if (this_value == "")
	{
		alert("You have not selected a valid folder. Please select another.");
		return false;
	}
	else
	{
	
		// change the action of form depending on whether folder is Public or Member
		if ((this_value == "consumer") ||		// public folders
				(this_value == "IHP_documents") ||
				(this_value == "IHP_minutes") ||
				(this_value == "well_life") ||
				(this_value == "prime_time") ||
				(this_value == "physical_activity") ||
				(this_value == "mental_wellbeing") ||
				(this_value == "service_coordination") ||
				(this_value == "SCG_documents") ||
				(this_value == "SCG_minutes") ||
				(this_value == "practitioner") ||
				(this_value == "regional") ||
				(this_value == "SCG_other") ||
				(this_value == "information_systems") ||
				(this_value == "IS_working_group") ||
				(this_value == "training_development") ||
				(this_value == "partnership") ||
				(this_value == "early_intervention") ||
				(this_value == "consumer") ||
				(this_value == "hypertension_project") ||
				(this_value == "falls_prevention_project") ||
				(this_value == "community_health") ||
				(this_value == "SPG_documents") ||
				(this_value == "SPG_minutes") ||
				(this_value == "OMG_documents") ||
				(this_value == "OMG_minutes") ||
				(this_value == "health_promotion") ||
				(this_value == "policy") ||
				(this_value == "progress_reports") ||
				(this_value == "protocols") ||
				(this_value == "reports_papers") ||
				(this_value == "service_planning") ||
				(this_value == "PMG_agenda") ||
				(this_value == "PMG_documents") ||
				(this_value == "PMG_minutes"))
		{
			if (action == POST)
				this.document.forms[0].action = "/publications/output.chtml"; //public
			else if (action == MODIFY)
				this.document.forms[0].action = "/publications/modify-output.chtml"; //public
		return true;
		}
	}
}
function pub_folder_check(action)
{
	this_value = this.document.forms[0].folder.value;
	// check that a valid folder has been selected
	if (this_value == "")
	{
		alert("You have not selected a valid folder. Please select another.");
		return false;
	}
	else
	{
		// change the action of form depending on whether folder is Public or Member
		if ((this_value == "privacy") ||		// public folders
				(this_value == "health_promotion_publications") ||
				(this_value == "service_coordination_publications") ||
				(this_value == "general_publications"))
				
		{
			if (action == POST)
				this.document.forms[0].action = "/publications/output.chtml"; //public
			else if (action == MODIFY)
				this.document.forms[0].action = "/publications/modify-output.chtml"; //public
		return true;
	}
	}
}
function document_check()
{
	// determine if this is a link or a file being submitted
	if (this.document.forms[0].document_file.value != "")
	{
		this.document.forms[0].document_file.name = "document";
		this.document.forms[0].document_link.value = "";
		this.document.forms[0].type.value = this.document.forms[0].type_select.value;
		this.document.forms[0].type_select.value = "";
		return true;
	}
	else if ((this.document.forms[0].document_link.value != "") && (this.document.forms[0].document_link.value != "http://"))
	{
		this.document.forms[0].document_link.name = "document";
		this.document.forms[0].type.value = "Link";
		this.document.forms[0].type_select.value = "";
		return true;
	}
	else if (this.document.forms[0].document_link.value != "http://")
	{
		this.document.forms[0].document_link.value = "";
	}
	else
	{	
		alert("You have not uploaded a document or specified a link.");
		return false;
	}
}

function set_icon(action)
{ 
	var thefile;
	var splitstr;
	var xten;
	var thefile;
	if (action == POST)
		thefile = this.document.forms[0].document_file.value;
	else
		thefile = this.document.forms[0].document.value;
	
	if(this.document.forms[0].type.value == "Link") // Item is a link, set icon to html.gif
	{
		this.document.forms[0].icon.value = "/publications/icons/html.gif";
		return true;
	}
	else if(thefile == "") // No (new) document, return true
	{
		return true;
	}
	else // New document, reset icon
	{
		if(thefile[thefile.length-1] == "/") 
		{ 
			thefile += "index.shtml"; 
		} 
		splitstr = thefile.split(".");
		xten = splitstr[splitstr.length - 1];
		xten = xten.toLowerCase();
		if(xten=="htm")  
		{ 
			xten = "html";
		} 
		if(xten=="jpeg")  
		{ 
			xten = "jpg"; 
		} 
		if(xten=="rtf")  
		{ 
			xten = "doc"; 
		} 
		if ((xten=="doc") || 
				(xten=="html") || 
				(xten=="pdf") || 
				(xten=="jpg") || 
				(xten=="gif") ||  
				(xten=="ppt") || 
				(xten=="pub") ||  
				(xten=="xls") || 
				(xten=="zip"))  
		{  
			this.document.forms[0].icon.value = '/publications/icons/' + xten + '.gif'; 
			return true; 
		} 
		else 
		{ 
			alert('Sorry, files with extensions, .'+xten+', cannot be accepted\nAcceptable types are htm, html, doc, xls, pdf, jpeg, jpg, gif, rtf, ppt, pub or zip');
			thefile="";
			return false; 
		}
	}
}
function replace(String) {

  var re = /\n/g
return String.replace(re,"<br>");
}
function spw(url) {
  var NewWin = window.open(url,"NewWin","toolbar=no,location=no,directories=no,menubar=no,status=yes,scrollbars=yes,resizable=yes,width=680,height=465");
  NewWin.moveTo("10", "10");
  NewWin.focus();
}
function spwc(url) {
  openthis = opener;
  if(openthis == null)  {
      openthis = self;
  }  else  {
      self.close();
  }
  openthis.location.href = url;
}
