blank = /^(\s+)/;
blankStart =/^(\s+)(\w*)/;

function termsRead(){
  	document.contest.terms.value = "termsRead";
	alert("Terms read getting called");
}

var Validate = {
	"blank":function(el, msg){
		if(el.value == ''){
			if(msg == "Email"){
				alert("Please enter an " + msg);
			} else {
				alert("Please enter a " + msg);
			}
			return Validate.highlight(el);
		}  
	},"space": function(el, msg){
		if(el.value.match(blank) != null){
			alert("Blank space is not allowed within " + msg);
			return Validate.highlight(el);
		}
	},"highlight":function(el){
		el.focus();
		el.className = "formError";
		return true;
	},"blankStart":function(el, msg){
		if(el.value.match(blankStart) != null){
			alert("Blank space is not a valid " + msg);
			return Validate.highlight(el);
		}
	}
}

function registerUser(){
	var username = document.contest.username;
	if(Validate.blank(username, "User Name")){
		return false;
	}
	if(Validate.space(username, "User Name")){
		return false;
	}
	
	if(username.value.length < 6){
		alert("Username can not have less than 6 characters");
		Validate.highlight(username);
		return false;
	}	
	
	var password = document.contest.password;
	if(Validate.blank(password, "password")){
		return false;
	}
		
	if(password.value == username.value){
		alert("Password can not be same as User Name.");
		Validate.highlight(password);
		return false;
	}
	
	if(password.value.length < 8){
		alert("Password can not have less than 8 characters");
		Validate.highlight(password);
		return false;
	}
	
	var passwordRepeat = document.contest.passwordRepeat;
	if(Validate.blank(passwordRepeat, "password")){
		return false;
	}
	if(passwordRepeat.value != password.value){
		alert("Passwords entered do not match.\nPlease re-enter passwords.");
		Validate.highlight(passwordRepeat);
		Validate.highlight(password);			
		password.value = passwordRepeat.value = "";
		return false;
	} else {
		encryptPwd();
	}
	
	var email = document.contest.email;
	
	if(Validate.blank(email, "Email")){
		return false;
	} 
	if(!validateEmail(email.value)){
		Validate.highlight(email);
		return false;
	}
		
	var firstname = document.contest.firstname;
	if(Validate.blank(firstname, "First Name")){
		return false;
	}
	
	if(Validate.blankStart(firstname, "First Name")){
		return false;
	}
	
	var lastname = document.contest.lastname;
	
	if(Validate.blank(lastname, "Last Name")){
		return false;
	}
	if(Validate.blankStart(lastname, "Last Name")){
		return false;
	}
	
	var hintanswer = document.contest.hintanswer;
	if(Validate.blank(hintanswer, "Hint Answer")){
		return false;
	}
	if(Validate.blankStart(hintanswer, "Hint Answer")){
		return false;
	}
	
	var address1 = document.contest.address1;
	if(Validate.blank(address1, "Street, Apt/House #")){
		return false;
	}
	if(Validate.blankStart(address1, "Street, Apt/House #")){
		return false;
	}
	
	var city = document.contest.city;	
	if(Validate.blank(city, "City")){
		return false;
	}
	if(Validate.blankStart(city, "City")){
		return false;
	}
	
	var state = document.contest.state;
	if(Validate.blank(state, "State")){
		return false;
	}
	if(Validate.blankStart(state, "State")){
		return false;
	}
	
	var zip = document.contest.zip;
	if(Validate.blank(zip, "Postal Code/Zip")){
		return false;
	}
	if(Validate.blankStart(zip, "Postal Code/Zip")){
		return false;
	}
	
	var country = document.contest.country;
	if(Validate.blank(country, "Country")){
		return false;
	}
	if(Validate.blankStart(country, "Country")){
		return false;
	}
	
	var terms = document.contest.terms.checked;
	var termsRead = document.contest.terms.value;
	if (!terms){
		alert("You MUST AGREE to Terms and Conditions of Contest before you can register");
		document.contest.terms.focus()
		return false;
	}
	
	if(termsRead == "notRead"){
		alert("You MUST READ the Terms and Conditions of Contest before you can register");
		document.contest.terms.focus()
		return false;
	}
	
	var captchaText = document.contest.captcha_text;
	if(Validate.blank(captchaText, "Captcha Text")){
		return false;
	}
	if(Validate.blankStart(captchaText, "Captcha Text")){
		return false;
	}
}
function openTerms(){
	document.contest.terms.value='termsRead';
	var termsWin = open("terms.php");
}

function processForgot(){
	if((document.forgot.email.value == "" 
		|| document.forgot.email.value.match(blank))
			&& (document.forgot.username.value == "" 
		||document.forgot.email.value.match(blank))){
		alert("Please enter either username or email.\nAt least one value is required.");
		return false;
	}
	return true;
}

function processHint(){
	var answer = document.hint.answer.value;
	if(answer == "" || answer.match(blank)){
		alert("Please enter an answer for the hint.");
		return false;
	}
	return true;
}

