/*
 * jQuery validation plug-in for CalangoMusic.com
 * 
 * @copyright Rodolfo Ferreira http://rodolfo42.net
 * @version 0.9
 */

RE = {
	NOTEMPTY: /^.+$/,
	CPF: /^[0-9]{3}\.[0-9]{3}\.[0-9]{3}\-[0-9]{2}$/,
	CNPJ: /^[0-9]{2}\.[0-9]{3}\.[0-9]{3}\/[0-9]{4}\-[0-9]{2}$/,
	MMYYYY: /^(0?[1-9]|1[0-2])\/[1-9][0-9]{3}$/,
	DDMMYYYY: /^(0?[1-9]|[1-2][0-9]|3[0-1])\/(0?[1-9]|1[0-2])\/[1-9][0-9]{3}$/,
	EMAIL: /^[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*@(?:[a-z0-9][-a-z0-9]*\.)*(?:[a-z0-9][-a-z0-9]{0,62})\.(?:(?:[a-z]{2}\.)?[a-z]{2,4}|museum|travel)$/i,
	CEP: /^[0-9]{5}\-[0-9]{3}$/
};

String.prototype.validaCPF = function() {
	cpf = this.replace(/[^0-9]+/g, '');
	if( /^([0]{11})|([1]{11})|([2]{11})|([3]{11})|([4]{11})|([5]{11})|([6]{11})|([7]{11})|([8]{11})|([9]{11})$/.test(cpf) )
	return false;
	add = 0;
	for (i=0; i < 9; i ++)
	add += parseInt(cpf.charAt(i)) * (10 - i);
	rev = 11 - (add % 11);
	if (rev == 10 || rev == 11)
	rev = 0;
	if (rev != parseInt(cpf.charAt(9)))
	return false;
	add = 0;
	for (i = 0; i < 10; i ++)
	add += parseInt(cpf.charAt(i)) * (11 - i);
	rev = 11 - (add % 11);
	if (rev == 10 || rev == 11)
	rev = 0;
	if (rev != parseInt(cpf.charAt(10)))
	return false;
	return true;
};

String.prototype.validaCNPJ = function() {
	cnpj = this.replace(/[^0-9]+/g, '');
	var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
	digitos_iguais = 1;
	if (cnpj.length < 14 && cnpj.length < 15)
	return false;
	for (i = 0; i < cnpj.length - 1; i++)
	if (cnpj.charAt(i) != cnpj.charAt(i + 1))
	{
	digitos_iguais = 0;
	break;
	}
	if (!digitos_iguais)
	{
	tamanho = cnpj.length - 2
	numeros = cnpj.substring(0,tamanho);
	digitos = cnpj.substring(tamanho);
	soma = 0;
	pos = tamanho - 7;
	for (i = tamanho; i >= 1; i--)
	{
	soma += numeros.charAt(tamanho - i) * pos--;
	if (pos < 2)
	pos = 9;
	}
	resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
	if (resultado != digitos.charAt(0))
	return false;
	tamanho = tamanho + 1;
	numeros = cnpj.substring(0,tamanho);
	soma = 0;
	pos = tamanho - 7;
	for (i = tamanho; i >= 1; i--)
	{
	soma += numeros.charAt(tamanho - i) * pos--;
	if (pos < 2)
	pos = 9;
	}
	resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
	if (resultado != digitos.charAt(1))
	return false;
	return true;
	}
	else
	return false;
};

(function($){
	$.extend($.fn, {
		disableValidation: function() {
			if( $(this).is('[validation=true]') ) {
				$(this).attr('validation', false);
			}
			return;
		},
		enableValidation: function() {
			if( $(this).is('[validation=false]') ) {
				$(this).attr('validation', true);
			}
			return;
		},
		bindValidation: function(fn, message) {
			if(!fn) return false;
			var bindFn;
			if( typeof fn == 'function' && fn.constructor != RegExp) {
				bindFn = function(){
					var args = Array.prototype.slice.call(arguments);
					if( !$(this).is('[validation=true]') ) {
						return;
					}
					if( $(this).is(':input') ) {
						args[0] = $(this).val();
					}
					var result = fn.apply(this, args);
					$(this).data('validationResult', !!result);
				};
			} else if( fn.constructor == RegExp && message ) {
				bindFn = function(){
					var result, value, response;
					if( !$(this).is('[validation=true]') ) {
						return;
					}
					if( $(this).is(':input') ) {
						value = $(this).val();
					} else {
						return false;
					}
					result = fn.test(value);
					if(result) {
						response = {
							type: 'success'
						};
					} else {
						response = {
							type: 'error',
							'message': message
						};
					}
					$(this).showInputFeedback(response);
					$(this).data('validationResult', !!result);
				};
			}
			
			if(typeof bindFn == 'function') {
				$(this).attr('validation', true);
				$(this).data('validationResult', false);
				$(this).bind('validate', bindFn);
			}
			
			return this;
		},
		checkValidation: function() {
			//pegar todos os inputs que tem validacao, mas nao estao validados
			var toValidate = $(this).find('[validation=true]');
			if( $(this).is('[validation=true]') ) {
				toValidate.push(this.get(0));
			}
			notValidated = toValidate
				.filter(function(){
					/*if(!$(this).data('validationResult')) {
						console.log(this);
						console.info($(this).data());
					}*/
					return !$(this).data('validationResult');
				});
			if(notValidated.length) return false;
			return true;
		},
		validateAll: function() {
			//pegar todos os inputs que tem validacao, validar
			var toValidate = $(this).find('[validation=true]');
			if( $(this).is('[validation=true]') ) {
				toValidate.push(this.get(0));
			}
			toValidate.trigger('validate');
			return $(this).checkValidation();
		}
	});
})(jQuery);
