/*

	AGCORP Network 2010 - Copyright Agcorp Servicos de Informatica Ltda
	Scripts desenvolvidos por Abdenego Santos
	Layout Jonas Ribeiro da Silva
	
	//Funçoes para Carregar Conteudo em Ajax
	//AGCORP - Abdenego 2008/2010

*/

function AbreJanela(url) {
          window.open(url);
}


function limpaCache(url)
{
   if (url.indexOf("?") >=0 ) return url + "&rand=" + encodeURI(Math.random());
   		else return url + "?randon=" + encodeURI(Math.random());
}


function paginacaoAjax( paginaurl, qualapagina, quantlinhas, parametros )
{
  pagina = limpaCache( "subpaginaAjax.php?url="+paginaurl+"&pp="+qualapagina+"&quant="+quantlinhas+"&parametros="+parametros);
  http.open("GET", pagina, true);
  mostra_div("espera");
  http.onreadystatechange = retorno_paginacaoAjax;
  http.send(null);
}

function divAjax( paginaurl, v1, v2, v3, v4, v5, v6 )
{
  //primeiro validar a pagina para prosseguir
	if (paginaurl == "assinarpacote.php") {
		d = document.dom;
		if (d.dominio.value == ""){
				alert("Favor preencher o domínio!");
				d.dominio.focus();
				return false;
			}
		if (d.sufixo.value == ""){
				alert("Favor preencher o sufixo!");
				d.sufixo.focus();
				return false;
		}
	}
  //passando pela validacao, enviar parametros para divAjax.php
  pagina = limpaCache( "divAjax.php?url="+paginaurl+"&v1="+v1+"&v2="+v2+"&v3="+v3+"&v4="+v4+"&v5="+v5+"&v6="+v6);
  http.open("GET", pagina, true);
  mostra_div("espera");
  http.onreadystatechange = retorno_divAjax;
  http.send(null);
}

function enviaFTP(setor, detalhe, pastachave)
{
  mostra_div("esperaftp");	
  pagina = limpaCache("enviaFTP.php?setor="+setor+"&det="+detalhe+"&pastachave="+pastachave);
  http.open("GET", pagina, true);
  http.onreadystatechange = resposta_QuadroFTP;
  http.send(null);
}

function resposta_QuadroFTP()
{
  if (http.readyState == 4) {
	
	  if (http.status == 200) {
			var Retorno = http.responseText.split("|");
			var div = document.getElementById("retorno_upload");
				div.innerHTML = Retorno[0];
				esconde_div("esperaftp");	
				
	  }
	  else	
	  {
		var div = document.getElementById("retorno_upload");
			div.innerHTML = "<div id='msg'>Houve um erro na solicitaçao. Tente mais tarde.</div>";
			esconde_div("esperaftp");
	  }
  }
}

function retorno_paginacaoAjax()
{
  if (http.readyState == 4) {
	
	  if (http.status == 200) {
			var Retorno = http.responseText.split("|");
			var div = document.getElementById("conteiner_ajax");
				div.innerHTML = Retorno[0];
				esconde_div("espera");
	  }
	  else	
	  {
		var div = document.getElementById("conteiner_ajax");
			div.innerHTML = "<div id='msg5'>Temporariamente Indisponivel. Volte mais tarde.</div>";
			esconde_div("espera");
	  }
  }
}

function retorno_divAjax()
{
  if (http.readyState == 4) {
	
	  if (http.status == 200) {
			var Retorno = http.responseText.split("|");
			var div = document.getElementById("div_ajax");
				div.innerHTML = Retorno[0];
				esconde_div("espera");
	  }
	  else	
	  {
		var div = document.getElementById("div_ajax");
			div.innerHTML = "<div id='msg5'>Temporariamente Indisponivel. Volte mais tarde.</div>";
			esconde_div("espera");
	  }
  }
}





function getHTTPObject() {
var req;

try {
 if (window.XMLHttpRequest) {
  req = new XMLHttpRequest();

  if (req.readyState == null) {
   req.readyState = 1;
   req.addEventListener("load", function () {
   req.readyState = 4;

   if (typeof req.onReadyStateChange == "function")
    req.onReadyStateChange();
   }, false);
  }

  return req;
 }

 if (window.ActiveXObject) {
  var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];

  for (var i = 0; i < prefixes.length; i++) {
   try {
    req = new ActiveXObject(prefixes[i] + ".XmlHttp");
    return req;
   } catch (ex) {};
  }
 }
} catch (ex) {}

alert("XmlHttp Objects not supported by client browser");
}
var http = getHTTPObject();





function MensagemFinal(qualmensagem,qualdiv)
{
		div = document.getElementById(qualdiv);
        div.innerHTML = qualmensagem;
}


function mostra_div(id) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';  
		}  
		else {    
			if (document.layers) { // Netscape 4      
			document.id.display = 'block';
			}    
			else { // IE 4      
				 document.all.id.style.display = 'block';    
				 }  
		}
	}
	
function esconde_div(id) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
		}
		else {
			if (document.layers) { // Netscape 4
			document.id.display = 'none';    
			}    
			else { // IE 4      
			document.all.id.style.display = 'none';    
			}  
		}
	}
	
	
function alterna_campo(val) {
	if (val == 0) { id1 = 'campocnpj'; id2 = 'campocpf'; }
	if (val == 1) { id1 = 'campocpf'; id2 = 'campocnpj'; }
	//esconde 1
	document.tipo.doc.value ="";
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id1).style.display = 'none';
		}
		else {
			if (document.layers) { // Netscape 4
			document.id1.display = 'none';    
			}    
			else { // IE 4      
			document.all.id1.style.display = 'none';    
			}  
		}
	//mostra 2
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id2).style.display = 'block';  
		}  
		else {    
			if (document.layers) { // Netscape 4      
			document.id2.display = 'block';
			}    
			else { // IE 4      
				 document.all.id2.style.display = 'block';    
				 }  
		}
	
	}	
	

function salvaContatoPed(dia,tipo,nome,razao,cnpj,cpf,contato,end,num,bairro,comp,cep,cidade,uf,tel1,tel2,mail1,mail2,elog,epas,dominio,plano,sitreg)
{
	//for (i=0; i<selectstatus.length; i++)
	//	if (selectstatus[i].selected) stpagto = selectstatus[i].value;
  mostra_div("espera");
  http.open("GET", "salvar_contato_form.php?dia="+dia+"&tipo="+tipo+"&nome="+nome+"&razao="+razao+"&cnpj="+cnpj+"&cpf="+cpf+"&contato="+contato+"&end="+end+"&num="+num+"&bairro="+bairro+"&comp="+comp+"&cep="+cep+"&cidade="+cidade+"&uf="+uf+"&tel1="+tel1+"&tel2="+tel2+"&mail1="+mail1+"&mail2="+mail2+"&elog="+elog+"&epas="+epas+"&dominio="+dominio+"&plano="+plano+"&sitreg="+sitreg, true);
  http.onreadystatechange = retorno_divAjax;
  http.send(null);
}

	
function ValidaSubmeteContato(tipo,acao,dominio,plano,sitreg){
	
           d = document.formContato;
		   
		  if (d.nome.value == ""){
                   alert("Preencha o nome do contato");
                     d.nome.focus();
                     return false;
		  }
		  
		  if (d.razao.value == "" && tipo == 1){
                   alert("Preencha a Razao Social");
                     d.razao.focus();
                     return false;
		  }
		  
		  if (d.cnpj.value == "" && tipo == 1){
                   alert("Preencha o CNPJ");
                     d.cnpj.focus();
                     return false;
		  }
		  
		  if (d.contato.value == "" && tipo == 1){
                   alert("Preencha o Contato");
                     d.contato.focus();
                     return false;
		  }
		  
		  if (d.cpf.value == "" && tipo ==0){
                   alert("Preencha o CPF");
                     d.cpf.focus();
                     return false;
		  }
		  
		  if (d.endereco.value == ""){
                   alert("Preencha o endereco");
                     d.endereco.focus();
                     return false;
		  }
		  
		  if (d.num.value == ""){
                   alert("Preencha o numero do endereco");
                     d.num.focus();
                     return false;
		  }
		  
		  if (d.bairro.value == ""){
                   alert("Preencha o Bairro");
                     d.bairro.focus();
                     return false;
		  }
		  
		  if (d.cep.value == ""){
                   alert("Preencha o CEP");
                     d.cep.focus();
                     return false;
		  }
		  
		  if (d.cidade.value == ""){
                   alert("Preencha a Cidade");
                     d.cidade.focus();
                     return false;
		  }
		  
		  if (d.uf.value == ""){
                   alert("Preencha o Estado (UF) ");
                     d.uf.focus();
                     return false;
		  }
		  
		  if (d.tel1.value == ""){
                   alert("O campo telefone1 deve ser preenchido!");
                     d.tel1.focus();
                     return false;
		  }
		  
		  if (d.mail.value == ""){
                   alert("Preencha o Email de Cobranca");
                     d.mail.focus();
                     return false;
		  }
		  
		  if (d.elog.value == ""){
                   alert("Preencha o LOGIN do contato");
                     d.elog.focus();
                     return false;
		  }
		  
		  if (d.epas.value == ""){
                   alert("Preencha a SENHA do contato");
                     d.epas.focus();
                     return false;
		  }
		  
		  if (acao == "editar") {
	       		// editaContato(tipo,d.nome.value,d.razao.value,d.cnpj.value,d.cpf.value,d.contato.value,d.endereco.value,d.num.value,d.bairro.value,d.comp.value,d.cep.value,d.cidade.value,d.uf.value,d.tel1.value,d.tel2.value,d.mail.value,d.mail2.value,d.elog.value,d.epas.value,dominio,plano);
		  }
		  if (acao == "salvar") {
	       		 salvaContatoPed(d.diav.value,tipo,d.nome.value,d.razao.value,d.cnpj.value,d.cpf.value,d.contato.value,d.endereco.value,d.num.value,d.bairro.value,d.comp.value,d.cep.value,d.cidade.value,d.uf.value,d.tel1.value,d.tel2.value,d.mail.value,d.mail2.value,d.elog.value,d.epas.value,dominio,plano,sitreg);
		  }
		  
				return true;
 }	
 
  function validaFormFTP(){
           
		  d = document.formftp;

         //validar setor
         if (d.setor.value == ""){
                   alert("Selecione um setor.");
                   d.setor.focus();
                   return false;
         }
		 //validar detalhe
         if (d.detalhe.value == ""){
                   alert("Informe alguma observacao sobre esse pedido.");
                   d.detalhe.focus();
                   return false;
         }
			 
		enviaFTP(d.setor.value, d.detalhe.value, d.pastachave.value, '', 0);
		return true;
 }	
