/*
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
::: Validar Formulario com campos obrigatorios
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
// Modo de Usar: onSubmit="return ValForm(this);"

function ValForm( theForm )
{

	var val_id      = 'REQ';
	var msg_intro   = 'Os campos abaixo são requeridos:\n';
	var msg_campos  = ''; // nome dos campos requeridos
	var foco_campo  = ''; // foco no primeiro campo requerido
	var msg_req     = '- ';
	var msg_req2    = '\n';
	var objetos     = theForm.length;
	var cont_campos = 0;  // contador de campos requeridos
	var tt_campos   = 10; // total de campos exibidos como requerido

	for (var i=0; i<objetos; i++)
	{

		// tirar espacos do campo
		while(''+theForm.elements[i].value.charAt(theForm.elements[i].value.length-1)==' ')theForm.elements[i].value=theForm.elements[i].value.substring(0,theForm.elements[i].value.length-1);

		// tirar apostrofes do campo
		//while(''+theForm.elements[i].value.charAt(theForm.elements[i].value.length-1)=="'")theForm.elements[i].value=theForm.elements[i].value.substring(0,theForm.elements[i].value.length-1);

		// obrigar campo texto
		if ((theForm.elements[i].type == 'text') && (theForm.elements[i].id.indexOf(val_id) != -1) && (theForm.elements[i].disabled == false) && (theForm.elements[i].value.length == 0))
		{
				if (cont_campos < tt_campos) {msg_campos = msg_campos + msg_req + verNomeCampo(theForm.elements[i]) + msg_req2};
				cont_campos = cont_campos + 1;
				if (foco_campo.length == 0) {foco_campo = theForm.elements[i]}
		}

		// obrigar campo password
		if ((theForm.elements[i].type == 'password') && (theForm.elements[i].id.indexOf(val_id) != -1) && (theForm.elements[i].disabled == false) && (theForm.elements[i].value.length == 0))
		{
				if (cont_campos < tt_campos) {msg_campos = msg_campos + msg_req + verNomeCampo(theForm.elements[i]) + msg_req2};
				cont_campos = cont_campos + 1;
				if (foco_campo.length == 0) {foco_campo = theForm.elements[i]}
		}

		// obrigar campo textarea
		else if ((theForm.elements[i].type == 'textarea') && (theForm.elements[i].id.indexOf(val_id) != -1) && (theForm.elements[i].disabled == false) && (theForm.elements[i].value.length == 0))
		{
				if (cont_campos < tt_campos) {msg_campos = msg_campos + msg_req + verNomeCampo(theForm.elements[i]) + msg_req2};
				cont_campos = cont_campos + 1;
				if (foco_campo.length == 0) {foco_campo = theForm.elements[i]}
		}

		// obrigar campo hidden
		if ((theForm.elements[i].type == 'hidden') && (theForm.elements[i].id.indexOf(val_id) != -1) && (theForm.elements[i].disabled == false) && (theForm.elements[i].value.length == 0))
		{
				if (cont_campos < tt_campos) {msg_campos = msg_campos + msg_req + verNomeCampo(theForm.elements[i]) + msg_req2};
				cont_campos = cont_campos + 1;
		}

		// obrigar campo combobox (listbox)
		else if ((theForm.elements[i].type == 'select-one') && (theForm.elements[i].id.indexOf(val_id) != -1) && (theForm.elements[i].disabled == false) && (theForm.elements[i].selectedIndex == 0))
		{
				if (cont_campos < tt_campos) {msg_campos = msg_campos + msg_req + verNomeCampo(theForm.elements[i]) + msg_req2};
				cont_campos = cont_campos + 1;
				if (foco_campo.length == 0) {foco_campo = theForm.elements[i]}
		}

		// obrigar campo combobox multiple (listbox multiple)
		else if ((theForm.elements[i].type == 'select-multiple') && (theForm.elements[i].id.indexOf(val_id) != -1) && (theForm.elements[i].disabled == false) && (theForm.elements[i].value.length == 0))
		{
				if (cont_campos < tt_campos) {msg_campos = msg_campos + msg_req + verNomeCampo(theForm.elements[i]) + msg_req2};
				cont_campos = cont_campos + 1;
				if (foco_campo.length == 0) {foco_campo = theForm.elements[i]}
		}

		// obrigar campo checkbox
		else if ((theForm.elements[i].type == 'checkbox') && (theForm.elements[i].id.indexOf(val_id) != -1) && (theForm.elements[i].disabled == false) && (theForm.elements[i].checked == false))
		{
			nome_campo = theForm.elements[i].name;
			qtd_opcoes = document.getElementsByName(nome_campo);
			tmpChecked = false;

			if (qtd_opcoes.length > 1)
			{
				for (j=0;j<qtd_opcoes.length;j++)
				{
					if (eval("theForm."+nome_campo+"[j].checked == true"))
					{
						tmpChecked = true;
						break;
					}
				}
			}

			if (tmpChecked == false)
			{
				if (cont_campos < tt_campos) {msg_campos = msg_campos + msg_req + verNomeCampo(theForm.elements[i]) + msg_req2};
				cont_campos = cont_campos + 1;
				if (foco_campo.length == 0) {foco_campo = theForm.elements[i]}
			}
		}

		// obrigar uma opcao valida no campo radio
		else if ((theForm.elements[i].type == 'radio') && (theForm.elements[i].id.indexOf(val_id) != -1) && (theForm.elements[i].disabled == false) && (theForm.elements[i].checked == false))
		{
			nome_campo = theForm.elements[i].name;
			qtd_opcoes = document.getElementsByName(nome_campo);
			tmpChecked = false;

			if (qtd_opcoes.length > 1)
			{
				for (j=0;j<qtd_opcoes.length;j++)
				{
					if (eval("theForm."+nome_campo+"[j].checked == true"))
					{
						tmpChecked = true;
						break;
					}
				}
			}
			
			if (tmpChecked == false)
			{
				if (cont_campos < tt_campos) {msg_campos = msg_campos + msg_req + verNomeCampo(theForm.elements[i]) + msg_req2};
				cont_campos = cont_campos + 1;
				if (foco_campo.length == 0) {foco_campo = theForm.elements[i]}
			}
		}

		// obrigar campo file
		if ((theForm.elements[i].type == 'file') && (theForm.elements[i].id.indexOf(val_id) != -1) && (theForm.elements[i].disabled == false) && (theForm.elements[i].value.length == 0))
		{
				if (cont_campos < tt_campos) {msg_campos = msg_campos + msg_req + verNomeCampo(theForm.elements[i]) + msg_req2};
				cont_campos = cont_campos + 1;
				if (foco_campo.length == 0) {foco_campo = theForm.elements[i]}
		}

    else if ((theForm.elements[i].type == 'submit') && (msg_campos.length == 0))
        {theForm.elements[i].disabled=true;}

	}


	if (msg_campos.length != 0)
	{
		if (cont_campos > tt_campos) // ha mais campos que a variavel tt_campos
		{
			msg_campos = msg_campos + '- entre outros.';
		}

		destReq(theForm, true);
		window.alert(msg_intro + msg_campos);
		destReq(theForm, false);
		if (foco_campo.length != 0)
		{
			eval("foco_campo.focus()")
		}
		return false;
	}
	else
	{
		return true;
	}

}

// funcao para a ValForm buscar o nome do campo pelo id="REQ|Nome do Campo"
function verNomeCampo(campo)
{
	if (campo.id.indexOf('|') != -1)
	{
			var arr_id = campo.id.split('|');
			return arr_id[1];
	}
	else
	{
			return campo.name;
	}
}


/*
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
::: Destacar campos obrigatorios
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
// Modo de Usar: destReq(document.nome_form, true)
// true = destacar, false = normal

function destReq(theForm, dest)
{
	var objetos = theForm.length;

	for (var i=0; i<objetos; i++)
	{
		tipo = theForm.elements[i].type;

		if ((theForm.elements[i].id.indexOf('REQ') != -1) && (tipo != 'radio') && (tipo != 'checkbox'))
		{
			if (dest)
			{
				theForm.elements[i].style.background='#F0F0F0';
			}
			else
			{
				theForm.elements[i].style.background='#FFFFFF';
			}
		}
	}
}

// funcao para a Abrir foto"

function AbrirFoto(foto)
{
	var w2 = (screen.width / 2);
	var h2 = (screen.height / 2);

	winPopup('../admin/show_image.asp?f='+foto, 'FotoGrande', w2, h2, 1, 'status=yes');
}

/*
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
::: Formatar Text com Mascaras
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
// Modo de Usar: onKeyPress="return txtBoxFormat(document.nomeform, 'NOMECAMPO', '99/99/9999', event);"

  function txtBoxFormat(objForm, strField, sMask, evtKeyPress) {
    var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

    if(document.all) { // Internet Explorer
      nTecla = evtKeyPress.keyCode;
	  sValue = objForm[strField].value;
	}
    else if(document.layers) { // Nestcape
      nTecla = evtKeyPress.which;
	  sValue = objForm.strField.value;
    }
	else { // Firefox
		sValue = eval("document.all."+strField+".value");
	}

    // Limpa todos os caracteres de formatação que
    // já estiverem no campo.
    sValue = sValue.toString().replace( "-", "" );

    sValue = sValue.toString().replace( "-", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( ".", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( "/", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( ":", "" );
    sValue = sValue.toString().replace( "|", "" );
    sValue = sValue.toString().replace( "|", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( "(", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( ")", "" );
    sValue = sValue.toString().replace( " ", "" );
    sValue = sValue.toString().replace( " ", "" );
    fldLen = sValue.length;
    mskLen = sMask.length;

    i = 0;
    nCount = 0;
    sCod = "";
    mskLen = fldLen;

    while (i <= mskLen) {
      bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
      bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))
      bolMask = bolMask || ((sMask.charAt(i) == ":") || (sMask.charAt(i) == "|"))

      if (bolMask) {
        sCod += sMask.charAt(i);
        mskLen++; }
      else {
        sCod += sValue.charAt(nCount);
        nCount++;
      }

      i++;
    }

	if(document.all)
	{
    	objForm[strField].value = sCod;
	}
	else
	{
		eval("document.all."+strField+".value") = sCod;
	}

    if (nTecla != 8) { // backspace
      if (sMask.charAt(i-1) == "9") { // apenas números...
        return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
      else { // qualquer caracter...
        return true;
      } }
    else {
      return true;
    }
  }


/*
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
::: Comparar Horas
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
// Modo de Usar: "javascript:comparaHora(campo_hora1, campo_hora2)"

  function comparaHora(campo_hora1, campo_hora2) { // funcao para comparar 2 campos hora

    if ((campo_hora1.value.length != 0) && (campo_hora2.value.length != 0))
    {
      var hora1		= campo_hora1.value.substring(0, 2); // pegar hora1
      var minuto1	= campo_hora1.value.substring(3, 5); // pegar minuto1
      var hora2		= campo_hora2.value.substring(0, 2); // pegar hora2
      var minuto2	= campo_hora2.value.substring(3, 5); // pegar minuto2

      hora1 = hora1 + minuto1; //une hora com numero para formar um numero inteiro
      hora2 = hora2 + minuto2;

      if (hora2 <= hora1) //hora termino menos que hora inicio
      {
        window.alert('A Hora de Término não pode ser igual ou menor que a Hora de Início.');
		campo_hora2.value='';
		campo_hora2.focus();
      }

    }

  }

/*
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
::: Preload Images
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
// Modo de Usar: preloadImages('http://www.site.com/img1.gif','http://www.site.com/img2.gif')

	function preloadImages() { //v3.0
  		var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    		var i,j=d.MM_p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    		if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
	}

/*
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
:::Filtrando as extensões de arquivo no upload: (gif, jpg, png)
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
// Modo de Usar: onBlur="ChecaExtensaoArquivo(this, ',.doc,.rtf,')"

function ChecaExtensaoArquivo(field, extensoesOk){
	//var extensoesOk = ",.doc,.rtf,";
	var extensao    = "," + field.value.substr( field.value.length - 4 ).toLowerCase() + ",";

	if (field.value.length != 0) {
		if( extensoesOk.indexOf( extensao ) == -1 )
		{
			alert( field.value + "\nO arquivo não possui uma extensão válida! Selecione novamente." )
			field.value = '';field.focus();
			return false;
		}
	}
	return true;
}

/*
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
::: Validar E-mail
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
// Modo de Usar: onBlur="ValEmail(this)"

	 function ValEmail(field) { // validar campo email
	 if (field.value.length != 0)
	 {
		if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(field.value)){
		return (true)
		}
		field.value = '';
		alert("O E-mail é inválido! Digite novamente.")
		field.focus();
		field.select();
		return (false)
	 }
	}

/*
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
::: Validar Campo Numérico com : (dois pontos)
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
// Modo de Usar: onKeyPress="return ValNumero_DP(event, this);"

function ValNumero_DP(e, campo) {

	if (document.all) // Internet Explorer
	{
		var tecla = event.keyCode;
	}
	else if (document.layers) // Nestcape
	{
		var tecla = e.which;
	}

	if (campo.value.length == 2)
	{
		campo.value = campo.value + ':'; // adiciona : (dois pontos)
	}

	if (tecla > 47 && tecla < 58) // numeros de 0 a 9 e : (dois pontos)
	{
		return true;
	}
	else
	{
		if (tecla != 8) // backspace
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}


/*
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
::: Validar Campo Numérico
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
// Modo de Usar: onKeyPress="return ValNumero(event, this);"

function ValNumero(e, campo) {

	if (document.all) // Internet Explorer
	{
		var tecla = event.keyCode;
	}
	else if (document.layers) // Nestcape
	{
		var tecla = e.which;
	}

	if (tecla > 47 && tecla < 58) // numeros de 0 a 9
	{
		return true;
	}
	else
	{
		if (tecla != 8) // backspace
		{
			return false;
		}
		else
		{
			return true;
		}
	}
 }


/*
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
::: Validar CNPJ
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
// Modo de Usar: onBlur="javascript:valCNPJCPF(this, 2);"

// CNPJ TEM ENTRE 7 A 15 CARACTERES
// CPF TEM 11 CARACTERES

function valCNPJCPF(valor, tipo)
{
	var tipomsg = 'CNPJ';

	if (tipo == 2)
	{
		tipomsg = 'CPF';
	}

	if ( (valor.value.length > 0) && (isCPFCNPJ(valor.value, tipo) == false) )
	{
		valor.value = '';
		window.alert(tipomsg+' inválido! Digite novamente.');
		valor.focus();
	}

	function isCPFCNPJ(campo,pType){
	   if( campo.length == 0 ){return false;}

	   var campo_filtrado = "", valor_1 = " ", valor_2 = " ", ch = "";
	   var valido = false;

	   for (i = 0; i < campo.length; i++){
	      ch = campo.substring(i, i + 1);
	      if (ch >= "0" && ch <= "9"){
	         campo_filtrado = campo_filtrado.toString() + ch.toString()
	         valor_1 = valor_2;
	         valor_2 = ch;
	      }
	      if ((valor_1 != " ") && (!valido)) valido = !(valor_1 == valor_2);
	   }
	   if (!valido) campo_filtrado = "12345678912";

	   if (campo_filtrado.length < 11){
	      for (i = 1; i <= (11 - campo_filtrado.length); i++){campo_filtrado = "0" + campo_filtrado;}
	   }

	   if(pType == 2){
	      if ( ( campo_filtrado.substring(9,11) == checkCPF( campo_filtrado.substring(0,9) ) ) && ( campo_filtrado.substring(11,12)=="") ){return true;}
	   }

	   if((pType <= 1) || (pType == 0)){
	      if (campo_filtrado.length >= 14){
	         if ( campo_filtrado.substring(12,14) == checkCNPJ( campo_filtrado.substring(0,12) ) ){ return true;}
	      }
	   }

	   return false;
	}

	function checkCNPJ(vCNPJ){
	   var mControle = "";
	   var aTabCNPJ = new Array(5,4,3,2,9,8,7,6,5,4,3,2);
	   for (i = 1 ; i <= 2 ; i++){
	      mSoma = 0;
	      for (j = 0 ; j < vCNPJ.length ; j++)
	         mSoma = mSoma + (vCNPJ.substring(j,j+1) * aTabCNPJ[j]);
	      if (i == 2 ) mSoma = mSoma + ( 2 * mDigito );
	      mDigito = ( mSoma * 10 ) % 11;
	      if (mDigito == 10 ) mDigito = 0;
	      mControle1 = mControle ;
	      mControle = mDigito;
	      aTabCNPJ = new Array(6,5,4,3,2,9,8,7,6,5,4,3);
	   }
	   return( (mControle1 * 10) + mControle );
	}

	function checkCPF(vCPF){
	   var mControle = ""
	   var mContIni = 2, mContFim = 10, mDigito = 0;
	   for (j = 1 ; j <= 2 ; j++){
	      mSoma = 0;
	      for (i = mContIni ; i <= mContFim ; i++)
	         mSoma = mSoma + (vCPF.substring((i-j-1),(i-j)) * (mContFim + 1 + j - i));
	      if (j == 2 ) mSoma = mSoma + ( 2 * mDigito );
	      mDigito = ( mSoma * 10 ) % 11;
	      if (mDigito == 10) mDigito = 0;
	      mControle1 = mControle;
	      mControle = mDigito;
	      mContIni = 3;
	      mContFim = 11;
	   }
	   return( (mControle1 * 10) + mControle );
	}
}


/*
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
::: Campo Hora
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
// Modo de Usar: OnBlur="txtHora(this)"

  function txtHora(campo_hora) { // funcao para validar hora/minuto

    if ((campo_hora.value.length != 5) && (campo_hora.value.length != 0))
    {
      window.alert('Campo Hora Incorreto. Digite Novamente.\nUtilize o formato hh:mm.');
      campo_hora.value = '';campo_hora.focus();
    }
    else
    {
      var hora		= campo_hora.value.substring(0, 2); // pegar hora
      var minuto	= campo_hora.value.substring(3, 5); // pegar minuto
      var erro = 0;

      if ( (campo_hora.value.substring(2, 3) != ':') && (campo_hora.value.length != 0) ) // dois pontos (invalido)
      {
        erro = 1;
      }

      if ((parseInt(hora) < 0) || (parseInt(hora) > 23)) // hora invalida
      {
        erro = 1;
      }

      if ((parseInt(minuto) < 0) || (parseInt(minuto) > 59)) // minuto invalido
      {
        erro = 1;
      }

      if (erro == 1)
      {
        window.alert('Campo Hora Incorreto. Digite Novamente.\nUtilize o formato hh:mm.');
	campo_hora.value = '';campo_hora.focus();
      }

    }

  }

/*
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
::: Comparar Horas
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
// Modo de Usar: "javascript:comparaHora(campo_hora1, campo_hora2)"

  function comparaHora(campo_hora1, campo_hora2) { // funcao para comparar 2 campos hora

    if ((campo_hora1.value.length != 0) && (campo_hora2.value.length != 0))
    {
      var hora1		= campo_hora1.value.substring(0, 2); // pegar hora1
      var minuto1	= campo_hora1.value.substring(3, 5); // pegar minuto1
      var hora2		= campo_hora2.value.substring(0, 2); // pegar hora2
      var minuto2	= campo_hora2.value.substring(3, 5); // pegar minuto2

      hora1 = hora1 + minuto1; //une hora com numero para formar um numero inteiro
      hora2 = hora2 + minuto2;

      if (hora2 <= hora1) //hora termino menos que hora inicio
      {
        window.alert('A Hora de Término não pode ser igual ou menor que a Hora de Início.');
		campo_hora2.value='';
		campo_hora2.focus();
      }

    }

  }

/*
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
::: Janela Pop Up Centralizada
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
// Modo de Usar: javascript:winPopup('pagina', 'nome', largura, altura, centralizar, 'propriedades da janela')
//pagina = caminho da pagina ou site para abrir na popup
//nome = nome da janela
//largura = largura da janela em px | altura = altura da janela em px
//centralizar = 1 ou 2 (1 = centralizada | 2 = sem centralizar)
//propriedades da janela = scrollbars=yes, status=no, etc etc

    function winPopup(pag, nome, larg, alt, centralizar, opcoes) { // abrir janela popup

  	  if (centralizar == 1)
	  {
	    var winl = (screen.width - larg) / 2;
	    var wint = (screen.height - alt) / 2;
			if ((alt >= 500) && (alt <= 540)) { wint = wint - 20;}
	  }
	  else
	  {
	    var winl = 0;
	    var wint = 0;
	  }

	  if (opcoes != '')
	  {
	    opcoes = ', ' + opcoes;
      }
	  newWindow = window.open('' + pag + '',''+nome+'','width=' + larg + ',height=' + alt + ',top='+wint+',left='+winl+'' + opcoes + '');
	  newWindow.focus();
    }


/*
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
::: Voltar Quantas Telas for Necessário
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
// Modo de Usar: OnBlur="GoBack(Número) onde Número é a quantidade de telas que deseja voltar o padrão é 1"

function GoBack(Qtde)
{
	if (Qtde==null)
		Qtde=1;
	if (Qtde>0)
		Qtde *= (-1);
	history.go(Qtde);
}


/*
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
::: Validar Data
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
// Modo de Usar: OnBlur="ValData(this)"

    function ValData(field) { // campo data (dd/mm/aaaa)

	var checkstr = "0123456789";
	var DateField = field;
	var Datevalue = "";
	var DateTemp = "";
	var separador = "/";
	var day; var month; var year;
	var leap = 0;
	var err = 0;
	var i;

	err = 0;
	DateValue = DateField.value;

	/* Deletar todos os caracteres que nao forem numeros (0-9) */
	for (i = 0; i < DateValue.length; i++) {
		if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
			DateTemp = DateTemp + DateValue.substr(i,1);
		}
	}

	DateValue = DateTemp;

	/* Always change date to 8 digits - string*/
	/* if year is entered as 2-digit / always assume 20xx */
	if (DateValue.length == 6) {
		DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
	if (DateValue.length != 8) {
		err = 19;}
	/* year is wrong if year = 0000 */
	year = DateValue.substr(4,4);
	if (year == 0) {
		err = 20;
	}

	/* Validation of month*/
	month = DateValue.substr(2,2);
	if ((month < 1) || (month > 12)) {
		err = 21;
	}

	/* Validation of day*/
	day = DateValue.substr(0,2);

	if (day < 1) {
		err = 22;
	}

	/* Validation leap-year / february / day */
	if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
		leap = 1;
	}
	if ((month == 2) && (leap == 1) && (day > 29)) {
		err = 23;
	}
	if ((month == 2) && (leap != 1) && (day > 28)) {
		err = 24;
	}

	/* Validation of other months */
	if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
		err = 25;
	}
	if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
		err = 26;
	}

	/* if 00 ist entered, no error, deleting the entry */
	if ((day == 0) && (month == 0) && (year == 00)) {
		err = 0; day = ""; month = ""; year = ""; separador = "";
	}
	/* if no error, write the completed date to Input-Field (e.g. 13/12/2001) */
	if (err == 0) {
		DateField.value = day + separador + month + separador + year;
	}
	/* Error-message if err != 0 */
	else if (field.value.length > 0) {
		field.value = '';
		alert("Campo Data Incorreto! Digite Novamente.\nUtilize o formato dd/mm/aaaa.");
		DateField.select();
		DateField.focus();
	}
}


/*
::: Limitar Quantidade de Caracteres em um Campo
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
// Modo de Usar: <input type="text" onKeyDown="limCampo(this, 8000);" onKeyUp="limCampo(this, 8000);">
function limCampo(campo, maxlimit) {
	if (campo.value.length > maxlimit)
	{
		campo.value = campo.value.substring(0, maxlimit);
	}
}


/*
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
::: Title
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
// Modo de Usar: <a href="?" class="helpLink" onclick="showHelpTip(event, 'Texto do title', true); return false">link</a>

	function Title(e, sHtml, bHideSelects) {

		// find anchor element
		var el = e.target || e.srcElement;
		while (el.tagName != "A")
			el = el.parentNode;

		// is there already a tooltip? If so, remove it
		if (el._helpTip) {
			helpTipHandler.hideHelpTip(el);
		}

		helpTipHandler.hideSelects = Boolean(bHideSelects);

		// create element and insert last into the body
		helpTipHandler.createHelpTip(el, sHtml);

		// position tooltip
		helpTipHandler.positionToolTip(e);

		// add a listener to the blur event.
		// When blurred remove tooltip and restore anchor
		el.onblur = helpTipHandler.anchorBlur;
		el.onkeydown = helpTipHandler.anchorKeyDown;
	}

	var helpTipHandler = {
		hideSelects:	false,

		helpTip:		null,

		showSelects:	function (bVisible) {
			if (!this.hideSelects) return;
			// only IE actually do something in here
			var selects = [];
			if (document.all)
				selects = document.all.tags("SELECT");
			var l = selects.length;
			for	(var i = 0; i < l; i++)
				selects[i].runtimeStyle.visibility = bVisible ? "" : "hidden";
		},

		create:	function () {
			var d = document.createElement("DIV");
			d.className = "help-tooltip";
			d.onmousedown = this.helpTipMouseDown;
			d.onmouseup = this.helpTipMouseUp;
			document.body.appendChild(d);
			this.helpTip = d;
		},

		createHelpTip:	function (el, sHtml) {
			if (this.helpTip == null) {
				this.create();
			}

			var d = this.helpTip;
			d.innerHTML = sHtml;
			d._boundAnchor = el;
			el._helpTip = d;
			return d;
		},

		// Allow clicks on A elements inside tooltip
		helpTipMouseDown:	function (e) {
			var d = this;
			var el = d._boundAnchor;

			if (!e) e = event;
			var t = e.target || e.srcElement;
			while (t.tagName != "A" && t != d)
				t = t.parentNode;
			if (t == d) return;

			el._onblur = el.onblur;
			el.onblur = null;
		},

		helpTipMouseUp:	function () {
			var d = this;
			var el = d._boundAnchor;
			el.onblur = el._onblur;
			el._onblur = null;
			el.focus();
		},

		anchorBlur:	function (e) {
			var el = this;
			helpTipHandler.hideHelpTip(el);
		},

		anchorKeyDown:	function (e) {
			if (!e) e = window.event
			if (e.keyCode == 27) {	// ESC
				helpTipHandler.hideHelpTip(this);
			}
		},

		removeHelpTip:	function (d) {
			d._boundAnchor = null;
			d.style.filter = "none";
			d.innerHTML = "";
			d.onmousedown = null;
			d.onmouseup = null;
			d.parentNode.removeChild(d);
			//d.style.display = "none";
		},

		hideHelpTip:	function (el) {
			var d = el._helpTip;
			d.style.visibility = "hidden";
			d.style.top = - el.offsetHeight - 100 + "px"
			d._boundAnchor = null;

			el.onblur = null;
			el._onblur = null;
			el._helpTip = null;
			el.onkeydown = null;

			this.showSelects(true);
		},

		positionToolTip:	function (e) {
			this.showSelects(false);
			var scroll = this.getScroll();
			var d = this.helpTip;

			var dw = (window.innerWidth || document.documentElement.offsetWidth) - 25;

			if (d.offsetWidth >= dw)
				d.style.width = dw - 10 + "px";
			else
				d.style.width = "";

			if (e.clientX > dw - d.offsetWidth)
				d.style.left = dw - d.offsetWidth + scroll.x + "px";
			else
				d.style.left = e.clientX - 2 + scroll.x + "px";
			d.style.top = e.clientY + 18 + scroll.y + "px";

			d.style.visibility = "visible";
		},

		// returns the scroll left and top for the browser viewport.
		getScroll:	function () {
			if (document.all && typeof document.body.scrollTop != "undefined") {	// IE model
				var ieBox = document.compatMode != "CSS1Compat";
				var cont = ieBox ? document.body : document.documentElement;
				return {x : cont.scrollLeft, y : cont.scrollTop};
			}
			else {
				return {x : window.pageXOffset, y : window.pageYOffset};
			}

		}

	};


/*
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
::: CurrencyFormat
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
// Modo de Usar: onKeyPress="return(currencyFormat(this,'.',',',event, 9))
// This script and many more are available free online at
// The JavaScript Source!! http://javascript.internet.com
// Ultimo Escopo define o tamanho máx de caracteres para valores

function currencyFormat(fld, milSep, decSep, e, maxchr) {
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13) return true;  // Enter
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
	len = fld.value.length;
	for(i = 0; i < len; i++)
	if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
	aux = '';
	for(; i < len; i++)
	if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) fld.value = '';
	if (len == 1) fld.value = '0'+ decSep + '0' + aux;
	if (len == 2) fld.value = '0'+ decSep + aux;
	if (len > 2 && len < (maxchr + 1)) {
	aux2 = '';
	for (j = 0, i = len - 3; i >= 0; i--) {
	if (j == 3) {
	aux2 += milSep;
	j = 0;
	}
	aux2 += aux.charAt(i);
	j++;
	}
	fld.value = '';
	len2 = aux2.length;
	for (i = len2 - 1; i >= 0; i--)
	fld.value += aux2.charAt(i);
	fld.value += decSep + aux.substr(len - 2, len);
	}
	return false;
}


/*
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
::: Desabilitar teclas [Backspace] e [Delete] em um campo
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
// Modo de Usar: onKeyDown="myKeyHandler();"

function myKeyHandler() {
	document.onKeyDown = myKeyHandler;

    if ((window.event && window.event.keyCode == 8) || (window.event && window.event.keyCode == 46)) {
        // try to cancel the backspace and delete
        window.event.cancelBubble = true;
        window.event.returnValue = false;
        return false;
    }
}

