// JavaScript Document

$("#alterarOrcamento").click(function(){
	alterarOrcamento();
});

function removeProdutoOrcamento(id_orcamento_produto, msg){
	
	//var msg = 'A remoção de qualquer produto acarretará num reorçamento de sua lista de produtos. Deseja prosseguir?';
	
	if(confirm(msg)){
		document.getElementById('acao').value = "removerProdutoOrcamento";
		document.getElementById('id_orcamento_produto_delete').value = id_orcamento_produto;
		document.getElementById('frm_orcamento').submit();		
	} else {
		return false;	
	}
	
}

function carregaTipoMaterial(id, codMat, id_produto_tipo_material_materiais){
	var arr = id.split("_");
	var linha = arr[1];
	var id = arr[2];
	var elSel = document.getElementById("tipo_"+linha+"_"+id);
	
	if(codMat==""){
		elSel.options.length = 0;
		var carregando = document.createElement("option");
		carregando.text = "selecione o tipo";
		carregando.value = "";
		try {
		  elSel.add(carregando, null);
		}catch(ex) {
		  elSel.add(carregando); // IE apenas
		}
		return false;
	}
	
	//Carregando..
	elSel.options.length = 0;
	elSel.disabled = "disabled";
	var carregando = document.createElement("option");
	carregando.text = "carregando..";
	carregando.value = "";
	try {
	  elSel.add(carregando, null);
	}catch(ex) {
	  elSel.add(carregando); // IE apenas
	}
			
	//Busca os tipos relativos ao material por AJAX		
	$.get("carregar_tipos_materiais.php?cod="+codMat,function(data){
		var arrayTipos = eval(data);
		elSel.options.length = 0;
		elSel.disabled = "";
		for(var i = 0; i<arrayTipos.length; i++){
			var tipo = document.createElement("option");
			tipo.text = arrayTipos[i][0];		
			tipo.value = arrayTipos[i][1];
			if(arrayTipos[i][1] == id_produto_tipo_material_materiais)
				tipo.selected = true;
			else
				tipo.selected = false;
				
			try {
			  elSel.add(tipo, null);
			}catch(ex) {
			  elSel.add(tipo); // IE apenas
			}
		}
   });
}

function contatoTelefonico(){
	document.getElementById('acao').value = "contatoTelefonico";
	document.getElementById('frm_orcamento').submit();		
}

function alterarOrcamento(){
	document.getElementById('acao').value = "alterar";
	document.getElementById('frm_orcamento').submit();
}

function fecharOrcamento(){
	document.getElementById('acao').value = "fecharOrcamento";
	document.getElementById('frm_orcamento').submit();			
}

function reabrirOrcamento(){
	document.getElementById('acao').value = "reabrirOrcamento";
	document.getElementById('frm_orcamento').submit();			
}

function carregaMateriais(indice, td, id_produto_acabamento, id_produto_tipo_material_materiais){
	
	$.post("./sistema/_orcamento/jx_carregaMateriais.php",{indice: indice, id_produto_acabamento: id_produto_acabamento, id_produto_tipo_material_materiais: id_produto_tipo_material_materiais}, function(data){
		
		$("#"+td).html(data);
				
	});

}
