// Criação de elementos
jQuery.fn.create = function(tag,origin,html) {
	if(origin == "appendTo") {
		return $(document.createElement(tag)).html(html).appendTo(this);
	}

	if(origin == "before") {
		return $(document.createElement(tag)).html(html).before(this);
	}

	if(origin == "after") {
		return $(document.createElement(tag)).html(html).after(this);
	}
};

// Validação para data
jQuery.fn.date = function() {
	$(this).mask('99/99/9999');

	$(this).keyup(function() {
		var dia = $(this).val()[0] + '' + $(this).val()[1];
		var mes = $(this).val()[3] + '' + $(this).val()[4];

		var ano = $(this).val()[6] + '' + $(this).val()[7] + '' + $(this).val()[8] + '' + $(this).val()[9];

		if(dia > (new Date((new Date).getFullYear(), (new Date).getMonth() + 1, 0)).getDate()) {
			$(this).val('');
			$(this).focus();
		}

		if(mes <= (new Date).getMonth() + 1) {
			if(dia < (new Date).getDate()) {
				$(this).val('');
				$(this).focus();
			}
		}

		if(mes < (new Date).getMonth() + 1) {
			$(this).val('');
			$(this).focus();
		}

		if(mes > (new Date((new Date).getFullYear(), 0, 0)).getMonth() + 1) {
			$(this).val('');
			$(this).focus();
		}

		if(dia > (new Date((new Date).getFullYear(), mes, 0)).getDate()) {
			$(this).val('');
			$(this).focus();
		}

		if(ano < (new Date).getFullYear()) {
			$(this).val('');
			$(this).focus();
		}

		if(dia > (new Date(ano, mes, 0)).getDate()) {
			$(this).val('');
			$(this).focus();
		}
	});
};

// Validação para período
jQuery.fn.period = function() {
	$(this).mask('99:99h às 99:99h');

	$(this).keyup(function() {
		var inicio = $(this).val()[0] + '' + $(this).val()[1];
		var inicio_min = $(this).val()[3] + '' + $(this).val()[4];

		var fim = $(this).val()[10] + '' + $(this).val()[11];
		var fim_min = $(this).val()[13] + '' + $(this).val()[14];

		if(inicio > 23) {
			$(this).val('');
			$(this).focus();
		}

		if(inicio_min > 59) {
			$(this).val('');
			$(this).focus();
		}

		if(fim > 23) {
			$(this).val('');
			$(this).focus();
		}

		if(fim_min > 59) {
			$(this).val('');
			$(this).focus();
		}

		if(fim < inicio) {
			$(this).val('');
			$(this).focus();
		}
	});
};

// Validação para permitir apenas números
jQuery.fn.numbers = function(formulario, label) {
	$(this).keyup(function() {
		if(isNaN($(this).val())) {
			$(this).val('');

			$('#' + formulario + ' label[for="' + label + '"] p').remove();
			$('#' + formulario + ' label[for="' + label + '"]').create('p', 'appendTo', 'Apenas <strong>n&uacute;meros</strong>.');
		} else {
			$('#' + formulario + ' label[for="' + label + '"] p').remove();
		}
	});
};

// Validação para permitir apenas números
jQuery.fn.currency = function() {
	$(this).mask('999,99');
};

// Validação para CPF
jQuery.fn.cpf = function() {
	var cpf = $(this).val().replace(/\.|-|\//gi, '');

	var expReg = /^0+$|^1+$|^2+$|^3+$|^4+$|^5+$|^6+$|^7+$|^8+$|^9+$/;
	var a = [];
	var b = new Number;
	var c = 11;

	for (i = 0; i < 11; i++) {
		a[i] = cpf.charAt(i);

		if(i < 9) {
			b += (a[i] * --c);
		}
	}

	if((x = b % 11) < 2) {
		a[9] = 0;
	} else {
		a[9] = 11 - x;
	}

	b = 0;
	c = 11;

	for(y = 0; y < 10; y++) {
		b += (a[y] * c--);
	}

	if((x = b % 11) < 2) {
		a[10] = 0;
	} else {
		a[10] = 11 - x;
	}

	if((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10]) || cpf.match(expReg)) {
		return false;
	} else {
		return true;
	}
}
