// JavaScript Document



function limitText(field, maxlimit)
{
	if (field.value.length > maxlimit) // if too long...trim it
	{
		field.value = field.value.substring(0, maxlimit);
	}
	else
	{
	//do nothing	
	}
}



function validateReqForm()
{

	var title = document.getElementById("title").value;
	var description = document.getElementById("description").value;
	var date_cap = document.getElementById("date_cap").value;
	var constraints = document.getElementById("constraints").value;
	var concepts = document.getElementById("concepts").value;
	var shortfalls = document.getElementById("shortfalls").value;
	var procurement = document.getElementById("procurement").value;
	var contacts = document.getElementById("contacts").value;
	
var wrongCharacters=/^[0-9a-zA-Z\s.,!?@();:+_-]+$/;

	//check if all required boxes are filled
	if(title=="" || description=="" || date_cap=="" || contacts=="")
	{
		alert("Please fill out all required fields");
	}
	else
	{
		constraints = constraints + " ";
		concepts = concepts + " ";
		shortfalls = shortfalls + " ";
		procurement = procurement + " ";
		//check if malicious characters are present
if(wrongCharacters.test(title)==false || wrongCharacters.test(description)==false || wrongCharacters.test(date_cap)==false || wrongCharacters.test(constraints)==false || wrongCharacters.test(concepts)==false || wrongCharacters.test(shortfalls)==false || wrongCharacters.test(procurement)==false || wrongCharacters.test(contacts)==false)
		{
			alert('Please only use alphanumeric characters and the symbols .,!?@();:+_-');
		}
		else
		{
			document.forms[1].submit();
		}
	}
}



function validateForumForm()
{

	var title = document.getElementById("title").value;
	var description = document.getElementById("description").value;
	
	var wrongCharacters=/^[0-9a-zA-Z\s.,!?@();:+_-]+$/;

	//check if all required boxes are filled
	if(title=="" || description=="")
	{
		alert("Please fill out all required fields");
	}
	else
	{
		//check if malicious characters are present
if(wrongCharacters.test(title)==false || wrongCharacters.test(description)==false)
		{
			alert('Please only use alphanumeric characters and the symbols .,!?@();:+_-');
		}
		else
		{
			document.forms[1].submit();
		}
	}
}

function validateForumChangeForm()
{


	var description = document.getElementById("description").value;
	
	var wrongCharacters=/^[0-9a-zA-Z\s.,!?@();:+_-]+$/;

	//check if all required boxes are filled
	if(description=="")
	{
		alert("Please fill out all required fields");
	}
	else
	{
		//check if malicious characters are present
if(wrongCharacters.test(description)==false)
		{
			alert('Please only use alphanumeric characters and the symbols .,!?@();:+_-');
		}
		else
		{
			document.forms[1].submit();
		}
	}
}



function validateCommentForm()
{

	var comment = document.getElementById("comment").value;
	
	var wrongCharacters=/^[0-9a-zA-Z\s.,!?@();:+_-]+$/;

	//comment may be empty
		comment = comment + " ";
		//check if malicious characters are present
if(wrongCharacters.test(comment)==false)
		{
			alert('Please only use alphanumeric characters and the symbols .,!?@();:+_-');
		}
		else
		{
			document.forms[1].submit();
		}

}



function validateRegistrationForm()
{

	var title = document.getElementById("title").value;
	var firstname = document.getElementById("firstname").value;
	var lastname = document.getElementById("lastname").value;
	var email = document.getElementById("email").value;
	var organization = document.getElementById("organization").value;
	var department = document.getElementById("department").value;
	var position = document.getElementById("position").value;
	var street = document.getElementById("street").value;
	var city = document.getElementById("city").value;
	var zip = document.getElementById("zip").value;
	var phone = document.getElementById("phone").value;
	var fax = document.getElementById("fax").value;
	
	var wrongCharacters=/^[0-9a-zA-Z\s.,!?@();:+_-]+$/;

	//check if all required boxes are filled
	if(title=="" || firstname=="" || lastname=="" || email=="" || organization=="" || street=="" || city=="" || zip=="" || phone=="")
	{
		alert("Please fill out all required fields");
	}
	else
	{
		department = department + " ";
		position = position + " ";
		fax = fax + " ";
		//check if malicious characters are present
if(wrongCharacters.test(title)==false || wrongCharacters.test(firstname)==false || wrongCharacters.test(lastname)==false || wrongCharacters.test(email)==false || wrongCharacters.test(organization)==false || wrongCharacters.test(department)==false || wrongCharacters.test(position)==false || wrongCharacters.test(street)==false || wrongCharacters.test(city)==false || wrongCharacters.test(zip)==false || wrongCharacters.test(phone)==false || wrongCharacters.test(fax)==false)
		{
			alert('Please only use alphanumeric characters and the symbols .,!?@();:+_-');
		}
		else
		{
			document.forms[1].submit();
		}
	}
}



function validateProposalForm()
{

	var title = document.getElementById("title").value;
	var description = document.getElementById("description").value;
	var approach = document.getElementById("approach").value;
	var tasks = document.getElementById("tasks").value;
	var summary = document.getElementById("summary").value;
	var rom = document.getElementById("rom").value;
	var deliverables = document.getElementById("deliverables").value;
	var contacts = document.getElementById("contacts").value;
	
	var wrongCharacters=/^[0-9a-zA-Z\s.,!?@();:+_-]+$/;

	//check if all required boxes are filled
	if(title=="" || description=="" || approach=="" || summary=="" || rom=="" || deliverables=="" || contacts=="")
	{
		alert("Please fill out all required fields");
	}
	else
	{
		tasks = tasks + " ";
		//check if malicious characters are present
if(wrongCharacters.test(title)==false || wrongCharacters.test(description)==false || wrongCharacters.test(approach)==false || wrongCharacters.test(tasks)==false || wrongCharacters.test(summary)==false || wrongCharacters.test(rom)==false || wrongCharacters.test(deliverables)==false || wrongCharacters.test(contacts)==false)
		{
			alert('Please only use alphanumeric characters and the symbols .,!?@();:+_-');
		}
		else
		{
			document.forms[1].submit();
		}
	}
}

function validateForgotPasswordForm()
{

	var email = document.getElementById("email").value;

	
	var wrongCharacters=/^[0-9a-zA-Z\s.,!?@();:+_-]+$/;

	//check if all required boxes are filled
	if(email=="")
	{
		alert("Please fill out all required fields");
	}
	else
	{
		//check if malicious characters are present
if(wrongCharacters.test(email)==false)
		{
			alert('Please only use alphanumeric characters and the symbols .,!?@();:+_-');
		}
		else
		{
			document.forms[1].submit();
		}
	}
}



function validatePwd()
{
	var old = document.getElementById("oldpass").value;
	var newone = document.getElementById("newpassone").value;
	var newtwo = document.getElementById("newpasstwo").value;
	var wrongCharacters=/^[0-9a-zA-Z\s.,!?@();:+_-]+$/;
	//Check if all boxes are filled
	if(old=="" || newone=="" || newtwo=="")
	{
		alert("Please fill out all boxes");
	}
	else
	{
		//check if malicious characters are present
		if(wrongCharacters.test(old)==false || wrongCharacters.test(newone)==false || wrongCharacters.test(newtwo)==false)
		{
			alert('Please only use alphanumeric characters and the symbols .,!?@();:+_-');
		}
		else
		{
			//Initialise variables
			var errorMsg = "";
			var space  = " ";
			fieldvalue  = newone;
			fieldlength = fieldvalue.length; 
 
			//It must not contain a space
			if (fieldvalue.indexOf(space) > -1)
			{
				errorMsg += "\nPasswords cannot include a space.\n";
			}     
			//It must contain at least one number character
			if (!(fieldvalue.match(/\d/)))
			{
				errorMsg += "\nStrong passwords must include at least one number.\n";
			}
			//It must start with at least one letter     
			if (!(fieldvalue.match(/^[a-zA-Z]+/)))
			{
     			errorMsg += "\nStrong passwords must start with at least one letter.\n";
			}
			//It must contain at least one upper case character     
			if (!(fieldvalue.match(/[A-Z]/)))
			{
 			    errorMsg += "\nStrong passwords must include at least one uppercase letter.\n";
			}
			//It must contain at least one lower case character
			if (!(fieldvalue.match(/[a-z]/)))
			{
     			errorMsg += "\nStrong passwords must include one or more lowercase letters.\n";
			}

			//It must be at least 8 characters long.
			if (!(fieldlength >= 8))
			{
     			errorMsg += "\nStrong passwords must be at least 8 characters long.\n";
			}
			//If there is aproblem with the form then display an error
     		if (errorMsg != "")
			{
				msg = "______________________________________________________\n\n";
				msg += "Please correct the following problem(s) with your password:\n";
				msg += "______________________________________________________\n";
				errorMsg += alert(msg + errorMsg + "\n\n");
				document.getElementById("newpassone").focus();
				return false;
			}
			else
			{
				document.forms[1].submit();
			}
		}
	}
}


function validateSearchForm()
{

	var keywords = document.getElementById("keywords").value;
	
	var wrongCharacters=/^[0-9a-zA-Z\s.,!?@();:+_-]+$/;

	//check if all required boxes are filled
	if(keywords=="")
	{
		alert("Please fill out all required fields");
	}
	else
	{
		//check if keyword length at least 3
		if(keywords.length<3)
		{
			alert("The length of your keyword must be at least three characters");
		}
		else
		{
			//check if malicious characters are present
			if(wrongCharacters.test(keywords)==false)
			{
				alert('Please only use alphanumeric characters and the symbols .,!?@();:+_-');
			}
			else
			{
				document.forms[1].submit();
			}
		}
	}
}


