$(document).ready(function() {
	$("#regMain").css('height', '750px');
	
	$("#dalej1").click(function() {
		if(($("#haslo").val() == $("#haslo2").val()) && $("#haslo").val() != '') {
		  $("#rejestracja1").animate({ 
			    left: "-410px"
			  }, 1500 );
			  
			  $("#rejestracja2").animate({ 
				  left: "0px"
			  }, 1500 );			
		}
		else {
			alert('Podane hasła nie sa takie same');
		}		
	});
	
	$("#login").blur(function() {
		if($("#login").val() != '') {
			sprawdzLogin();
		}
	});
	$("#pesel").blur(function() {
		if($("#login").val() != '') {
			sprawdzPesel();
		}
	});
	
	$("#powrot1").click(function(){
		$("#rejestracja1").animate({ 
			left: "0px"
		}, 1500 );
		
		$("#rejestracja2").animate({ 
			left: "410px"
		}, 1500 );
	});
	
	$("#dalej2").click(function() {
		checkElements();
	});

	function sprawdzLogin() {
		advAJAX.post({
			url : "_INC/ajax/php/sprawdzLogin.php",
			parameters : {
				"login" : $("#login").val()
			},
			uniqueParameter: "uniqe",
			timeout : 40000,
	   		retry: 2,
	    	retryDelay: 1000,
			
	    	onTimeout : function()
			{
				alert("Connection timeout.");
			},
			
			onInitialization : function(obj)
			{
				$("#loginCheck").text('Sprawdzam login...');
			},
			
			onSuccess : function(obj)
			{
				var xml = obj.responseXML.getElementsByTagName("row");
				if($(xml[0]).attr('value') == '1') {
					$("#dalej1").attr('disabled', '');
					$("#loginCheck").text('');
				}
				else {
					alert('Wybrany przez Ciebie login jest już zajęty');
					$("#dalej1").attr('disabled', 'disabled');
					$("#loginCheck").text('Wybrany login jest już zajęty!');	
				}
							
				
			},
			
			onError : function(obj)
			{
				alert("Error: " + obj.status)
			}
		});	
	}
	
	function sprawdzPesel() {
		advAJAX.post({
			url : "_INC/ajax/php/sprawdzPesel.php",
			parameters : {
			"pesel" : $("#pesel").val()
		},
		uniqueParameter: "uniqe",
		timeout : 40000,
		retry: 2,
		retryDelay: 1000,
		
		onTimeout : function()
		{
			alert("Connection timeout.");
		},
		
		onSuccess : function(obj)
		{
			var xml = obj.responseXML.getElementsByTagName("row");
			if($(xml[0]).attr('value') != '1') {
				alert('Niepoprawny numer PESEL');
				$("#dalej2").attr('disabled', 'disabled');	
			}
			else {
				$("#dalej2").attr('disabled', '');
			}
			
			
		},
		
		onError : function(obj)
		{
			alert("Error: " + obj.status)
		}
		});	
	}
	
	
	
	function checkElements() {

		var el = $("*[ob=1]");
		for(var index = 0; index < el.length; index++) {
			var type = el[index].type;
		
			if((type == 'text') || (type == 'textarea') || (type == 'password')) {
				if(!el[index].value) {
					alert(el[index].getAttribute('komunikat'));
					return;
				}
				else {
					if(el[index].getAttribute('email') == 1) {
						if(!checkEmail(el[index].value)) {
							alert(el[index].getAttribute('komunikat'));
							return;
						}
					}
				}
			}
			else if(type == 'select-one') {
				if(!$("select[name='"+el[index].name+"'] option:selected").val()) {
					alert(el[index].getAttribute('komunikat'));
					return;
				}
			}
			else if(type == 'radio') {
				if(!$(":radio[name="+el[index].name+"]:checked").val()) {
					alert(el[index].getAttribute('komunikat'));
					return;
				}
			}
			else if(type == 'checkbox') {
				if(!$(":checkbox[name="+el[index].name+"]:checked").val()) {
					alert(el[index].getAttribute('komunikat'));
					return;
				}
			}
		}
		document.forms['rejestracjaAFF'].submit();
	}



	function checkEmail(email) {
		   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		    if(reg.test(email) == false) {
		      return false;
		   }
		    else {
		    	return true;
		    }
		}
	
});