/*****************************************************************************
/* JavaScript Library (Default)
/* Author	: Edmundo N. Oliveira Jr
/* Sintaxe	: <script src="inc/jsGetDefault.js"></script>
/* To		: Input forms
/****************************************************************************/

/*****************************************************************************
/* Utilização: onKeyDown="return mascaraFone(this, event)"
 * 17/09/2008
 */
/****************************************************************************/
function mascaraFone(objeto, event) {
	var tecla = event.keyCode?event.keyCode:event.which;
	var fone = objeto.value;
	if 	(fone.length >= 9) {
		if	(tecla != 9 && tecla != 46 && tecla != 8 && tecla != 16 & tecla != 36) {
			return false;
		} else {
			return true;
		}
	}
	if ( (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105 ) || /* Numeros */
		(tecla == 9 || tecla == 8 || tecla == 46 || tecla == 35 || tecla == 36 || tecla == 37 || tecla == 39 || tecla == 13) ) { /* Controle */
		if 	(fone.length == 4 && tecla != 8) {
			fone += '-';
			objeto.value = fone;
		}
		return true;
	} else {
		return false;
	} 
}

/*****************************************************************************
/* Utilização: onKeyDown="return mascaraInt(this, event)"
/****************************************************************************/
function mascaraInt(objeto, event) {
	var valor = objeto.value;
	var tecla = event.keyCode?event.keyCode:event.which;
	if ( tecla < 48 || (tecla > 57 && tecla < 96) || tecla > 105 ) {
		if	(tecla==9 || tecla==8 ||tecla==46 || tecla==35 || tecla==36 || tecla==37 || tecla==39 || tecla==13) {
			return true;
		}
		objeto.value = valor;
		return false;
	} 
} 

/*****************************************************************************
/* Utilização: onKeyDown="mascaraCep(this, event)"
/****************************************************************************/
function mascaraCep(objeto, tecla) {
	var validChar =  "#48;#49;#50;#51;#52;#53;#54;#55;#56;#57;#96;#97;#98;#99;#100;#101;#102;#103;#104;#105;"; // Válidos
	validChar += "#8;#9;#13;#16;#27;#36;#46;"; // Controle
	validChar += "#45;"; // Especiais para o campo
	if (validChar.indexOf("#" + String(event.keyCode + ";")) == -1) {
	    event.returnValue = false;
	}
	vlrCampo = objeto.value;
	if	(tecla.keyCode != 8) {
		if 	(vlrCampo.length == 5) {
			vlrCampo += '-';
			objeto.value = vlrCampo;
		}
	}
	return true;
}

/*****************************************************************************
/* Utilização: onKeyDown="return mascaraInt2(this, event, max)"
/****************************************************************************/
function mascaraInt2(objeto, event, max) {
	var valor = objeto.value;
	var tecla = event.keyCode?event.keyCode:event.which;
	if	(valor.length >= max) {
		if	(tecla==9 || tecla==8 ||tecla==46 || tecla==35 || tecla==36 || tecla==37 || tecla==39 || tecla==13) {
			return true;
		} else {
			return false;
		}
	}
	if ( tecla < 48 || (tecla > 57 && tecla < 96) || tecla > 105 ) {
		if	(tecla==9 || tecla==8 ||tecla==46 || tecla==35 || tecla==36 || tecla==37 || tecla==39 || tecla==13) {
			return true;
		}
		objeto.value = valor;
		return false;
	} 
} 
/*****************************************************************************
/* Utilização: onKeyDown="return mascaraCep(this, event)" - 17/09/2008
/****************************************************************************/
function mascaraCep(objeto, event) {
	var tecla = event.keyCode?event.keyCode:event.which;
	var cep = objeto.value;
	if ( (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105 ) || /* Numeros */
		(tecla == 9 || tecla == 8 || tecla == 46 || tecla == 35 || tecla == 36 || tecla == 37 || tecla == 39 || tecla == 13) ) { /* Controle */
		if 	(cep.length == 5) {
			cep += '-';
			objeto.value = cep;
		}
		return true;
	} else {
		return false;
	}
}
/*****************************************************************************
/* Utilização: onKeyDown="return mascaraCPF(this, event)"
/****************************************************************************/
function mascaraCPF(objeto, event) {
	var tecla = event.keyCode?event.keyCode:event.which;
	var cpf = objeto.value;

	if 	(cpf.length >= 14) {
		if	(tecla != 9 && tecla != 46 && tecla != 8 && tecla != 16 & tecla != 36) {
			return false;
		} else {
			return true;
		}
	}
	
	if ( (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105 ) || /* Numeros */
		(tecla == 9 || tecla == 8 || tecla == 46 || tecla == 35 || tecla == 36 || tecla == 37 || tecla == 39 || tecla == 13) ) { /* Controle */
		if 	((cpf.length == 3 || cpf.length == 7)  && tecla != 8) {
			cpf += '.';
			objeto.value = cpf;
		}
		if 	((cpf.length == 11) && tecla != 8) {
			cpf += '-';
			objeto.value = cpf;
		}
		return true;
	} else {
		return false;
	} 
}
	

/*****************************************************************************
/* Utilização: onKeyDown="mascaraCNPJ(this, event)"
/****************************************************************************/
function mascaraCNPJ(objeto, event) {
	var tecla = event.keyCode?event.keyCode:event.which;
	var cnpj = objeto.value;
	if 	(cnpj.length >= 18) {
		if	(tecla != 9 && tecla != 46 && tecla != 8 && tecla != 16 & tecla != 36) {
			return false;
		} else {
			return true;
		}
	}
	if ( (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105 ) || /* Numeros */
		(tecla == 9 || tecla == 8 || tecla == 46 || tecla == 35 || tecla == 36 || tecla == 37 || tecla == 39 || tecla == 13) ) { /* Controle */
		if 	((cnpj.length == 2 || cnpj.length == 6) && tecla != 6) {
			cnpj += '.';
			objeto.value = cnpj;
		}
		if 	(cnpj.length == 10 && tecla != 6) {
			cnpj += '/';
			objeto.value = cnpj;
		}
		if 	(cnpj.length == 15 && tecla != 6) {
			cnpj += '-';
			objeto.value = cnpj;
		}
		return true;
	} else {
		return false;
	} 
}

/*****************************************************************************
/* Utilização: onKeyDown="return mascaraFloat(this, event)" onBlur="formataFloat(this)"
/****************************************************************************/
function mascaraFloat(objeto, event) {
	var valor = objeto.value;
	var tecla = event.keyCode?event.keyCode:event.which;
	if	(tecla == 9 || tecla == 13) { /* TAB e ENTER */ 
		return true;
	}
	if ( (tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105 ) || /* Numeros */
		(tecla == 8 || tecla == 46 || tecla == 35 || tecla == 36 || tecla == 37 || tecla == 39 || tecla == 13) ) { /* Controle */
		if 	(valor.length == 2 && tecla != 8) {
			valor = valor.replace(",","");
			valor = valor.replace(".","");
			valor = valor.substring(0,1)+','+valor.substring(1,2);
		}
		if 	(valor.length >= 4 && tecla != 8) {
			valor = valor.replace(",","");
			valor = valor.substring(0,valor.length-1)+','+valor.substring(valor.length-1);
		}
		if 	(valor.length == 6 && tecla != 8) {
			valor = valor.substring(0,1)+'.'+valor.substring(1);
		}
		if 	(valor.length >= 7 && valor.length < 10 && tecla != 8) {
			valor = valor.replace(".","");
			valor = valor.substring(0,valor.length-5)+'.'+valor.substring(valor.length-5);
		}
		if 	(valor.length == 10 && tecla != 8) {
			valor = valor.replace(".","");
			valor = valor.substring(0,valor.length-5)+'.'+valor.substring(valor.length-5);			
			valor = valor.substring(0,valor.length-9)+'.'+valor.substring(valor.length-9);			
		}
		if 	(valor.length > 10 && tecla != 8) {
			valor = valor.replace(".","");
			valor = valor.replace(".","");
			valor = valor.substring(0,valor.length-5)+'.'+valor.substring(valor.length-5);			
			valor = valor.substring(0,valor.length-9)+'.'+valor.substring(valor.length-9);			
		}
		objeto.value = valor;
		return true;
	} 
	objeto.value = valor;
	return false;
}

/*****************************************************************************
/* Utilização: onKeyDown="mascaraFloat(this, event)" onBlur="formataFloat(this)"
/****************************************************************************/
function formataFloat(objValor) {
	valor = objValor.value;
	if	(valor.length == 2) {
		valor = "0,"+valor;
		objValor.value = valor;
		return true;
	}
}

function formataFloat2(valor) {
	var ret = 0;
	ret = valor.toLocaleString();
	return ret;
	/*
	valor = valor.replace(".","");
	if 	(valor.length == 2) {
		valor = valor.replace(",","");
		valor = valor.replace(".","");
		valor = valor.substring(0,1)+','+valor.substring(1,2);
	}
	if 	(valor.length >= 4) {
		valor = valor.replace(",","");
		valor = valor.substring(0,valor.length-1)+','+valor.substring(valor.length-1);
	}
	if 	(valor.length == 6) {
		valor = valor.substring(0,1)+'.'+valor.substring(1);
	}
	if 	(valor.length >= 7 && valor.length < 10) {
		valor = valor.replace(".","");
		valor = valor.substring(0,valor.length-5)+'.'+valor.substring(valor.length-5);
	}
	if 	(valor.length == 10) {
		valor = valor.replace(".","");
		valor = valor.substring(0,valor.length-5)+'.'+valor.substring(valor.length-5);			
		valor = valor.substring(0,valor.length-9)+'.'+valor.substring(valor.length-9);			
	}
	if 	(valor.length > 10) {
		valor = valor.replace(".","");
		valor = valor.replace(".","");
		valor = valor.substring(0,valor.length-5)+'.'+valor.substring(valor.length-5);			
		valor = valor.substring(0,valor.length-9)+'.'+valor.substring(valor.length-9);			
	}
	ret = valor;
	return ret;
	*/
}

/*****************************************************************************
/* Utilização: onKeyDown="mascaraData(this, event)"
/****************************************************************************/
function mascaraData(objData, tecla) {
	if 	(tecla.keyCode == 8)
		return true;
	if	(tecla.keyCode == 113) {
		objData.value = formatarData(new Date());
	}
	data = objData.value;
	if 	(data.length == 2 || data.length == 5) {
		data+='/';
		objData.value = data;
	}
	return true;
}



/*****************************************************************************
/* Utilização: returnNumber(parameter)
/****************************************************************************/
function returnNumber(parameter) {
	var parameter = toFloat(parameter);
	var tmp = parseInt(parameter);
	if	( isNaN(tmp) ) {
		tmp = 0;
	} else {
		tmp = parseFloat(parameter);
	}
	return tmp;
}
/*****************************************************************************
/* Utilização: toFloat(parameter)
/****************************************************************************/
function toFloat(parameter) {
	var ret;
	var tmp = parameter.indexOf(",");
	var tmp2;
	if	(tmp != -1) {  // if have virgula
		tmp2 = parameter.replace('.','');
		tmp2 = tmp2.replace(',','.');
		ret = tmp2;
	} else {
		tmp = parameter.indexOf(".");
		if	(tmp != -1) {  // if have virgula
			ret = parameter;
		} else {
			if	(parameter == 0) {
				ret = 0;
			} else {
				ret = parseFloat(parameter+'.00');
			}
		}
	}
	return ret;
}
/*****************************************************************************
/* Utilização: numberToValueForm(parameter)
/****************************************************************************/
function numberToValueForm(parameter) {
	var valor = parameter.toString();
	var fponto;
	var sponto;
	var ret;
	valor = valor.replace(".",",");
	fponto = valor.indexOf(',')-4;
	sponto = valor.indexOf(',')-7;
	ret = valor.substr(0,1);
	for (i = 1; i < valor.length; i++) {
		ret = ret + valor.substr(i,1);
		if	(i == fponto || i == sponto) {
			ret = ret + '.';
		}
	}
	return ret;
}
/*****************************************************************************
/* 	Utilização: onKeyDown="mascaraCNPJ(this, event)" 
	Last Modify: 08/11/2008
/****************************************************************************/
function mascaraCNPJCPF(objeto, tecla, type) {
	var validChar =  "#48;#49;#50;#51;#52;#53;#54;#55;#56;#57;#96;#97;#98;#99;#100;#101;#102;#103;#104;#105;"; // Válidos
	validChar += "#8;#9;#13;#16;#27;#36;#46;"; // Controle
	/* validChar += "#45;"; // Especiais para o campo */
	if (validChar.indexOf("#" + String(event.keyCode + ";")) == -1) {
	    event.returnValue = false;
	}
	vlrCampo = objeto.value;
	if	(type == 'CNPJ') {
		if	(tecla.keyCode != 8) {
			if 	(vlrCampo.length == 2 || vlrCampo.length == 6) {
				vlrCampo += '.';
				objeto.value = vlrCampo;
			}
			if 	(vlrCampo.length == 10) {
				vlrCampo += '/';
				objeto.value = vlrCampo;
			}
			if 	(vlrCampo.length == 15) {
				vlrCampo += '-';
				objeto.value = vlrCampo;
			}
		}
	} else {
		if	(tecla.keyCode != 8) {
			if 	(vlrCampo.length == 3 || vlrCampo.length == 7) {
				vlrCampo += '.';
				objeto.value = vlrCampo;
			}
			if 	(vlrCampo.length == 11) {
				vlrCampo += '-';
				objeto.value = vlrCampo;
			}
		}
	}
	return true;
}

/*****************************************************************************
/* Utilização: formatarData(parameter)
/****************************************************************************/
function formatarData(data) {
	var saida = "";
	var tmp;
	tmp = "" + data.getDate();
	if( tmp.length == 1 ) {
		tmp = "0" + tmp;
	}
	saida = tmp + "/";
	tmp = "" + (data.getMonth() + 1);
	if( tmp.length == 1 ) {
		tmp = "0" + tmp;
	}
	saida = saida + tmp +"/";
	tmp = "" + data.getYear();
	for( var i = 0 ; i < (4 - tmp.length) ; i++ ) {
		saida = saida + "0";
	}
	saida = saida + tmp;
	return(saida);
}

/*****************************************************************************
/* Utilização: onKeyDown="mascaraHora(this, event)"
/****************************************************************************/
function mascaraHora(objeto, tecla) {
	var validChar =  "#48;#49;#50;#51;#52;#53;#54;#55;#56;#57;#96;#97;#98;#99;#100;#101;#102;#103;#104;#105;"; // Válidos
	validChar += "#8;#9;#13;#16;#27;#36;#46;"; // Controle
	validChar += "#45;"; // Especiais para o campo
	if (validChar.indexOf("#" + String(event.keyCode + ";")) == -1) {
	    event.returnValue = false;
	}
	vlrCampo = objeto.value;
	if	(tecla.keyCode != 8) {
		if 	(vlrCampo.length == 2) {
			vlrCampo += ':';
			objeto.value = vlrCampo;
		}
	}
	return true;
}

