$(function() {
	
	$(".image-submit").click(function() {
		// validate and process form
		// empty the error box error messages
    	$('.error-box').html("");
		$('.error-box-fade').css({display:"none"});
		
		// validate name
		var name = $("input#name").val();
		var nameLength = name.length;
		var nameSpace = name.indexOf(" ");
		
		if (name == "" || nameLength < 3 || nameSpace < 0) {
			$('.error-box-fade').html("You must enter your first and last names before we create your account.").fadeIn("def");
			$("input#name").focus();
      		return false;
    	}

		// validate email
		var email = $("input#email").val();
		
		if (email == "" || !isEmail(email)) {
	    	$('.error-box-fade').html("Sorry, that is not a valid email address.").fadeIn("def");
			$("input#email").focus();
	      	return false;
	    }
		
		function isEmail(string) {
			if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1){
				return true;
			}else{
				return false;
			}
		}
		
		
		
		// validate password
		var password = $("input#password").val();
		var passwordLength = password.length;
		var onlyLettersNumbers = /[\W_]/;
		
		if (password == "" ) {
	    	$('.error-box-fade').html("You must create a password before we create your account.").fadeIn("def");
			$("input#password").focus();
	      	return false;
	    }
	
		if (passwordLength < 6 ) {
	    	$('.error-box-fade').html("Your password must be at least 6 characters.").fadeIn("def");
			$("input#password").focus();
	      	return false;
	    }
	
		if (onlyLettersNumbers.test(password) ) {
	    	$('.error-box-fade').html("Your password must be any combination of letters and numbers only.").fadeIn("def");
			$("input#password").focus();
	      	return false;
	    }
		
		
		// vaildate province
		var province = $("select#province").val();
		if (province == "" ) {
		   	$('.error-box-fade').html("You must select your region before we create your account.").fadeIn("def");
			$("select#province").focus();
		    return false;
		}
		
		//future communications
		if (!$('#future').attr('checked')) {
			var future = "no";
		}else{
			var future = $("input#future").val();	
		}
		
		// vaildate rules and regs
		if (!$('#rules_regs').attr('checked')) {
		   	$('.error-box-fade').html("You must agree to the Rules and Regs before we create your account.").fadeIn("def");
			return false;
		}else{
			var terms = $("input#rules_regs").val();	
		}
		
		if (!$('#future').attr('checked')) {
			var future = "no";
		}else{
			var future = $("input#future").val();
		}
		
		var dataPosted = 'name='+ name + '&email=' + email  + '&password=' + password + '&province=' + province + '&future=' + future + '&rules_regs=' + terms;
		
		/*$('.image-submit').fadeOut(100, function() {
			$('.error-box-fade').html("<img src='images/progress.gif'><br>&nbsp;&nbsp;Sending data..").fadeIn("def");
		});*/
	

		$.ajax({
			url : "includes/check_email.php?email=" + email,
			success : function (data_in) {
		   		if(data_in == "not_unique"){
					$('.error-box-fade').html("There is already an account existing with that email address. If you have forgotten your password, <a href=\"\" onclick=\"self.parent.tb_open_new('forgot.php?TB_iframe=true&height=320&width=358&modal=true')\">click here</a>.").fadeIn("def")
					$("input#email").focus()
			      	return false;
			  	}else{
					$.ajax({
						type: "POST",
			      		url: "register.php",
			      		data: dataPosted,
			      		success: function(data) {
							
							if(data == "error"){
								$('.error-box-fade').html("There has been an error with your registration, please try again.").fadeIn("def");
								return false;
							}else{
								$('#pop-page-interior').html("	<h1>Register to <br>Natura Your World</h1><div id='error-box'><div class='error-box-fade'> </div></div>");
						        $('.error-box-fade').html("Thank you for registering for Benjamin Moore's Natura Your World contest!")
						        .fadeIn().animate({ opacity: 1.0 },3000).fadeOut(500, function() {
						          	self.parent.tb_remove('childCallUpload(true);');
						        });
							}
			      		}
					});
				}
			}
		});
	

	
	
		
    	return false;
	});
});

/*
runOnLoad(function(){
  $("input#name").select().focus();
});
*/
