var Interested = {
		step : 1,
		
		init : function() {
		$(".step-content .step").each(function(){
			$(this).hide();
		});
		$("#validate").live("click", this.processForm);
		$("#step-1 input[type='text']").live("keypress", this.pressed);
		//$("#validate").live("click", this.submitForm);
		
		this.openStep();
	},

	pressed : function(event) {
		if(event.keyCode == 13) {
			Interested.processForm();	
		}
	},

	openStep : function() {
		var step = Interested.step;
		
		$(".step-content .step").each(function(){
			if($(this).attr("id") == "step-" + Interested.step.toString()) {
				$(this).show();
			} else {
				$(this).hide();
			}
		});

		$('html, body').animate({scrollTop:0}, 0);
	},

	decrementStep : function() {
		Interested.step = 1;
		Interested.openStep();
	},
	
	processForm : function() {
		var errors = Interested.validate();
		
		if(!errors.failure) {

			$("#step-1 input[type='text'], #step-1 textarea").each(function(){
				$(".field" + $(this).attr("id")).text($(this).val());
			});
			
			Interested.step = 2;
			Interested.openStep();
			Interested.submitForm();
		} else {
			Interested.processErrors(errors);
		}
	},


	processErrors: function(errors) {
		var errs = "";
		for(key in errors) {
			$("#" + key.toString()).addClass("error");

			if(errors[key] != true) {
				errs += "<li>" +  errors[key] + "</li>";
			}
		}


		$("#errors").html(errs);

		$('html, body').animate({
		    scrollTop: $("#errors").offset().top
		}, 500);
					
	},
	
	validate : function() {
		var max = Interested.max;
		
		$("#errors").html("");
		$(".error").removeClass("error");
		
		var errors = {};
		errors.failure = false;
		
		if($("#firstName").val() == "") {
			errors.failure = true;
			errors.firstName = "Jméno je povinné pole a nemůže být prázdné.";	
		}
		if($("#lastName").val() == "") {
			errors.failure = true;
			errors.lastName = "Příjmení je povinné pole a nemůže být prázdné.";	
		}
		if(!$("#email").val().match(/.+@.+/)) {
			errors.failure = true;
			errors.email = "Neplatná e-mailová adresa.";	
		}
		if($("#phone").val() == "") {
			errors.failure = true;
			errors.phone = "Telefon je povinné pole a nemůže být prázdné.";
		}
		if($("#education").val() == "") {
			errors.failure = true;
			errors.education = "Popis vzdělání je povinné pole a nemůže být prázdné.";
		}
		if($("#practice").val() == "") {
			errors.failure = true;
			errors.practice = "Popis praxe je povinné pole a nemůže být prázdné.";
		}

		return errors;
	},

	submitForm : function() {
		var request = {};
		
		$("#step-1").find("input[type='text'], textarea, select").each(function() {
			request[$(this).attr("id")] = $(this).val();
		});

		$.ajax({
			url : "/mail/pre-login",
			data : request,
			type : "post",
			dataType : "json"
		});

		Interested.step = 2;
		Interested.openStep();
	}
};

