	function trim(str) {
		return str.replace(/^\s+|\s+$/g, '');
	}
	
	function validField(formField,fieldLabel)
	{
		var result = true;
		
		if (formField.value == "")
		{
			alert('Please enter a value for the "' + fieldLabel +'" field.');
			formField.focus();
			result = false;
		}
		
		return result;
	}
	
	function checkoath(formField)
	{
		var result = true;
		
		if (formField.checked == false)
		{
			alert('Please click the checkbox to sign the oath');
			formField.focus();
			result = false;
		}
		
		return result;
	}
	
	function validEmail(formField,fieldLabel,required)
	{
		var result = true;
		
		if (required && !validField(formField,fieldLabel))
			result = false;
		
		if (result && ((formField.value.length < 3) || !isEmailAddr(formField.value)) )
		{
			alert("Please enter a complete email address in the form: yourname@yourdomain.com");
			formField.focus();
			result = false;
		}
		
		return result;
		
	}
	
	function isUrl(url)
	{
		var result = false;
		var theStr = trim(url.value);
		
		var spindex = theStr.indexOf(" ");
		if (spindex < 0) { //no space
			var pindex = theStr.indexOf(".");
			if ((pindex > 0) && (theStr.length > pindex))
				result = true;
		}
		
		return result;
	}
	
	function isEmpty(field)
	{
		return trim(field.value).length == 0;
	}
	
	function isEmailAddr(email)
	{
		var result = false;
		var theStr = new String(email);
		var index = theStr.indexOf("@");
		if (index > 0)
		{
			var pindex = theStr.indexOf(".",index);
			if ((pindex > index+1) && (theStr.length > pindex+1))
				result = true;
		}
		return result;
	}
	
	function validOccupation(occupation, other_occupation)
	{
		var result = true; 
		if (occupation.options[occupation.selectedIndex].text == "Not Listed" && other_occupation.value == "")
		{
			alert('If your accupation/area of specialization is Not Listed, please add it in the field provided.');
			other_occupation.focus();
			result = false;
		}
		
		return result;
	}
	
	function samePassword(pass1, pass2)
	{
		var result = true; 
		if (pass1.value != pass2.value)
		{
			alert('The two new password entries did not match - please re-enter news password');
			pass2.focus();
			result = false;
		}
		
		return result;
	}
	
	function validUrl(homePage)
	{
		if (isEmpty(homePage))
			return true;
		
		if (!isUrl(homePage)) {
			alert('Sorry, you must type a valid URL.  example: http://www.myhomepage.com');
			homePage.focus();
			return false;
		}
		
		return true;
	}
	
	function validateLoginForm(theForm)
	{
		if (!validEmail(theForm.username,"Username",true))
			return false;
		if (!validField(theForm.password,"Password"))
			return false;
		return true;
	}
	
	
	
	function validateForm(theForm)
	{
		if (!validField(theForm.surname,"Surname"))
			return false;
		if (!validField(theForm.other_names,"Other Names"))
			return false;
		if (!validEmail(theForm.e_mail,"NMN Registered E-Mail",true))
			return false;
		if (!validOccupation(theForm.occupation, theForm.other_occupation))
			return false;
		if (!checkoath(theForm.oath))
			return false;
		if (!validUrl(theForm.home_page))
			return false;
		
		return true;
	}
	
	function validatePassForm(theForm)
	{
		if (!validField(theForm.old_password,"Enter Old Password"))
			return false;
		if (!validField(theForm.new_password,"Enter New Password"))
			return false;
		if (!validField(theForm.new_password2,"Re-enter New Password"))
			return false;
		if (!samePassword(theForm.new_password, theForm.new_password2))
			return false;
		
		return true;
	}
	

