var over = false;
function mostrar(objeto){
	document.getElementById(objeto).style.display = "block";
	over = true;
	document.getElementById(objeto).onmouseover=function (){over=true;setTimeout("document.getElementById('"+objeto+"').style.display='block';",200);}
	document.getElementById(objeto).onmouseout=function (){over=false;setTimeout("if(!over)document.getElementById('"+objeto+"').style.display='none';",200);}
	
}

function esconder(objeto){
	over = false;
	setTimeout("if(!over)document.getElementById('"+objeto+"').style.display='none';",100);

	document.getElementById(objeto).onmouseout=function (){over=false;setTimeout("if(!over)document.getElementById('"+objeto+"').style.display='none';",200);}
	
	document.getElementById(objeto).onmouseover=function (){over=true;setTimeout("document.getElementById('"+objeto+"').style.display='block';",200);}
}

//Máscara---------------------------

addEvent = function(o, e, f, s){
    var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
    r[r.length] = [f, s || o], o[e] = function(e){
        try{
            (e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
            e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
            e.target || (e.target = e.srcElement || null);
            e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
        }catch(f){}
        for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
        return e = null, !!d;
    }
};

removeEvent = function(o, e, f, s){
    for(var i = (e = o["_on" + e] || []).length; i;)
        if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
            return delete e[i];
    return false;
};

MaskInput = function(f, m){
    function mask(e){
        var patterns = {"1": /[A-Z]/i, "2": /[0-9]/, "4": /[\xC0-\xFF]/i, "8": /./ },
            rules = { "a": 3, "A": 7, "9": 2, "C":5, "c": 1, "*": 8};
        function accept(c, rule){
            for(var i = 1, r = rules[rule] || 0; i <= r; i<<=1)
                if(r & i && patterns[i].test(c))
                    break;
                return i <= r || c == rule;
        }
        var k, mC, r, c = String.fromCharCode(k = e.key), l = f.value.length;
        (!k || k == 8 ? 1 : (r = /^(.)\^(.*)$/.exec(m)) && (r[0] = r[2].indexOf(c) + 1) + 1 ?
            r[1] == "O" ? r[0] : r[1] == "E" ? !r[0] : accept(c, r[1]) || r[0]
            : (l = (f.value += m.substr(l, (r = /[A|9|C|\*]/i.exec(m.substr(l))) ?
            r.index : l)).length) < m.length && accept(c, m.charAt(l))) || e.preventDefault();
    }
    for(var i in !/^(.)\^(.*)$/.test(m) && (f.maxLength = m.length), {keypress: 0, keyup: 1})
        addEvent(f, i, mask);
};
//Termina máscara--------------------------

function cria_oculto(nome,valor){
	var campo = "<input type=hidden name='"+nome+"' value ='"+valor+"' id='"+nome+"'>";
	return (campo);
}


//FUNÇÃO DE INSERIR SERVIÇO -------------------------------

function subtrai(valor){
var v=valor*1;
document.getElementById("total").value = document.getElementById("total").value - v;
}


var conta_itens_servico = 0; var indice_servico = 0;
function insere_servico(){

 valor_s = document.getElementById("tratamentos").value;
 nome_s = document.form1.tratamentos.options[document.form1.tratamentos.selectedIndex].nome;

	//Insere os campos na grade
	var tabela = document.getElementById('tab_servicos');
 	tabela.insertRow(conta_itens_servico+1);
 	var linha = tabela.rows[conta_itens_servico+1];
	linha.id = "tr_"+ conta_itens_servico; 
/*	linha.className = "peq";*/
	linha.style.textAlign = "center";
	linha.style.backgroundColor = "#f5f5f5";
	linha.insertCell(0);linha.insertCell(1);linha.insertCell(2);
	linha.cells[0].innerHTML = nome_s+cria_oculto('nome_s'+indice_servico,nome_s);
	linha.cells[1].innerHTML = valor_s+cria_oculto('valor_s'+indice_servico,valor_s);
	
	var valor_num = linha.id.split("_"); //Cria a variável para resgatar o valor do anual na hora de excluir, para subtrair de total
	document.getElementById("total").value=(document.getElementById("total").value*1)+(valor_s*1);
	
	//Insere linha de excluir na grade normal.
	linha.cells[2].innerHTML = "<a class=mao title='Excluir' href=\"javascript:subtrai(document.getElementById('valor_s"+valor_num[1]+"').value);document.getElementById('tab_servicos').deleteRow(document.getElementById('"+linha.id+"').rowIndex);conta_itens_servico--;void(0)\">X</a>";
	
	conta_itens_servico++;
	indice_servico++;

}
//----FIM DE INSERIR SERVIÇO-------------------------------

function abre_janela(janela,width,height){
	var atributos= 'width='+width + ', height='+height+',toolbar=no,top=0,left=0';
	window.open(janela,'',atributos);
}