function isNumericAlphaCheck(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57) && (charCode < 65 || charCode > 122))
        return false;
    return true;
}

// 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 tel = $('#dsTelefon1').val();
      if (tel.length < 8 )
            return false;
      else
            return true;
}, "Ongeldig telefoonnummer");

$.validator.addClassRules('telSta', { telSt: true, tel:true });

$.validator.addMethod("telKo", function(value, element) {
      var tel = $('#dsKomorka1').val();
      if (tel.length < 13)
            return false;
      else
            return true;
}, "Ongeldig telefoonnummer");

$.validator.addClassRules('telKom', { telKo: true, tel:true });

$.validator.addMethod("tel", function(value, element) {
      var tel = $('#dsTelefon1').val();
      var kom = $('#dsKomorka1').val();
      if(!tel && !kom)
            return false;
      else 
            return true;     
}, "Geef minimaal één telefoonnummer");

$(function() {
	$('.date-pick').datepicker({
		dateFormat: "yy-mm-dd",
		minDate: '-109Y',
		maxDate: '+0D',
		yearRange: '1900:2009',
		//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("properBirthDate", function(value, element) {
	var rExp = new RegExp("^[0-9]{4}-[0-9]{2}-[0-9]{2}$");
	var data = value;
	//console.log(kod);
	//console.log(rExp.test(kod)); 
	if (rExp.test(data) || data=="") return true;
}, "Foutief datum formaat: jjjj-mm-dd");

$.validator.addMethod("kodpocztowy", function(value, element) {
	var rExp = new RegExp("^[0-9]{4}[A-Za-z]{2}$");
	var kod = value;
	//console.log(kod);
	//console.log(rExp.test(kod)); 
	return rExp.test(kod);
}, "Proszę wprowadzić poprawny kod pocztowy np. 8856-8872");

$.validator.addMethod("krajCheck", function(value, element){
	switch (value)
	{
		case '0':
			return true
			break;
		case '1':
			return false
			break;
		case '2':
			return false
			break;
		default:
			return false
	}
}, whichMessage);

$.validator.addMethod("dowodCheck", function(value, element){
	var rExp = new RegExp("^[0-9A-Za-z]{9}$");
	var dowod = value;
	return rExp.test(dowod);
},"Dupa")

function whichMessage() {
		if($("#dsKraj").val() == 1){
			return 'Als u iemand wil inschrijven in die in België woont, gelieve het Belgische registratieformulier te gebruiken. <a href="http://www.register-be.be/">www.register-be.be</a>';
		}
		if($("#dsKraj").val() == 2){
			return 'Voor inschrijvingen van nieuwe contacten die niet wonen in België of Nederland, gelieve contact op te nemen met de dienst inschrijvingen: <a href="mailto:contract@fmgroup.be">contract@fmgroup.be</a>';
		}
}

$().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';
			var addr1 = 'http://' + snValue + '/potwierdzenie.php';
			//var addr1 = 'http://' + snValue + '/register-nl-logowanie/potwierdzenie.php';
			$('.cmxform').attr('action', addr1);
		});
	}
	$('#dsFimaMam').click(function (){
		if($(this).attr('checked')){
			$('.firmaDesc').css('display', 'block');
		}else{
			$('.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: {
			spNumer: "required",
			//spNazwa: "required",
			//dsKodStartera: "required",
			dsDataWstapienia: "required",
			dsNazwisko: "required",
			dsImie: "required",
			dsDataUrodzenia: "properBirthDate",
			dsDowod: "dowodCheck",
			dsAdres: "required",
			dsKodPoczt: {
				required: true,
				kodpocztowy: true
				},
			dsMiasto: "required",
			dsKraj: {
				krajCheck: true
			},
			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
			}
			/*
			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: {
			spNumer: "Geef Sponsor Nummer",
			//spNazwa: "Gelieve Naam en achternaam of bedrijf Sponsor",
			dsDataWstapienia: "Proszę wybrać datę",
			dsNazwisko: "Geef uw achternaam",
			dsImie: "Geef uw naam",
			dsDowod: "Geef het nummer van Sociaal Fiscaal Nummer",
			dsAdres: "Geef het adres",
			dsMiasto: "Geef de naam van de stad",
			dsEmail: "Geef een correct e-mail adres (zeer belangrijk)",
			dsKodPoczt: "Geef juiste bericht aantal",
			dsTelefon1: {
				required: "Dit veld is vereist",
				minlength: "Pole powinno zawierać 3 znaki"
			},
			dsTelefon2: {
				required: "Dit veld is vereist",
				minlength: "Pole powinno zawierać 3 znaki"
			},
			dsTelefon3: {
				required: "Dit veld is vereist",
				minlength: "Pole powinno zawierać 2 znaki"
			},
			dsTelefon4: {
				required: "Dit veld is vereist",
				minlength: "Pole powinno zawierać 2 znaki"
			},
			dsKomorka1: {
				required: "Dit veld is vereist",
				minlength: "Pole powinno zawierać 4 znaki"
			},
			dsKomorka2: {
				required: "Dit veld is vereist",
				minlength: "Pole powinno zawierać 3 znaki"
			},
			dsKomorka3: {
				required: "Dit veld is vereist",
				minlength: "Pole powinno zawierać 3 znaki"
			},
			dsKomorka4: {
				required: "Dit veld is vereist",
				minlength: "Pole powinno zawierać 3 znaki"
			},
			dsNrKonta1: {
				required: "Dit veld is vereist",
				minlength: "Proszę wprowadzić 2 cyfry"
			},
			dsNrKonta2: {
				required: "Dit veld is vereist",
				minlength:  "Proszę wprowadzić 4 cyfry"
			},
			dsNrKonta3: {
				required: "Dit veld is vereist",
				minlength:  "Proszę wprowadzić 4 cyfry"
			},
			dsNrKonta4: {
				required: "Dit veld is vereist",
				minlength:  "Proszę wprowadzić 4 cyfry"
			},
			dsNrKonta5: {
				required: "Dit veld is vereist",
				minlength:  "Proszę wprowadzić 4 cyfry"
			},
			dsNrKonta6: {
				required: "Dit veld is vereist",
				minlength:  "Proszę wprowadzić 4 cyfry"
			},
			dsNrKonta7: {
				required: "Dit veld is vereist",
				minlength:  "Proszę wprowadzić 4 cyfry"
			},
			dsNazwaBanku: "Proszę podać nazwę banku",
			dsJestPlat: "Proszę zaznaczyć odpowiednią opcję",
			dsZgodaReg: "Je dient te bevestigen",
			dsZgodaPrzet: "Je dient te bevestigen",
			dsZgodaDane: "Proszę zaznaczyć odpowiednią opcję",
			dsStatusOdbiorcy: "Je dient te bevestigen",
			dsPelnoletnia: "Je dient te bevestigen",
			dsKodStartera: "Geef de juiste starterscode"
		}
	});
});
