/* ==================================================
ROTINAS COMUNS EM JAVASCRIPT CLIENT SIDE 
ULTIMA ATUALIZAÇÃO EM 10/10/2002
================================================== */
/* ==================================================
Nome:		confFields(varValue1, varValue2)
Compat.:	Netscape 4/IE 4 +
Função:		verifica se os dois valores são iguais
Retorno:	true ou false
================================================== */
function confFields(varValue1, varValue2){
	if (varValue1!=varValue2)
		return true;
	else
		return false;
}
/* ==================================================
Nome:		CheckForSelected(objForm)
Compat.:	Netscape 4/IE 4 +
Função:		verifica se tem elementos checkbox selecionados
Retorno:	true ou false
================================================== */
function CheckForSelected(objForm){
	for (var i=0; i < objForm.elements.length; i++) {
		var e=objForm.elements[i];
		if (e.checked){return true;}
	}
	return false;
}
/* ==================================================
Nome:		deleteConfirm()
Compat.:	Netscape 4/IE 4 +
Função:		mostra uma msg de confirmação
Retorno:	true ou false
================================================== */
function deleteConfirm(){
	var blnRes = window.confirm('Deseja apagar os eventos selecionados ?');
	return blnRes;
}
/* ==================================================
Nome:		invalidStr(strValue,strInvalids)
Compat.:	Netscape 4/IE 4 +
Função:		produra por caracteres invalidos de strInvalids
			em strValue
Retorno:	true ou false
================================================== */
function invalidStr(strValue,strInvalids){
	for (var i=0;i<strInvalids.length;i++){
		if (strValue.indexOf (strInvalids.charAt(i),0) != -1){
			return true;
		}
	}
	return false;
}
/* ==================================================
Nome:		validStr(strValue,strValids)
Compat.:	Netscape 4/IE 4 +
Função:		Procura por caracteres validos de strValids
			em strValue
Retorno:	true ou false
================================================== */
function validStr(strValue,strValids){
	for (var i=0;i<strValue.length;i++){
		if (strValids.indexOf (strValue.charAt(i),0) == -1){
			return false;
		}
	}
	return true;
}
/* ==================================================
Nome:		minLen(strValue,intTamanho)
Compat.:	Netscape 4/IE 4 +
Função:		Verifica o tamanho minimo de uma string
Retorno:	true ou false
================================================== */
function minLen(strValue,intTamanho)
{
	if (strValue.length < intTamanho)
	{
		return true;
	}
	return false;
}
/* ==================================================
Nome:		isFieldEmpty(strValue)
Compat.:	Netscape 4/IE 4 +
Função:		Checa por campo vazio
Retorno:	true ou false
================================================== */
function isFieldEmpty(strValue)
{
	if (strValue==''){
		return true;
	}
	return false;
}
/* ==================================================
Nome:		isEmail(strEmail)
Compat.:	Netscape 4/IE 4 +
Função:		Checa se o formato de um email é valido
Retorno:	true ou false
================================================== */
function isEmail(strEmail){
	if (strEmail != ''){
		var intArroba = strEmail.indexOf('@',0)
		if (strEmail.indexOf(' ',0) > 0){ return false; }
		if (intArroba < 1){ return false; }
		var intDot = strEmail.indexOf('.',intArroba) 
		if (intDot > intArroba + 1 && strEmail.substring(intDot+1,strEmail.length) != ''){
			return true;
		}
		else{
			return false;
		}
	}
}
/* ==================================================
Nome:		CheckDate(dtaDate)
Compat.:	Netscape 4/IE 4 +
Função:		Checa a validade de uma data
Retorno:	true ou false
================================================== */
function CheckDate(dtaDate) 
{
	if (dtaDate.value == "" ) //verifica se a data foi digitada
	{
	return false;
	}
	var err=0;
	dtaValue=dtaDate.value;
	if (dtaValue.length != 8 && dtaValue.length != 10 ) err=1
	mm = dtaValue.substring(3, 5);
	dd = dtaValue.substring(0, 2);
	yy = dtaValue.substring(6, 10);
	if (mm<1 || mm>12) err = 1
	if (dd<1 || dd>31) err = 1
	if (yy.length == 4){
		if (yy<1900) err = 1
	}
	else {
		//se ano for inferior a 30 se entende 20??
		//se for maior que 29 se entende 19??
		yy=parseInt(yy,10)
		yy += yy<30?2000:1900
	}
	if (mm==4 || mm==6 || mm==9 || mm==11)
	{
		if (dd==31) err=1
	}
	if (mm==2)
	{
		var dtaYear=parseInt(yy/4);
		if (isNaN(dtaYear)) 
		{
			err=1;
		}
		if (dd>29) err=1
		if (dd==29 && ((yy/4)!=parseInt(yy/4))) err=1
	}
	if (err==1) 
	{
		return false;
	}
	return true;
}
/* =========================================================================================================================
Nome:		FormatPhone(i, delKey,direction)
Compat.:	IE 4 +
Função:		Formata um campo de telefone (DD) 1234-5678
Uso:		onkeydown="FormatPhone(this, window.event.keyCode,'down')" onkeyup="FormatPhone(this, window.event.keyCode,'up')"
========================================================================================================================== */
function FormatPhone(i, delKey,direction)
{
	if (i.value.length < 16 && navigator.appName == 'Microsoft Internet Explorer') 
	{
		if (delKey!=9) 
		{ // se for tab
			if(delKey!=13 && delKey!=8 && delKey!=46 && delKey!=16 &&  !(delKey>36 && delKey<41))
			{ //teclas delete, backspace, shift, nao disparam o evento
				var fieldLen = i.value.length
				if ((delKey >= 48 && delKey <= 57) || (delKey >= 96 && delKey <=105)) 
				{
					if (fieldLen == 0) 
					{
						i.value = i.value + "(";
					}
					if (fieldLen == 3) 
					{
						i.value = i.value + ") ";
					}
					if (fieldLen == 9) 
					{
						i.value = i.value + "-";
					}
				} 
				else 
				{
					if (direction == "up") 
					{
						if (i.value.length == 0) 
						{
							i.value = "";
						}
						else 
						{
							i.value = i.value.substring(0,i.value.length-1);
						}
					}
				}
				i.focus();
			}
		} 
	}
}
/* =========================================================================================================================
Nome:		FormatDate(i, delKey,direction)
Compat.:	IE 4 +
Função:		Formata um campo de data dd/mm/aaaa
Uso:		onkeydown="FormatDate(this, window.event.keyCode,'down')" onkeyup="FormatDate(this, window.event.keyCode,'up')"
========================================================================================================================== */
function FormatDate(i, delKey,direction)
{
	if (i.value.length < 10 && navigator.appName == 'Microsoft Internet Explorer') 
	{
		if (delKey!=9) 
		{ // se for tab
			if(delKey!=13 && delKey!=8 && delKey!=46 && delKey!=16 &&  !(delKey>36 && delKey<41))
			{ //teclas delete, backspace, shift, nao disparam o evento
				var fieldLen = i.value.length
				if ((delKey >= 48 && delKey <= 57) || (delKey >= 96 && delKey <=105)) 
				{
					if (fieldLen == 2 || fieldLen == 5) 
					{
						i.value = i.value + "/";
					}
				} 
				else 
				{
					if (direction == "up") 
					{
						if (i.value.length == 0) 
						{
							i.value = "";
						} 
						else 
						{
							i.value = i.value.substring(0,i.value.length-1);
						}
					}
				}
				i.focus();
			}
		} 
		else 
		{
			if (direction == "down") 
			{
				CheckDate(i);
			}
		}
	}
}
/* ==================================================
Nome:		checkCNPJ(pcnpj)
Função:		Checa a validade de um CNPJ
Compat.:	Netscape 4/IE 4 +
Retorna:	true ou false
================================================== */
function checkCNPJ(pcnpj){
	// verifica o tamanho
	if (pcnpj.length != 14) {
		sim=false;
		return false;
	}
	else {sim=true}
	// verifica se e numero
	if (sim ){
		for (i=0;((i<=(pcnpj.length-1))&& sim); i++){
			val = pcnpj.charAt(i);
			if((val!="9")&&(val!="0")&&(val!="1")&&(val!="2")&&(val!="3")&&(val!="4") &&
			(val!="5")&&(val!="6")&&(val!="7")&&(val!="8")) {sim=false}
		}
		// se for numero continua
		if (sim){
			m2 = 2;
			soma1 = 0;
			soma2 = 0;
			for (i=11;i>=0;i--){
				val = eval(pcnpj.charAt(i));
				// alert ("Valor do Val: "+val)
				m1 = m2;
				if (m2<9) { m2 = m2+1}
				else {m2 = 2}
				soma1 = soma1 + (val * m1);
				soma2 = soma2 + (val * m2);
			}  // fim do for de soma
			soma1 = soma1 % 11;
			if (soma1 < 2) {  d1 = 0}
			else { d1 = 11- soma1}
			soma2 = (soma2 + (2 * d1)) % 11;
			if (soma2 < 2) { d2 = 0}
			else { d2 = 11- soma2}
			// alert (d1)
			// alert (d2)
			if ((d1==pcnpj.charAt(12)) && (d2==pcnpj.charAt(13))){ 
				return true;
			}
			else return false
		}
	}
}
/* ==================================================
Nome:		checkCPF(pcpf)
Função:		Checa a validade de um CPF
Compat.:	Netscape 4/IE 4 +
Retorno:	true ou false
================================================== */
function checkCPF(pcpf){
	if (pcpf.length != 11) {sim=false}
	else {sim=true}
	// valida o primeiro digito
	if (sim ){
		for (i=0;((i<=(pcpf.length-1))&& sim); i++){
			val = pcpf.charAt(i)
			if((val!="9")&&(val!="0")&&(val!="1")&&(val!="2")&&(val!="3")&&(val!="4")
			&& (val!="5")&&(val!="6")&&(val!="7")&&(val!="8")) {sim=false}
		}
		if (sim){
			soma = 0
			for (i=0;i<=8;i++){
				val = eval(pcpf.charAt(i))
				soma = soma + (val*(i+1))
			}
			resto = soma % 11
			if (resto>9) dig = resto -10
			else  dig = resto
			if (dig != eval(pcpf.charAt(9))) { sim=false }
			// valida o segundo digito
			else{
				soma = 0
				for (i=0;i<=7;i++){
					val = eval(pcpf.charAt(i+1))
					soma = soma + (val*(i+1))
				}
				soma = soma + (dig * 9)
				resto = soma % 11
				if (resto>9) dig = resto -10
				else  dig = resto
				if (dig != eval(pcpf.charAt(10))) { sim = false }
				else sim = true
			}
		}
	}
	if (sim) { return true }
	else
	return false;
}
