/******************************************************************************************
'	Author: Sergio P. Gallafrio - SPG3 Consultoria	
'	Descrição: Permitir somente digitação de numeros
'*****************************************************************************************/
function iKeyPress() {
	
	var keyascii = window.event.keyCode;
	
	if (keyascii == 8)
		return;
	if (keyascii == 9)
		return;
	if (keyascii == 35)
		return;
	if (keyascii == 36)
		return;
	if (keyascii == 37)
		return;
	if (keyascii == 39)
		return;
	if (keyascii == 46)
		return;

	if (window.event.shiftKey){
		window.event.returnValue = false;
		return;
	}
	
	if ((keyascii < 48 || keyascii > 57) && (keyascii < 96 || keyascii > 105)) {
		window.event.returnValue = false;
	}
	
}

/******************************************************************************************
'	Author: Sergio P. Gallafrio - SPG3 Consultoria	
'	Descrição: Permitir somente digitação de valores
'*****************************************************************************************/
function vKeyPress(obj) {
	
	var keyascii = window.event.keyCode;
	
	if (keyascii == 8)
		return;
	if (keyascii == 9)
		return;
	if (keyascii == 35)
		return;
	if (keyascii == 36)
		return;
	if (keyascii == 37)
		return;
	if (keyascii == 39)
		return;
	if (keyascii == 46)
		return;
		
	if (obj.value.length == 4){
		if(obj.value.indexOf(",") < 0){
			if (keyascii != 188){
				window.event.returnValue = false;
				return;
			}
		}
	}
		
	if (keyascii == 188){
		if (obj.value.indexOf(",") >= 0)
			window.event.returnValue = false;
			
		return;
	}
	if (window.event.shiftKey){
		window.event.returnValue = false;
		return;
	}
	
	if ((keyascii < 48 || keyascii > 57) && (keyascii < 96 || keyascii > 105)) {
		window.event.returnValue = false;
	}
	
}

/******************************************************************************************
'	Author: Sergio P. Gallafrio - SPG3 Consultoria	
'	Descrição: Formata valor
'*****************************************************************************************/
function vFormat(obj){

	var valor = obj.value.replace(",", ".");
	valor = Math.round(valor*100)/100;
	valor = valor.toString().replace(".", ",");
	
	if (valor.indexOf(",") < 0)
		valor = valor + ",00";
	
	var dec = valor.substring(valor.indexOf(","),valor.length);
	var formatado = "";
	var y = 0;
	
	valor = valor.substring(0,valor.indexOf(","));
	
	for (x=0; x < valor.length; x++){
		formatado = valor.substring(valor.length -x -1,valor.length -x) + formatado;
		y = y+1;
		
		if (y == 3 && (x < valor.length -1)){
			formatado = "." + formatado;
			y = 0;
		}
	}
	
	if (dec.length < 3)
		dec = dec + "0";
		
	obj.value = formatado + dec;

}

/******************************************************************************************
'	Author: Sergio P. Gallafrio - SPG3 Consultoria	
'	Descrição: Formata valor
'*****************************************************************************************/
function formatarValor(sValor){

	var valor = sValor.replace(",", ".");
	valor = Math.round(valor*100)/100;
	valor = valor.toString().replace(".", ",");
	
	if (valor.indexOf(",") < 0)
		valor = valor + ",00";
	
	var dec = valor.substring(valor.indexOf(","),valor.length);
	var formatado = "";
	var y = 0;
	
	valor = valor.substring(0,valor.indexOf(","));
	
	for (x=0; x < valor.length; x++){
		formatado = valor.substring(valor.length -x -1,valor.length -x) + formatado;
		y = y+1;
		
		if (y == 3 && (x < valor.length -1)){
			formatado = "." + formatado;
			y = 0;
		}
	}
	
	if (dec.length < 3)
		dec = dec + "0";
		
	return formatado + dec;

}

/******************************************************************************************
'	Author: Sergio P. Gallafrio - SPG3 Consultoria	
'	Descrição: Retirar formatação de valor
'*****************************************************************************************/
function desformatarValor(svalor){

	var valor = svalor.replace(".", "");
	valor = valor.replace(",", ".");
	return valor;
		
}

/******************************************************************************************
'	Author: Sergio P. Gallafrio - SPG3 Consultoria	
'	Descrição: Retirar formatação de valor
'*****************************************************************************************/
function vUnformat(obj){

	var valor = obj.value.replace(".", "");
	obj.value = valor;
		
}

/******************************************************************************************
'	Author: Sergio P. Gallafrio - SPG3 Consultoria	
'	Descrição: Permitir somente digitação de datas
'*****************************************************************************************/
function dKeyPress(obj) {
	
	var keyascii = window.event.keyCode;
	
	if (keyascii == 8)
		return;
	if (keyascii == 9)
		return;
	if (keyascii == 35)
		return;
	if (keyascii == 36)
		return;
	if (keyascii == 37)
		return;
	if (keyascii == 39)
		return;
	if (keyascii == 46)
		return;
		
	if (window.event.shiftKey){
		window.event.returnValue = false;
		return;
	}
	
	if ((keyascii < 48 || keyascii > 57) && (keyascii < 96 || keyascii > 105)) {
		window.event.returnValue = false;
	}
	
	if ((obj.value.length == 2) || (obj.value.length == 5)){
		obj.value = obj.value + "/";
	}
	
}

/******************************************************************************************
'	Author: Sergio P. Gallafrio - SPG3 Consultoria	
'	Descrição: Permitir somente digitação de telefones
'*****************************************************************************************/
function tKeyPress(obj) {
	
	var keyascii = window.event.keyCode;
	
	if (keyascii == 8)
		return;
	if (keyascii == 9)
		return;
	if (keyascii == 35)
		return;
	if (keyascii == 36)
		return;
	if (keyascii == 37)
		return;
	if (keyascii == 39)
		return;
	if (keyascii == 46)
		return;
		
	if (window.event.shiftKey){
		window.event.returnValue = false;
		return;
	}
	
	if ((keyascii < 48 || keyascii > 57) && (keyascii < 96 || keyascii > 105)) {
		window.event.returnValue = false;
	}
	
	if (obj.value.length == 4){
		obj.value = obj.value + "-";
	}
	
}

/******************************************************************************************
'	Author: Sergio P. Gallafrio - SPG3 Consultoria	
'	Descrição: Cancela Validação client
'*****************************************************************************************/
function validarData(obj) {

	if(verificarData(obj.value) == false){
		alert("Data incorreta, favor corrigir!");
		obj.focus();
	}
}

/******************************************************************************************
'	Author: Sergio P. Gallafrio - SPG3 Consultoria	
'	Descrição: Cancela Validação client
'*****************************************************************************************/
function validarCPF(obj) {

	obj.value = formatCpfCnpj(obj.value, true, false);
	
	if(isCpf(obj.value) == false){
		alert("CPF incorreto, favor corrigir!");
		obj.focus();
	}
}

/******************************************************************************************
'	Author: Vinicius Gallafrio	
'	Descrição: Valida e Formata CNPJ
'*****************************************************************************************/
function validarCNPJ(obj) {
	if (obj.value.length == 0) return; 
	obj.value = formatCpfCnpj(obj.value, true, true);
	
	if(isCnpj(obj.value) == false){
		alert("CNPJ incorreto, favor corrigir!");
		obj.focus();
	}
}

/******************************************************************************************
'	Author: Vinicius Gallafrio	
'	Descrição: Valida e Formata CPF ou CNPJ
'*****************************************************************************************/
function validarCpfCnpj(obj) {
	if (this.value.lenght = 0) return;
	sUnformat = unformatNumber(obj.value);
	obj.value = formatCpfCnpj(obj.value, true, (sUnformat.length > 11 ? true : false) );
	
	if(isCpfCnpj(obj.value) == false){
		alert( (sUnformat.length > 11 ? "CNPJ" : "CPF") + " incorreto, favor corrigir!");
		obj.focus();
	}
}

/******************************************************************************************
'	Author: Sergio P. Gallafrio - SPG3 Consultoria	
'	Descrição: Cancela Validação client
'*****************************************************************************************/
function verificarData(objName) {
	var strDate;
	var strDateArray;
	var strDay;
	var strMonth;
	var strYear;
	var intday;
	var intMonth;
	var intYear;
	var booFound = false;
	var datefield = objName;
	var strSeparatorArray = new Array("-"," ","/",".");
	var intElementNr;
	var err = 0;
	var strMonthArray = new Array(12);
	strMonthArray[0] = "Jan";
	strMonthArray[1] = "Feb";
	strMonthArray[2] = "Mar";
	strMonthArray[3] = "Apr";
	strMonthArray[4] = "May";
	strMonthArray[5] = "Jun";
	strMonthArray[6] = "Jul";
	strMonthArray[7] = "Aug";
	strMonthArray[8] = "Sep";
	strMonthArray[9] = "Oct";
	strMonthArray[10] = "Nov";
	strMonthArray[11] = "Dec";
		
	strDate = objName;
		
	if (strDate.length < 1) {
		return true;
	}
	
	//Adjustment for short years entered
	if (strDate.length == 8){
		strDay = strDate.substring(0,2);
		strMonth = strDate.substring(3,5);
		strYear = '20' + strDate.substring(6,8);
	}else{
		strDay = strDate.substring(0,2);
		strMonth = strDate.substring(3,5);
		strYear = strDate.substring(6,10);
	}
	
	intday = parseInt(strDay, 10);
	
	if (isNaN(intday)){
		err = 2;
		return false;
	}
		
	intMonth = parseInt(strMonth, 10);
	if (isNaN(intMonth)){
		for (i = 0;i<12;i++){
			if (strMonth.toUpperCase() == strMonthArray[i].toUpperCase()){
				intMonth = i+1;
				strMonth = strMonthArray[i];
				i = 12;
			}
		}
		
		if (isNaN(intMonth)){
			err = 3;
			return false;
		}
	}
	
	intYear = parseInt(strYear, 10);
	if (isNaN(intYear)){
		err = 4;
		return false;
	}
	
	if (intMonth>12 || intMonth<1){
		err = 5;
		return false;
	}

	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)){
		err = 6;
		return false;
	}
	
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)){
		err = 7;
		return false;
	}
	
	if (intMonth == 2){
		if (intday < 1){
			err = 8;
			return false;
		}
		
		if (AnoBi(intYear) == true){
			if (intday > 29){
				err = 9;
				return false;
			}
		}else{
			if (intday > 28){
				err = 10;
				return false;
			}
		}
	}
	return true;
}

/******************************************************************************************
'	Author: Sergio P. Gallafrio - SPG3 Consultoria	
'	Descrição: Cancela Validação client
'*****************************************************************************************/
function AnoBi(intYear) {
	if (intYear % 100 == 0){
		if (intYear % 400 == 0){
			return true;
		}
	}else{
		if ((intYear % 4) == 0){
			return true;
		}
	}
	
	return false;
}

function FormataCEP(Campo, teclapres){
		var tecla = teclapres.keyCode;
		
		var vr = new String(Campo.value);
		vr = vr.replace(".", "");
		vr = vr.replace(".", "");
		vr = vr.replace("-", "");

		tam = vr.length + 1;
		
		if (tecla != 8 && tecla != 9){
			if (tam > 5 && tam < 7)
				Campo.value = vr.substr(0, 5) + '-' + vr.substr(6, tam);
			}
	}

