jQuery.validator.addMethod("msisdn", function(value, element){
	var v1 = value.charAt(0);
	var v2 = value.charAt(1);
	var msisdn = parseInt(value, 10);
	
	if(isNaN(msisdn) || msisdn < 210000000 || msisdn > 969999999 || value.length != 9)
		return false;
	
	if(v1 == "2")
		return true;
	else if(v1 != "9")
		return false;
	
	if(v2 != "1" && v2 != "2" && v2 != "3" && v2 != "6")
		return false;
	
	return true;
}, "Introduza um n&uacute;mero de contacto v&aacute;lido!");

jQuery.validator.addMethod("zipCode", function(value, element){
	var zip = parseInt(value, 10);
	if(isNaN(zip) || zip < 1000 || zip > 9999 || value.length != 4)
		return false;
	
	return true;
}, "Introduza um c&oacute;digo postal v&aacute;lido!");

jQuery.validator.addMethod("validate_nif", function(value, element){
	var c;
	var checkDigit = 0;

	if(value == '123456789')
    	return false;

	if(value != null && value.length == 9){
		c = value.charAt(0);

		if(c == '1' || c == '2' || c == '5' || c == '6' || c == '8' || c == '9'){
			checkDigit = c * 9;
			
			var i = 0;
			for(i = 2; i <= 8; i++){
				checkDigit += value.charAt(i-1) * (10-i);
			}

			checkDigit = 11 - (checkDigit % 11);

			if(checkDigit >= 10)
				checkDigit = 0;

			if(checkDigit == value.charAt(8))
				return true;
		}
	}
	
	return false;
}, "Introduza um nif v&aacute;lido!");

jQuery.validator.addMethod("username", function(value, element){
	var validChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
	
	for(var i = 0; i < value.length; i++){
		var checks = false;
		
		for(var j = 0; j < validChars.length; j++){
			if(value.charAt(i) == validChars.charAt(j)){
				checks = true;
				break;
			}
		}
		
		if(!checks) return false;
	}
	
	return true;
}, "O nome de utilizador s&oaacute; pode conter letras e n&uaacute;ros!");

