/*
 * dodatkowe metody walidacji formularzy jquery
 *
 * copyright Copyright 2010, NetArt
 *  
 * date 2010-01-27
 */

function checkInsideLabel(value, element) {
	if ($(element).hasClass('insideLabel') && value == $(element).attr('jlabel')) return true;
}

jQuery.validator.addMethod("treatInsideLabelAsEmpty", function(value, element) {
	if(value == $(element).attr('jlabel')){
       return false;
    }
    return true; 
}, 'Uzupełnienie pola jest wymagane');

jQuery.validator.addMethod("vourcherCode", function(value) {
    if(value == ''){
       return true;
    }
    return /^[a-zA-Z2-9]{3}-[a-zA-Z0-9]{6,7}$/.test(value); 
}, 'Użyto niedozwolonego znaku!');

jQuery.validator.addMethod("allowedServerName", function(value) {
	return /^[a-z][-a-z0-9]{1,10}[a-z0-9](?:\.(?:nazwa|serwery).pl){0,1}$/.test(value); 
}, 'Użyto niedozwolonego znaku!');

jQuery.validator.addMethod("allowedWebsiteLogin", function(value) {
    return jQuery.validator.methods.allowedServerName(value) || value == '';
}, 'Użyto niedozwolonego znaku!');

jQuery.validator.addMethod("preventletterAndSpecial", function(value) {
	return !/^[0-9]/.test(value) && /^[a-z0-9]{0,}$/.test(value); 
}, 'Użyto niedozwolonego znaku!');

jQuery.validator.addMethod("allowedDomain", function(value) {
	return /^(?:(?:(?:[\x30-\x39\x41-\x5a\x61-\x7a\xb7\xe0-\xff\u0101-\u017e\u0219\u021b][\x30-\x39\x41-\x5a\x61-\x7a\xb7\xe0-\xff\u0101-\u017e\u0219\u021b\-]{0,61}[\x30-\x39\x41-\x5a\x61-\x7a\xb7\xe0-\xff\u0101-\u017e\u0219\u021b]|[\x30-\x39\x41-\x5a\x61-\x7a\xb7\xe0-\xff\u0101-\u017e\u0219\u021b])|(?:[\x30-\x39\u0390\u03ac-\u03ce\u1f00-\u1f07\u1f10-\u1f15\u1f20-\u1f27\u1f30-\u1f37\u1f40-\u1f45\u1f50-\u1f57\u1f60-\u1f67\u1f70-\u1f7d\u1f80-\u1f87\u1f90-\u1f97\u1fa0-\u1fa7\u1fb0-\u1fb7\u1fc2-\u1fc7\u1fd0-\u1fd7\u1fe0-\u1fe7\u1ff2-\u1ff7][\x30-\x39\u0390\u03ac-\u03ce\u1f00-\u1f07\u1f10-\u1f15\u1f20-\u1f27\u1f30-\u1f37\u1f40-\u1f45\u1f50-\u1f57\u1f60-\u1f67\u1f70-\u1f7d\u1f80-\u1f87\u1f90-\u1f97\u1fa0-\u1fa7\u1fb0-\u1fb7\u1fc2-\u1fc7\u1fd0-\u1fd7\u1fe0-\u1fe7\u1ff2-\u1ff7\-]{0,61}[\x30-\x39\u0390\u03ac-\u03ce\u1f00-\u1f07\u1f10-\u1f15\u1f20-\u1f27\u1f30-\u1f37\u1f40-\u1f45\u1f50-\u1f57\u1f60-\u1f67\u1f70-\u1f7d\u1f80-\u1f87\u1f90-\u1f97\u1fa0-\u1fa7\u1fb0-\u1fb7\u1fc2-\u1fc7\u1fd0-\u1fd7\u1fe0-\u1fe7\u1ff2-\u1ff7]|[\x30-\x39\u0390\u03ac-\u03ce\u1f00-\u1f07\u1f10-\u1f15\u1f20-\u1f27\u1f30-\u1f37\u1f40-\u1f45\u1f50-\u1f57\u1f60-\u1f67\u1f70-\u1f7d\u1f80-\u1f87\u1f90-\u1f97\u1fa0-\u1fa7\u1fb0-\u1fb7\u1fc2-\u1fc7\u1fd0-\u1fd7\u1fe0-\u1fe7\u1ff2-\u1ff7])|(?:[\x30-\x39\u05d0-\u05ea][\x30-\x39\u05d0-\u05ea\-]{0,61}[\x30-\x39\u05d0-\u05ea]|[\x30-\x39\u05d0-\u05ea])|(?:[\x30-\x39\u0430-\u045f\u0491-\u04c2][\x30-\x39\u0430-\u045f\u0491-\u04c2\-]{0,61}[\x30-\x39\u0430-\u045f\u0491-\u04c2]|[\x30-\x39\u0430-\u045f\u0491-\u04c2]))\.){1,4}[a-z]{2,4}$/.test(value); 
}, 'Użyto niedozwolonego znaku!');

jQuery.validator.addMethod("allowedLogin", function(value) {
	return /^[A-Za-z0-9][-._A-Za-z0-9]{1,31}$/.test(value); 
}, 'Użyto niedozwolonego znaku!');

jQuery.validator.addMethod("allowedEmailLogin", function(value) {
	/*return /^[A-Za-z0-9][-._A-Za-z0-9]{1,31}$/.test(value);*/ 
    return /^[a-z0-9]+(?:[+_\.-][_a-zA-Z0-9]+){0,31}$/.test(value);
}, 'Użyto niedozwolonego znaku!');

jQuery.validator.addMethod("allowedEmail", function(value) {
    /*return /^[A-Za-z0-9][-._A-Za-z0-9]{1,31}$/.test(value);*/ 
    return /^[_a-zA-Z0-9]+(?:[+_\.-][_a-zA-Z0-9]+)*@(?:[a-zA-Z0-9]+(?:[\.-][a-zA-Z0-9]+)*)+\.[a-zA-Z]{2,}$/.test(value);
}, 'Użyto niedozwolonego znaku!');

jQuery.validator.addMethod("allowedNotRequiredEmail", function(value) {
    /*return /^[A-Za-z0-9][-._A-Za-z0-9]{1,31}$/.test(value);*/
	if(value.length > 0) {
		return /^[_a-zA-Z0-9]+(?:[+_\.-][_a-zA-Z0-9]+)*@(?:[a-zA-Z0-9]+(?:[\.-][a-zA-Z0-9]+)*)+\.[a-zA-Z]{2,}$/.test(value);
	}
	else {
		return true
	}
}, 'Niepoprawny adres e-mail!');

jQuery.validator.addMethod("allowedLoginOld", function(value) {
	return /^[-._A-Za-z0-9]{1,32}$/.test(value); 
}, 'Użyto niedozwolonego znaku!');

jQuery.validator.addMethod("allowedPasswordOld", function(value) {
	return /^[\x20-\xff]{3,64}$/.test(value); 
}, 'Użyto niedozwolonego znaku!');

jQuery.validator.addMethod("allowedPassword8New", function(value) {
    return /^(?=[\x21-\x7e]{8,255}$)(?=.+(\d|[`~!@#$%^\x26*()\-_=+[{\]};:\'\",\x3c.>\/?\\|]))(?=.*[a-z])(?=.*[A-Z].*[A-Z])[\x21-\x7e]*$/.test(value);
}, 'Użyto niedozwolonego znaku!');
    
jQuery.validator.addMethod("allowedPassword8New-oneUpper", function(value) {
    return /^(?=[\x21-\x7e]{8,255}$)(?=.+(\d|[`~!@#$%^\x26*()\-_=+[{\]};:\'\",\x3c.>\/?\\|]))(?=.*[a-z])(?=.*[A-Z])[\x21-\x7e]*$/.test(value);
}, 'Użyto niedozwolonego znaku!');

jQuery.validator.addMethod("allowedLoginRecom", function(value, element) {
	if( checkInsideLabel(value, element) ) return true;
	return /^[-._A-Za-z0-9]{0,32}$/.test(value); 
}, 'Użyto niedozwolonego znaku!');

jQuery.validator.addMethod("allowedName", function(value) {
	return /^[\x20-\x22\x26-\x29\x2b-\x3a\x3f-\x5a\x5f-\x7a\xa1-\xff\u0101-\u017e\u0390\u03ac-\u03ce\u05d0-\u05ea\u0430-\u045f\u0491-\u04c2]{0,255}$/.test(value);
}, 'Użyto niedozwolonego znaku!');

jQuery.validator.addMethod("allowedShortName", function(value) {
	return /^[\x20-\x22\x26-\x29\x2b-\x39\x40-\x5a\x5f-\x7a\xa1-\xff\u0101-\u017e\u0390\u03ac-\u03ce\u05d0-\u05ea\u0430-\u045f\u0491-\u04c2]{1,50}$/.test(value);
}, 'Użyto niedozwolonego znaku!');

jQuery.validator.addMethod("allowedStreet", function(value) {
	return /^[\x20\x22\x26\x27\x2c-\x39\x41-\x5a\x5f-\x7a\xa1-\xff\u0101-\u017e\u0390\u03ac-\u03ce\u05d0-\u05ea\u0430-\u045f\u0491-\u04c2]{1,255}$/.test(value);
}, 'Użyto niedozwolonego znaku!');

jQuery.validator.addMethod("allowedPosition", function(value) {
	if (value == '') return true; 
	return /^[\x20-\x22\x26-\x29\x2b-\x3a\x3f-\x5a\x5f-\x7a\xa1-\xff\u0101-\u017e\u0390\u03ac-\u03ce\u05d0-\u05ea\u0430-\u045f\u0491-\u04c2]{1,255}$/.test(value);
}, 'Użyto niedozwolonego znaku!');

jQuery.validator.addMethod("allowedComment", function(value) {
	return /.{0,}/.test(value); // /.+/ 
}, 'Użyto niedozwolonego znaku!');

jQuery.validator.addMethod("selectWithout0", function(value) {
	return value != 0;
}, 'Należy wybrać pozycję!');

jQuery.validator.addMethod("preventSpace", function(value) {
	return !/[ ]/.test(value);
}, 'Pole nie może zawirać pustego znaku "spacji"!');

jQuery.validator.addMethod("passwordWithoutLogin", function(value) {
	var $login = $("#login").val();
	if (value.toLowerCase().indexOf($login.toLowerCase()) > -1) {
		return false;
	} else return true;
}, 'Hasło nie może zawierać w sobie loginu!');

jQuery.validator.addMethod("preventTextFrom", function(value, element, relatedField) {
	var $relatedValue = $(relatedField).val();
	if ($relatedValue == '') return true;
	if (value.toLowerCase().indexOf($relatedValue.toLowerCase()) > -1) {
			return false;
		} else return true;
}, 'Pole nie może zawierać zawartości pola powiązanego!');

jQuery.validator.addMethod("preventEqualFields", function(value, element, relatedField) {
	var $relatedValue = $(relatedField).val();
	if ($relatedValue == '') return true;
	if (value.toLowerCase() == $relatedValue.toLowerCase()) {
			return false;
		} else return true;
}, 'Pola \"Hasło\" i \"Powtórzenie hasła\" muszą mieć taką samą zawartość.');

jQuery.validator.addMethod("preventRelatedEqual", function(value, element, relatedField) {
    var $relatedValue = $(relatedField).val();
    if (value == $relatedValue) {
            return true;
        } else {
            return false;
        }
}, 'Pole nie może być równe zawartości pola powiązanego!');

jQuery.validator.addMethod("preventEqualInField", function(value, element, relatedField) {
    var $relatedValue = $(relatedField).val();
    if (value == $relatedValue) {
            return false;
        } else {
            return true;
        }
}, 'Pole nie może być równe zawartości pola powiązanego!');

jQuery.validator.addMethod("preventValue", function(value, element, bannedValue) {
	if (value == bannedValue) {
			return false;
		} else return true;
}, 'Wprowadzona wartość jest zabroniona');

jQuery.validator.addMethod("min2digits", function(value) {
	return /[0-9]{1}.*[0-9]{1}/.test(value);
}, 'Wymagane przynajmniej dwie cyfry!');

jQuery.validator.addMethod("min2upper", function(value) {
	return /[A-Z].*[A-Z]/.test(value);
}, 'Wymagane przynajmniej dwie duże litery!');

jQuery.validator.addMethod("min1upper", function(value) {
	return /[A-Z]/.test(value);
}, 'Wymagana przynajmniej jedna duża litera!');

jQuery.validator.addMethod("min1lower", function(value) {
	return /[a-z]{1}/.test(value);
}, 'Wymagana przynajmniej jedna mała litera!');

jQuery.validator.addMethod("min1digitOrSpecial", function(value) {
	return /[0-9]{1}|[`~!@#$%^\x26*()\-_=+[{\]};:\'\",\x3c.>\/?\\|]/.test(value);
}, 'Wymagana przynajmniej jedna cyfra lub znak specjalny!');

jQuery.validator.addMethod("onlyLetters", function(value) {
	return /^[a-zA-ZąćęłńóśżźĄĆĘŁŃÓŚŻŹ\-\x20]{0,}$/.test(value);
}, 'Można użyć tylko liter!');

jQuery.validator.addMethod("onlySmallLetters", function(value, element) {
	if( checkInsideLabel(value, element) ) return true;
	return !/[A-Z]{1}/.test(value);
}, 'Można użyć tylko małych liter!');

jQuery.validator.addMethod("onlyDigits", function(value, element) {
	if( checkInsideLabel(value, element) ) return true;
	return /^[0-9\x20\x2d]*$/.test(value);
}, 'Można użyć tylko cyfr!');

jQuery.validator.addMethod("phoneMinLength", function(value, element) {
	if( checkInsideLabel(value, element) ) return true;
	var new_value = value.replace(/ /g, '').replace(/-/g, '');
	if( (new_value.length>=6) || (new_value.length==0) ) return true;
	return false;
}, 'Wprowadzono zbyt mało znaków!');

jQuery.validator.addMethod("phoneMaxLength", function(value) {
	var new_value = value.replace(/ /g, '').replace(/-/g, '');
	if(new_value.length<=12) return true;
	return false;
}, 'Wprowadzono zbyt wiele znaków!');

jQuery.validator.addMethod("validatePostalCode", function(value) {
	if( $("#countryCode option:selected").val() != "pl") {
		return /^[A-Za-z0-9\s\-]{1,10}$/.test(value);
	} else {
		return /^[0-9]{2}-[0-9]{3}$/.test(value);
	}
}, 'Proszę wprowadzić poprawny kod pocztowy!');

jQuery.validator.addMethod("validatePolishPostalCode", function(value) {
	return /^[0-9]{2}-[0-9]{3}$/.test(value);
}, 'Proszę wprowadzić poprawny kod pocztowy!');

jQuery.validator.addMethod("validateCorrPostalCode", function(value) {
	if( $("#corrCountryCode option:selected").val() != "pl") {
		return /^[A-Za-z0-9\s\-]{1,10}$/.test(value);
	} else {
		return /^[0-9]{2}-[0-9]{3}$/.test(value);
	}
}, 'Proszę wprowadzić poprawny kod pocztowy!');

jQuery.validator.addMethod("validateLocalNo", function(value) {
	if( $("#countryCode option:selected").val() != "pl") {
		return /^[A-Za-z\-0-9\/\s\.,]{1,20}$/.test(value);
	} else {
		return /^[A-Za-z\-0-9\/\s\.]{1,20}$/.test(value);
	}
}, 'Proszę wprowadzić poprawny numer!');

jQuery.validator.addMethod("validateCorrLocalNo", function(value) {
	if( $("#corrCountryCode option:selected").val() != "pl") {
		return /^[A-Za-z\-0-9\/\s\.,]{1,20}$/.test(value);
	} else {
		return /^[A-Za-z\-0-9\/\s\.]{1,20}$/.test(value);
	}
}, 'Proszę wprowadzić poprawny numer!');

jQuery.validator.addMethod("validateTown", function(value) {
	if( $("#countryCode option:selected").val() != "pl") {
		return /^[\x20\x22\x26-\x29\x2c-\x39\x41-\x5a\x5f-\x7a\xa1-\xff\u0101-\u017e\u0390\u03ac-\u03ce\u05d0-\u05ea\u0430-\u045f\u0491-\u04c2]{1,255}$/.test(value);
	} else {
		return /^[\x20\x22\x26\x27\x2c-\x39\x41-\x5a\x5f-\x7a\xa1-\xff\u0101-\u017e\u0390\u03ac-\u03ce\u05d0-\u05ea\u0430-\u045f\u0491-\u04c2]{1,255}$/.test(value);
	}
}, 'Użyto niedozwolonego znaku!');

jQuery.validator.addMethod("validateCorrTown", function(value) {
	if( $("#corrCountryCode option:selected").val() != "pl") {
		return /^[\x20\x22\x26-\x29\x2c-\x39\x41-\x5a\x5f-\x7a\xa1-\xff\u0101-\u017e\u0390\u03ac-\u03ce\u05d0-\u05ea\u0430-\u045f\u0491-\u04c2]{1,255}$/.test(value);
	} else {
		return /^[\x20\x22\x26\x27\x2c-\x39\x41-\x5a\x5f-\x7a\xa1-\xff\u0101-\u017e\u0390\u03ac-\u03ce\u05d0-\u05ea\u0430-\u045f\u0491-\u04c2]{1,255}$/.test(value);
	}
}, 'Użyto niedozwolonego znaku!');


jQuery.validator.addMethod("validateUrl", function(value) {
	return /^(?:(?:ht|f)tps?\:\/\/)(?:[^\:;@#]+(?:(?:\:?[^\:;@#]+)|\:?)?@)?(?:(?:[a-z0-9\-_\xb1\xb3\xb6\xbc\xbf\xe4\xe6\xea\xf1\xf3\xf6\xfc\u0105\u0107\u0119\u0142\u0144\u015b\u017a\u017c]{1,64}\.){1,4}[a-z]{2,6}|(?:(?:[0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\.){3}(?:[0-1]?[0-9]{1,2}|2[0-4][0-9]|25[0-5]))(?::[0-9]{2,5})?(?:(?:\/.*)|\/?)$/.test(value.toLowerCase());	
}, 'Wprowadzony adres url jest niepoprawny!');

jQuery.validator.addMethod("validateWebsite", function(value) {
    return /^(?:(?:(?:[\x30-\x39\x41-\x5a\x61-\x7a\xb7\xe0-\xff\u0101-\u017e\u0219\u021b][\x30-\x39\x41-\x5a\x61-\x7a\xb7\xe0-\xff\u0101-\u017e\u0219\u021b\-]{0,61}[\x30-\x39\x41-\x5a\x61-\x7a\xb7\xe0-\xff\u0101-\u017e\u0219\u021b]|[\x30-\x39\x41-\x5a\x61-\x7a\xb7\xe0-\xff\u0101-\u017e\u0219\u021b])|(?:[\x30-\x39\u0390\u03ac-\u03ce\u1f00-\u1f07\u1f10-\u1f15\u1f20-\u1f27\u1f30-\u1f37\u1f40-\u1f45\u1f50-\u1f57\u1f60-\u1f67\u1f70-\u1f7d\u1f80-\u1f87\u1f90-\u1f97\u1fa0-\u1fa7\u1fb0-\u1fb7\u1fc2-\u1fc7\u1fd0-\u1fd7\u1fe0-\u1fe7\u1ff2-\u1ff7][\x30-\x39\u0390\u03ac-\u03ce\u1f00-\u1f07\u1f10-\u1f15\u1f20-\u1f27\u1f30-\u1f37\u1f40-\u1f45\u1f50-\u1f57\u1f60-\u1f67\u1f70-\u1f7d\u1f80-\u1f87\u1f90-\u1f97\u1fa0-\u1fa7\u1fb0-\u1fb7\u1fc2-\u1fc7\u1fd0-\u1fd7\u1fe0-\u1fe7\u1ff2-\u1ff7\-]{0,61}[\x30-\x39\u0390\u03ac-\u03ce\u1f00-\u1f07\u1f10-\u1f15\u1f20-\u1f27\u1f30-\u1f37\u1f40-\u1f45\u1f50-\u1f57\u1f60-\u1f67\u1f70-\u1f7d\u1f80-\u1f87\u1f90-\u1f97\u1fa0-\u1fa7\u1fb0-\u1fb7\u1fc2-\u1fc7\u1fd0-\u1fd7\u1fe0-\u1fe7\u1ff2-\u1ff7]|[\x30-\x39\u0390\u03ac-\u03ce\u1f00-\u1f07\u1f10-\u1f15\u1f20-\u1f27\u1f30-\u1f37\u1f40-\u1f45\u1f50-\u1f57\u1f60-\u1f67\u1f70-\u1f7d\u1f80-\u1f87\u1f90-\u1f97\u1fa0-\u1fa7\u1fb0-\u1fb7\u1fc2-\u1fc7\u1fd0-\u1fd7\u1fe0-\u1fe7\u1ff2-\u1ff7])|(?:[\x30-\x39\u05d0-\u05ea][\x30-\x39\u05d0-\u05ea\-]{0,61}[\x30-\x39\u05d0-\u05ea]|[\x30-\x39\u05d0-\u05ea])|(?:[\x30-\x39\u0430-\u045f\u0491-\u04c2][\x30-\x39\u0430-\u045f\u0491-\u04c2\-]{0,61}[\x30-\x39\u0430-\u045f\u0491-\u04c2]|[\x30-\x39\u0430-\u045f\u0491-\u04c2]))\.){1,4}[a-z]{2,4}$/.test(value.toLowerCase());    
}, 'Wprowadzony adres jest niepoprawny!');

jQuery.validator.addMethod("validatePesel", function(value) {
	var weights = [1, 3, 7, 9, 1, 3, 7, 9, 1, 3];
	if (value == null || !checkControlSum(value, weights, 10, false))
	{
		return false;
	}
	var peselAnalyzer = new NetartPeselAnalyzer(value.toString(), new Date);
	if(!peselAnalyzer.isValid())
	{
		return false;
	}
	return true;
}, 'Niepoprawny numer PESEL!');

jQuery.validator.addMethod("validateNip", function(value, element) {
	if( checkInsideLabel(value, element) ) return true;
	
	if (value != '') {
		var getFieldPattern = function() {
			return /^\d{10}|\d{3}\-\d{3}\-\d{2}\-\d{2}|\d{3}\-\d{2}\-\d{2}\-\d{3}$/;
		}
		var regexp = new RegExp(getFieldPattern());
		var weights = [6,5,7,2,3,4,5,6,7];
		if (value == null || !checkControlSum(value, weights, 11, true) || !regexp.test(value) )
		{
			return false;
		}
	}
	return true;
}, 'Niepoprawny numer NIP!');

jQuery.validator.addMethod("validateRegon", function(value) {
	var controlSum = false;
	var mixedLength = value.length;
	if (mixedLength == 9)
	{
		var weights = [8, 9, 2, 3, 4, 5, 6, 7];
		controlSum = checkControlSum(value, weights, 11, true);
	}
	else if (mixedLength == 14)
	{
		var weights = [2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8];
		controlSum = checkControlSum(value, weights, 11, true);
	}
	if (!controlSum)
	{
		return false;
	}
	return true;
}, 'Niepoprawny numer REGON!');

jQuery.validator.addMethod("validatePrefix", function(value) {
	if (value == '') return true; 
	return /^\+[0-9]{2,3}$/.test(value);
}, 'Numer kierunkowy kraju niepoprawny');

jQuery.validator.addMethod("validateCaptchaLength", function(value) {
	if( value.length == 4 ) return true;
	return false;
}, 'Pole powinno zawierac 4 znaki');

jQuery.validator.addMethod("canChangeToPolandWithoutPesel", canChangeToPoland, 'Nie można zmienić kraju, brak numeru pesel');
jQuery.validator.addMethod("canChangeToPolandWithoutRegon", canChangeToPoland, 'Nie można zmienić kraju, brak numeru regon');

jQuery.validator.addMethod("validateVoucherCode", function(value) {
	return value.length > 0;
}, 'Podany kod rabatowy nie jest prawidłowy.');

jQuery.validator.addMethod("validateMultiDomain", function(value) {
    var val = value.replace(/\s+/g, '');
    var error = 0;
    val = val.split(',');

    for(var i = 0, max = val.length; i < max; i++){
        if(/^(?:(?:(?:[\x30-\x39\x41-\x5a\x61-\x7a\xb7\xe0-\xff\u0101-\u017e\u0219\u021b][\x30-\x39\x41-\x5a\x61-\x7a\xb7\xe0-\xff\u0101-\u017e\u0219\u021b\-]{0,61}[\x30-\x39\x41-\x5a\x61-\x7a\xb7\xe0-\xff\u0101-\u017e\u0219\u021b]|[\x30-\x39\x41-\x5a\x61-\x7a\xb7\xe0-\xff\u0101-\u017e\u0219\u021b])|(?:[\x30-\x39\u0390\u03ac-\u03ce\u1f00-\u1f07\u1f10-\u1f15\u1f20-\u1f27\u1f30-\u1f37\u1f40-\u1f45\u1f50-\u1f57\u1f60-\u1f67\u1f70-\u1f7d\u1f80-\u1f87\u1f90-\u1f97\u1fa0-\u1fa7\u1fb0-\u1fb7\u1fc2-\u1fc7\u1fd0-\u1fd7\u1fe0-\u1fe7\u1ff2-\u1ff7][\x30-\x39\u0390\u03ac-\u03ce\u1f00-\u1f07\u1f10-\u1f15\u1f20-\u1f27\u1f30-\u1f37\u1f40-\u1f45\u1f50-\u1f57\u1f60-\u1f67\u1f70-\u1f7d\u1f80-\u1f87\u1f90-\u1f97\u1fa0-\u1fa7\u1fb0-\u1fb7\u1fc2-\u1fc7\u1fd0-\u1fd7\u1fe0-\u1fe7\u1ff2-\u1ff7\-]{0,61}[\x30-\x39\u0390\u03ac-\u03ce\u1f00-\u1f07\u1f10-\u1f15\u1f20-\u1f27\u1f30-\u1f37\u1f40-\u1f45\u1f50-\u1f57\u1f60-\u1f67\u1f70-\u1f7d\u1f80-\u1f87\u1f90-\u1f97\u1fa0-\u1fa7\u1fb0-\u1fb7\u1fc2-\u1fc7\u1fd0-\u1fd7\u1fe0-\u1fe7\u1ff2-\u1ff7]|[\x30-\x39\u0390\u03ac-\u03ce\u1f00-\u1f07\u1f10-\u1f15\u1f20-\u1f27\u1f30-\u1f37\u1f40-\u1f45\u1f50-\u1f57\u1f60-\u1f67\u1f70-\u1f7d\u1f80-\u1f87\u1f90-\u1f97\u1fa0-\u1fa7\u1fb0-\u1fb7\u1fc2-\u1fc7\u1fd0-\u1fd7\u1fe0-\u1fe7\u1ff2-\u1ff7])|(?:[\x30-\x39\u05d0-\u05ea][\x30-\x39\u05d0-\u05ea\-]{0,61}[\x30-\x39\u05d0-\u05ea]|[\x30-\x39\u05d0-\u05ea])|(?:[\x30-\x39\u0430-\u045f\u0491-\u04c2][\x30-\x39\u0430-\u045f\u0491-\u04c2\-]{0,61}[\x30-\x39\u0430-\u045f\u0491-\u04c2]|[\x30-\x39\u0430-\u045f\u0491-\u04c2]))\.){1,4}[a-z]{2,4}$/.test(val[i])){
        }else{
            error++;
        }
    }
    if(error > 0){
        return false;
    }else{
        return true;
    }
}, 'Jedna z podanych domen nie jest prawidłowa.');

jQuery.validator.addMethod("validateServerID", function(value) {
    if (value == '') return true; 
    return /^(?:[a-z0-9][a-z0-9\-]{0,24}[a-z0-9]|[a-z0-9]).(?:nazwa|serwery).pl$/.test(value);
}, 'Identyfikator serwera jest niepoprawny.');

/*
 * Dodatkowe funkcje walidacji numeru PESEL, NIP, REGON
 */

 /**
 * @access private
 */
function checkControlSum(str, weights, modulo, allow_high)
{
	str = str.replace(/[^\d]/g,"");
	if(!str.length) return false;

	var nsize = str.length;
	var j = 0, sum = 0, control = 0;
	var csum = str.substring(nsize - 1);

	for (var i = 0; i < nsize - 1; i++)
	{
			j = parseInt(str.charAt(i));
			sum += j * weights[i];
	}
	if(sum == 0 || str.length < 9)
	{
		return false;
	}
	control = sum % modulo;

	if(allow_high == false) {
			control = 10 - control;
	}

	if (control == 10) {
		control = 0;
	}
	return control == csum;
}

Date.prototype.after = function(p_date) {
    return this.valueOf() > p_date.valueOf();
}

Date.prototype.before = function(p_date) {
    return this.valueOf() < p_date.valueOf();
}

Date.prototype.equal = function(p_date) {
    return this.valueOf() == p_date.valueOf();
}

function NetartPeselAnalyzer(p_PESEL, p_now) {

  this.PESEL = null;

  this.now = null;

  this.minAllowAge = 13;

  this.maxAllowAge = 100;

  this.testedBornDate = null;

  this.minBornDate = null;

  this.maxBornDate = null;

  this.isAdult = function() {
    var bornDate = this.testedBornDate.getCalBornDate();
    if (bornDate == null) {
      return false;
    }

    if (bornDate.after(this.now)) {
      return false;
    }

    var enoughYoung = this.testedBornDate.after(this.minBornDate);
    var enoughOld = this.testedBornDate.before(this.maxBornDate);

    return enoughYoung && enoughOld;
  }

  this.getMinAllowBornDate = function() {
    var minAllowBornDate = null;
    if (this.maxAllowAge != null) {
      minAllowBornDate = cloneDate(this.now);
      minAllowBornDate.setFullYear(minAllowBornDate.getFullYear() - this.maxAllowAge);
      minAllowBornDate = this.truncateToDay(minAllowBornDate);
    }
    return minAllowBornDate;
  }

  this.getMaxAllowBornDate = function() {
    var maxAllowBornDate = null;
    if (this.minAllowAge != null) {
      maxAllowBornDate = cloneDate(this.now);
      maxAllowBornDate.setFullYear(maxAllowBornDate.getFullYear() - this.minAllowAge);
      maxAllowBornDate = this.truncateToDay(maxAllowBornDate);
    }
    return maxAllowBornDate;
  }

  this.isValid = function() {
    var weights = [1,3,7,9,1,3,7,9,1,3];
    if (this.PESEL == null || !checkControlSum(this.PESEL, weights, 10, false)) {
      return false;
    }
    return this.isAdult();
  }

  this.truncateToDay = function(date) {
    var pB = cloneDate(date);
    pB.setHours(0);
    pB.setMinutes(0);
    pB.setSeconds(0);
    return pB;
  }

  this.getPESEL = function() {
    return PESEL;
  }

  this.getNow = function() {
    return now;
  }

  this.setNow = function(now) {
    this.now = now;
  }

  this.getMinAllowAge = function() {
    return minAllowAge;
  }

  this.getMaxAllowAge = function() {
    return maxAllowAge;
  }

  
    this.PESEL = p_PESEL.replace("\\D*", "");
    this.now = p_now;

    this.testedBornDate = new BornDate(peselToCalDate(this.PESEL));
    this.minBornDate = new BornDate(this.getMinAllowBornDate());
    this.maxBornDate = new BornDate(this.getMaxAllowBornDate());
};


function BornDate(p_date) {

    var calBornDate = null;

  this.after = function(p_bornDate) {
    var after = false;
    var checkCalendarDate = p_bornDate.getCalBornDate();
    if (this.calBornDate != null && checkCalendarDate != null) {
      after = this.calBornDate.after(checkCalendarDate);
    }
    return after;
  }

  this.before = function(p_bornDate) {
    var before= false;
    var checkCalendarDate = p_bornDate.getCalBornDate();
    if (this.calBornDate != null && checkCalendarDate != null) {
      before = this.calBornDate.before(checkCalendarDate);
    }
    return before;
  }

  this.getCalBornDate = function() {
    return this.calBornDate;
  }

  this.setCalBornDate = function(calBornDate) {
    this.calBornDate = calBornDate;
  }

  this.calBornDate = p_date;
}


function peselToCalDate(p_pesel) {

    var calBornDate = null;
    var dayDigit = p_pesel.substring(4, 6);
    var mountDigit = p_pesel.substring(2, 4);
    var yearDigit = p_pesel.substring(0, 2);
    var day = parseInt(dayDigit);
    var month = -1;
    var monthCode = parseInt(mountDigit);
    var year = -1;

    if (monthCode>=1 && monthCode<=12) {
      month = monthCode;
      yearDigit = "19" + yearDigit;
    } else if (monthCode>=81 && monthCode<=92) {
      month = monthCode - 80;
      yearDigit = "18" + yearDigit;
    } else if (monthCode>=21 && monthCode<=32) {
      month = monthCode - 20;
      yearDigit = "20" + yearDigit;
    } else if (monthCode>=41 && monthCode<=52) {
      month = monthCode - 40;
      yearDigit = "21" + yearDigit;
    } else if (monthCode>=61 && monthCode<=72) {
      month = monthCode - 60;
      yearDigit = "22" + yearDigit;
    }

	// Date object's months start from 0
    month--;

    var sMount = month.toString();

    if (sMount.length == 1) {
      sMount = "0" + sMount;
    }

    year = parseInt(yearDigit);
    return new Date(year, sMount, dayDigit);
}


function cloneDate(p_date) {

    return new Date(p_date);
}

function canChangeToPoland(value, element, params) {
	var $relatedValue = $(params[0]).val();
	var $isProperStatus = $(params[1]).is(':checked') ? true : false;
	
	if ($relatedValue.length == 0 && value == 'pl' && $isProperStatus) return false;
	return true;
}
