function ltrim(palavra){
   for(i=0;i<palavra.length;i++){
      letra=palavra.charAt(i);
      if(letra!=' '){
         return(palavra.substring(i,palavra.length));
      }
   }
   return("");
}

function rtrim(palavra){
   for(i=palavra.length-1;i>=0;i--){
      letra=palavra.charAt(i);
      if(letra!=' '){
         return(palavra.substring(0,i+1));
      }
   }
   return("");
}

function trim(palavra){
   return(ltrim(rtrim(palavra)));
}

function formataMoeda(str){
	str = str + '';
	str = replaceAll(str, ".", ",");
	var antVirgula = "";
	cont = str.indexOf(",")-1;
	if (cont <= -1) {
		antVirgula = str; 
		posVirgula = "";
	}else{ 
		antVirgula = str.substring(0,cont+1);
		posVirgula = str.substring(cont+1,str.length);
		if ((posVirgula.length < 3) ){
			posVirgula = posVirgula + "0";
		}
	}
	auxValor = "";
	numVig = 0;	
	for (n=antVirgula.length-1;n>=0;n--){
		auxValor = antVirgula.charAt(n) + auxValor;
		if ((auxValor.length - numVig) % 3 == 0) {
			auxValor = "." + auxValor;
			numVig++;
		}
	}
	if (auxValor.substring(0,1) == '.') {
		auxValor = auxValor.substring(1,auxValor.length);
	}
	antVirgula = auxValor;
	if (cont <= -1){
		antVirgula = antVirgula + ",00";
	}else{
		if (posVirgula.length > 3){
			if (posVirgula.substring(3,4) >= 5){
				posVirgula = "," + (parseInt(posVirgula.substring(1,3),10) + 1);
			}
			else{
				posVirgula = posVirgula.substring(0,3);
			}
		}
	}
	str = antVirgula + posVirgula;
	return str;
}

function replaceAll(str, c1, c2) {
	temp = "" + str; 
	while (temp.indexOf(c1)>-1) {
		pos= temp.indexOf(c1);
		temp = "" + (temp.substring(0, pos) + c2 + 
		temp.substring((pos + c1.length), temp.length));
	}
	return temp;
}

// Compare two options within a list by VALUES
function compareOptionValues(a, b) { 
  // Radix 10: for numeric values
  // Radix 36: for alphanumeric values
  var sA = parseInt( a.value, 36 );  
  var sB = parseInt( b.value, 36 );  
  return sA - sB;
}

// Compare two options within a list by TEXT
function compareOptionText(a, b) { 
  // Radix 10: for numeric values
  // Radix 36: for alphanumeric values
  var sA = 0;  
  var sB = 0;
  var r = 0;
  for (i=0; sA==sB; i++){
	sA = parseInt( a.text.substring(i,i+1), 36 );  
	sB = parseInt( b.text.substring(i,i+1), 36 );
	r = sA - sB;
  }
  return r;
}

// Dual list move function
function moveDualList( srcList, destList, moveAll ) {

  // Do nothing if nothing is selected
  if (  ( srcList.selectedIndex == -1 ) && ( moveAll == false )   )  {
    return;
  }
  newDestList = new Array( destList.options.length );

  var len = 0;

  for( len = 0; len < destList.options.length; len++ )   {
    if ( destList.options[ len ] != null )  {
      newDestList[ len ] = new Option( destList.options[ len ].text, destList.options[ len ].value, destList.options[ len ].defaultSelected, destList.options[ len ].selected );
    }
  }

  for( var i = 0; i < srcList.options.length; i++ )  { 
    if ( srcList.options[i] != null && ( srcList.options[i].selected == true || moveAll ) )    {
       newDestList[ len ] = new Option( srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, srcList.options[i].selected );
       len++;
    }
  }

  // Sort out the new destination list 
  // A ÚNICA TELA QUE USA DUALLIST NÃO PODE ORDENAR, SE APARCER OUTRA DEVE DESCOMENTAR CÓDIGO ABAIXO E TRATAR
  //newDestList.sort( compareOptionValues );   // BY VALUES
  //newDestList.sort( compareOptionText );   // BY TEXT

  // Populate the destination with the items from the new array
  for (var j = 0; j < newDestList.length; j++)  {
    if (newDestList[j] != null)  {
      destList.options[j] = newDestList[ j ];
    }
  }

  // Erase source list selected elements
  for(var i = srcList.options.length - 1; i >= 0; i-- )  { 
    if (srcList.options[i] != null && (srcList.options[i].selected == true || moveAll)){
       srcList.options[i] = null;
    }
  }

} // End of moveDualListOrdem()

// Dual list move function
function moveDualListOrdem(srcList, flgSobe) {

	// Do nothing if nothing is selected
	if (( srcList.selectedIndex == -1 )) return;
	
	newDestList = new Array( srcList.options.length );

	var len = 0;
	var lenAux = 0;
	var itm1;
	
	if (flgSobe){
		for(var i = 0; i < srcList.options.length; i++)  { 
			if (srcList.options[i] != null)    
				if (srcList.options[i].selected == true){
					newDestList[len] = new Option(srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, srcList.options[i].selected);
					len++;
				}else{
					if (itm1 != null){
						newDestList[len] = itm1;
						len++;
						itm1 = null;
					}
					itm1 = new Option( srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, srcList.options[i].selected );
				}
		}
		if (itm1 != null){
			newDestList[len] = itm1;
			len++;
			itm1 = null;
		}
	}else{
		newDestListAux = new Array( srcList.options.length );
		for(var i = 0; i < srcList.options.length; i++)  { 
			if (srcList.options[i] != null)    
				if (srcList.options[i].selected != true){
					newDestList[len] = new Option(srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, srcList.options[i].selected);
					len++;
					for (var a = 0; a < lenAux; a++)  {
						if (newDestListAux[a] != null)  {
							newDestList[len] = newDestListAux[a];
							newDestListAux[a] = null;
							len++;
						}
					}
					lenAux = 0;
				}else{
					newDestListAux[lenAux] = new Option( srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, srcList.options[i].selected );
					lenAux++;
				}
		}
		for (var a = 0; a < lenAux; a++)  {
			if (newDestListAux[a] != null)  {
				newDestList[len] = newDestListAux[a];
				newDestListAux[a] = null;
				len++;
			}
		}
		lenAux = 0;
	}
	
	// Erase source list selected elements
	for(var i = srcList.options.length - 1; i >= 0; i-- )  { 
		if (srcList.options[i] != null){
			srcList.options[i] = null;
		}
	}
	// Populate the destination with the items from the new array
	for (var j = 0; j < len; j++)  {
		if (srcList[j] == null)  {
			srcList.options[j] = newDestList[j];
		}
	}

} // End of moveDualListOrdem()

function copyDualList( srcList, destList, moveAll ) {

  // Do nothing if nothing is selected
  if (  ( srcList.selectedIndex == -1 ) && ( moveAll == false )   )  {
    return;
  }
  newDestList = new Array( destList.options.length );

  var len = 0;

  for( len = 0; len < destList.options.length; len++ )   {
    if ( destList.options[ len ] != null )  {
      newDestList[ len ] = new Option( destList.options[ len ].text, destList.options[ len ].value, destList.options[ len ].defaultSelected, destList.options[ len ].selected );
    }
  }

  for( var i = 0; i < srcList.options.length; i++ )  { 
    if ( srcList.options[i] != null && ( srcList.options[i].selected == true || moveAll ) )    {
       newDestList[ len ] = new Option( srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, srcList.options[i].selected );
       len++;
    }
  }

  // Sort out the new destination list 
  // A ÚNICA TELA QUE USA DUALLIST NÃO PODE ORDENAR, SE APARCER OUTRA DEVE DESCOMENTAR CÓDIGO ABAIXO E TRATAR
  //newDestList.sort( compareOptionValues );   // BY VALUES
  //newDestList.sort( compareOptionText );   // BY TEXT

  // Populate the destination with the items from the new array
  for (var j = 0; j < newDestList.length; j++)  {
    if (newDestList[j] != null)  {
      destList.options[j] = newDestList[ j ];
    }
  }

} // End 


// Dual list move function
function removeDualList( srcList, moveAll ) {

  // Do nothing if nothing is selected
  if (  ( srcList.selectedIndex == -1 ) && ( moveAll == false )   )  {
    return;
  }

  // Erase source list selected elements
  for(var i = srcList.options.length - 1; i >= 0; i-- )  { 
    if (srcList.options[i] != null && (srcList.options[i].selected == true || moveAll)){
       srcList.options[i] = null;
    }
  }

} // End 

//validação de valores reais
function FiltroReal(event){
	var tecla;
	var key;
	var strValidos = '0123456789,';
	if( navigator.appName.indexOf('Netscape')!= -1 )
		tecla= event.which;
	else
		tecla= event.keyCode;
	caractere = String.fromCharCode( tecla);
	if ((strValidos.indexOf(caractere) == -1) && (tecla != 0) && (tecla != 8))
		return false;
	else
		return true;
}
function ValidaReal(checkStr){
    if( checkStr == '' )
      return  true;
    var marMil = '.';
    var marDec = ',';
    var checkOK = '0123456789-';
    var allValid = true;
    var decPoints = 0;
    var allNum = '';
    var contCasas = 0;
    var Ponto = false;
    var temPonto = 0;
	temPonto = checkStr.indexOf(marMil);
    ch = checkStr.charAt(0);
    for (j = 0;  j < checkOK.length;  j++){
	    if (ch == checkOK.charAt(j)) break;
		if ((j+1) == checkOK.length){
		allValid = false;
		break;
		}
    }
	if (allValid) {
		allNum += ch;
		if (ch == '-'){
		checkOK = '0123456789';
		if (checkStr.length == 1) return false;
		}
		else { checkOK = '0123456789,.'; contCasas++; }
		for (i = 1;  i < checkStr.length;  i++){
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		    if (ch == checkOK.charAt(j))
		    break;
		    if (j == checkOK.length){
		    allValid = false;
		    break;
		    }
		if (ch == marDec){
		    allNum += '.';
		    decPoints++;
		    checkOK = '0123456789';
		    contCasas = 0;
		    Ponto = false;
		}
		else{
			if (ch != marMil){
		    allNum += ch;
			contCasas++;
			if ((contCasas > 3) && (temPonto >= 0) && (decPoints == 0)){
				allValid = false;
				break;
			}
			if ((contCasas == 3) && (temPonto >= 0) && (decPoints == 0)) checkOK = '0123456789.,';
		    }
		    else{
			if ((contCasas > 3) && (decPoints == 0)){
				allValid = false;
				break;
			}
			Ponto = true;
			contCasas = 0;
			checkOK = '0123456789';
		    }
		}
		}
	}
	if (Ponto && (contCasas != 3)) return false;
    if (!allValid)
		return false;
    if (decPoints > 1)
        return false;
    return true;
}//fim validação de valores reais


function TextBoxKeyPressFormat(Mascara) 
{
	var event = window.event;  // to handle both NS and IE events
	var objForm = event.target ? event.target : event.srcElement;
	var inputChar = String.fromCharCode(event.keyCode ? event.keyCode : event.charCode);
	var iKeyCode = event.keyCode;

	var texto = objForm.value;
	if (texto.length >= objForm.maxLength) return false;
	var textoAux = "";

	for (i=0; i<Mascara.length; i++)
	{
		if (Mascara.charAt(i) != '0' && Mascara.charAt(i) != '#' && Mascara.charAt(i) != 'a' && Mascara.charAt(i) != 'l')
		{
			texto = texto.replace(Mascara.charAt(i),"");
		} 
	}
	texto = texto + inputChar;

	if (iKeyCode == 8)
		texto = texto.substr(0, texto.length-1);

	var indiceMascara = 0;
	var indiceTexto = 0;
	var inseriuMascara = false;

	if (Mascara.charAt(0) == '#')
	{
		indiceMascara = Mascara.length;
		for (indiceTexto=texto.length -1; indiceTexto>=0; indiceTexto-- )	
		{
			do 
			{
				inseriuMascara = false;
				if (indiceMascara > 0) indiceMascara = indiceMascara - 1;
				if (Mascara.charAt(indiceMascara) == '0' || Mascara.charAt(indiceMascara) == '#') 
				{
					if (iKeyCode > 47 && iKeyCode < 58)
						textoAux = texto.charAt(indiceTexto) + textoAux;
					else return false;
				} else if (Mascara.charAt(indiceMascara) == 'a') 
				{
					if ((iKeyCode > 47 && iKeyCode < 58) || (iKeyCode > 64 && iKeyCode < 91) || (iKeyCode > 96 && iKeyCode < 123))
						textoAux = texto.charAt(indiceTexto) + textoAux;
					else return false;
				} else if (Mascara.charAt(indiceMascara) == 'l') 
				{
					if ((iKeyCode > 64 && iKeyCode < 91) || (iKeyCode > 96 && iKeyCode < 123))
						textoAux = texto.charAt(indiceTexto) + textoAux;
					else return false;
				} else
				{
					textoAux = Mascara.charAt(indiceMascara) + textoAux;
					inseriuMascara = true;
				}
			} while (inseriuMascara);
			if (textoAux.length >= objForm.maxLength) break;
		}
	}
	else
	{
		for (indiceTexto=0; indiceTexto< texto.length; indiceTexto++ )	
		{
			iKeyCode = texto.charCodeAt(indiceTexto)
			do 
			{
				inseriuMascara = false;
				if (Mascara.charAt(indiceMascara) == '0' || Mascara.charAt(indiceMascara) == '#') 
				{
					if (iKeyCode > 47 && iKeyCode < 58)
						textoAux = textoAux + texto.charAt(indiceTexto);
					else return false;
				} else if (Mascara.charAt(indiceMascara) == 'a') 
				{
					if ((iKeyCode > 47 && iKeyCode < 58) || (iKeyCode > 64 && iKeyCode < 91) || (iKeyCode > 96 && iKeyCode < 123))
						textoAux = textoAux + texto.charAt(indiceTexto);
					else return false;
				} else if (Mascara.charAt(indiceMascara) == 'l') 
				{
					if ((iKeyCode > 64 && iKeyCode < 91) || (iKeyCode > 96 && iKeyCode < 123))
						textoAux = textoAux + texto.charAt(indiceTexto);
					else return false;
				} else
				{
					textoAux = textoAux + Mascara.charAt(indiceMascara);
					inseriuMascara = true;
				}
				if (indiceMascara < Mascara.length - 1) indiceMascara = indiceMascara + 1;
			} while (inseriuMascara);
			if (textoAux.length >= objForm.maxLength) break;
		}
	}
	objForm.value = textoAux;
	return false;		
}

function AbrePopUpClienteDados(codigo){
    window.open('../inscricao/EVEPOPClienteDados.aspx?cod_cliente_cli=' + codigo, 'EVEPOPDadosClientes', 'top=5,left=5,width=710,height=550')
}

function AbrePopUpClienteDadosTurismo(codigo){
    window.open('../turismo/EVEPOPClienteDados.aspx?cod_cliente_cli=' + codigo, 'EVEPOPDadosClientesTurismo', 'top=5,left=5,width=710,height=550')
}

function AbrePopUpTrabalhoDados(codigo){
    window.open('../temalivre/EVEPOPTrabalhoDados.aspx?cod_trabalho_tra=' + codigo, 'EVEPOPDadosTrabalho', 'top=5,left=5,width=710,height=550')
}

function AbrePopUpTrabalhoAutorPrincipal(tipotela, codigo){
    window.open('../temalivre/EVEPOPTrabalhoAutorPrincipal.aspx?TipoTela=' + tipotela + '&cod_trabalho_tra=' + codigo, 'EVEPOPTrabalhoAutorPrincipal', 'top=5,left=5,width=710,height=400')
}

function MarcarTodos(crtl){
	for (i=0; i < document.forms[0].elements.length; i++){
		if (document.forms[0].elements[i].type == 'checkbox'){
			document.forms[0].elements[i].checked = crtl.checked;
		}
	}
}