var xmlHttpReq = false;

function GetXmlHttpObject() {
  var xmlHttpReq = false;
  var xmlHttpReq = null;
  try {
    // Firefox, Opera 8.0+, Safari
    xmlHttpReq = new XMLHttpRequest();
  } catch (e) {
    // Internet Explorer
    try {
      xmlHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
  return xmlHttpReq;
}

function veja_usuario(){
   xmlHttpReq = GetXmlHttpObject();
   if (xmlHttpReq == null) {
      alert ("Seu Browser não suporta AJAX!");
      return;
   }
   if (document.frm.login.value==""){
      return;
   }
   if (document.frm.senha.value==""){
      return;
   }
   xmlHttpReq.open('POST', 'veja_acesso.php', true);
   xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   xmlHttpReq.onreadystatechange = function() {
   if (xmlHttpReq.readyState == 4) {
      tmx = xmlHttpReq.responseText;
      if (tmx.indexOf("!!!")>0){
         alert(tmx);
         document.frm.senha.value="";
         document.frm.senha.focus();
      }else{
         document.getElementById("main").innerHTML = tmx;
      }
     }
   }
   xmlHttpReq.send("login="+document.frm.login.value+"&senha="+document.frm.senha.value);
}

function sair(){
   xmlHttpReq = GetXmlHttpObject();
   if (xmlHttpReq == null) {
      alert ("Seu Browser não suporta AJAX!");
      return;
   }
   xmlHttpReq.open('POST', 'sair.php', true);
   xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   xmlHttpReq.onreadystatechange = function() {
   if (xmlHttpReq.readyState == 4) {
      document.getElementById("main").innerHTML = xmlHttpReq.responseText;
      setTimeout("location.href='admin.php';",500);
      }
   }
   xmlHttpReq.send();
}


function trajetoria(opc){
   xmlHttpReq = GetXmlHttpObject();
   if (xmlHttpReq == null) {
      alert ("Seu Browser não suporta AJAX!");
     return;
   }
   xmlHttpReq.open('POST', 'screen_admin.php', true);
   xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   xmlHttpReq.onreadystatechange = function() {
   if (xmlHttpReq.readyState == 4) {
      document.getElementById("main").innerHTML = xmlHttpReq.responseText;
      }
   }
   xmlHttpReq.send("ifrm="+opc);
}

function show_trajetoria(){
   xmlHttpReq = GetXmlHttpObject();
   if (xmlHttpReq == null) {
      alert ("Seu Browser não suporta AJAX!");
     return;
   }
   xmlHttpReq.open('POST', 'trajetoria.php', true);
   xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   xmlHttpReq.onreadystatechange = function() {
   if (xmlHttpReq.readyState == 4) {
      document.getElementById("conteudo").innerHTML = xmlHttpReq.responseText;
      }
   }
   xmlHttpReq.send();
}

function modar_logo(x){
  if (x==1){
     document.getElementById("logo").src = "images/cf1410.jpg";
  }else{
     document.getElementById("logo").src = "images/partido.gif";
  }
}

function contato(){
   xmlHttpReq = GetXmlHttpObject();
   if (xmlHttpReq == null) {
      alert ("Seu Browser não suporta AJAX!");
     return;
   }
   xmlHttpReq.open('POST', 'listar_contato.php', true);
   xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   xmlHttpReq.onreadystatechange = function() {
   if (xmlHttpReq.readyState == 4) {
      document.getElementById("conteudo").innerHTML = xmlHttpReq.responseText;
      document.getElementById("ifrm").innerHTML = "";
      }
   }
   xmlHttpReq.send();
}

function editar_contato(id){
   xmlHttpReq = GetXmlHttpObject();

   if (xmlHttpReq == null) {
      alert ("Seu Browser não suporta AJAX!");
      return;
   }

   xmlHttpReq.open('POST', 'responda_contato.php', true);
   xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   xmlHttpReq.onreadystatechange = function() {
   if (xmlHttpReq.readyState == 4) {
      myWindow = window.open("", "SAMU","top=130,left=90,width=888,height=300");
      myWindow.scrollbars = true;
      myWindow.document.write(xmlHttpReq.responseText);
      myWindow.document.close();
     }
   }
   xmlHttpReq.send("id="+id);
}

function apagar_contato(id,nome){
   xmlHttpReq = GetXmlHttpObject();
   if (xmlHttpReq == null) {
      alert ("Seu Browser não suporta AJAX!");
      return;
   }
   if(!confirm("CONFIRMA EXCLUSÃO DO CONTATO ( " + nome +" ) ?")){
      return
   }
   xmlHttpReq.open('POST', 'listar_contato.php', true);
   xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   xmlHttpReq.onreadystatechange = function() {
   if (xmlHttpReq.readyState == 4) {
      document.getElementById("conteudo").innerHTML = xmlHttpReq.responseText;
      document.getElementById("ifrm").innerHTML = "";
      }
   }
   xmlHttpReq.send("id="+id+"&ac=0");
}

function agenda(){
   xmlHttpReq = GetXmlHttpObject();
   if (xmlHttpReq == null) {
      alert ("Seu Browser não suporta AJAX!");
     return;
   }
   xmlHttpReq.open('POST', 'agenda.php', true);
   xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   xmlHttpReq.onreadystatechange = function() {
   if (xmlHttpReq.readyState == 4) {
      document.getElementById("conteudo").innerHTML = xmlHttpReq.responseText;
      document.getElementById("ifrm").innerHTML = "";
      }
   }
   xmlHttpReq.send();
}

function gravar_agenda(){
  xmlHttpReq = GetXmlHttpObject();
  if (xmlHttpReq == null) {
     alert ("Seu Browser não suporta AJAX!");
     return;
  }
  
  if (document.cad.acao.type=="hidden"){
     acao = document.getElementById("acao").value;
  }else{
     acao = 0;
     for(var i = 0; i < document.cad.acao.length; i++) {
  	   if(document.cad.acao[i].checked) {
	  	 acao = document.cad.acao[i].value;
 	   }
     }
  }

  param = "acao="+acao+"&data="+document.cad.data.value+
          "&hora="+document.cad.hora.value+"&assunto="+document.cad.assunto.value+
          "&atividade="+document.cad.atividade.value+"&id="+document.cad.id.value;
          
  xmlHttpReq.open('POST', 'agenda.php', true);
  xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlHttpReq.onreadystatechange = function() {
  if (xmlHttpReq.readyState == 4) {
     document.getElementById("conteudo").innerHTML = xmlHttpReq.responseText;
     }
  }
  xmlHttpReq.send(param);
}

function editar_agenda(id){
  xmlHttpReq = GetXmlHttpObject();
  if (xmlHttpReq == null) {
     alert ("Seu Browser não suporta AJAX!");
     return;
  }
  xmlHttpReq.open('POST', 'agenda.php', true);
  xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlHttpReq.onreadystatechange = function() {
  if (xmlHttpReq.readyState == 4) {
     document.getElementById("conteudo").innerHTML = xmlHttpReq.responseText;
     }
  }
  xmlHttpReq.send("id="+id+"&acao=4");
}

function candidato(){
   xmlHttpReq = GetXmlHttpObject();
   if (xmlHttpReq == null) {
      alert ("Seu Browser não suporta AJAX!");
     return;
   }
   xmlHttpReq.open('POST', 'candidato.php', true);
   xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   xmlHttpReq.onreadystatechange = function() {
   if (xmlHttpReq.readyState == 4) {
      document.getElementById("conteudo").innerHTML = xmlHttpReq.responseText;
      document.getElementById("ifrm").innerHTML = "";
      }
   }
   xmlHttpReq.send();
}

function gravar_candidato(){
  xmlHttpReq = GetXmlHttpObject();
  if (xmlHttpReq == null) {
     alert ("Seu Browser não suporta AJAX!");
     return;
  }

  if (document.cad.acao.type=="hidden"){
     acao = document.getElementById("acao").value;
  }else{
     acao = 0;
     for(var i = 0; i < document.cad.acao.length; i++) {
  	   if(document.cad.acao[i].checked) {
	  	 acao = document.cad.acao[i].value;
 	   }
     }
  }

  param = "acao="+acao+"&numero="+document.cad.numero.value+
          "&cargo="+document.cad.cargo.value+"&nome="+document.cad.nome.value+
          "&uf="+document.cad.uf.value+"&url="+document.cad.url.value+
          "&id="+document.cad.id.value;

  xmlHttpReq.open('POST', 'candidato.php', true);
  xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlHttpReq.onreadystatechange = function() {
  if (xmlHttpReq.readyState == 4) {
     document.getElementById("conteudo").innerHTML = xmlHttpReq.responseText;
     }
  }
  xmlHttpReq.send(param);
}

function editar_candidato(id){
  xmlHttpReq = GetXmlHttpObject();
  if (xmlHttpReq == null) {
     alert ("Seu Browser não suporta AJAX!");
     return;
  }
  xmlHttpReq.open('POST', 'candidato.php', true);
  xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  xmlHttpReq.onreadystatechange = function() {
  if (xmlHttpReq.readyState == 4) {
     document.getElementById("conteudo").innerHTML = xmlHttpReq.responseText;
     }
  }
  xmlHttpReq.send("id="+id+"&acao=4");
}

function validaData(campo,valor) {
	var date=valor;
	var ardt=new Array;
	var ExpReg=new RegExp("(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[012])/[12][0-9]{3}");
	if (campo.value==""){
       return true;
    }
	ardt=date.split("/");
	erro=false;
	if ( date.search(ExpReg)==-1){
		erro = true;
		}
	else if (((ardt[1]==4)||(ardt[1]==6)||(ardt[1]==9)||(ardt[1]==11))&&(ardt[0]>30))
		erro = true;
	else if ( ardt[1]==2) {
		if ((ardt[0]>28)&&((ardt[2]%4)!=0))
			erro = true;
		if ((ardt[0]>29)&&((ardt[2]%4)==0))
			erro = true;
	}
	if (erro) {
		alert("\"" + valor + "\" não é uma data válida!!!");
		campo.focus();
		campo.value = "";
		return false;
	}
	return true;
}

// Exemplo: onKeyPress="Mascara('CEP',this,event);"
function Mascara(tipo, campo, teclaPress) {
	if (window.event)
	{
		var tecla = teclaPress.keyCode;
	} else {
		tecla = teclaPress.which;
	}

	var s = new String(campo.value);
	// Remove todos os caracteres à seguir: ( ) / - . e espaço, para tratar a string denovo.
	s = s.replace(/(\.|\(|\)|\/|\-| )+/g,'');

	tam = s.length + 1;

	if ( tecla != 9 && tecla != 8 ) {
		switch (tipo)
		{
		case 'CPF' :
			if (tam > 3 && tam < 7)
				campo.value = s.substr(0,3) + '.' + s.substr(3, tam);
			if (tam >= 7 && tam < 10)
				campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,tam-6);
			if (tam >= 10 && tam < 12)
				campo.value = s.substr(0,3) + '.' + s.substr(3,3) + '.' + s.substr(6,3) + '-' + s.substr(9,tam-9);
		break;

		case 'CNPJ' :

			if (tam > 2 && tam < 6)
				campo.value = s.substr(0,2) + '.' + s.substr(2, tam);
			if (tam >= 6 && tam < 9)
				campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,tam-5);
			if (tam >= 9 && tam < 13)
				campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,tam-8);
			if (tam >= 13 && tam < 15)
				campo.value = s.substr(0,2) + '.' + s.substr(2,3) + '.' + s.substr(5,3) + '/' + s.substr(8,4)+ '-' + s.substr(12,tam-12);
		break;

		case 'TEL' :
			if (tam > 2 && tam < 4)
				campo.value = '(' + s.substr(0,2) + ') ' + s.substr(2,tam);
			if (tam >= 7 && tam < 11)
				campo.value = '(' + s.substr(0,2) + ') ' + s.substr(2,4) + '-' + s.substr(6,tam-6);
		break;

		case 'DATA' :
			if (tam > 2 && tam < 4)
				campo.value = s.substr(0,2) + '/' + s.substr(2, tam);
			if (tam > 4 && tam < 11)
				campo.value = s.substr(0,2) + '/' + s.substr(2,2) + '/' + s.substr(4,tam-4);
		break;

		case 'HORA' :
			if (tam > 2 && tam < 4)
				campo.value = s.substr(0,2) + ':' + s.substr(2, tam);
			if (tam > 3 && tam < 5)
				campo.value = s.substr(0,2) + ':' + s.substr(2,2);
		break;

		case 'CEP' :
			if (tam > 5 && tam < 7)
				campo.value = s.substr(0,5) + '-' + s.substr(6, tam);
			if (tam > 7 && tam < 9)
				campo.value = s.substr(0,5) + '-' + s.substr(5,tam-3);
		break;
		}
	}
}

// Exemplo: OnKeyUp="Mascara_Hora(this.value, this)"
 function Mascara_Hora(Hora, campo){
    var hora01 = '';
    hora01 = hora01 + Hora;
    if (hora01.length == 2){
       hora01 = hora01 + ':';
       campo.value = hora01;
    }
    if (hora01.length == 5){
       Verifica_Hora(campo);
    }
 }

 function Verifica_Hora(campo){
    hrs = (campo.value.substring(0,2));
    min = (campo.value.substring(3,5));
    estado = "";
    if ((hrs < 00 ) || (hrs > 23) || ( min < 00) ||( min > 59)){
       estado = "errada";
    }

    if (campo.value == "") {
       estado = "errada";
    }
    if (estado == "errada") {
       alert("Hora invalida!");
       campo.value="";
       campo.focus();
    }
 }

