function doReg(){
	$.ajax({
		type: "POST",
		url: "/_asp/create-account.aspx",
		data: $("#regForm").serialize(), 
		dataType: "text",
		error: function(XMLHttpRequest, textStatus, errorThrown){
			window.location = "error.aspx";
		},
		success: function(text){
			if( text == "error:" ){
				window.location = "success.aspx";
			}
			else{
				window.location = "error.aspx";
			}
		}
	});
}

function updateProfile(target, src, gender) {
	$("#"+target).val(src);
	$("#"+target+"_gender").val(gender);
	var n = $(".page:visible").next().attr("id");
	$(".page").hide().filter("#"+n).show();
	if ( n == "pageFour" ){
		doReg();
	}
};

$(document).ready(function(){
	
	var valid = 0;
	
	function checkReq(target,len){
		var v = $(target).val();
		if ( v.length < len ){
			valid++;
		}
	}
	function checkSelect(target,min){
		var v = $(target).val();
		if ( v < min ){
			valid++;
		}
	}
	function checkCheckBox(target){
		
		if ( $(target).is(":checked") ){
	
		} 
		else{
			alert("You must agree to the Terms of Service before proceeding.")
			valid++;
		}
		
	}
	function invalidate(target){
		valid++;
		$(target).val('').css("background-color","#fdafaf");
	}
	function checkMatch(match,target){
		var a = $(match).val();
		var b = $(target).val();
		if ( a != b ){
			invalidate(target);
			alert("Please make sure you entered your password correctly both times.");
		}
	}
	function checkEml(target){
		var e = $(target).val();
		if ( e.checkEmail() == false ){
			invalidate(target);
			alert("Please enter a valid email address.")
		}
		else{
			$.post('/_asp/email-check.aspx',{profile_email: e},
			function(data){
				if ( data == "False" ){
					$(".page").hide().filter("#pageTwo").show();
				}
				else{
					alert("That email address is already in use.")
				}
			},"text")
		}
	}
	
	$("input").focus(function(){
		$(this).css("background-color","#ffffff");
	});
	
	$("#next1").click(function(){
		
		checkReq("#profile_fname",1);
		checkReq("#profile_lname",1);
		checkReq("#profile_email",6);
		checkReq("#profile_password",8);
		checkReq("#profile_password_confirm",8);
		checkSelect("#profile_country",1);
		checkSelect("#profile_date_birth_month",1);
		checkSelect("#profile_date_birth_day",1);
		checkSelect("#profile_date_birth_year",1);
		checkCheckBox("#terms");
		
		if ( valid > 0 ){
			alert("Please check that all fields are complete and that your password is at least 8 characters.");
		}
		checkMatch("#profile_password","#profile_password_confirm");
		if ( valid === 0 ){
			checkEml("#profile_email");
		}
		
		valid = 0;
		
	});

	
});
