var loading_img = "<img src='/static/img/ajax-loader.gif'/>";
function check_emailAlt(){
	var data = $.ajax({
      url: "http://openid.pino.pl/app/konto-validEmailAlt/",
      type: "POST",
	  async: false,
      data: ({email: $('#email_alt').val()}),
      dataType: "text"
   }).responseText;
	if (data=='2'){
		$("#email_alt_error").html( "Adres alternatywny istnieje w bazie." );
		$(".email_alt_msg").hide();
		$(".email_alt_error").show();
		return false;
	}
	else if (data=='1'){
		$("#email_alt_error").html( "Email jest niepoprawny." );
		$(".email_alt_msg").hide();
		$(".email_alt_error").show();
		return false;
	}
	else if (data=='3'){
		$("#email_alt_error").html( "Email nie może być w domenie @pino.pl." );
		$(".email_alt_msg").hide();
		$(".email_alt_error").show();
		return false;
	}
	else{
		$("#email_alt_error").html( "" );
		$(".email_alt_error").hide();
		$(".email_alt_msg").hide();

	}

	//if($('#email_alt2').val().length>0){
	//	return check_emailAlt2();
	//}
}
function check_emailAlt2(){
	var bRes = $.ajax({
      url: "http://openid.pino.pl/app/konto-validEmailAlt2/",
      type: "POST",
	  async: false,
      data: ({email: $('#email_alt').val(), email2: $('#email_alt2').val()}),
      dataType: "text"
	}).responseText;
	if (bRes=='1'){
		$("#email_alt2_error").html( "Email jest niepoprawny." );
		$(".email_alt2_msg").hide();
		$(".email_alt2_error").show();
		return false;
	}
	else if (bRes=='2'){
		$("#email_alt2_error").html( "Powtórz poprawnie email alternatywny." );
		$(".email_alt2_msg").hide();
		$(".email_alt2_error").show();
		return false;
	}
	else{
		$("#email_alt2_error").html( "" );
		$(".email_alt2_error").hide();
		$(".email_alt2_msg").hide();
		return true;
	}

}
function check_login(){
	$.post("http://openid.pino.pl/app/konto-validateLogin/", {login: $('#login').val()},
  		function(data)
			{
    			if (data=='1'){
					$("#login_error").html( "Login musi mieć od 3 do 32 znaków." ).show();
					$(".login_msg").hide();
					$(".login_error").show();
					return false;
				}
				else if (data=='2'){
					$("#login_error").html( "Login może składać się z liter, cyfr i  '_ - .' ." ).show();
					$(".login_msg").hide();
					$(".login_error").show();
					return false;
				}
				else if (data=='3'){
					$("#login_error").html( "Ten login jest niedozwolony." ).show();
					$(".login_msg").hide();
					$(".login_error").show();
					return false;
				}
				else if (data=='4'){
					$("#login_error").html( "Login zajęty. Spróbuj wybrać inny." ).show();
					$(".login_msg").hide();
					$(".login_error").show();
					return false;
				}
				else{
					$("#login_error").html( "" );
					$(".login_error").hide();
					$(".login_msg").hide();
					return true;
				}
 			}
		, "text");
}
function check_password(){
	if ($('#password').val().length < 4 || $('#password').val().length > 20) {
		$("#password_error").html("Hasło musi miec od 4 do 20 znaków.");
		$(".password_msg").hide();
		$(".password_error").show();
		return false;
	}
	else {
		$("#password_error").html( "" );
		$(".password_error").hide();
		$(".password_msg").hide();
	}
	if($('#password2').val().length > 0){
		if(check_password2()) return true;
		else return false;
	}
	return true;
}
function check_password2(){
	if ($('#password2').val().length < 4 || $('#password2').val().length > 20) {
		$("#password2_error").html( "Hasło musi miec od 4 do 20 znaków." );
		$(".password2_msg").hide();
		$(".password2_error").show();
		return false;
	}
	else if ($('#password').val() != $('#password2').val()) {
		$("#password2_error").html( "Powtórz poprawnie hasło." );
		$(".password2_msg").hide();
		$(".password2_error").show();
		return false;
	}
	else{
		$("#password2_error").html( "" );
		$(".password2_error").hide();
		$(".password2_msg").hide();
	}
	return true;
}
function check_city(){
	if ($('#city').val().length < 2 ) {
		$("#city_error").html("Wpisz miasto.");
		$(".city_msg").hide();
		$(".city_error").show();
		return false;
	}
	else {
		$("#city_error").html("");
		$(".city_error").hide();
		$(".city_msg").hide();
	}
	return true;
}
function check_sex(){
	$iSex = $("input[name='sex']:checked").val();
	if( typeof $iSex == 'undefined' ) {
		$("#sex_error").html("Wybierz płeć.");
		$(".sex_msg").hide();
		$(".sex_error").show();
		return false;
	} else {
		if(( $iSex == 1 ) || ( $iSex == 2 ) ) {
			$("#sex_error").html("").hide();
			$(".sex_error").hide();
			$(".sex_msg").hide();
			return true;
		}
		else {
			$("#sex_error").html("Wybierz płeć.");
			$(".sex_msg").hide();
			$(".sex_error").show();
			return false;
		}
	}

}
function check_date(){
	if ($('#day').val() == 0 || $('#month').val() == 0 || $('#year').val() == 0) {
		$("#date_error").html("Wybierz datę urodzenia.");
		$(".date_msg").hide();
		$(".date_error").show();
		return false;
	}
	else {
		$("#date_error").html("");
		$(".date_error").hide();
		$(".date_msg").hide();
		return true;
	}
}
function check_reg(){
	if($('#reg:checked').val() != 1){
		$("#reg_error").html("Potwierdź akceptację.");
		$(".reg_msg").hide();
		$(".reg_error").show();
		return false;
	}
	else {
		$("#reg_error").html("");
		$(".reg_error").hide();
		$(".reg_msg").hide();
		return true;
	}
}
function check_reg2(){
	if($('#reg2:checked').val() != 1){
		$("#reg2_error").html("Potwierdź akceptację.");
		$(".reg2_msg").hide();
		$(".reg2_error").show();
		return false;
	}
	else {
		$("#reg2_error").html("");
		$(".reg2_error").hide();
		$(".reg2_msg").hide();
		return true;
	}
}
function check_captcha(){
	if($('#captcha').val().length < 2){
		$("#captcha_error").html("Wpisz kod z obrazka.");
		$(".captcha_msg").hide();
		$(".captcha_error").show();
		return false;
	}
	else {
		$("#captcha_error").html("");
		$(".captcha_error").hide();
		$(".captcha_msg").hide();
		return true;
	}
}
function validateAll(){
	$isError = 0;
	$check_password = check_password();
	$check_password2 = check_password2();
	$check_date = check_date();
	$checkSex = check_sex();
	$check_city = check_city();
	$check_captcha = check_captcha();
	$check_reg = check_reg();
	$check_reg2 = check_reg2();
	if($('#email_alt').val().length > 0){
		check_emailAlt();
	}
	else{
		$("#email_alt_error").html( "Wpisz email." );
		$(".email_alt_msg").hide();
		$(".email_alt_error").show();
		$isError = 1;
	}
	if($('#email_alt2').val().length > 0){
		check_emailAlt2();
	}
	else{
		$("#email_alt2_error").html( "Wpisz email." );
		$(".email_alt2_msg").hide();
		$(".email_alt2_error").show();
		$isError = 1;
	}
	if($('#login').val().length > 0){
		check_login();
	}
	else{
		$("#login_error").html( "Wpisz login." );
		$(".login_msg").hide();
		$(".login_error").show();
		$isError = 1
	}

	if(!$checkSex || !$check_city || !$check_password2 || !$check_password || !$check_date || !$check_reg || !$check_reg2 || !$check_captcha || $isError ==1){
		return false;
	}
	return true;
}

function validateActivate(){
	$isError = 0;
	if($('#email_alt').val().length > 0){
		return check_emailAlt();

	}
	else{
		$("#email_alt_error").html( "Wpisz email." );
		$(".email_alt_msg").hide();
		$(".email_alt_error").show();
		$isError = 1;
	}
	if($('#email_alt2').val().length > 0){
		return check_emailAlt2();
	}
	else{
		$("#email_alt2_error").html( "Wpisz email." );
		$(".email_alt2_msg").hide();
		$(".email_alt2_error").show();
		$isError = 1;
	}

	if($isError ==1){
		return false;
	}
	return true;
}


$(document).ready(function(){
	 $('#email_alt').bind("blur", function(){
     	check_emailAlt();
    });
	 $('#email_alt').bind("focus", function(){
     	$(".email_alt_error").hide();
		$(".email_alt_msg").show();

    });

	// $('#email_alt2').bind("blur", function(){
     //	check_emailAlt2();
    //});
	// $('#email_alt').bind("focus", function(){
     //	$(".email_alt2_error").hide();
	//	$(".email_alt2_msg").show();
    //});

	 $('#login').bind("blur", function(){
     	check_login();
    });
	 $('#login').bind("focus", function(){
     	$(".login_error").hide();
		$(".login_msg").show();
    });

	 $('#password').bind("blur", function(){
     check_password();
    });
	 $('#password').bind("focus", function(){
     	$(".password_error").hide();
		$(".password_msg").show();
    });

	 $('#password2').bind("blur", function(){
     check_password2();
    });
	 $('#password2').bind("focus", function(){
     	$(".password2_error").hide();
		$(".password2_msg").show();
    });

	 $('#city').bind("blur", function(){
     check_city();
    });
	 $('#city').bind("focus", function(){
     	$(".city_error").hide();
		$(".city_msg").show();
    });

 	$('#captcha').bind("blur", function(){
     check_captcha();
    });
	$('#captcha').bind("focus", function(){
     	$(".captcha_error").hide();
		$(".captcha_msg").show();
    });

	$('#reg').bind("blur", function(){
		check_reg();
	});
	$('#reg').bind("focus", function(){
     	$(".reg_error").hide();
		$(".reg_msg").show();
    });

	$('#reg2').bind("blur", function(){
		check_reg2();
	});
	$('#reg2').bind("focus", function(){
     	$(".reg2_error").hide();
		$(".reg2_msg").show();
    });

	$('#day').bind("blur", function(){
		check_date();
	});
	$('#day').bind("focus", function(){
     	$(".date_error").hide();
		$(".date_msg").show();
    });

	$('#month').bind("blur", function(){
		check_date();
	});
	$('#month').bind("focus", function(){
     	$(".date_error").hide();
		$(".date_msg").show();
    });

	$('#year').bind("blur", function(){
		check_date();
	});
	$('#year').bind("focus", function(){
     	$(".date_error").hide();
		$(".date_msg").show();
    });

	$("input[name='sex']").bind("blur", function(){
		check_sex();
	});
	$("input[name='sex']").bind("focus", function(){
     	$(".sex_error").hide();
		$(".sex_msg").show();
    });



});