var POST = 0;
var MODIFY = 1;

function modify_check()
{
	if(title_check() && folder_check(MODIFY) && set_icon(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].item.value == "")
	{
		alert("You must enter a title for the document.");
		return false;
	}
	else
		return true;
}

function folder_check(action)
{
	this_value = this.document.forms[0].category.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 == "General Information") ||		// public folders
				(this_value == "Issues") ||
				(this_value == "Media") ||
				(this_value == "Mental Health Week") ||
				(this_value == "Topic 1") ||
				(this_value == "Topic 2") ||
				(this_value == "Topic 3") ||
				(this_value == "Topic 4") ||
				(this_value == "Topic 5") ||
				(this_value == "Topic 6"))
		{*/
			if (action == POST)
				this.document.forms[0].action = "/equipment/output.chtml"; //public
			else if (action == MODIFY)
				this.document.forms[0].action = "/equipment/modify-output.chtml"; //public
		/*}
		else if  ((this_value == "Funding/Submissions/Applications") ||			// member folders
							(this_value == "Legislation") ||
							(this_value == "Reports & Papers") ||
							(this_value == "Projects & Services") ||
							(this_value == "Agency Toolkit") ||
							(this_value == "Agency News") ||
							(this_value == "The Queensland Alliance") ||
							(this_value == "Mental Health Association") ||
							(this_value == "Miscellaneous") ||
							(this_value == "Regional Development") ||
							(this_value == "Community Relations") ||
							(this_value == "Activities"))
		{
			if (action == POST)
				this.document.forms[0].action = "/library/member/output.chtml"; //public
			else if (action == MODIFY)
				this.document.forms[0].action = "/library/member/modify-output.chtml"; //public
		}
		else
		{
			alert("Sorry, you seem to have selected an unrecognised valid folder. Please select another.");
			return false;
		}*/
		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
	{	
		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.html"; 
		} 
		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 = '/equipment/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; 
		}
	}
}

