function CheckAll(form){
	var numChecks;
	var cont;
	var elementoForm;
	
	numChecks = form.elements.length;
	
	for (cont = 0; cont < numChecks; cont++)
	{
		elementoForm = form.elements[cont];
		
		if (elementoForm.name == "check[]" && elementoForm.disabled == false)
			elementoForm.checked = true;
	}
	
	form.checkTodos.checked = true;
}

function unCheckAll(form)
{
	var numChecks;
	var cont;
	var elementoForm;
	
	numChecks = form.elements.length;
	
	for (cont = 0; cont < numChecks; cont++)
	{
		elementoForm = form.elements[cont];
		
		if (elementoForm.name == "check[]")
			elementoForm.checked = false;
	}
	
	form.checkTodos.checked = false;
}

function UnCheckTodos(form)
{
	form.checkTodos.checked = false;
}

function Checar(form)
{
	if (form.checkTodos.checked)
		CheckAll(form);
	else
		unCheckAll(form);
}

function confirma(form)
{
	var strConfirma;
	
	switch (form.acao.value)
	{
		case "excluir":
			strConfirma = "Confirma exclusão?";
			break;
		case "disponibilizar":
			strConfirma = "Confirma disponibilização?";
			break;
		case "indisponibilizar":
			strConfirma = "Confirma indisponibilização?";
			break;
		case "enviarDados":
			strConfirma = "Confirma envio de dados?";
			break;
		case "backup":
			strConfirma = "Confirma backup?";
			break;
		case "atualizar":
			strConfirma = "Confirma atualização de saldos?";
			break;
		case "reverter":
			strConfirma = "Confirma reversão de saldos?";
			break;
	}
		
	if (confirm(strConfirma))
		return true;
	else
		return false;
}

function confirmaBackup(form) 
{
	if (confirm("Confirma criação de Backup?"))
		return true;
	else
		return false;
}

function ativo(src, event)
{
	if (!src.contains(event.fromElement))
    		src.bgColor = '#dddddd';
}

function inativo(src, cor, event)
{
	if (!src.contains(event.toElement))
		src.bgColor = cor;
}

function verificaMail(email)
{
	var cont;
	var contArro;
	var contPonto;

	if (email.length == 0)
		return true;
	else
	{
		contPonto = 0;
		contArro = 0;
		
		for (cont = 0; cont < email.length; cont++)
		{
			if (email.charAt(cont) == '@')
				contArro++;

			if (email.charAt(cont) == '.')
				contPonto++;
		}//for
		
		if (contArro > 1 || contArro == 0)
		{
			alert("E-mail inválido.");
			return false;
		}
		
		if (contPonto == 0)
		{
			alert("E-mail inválido.");
			return false;
		}
		
		return true;
	}	
}

function openWindow(theURL,winName,features)
{
  window.open(theURL,winName,features);
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

//verificação de datas
function verificaDiaMes(dia, mes)
{
	if (dia == 31)
	{
		if ((mes == 2) || (mes == 4) || (mes == 6) || (mes == 9) || (mes == 11))
		{
			alert('Data Inválida:\nMês que não possui o 31° dia.');
			return false;
		}
	}
	else
	{
		if ((dia > 29) && (mes == 2))
		{
			alert('Data Inválida:\nMês que não possui o 30° dia.');
			return false;
		}
		
		return true;
	}
	
	return true;
}

function verificaAnoeFev(dia, mes, ano)
{
	if ((ano % 4) != 0)
	{
		if ((dia == 29) && (mes == 2))
		{
			alert('Data Inválida:\nAno que não é bissexto.');
			return false;
		}
		
		return true;	
	}
	
	return true;
}

function dia(diaForm)
{
	if (diaForm == "")
	{
		alert('Informe inicialmente o dia para a Data.'); 
		return false;
	}
	
	return true;
}

function mes(mesForm)
{
	if (mesForm == "")
	{
		alert('Informe inicialmente o mês para a Data.');
		return false;
	}
	return true;
}

function verificaNum(texto)
{
	var cont;
	
	if (texto.length == 0)
		return true;
	else
	{
		for (cont = 0; cont < texto.length; cont++)
		{
			if (isNaN(texto.charAt(cont)))
				return false;
		}//for

		return true;
	}	
}

function verificaNumDecimal(texto)
{
	var cont;
	var contVirg = 0;

	if (texto.length == 0)
		return true;
	else
	{
		for (cont = 0; cont < texto.length; cont++)
		{
			if (texto.charAt(0) == ',')
				return false;
			else
			{
				if (isNaN(texto.charAt(cont)) && texto.charAt(cont) != ',')
					return false;
				else
				{
					if (texto.charAt(cont) == ',')
						contVirg++;

					
					if (contVirg > 1)
						return false;
				}
			}
		}

		return true;
	}	
}

function formatacao(formato)
{ 
	//Texto selecionado.
	var textoSelec = document.selection.createRange().text;

	//Novo texto.
	var novoTexto = "";
	var novoTextoI = "";
	var novoTextoF = "";

	//Verifica se tem algo selecionado.
	if (textoSelec.length == 0)
		alert ("Selecione algo.");
	else
	{
		//Aplica a formatação escolhida.
		if (formato == 'negrito')
		{
			novoTextoI = "<b>";
			novoTextoF = "</b>";
		}
		
		if (formato == 'italico')
		{
			novoTextoI = "<i>";
			novoTextoF = "</i>";
		}
		
		if (formato == 'sublinhado')
		{
			novoTextoI = "<u>";
			novoTextoF = "</u>";
		}
		
		if (formato == 'aEsquerda')
		{
			novoTextoI = "<div align=left>";
			novoTextoF = "</div>";
		}
		
		if (formato == 'centralizar')
		{
			novoTextoI = "<div align=center>";
			novoTextoF = "</div>";
		}
		
		if (formato == 'aDireita')
		{
			novoTextoI = "<div align=right>";
			novoTextoF = "</div>";
		}	
		
		//Texto final.
		novoTexto = novoTextoI + textoSelec + novoTextoF;
	
		//Substitui o texto selecionado com o novo formato.
		document.selection.createRange().text = novoTexto;
	}
}

function linke()
{
	//Pegando o texto selecionado.
	var textoSelec = document.selection.createRange().text;

	//Novo texto.
	var novoTexto = "";
	var novoTextoI = "";
	var novoTextoF = "";

	// verifica se tem algo selecionado
	if (textoSelec.length == 0)
		alert ("Selecione algo");
	else
	{
		// Usuário digita o link da página
		var endereco = prompt("Digite o endereço eletrônico. Exemplo: www.empresa.com.br","");
		
		//Inclui o link no texto
		if (endereco != null)
		{
			if (endereco.length == 0)
				endereco = "#"
			else
				endereco = "'http://" + endereco + "'"
		
			novoTextoI = "<a href=" + endereco + " target=blank>";
			novoTextoF = "</a>";
		}

		//texto final
		novoTexto = novoTextoI + textoSelec + novoTextoF;
	
		//substitui o texto antigo com o novo, formatado
		document.selection.createRange().text=novoTexto;
	}
}

function validaCNPJ(num)
{
	var sim;
	var val;
	var m1;
	var m2;
		
	if (num.length == 0)
		return true;
	else
	{
		if (num.length != 14)
		{
			sim = 0;
			alert("Para preencher o campo CNPJ são necessário 14 dígitos.");
			return false;
		}
		else
			sim = 1;
	
		// verifica se e numero
		if (sim)
		{
			for (i = 0; ((i <= (num.length - 1)) && sim); i++)
			{
				val = num.charAt(i)
				if (isNaN(val))
					sim = 0;
			}
			
			// se for numero continua
			if (sim)
			{
				m2 = 2;
				soma1 = 0;
				soma2 = 0;
				
				for (i = 11; i >= 0; i--)
				{
					val = eval(num.charAt(i));
					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;
				
				if ((d1 == num.charAt(12)) && (d2 == num.charAt(13)))
					return true;
				else
				{
					alert("CNPJ inválido.");
					return false;
				}
			}
			else
			{
				alert("CNPJ inválido.")
				return false;
			}
		}
	}
}
