// only number returns true
function isNumericCheck(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    return true;
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

$.validator.addMethod("telSt", function(value, element) {
      var tel1 = $('#dsTelefon1').val();
      var tel2 = $('#dsTelefon2').val();
      var tel3 = $('#dsTelefon3').val();
      var tel4 = $('#dsTelefon4').val();
      var tel = tel1 + tel2 + tel3 + tel4; 
      if (tel1 && tel2 && tel3 && tel4 && tel.length < 10)
            return false;
      else
            return true;  
}, "Proszę podać poprawny numer telefonu");

$.validator.addClassRules('telSta', { telSt: true, tel:true });

$.validator.addMethod("telKo", function(value, element) {
      var tel1 = $('#dsKomorka1').val();
      var tel2 = $('#dsKomorka2').val();
      var tel3 = $('#dsKomorka3').val();
      var tel4 = $('#dsKomorka4').val();
      var tel = tel1 + tel2 + tel3 + tel4;
      if (tel1 && tel2 && tel3 && tel4 && tel.length < 13)
            return false;
      else
            return true;  
}, "Proszę podać poprawny numer telefonu");

$.validator.addClassRules('telKom', { telKo: true, tel:true });

$.validator.addMethod("tel", function(value, element) {
      var tel2 = $('#dsTelefon2').val();
      var tel3 = $('#dsTelefon3').val();
      var tel4 = $('#dsTelefon4').val();
      var tel = tel2+tel3+tel4;
      var kom2 = $('#dsKomorka2').val();
      var kom3 = $('#dsKomorka3').val();
      var kom4 = $('#dsKomorka4').val();
      var kom = kom2+kom3+kom4;
      if(!tel && !kom)
            return false;
      else 
            return true;      
}, "Proszę podać przynajmniej jeden z telefonów");

$(function() {
	$('.date-pick').datepicker({
		dateFormat: "yy-mm-dd",
		monthNames: ['Styczeń', 'Luty', 'Marzec', 'Kwiecień', 'Maj', 'Czerwiec', 'Lipiec', 'Sierpień', 'Wrzesień', 'Październik', 'Listopad', 'Grudzień'],
		dayName: ['Poniedziałek', 'Wtorek', 'Środa', 'Czwartek', 'Piątek', 'Sobota', 'Niedziela'],
		dayNamesMin: ['Pn', 'Wt', 'Śr', 'Cz', 'Pt', 'So', 'Ni'], 
		clearText: 'Wyczyść',
		closeText: 'Zamknij',
		currentText: 'Dzisiaj',
		prevText: '<<',
		nextText: '>>'
	});
});

//$.validator.setDefaults({
//	submitHandler: function() { alert("submitted!"); }
//});

$.metadata.setType("attr", "validate");


$.validator.addMethod("nip", function(value, element) {
	var sum = 0;
	var nip = $('#dsNIP1').val() + $('#dsNIP2').val() + $('#dsNIP3').val() + $('#dsNIP4').val();
	
	if(nip.length == 0) {
		//console.log('test');
		return true;
	}
	var mnozniki = '657234567';
	if (!nip.length == 10 && IsNumeric(nip)){
		return false;
	}
	for(x=0; x<=8; x++) {
		sum += parseInt(nip.charAt(x)) * parseInt(mnozniki.charAt(x));
	}
	if ((sum % 11) == nip.charAt(9)) {
		return true;
	} else {
		return false;
	}
	
	//return !this.optional(element) && !this.optional($(element).parent().prev().children("select")[0]);
}, "Proszę wpisać poprawny NIP.");


$.validator.addMethod("pesel", function(value, element) {
	var pesel = value;
	var mnozniki = '13791379131';
	var sumaKontrolna = 0;
	for (i = 1; i <= 11; i++) {
		sumaKontrolna += parseInt(pesel.substr(i - 1, 1)) * parseInt(mnozniki.substr(i - 1, 1));
	};
	if(!(sumaKontrolna%10==0)){
		return false;
	} else {
		return true;
	} 
	
	//return !this.optional(element) && !this.optional($(element).parent().prev().children("select")[0]);
}, "Proszę wpisać poprawny PESEL.");

$.validator.addMethod("kodpocztowy", function(value, element) {
	var rExp = new RegExp("^[0-9]{2}-[0-9]{3}$");
	var kod = value;
	//console.log(kod);
	//console.log(rExp.test(kod)); 
	return rExp.test(kod);
}, "Proszę wprowadzić poprawny kod pocztowy np. 12-345");

$().ready(function() {
	var sn = $('#servername');
	if(sn) {
		snValue = sn.attr('value');
		$('#doSklepu').click(function() {
			var addr = 'http://' + snValue + '/index.php';
			$('.cmxform').attr('action', addr);
		});
		
		$('#genUmowa').click(function() {
			var addr1 = 'http://' + snValue + '/rejestracja/potwierdzenie.php';
			$('.cmxform').attr('action', addr1);
		});
	}

	if($('input:radio[name=dsFirma]:checked').val() == 1) {
		$('.firmaDesc').css('display', 'block');
	}
	$('#dsFirma').click(function (){
		$('.firmaDesc').css('display', 'block');
	});
	$('#dsNieFirma').click(function (){
		$('.firmaDesc').css('display', 'none');
	});
	$('a').each(function (){
		if($(this).attr("rel")	== "external"){
			$(this).attr("target", "_blank");
		}
	});
	
	//$("#dsKodStartera").blur(function() {
		//if ($(this).attr("value") == "") {
			//$("#submit").attr("value", "Przejdz do zamówienia startera >>");
		//} else {
			//$("#submit").attr("value", "Wygeneruj umowę >>");
		//}
	//}); 

	$("#umowa").validate({
		groups: {
			nipnum: "dsNIP1-dsNIP2-dsNIP3-dsNIP4"
		},
		errorPlacement: function(error, element) {
			error.insertAfter(element.parent());
		},
		rules: {
			dsDataWstapienia: "required",
			dsNazwisko: "required",
			dsImie: "required",
			dsDowod: {
				required: true,
				minlength: 9
				},
			dsPESEL: {
				required: true,
				minlength: 11,
				pesel: true
				},
			dsAdres: "required",
			dsKodPoczt: {
				required: true,
				kodpocztowy: true
				},
			dsMiasto: "required",
			dsEmail: {
				required: true,
				email: true
			},
			dsKomorka1: {
				required: true,
				minlength: 4
			},
			dsKomorka2: {
				required: true,
				minlength: 3
			},
			dsKomorka3: {
				required: true,
				minlength: 3
			},
			dsKomorka4: {
				required: true,
				minlength: 3
			},
			dsNIP4: {
				nip: true
				}
			/*
			spNazwa: "required",
			dsNIP1: {
				required: true,
				minlength: 3
				},
			dsNIP2: {
				required: true,
				minlength: 3
				},
			dsNIP3: {
				required: true,
				minlength: 2
				},
			dsNIP4: {
				required: true,
				minlength: 2,
				nip: true
				},
			dsTelefon1: {
				required: true,
				minlength: 3
			},
			dsTelefon2: {
				required: true,
				minlength: 3
			},
			dsTelefon3: {
				required: true,
				minlength: 2
			},
			dsTelefon4: {
				required: true,
				minlength: 2
			},
			//dsNazwaBanku: "required",
			dsNrKonta1: {
				required: true,
				minlength: 2
			},
			dsNrKonta2: {
				required: true,
				minlength: 4
			},
			dsNrKonta3: {
				required: true,
				minlength: 4
			},
			dsNrKonta4: {
				required: true,
				minlength: 4
			},
			dsNrKonta5: {
				required: true,
				minlength: 4
			},
			dsNrKonta6: {
				required: true,
				minlength: 4
			},
			dsNrKonta7: {
				required: true,
				minlength: 4
			}*/
		},
		messages: {
			dsDataWstapienia: "Proszę wybrać datę",
			dsNazwisko: "Proszę wpisać Nazwisko",
			dsImie: "Proszę wpisać Imię",
			dsDowod: "Proszę podać serię i numer<br/> dowodu/paszportu - 9 znaków",
			dsPESEL: "Proszę podać numer PESEL - 11 znaków",
			dsAdres: "Proszę wpisać adres",
			dsMiasto: "Proszę wpisać Miasto/Miejscowość",
			dsEmail: "Proszę podać poprawny adres e-mail",
			dsKodPoczt: "Proszę wpisać poprawny kod pocztowy np. 12-345",
			dsTelefon1: {
				required: "To pole jest wymagane",
				minlength: "Pole powinno zawierać 3 znaki"
			},
			dsTelefon2: {
				required: "To pole jest wymagane",
				minlength: "Pole powinno zawierać 3 znaki"
			},
			dsTelefon3: {
				required: "To pole jest wymagane",
				minlength: "Pole powinno zawierać 2 znaki"
			},
			dsTelefon4: {
				required: "To pole jest wymagane",
				minlength: "Pole powinno zawierać 2 znaki"
			},
			dsKomorka1: {
				required: "To pole jest wymagane",
				minlength: "Pole powinno zawierać 4 znaki"
			},
			dsKomorka2: {
				required: "To pole jest wymagane",
				minlength: "Pole powinno zawierać 3 znaki"
			},
			dsKomorka3: {
				required: "To pole jest wymagane",
				minlength: "Pole powinno zawierać 3 znaki"
			},
			dsKomorka4: {
				required: "To pole jest wymagane",
				minlength: "Pole powinno zawierać 3 znaki"
			},
			dsNrKonta1: {
				required: "To pole jest wymagane",
				minlength: "Proszę wprowadzić 2 cyfry"
			},
			dsNrKonta2: {
				required: "To pole jest wymagane",
				minlength:  "Proszę wprowadzić 4 cyfry"
			},
			dsNrKonta3: {
				required: "To pole jest wymagane",
				minlength:  "Proszę wprowadzić 4 cyfry"
			},
			dsNrKonta4: {
				required: "To pole jest wymagane",
				minlength:  "Proszę wprowadzić 4 cyfry"
			},
			dsNrKonta5: {
				required: "To pole jest wymagane",
				minlength:  "Proszę wprowadzić 4 cyfry"
			},
			dsNrKonta6: {
				required: "To pole jest wymagane",
				minlength:  "Proszę wprowadzić 4 cyfry"
			},
			dsNrKonta7: {
				required: "To pole jest wymagane",
				minlength:  "Proszę wprowadzić 4 cyfry"
			},
			dsNazwaBanku: "Proszę podać nazwę banku",
			dsJestPlat: "Proszę zaznaczyć odpowiednią opcję",
			dsZgodaPrzetwRozlicz: "Wymagane jest potwierdzenie",
			dsZgodaReg: "Wymagane jest potwierdzenie",
			dsZgodaPrzet: "Wymagane jest potwierdzenie",
			dsZgodaDane: "Proszę zaznaczyć odpowiednią opcję",
			dsStatusOdbiorcy: "Wymagane jest potwierdzenie",
			dsPelnoletnia: "Wymagane jest potwierdzenie"
		}
	});
});