function ChangeToCorp() {
  
  $("#pesel").rules("remove", "required");
  
  $("#nip").rules("add", {
      required: true,
      regex: "^[0-9]{10}$"
  });
/*
  $("#regon").rules("add", {
      required: true,
      regex: "^[0-9]{9}$"
  });
*/
  $("#register").show('slow');

  $("#pesel,#consent_publication").hide('slow');

  $("label[@for=pesel], label[@for=consent_publication]").hide('slow');

  $("#nip").show('slow');

  $("label[@for=nip]").show('slow');

  $("label[@for=name]").html('Nazwa firmy');

  $("input#account_type").attr("value", "0"); 

  $("input#consent_publication").attr("value", "1"); 


}

function ChangeToPrivate() {

 $("#register").show('slow');
  $("#pesel").rules("add", {
      required: true,
      regex: "^[0-9]{11}$"
  });

  $("#pesel,#consent_publication").show('slow');

  $("label[@for=pesel], label[@for=consent_publication]").show('slow');

  $("#nip").hide('slow');

  $("label[@for=nip]").hide('slow');

  $("label[@for=name]").html('Imię i nazwisko');

  $("input#account_type").attr("value", "1"); 
  $("input#consent_publication").attr("value", "0"); 


}

function ChangeValue() {

	var e = $("input#consent_publication").val();

	if(e == '1') {

  		$("input#consent_publication").attr("value", "0"); 

	} else {
		
		$("input#consent_publication").attr("value", "1"); 
	}

}

function BackReg() {

$("#status").hide('slow');

$("#top").hide('slow');

$("#top").html('<input type="button" onClick="ChangeToCorp();" value="Konto Firmowe" class="button" /><input type="button" onClick="ChangeToPrivate();" value="Konto Prywatne" class="button"  /><img src="design/logo.gif" />');

$("#top").show('slow');

$("#register").show('slow');



}

function CreateNewAccount() {

$("#register").hide('slow');
$("#top").hide('slow');
$("#top").html('<img src="design/logo.gif" />');
$("#top").show('slow');
$("#status").show('slow');

	l = $('input#login').val();
	h = $('input#password').val();
	n = $('input#nip').val();
	r = '';
	p = $('input#pesel').val();
	i = $('input#name').val();
	rp = $('input#representative').val();
	c = $('input#country').val();
	m = $('input#city').val();
	s = $('input#street').val();
	k = $('input#post_code').val();
	t = $('input#telephone_number').val();
	g = $('input#mobilephone').val();
	e = $('input#email').val();
	z = $('input#consent_publication').val();
	a = $('input#account_type').val();




	$.post("zaloz,konto.html", { login: l, haslo: h, nip: n, regon: r, pesel: p, name: i, reprezentant: rp, kraj: c, miasto: m, ulica: s, kod_pocztowy: k, telefon: t, komorka: g, email: e, zgoda: z, typ: a },

			function(data){

				if(data == 1) {

					$("#status").html('<span class="ok">'+account_created+'</span>');

				} else {
			
					$("#status").html('<span class="error">'+data+'</span> <br /><br /><input type="button" class="create" value="'+back+'" onClick="BackReg();" />');

				}

			});


}


$(document).ready(function(){

    $.validator.addMethod(
            "regex",
            function(value, element, regexp) {
                var check = false;
                var re = new RegExp(regexp);
                return this.optional(element) || re.test(value);
            },
            "Podano niedozwolone znaki!"
    );
    
    $.validator.addMethod('postalCode', function (value) { 
        return /^((\d{2}-\d{3}))$/.test(value); 
    }, 'Proszę podać prawidłowy kod pocztowy np: 00-00');
    
    $("#registerForm").validate({
         messages: {
           required: "To pole jest wymagane!",
           password: "Proszę wpisać hasło",
           pesel: "Proszę podać 11 cyfrowy numer PESEL",
           representative: "Proszę podać reprezentanta",
           street: {
              required: "Proszę podać ulicę",
              regex: "Proszę podać poprawny adres!",
              minlength: "Ulica musi składać się z min. 7 znaków",
           },
           city: "Proszę podać miasto",
           post_code: {
              required: "Proszę podać kod pocztowy",
              postalCode: "Proszę podać prawidłowy kod pocztowy np: 00-00",
              minlength: jQuery.format("Kod pocztowy musi składać się z {0} znaków"),
              maxlength: jQuery.format("Kod pocztowy musi składać się z {0} znaków")
           },
           nip: "Proszę podać 10 cyfrowy numer NIP",
           /*
           regon: "Proszę podać 9 cyfrowy numer REGON",
           */
           login: {
              required: "Musisz podać login",
              minlength: jQuery.format("Login musi posiadać conajmniej {0} znaków"),
              maxlength: jQuery.format("Login nie może być dłuższy niż {0} znaków"),
              regex: "Login musi sk&#322;adać się tylko i wy&#322;&#261;cznie z znaków <strong>alfanumerycznych</strong>"
           },
           name: "Proszę wypełnić pole",
           email: {
             required: "Musisz podać adres e-mail",
             email: "Musisz podać prawidłowy adres e-mail"
           },
           telephone_number: {
              required: "Musisz podać numer telefonu",
              regex: "Proszę podać nr. tel w formacie +48.221112233"
           },
           mobilephone: {
              regex: "Błędny format! Poprawny format to: +48500123456",
           }
           },
          submitHandler: function() { CreateNewAccount(); }
    });
    
    $("input#login").rules("add", { regex: "^[a-zA-Z0-9\s]{5,20}$" });
    $("input#mobilephone").rules("add", { regex: "^([\+]+)[0-9]{11}$" });
    $("input#telephone_number").rules("add", { regex: "^([\+]+){1}[0-9]{2}\.[0-9]{6,12}$" });
    $("input#street").rules("add", { regex: "^(.*?) (([1-9]{1,3}[a-zA-Z]{1,2}|[0-9]{1,3})\/[0-9]{1,4})|([0-9]{1,3})$" });

});


