function nuevoAjax(){
var xmlhttp=false;
 try {
  xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
  try {
   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
  } catch (E) {
   xmlhttp = false;
  }
 }

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
  xmlhttp = new XMLHttpRequest();
}
return xmlhttp;
}
//-----------------------------------------------------------
function enviarMsg(){

var t1, t2, t3, contenedor;
var errores = '';
var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;

contenedor = document.getElementById('contenedor');
t1 = document.getElementById('nombre').value;
t2 = document.getElementById('email').value;
t3 = RemoveBad(document.getElementById('mensaje').value.replace(/\n/g, "<br />"));

//Controles de campos
if(t1=='Su Nombre:'||t1.length==0){
	errores = errores + '- Debe ingresar su nombre\n';
}
if(t2.length==0){
	errores = errores + '- Debe indicar su EMail\n';
}else{
	if(!filter.test(t2)){
		errores = errores + '- Su EMail es erroneo\n';
	}
}
if(t3.length==0){
	errores = errores + '- Debe ingresar su consulta\n';
}

if(errores!=''){
	alert('Se han encontrado los siguientes errores: \n\n' + errores);
	return;
}	

ajax=nuevoAjax();
ajax.open("GET", "mensaje.php?n="+t1+"&e="+t2+"&m="+t3,true);
ajax.onreadystatechange=function() {
if (ajax.readyState==1){
document.getElementById('contenedor').innerHTML = '<img src="images/mail-loader.gif" align="center">';
}	
if (ajax.readyState==4) {
contenedor.innerHTML = ajax.responseText
}
}
ajax.send(null)
}

//-----------------------------------------------------------
function submitLogin(){
	var errores = '';
	if(document.getElementById("login_user").value.length <= 4||document.getElementById("login_user").value=='Su Usuario:'){
		errores = errores + '- Debe ingresar un usuario valido\n';
	}
	if(document.getElementById("login_pass").value.length <= 4){
		errores = errores + '- Debe ingresar una contraseņa valida\n';
	}
	if(errores!=''){
		alert('Se han encontrado los siguientes errores: \n\n' + errores);
	} else {
		cargarContenido();
	}
}

function cargarContenido(){
var t1, t2, contenedor;
contenedor = document.getElementById('contenedor');
t1 = document.getElementById('login_user').value;
t2 = document.getElementById('login_pass').value;
ajax=nuevoAjax();
ajax.open("GET", "validloginajax.php?user="+t1+"&pass="+t2,true);
ajax.onreadystatechange=function() {
if (ajax.readyState==1){
document.getElementById('contenedor').innerHTML = '<img src="images/login-loader.gif" align="center">';
}
if (ajax.readyState==4) {
contenedor.innerHTML = ajax.responseText
}
}
ajax.send(null)
}

function submitLogout(){
ajax=nuevoAjax();
ajax.open("GET", "logout.php",true);
ajax.onreadystatechange=function() {
if (ajax.readyState==1){
document.getElementById('contenedor').innerHTML = '<img src="images/login-loader.gif" align="center">';
}	
if (ajax.readyState==4) {
contenedor.innerHTML = ajax.responseText
}
}
ajax.send(null)
}

//-----------------------------------------------------------
function altaIncidente(){
var errores = '';
var t1, t2, t3, contenedor;
contenedor = document.getElementById('contenedor');
t1 = document.getElementById('critico').value;
t2 = document.getElementById('descripcion').value;
t3 = document.getElementById('comentarios').value.replace(/\n/g, "<br />");
//Validaciones.
if(t2.length==0){
	errores = errores + '- Debe ingresar una descripcion, sino como llamamos a esto ?\n'; 	
}
if(t3.length==0){
	errores = errores + '- Debe ingresar un comentario. Por favor, no sea escueto.\n'; 	
}
if(errores!=''){
	alert(errores);
	return;
}

ajax=nuevoAjax();
ajax.open("GET", "altainc.php?critico="+t1+"&descripcion="+t2+"&comentarios="+t3,true);
ajax.onreadystatechange=function() {
if (ajax.readyState==1){
document.getElementById('contenedor').innerHTML = '<img src="images/mail-loader.gif" align="center">';
}
if (ajax.readyState==4) {
contenedor.innerHTML = ajax.responseText
}
}
ajax.send(null)
}

//-----------------------------------------------------------
function modiIncidente(id){

var t1, t2, contenedor;
contenedor = document.getElementById('contModif');
t1 = document.getElementById('estado').value;
t2 = document.getElementById('horas').value;

//Validaciones.
if(t2.length==0){
	t2 = '0'; 	
}

ajax=nuevoAjax();
ajax.open("GET", "modiinci.php?id="+id+"&estado="+t1+"&horas="+t2,true);
ajax.onreadystatechange=function() {
if (ajax.readyState==1){
	contenedor.innerHTML = '<img src="images/mail-loader.gif" align="center">';
}
if (ajax.readyState==4) {
	contenedor.innerHTML = ajax.responseText
}
}
ajax.send(null)
}

//-----------------------------------------------------------
function altaProyecto(){
var errores = '';
var t1, t2, t3, t4, contenedor;
contenedor = document.getElementById('contenedor');
t1 = document.getElementById('adjunto').value;
t2 = document.getElementById('descripcion').value;
t3 = document.getElementById('comentarios').value.replace(/\n/g, "<br />");
t4 = document.getElementById('fechaesti').value;
//Validaciones.
if(t2.length==0){
	errores = errores + '- Debe ingresar una descripcion, sino como llamamos a esto ?\n'; 	
}
if(t3.length==0){
	errores = errores + '- Debe ingresar un comentario. Por favor, no sea escueto.\n'; 	
}
if(errores!=''){
	alert(errores);
	return;
}

ajax=nuevoAjax();
ajax.open("GET", "altaproy.php?adjunto="+t1+"&descripcion="+t2+"&comentarios="+t3+"&fechaesti="+t4,true);
ajax.onreadystatechange=function() {
if (ajax.readyState==1){
document.getElementById('contenedor').innerHTML = '<img src="images/mail-loader.gif" align="center">';
}
if (ajax.readyState==4) {
contenedor.innerHTML = ajax.responseText
}
}
ajax.send(null)
}

//-----------------------------------------------------------
function modiProyecto(id){

var t1, t2, t3, contenedor;
contenedor = document.getElementById('contModif');
t1 = document.getElementById('estado').value;
t2 = document.getElementById('poravance').value;
t3 = document.getElementById('fechaesti').value;
//Validaciones.
if(t2.length==0){
	t2 = '0'; 	
}

ajax=nuevoAjax();
ajax.open("GET", "modiproy.php?id="+id+"&estado="+t1+"&poravance="+t2+"&fechaesti="+t3,true);
ajax.onreadystatechange=function() {
if (ajax.readyState==1){
	contenedor.innerHTML = '<img src="images/mail-loader.gif" align="center">';
}
if (ajax.readyState==4) {
	contenedor.innerHTML = ajax.responseText
}
}
ajax.send(null)
}

//-----------------------------------------------------------
function listarIncidente(){
var t1, t2, t3, contenedor;
contenedor = document.getElementById('contenedor');

if(document.getElementById('miuser').checked){
	t1 = document.getElementById('miuser').value;	
}else{ t1 = '';
}
if(document.getElementById('pendientes').checked){
	t2 = document.getElementById('pendientes').value;	
}else{ t2 = '';
}
t3 = document.getElementById('orden').value;

ajax=nuevoAjax();
ajax.open("GET", "listarincidentes.php?miuser="+t1+"&pendientes="+t2+"&orden="+t3,true);
ajax.onreadystatechange=function() {
if (ajax.readyState==1){
document.getElementById('contenedor').innerHTML = '<img src="images/mail-loader.gif" align="center">';
}
if (ajax.readyState==4) {
contenedor.innerHTML = ajax.responseText
}
}
ajax.send(null)
}

//-----------------------------------------------------------
function listarProyecto(){
var t1, t2, t3, contenedor;
contenedor = document.getElementById('contenedor');

if(document.getElementById('miuser').checked){
	t1 = document.getElementById('miuser').value;	
}else{ t1 = '';
}
if(document.getElementById('pendientes').checked){
	t2 = document.getElementById('pendientes').value;	
}else{ t2 = '';
}
t3 = document.getElementById('orden').value;

ajax=nuevoAjax();
ajax.open("GET", "listarproyectos.php?miuser="+t1+"&pendientes="+t2+"&orden="+t3,true);
ajax.onreadystatechange=function() {
if (ajax.readyState==1){
document.getElementById('contenedor').innerHTML = '<img src="images/mail-loader.gif" align="center">';
}
if (ajax.readyState==4) {
contenedor.innerHTML = ajax.responseText
}
}
ajax.send(null)
}
//-----------------------------------------------------------
function agregarComment(tipo, id){
var t1, contenedor;
contenedor = document.getElementById('contenedor');
t1 = document.getElementById('comentarios').value.replace(/\n/g, "<br />");
t1 = t1.replace(/insert into/gi,"insert_into");

if(t1.length==0){
	alert('Debe ingresar un comentario.\nHable ahora o calle para siempre.');
	return 0;
}

ajax=nuevoAjax();
ajax.open("GET", "altacom.php?tipo="+tipo+"&id="+id+"&comentarios="+t1,true);
ajax.onreadystatechange=function() {
if (ajax.readyState==1){
document.getElementById('contenedor').innerHTML = '<img src="images/mail-loader.gif" align="center">';
}
if (ajax.readyState==4) {
contenedor.innerHTML = ajax.responseText
}
}
ajax.send(null)
}

//-----------------------------------------------------------
/* Controla las extensiones de Archivo que se intentan subir al servidor */
extArray = new Array(".doc", ".pdf", ".txt", ".rtf", ".xls", ".zip", ".rar");
function LimitAttach(form, file) {
allowSubmit = false;
if (!file) return "";
while (file.indexOf("\\") != -1)
file = file.slice(file.indexOf("\\") + 1);
ext = file.slice(file.indexOf(".")).toLowerCase();
for (var i = 0; i < extArray.length; i++) {
if (extArray[i] == ext) { allowSubmit = true; break; }
}
if (allowSubmit) return "";
else return "Error en el tipo de archivo a publicar,\nsolo se permiten con extension: " + (extArray.join("  ")); 
}

function subirAdjunto(){
var mensaje = LimitAttach(document.forms[1], document.forms[1].filefieldname.value);
if(mensaje!=""){
	errores = mensaje + '\n';
	alert(errores);
	document.forms[1].filefieldname.value='';
	return "";
}else{	
	//document.getElementById('btnsubmit').disabled = true;
	document.uploadform.submit();
	parent.document.getElementById('uploadedfile').innerHTML = '<p id="uploadedfile"><img src="images/mail-loader.gif" align="center"><br>Subiendo Adjunto, por favor, espere a que culmine.</p>';
}
}

function RemoveBad(strTemp) { strTemp = strTemp.replace(/\"|\'|\;|\(|\)|\&|\+/g,""); return strTemp; }

//-----------------------------------------------------------
//Funcion que sirve para crear una ventana popup 
function OpenWindow(page, nombre) 
{
   var windowName=nombre;
   var windowPage=page;
   var features =  'width='        + 700 +
                   ',height='      + 500 +
                   ',directories=' + 0 +
                   ',location='    + 0 +
                   ',menubar='     + 0 +
                   ',scrollbars='  + 1 +
                   ',status='      + 0 +
                   ',toolbar='     + 0 +
                   ',screenX='     + 150 +
                   ',screenY='     + 150 +
                   ',top='         + 200 +
                   ',left='        + 300 +
                   ',resizable='   + 0;
              
   //Abre el popup con los parametros de la var features
   window.open (windowPage, windowName, features);
}