// JavaScript Document
//

$(document).ready(function(){	
	//
	var jsonError = new Object();
	jsonError = {
			"fn" : false,
			"ln" : false,
			"email" : false,
			"pwd" : false,
			"conf" : false
			};
	//
	function showSignupButton() {
		e = [];
		$.each(jsonError, function(i,error){
			if(error == false){
				e.push("error");
			}
		});
		if(e.length != 0){
			$("#signupSubmit").hide();
			$("#inactive").show();
		}else{
			$("#inactive").hide();
			$("#signupSubmit").show();
			
			// submit form
			$("#signupSubmit").click(function(){
					var button = $(this);
					button.attr("disabled","disabled");
					$.ajax({
						type : 'POST',
						url : "sign-up.php",
						data : {
							first : $("#sfn").val(),
							last : $("#sln").val(),
							email : $("#se").val(),
							pwd : $("#sp").val()
							},
						success : function(html) {
							$("#sign-up-bg").html(html);
							$("#sfn").val('');
							$("#sln").val('');
							$("#se").val('');
							$("#sp").val('');
						}
					});
			});
			
		}
	}
	//
	$("#signupSubmit").hide();
	//first name
	$("#sfn").keyup(function(){
		if($(this).val().length == 0){ 
			jsonError.fn = false;
			$("#fname .error").html("&nbsp;&nbsp;please enter first name");
			// console.log(jsonError.fn);
		}else{
			jsonError.fn = true;
			$("#fname .error").html("");
			// console.log(jsonError.fn);
		}
		showSignupButton();
	});
	//last name
	$("#sln").keyup(function(){
		if($(this).val().length == 0){ 
			jsonError.ln = false;
			$("#lname .error").html("&nbsp;&nbsp;please enter last name");
			// console.log(jsonError.ln);
		}else{
			jsonError.ln = true;
			$("#lname .error").html("");
			// console.log(jsonError.ln);
		}
		showSignupButton();
	});		
	//email
	$("#se").keyup(function(){
		var emailString = $(this).val();
		$.ajax({
			   	type : 'POST',
				url : "check-email.php",
				data : "email=" + emailString,
				success : function(text) {
					//console.log(text);
					//invalid email
					if(text == "0"){
						jsonError.email = false;	
						$("#email .error").html("please enter a valid email");
						// console.log(jsonError.email);
					}
					if(text == "1"){
						jsonError.email = false;	
						$("#email .error").html("This email is associated with an existing account");
						// console.log(jsonError.email);
					}
					if(text == "2"){
						jsonError.email = true;
						$("#email .error").html("");
						// console.log(jsonError.email);
					}
					showSignupButton();
				}	   
		});
	});	// end email
	// password
	$("#sp").keyup(function(){
		$("#sc").val("");
		if($(this).val().length < 4){ 
			jsonError.pwd = false;
			$("#pass .error").html("&nbsp;&nbsp;password must be at least 4 characters");
			// console.log(jsonError.pwd);
		}else{
			jsonError.pwd = true;
			$("#pass .error").html("");
			// console.log(jsonError.pwd);
		}
		showSignupButton();
	});
	// confirm
	$("#sc").keyup(function(){
		if($(this).val() != $("#sp").val()){ 
			jsonError.conf = false;
			$("#conf .error").html("&nbsp;&nbsp;password must match");
			// console.log(jsonError.conf);
		}else{
			jsonError.conf = true;
			$("#conf .error").html("");
			// console.log(jsonError.conf);
		}
		showSignupButton();
	});
	//
	// submit form via ajax

});
