/**
 * Formata o campo no formato dd/mm/aaaa.
 */
function formatData( campo, teclapres ) {
	var tecla = teclapres.keyCode;

	vr = campo.value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	tam = vr.length + 1;

	if ( tecla != 9 && tecla != 8 ){
		if ( tam > 2 && tam < 5 )
			campo.value = vr.substr( 0, 2  ) + '/' + vr.substr( 2, tam );
		if ( tam >= 5 && tam <= 10 )
			campo.value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 );
	}
}


function unload(){
	setCookie( "pesqele", escape( document.pesquisa.getValues()) )
}

function load(){
   pesq = getCookie( "pesqele" )
   if( pesq != null )
		document.pesquisa.setValues( pesq )
}

function setCookie( nome, valor ) {
	document.cookie = nome + "=" + valor
}

function getCookie(Name) {
    var search = Name + "="
    if (document.cookie.length > 0) { // if there are any cookies
            offset = document.cookie.indexOf(search)
            if (offset != -1) { // if cookie exists
                    offset += search.length
                    // set index of beginning of value
                    end = document.cookie.indexOf(";", offset)
                    // set index of end of cookie value
                    if (end == -1)
                    end = document.cookie.length
                    return unescape(document.cookie.substring(offset, end))
            }
    }
}

/**
 * Abre uma nova janela com nome target e de tamanho height x width
 */
function abreJanela( url, target, height, width ) {
	novaJanela = window.open( url, target, 'scrollbars=yes,height=' + height + ',width=' + width );
	novaJanela.focus();
}
function showPagina( url, namePg ){
	conf = "top=0,left=0,width=720,height=400,toolbar=no,resizable=yes,menubar=yes,scrollbars=yes,location=no"
	window.open( url, namePg, conf )
}

function showContratadas( tribunal ){
	urlClasse = "contratadas.jsp?tribunal=" + tribunal
	var hWnd = window.open( urlClasse, "Empresas Contratadas","width=400,height=325,resizable=yes,scrollbars=yes,menubar=yes" );
	if( ( document.window != null ) && !hWnd.opener ){
		hWnd.opener = document.window;
	}
}

/**
 * Abre uma pagina para o usuario.
 */
function abrePagina( url, height, width ){
	var hWnd = window.open( url, "url", 'scrollbars=yes,height=' + height + ',width=' + width );
	if( ( document.window != null ) && !hWnd.opener )
		hWnd.opener = document.window;
}

/**
 * Retorna a string de pesquisa para o campo passado.
 *
 * Caso o campo esteja vazio retornará uma string vazia
 */
function getQuery( valor, par ){
	par = ( par != ""? "[" + par + "]": "" );
	if( valor.length != 0 )
		return "(" + valor + ")" + par
	return ""
}
/**
 * Retorna a String de pesquisa por intervalo de data.
 */
function getQueryData1( dataIni, dataFim, par ){
	if( dataIni.length != 0 && dataFim.length != 0 ){
		return "@" + par + ">=" + invertData( dataIni ) + "<=" + invertData( dataFim )
	}else if( dataIni.length != 0 ){
		return "@" + par + ">=" + invertData( dataIni )
	}else if( dataFim.length != 0 ){
		return "@" + par + "<=" + invertData( dataFim )
	}else return ""
}
/**
 * Formata o campo data para o formato aaaammdd.
 */
function invertData( data ){
	if( data.length == 10 )
		return data.substr( 6, 4 ) + data.substr( 3, 2 ) + data.substr( 0, 2 )
	return valor;
}
/**
 * define o valor de varias checkbox ao mesmo tempo
 */
function checkAll( form, value ){
	for( i = 0; i < form.length; i++ ){
		if( form.elements[ i ].type == 'checkbox' ){
		   form.elements[ i ].checked = value
		}
	}
}
