var Login = {
		step : 1,
		
		init : function() {
			$("#note").hide();
		$(".step-content .step").each(function(){
			$(this).hide();
		});
		$("#validate").live("click", this.processForm);
		$("#previous").live("click", this.decrementStep);
		$("#insert-note").live("change", this.swapNote);	
		$("#step-1 input[type='text']").live("keypress", this.pressed);
		$("#submit-order").live("click", this.submitForm);
		
		this.openStep();
		this.swapNote();
	},

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

	swapNote : function() {
		var noteStatus = $("#insert-note:checked").size();

		if(noteStatus) {
			$("#note").show();
		} else {
			$("#note").hide();
		}
	},
	
	openStep : function() {
		var step = Login.step;
		
		$("#prihlaska-navigation div").removeClass("active");
		$("#prihlaska-navigation").find("div.li" + step.toString()).addClass("active");
		$(".step-content .step").each(function(){
			if($(this).attr("id") == "step-" + Login.step.toString()) {
				$(this).show();
			} else {
				$(this).hide();
			}
		});

		$("#step-name").text($("#step-" + Login.step.toString()).find(".step-name").text());
		$('html, body').animate({scrollTop:0}, 0);
	},

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

			$("#course-login input[type='text'], #course-login textarea").each(function(){
				$(".field" + $(this).attr("id")).text($(this).val());
			});
			
			Login.step = 2;
			Login.openStep();
		} else {
			Login.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 = Login.max;
		
		$("#errors").html("");
		$(".error").removeClass("error");
		
		var errors = {};
		errors.failure = false;
		
		if($("#name").val() == "") {
			errors.failure = true;
			errors.name = "Jméno je povinné pole a nemůže být prázdné.";	
		}
		/*
		if(!$("#email").val().match(/^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/)) {
			errors.failure = true;
			errors.email = "Neplatná e-mailová adresa.";	
		}
		*/
		if($("#email").val() == "") {
			errors.failure = true;
			errors.email = "E-mail je povinné pole a nemůže být prázdné.";
		}		
		if($("#phone").val() == "") {
			errors.failure = true;
			errors.phone = "Telefon je povinné pole a nemůže být prázdné.";
		}
		if($("#count").val() == "" ||  !Login.isNumber($("#count").val()) || parseInt($("#count").val()) > max) {
			errors.failure = true;
			errors.count = "Neplatný počet účastníků, maximální možný počet je " + max.toString() + ".";
		}

		return errors;
	},


	isNumber : function(n) {
		return !isNaN(parseFloat(n)) && isFinite(n);
		
	},

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

		$.ajax({
			url : "/course-login/login/id_course/" + Login.id_course,
			data : request,
			type : "post",
			dataType : "json"
		});

		Login.step = 3;
		Login.openStep();
	}
};

