var POST = 0;
var MODIFY = 1;

function modify_check()
{
	if(title_check() && document_check() && set_icon(MODIFY))
		return true;
	else
		return false;
}
function member_modify_check()
{
	if(title_check() && folder_check2(MODIFY) && set_icon(MODIFY))
		return true;
	else
		return false;
}
function submit_check()
{
	if(title_check() && set_icon(POST))
		return true;
	else
		return false;
}
function member_check()
{
	if(title_check() && folder_check2(POST) && document_check() && set_icon(POST))
		return true;
	else
		return false;
}
function title_check()
{
	// check that a title has been added
	if (this.document.resource_post.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))
		return true;
	else
		return false;
}
function link_modify()
{
	if(title_check() && valid_link(MODIFY))
		return true;
	else
		return false;
}	
function folder_check(action)
{
	var status = 0;
		// change the action of form depending on whether folder is Public or Member
        if(this.document.forms[0].Proforma.checked == true)
		{
			status = 1;
		}
		if(this.document.forms[0].Research.checked == true)
		{
			status = 1;
		}
		if(this.document.forms[0].Partnerships.checked == true)
		{
			status = 1;
		}
		if(this.document.forms[0].Evaluation.checked == true)
		{
			status = 1;
		}
		if(this.document.forms[0].region.options[this.document.forms[0].region.selectedIndex].value != "")
			status = 1;
		if (status == 0)
                {
                        alert("Please select a Folder.");
                        return false;
                }
		this.document.forms[0].action = "/resources/public/output.chtml"; //public
    return true;
}
function modify_folder_check(action)
{
	var status = 0;
		// change the action of form depending on whether folder is Public or Member
	        if(this.document.forms[0].Proforma.checked == true)
		{
			status = 1;
		}
		if(this.document.forms[0].Proforma.checked == false)
    {
      this.document.forms[0].Proforma.checked = true;
      this.document.forms[0].Proforma.value="";
    }
		if(this.document.forms[0].Research.checked == true)
		{
			status = 1;
		}
		if(this.document.forms[0].Research.checked == false)
    {
      this.document.forms[0].Research.checked = true;
      this.document.forms[0].Research.value="";
    }
		if(this.document.forms[0].Partnerships.checked == true)
		{
			status = 1;
		}
		if(this.document.forms[0].Partnerships.checked == false)
    {
      this.document.forms[0].Partnerships.checked = true;
      this.document.forms[0].Partnerships.value="";
    }
		if(this.document.forms[0].Evaluation.checked == true)
		{
			status = 1;
		}
		if(this.document.forms[0].Evaluation.checked == false)
    {
      this.document.forms[0].Evaluation.checked = true;
      this.document.forms[0].Evaluation.value="";
    }
		if(this.document.forms[0].region.options[this.document.forms[0].region.selectedIndex].value != "")
			status = 1;
		if (status == 0)
    {
            alert("Please select a Folder.");
            this.document.forms[0].Proforma.checked = false;
            this.document.forms[0].Research.checked = false;
            this.document.forms[0].Partnerships.checked = false;
            this.document.forms[0].Evaluation.checked = false;
            return false;
    }
		this.document.forms[0].action = "/resources/public/modify-output.chtml"; //public
    return true;
}
function folder_check2(action)
{
	var status = 0;	
		if(this.document.forms[0].Website.checked == false)
		{
			this.document.forms[0].Website.checked = true;
			this.document.forms[0].Website.value = '';
			status = 2;
		}
		if(this.document.forms[0].Future.checked == false)
		{
			this.document.forms[0].Future.checked = true;
			this.document.forms[0].Future.value = '';
			status = 2;
		}
		if(this.document.forms[0].Outcome.checked == false)
		{
			this.document.forms[0].Outcome.checked = true;
			this.document.forms[0].Outcome.value = '';
			status = 2;
		}
		if(this.document.forms[0].Development.checked == false)
		{
			this.document.forms[0].Development.checked = true;
			this.document.forms[0].Development.value = '';
			status = 2;
		}
		if (status == 0)
                {
                        alert("Sorry, you seem to have selected an unrecognised folder. Please select another.");
                        return false;
                }
		if (action == POST)
                        	this.document.forms[0].action = "/resources/members/output.chtml"; //public
                else if (action == MODIFY)
                        	this.document.forms[0].action = "/resources/members/modify-output.chtml";
                return true;
}

function document_check()
{
	// determine if this is a link or a file being submitted
	if (this.document.resource_post.document.value != "")
	{	
		this.document.resource_post.link.value = "";
		this.document.resource_post.type.value = "none";
		this.document.resource_post.document.name = this.document.resource_post.document.name;
		return true;
	}
	else if ((this.document.resource_post.link.value != "") && (this.document.resource_post.link.value != "http://"))
	{	
		this.document.resource_post.type.value = "Link";
		this.document.resource_post.link.name = this.document.resource_post.document.name;
		return true;
	}
	else if (this.document.resource_post.document.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.resource_post.document.value;
	else
		thefile = this.document.resource_post.document.value;
	if(this.document.resource_post.type.value == "Link") // Item is a link, set icon to html.gif
	{
		this.document.resource_post.icon.value = "/resources/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.resource_post.icon.value = '/resources/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;
}
