
function maskCPF() {
	var objDisp = window.event.srcElement;
	var chrDigito = String.fromCharCode(window.event.keyCode);
	if(isNaN(chrDigito) || (chrDigito == ' ')) return false
	
	switch(objDisp.value.length) {
		case 3: objDisp.value += "."; break;
		case 7: objDisp.value += "."; break;
		case 11: objDisp.value += "-"; 
		}
	}


function VerificaCPF(CPF) {
	var intSoma = 0, intResultado1 = 0, intResultado2 = 0;
    var arrDigito = new Array(); 
    var strCPF = CPF;
	
	if (strCPF == "") {	alert("Preencha o CPF!"); return false; }
	
    while(strCPF.indexOf(".") != -1) strCPF = strCPF.replace(".","");

    strCPF = strCPF.replace("-","");
    if (strCPF.length != 11) { alert("CPF deve ter 11 dígitos!"); return false; }
    else if(strCPF == "00000000000") { alert("CPF inválido!"); return false; }
	else { arrDigito[0] = parseInt(strCPF.substr(0, 1));
		arrDigito[1] = parseInt(strCPF.substr(1, 1));
        arrDigito[2] = parseInt(strCPF.substr(2, 1));
        arrDigito[3] = parseInt(strCPF.substr(3, 1));
        arrDigito[4] = parseInt(strCPF.substr(4, 1));
        arrDigito[5] = parseInt(strCPF.substr(5, 1));
        arrDigito[6] = parseInt(strCPF.substr(6, 1));
        arrDigito[7] = parseInt(strCPF.substr(7, 1));
        arrDigito[8] = parseInt(strCPF.substr(8, 1));
        arrDigito[9] = parseInt(strCPF.substr(9, 1));
        arrDigito[10] = parseInt(strCPF.substr(10, 1));
    
        intSoma = 10 * arrDigito[0] + 9 * arrDigito[1] + 8 * arrDigito[2] + 7 * arrDigito[3] + 6 * arrDigito[4] + 5 * arrDigito[5] + 4 * arrDigito[6] + 3 * arrDigito[7] + 2 * arrDigito[8];
        intSoma -= (11 * parseInt(intSoma / 11));
        if ((intSoma == 0) || (intSoma == 1)) intResultado1 = 0;     
        else intResultado1 = 11 - intSoma;

        if (intResultado1 == arrDigito[9]) {
            intSoma = arrDigito[0] * 11 + arrDigito[1] * 10 + arrDigito[2] * 9 + arrDigito[3] * 8 + arrDigito[4] * 7 + arrDigito[5] * 6 + arrDigito[6] * 5 + arrDigito[7] * 4 + arrDigito[8] * 3 + arrDigito[9] * 2;
            intSoma -= (11 * parseInt(intSoma / 11));

            if ((intSoma == 0) || (intSoma == 1)) intResultado2 = 0;
            else intResultado2 = 11 - intSoma;

            if (intResultado2 != arrDigito[10]) { alert("CPF incorreto!"); return false; }
			}
        else { alert("CPF inválido !"); return false; }
		}
    return true;
	}

function mCNPJ() {
	var objDisp = window.event.srcElement;
	var chrDigito = String.fromCharCode(window.event.keyCode);
	if(isNaN(chrDigito) || (chrDigito == ' ')) return false
	
	switch(objDisp.value.length) {
		case 2: objDisp.value += "."; break;
		case 6: objDisp.value += "."; break;
		case 10: objDisp.value += "/"; break;
		case 15: objDisp.value += "-";
		}
	}

function ValidaNumero(e) {
	if (isNaN(String.fromCharCode(window.event.keyCode))) {
		event.returnValue = false; }
	}

function valDecimal() {
	Keycode = window.event.keyCode;
	var KeyChar = String.fromCharCode(Keycode)

	if (KeyChar != ',' && KeyChar != '.') {
		if (isNaN(KeyChar) || (KeyChar == ' ') || (KeyChar == ' ')) { event.returnValue = false; }
		}
	else { //verificar se já digitou vírgula ou ponto no campo
		if (KeyChar == ',') { if (window.event.srcElement.value.indexOf(",",0) > -1) { event.returnValue = false; } }
		//if (KeyChar == '.') { if (window.event.srcElement.value.indexOf(".",0) > -1) { event.returnValue = false; } }
		}
	}

function valDocumento() {
	Keycode = window.event.keyCode;
	var KeyChar = String.fromCharCode(Keycode)

	if (KeyChar != '-' && KeyChar != '.' && KeyChar != '/') {
		if (isNaN(KeyChar) || (KeyChar == ' ') || (KeyChar == ' ')) { event.returnValue = false; }
		}
	}
	
function checkNum(string) {
   if (!string) return false;
   var Chars = "0123456789";

   for (var j=0; j < string.length; j++) {
      if (Chars.indexOf(string.charAt(j)) == -1)
         return false;
   }
   return true;
}

function VerificaCEP(pCEP) {
	if(pCEP.value!="") {
		var vCEPNumero = trim(replaceAll(pCEP.value, "-", ""));
	
		if(!ValidaCEP(vCEPNumero, pCEP)) {
			alert('CEP Incorreto!'); pCEP.focus(); return (false);
			}
		}
	return (true);
	}
	
function ValidaCEP(vCEP, cpoCEP) {
	if(!checkNum(vCEP)) { return false; }
	if(vCEP.length != 8){ return false; }

	var vRetorno = VerificaTextoRepeticao(vCEP); 
	if(!vRetorno) { return false; }

	var vCEP = vCEP.substr(0, 5) + "-" +  vCEP.substr(5, 3);
	return vRetorno;
	}
	
function VerificaData(pData) {
	if(pData.value!="") {
		var vDataNumero = trim(replaceAll(pData.value, "/", ""));
	
		if(!ValidaData(vDataNumero, pData)) {
			alert('Data Incorreta!'); pData.focus(); return (false);
			}
		}
	return (true);
	}

function ValidaData(vData, cpoData) {
	if(!checkNum(vData)) { return false; }
	if(vData.length < 8){ return false; }

	var vData = vData.substr(0, 2) + "/" +  vData.substr(2, 2) + '/' + vData.substr(4, 4);
	var vDia = vData.substr(0, 2);
	var vMes = vData.substr(3, 2);
	var vAno = vData.substr(6, 4);

	var vRetorno = isValidDate(vDia, vMes, vAno);
	
	if(vRetorno) { cpoData.value = vData; }
	return vRetorno;
	}

function VerificaTelefone(pTelefone) {
	if (pTelefone.value != "") {
		var re;
		re = /^([(]\d{2}[)]\d{3,4}[-]\d{4})$/i;

		if (!re.test(pTelefone.value)){
			alert('Telefone Incorreto! Formato esperado "(99)9999-9999"'); pTelefone.focus(); return (false);
			}
		}
	
	return (true);
	}

function ValidaTelefone(vTelefone, cpoTelefone) {
	if(!checkNum(vTelefone)) { return false; }
	if(vTelefone.length < 9){ return false; }
	if(vTelefone.length > 10){ return false; }
	
	var vRetorno = VerificaTextoRepeticao(vTelefone); 
	if(!vRetorno) { return false; }
	
	//if (vTelefone.length == 9) {
	//	var vTelefone = "(" + vTelefone.substr(0, 2) + ")" +  vTelefone.substr(2, 3) + '-' + vTelefone.substr(5, 4);
	//	}
	//else {
	//	var vTelefone = "(" + vTelefone.substr(0, 2) + ")" +  vTelefone.substr(2, 4) + '-' + vTelefone.substr(6, 4);
	//	}
	//cpoTelefone.value = vTelefone;

	return vRetorno;
	}

// Verifica se a data é válida
function isValidDate(vDia, vMes, vAno)
{
	var vDias
	if(vAno%4 == 0)
		vDias = 29

	else
		vDias = 28

	Meses = new Array("31",vDias,"31","30","31","30","31","31","30","31","30","31")

	if (vDia == null || vMes == null ||vAno == null)
		return false

	else if (vMes < 1 || vMes > 12 || vAno == "")
		return false

	if (vDia > Meses[vMes-1] || vDia < 1)
		return false

	return true
}

function replaceAll(string,str1,srt2) {
	for(var awxy=0; awxy<string.length; awxy++) {
		string = string.replace(str1,srt2);
	}
	return string;
}

function lTrim(s) {
	if (s == null)
		return null;
		
	for (var i = 0; i < s.length; i++)
		if (s.charAt(i) != " ")
			break;

	return s.substr(i);
}

function rTrim(s) {
	if (s == null)
		return null;
		
	for (var i = s.length; i > 0; i--)
		if (s.charAt(i - 1) != " ")
			break;

	return s.substring(0, i);
}

function trim(s) {
	return rTrim(lTrim(s));
}

function checkMax(obj, maxLength) {
	if (maxLength != null) 	{
		if (obj.value.length > maxLength) {
			var vMsg = 'Campo limitado em ' + maxLength + ' caracteres!';
			alert(vMsg);
			obj.value = obj.value.substr(0,maxLength);
			obj.focus();
		}
	}
}

// Verifica se algum dos checkboxes foi selecionado
function VerificaAlgumCheckSelecionado(p_objChecks) {

	var blnStatusFinal = false;

	if (p_objChecks.length > 0) 
	
	for (var i = 0; i < p_objChecks.length; i++) {
		if (p_objChecks[i].checked)
			blnStatusFinal = true;
	}

	else {
		if (p_objChecks.checked)
			blnStatusFinal = true;}

	return(blnStatusFinal)
}

// VerificaMoeda: verifica se valor informado está no formato preco (0,00)	
function VerificaMoeda(p_obj){	
	var re;
	re = /^(\d+|\d+,\d{2}|\d+[.]\d{3},\d{2}|\d+[.]\d{3}[.]\d{3},\d{2})$/i;

	if (!re.test(p_obj.value)){
		alert('Valor incorreto!'); p_obj.focus(); return false;
		}
	return true;
	}

function VerificaValor(p_obj) {
	var re;
	re = /^((\d{1,3}[.]\d{3}[.]\d{3}[,]\d{2})|(\d{1,3}[.]\d{3}[,]\d{2})|(\d{1,3}[,]\d{2}))$/i;

	if (!re.test(p_obj.value)){
		alert('Valor incorreto!'); p_obj.focus(); return false;
		}
	return true;
	}

function VerificaCPF(p_obj){	
	var re;
	re = /^(\d{2,3}[.]\d{3}[.]\d{3}[-]\d{2})$/i;

	if (!re.test(p_obj.value)){
		alert('CPF incorreto!'); p_obj.focus(); return false;
		}
	else { 
		aux = replaceAll(replaceAll(p_obj.value, ".",""), "-", "");
		if (!VerificaTextoRepeticao(aux)) {
			alert('CPF incorreto!'); p_obj.focus(); return false;
			}
		}
	return true;
	}

function VerificaCNPJ(p_obj){	
	var re; aux = '';
	re = /^(\d{2,3}[.]\d{3}[.]\d{3}[/]\d{4}[-]\d{2})$/i;

	if (!re.test(p_obj.value)){
		alert('CNPJ incorreto!'); p_obj.focus(); return false;
		}
	else { 
		aux = replaceAll(replaceAll(replaceAll(p_obj.value, ".",""), "-", ""), "/", "");
		if (!VerificaTextoRepeticao(aux)) {
			alert('CNPJ incorreto!'); p_obj.focus(); return false;
			}
		}
	return true;
	}

// ComparaDatas: verifica se a data 1 é menor ou igual a data 2.
function ComparaDatas(p_strDia1, p_strMes1, p_strAno1, p_strHora1, p_strMinuto1, p_strSegundo1, p_strDia2, p_strMes2, p_strAno2, p_strHora2, p_strMinuto2, p_strSegundo2) 
{ 
	var data1Parse, data2Parse; 	
	
	data1Parse = new Date(p_strAno1, p_strMes1 - 1, p_strDia1, p_strHora1, p_strMinuto1, p_strSegundo1); 
	data2Parse = new Date(p_strAno2, p_strMes2 - 1, p_strDia2, p_strHora2, p_strMinuto2, p_strSegundo2); 

	if (data1Parse <= data2Parse) 
	{ 
		return true; 
	} 
	else 
	{ 
		return false; 
	} 
} 

function vDataMaior(DataInicial, DataFinal) {
	var arrAux = new Array(); var intDiff, int1, int2;

	arrAux = DataInicial.split("/"); int1 = (arrAux[2] + '' + arrAux[1] + '' + arrAux[0]); 
	arrAux = DataFinal.split("/"); int2 = (arrAux[2] + '' + arrAux[1] + '' + arrAux[0]);
	intDiff = int2 - int1;
	return intDiff;
	}
	
function ConsisteTextoLetra(source, arguments) {
	var Texto = arguments.Value; var Tamanho = Texto.length; var repeticao = 0; letra = '';

	if (Tamanho > 1) {
		letra = Texto.substr(0, 1);
		for (var contador = 0; contador < Tamanho; contador++) {
			if ((Texto.substr(contador, 1) == letra) || (Texto.substr(contador, 1) == ' ')) {
				repeticao += 1;
				if (Texto.substr(contador, 1) != ' ') { letra = Texto.substr(contador, 1); }
				}
			else { break; }
			}
		if (repeticao == Tamanho) {
			alert('Valor do campo incorreto! Os caracteres digitados devem ser diferentes!'); arguments.IsValid = false;
			}
		else { arguments.IsValid = true; }
		}
	else { arguments.IsValid = true; }

	return;			
	}

function VerificaTextoRepeticao(valor) {
	valor = trim(valor);
	var Tamanho = valor.length; var repeticao = 0; letra = '';
	
	if (Tamanho > 0) {
		letra = valor.substr(0, 1);
		for (var contador = 0; contador < Tamanho; contador++) {
			if ((valor.substr(contador, 1) == letra) || (valor.substr(contador, 1) == ' ')) {
				repeticao += 1;
				if (valor.substr(contador, 1) != ' ') { letra = valor.substr(contador, 1); }
				}
			else { break; }
			}
		if (repeticao == Tamanho) {
			return false;
			}
		else { return true; }
		}
	else { return true; }
	}
	

function maskTelefone() {
	var objDisp = window.event.srcElement;
	var chrDigito = String.fromCharCode(window.event.keyCode);
	if(isNaN(chrDigito) || (chrDigito == ' ')) return false
	
	switch(objDisp.value.length) {
		case 0: objDisp.value += "("; break;
		case 3: objDisp.value += ")"; break;
		case 8: objDisp.value += "-"; 
		}
	}

function limitaTexto(Tamanho) {
	if (event.srcElement.tagName.toUpperCase() == "TEXTAREA") {
		var strTexto = event.srcElement.value;
		if(strTexto.length > Tamanho) {
			strTexto = strTexto.substring(0,Tamanho);
			event.srcElement.value = strTexto; event.srcElement.focus(); event.srcElement.select(); 
			alert('Campo limitado em ' + Tamanho + ' caracteres!');
			return false;
			}
		}
	else { throw new Error("A função de limitação de texto só deve ser usada com um controle textarea!"); }
	}
