function objetoAjax(){
	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;
}

/***********/
/*  LOGIN  */
/***********/
function login(form){
	var user = form.usuario.value;
  form.contrasena.value = hex_md5(form.contrasena.value);
  var pass = form.contrasena.value;

  //donde se mostrará el resultado de la validacion
  divResultado = document.getElementById('principal');

  //instanciamos el objetoAjax
  ajax=objetoAjax();
  //uso del medotod POST
  //indicamos el archivo que realizará el proceso de validación
  //ajax.open("POST", "login.php?usuario="+user+"&contrasena="+pass);
  ajax.open("POST", "login.php");
  ajax.onreadystatechange=function() {
    if (ajax.readyState==4) {
      //mostrar resultados en esta capa
      divResultado.innerHTML = ajax.responseText
    }
  }
  //como hacemos uso del metodo POST enviamos
  ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
 	ajax.send("usuario="+user+"&contrasena="+pass);
  return
}

function logout(){
  //donde se mostrará el resultado de la validacion
  divResultado = document.getElementById('principal');

  //instanciamos el objetoAjax
  ajax=objetoAjax();
  //uso del medotod GET
  //indicamos el archivo que realizará el proceso de validación
  ajax.open("GET", "logout.php");
  ajax.onreadystatechange=function() {
    if (ajax.readyState==4) {
      //mostrar resultados en esta capa
      divResultado.innerHTML = ajax.responseText
    }
  }
  //como hacemos uso del metodo GET enviamos null
 	ajax.send(null);
  return
}

/**************/
/* DEDICACIÓN */
/**************/
function cambia_proyectos(form) {
	//instanciamos el objetoAjax
	ajax=objetoAjax();
	//uso del medotod GET
	//indicamos el archivo que se cargará
	//ajax.open("GET", "dedicacion_cambia_proyectos.php?id="+form.contrato.value);
  ajax.open("GET", "dedicacion_cambia_proyectos.php?id="+form.contrato.value+"&amp;proy="+form.hidden_proyecto.value);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			//mostrar resultados en esta capa
			document.getElementById('proyectos_select').innerHTML = ajax.responseText
		}
	}
	//como hacemos uso del metodo GET colocamos null
	ajax.send(null)
}

function editarDedicacion(id) {
	//instanciamos el objetoAjax
	ajax=objetoAjax();
	//uso del medotod GET
	//indicamos el archivo que se cargará
	ajax.open("GET", "dedicacion_formulario.php?id="+id);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			//mostrar resultados en esta capa
			document.getElementById('formulario_dedicacion').innerHTML = ajax.responseText
		}
	}
	//como hacemos uso del metodo GET colocamos null
	ajax.send(null)
}

function validar_dedicacion(form){
	var texto;
  var formatoFecha = /^\d{4}\-\d{2}\-\d{2}$/;
	texto = '';
	if (form.contrato.value == "..."){ //...
		texto += '<br />- Debe seleccionar el trabajo';
	}
  if (form.proyecto.value == '...') {
  	texto += '<br />- Debe elegir el proyecto';
  }
  if (form.tarea.value == '...') {
  	texto += '<br />- Debe indicar la tarea realizada';
  }
  if (form.fecha.value.length < 1) {
  	texto += '<br />- Ha de seleccionar la fecha';
  }
  else if (!form.fecha.value.match(eval(formatoFecha))) {
		texto += '<br />- El formato de la fecha es incorrecto (AAAA-MM-DD)';
	}
  if (form.tiempo.value.length < 1) {
  	texto += '<br />- Debe indicar el tiempo dedicado';
  }
  else {
	  if (isNaN(form.tiempo.value)){
	    texto += '<br />- El tiempo que ha indicado no es un número (debe usar el punto para indicar decimales)';
    }
  }
  
	if (texto.length > 0) {
	  var divResultado = document.getElementById('errores');
		divResultado.style.visibility = 'visible';
		divResultado.innerHTML = '<img src="images/error-trans.png" alt="Error" /> &nbsp; Se han encontrado los siguientes errores:'+texto;
	}
	else {
		form.submit();
    // llamar a otra función que llame a dedicacion_editar o dedicacion_crear
    /*
    if (form.hidden_dedicacion.value > 0) {
    	dedicacionEditar(form);
    }
    else {
    	dedicacionCrear(form);
    }
    */
	}
}

function validar_dedicacion_avanzada(form){
	var texto;
  var formatoFecha = /^\d{4}\-\d{2}\-\d{2}$/;
	texto = '';
  if (!form.fecha.value.match(eval(formatoFecha))) {
		texto += '<br />- El formato de la fecha es incorrecto (AAAA-MM-DD)';
	}
  if (form.proyecto1.value != '...') {
	  if (form.tarea1.value == '...') {
  		texto += '<br />- Debe indicar la tarea1 realizada';
  	}
    if (form.tiempo1.value.length < 1) {
    	texto += '<br />- Debe indicar el tiempo1 dedicado';
    }
    else {
  	  if (isNaN(form.tiempo1.value)){
  	    texto += '<br />- El tiempo1 que ha indicado no es un número (debe usar el punto para indicar decimales)';
      }
  	}
  }
  
  if (form.proyecto2.value != '...') {
	  if (form.tarea2.value == '...') {
  		texto += '<br />- Debe indicar la tarea2 realizada';
  	}
    if (form.tiempo2.value.length < 1) {
    	texto += '<br />- Debe indicar el tiempo2 dedicado';
    }
    else {
  	  if (isNaN(form.tiempo2.value)){
  	    texto += '<br />- El tiempo2 que ha indicado no es un número (debe usar el punto para indicar decimales)';
      }
  	}
  }
  
  if (form.proyecto3.value != '...') {
	  if (form.tarea3.value == '...') {
  		texto += '<br />- Debe indicar la tarea3 realizada';
  	}
    if (form.tiempo3.value.length < 1) {
    	texto += '<br />- Debe indicar el tiempo3 dedicado';
    }
    else {
  	  if (isNaN(form.tiempo3.value)){
  	    texto += '<br />- El tiempo3 que ha indicado no es un número (debe usar el punto para indicar decimales)';
      }
  	}
  }  
  
  if (form.proyecto4.value != '...') {
	  if (form.tarea4.value == '...') {
  		texto += '<br />- Debe indicar la tarea4 realizada';
  	}
    if (form.tiempo4.value.length < 1) {
    	texto += '<br />- Debe indicar el tiempo4 dedicado';
    }
    else {
  	  if (isNaN(form.tiempo4.value)){
  	    texto += '<br />- El tiempo4 que ha indicado no es un número (debe usar el punto para indicar decimales)';
      }
  	}
  }  
  
  if (form.proyecto5.value != '...') {
	  if (form.tarea5.value == '...') {
  		texto += '<br />- Debe indicar la tarea5 realizada';
  	}
    if (form.tiempo5.value.length < 1) {
    	texto += '<br />- Debe indicar el tiempo5 dedicado';
    }
    else {
  	  if (isNaN(form.tiempo5.value)){
  	    texto += '<br />- El tiempo5 que ha indicado no es un número (debe usar el punto para indicar decimales)';
      }
  	}
  }  
  
	if (texto.length > 0) {
	  var divResultado = document.getElementById('errores');
		divResultado.style.visibility = 'visible';
		divResultado.innerHTML = '<img src="images/error-trans.png" alt="Error" /> &nbsp; Se han encontrado los siguientes errores:'+texto;
	}
	else {
		form.submit();
  }
}

function dedicacionBorrar(form) {
	if (confirm('¿Está seguro que desea borrar este registro?')) {
  	form.action = 'dedicacion_borrar.php';
    form.submit();
  }
}

/********/
/* PASS */
/********/
function validar_pass(form){
	var texto;
	texto = '';
	if (form.actual.value.length < 1){ //...
		texto += '<br />- Debe introducir su contraseña actual';
	}
	if (form.nueva.value != form.renueva.value){ //...
		texto += '<br />- Las campos de contraseña nueva no se corresponden';
	}
  if (form.nueva.value.length < 5 || form.renueva.value.length < 5) {
		texto += '<br />- Las nueva contraseña ha de tener al menos 5 caracteres';
  }
 
	if (texto.length > 0) {
	  var divResultado = document.getElementById('errores');
		divResultado.style.visibility = 'visible';
		divResultado.innerHTML = '<img src="images/error-trans.png" alt="Error" /> &nbsp; Se han encontrado los siguientes errores:'+texto;
	}
	else {
		form.submit();
	}
}

/***********/
/* INFORME */
/***********/
function validar_informe(form){
	var texto;
  var formatoFecha = /^\d{4}\-\d{2}\-\d{2}$/;
	texto = '';
	if (form.ini.value.length < 1){ //...
		texto += '<br />- Debe introducir la fecha de inicio';
	}
  else if (!form.ini.value.match(eval(formatoFecha))) {
	    texto += '<br />- Formato incorrecto en la fecha de fin (AAAA-MM-DD)';
    }
	if (form.fin.value.length < 1){ //...
		texto += '<br />- Debe introducir la fecha de fin';
	}
  else if (!form.fin.value.match(eval(formatoFecha))) {
	    texto += '<br />- Formato incorrecto en la fecha de inicio (AAAA-MM-DD)';
    }
	if (form.ini.value.length > 0 && form.fin.value.length > 0){ //...
  	if (form.ini.value > form.fin.value){ //...
  		texto += '<br />- La fecha de inicio ha de ser menor que la de fin';
  	}
	}
 
	if (texto.length > 0) {
	  var divResultado = document.getElementById('errores');
		divResultado.style.visibility = 'visible';
		divResultado.innerHTML = '<img src="images/error-trans.png" alt="Error" /> &nbsp; Se han encontrado los siguientes errores:'+texto;
	}
	else {
		form.submit();
	}
}