function validaCPFRegex(v) {
	return !Validation.get('IsEmpty').test(v) && /^\d{3}\.?\d{3}\.?\d{3}-?\d{2}$/.test(v);
}
function validaCNPJRegex(v) {
	return !Validation.get('IsEmpty').test(v) && /^\d{2}\.?\d{3}\.?\d{3}\/?\d{4}-?\d{2}$/.test(v);
}

function validaCPF(cpf,pType) {
	if(validaCPFRegex(cpf)) {
		cpf = cpf.replace(/\./g,"").replace(/-/g, "");
		return cpf.substring(9,11) == checkCPF(cpf.substring(0,9));
	}
	
	if(validaCNPJRegex(cpf)) {
		cpf = cpf.replace(/\./g,"").replace(/-/g, "").replace(/\//g, "");
		return cpf.substring(12,14) == checkCNPJ(cpf.substring(0,12));
	}

	return false;
}

function checkCNPJ(vCNPJ) {
	var mControle = "";
	var aTabCNPJ = new Array(5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2);
	for (i = 1 ; i <= 2 ; i++){
		mSoma = 0;
		for (j = 0 ; j < vCNPJ.length ; j++)
			mSoma = mSoma + (vCNPJ.substring(j,j+1) * aTabCNPJ[j]);
		if (i == 2 ) mSoma = mSoma + ( 2 * mDigito );
		mDigito = ( mSoma * 10 ) % 11;
		if (mDigito == 10 ) mDigito = 0;
		mControle1 = mControle;
		mControle = mDigito;
		aTabCNPJ = new Array(6,5,4,3,2,9,8,7,6,5,4,3);
	}
	return( (mControle1 * 10) + mControle );
}

function checkCPF(vCPF) {
	var mControle = ""
		var mContIni = 2, mContFim = 10, mDigito = 0;
	for (j = 1 ; j <= 2 ; j++){
		mSoma = 0;
		for (i = mContIni ; i <= mContFim ; i++)
			mSoma = mSoma + (vCPF.substring((i-j-1),(i-j)) * (mContFim + 1 + j - i));
		if (j == 2 ) mSoma = mSoma + ( 2 * mDigito );
		mDigito = ( mSoma * 10 ) % 11;
		if (mDigito == 10) mDigito = 0;
		mControle1 = mControle;
		mControle = mDigito;
		mContIni = 3;
		mContFim = 11;
	}
	return( (mControle1 * 10) + mControle );
}
