
/* Funções básicas */ 

function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

function focusFirst() {
  if (document.forms.length > 0) {
    var field = document.forms[0];
    for (i = 0; i < field.length; i++) {
      if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type == "select")) {
        if (!field.elements[i].disabled) {
          field.elements[i].focus();
          break;
        }
      }
    }
  }
}

function loadingMessage(msg) {
  
  return "<span class='title'>" + msg + "</span>";
  
}

function elementById(id) {
  return document.getElementById(id);
}

function showErrorMessage(sMessage) {
  var aMessage = sMessage.split("|");
  sMessage = "";
  for (var i = 1; i < aMessage.length; i++) {
    sMessage = sMessage + "* " + aMessage[i] + "\n";
  }
  
  if (aMessage[0] != "") {
    sMessage = sMessage + "* " + aMessage[0] + "\n";
  }
  
  alert("Ocorreram os seguintes erros: \n\n" + sMessage);
}

function errorRequest(obj){
  alert("Ocorreu um erro durante a requisição \n" + obj.status + " " + obj.statusText);
}

function focusFirst() {
  if (document.forms.length > 0) {
    var field = document.forms[0];
    for (i = 0; i < field.length; i++) {
      if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type == "select")) {
        if (!field.elements[i].disabled) {
          field.elements[i].focus();
          break;
        }
      }
    }
  }
}

function submitForm(form) {

	advAJAX.submit(form, {
	  onSuccess : function(obj) { var result = trim(obj.responseText); if (result != "") { showErrorMessage(result); } else { focusFirst(); } }, 
	  onError : function(obj) { errorRequest(obj); }
	});
	return false;
	
}

/* fim das funções básicas */

/* 
  Roberto Almeida Longhi
  funções para gerenciar o cadastro de clientes
*/

function carregaCliente() {

  listaCliente();
  editaCliente(0);
  
}

function listaCliente() {

  advAJAX.get({
    url: "cliente_lis.php",
    onInitialization : function(obj) { elementById("list").innerHTML = loadingMessage("Carregando listagem..."); },
    onSuccess : function(obj) { elementById("list").innerHTML = obj.responseText; },
    onError : function(obj) { errorRequest(obj); }
  });

}

function editaCliente(id) {
  
  advAJAX.get({
    url: "cliente_cad.php",
	id: id,
    onInitialization : function(obj) { elementById("content").innerHTML = loadingMessage("Carregando registro..."); },
    onSuccess : function(obj) { elementById("content").innerHTML = obj.responseText; },
    onError : function(obj) { errorRequest(obj); }
  });
  
}

function insereCliente(id) {
  
  editaCliente(0);
  
}

function apagaCliente(id) {
  
  if (confirm("Deseja realmente excluir este registro ?")) {
	  advAJAX.get({
		url: "cliente_cad.php",
		id: id,
		op: "del",
		onSuccess : function(obj) { elementById("usr_" + id).style.display = "none"; },
		onError : function(obj) { errorRequest(obj); }
	  }); 
  }
  
}

function submitCliente(form) {

	advAJAX.submit(form, {
	  onSuccess : function(obj) { var result = trim(obj.responseText); if (result != "") { showErrorMessage(result); } else { focusFirst(); } listaCliente(); }, 
	  onError : function(obj) { errorRequest(obj); }
	});
	return false;
	
}

/* 
  Roberto Almeida Longhi
  funções para gerenciar o cadastro de usuários
*/

function carregaUsuario() {

  listaUsuario();
  insereUsuario();
  
}

function listaUsuario() {

  advAJAX.get({
    url: "usuario_lis.php",
    onInitialization : function(obj) { elementById("list").innerHTML = loadingMessage("Carregando listagem..."); },
    onSuccess : function(obj) { elementById("list").innerHTML = obj.responseText; },
    onError : function(obj) { errorRequest(obj); }
  });

}

function editaUsuario(id) {
  
  advAJAX.get({
    url: "usuario_cad.php",
	id: id,
    onInitialization : function(obj) { elementById("content").innerHTML = loadingMessage("Carregando cadastro..."); },
    onSuccess : function(obj) { elementById("content").innerHTML = obj.responseText; },
    onError : function(obj) { errorRequest(obj); }
  });
  
}

function insereUsuario() {
  
  advAJAX.get({
    url: "usuario_cad.php",
	op: "ins",
    onInitialization : function(obj) { elementById("content").innerHTML = loadingMessage("Carregando cadastro..."); },
    onSuccess : function(obj) { elementById("content").innerHTML = obj.responseText; },
    onError : function(obj) { errorRequest(obj); }
  });
  
}

function apagaUsuario(id) {
  
  if (confirm("Deseja realmente excluir este registro ?")) {
	  advAJAX.get({
		url: "usuario_cad.php",
		id: id,
		op: "del",
		onSuccess : function(obj) { elementById("itm" + id).style.display = "none"; },
		onError : function(obj) { errorRequest(obj); }
	  }); 
  }
  
}

function submitUsuario(form) {

	advAJAX.submit(form, {
	  onSuccess : function(obj) { var result = trim(obj.responseText); if (result != "") { showErrorMessage(result); } else { focusFirst(); } listaUsuario(); }, 
	  onError : function(obj) { errorRequest(obj); }
	});
	return false;
	
}

/* 
  Roberto Almeida Longhi
  funções para gerenciar o cadastro de empresas
*/

function carregaEmpresa() {

  listaEmpresa();
  insereEmpresa();
  
}

function listaEmpresa() {

  advAJAX.get({
    url: "empresa_lis.php",
    onInitialization : function(obj) { elementById("list").innerHTML = loadingMessage("Carregando listagem..."); },
    onSuccess : function(obj) { elementById("list").innerHTML = obj.responseText; },
    onError : function(obj) { errorRequest(obj); }
  });

}


function ivan() {

  advAJAX.get({
    url: "rat_lis.php",
    onInitialization : function(obj) { elementById("list").innerHTML = loadingMessage("Carregando listagem..."); },
    onSuccess : function(obj) { elementById("list").innerHTML = obj.responseText; },
    onError : function(obj) { errorRequest(obj); }
  });

}



function editaEmpresa(id) {
  
  advAJAX.get({
    url: "empresa_cad.php",
	id: id,
    onInitialization : function(obj) { elementById("content").innerHTML = loadingMessage("Carregando cadastro..."); },
    onSuccess : function(obj) { elementById("content").innerHTML = obj.responseText; },
    onError : function(obj) { errorRequest(obj); }
  });
  
}

function insereEmpresa() {
  
  advAJAX.get({
    url: "empresa_cad.php",
	op: "ins",
    onInitialization : function(obj) { elementById("content").innerHTML = loadingMessage("Carregando cadastro..."); },
    onSuccess : function(obj) { elementById("content").innerHTML = obj.responseText; },
    onError : function(obj) { errorRequest(obj); }
  });
  
}

function apagaEmpresa(id) {
  
  if (confirm("Deseja realmente apagar esta empresa?")) {
	
	  advAJAX.get({
		url: "empresa_cad.php",
		id: id,
		op: "del",
		onSuccess : function(obj) { 
			elementById("itm" + id).style.display = "none";
		},
		onError : function(obj) { errorRequest(obj); }
	  });
  }
  
}

function submitEmpresa(form) {

	advAJAX.submit(form, {
	  onSuccess : function(obj) { var result = trim(obj.responseText); if (result != "") { showErrorMessage(result); } else { focusFirst(); } listaEmpresa(); }, 
	  onError : function(obj) { errorRequest(obj); }
	});
	return false;
	
}

/* 
  Roberto Almeida Longhi
  funções para gerenciar o cadastro de programas
*/

function carregaPrograma() {

  listaPrograma();
  inserePrograma();
  
}

function listaPrograma() {

  advAJAX.get({
    url: "programa_lis.php",
    onInitialization : function(obj) { elementById("list").innerHTML = loadingMessage("Carregando listagem..."); },
    onSuccess : function(obj) { elementById("list").innerHTML = obj.responseText; },
    onError : function(obj) { errorRequest(obj); }
  });

}

function editaPrograma(id) {
  
  advAJAX.get({
    url: "programa_cad.php",
	id: id,
    onInitialization : function(obj) { elementById("content").innerHTML = loadingMessage("Carregando listagem..."); },
    onSuccess : function(obj) { elementById("content").innerHTML = obj.responseText; },
    onError : function(obj) { errorRequest(obj); }
  });
  
}

function inserePrograma() {
  
  advAJAX.get({
    url: "programa_cad.php",
	op: "ins",
    onInitialization : function(obj) { elementById("content").innerHTML = loadingMessage("Carregando cadastro..."); },
    onSuccess : function(obj) { elementById("content").innerHTML = obj.responseText; },
    onError : function(obj) { errorRequest(obj); }
  });
  
}

function apagaPrograma(id) {
  
  if (confirm("Deseja realmente apagar este programa?")) {
	
	  advAJAX.get({
		url: "programa_cad.php",
		id: id,
		op: "del",
		onSuccess : function(obj) { 
			elementById("itm" + id).style.display = "none";
		},
		onError : function(obj) { errorRequest(obj); }
	  });
  }
  
}


function submitPrograma(form) {

	advAJAX.submit(form, {
	  onSuccess : function(obj) { var result = trim(obj.responseText); if (result != "") { showErrorMessage(result); } else { focusFirst(); } listaPrograma(); }, 
	  onError : function(obj) { errorRequest(obj); }
	});
	return false;
	
}

/* 
  Roberto Almeida Longhi
  funções para gerenciar o cadastro de RAT
*/

function carregaRAT() {

  listaRAT(2);
  insereRAT();
  
}

function listaRAT(tipoRat) {

  advAJAX.get({
    url: "rat_lis.php",
		tipo: tipoRat,
    onInitialization : function(obj) { elementById("list").innerHTML = loadingMessage("Carregando listagem..."); },
    onSuccess : function(obj) { elementById("list").innerHTML = obj.responseText; },
    onError : function(obj) { errorRequest(obj); }
  });

}

function editaRAT(id) {
  
  advAJAX.get({
    url: "rat_cad.php",
	id: id,
    onInitialization : function(obj) { elementById("content").innerHTML = loadingMessage("Carregando listagem..."); },
    onSuccess : function(obj) { elementById("content").innerHTML = obj.responseText; },
    onError : function(obj) { errorRequest(obj); }
  });
  
}

function insereRAT() {
  
  advAJAX.get({
    url: "rat_cad.php",
	op: "ins",
    onInitialization : function(obj) { elementById("content").innerHTML = loadingMessage("Carregando cadastro..."); },
    onSuccess : function(obj) { elementById("content").innerHTML = obj.responseText; },
    onError : function(obj) { errorRequest(obj); }
  });
  
}

function apagaRAT(id) {
  
  if (confirm("Deseja realmente apagar este RAT?")) {
	
	  advAJAX.get({
		url: "rat_cad.php",
		id: id,
		op: "del",
		onSuccess : function(obj) { 
			elementById("itm" + id).style.display = "none";
		},
		onError : function(obj) { errorRequest(obj); }
	  });
  }
  
}

function imprimeRAT(id) {
  window.open("rat_cad_imprime.php?id=" + id, "rat", "height = 600, width = 750, scrollbars = yes");    //window.open("rat_cad_imprime.php?id=" + id, "rat", "fullscreen = yes");  
  
}

function submitRat(form) {

	advAJAX.submit(form, {
	  onSuccess : function(obj) { var result = trim(obj.responseText); if (result != "") { showErrorMessage(result); } else { focusFirst(); } listaRAT(); }, 
	  onError : function(obj) { errorRequest(obj); }
	});
	return false;
	
}


/* 
  Roberto Almeida Longhi
  funções para gerenciar os arquivos
*/

function carregaInicio() {

  listaArquivos();
  listaSistemas();
	ajustaDivs();
  
}

function listaArquivos() {

  advAJAX.get({
    url: "explorer.php",
    onInitialization : function(obj) { elementById("content").innerHTML = loadingMessage("Carregando arquivos..."); },
    onSuccess : function(obj) { elementById("content").innerHTML = obj.responseText; },
    onError : function(obj) { errorRequest(obj); }
  });

}

function listaSistemas() {

  advAJAX.get({
    url: "sistemas.php",
    onInitialization : function(obj) { elementById("list").innerHTML = loadingMessage("Carregando arquivos..."); },
    onSuccess : function(obj) { elementById("list").innerHTML = obj.responseText; },
    onError : function(obj) { errorRequest(obj); }
  });

}

function adicionarArquivo(elemento, arquivo, id) {

  advAJAX.get({
    url: "programa_arquivo_cad.php",
		op: "ins",
		arquivo: arquivo.value,
		id: id,
    onSuccess : function(obj) { 
			item = document.createElement("li");
			item.innerHTML = arquivo.value + " <a href='#' fname='" + arquivo.value + "' onclick='apagarArquivo(this, " + id + ");'>apagar</a>";
			elemento.appendChild(item);
			arquivo.value = "";
		},
    onError : function(obj) { errorRequest(obj); }
  });
  

}

function apagarArquivo(elemento, id) {

  advAJAX.get({
    url: "programa_arquivo_cad.php",
		op: "del",
		arquivo: elemento.getAttribute("fname"),
		id: id,
    onSuccess : function(obj) { 
			elemento.parentNode.style.display = "none";
		}
  });

}


function adicionarPrograma(elemento, programa, id) {

  advAJAX.get({
    url: "empresa_programa_cad.php",
		op: "ins",
		programa: programa.value,
		id: id,
    onSuccess : function(obj) { 
			item = document.createElement("li");
			item.innerHTML = programa.options[programa.selectedIndex].innerHTML + " <a href='#' proid='" + programa.value + "' onclick='apagarPrograma(this, " + id + ");'>apagar</a>";
			elemento.appendChild(item);
			programa.value = "";
		},
    onError : function(obj) { errorRequest(obj); }
  });
  

}

function apagarPrograma(elemento, id) {

  advAJAX.get({
    url: "empresa_programa_cad.php",
		op: "del",
		programa: elemento.getAttribute("proid"),
		id: id,
    onSuccess : function(obj) { 
			elemento.parentNode.style.display = "none";
		}
  });

}

function ajustaDivs() {
  

	if (parseInt(navigator.appVersion)>3) {
	 if (navigator.appName=="Netscape") {
		winH = window.innerHeight;
	 }
	 if (navigator.appName.indexOf("Microsoft")!=-1) {
		winH = document.body.offsetHeight;
	 }
	}
	
	fator = 80;
  elementById("list").style.height = (winH - fator) + "px";
  elementById("content").style.height = (winH - fator) + "px";
  
}
