function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

// Funcao que valida o formulário de login
function validaFormLogin(nmForm)
{
	if(!validaCampoNull(eval(nmForm).txtEmail))
	{
		return false;
	}
	if(!validaCampoNull(eval(nmForm).txtSenha))
	{
		return false;
	}
	eval(nmForm).submit();
}

function validaCampoNull(campo)
{
	if(campo.value == "")
	{
	  alert("Campo Obrigatório, favor preencher !");
		campo.focus();
		return false;
	}
	return true;
}

// Funcao sem numeros
function somenteNumeros(e)
{
  var keynum = returnKeyCode(e);
  var keychar = returnKeyChar(e);
  var numcheck = /\d/;
	
	icValido = true;
	if(typeof(keynum) != "undefined" && !numcheck.test(keychar) && keynum != 8)
	{
	  icValido = false;
	}
  return icValido;
}

function somenteNumeros1Virgula(e, campo)
{
	// Incializo as variaveis
	icValido = false;
	valor = campo.value;
	
	// Inicio valido a quantidade de virgulas
	qtVirgulas = returnQtCaracteres(valor, ",");
	
	if(qtVirgulas > 1)
	{
		alert("Valor Incorreto, o valor deve estar no formato 9999,99");
		campo.value = "";
	}
	else
	{
		// Fim valido a quantidade de virgulas
		keychar = returnKeyChar(e);
	
		if(keychar == "," && qtVirgulas == 0)
		{
			icValido = true;
		}
		else
		{
				icValido = somenteNumeros(e);
		}
	}

	return icValido;
}

// Funcao e retorna o codigo da tecla pressionada
function returnKeyCode(e)
{
	if(window.event) // IE
	{
		keynum = e.keyCode
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which
	}
	return keynum;
}

// Funcao que retorna o caracter digitado
function returnKeyChar(e)
{
	keynum = returnKeyCode(e);
	keychar = String.fromCharCode(keynum);	
	return keychar;
}

// Funcao que valida o valor
function validaValor(campo)
{
	  // Verifico se o campo nao esta vazio
		if(campo.value.length == 0)
		{
			campo.value = "0,00";
		}
		else
		{
			qtVirgulas = returnQtCaracteres(campo.value, ",");
			valor = campo.value;
			
			// Verifico se possui mais de uma virgulas
			if(qtVirgulas > 1)
			{
				alert("Valor Incorreto, o valor deve estar no formato 9999,99");
				campo.focus();
				return false;
			}
			
			// Verifico se nao possui virgula
			if(valor.indexOf(",") == -1)
			{
				campo.value += ",00"	;
			} else {
				casasDecimais = valor.substring(valor.indexOf(",")+1);
				if(casasDecimais.length == 0)
				{
					campo.value += "00";				
				} else if(casasDecimais.length == 1)
				{
					campo.value += "0";
				} else if(casasDecimais.length != 2) {
					alert("Valor Incorreto, informe apenas 2 casas decimais.");
					campo.value = "0,00";
					campo.focus();
					return false;
				}
			}
			
			valor = campo.value;
			valor = valor.replace(",","");
			var numcheck = /\d/;
			while(valor.length > 0)
			{
				caracter = valor.substring(0,1);
				valor = valor.substring(1);
				if(!numcheck.test(caracter))
				{
					alert("Valor Incorreto, o valor deve estar no formato 9999,99");
					campo.value = "";
					campo.focus();
					return false;
				}
			}
		}
		return true;
}

// Funcao que retonra a quantidade de caracteres
function returnQtCaracteres(valor, caracter)
{
	qtCaracter = 0;
	while(valor.indexOf(caracter) != -1)
	{
		qtCaracter++;
		valor = valor.replace(caracter, "");
	}
	return qtCaracter;
}

// Funcao que formata o valor da forma 9999,99
function formataValor(valor)
{
	valor = "" + valor;
  valor = valor.replace(".", ",");
	
	if(valor.indexOf(",") == -1)
	{
		valor += ",00";
	} else {
		casasDecimais = valor.substring(valor.indexOf(",")+1);
		if(casasDecimais.length == 0)
		{
			valor += "00";				
		} else if(casasDecimais.length == 1)
		{
			valor += "0";
		}
	}

	return valor;
}

// Funcao Responsavel por verificar se os campos obrigatorios foram preenchidos
function validaCamposObrigatorios(formulario)
{
	elementos = document.getElementsByTagName('*');
	var frmAtual = "";
	for(i=0; i < elementos.length; i++)
	{
		if(typeof(elementos[i].name) != "undefined" && elementos[i].name == formulario)
		{
		  frmAtual = 	elementos[i].name;
		}
		
		// Verifico se o form e o que deve ser validado
		if(frmAtual == formulario)
		{
			// Verifico se o campo e obrigatorio
			if(typeof(elementos[i].getAttribute('obrigatorio')) != "undefined")
			{
				if(elementos[i].getAttribute('obrigatorio') == "true")
				{
					if(elementos[i].type == 'textarea')
					{
						if(typeof(tinyMCE) != "undefined")
						{
							var inst = tinyMCE.getInstanceById(elementos[i].name);
							valor = inst.getHTML();
						} else {
							valor = elementos[i].value;
						}
					} else {
						valor = elementos[i].value;
					}
					if(valor == "")
					{
						alert("Campo Obrigatório, favor verificar.");
						elementos[i].focus();
						return false;
					}
				}
			}
			// Verifico se o campo deve ser igual a algum outro
			if(typeof(elementos[i].getAttribute('igual')) != "undefined" && elementos[i].getAttribute('igual') != "")
			{
				eleDepende = elementos[i].getAttribute('igual');
				
				// Verifico se o elemento existe
				if(typeof(elementos[eleDepende]) != "undefined")
				{
					// Verifico se os valores sao Diferentes
					if(elementos[i].value != elementos[eleDepende].value)
					{
						alert("O valor da Confirmação deve ser igual, favor verificar.");
						elementos[i].focus();
						return false;
					}
				}
			}
		}
	}
	return true;
}

//Funcao que valida a data
function valida_data(campo) {
	if(campo.value != "")
	{
		var date = campo.value;
		var array_data = new Array;
		var ExpReg = new RegExp("(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[012])/[12][0-9]{3}");
		//vetor que contem o dia o mes e o ano
		array_data = date.split("/");
		erro = false;
		//Valido se a data esta no formato dd/mm/yyyy e se o dia tem 2 digitos e esta entre 01 e 31
		//se o mes tem d2 digitos e esta entre 01 e 12 e o ano se tem 4 digitos e esta entre 1000 e 2999
		if ( date.search(ExpReg) == -1 )
			erro = true;
		//Valido os meses que nao tem 31 dias com execao de fevereiro
		else if ( ( ( array_data[1] == 4 ) || ( array_data[1] == 6 ) || ( array_data[1] == 9 ) || ( array_data[1] == 11 ) ) && ( array_data[0] > 30 ) )
			erro = true;
		//Valido o mes de fevereiro
		else if ( array_data[1] == 2 ) {
			//Valido ano que nao e bissexto
			if ( ( array_data[0] > 28 ) && ( ( array_data[2] % 4 ) != 0 ) )
				erro = true;
			//Valido ano bissexto
			if ( ( array_data[0] > 29 ) && ( ( array_data[2] % 4 ) == 0 ) )
				erro = true;
		}
		if ( erro ) {
			alert("Data Inválida");
			campo.value = "";
			campo.focus();
		}
	}
}

//Funcao que valida a data
function valida_data_hora(campo) {
	if(campo.value != "")
	{
		if(campo.value.length == 16)
		{
  		hora        = campo.value.substring(11,16);
			campo.value = campo.value.substring(0,10);
			valida_data(campo);
			data = campo.value;
			campo.value = hora;
			valida_hora(campo);
			
			campo.value = data + " " + campo.value;
		} else {
		  alert("Data/Hora Inválida");
			campo.value = "";
			campo.focus();
		}
	}
}

// Formato a Data
function formataData(campo, e)
{
	if(somenteNumeros(e))
	{
		if(campo.value.length == 2)
		{
			if(campo.value.substring(2,3) != "/" && returnKeyChar(e) != "/")
			{
					campo.value += "/";
			}
		}
		if(campo.value.length == 5)
		{
			if(campo.value.substring(5,6) != "/" && returnKeyChar(e) != "/")
			{
					campo.value += "/";
			}
		}
	} else {
	  return false;	
	}
	return true;
}


// Formato a Data
function formataDataHora(campo, e)
{
	if(somenteNumeros(e))
	{
		if(campo.value.length == 2)
		{
			if(campo.value.substring(2,3) != "/" && returnKeyChar(e) != "/")
			{
					campo.value += "/";
			}
		} else if(campo.value.length == 5)
		{
			if(campo.value.substring(5,6) != "/" && returnKeyChar(e) != "/")
			{
					campo.value += "/";
			}
		} else if(campo.value.length == 10)
		{
			if(campo.value.substring(10,11) != " " && returnKeyChar(e) != " ")
			{
					campo.value += " ";
			}			
		} else if(campo.value.length == 13)
		{
			if(campo.value.substring(13,14) != ":" && returnKeyChar(e) != ":")
			{
					campo.value += ":";
			}			
		}
	} else {
	  return false;	
	}
	return true;
}

//Funcao que valida a hora
function valida_hora(campo) {
	if(campo.value != "")
	{
		if(campo.value.length == 5)
		{
			var hora = campo.value;
			
			horaParte1 = hora.substring(0,2);
			horaParte2 = hora.substring(3,5);
			if(horaParte1.length != 2 || parseFloat(horaParte1) > 24 || parseFloat(horaParte1) < 0)
			{
			  erro = true;
			}
			if(horaParte2.length != 2 || parseFloat(horaParte2) > 60 || parseFloat(horaParte2) < 0)
			{
			  erro = true;
			}
		} else {
		  erro = true;	
		}
		
		if ( erro ) {
			alert("Hora Inválida");
			campo.value = "";
			campo.focus();
		}
	}
}

// Detecta versao do Flash
var release = "7,0,19,0";
// Cria variaveis de impressao
function writeFlash(swf,flashVarString,w,h,bgcolor,menu,mode,q,id){
     document.write(' <OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '
     +' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version='+release+'" '
     +' width="'+w+'" height="'+h+'" id="'+id+'" align=""> '
     +' <param name="movie" value="'+swf+'"> '
     +' <param name="menu" value="'+menu+'"> '
     +' <param name="quality" value="'+q+'"> '
     +' <param name="wmode" value="'+mode+'"> '
     +' <param name="bgcolor" value="'+bgcolor+'"> '
     +' <param name="flashvars" value="'+flashVarString+'"> '
     +' <embed src="'+swf+'" flashvars="'+flashVarString+'" menu="'+menu+'" quality="'+q+'" wmode="'+mode+'" '
     +' bgcolor="'+bgcolor+'"  width="'+w+'" height="'+h+'" name="'+swf+'" '
     +' align="" type="application/x-shockwave-flash" '
     +' pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object> ');
}

// Funcao que limpa a string
function ApenasNum(strParm)
{
  strParm = String(strParm);
  var chrPrt = "0";
  var strRet = "";
  var j=0;
  for(var i=0; i < strParm.length; i++)
  {
    chrPrt = strParm.substring(i, i+1);
    if(chrPrt.match(/\d/))
    {
      if(j==0)
      {
        strRet = chrPrt;
        j=1;
      }
      else
      {
        strRet = strRet.concat(chrPrt);
      }
    }
  }
  return strRet;
}

// Funcao que calcula os digitos
function digitoCPFCNPJ(prNumero)
{
  var numLim;
  var numDois = prNumero.substring( prNumero.length - 2, prNumero.length);
  var novoCPF = prNumero.substring( 0, prNumero.length - 2);
  if(prNumero.length == 11)
  {
    numLim = 11;
  }
  else if(prNumero.length == 14)
  {
    numLim = 9;
  }
  else
  {
    return false;
  }
  var Fator = 1;
  var numSoma = 0;
  for (var i = novoCPF.length-1; i >= 0; i--)
  {
    Fator = Fator + 1;
    if (Fator > numLim)
    {
      Fator = 2;
    }
    numSoma = numSoma + ( Fator * Number( novoCPF.substring( i, i + 1)));
  }
  numSoma = numSoma/11;
  var numResto = Math.round( 11 * ( numSoma - Math.floor( numSoma)));
  if (numResto > 1)
  {
    numResto = 11 - numResto;
  }
  else
  {
    numResto = 0;
  }
 
  var numDigito = String(numResto);
  novoCPF = novoCPF.concat(numResto);

  numSoma = 0;
  Fator = 1;
  for (var i = novoCPF.length - 1; i >= 0; i--)
  {
    Fator = Fator + 1;
    if (Fator > numLim)
    {
      Fator = 2;
    }
    numSoma = numSoma + (Fator * Number( novoCPF.substring( i, i + 1)));
  }
  numSoma = numSoma / 11;
  numResto = numResto = Math.round( 11 * (numSoma - Math.floor( numSoma)));
  if (numResto > 1)
  {
    numResto = 11 - numResto;
  }
  else
  {
    numResto = 0;
  }
  numDigito = numDigito.concat(numResto);
  if (numDigito == numDois)
  {
    return true;
  }
  else
  {
    return false;
  }
}

// Funcao que valida e formata CPF ou CNPJ
function validaCPFCNPJ(objCPFCNPJ)
{
	if(objCPFCNPJ.value != "")
	{
		if(objCPFCNPJ.value == null)
		{
			alert("ATENÇÃO!!!\nPor favor digite o CPF ou CNPJ");
			return false;
		}
		numCPFCNPJ = ApenasNum(objCPFCNPJ.value);
		if(parseFloat(numCPFCNPJ) == 0)
		{
			alert("ATENÇÃO!!!\nO CPF informado é inválido!");
			objCPFCNPJ.value = "";
			return false;		
		}
		if(!digitoCPFCNPJ(numCPFCNPJ))
		{
			alert("ATENÇÃO!!!\nO dígito verificador do CPF ou CNPJ é inválido!");
			objCPFCNPJ.value = "";
			return false;
		}
	}
}

// Funcoes para exibir bloco de ajuda
function mostraAjuda(e, texto, controle)
{
  // Verifico se o elemento nao existe
	var div = document.getElementById('lblAjuda');
	if(div == null)
	{
      div = document.createElement('div');
		  div.id = "lblAjuda";
      div.style.color = "#000000";
      div.style.zIndex = "1";
//      div.style.backgroundColor = "#FFFFEC";
      div.style.backgroundColor = "#FFFFFF";
      div.style.fontFamily = "Arial, Helvetica, sans-serif";
      div.style.position = "absolute";
      div.style.visibility = "hidden";
//      div.style.border = "1px solid #CC0000";
      div.style.border = "1px solid #000000";
      div.style.fontSize = "12px";
      div.style.padding = "4px";
		  div.style.filter = "progid:DXImageTransform.Microsoft.Shadow(color=gray, direction=135, strength=4)";
      document.body.appendChild(div);
	
			controle.onmouseout = function ()
			{
				div.style.visibility = 'hidden';
			}
	}

	div.innerHTML = texto;
	div.style.visibility = 'visible';
	
	var ev = (!e) ? window.event : e;

	var SomaLeft = 20;
	var SomaTop = -80;	

	if(typeof(ev.pageX) != "undefined") // Mozilla
	{
		div.style.left = (ev.pageX + SomaLeft) + 'px';
		div.style.top  = (ev.pageY + SomaTop) + 'px';
	} else { // IE
	  alturaY = ev.clientY + SomaTop;
		alturaX = ev.clientX + SomaLeft;
		if (document.documentElement && document.documentElement.scrollTop) // IE6 +4.01 and user has scrolled
		{
			alturaY += document.documentElement.scrollTop;
			alturaX += document.documentElement.scrollLeft;
		}
		else if (document.body && document.body.scrollTop) // IE5 or DTD 3.2
		{
			alturaY += document.body.scrollTop;
			alturaX += document.body.scrollLeft;
		}
		
		div.style.left = (alturaX) + 'px';
		div.style.top = (alturaY) + 'px';
	}
}

// Funcoes para exibir bloco de ajuda
function mostraAjudaTexto(e, texto, controle)
{
  // Verifico se o elemento nao existe
	var div = document.getElementById('lblAjuda');
	if(div == null)
	{
      div = document.createElement('div');
		  div.id = "lblAjuda";
      div.style.color = "#000000";
      div.style.zIndex = "1";
//      div.style.backgroundColor = "#FFFFEC";
      div.style.backgroundColor = "#FFFFFF";
      div.style.fontFamily = "Arial, Helvetica, sans-serif";
      div.style.position = "absolute";
      div.style.visibility = "hidden";
//      div.style.border = "1px solid #CC0000";
      div.style.border = "1px solid #000000";
      div.style.fontSize = "12px";
      div.style.padding = "4px";
		  div.style.filter = "progid:DXImageTransform.Microsoft.Shadow(color=gray, direction=135, strength=4)";
      document.body.appendChild(div);
	
			controle.onmouseout = function ()
			{
				div.style.visibility = 'hidden';
			}
	}

	div.innerHTML = texto;
	div.style.visibility = 'visible';
	
	var ev = (!e) ? window.event : e;

	var SomaLeft = 10;
	var SomaTop = 10;	

	if(typeof(ev.pageX) != "undefined") // Mozilla
	{
		div.style.left = (ev.pageX + SomaLeft) + 'px';
		div.style.top  = (ev.pageY + SomaTop) + 'px';
	} else { // IE
	  alturaY = ev.clientY + SomaTop;
		alturaX = ev.clientX + SomaLeft;
		if (document.documentElement && document.documentElement.scrollTop) // IE6 +4.01 and user has scrolled
		{
			alturaY += document.documentElement.scrollTop;
			alturaX += document.documentElement.scrollLeft;
		}
		else if (document.body && document.body.scrollTop) // IE5 or DTD 3.2
		{
			alturaY += document.body.scrollTop;
			alturaX += document.body.scrollLeft;
		}
		
		div.style.left = (alturaX) + 'px';
		div.style.top = (alturaY) + 'px';
	}
}

// Adiciono o Js Custom
document.write('<script language="javascript" type="text/javascript" src="js/funcoesCustom.js"></script>');