
function validateForm(formObj)
{
    var form_name = formObj.form_name.value;
    var error = "Submission Error\n\t\t\t\t\t\t\n";
    var error_counter = 1;
    var regex_date = /[0-9]{1,2}\/[0-9]{1,2}\/[0-9]{2,4}/i;
    var regex_email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    var regex_url = /^http([s]?):\/\/([a-zA-Z0-9\.]+)([\/a-zA-Z0-9\.]*)/;
    var allowable_document_extensions = new Array(".htm", ".html", ".doc", ".pdf", ".txt");
    var allowable_image_extensions = new Array(".jpg", ".jpeg", ".gif", ".bmp");
    
    switch(form_name)
	{
	case "resources" :
	    {
		if(formObj.Title.value == "")
		    {
			error += error_counter + ". ";
			error += "A title must be given to this regional resource \n"
			    +  "before it can be added to our database.  Please \n"
			    +  "type the title for the new regional resource in the \n"
			    +  "title box on the form.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.DocDate.value == "")
		    {
			error += error_counter + ". ";
			error += "The creation date for this document is missing from \n"
			    +  "this form.  Please enter in the documents creation date \n"
			    +  "in the space provided for it below.\n\n";
			
			error_counter++;
		    }
		else if(!regex_date.test(formObj.DocDate.value))
		    {
			error += error_counter + ". ";
			error += "The creation date for this document has been \n"
			    +  "incorrectly written. The correct format for \n"
			    +  "the date is dd/mm/yyyy where dd is the day, \n"
			    +  "mm is the month and yyyy is the year. \n\n";
			
			error_counter++;
		    }
		
		if(formObj.DocFileName.value == "" && formObj.action.value != "Update")
		    {
			error += error_counter + ". ";
			error += "A document has not been selected for this new \n"
			    +  "regional resource.  Please click the \"Browse\" \n"
			    +  "button next to the Document box and select a file.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.DocFileName.value != "")
		    {
			var regex_extension = /\.[a-zA-Z]{3,4}/i;
			var ar = regex_extension.exec(formObj.DocFileName.value);
			
			if(ar == null)
			    {
				error += error_counter + ". ";
				error += "The type of document you are trying to upload \n"
				    +  "cannot be detected.  This is due to its lack \n"
				    +  "of file extension.  Please add an extension to \n"
				    +  "the filename or try a different file.\n\n";
				
				error_counter++;
			    }
			else if(!in_array(ar[0], allowable_document_extensions))
			    {
				error += error_counter + ". ";
				error += "The type of document you are trying to upload \n"
				    +  "is not a document that this site will accept. \n"
				    +  "Please contact the owner of this website to get \n"
				    +  "the list of file types that can be uploaded.\n\n";
				
				error_counter++;
			    }
		    }
		
		if(error_counter != 1)
		    {
			alert(error);
		    }
		else
		    {
			formObj.submit();
		    }
		
		break;
	    }
	case "research" :
	    {
		if(formObj.Title.value == "")
		    {
			error += error_counter + ". ";
			error += "A title must be given to this research resource \n"
			    +  "before it can be added to our database.  Please \n"
			    +  "type the title for the new research resource in the \n"
			    +  "title box on the form.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.Authors.value == "")
		    {
			error += error_counter + ". ";
			error += "The respetive author(s) for the new research \n"
			    +  "have not been listed.  Please enter their names \n"
			    +  "in correct bibliographic format.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.Year.value == "")
		    {
			error += error_counter + ". ";
			error += "The year in which the new research document \n"
			    +  "was published has not been given.  Please enter \n"
			    +  "it in the space provided in the form.\n\n";
			
			error_counter++;
		    }
		else
		    {
			now = new Date();
			var regex_year = /[0-9]{4}/i;
			
			if(parseInt(formObj.Year.value) > now.getFullYear()
			   || !regex_year.test(formObj.Year.value))
			    {
				error += error_counter + ". ";
				error += "The year in which the new resource document \n"
				    +  "has been published is invalid.	All publication \n"
				    +  "years should only be up to and including this year \n"
				    +  "and of the format yyyy (e.g., 2002).\n\n";
				
				error_counter++;
			    }
		    }
		
		if(formObj.Citation.value == "")
		    {
			error += error_counter + ". ";
			error += "A citation for the research document has not \n"
			    +  "been given.  Please enter a bibliographically \n"
						  +  "correct citation.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.Abstract.value == "")
		    {
			error += error_counter + ". ";
			error += "An abstract for the research document has not \n"
			    +  "been given.  Please enter a short yet descriptive \n"
			    +  "abstract in the space provided below.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.Keywords.value == "")
		    {
			error += error_counter + ". ";
			error += "Keywords that describe the new research document \n"
			    +  "have not been given.  Please enter the keywords, \n"
			    +  "seperated by commas, in the space provided on the form.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.DocFileName.value != "")
		    {
			var regex_extension = /\.[a-zA-Z]{3,4}/i;
			var ar = regex_extension.exec(formObj.DocFileName.value);
			
			if(ar == null)
			    {
				error += error_counter + ". ";
				error += "The type of document you are trying to upload \n"
				    +  "cannot be detected.  This is due to its lack \n"
				    +  "of file extension.  Please add an extension to \n"
				    +  "the filename or try a different file.\n\n";
				
				error_counter++;
			    }
			else if(!in_array(ar[0], allowable_document_extensions))
			    {
				error += error_counter + ". ";
				error += "The type of document you are trying to upload \n"
				    +  "is not a document that this site will accept. \n"
				    +  "Please contact the owner of this website to get \n"
				    +  "the list of file types that can be uploaded.\n\n";
				
				error_counter++;
			    }
		    }
		
		if(error_counter != 1)
		    {
			alert(error);
		    }
		else
		    {
			formObj.submit();
		    }
		
		break;
	    }
	case "admin_reports" :
	    {
		if(formObj.Title.value == "")
		    {
			error += error_counter + ". ";
			error += "A title must be given to this report \n"
			    +  "before it can be added to our database.  Please \n"
			    +  "type the title for the new report in the \n"
			    +  "title box on the form.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.DocDate.value == "")
		    {
			error += error_counter + ". ";
			error += "The creation date for this document is missing from \n"
			    +  "this form.  Please enter in the documents creation date \n"
			    +  "in the space provided for it below.\n\n";
			
			error_counter++;
		    }
		else if(!regex_date.test(formObj.DocDate.value))
		    {
			error += error_counter + ". ";
			error += "The creation date for this document has been \n"
			    +  "incorrectly written. The correct format for \n"
			    +  "the date is dd/mm/yyyy where dd is the day, \n"
			    +  "mm is the month and yyyy is the year. \n\n";
			
			error_counter++;
		    }
		
		if(formObj.DocFileName.value == "" && formObj.action.value != "Update")
		    {
			error += error_counter + ". ";
			error += "A document has not been selected for this new \n"
			    +  "report.  Please click the \"Browse\" button\n"
			    +  "next to the Document box and select a file.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.DocFileName.value != "")
		    {
			var regex_extension = /\.[a-zA-Z]{3,4}/i;
			var ar = regex_extension.exec(formObj.DocFileName.value);
			
			if(ar == null)
			    {
				error += error_counter + ". ";
				error += "The type of document you are trying to upload \n"
				    +  "cannot be detected.  This is due to its lack \n"
				    +  "of file extension.  Please add an extension to \n"
				    +  "the filename or try a different file.\n\n";
				
						error_counter++;
			    }
			else if(!in_array(ar[0], allowable_document_extensions))
			    {
				error += error_counter + ". ";
				error += "The type of document you are trying to upload \n"
				    +  "is not a document that this site will accept. \n"
				    +  "Please contact the owner of this website to get \n"
				    +  "the list of file types that can be uploaded.\n\n";
				
				error_counter++;
			    }
		    }
		
		if(error_counter != 1)
		    {
			alert(error);
		    }
		else
		    {
			formObj.submit();
		    }
		
		break;
	    }
	case "gallery" :
	    {
		if(formObj.gallery_image.value == "" && formObj.action.value != "Update")
		    {
			error += error_counter + ". ";
			error += "To create a new gallery item, an image needs to be\n"
			    +  "given.	Please click the \"browse\" button next to\n"
			    +  "the file box to find an image, or simply type the name\n"
			    +  "of the file complete with path into the space provided.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.gallery_image.value != "")
		    {
			var regex_extension = /\.(.){3,4}/i;
			var ar = regex_extension.exec(formObj.gallery_image.value);
			
			if(ar == null)
			    {
				error += error_counter + ". ";
				error += "The type of image you are trying to upload \n"
				    +  "cannot be detected.  This is due to its lack \n"
				    +  "of file extension.  Please add an extension to \n"
				    +  "the filename or try a different file.\n\n";
				
				error_counter++;
			    }
			else if(!in_array(ar[0], allowable_image_extensions))
			    {
				error += error_counter + ". ";
				error += "The type of image you are trying to upload \n"
				    +  "is not a image that this site will accept. \n"
				    +  "Please contact the owner of this website to get \n"
				    +  "the list of image types that can be uploaded.\n\n";
				
				error_counter++;
			    }
		    }
		
		if(formObj.Description.value == "")
		    {
			error += error_counter + ". ";
			error += "The description for the new gallery item has not\n"
			    +  "been written in the box provided for it below.\n"
			    +  "Please write a description for this gallery item.\n\n";
			
			error_counter++;
		    }
		
		if(error_counter != 1)
		    {
			alert(error);
		    }
		else
		    {
			formObj.submit();
		    }
		
		break;
	    }
	case "forumlogin" :
	    {
		if(formObj.EmailAddress.value == "")
		    {
			error += error_counter + ". ";
			error += "Please enter an email address to login to the\n"
			    +  "forum.\n\n";
			
			error_counter++;
		    }
		else if(!regex_email.test(formObj.EmailAddress.value))
		    {
			error += error_counter + ". ";
			error += "Please enter a valid email address to login to\n"
			    +  "the system.  Valid email addresses should be of\n"
			    +  "the format username@domain.\n\n";
			
			error_counter++;
		    }
		
		if(error_counter != 1)
		    {
			alert(error);
		    }
		else
		    {
			formObj.submit();
		    }
		
		break;
	    }
	case "forumregister" :
	    {
		if(formObj.Firstname.value == "")
		    {
			error += error_counter + ". ";
			error += "Please enter a firstname in the box provided.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.Surname.value == "")
		    {
			error += error_counter + ". ";
			error += "Please enter a surname in the box provided.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.EmailAddress.value == "")
		    {
			error += error_counter + ". ";
			error += "Please enter an email address to login to the\n"
			    +  "forum.\n\n";
			
			error_counter++;
		    }
		else if(!regex_email.test(formObj.EmailAddress.value))
		    {
			error += error_counter + ". ";
			error += "Please enter a valid email address to login to\n"
			    +  "the system.  Valid email addresses should be of\n"
			    +  "the format username@domain.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.Country.value == "")
		    {
			error += error_counter + ". ";
			error += "Please enter the country from which you are operating\n"
			    +  "from in the box provided.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.Position.value == "")
		    {
			error += error_counter + ". ";
			error += "Please enter your offical position in the box provided.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.password_first)
			{
			if(formObj.password_first.value == "" || formObj.password_second.value == "")
				{
				error += error_counter + ". ";
				error += "Please enter both a password and a confirmation password\n"
					  +  "in the boxes provided.\n\n";
			
				error_counter++;
				}
			else if (formObj.password_first.value != formObj.password_second.value)
				{
				error += error_counter + ". ";
				error += "Passwords don't match.  Please retype your password.\n\n";
			
				error_counter++;
				}
			}

		if(error_counter != 1)
		    {
			alert(error);
		    }
		else
		    {
			formObj.submit();
		    }
		
		break;
	    }
	case "forumadd" :
	    {
		if(formObj.Subject.value == "")
		    {
			error += error_counter + ". ";
			error += "Please enter a subject for this message.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.Message.value == "")
		    {
			error += error_counter + ". ";
			error += "You cannot submit a forum posting without a message.\n"
			    +  "Please enter your message in the space provided for\n"
			    +  "it below.\n\n";
			
			error_counter++;
		    }
		
		if(error_counter != 1)
		    {
			alert(error);
		    }
		else
		    {
			formObj.submit();
		    }
		
		break;
	    }
	case "forumrespond" :
	    {
		if(formObj.Message.value == "")
		    {
			error += error_counter + ". ";
			error += "You cannot submit a forum posting without a message.\n"
			    +  "Please enter your message in the space provided for\n"
			    +  "it below.\n\n";
			
			error_counter++;
		    }
		
		if(error_counter != 1)
		    {
			alert(error);
		    }
		else
		    {
			formObj.submit();
		    }
		
		break;
	    }
	case "admin_forumusers" :
	    {
		if(formObj.Firstname.value == "")
		    {
			error += error_counter + ". ";
			error += "You cannot update a user to have no firstname.\n"
			    +  "Please enter the users firstname in the space provided.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.Surname.value == "")
		    {
			error += error_counter + ". ";
			error += "You cannot update a user to have no surname.\n"
			    +  "Please enter the users surname in the space provided.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.EmailAddress.value == "")
		    {
			error += error_counter + ". ";
			error += "You cannot update a user to have no email address.\n"
			    +  "Please enter the users email address in the space\n"
			    +  "provided.\n\n";
			
			error_counter++;
		    }
		else if(!regex_email.test(formObj.EmailAddress.value))
		    {
			error += error_counter + ". ";
			error += "Please enter a valid email address to update the\n"
			    +  "user.  Valid email addresses should be of\n"
			    +  "the format username@domain.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.Country.value == "")
		    {
			error += error_counter + ". ";
			error += "You cannot update a user to have no country.\n"
			    +  "Please enter the users country in the space provided.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.Position.value == "")
		    {
			error += error_counter + ". ";
			error += "You cannot update a user to have no position.\n"
			    +  "Please enter the users position in the space provided.\n\n";
			
			error_counter++;
		    }
		
		if(error_counter != 1)
		    {
			alert(error);
		    }
		else
		    {
			formObj.submit();
		    }
		
		break;
	    }
	case "admin_forum" :
	    {
		if(formObj.Subject.value == "")
		    {
			error += error_counter + ". ";
			error += "You cannot update a forum posting without a subject.\n"
			    +  "Please enter your subject in the space provided for\n"
			    +  "it.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.Message.value == "")
		    {
			error += error_counter + ". ";
			error += "You cannot update a forum posting without a message.\n"
			    +  "Please enter your message in the space provided for\n"
			    +  "it.\n\n";
			
			error_counter++;
		    }
		
		if(error_counter != 1)
		    {
			alert(error);
		    }
		else
		    {
			formObj.submit();
		    }
		
		break;
	    }
	case "admin_news" :
	    {
		if(formObj.Headline.value == "")
		    {
			error += error_counter + ". ";
			error += "The new news items does not have a headline.	Please\n"
			    +  "enter the headline in the space provided for it.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.Summary.value == "")
		    {
			error += error_counter + ". ";
			error += "This news item has not been given a summary.	Please\n"
			    +  "enter the summary in the space provided for it.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.Article.value == "")
		    {
			error += error_counter + ". ";
			error += "This news item has not been given a article.	Please\n"
			    +  "enter the article in the space provided for it.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.article_image.value == "" && formObj.action.value != "Update")
		    {
			error += error_counter + ". ";
			error += "To create a new news item, an image needs to be\n"
			    +  "given.	Please click the \"browse\" button next to\n"
			    +  "the file box to find an image, or simply type the name\n"
			    +  "of the file complete with path into the space provided.\n\n";

			error_counter++;
		    }
		
		if(formObj.article_image.value != "")
		    {
			var regex_extension = /\.(.){3,4}/i;
			var ar = regex_extension.exec(formObj.article_image.value);
			
			if(ar == null)
			    {
				error += error_counter + ". ";
				error += "The type of image you are trying to upload \n"
				    +  "cannot be detected.  This is due to its lack \n"
				    +  "of file extension.  Please add an extension to \n"
				    +  "the filename or try a different file.\n\n";
				
				error_counter++;
			    }
			else if(!in_array(ar[0], allowable_image_extensions))
			    {
				error += error_counter + ". ";
				error += "The type of image you are trying to upload \n"
				    +  "is not a image that this site will accept. \n"
				    +  "Please contact the owner of this website to get \n"
				    +  "the list of image types that can be uploaded.\n\n";
				
				error_counter++;
			    }
		    }
		
		
		
		if(error_counter != 1)
		    {
			alert(error);
		    }
		else
		    {
			formObj.submit();
		    }
		
		break;
	    }
	case "admin_links" :
	    {
		if(formObj.link_name.value == "")
		    {
			error += error_counter + ". ";
			error += "The name for the website link has not been given.\n"
			    +  "Please enter the name that best describes the url\n"
			    +  "in the space provided.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.link_url.value == "")
		    {
			error += error_counter + ". ";
			error += "The URL for this website link has not been given\n"
			    +  "Please enter the target URL in the space provided.\n\n";
			
			error_counter++;
		    }
		else if(!regex_url.test(formObj.link_url.value))
		    {
			error += error_counter + ". ";
			error += "Please enter a valid URL for the website link.\n\n";
			
			error_counter++;
		    }
		
		if(formObj.link_description.value == "")
		    {
			error += error_counter + ". ";
			error += "The description for the website link has not been given.\n"
			    +  "Please enter the description that best describes the link\n"
			    +  "in the space provided.\n\n";
			
			error_counter++;
		    }
		
		
		if(error_counter != 1)
		    {
			alert(error);
		    }
		else
		    {
			formObj.submit();
		    }
		
		break;
	    }
	case "admin_tips" :
	    {
		if(formObj.tip.value == "")
		    {
			error += error_counter + ". ";
			error += "You have not supplied a tip.	Please enter an\n"
			    +  "appropriate message in the space provided.\n\n";
			
			error_counter++;
		    }
		
		if(error_counter != 1)
		    {
			alert(error);
		    }
		else
		    {
			formObj.submit();
		    }
		
		break;
	    }
	case "admin_contacts" :
	    {
		if(formObj.contact_country.value == "")
		    {
			error += error_counter + ". ";
			error += "You have not supplied a country.  Please enter\n"
			    +  "the country in the space provided.\n\n";

			error_counter++;
		    }

		if(error_counter != 1)
		    {
			alert(error);
		    }
		else
		    {
			formObj.submit();
		    }

		break;
	    }
	default :
	    {
		alert("Cannot validate form.  Form name unknown.");
		break;
	    }
	}
}

function in_array(needle, haystack)
{
    for(var i = 0; i < haystack.length; i++)
	{
	    if(haystack[i] == needle.toLowerCase())
		{
		    return(1);
		}
	}
    return(0);
}

