// JavaScript Document
background_error ="#fff2f8";
background_ok ="#ffffff";
function destroy(_url){
    if (confirm('Deseja excluir?')){
        window.location=_url;
    }else{
        return false;
    }
}
function get_id(id){
    return document.getElementById(id);
}
function get_value(id){
    return document.getElementById(id).value;
}
function set_value(id, _value){
    document.getElementById(id).value = _value;
}
function set_focus(id){
    document.getElementById(id).focus();
}
/** utilizado para deixar cada tr da tabela de uma cor **/
function zebra(id_tabela){
    var $t = $(id_tabela);
    //var $t = $('table tbody');
    $('tr:odd',$t).addClass('row0');
    $('tr:even',$t).addClass('row1');
    $('tr',$t).hover(
    function(){
        $(this).addClass('row_hover');
    },
    function(){
        $(this).removeClass('row_hover');
    }
);
}
/** validações **/
function valida_data(data){
    var dia = 0; var mes = 0; var ano = 0;
    if (data.length == 10){
        dia = parseInt(data.substring(0,2)); mes = parseInt(data.substring(3,5)); ano = parseInt(data.substring(6,10));
    }
    if ( (dia > 0 && dia < 32) && (mes > 0 && mes < 13) && (ano > 1900) && data.length == 10 ){
        return true;
    }else{
        return false;
    }
}
function centraliza_janela(largura, altura){
    var windowH = altura;
    var windowW = largura
    var windowX = Math.ceil( (window.screen.width  - windowW) / 2 );
    var windowY = Math.ceil( (window.screen.height - windowH) / 2 );
    resizeTo( Math.ceil( largura ) , Math.ceil( altura ) );
    moveTo  ( Math.ceil( windowX ) , Math.ceil( windowY ) );
}
function get_x(largura){
    var windowW = largura
    return Math.ceil( (window.screen.width  - windowW) / 2 );
}
function get_y(altura){
    var windowH = altura;
    return Math.ceil( (window.screen.height - windowH) / 2 );
}
function formata_reais(fld, milSep, decSep, e){
    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;
    //alert(whichCode);
    if (whichCode == 13 || whichCode == 8 || whichCode == 0) return true;

    key = String.fromCharCode(whichCode);// Valor para o código da Chave

    if (strCheck.indexOf(key) == -1) return false; // Chave inválida

    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) {
        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;
}// fim do método

// Formata o campo valor
function formataValor(campo) {
	//campo.value = filtraCampo(campo);
	vr = Limpar(campo.value, "0123456789");
	tam = vr.length;

	if ( tam <= 2 ){
 		campo.value = vr ; }
 	if ( (tam > 2) && (tam <= 5) ){
 		campo.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
 	if ( (tam >= 6) && (tam <= 8) ){
 		campo.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
 	if ( (tam >= 9) && (tam <= 11) ){
 		campo.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
 	if ( (tam >= 12) && (tam <= 14) ){
 		campo.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
 	if ( (tam >= 15) && (tam <= 18) ){
 		campo.value = vr.substr( 0, tam - 14 ) + '.' + vr.substr( tam - 14, 3 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ;}

}
function Limpar(valor, validos) {
  // retira caracteres invalidos da string
  var result = "";
  var aux;
  for (var i=0; i<valor.length; i++)
  {
    aux = validos.indexOf(valor.substring(i, i+1));
    if (aux>=0)
    {
    	result += aux;
    }
  }
  return result;
}