//	Performs the validation of the Living History form (used for contributing)
//	Must be included after the communfunctions.js file.
//
//	Returns:
//	false:	If any validation fails.
//	true:	If validation passes.
//
//	20041021. SS. TPR 257. Changed email validation message.

function validateLivingHistoryContribute()
{
	var form = document.contributeform;
	var formtitle = form.title;
	var formtextfile;
	var formimage1;
	var formimage2;
	var formimage3;
	var formname = form.name;
	var formemail = form.email;
	var formphone = form.phone;
	var bReturn = true;
	var bEmpty = false;
	var ext;
	var extArray;
	var allowSubmit;
	var sErrorMessage = "";
	var bIsNav = true;
	
	bIsNav = isNav4();
	if (bIsNav) {
 		return true;
	}
	
	// TITLE VALIDATION
	// Determine if value is entered
	bEmpty = isEmptyNoSpace(formtitle);

	if(bEmpty)
	{
		bReturn = false;
		alert("I'm sorry, you cannot complete this form unless all the "
				+ "mandatory field information (marked with a red asterix) is complete."
				+" Please enter your contribution title.");
		chgFocus(formtitle);
	} // End if
	


	// Determine if valid length
	if(bReturn)
	{
		bReturn = isValidLength (formtitle, 100);
		if(!bReturn)
		{
			bReturn = true;
		}
		else
		{
			alert ("I'm sorry you have supplied too many letters or numerals "
					+ "for the title. Please try again.");
			bReturn = false;
		}
	}


	
	// Determine if title field contains invalid characters 
	if(bReturn)
	{
		sErrorMessage = validateName(formtitle);
		
		bReturn = (sErrorMessage=="");
		if(!bReturn)
		{
			alert(sErrorMessage);
			chgFocus(formtitle);
		} // End if
	} // End if	

	// NAME VALIDATION
	// Determine if value is entered
	bEmpty = isEmptyNoSpace(formname);

	if(bEmpty && bReturn)
	{
		bReturn = false;
		alert("I'm sorry, you cannot complete this form unless all the "
				+ "mandatory field information (marked with a red asterix) is complete."
				+" Please enter your name.");
		chgFocus(formname);
	} // End if
	
	// Determine if valid length
	if(bReturn)
	{
		bReturn = isValidLength (formname, 80);
		if(!bReturn)
		{
			bReturn = true;
		}
		else
		{
			alert ("I'm sorry you have supplied too many letters or numerals "
					+ "for your name. Please try again.");
			bReturn = false;
		}
	}
	
	// Determine if name field contains invalid characters 
	if(bReturn)
	{
		sErrorMessage = validateName(formname);
		bReturn = (sErrorMessage=="");
		
		if(!bReturn)
		{
			alert(sErrorMessage);
			chgFocus(formname);
		} // End if
	} // End if
	
	// EMAIL VALIDATION
	// Validate email field
	bEmpty = isEmptyNoSpace(formemail);

	if(bEmpty && bReturn)
	{
		bReturn = false;
		alert("I'm sorry, you cannot complete this form unless all the "
				+ "mandatory field information (marked with a red asterix) is complete."
				+" Please enter your email address.");
		chgFocus(formemail);
	} // End if
	
	if(bReturn) 
	{

		sErrorMessage = validateEmail(formemail);

		if(sErrorMessage != "")
		{
			alert (sErrorMessage);
			bReturn = false;
			chgFocus(formemail);
		 }	
	}
	
	
	// PHONE VALIDATION
	 bEmpty = isEmptyNoSpace(formphone);

	 if(!bEmpty && bReturn)
	 {
	 // Determine if valid length

			bReturn = isValidLength (formphone, 20);
			
			if(!bReturn)
			{
				bReturn = true;
			}
			else
			{
				alert ("I'm sorry you have supplied too many letters or numerals "
					+ "for the phone number. Please try again.");
				bReturn = false;
			}

		
		// Determine if valid phone number format
	 	if(bReturn)
		{	
			var phonemsg = isPhone(formphone);
			if (phonemsg != "") {
				bReturn = false;
				alert(phonemsg);
				chgFocus(formphone)
			}
		}
	 }
	 
	// VALIDATE TEXT FIELD
	
	if(bReturn)
	{

		formtextfile = document.getElementById("fd17file").value;

		if (bReturn){
		
			extArray = new Array(".txt", ".doc", ".rtf");
			
			if ( (!formtextfile) || (formtextfile == "") || (formtextfile == null) )
			{
				bReturn = false;
				alert ("I'm sorry, you must select a Text file (.txt), Word document (.doc) or Rich Text File (.rtf) containing the story you wish to contribute to living history.");
			}
		}
		
		if(bReturn)
		{
			while (formtextfile.indexOf("\\") != -1)
			formtextfile = formtextfile.slice(formtextfile.indexOf("\\") + 1);
			ext = formtextfile.slice(formtextfile.lastIndexOf(".")).toLowerCase();
			
			for (var i = 0; i < extArray.length; i++)
			{
				if (extArray[i] == ext)
				{
					allowSubmit = true;
					break;
				}
			}
			
			if (!allowSubmit)
			{	
				bReturn = false;
				alert ("I'm sorry the file format you have attached for your "
					+ "contribution is not used on seniors.gov.au. Please reformat your file as "
					+ "a Text file (.txt), Word document (.doc) or "
					+ "Rich Text File (.rtf) and try again.");
			}
		}
	}
	
	// VALIDATE IMAGE FIELD 1
	formimage1 = document.getElementById("fd17image1").value;
	
	if((formimage1 != null)&&(formimage1 != ""))
	{
		extArray = new Array(".png", ".gif", ".jpg", ".jpeg");
		
		if(bReturn)
		{
			while (formimage1.indexOf("\\") != -1)
			formimage1 = formimage1.slice(formimage1.indexOf("\\") + 1);
			ext = formimage1.slice(formimage1.lastIndexOf(".")).toLowerCase();
			allowSubmit = false;
			
			for (var i = 0; i < extArray.length; i++)
			{
				if (extArray[i] == ext)
				{
					allowSubmit = true;
					break;
				}
			}
			
			if (!allowSubmit)
			{	
				bReturn = false;
				alert ("I'm sorry the file format you have attached for your "
					+ "contribution is not used on seniors.gov.au. Please reattach as "
					+ "a file of the following format: .jpg, .jpeg, .gif or .png and try again.");
			}
		}
	}
	
		// VALIDATE IMAGE FIELD 2
	formimage2 = document.getElementById("fd17image2").value;	
	
	if((formimage2 != null)&&(formimage2 != ""))
	{
		extArray = new Array(".png", ".gif", ".jpg", ".jpeg");
		
		if(bReturn)
		{
			while (formimage2.indexOf("\\") != -1)
			formimage2 = formimage2.slice(formimage2.indexOf("\\") + 1);
			ext = formimage2.slice(formimage2.lastIndexOf(".")).toLowerCase();
			allowSubmit = false;
			
			for (var i = 0; i < extArray.length; i++)
			{
				if (extArray[i] == ext)
				{
					allowSubmit = true;
					break;
				}
			}
			
			if (!allowSubmit)
			{	
				bReturn = false;
				alert ("I'm sorry the file you have attached for your contribution does not have a valid file name."
					+ "Please attach a file of the following format: .jpg, .jpeg, .gif or .png and try again.");
			}
		}
	}
	
		// VALIDATE IMAGE FIELD 3
	formimage3 = document.getElementById("fd17image3").value;
	
	if((formimage3 != null)&&(formimage3 != ""))
	{
		extArray = new Array(".png", ".gif", ".jpg", ".jpeg");
		
		if(bReturn)
		{
			while (formimage3.indexOf("\\") != -1)
			formimage3 = formimage3.slice(formimage3.indexOf("\\") + 1);
			ext = formimage3.slice(formimage3.lastIndexOf(".")).toLowerCase();
			allowSubmit = false;
			
			for (var i = 0; i < extArray.length; i++)
			{
				if (extArray[i] == ext)
				{
					allowSubmit = true;
					break;
				}
			}
			
			if (!allowSubmit)
			{	
				bReturn = false;
				alert ("I'm sorry the file format you have attached for your "
					+ "contribution is not used on seniors.gov.au. Please reattach as "
					+ "a file of the following format: .jpg, .jpeg, .gif or .png and try again.");
			}
		}
	}
	
	// FINAL VARIABLE RETURN
	return bReturn;
	
	
} // End validateLivingHistoryContributionForm
