var Utils = {};
Utils.Validator = function() {
}

Utils.Validator.prototype.type = {
	'REQUIRED':0,
	'EMAIL':1,
	'CPF':2,
	'CNPJ':3,
	'CEP':4,
	'PHONE':5,
	'NUMBER':6,
	'COMPARE':7
};

Utils.Validator.prototype.check = function(value, type) {
	switch(type) {
		case this.type.EMAIL:
			var regexp = RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
			return regexp.test(value);
			break;
		case this.type.CPF:
			return CPFValidate(value);
			break;
		case this.type.CNPJ:
			return CNPJValidate(value);
			break;
		case this.type.CEP:
			var regexp = RegExp(/^\d{5}-?\d{3}?/);
			return regexp.test(value);
			break;
		case this.type.PHONE:
			return phoneValidate(value);
			break;
		case this.type.NUMBER:
			var regexp = RegExp(/^\d+$/);
			return regexp.test(value);
			break;
		case this.type.COMPARE:
			return value[0] == value[1];
			break;
		case this.type.REQUIRED:
		default:
			return value != '';
	}
}

Utils.Mask = function() {
}

Utils.Mask.prototype.type = {
	'CPF':0
};

Utils.Mask.prototype.set = function(id, type) {
	var obj = document.getElementById(id);
	switch(type) {
		case this.type.NUMBER:
			obj.onkeyup = function() {
				this.value = this.value.replace(/\D/g,'');
			}
			break;
	}
}

function CPFValidate(value) {
	var cpf = value.replace(/[\.-]/g,"");
	if (cpf.length < 11) return false
    var nonNumbers = /\D/
    if (nonNumbers.test(cpf))return false
    if (cpf == "00000000000" || cpf == "11111111111" ||
        cpf == "22222222222" || cpf == "33333333333" ||
        cpf == "44444444444" || cpf == "55555555555" ||
        cpf == "66666666666" || cpf == "77777777777" ||
        cpf == "88888888888" || cpf == "99999999999")
            return false
    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]))return false
    return true
}

function CNPJValidate(value) {
	var cnpj = value.replace(/[\.\-/]/g,"");
	if (cnpj.length < 14) return false
	
	var nonNumbers = /\D/;
	if (nonNumbers.test(cnpj)) return false
	var a = [];
	var b = new Number;
	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
	for (i=0; i<12; i++){
		a[i] = cnpj.charAt(i);
		b += a[i] * c[i+1];
	}
	if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
	b = 0;
	for (y=0; y<13; y++) {
		b += (a[y] * c[y]);
	}
	if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
	if ((cnpj.charAt(12) != a[12]) || (cnpj.charAt(13) != a[13])) return false
	return true;
}

function phoneValidate(value) {
	var phone = value.replace(/[\(\)\.\-\s/]/g,"");
	var regexp = RegExp(/\d{10}/);
	return regexp.test(phone);
}