function validateUsername(Fieldobject)
{
	
	var regexCheck = /^\w{1,20}$/;
	 
	if( !regexCheck.test(Fieldobject.value) )
	{
		return false;
	} else {
		return true;
	}
}

function validatePassword(Fieldobject)
{
	
	var regexCheck = /^.{4,16}$/;
	 
	if( !regexCheck.test(Fieldobject.value) )
	{
		return false;
	} else {
		return true;
	}
}

function validatePasswordHint(Fieldobject)
{
		return true;
}

function validateEmailAddy(Fieldobject)
{
	if(Fieldobject.value.length == 0)
	{
		return true;
	}
	
	var regexCheck = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/;
	 
	if( !regexCheck.test(Fieldobject.value) )
	{
		return false;
	} else {
		return true;
	}
}

function validateRegistrationForm()
{

	bError = false;
	eString = "";
	
	if(!checkProfile())
	{ 
		  eString = eString  +"Sorry if you wish to enter a optional profile in step three, all boxes must be filled in" + "\n\n";
			bError = true;
	}
	
	//Make sure we have a valid username
	if(!validateUsername(document.getElementById('registrationUsername')))
	{
		eString = eString  + "Sorry there is a error in the username field \n - Usernames must be between 1 and 20 characters long \n - Users names can only contain the letters A-Z, the numbers 0-9 and the underscore _ character" + "\n\n";
		bError = true;
	}
	
	
	
	//Make sure we have a valid password
	if(!validatePassword(document.getElementById('registrationPassword')))
	{
		eString = eString  + "Sorry, there is an error in the password field \n - Passwords should be between 4 and 16 characters long \n - Passwords may contain any alphanumeric characters and symbols \n - Spaces are not allowed in passwords" + "\n\n";
		bError = true;
	}
	
	//Make sure the both passwords match
	if(document.getElementById('registrationPassword').value != document.getElementById('registrationPassword2').value)
	{
		eString = eString  + "Sorry, your password was not retyped correctly." + "\n\n";
		bError = true;
	}
	
	
	//Make sure the person is over 18 years old
	if(document.getElementById('registrationAgeCheck').selectedIndex != 1)
	{
		eString = eString  + "Sorry, you need to be 18 or over to join this site. Please confirm you are over 18 to join." + "\n\n";
		bError = true;
	}
	
	
	//If we require a email address, validate it
	if( document.getElementById('cWeekly').checked || document.getElementById('cSpecial').checked || document.getElementById('cEmailNotify').checked )
	{
			
			if( !validateEmailAddy(document.getElementById('registrationEmailAddress')) )
			{
				eString = eString  + "Sorry, the email address appears to be invalid.  An email address is not required, but it is recommended." + "\n\n";
				bError = true;
			}
	}
	
	if(bError == true)
	{
		alert(eString);
		return false;
	}
}

function checkProfile()
{
	var orient = document.getElementById('registrationOrient');
	var dominance = document.getElementById('registrationDominance');
	var gender = document.getElementById('registrationGender');
	var bmonth = document.getElementById('registrationBMonth');
	var bday = document.getElementById('registrationBDay');
	var byear = document.getElementById('registrationBYear');
	var country = document.getElementById('i_countries');
	var state = document.getElementById('i_states'); 
	
	if( (orient.selectedIndex > 0) || (dominance.selectedIndex > 0) || (gender.selectedIndex > 0) || (bmonth.selectedIndex > 0) || (bday.selectedIndex > 0) || (byear.selectedIndex > 0))
	{
		if( !((orient.selectedIndex > 0) && (dominance.selectedIndex > 0) && (gender.selectedIndex > 0) && (bmonth.selectedIndex > 0) && (bday.selectedIndex > 0) && (byear.selectedIndex > 0)))
		{
			return false;
		}
	}
	
	return true;
}
