/*

Class: validation

Depends: general

Description: Implements field validation functions

*/

function yagi_validation () {

	this.objGeneral = new yagi_general();

	this.validate = function (id, validation, value, message, messageField) {

		var invertResult = false;

		if (validation.charAt(0) == "!") {

			validation = validation.substr(1);
			invertResult = true;

		}

		var validationResult = this[validation](this.objGeneral.getValue(id), value);

		validationResult = (invertResult) ? !validationResult : validationResult;

		if (validationResult === false) {

			this.error(id, message, messageField);

		}

		return validationResult;

	}

	this.error = function (id, message, messageField) {

		var obj = this.objGeneral.getObject(id, true);
		var mainObj = obj;

		if (!obj.errorFocus) {

			obj.errorFocus = obj;

		}

		obj.errorFocus.classNameNoError = obj.errorFocus.className;
		obj.errorFocus.className += " error";

		obj.onblur = function (e) {

			this.errorFocus.className = this.classNameNoError;
			this.errorFocus.classNameNoError = "";

			this.onblur = function (e) { };

		}

		if (message) {

			this.errorAlert(message, messageField);

		}

		obj.focus();

	}

	this.errorAlert = function (message, messageField) {

		if (messageField) {

			this.objGeneral.getObject(messageField).innerHTML = message;

		}
		else {

			alert(message);

		}

	}

	// Validations

	this.empty = function (value) {

		if (this.objGeneral.trim(value).length == 0) {

			return true;

		}
		else {

			return false;

		}

	}

	this.email = function (value) {

		if (this.objGeneral.trim(value) != "") {

			var emailRegExp = new RegExp("^[a-zA-Z0-9]+[_a-zA-Z0-9-]*(\.[_a-z0-9-]+)*@[a-z0-9]+(-[a-z0-9]+)*(\.[a-z0-9-])+\\.([a-z]{2,4})$");

			return emailRegExp.test(value);

		}
		else {

			return false;

		}

	}

	this.equals = function (value, compareValue) {

		if (value == compareValue) {

			return true;

		}
		else {

			return false;

		}

	}

	this.equalsField = function (value, field) {

		return this.equals(value, this.objGeneral.getValue(field));

	}

	this.verifyCharList = function (value, charList) {

		for (var i = 0; i < value.length; i++) {

			if (charList.indexOf(value.charAt(i)) == -1) {

				return false;

			}

		}

		return true;

	}

	this.numbersOnly = function (value) {

		var charList = "0123456789";

		return this.verifyCharList(value, charList);

	}

	this.date = function (value, format) {

		if (!format) {

			format = "yyyy-mm-dd";

		}

		if (this.empty(value)) {

			return false;

		}

		var dayPosition = format.indexOf("dd");
		var monthPosition = format.indexOf("mm");
		var year2digitPosition = format.indexOf("yy");
		var year4digitPosition = format.indexOf("yyyy");

		var day = value.substr(dayPosition, 2);
		var month = value.substr(monthPosition, 2);
		var year = (year4digitPosition == -1) ? value.substr(year2digitPosition, 2) : value.substr(year4digitPosition, 4);

		var verificationDate = new Date(year, month - 1, day);

		if (year.length == 2) {

			year = verificationDate.getFullYear().toString().substr(0, 2) + year;

		}

		if ((verificationDate.getDate() != day)||(verificationDate.getMonth() != month - 1)||(verificationDate.getFullYear() != year)) {

			return false;

		}
		else {

			return true;

		}

	}

	this.cpf = function (value) {

		var digitoVerificador = new Array();

		var cpfInvalido = new Array("00000000000", "11111111111", "22222222222", "33333333333", "44444444444", "55555555555", "66666666666", "77777777777", "88888888888", "99999999999");

		if (!value.length) {

			return false;

		}

		var numerosCPF = value.match(/\d/g);

		if (!numerosCPF) {

			return false;

		}

		if (numerosCPF.length) {

			value = numerosCPF.join("");

		}
		else {

			return false;

		}

		if ((value.length != 11) || (this.objGeneral.inArray(value, cpfInvalido) !== false)) {

			return false;

		}

		var soma = 0;

		for (var i = 0; i < value.length - 2; i++) {

			soma += value.charAt(i) * (10 - i);

		}

		digitoVerificador[0] = 11 - (soma % 11);

		if ((digitoVerificador[0] == 10) || (digitoVerificador[0] == 11)) {

			digitoVerificador[0] = 0;

		}

		var soma = 0;

		for (var i = 0; i < value.length - 1; i++) {

			soma += value.charAt(i) * (11 - i);

		}

		digitoVerificador[1] = 11 - (soma % 11);

		if ((digitoVerificador[1] == 10) || (digitoVerificador[1] == 11)) {

			digitoVerificador[1] = 0;

		}

		if ((digitoVerificador[0] == value.charAt(value.length - 2)) && (digitoVerificador[1] == value.charAt(value.length - 1))) {

			return true;

		}
		else {

			return false;

		}

	}

	this.cnpj = function (value) {

		var digitoVerificador = new Array();

		var digitosReferencia = new Array();

		digitosReferencia[0] = new Array(6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2);
		digitosReferencia[1] = new Array(7, 6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2);

		var cnpjInvalido = new Array("000000000000000", "111111111111111", "222222222222222", "333333333333333", "444444444444444", "555555555555555", "666666666666666", "777777777777777", "888888888888888", "999999999999999");

		if (!value.length) {

			return false;

		}

		var numerosCNPJ = value.match(/\d/g);

		if (!numerosCNPJ) {

			return false;

		}

		if (numerosCNPJ.length) {

			value = numerosCNPJ.join("");

		}
		else {

			return false;

		}

		if (value.length == 14) {

			value = "0" + value;

		}

		if ((value.length != 15) || (this.objGeneral.inArray(value, cnpjInvalido) !== false)) {

			return false;

		}

		var soma = 0;

		for (var i = 0; i < value.length - 2; i++) {

			soma += value.charAt(i) * digitosReferencia[0][i];

		}

		digitoVerificador[0] = 11 - (soma % 11);

		if ((digitoVerificador[0] == 10) || (digitoVerificador[0] == 11)) {

			digitoVerificador[0] = 0;

		}

		var soma = 0;

		for (var i = 0; i < value.length - 1; i++) {

			soma += value.charAt(i) * digitosReferencia[1][i];

		}

		digitoVerificador[1] = 11 - (soma % 11);

		if ((digitoVerificador[1] == 10) || (digitoVerificador[1] == 11)) {

			digitoVerificador[1] = 0;

		}

		if ((digitoVerificador[0] == value.charAt(value.length - 2)) && (digitoVerificador[1] == value.charAt(value.length - 1))) {

			return true;

		}
		else {

			return false;

		}

	}

}
