/**
	* Encuestas AJAX
	*
	* Funciones de JavaScript necesarias para el funcionamiento del
	* contacta en AJAX
	*
	* @author José Manuel Pérez-Montes Regajo
	* @author http://www.aupex.org
	* @date 2008/04/09
*/

function contacta(url)
{	
	http = getXmlHttpObject();
	
	http.open("GET", url, true);
		
	http.onreadystatechange = function() 
	{
		if(http.readyState == 1 || http.readyState == 2 || http.readyState == 3) 
		{
			document.getElementById('carga_general').style.visibility = 'visible';
		}
		if(http.readyState == 4 && http.status == 200) 
		{
			document.getElementById('carga_general').style.visibility = 'hidden';
			document.getElementById('central').innerHTML = http.responseText;
		}
	}
	//http.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
	http.send(null);
}

/**
	* mail
	* 
	* Función que dado un correo, comprueba si es válido o no
	*
	* @para texto es el correo electrónico que se va a validar
	* @return valor true o false en función de si el correo es válido o no
*/
function mail(texto)
{
	var mailres = true;
	var cadena = "abcdefghijklmnñopqrstuvwxyzABCDEFGHIJKLMNÑOPQRSTUVWXYZ1234567890@._-";
	var arroba = texto.indexOf("@",0);

	if ( (texto.lastIndexOf("@")) != arroba) 
	{
		arroba = -1;
	}

	var punto = texto.lastIndexOf(".");

	for (var contador = 0 ; contador < texto.length ; contador++)
	{
		if (cadena.indexOf(texto.substr(contador, 1),0) == -1)
		{
			mailres = false;
			break;
		}
	}

	if ((arroba > 1) && (arroba + 1 < punto) && (punto + 1 < (texto.length)) && (mailres == true) && (texto.indexOf("..",0) == -1))
	{
		mailres = true;
	}
	else
	{
		mailres = false;
	}
	return mailres;
}


function enviar_formulario(url)
{
	var OK = 1;
	
	if ((document.getElementById('nombre').value == ""))
	{
		OK = 0;
		document.getElementById('nombre').focus();
		alert("Debe escribir su nombre");
	}
	
	if ((document.getElementById('apellidos').value == "") && (OK==1))
	{
		OK = 0;
		document.getElementById('apellidos').focus();
		alert("Debe escribir sus apellidos");
	}
	
	if ((document.getElementById('correo').value == "") && (OK==1))
	{
		OK = 0;
		document.getElementById('apellidos').focus();
		alert("Debe escribir su correo electrónico");
	}
	
	if ( (mail(document.getElementById('correo').value) == false) && (OK==1) )
	{
		OK = 0;
		document.getElementById('correo').focus();
		alert("El correo electrónico no es válido. Escríbalo de nuevo");
	}
	
	if ((document.getElementById('area').value == "") && (OK==1))
	{
		OK = 0;
		document.getElementById('area').focus();
		alert("Debe seleccionar un área");
	}
	
	if ((document.getElementById('comentario').value == "") && (OK==1))
	{
		OK = 0;
		document.getElementById('comentario').focus();
		alert("Debe escribir un comentario");
	}
	
	if ((document.getElementById('codigo').value == "") && (OK==1))
	{
		OK = 0;
		document.getElementById('codigo').focus();
		alert("Debe insertar el código que aparece en la imagen");
	}
	
	
	if (OK)
	{
		p1 = 'nombre=' + document.getElementById('nombre').value + '&';
		p2 = 'apellidos=' + document.getElementById('apellidos').value + '&';
		p3 = 'correo=' + document.getElementById('correo').value + '&';
		p4 = 'area=' + document.getElementById('area').value + '&';
		p5 = 'codigo=' + document.getElementById('codigo').value + '&';
		p6 = 'comentario=' + document.getElementById('comentario').value + '&';
		p7 = 'up=' + document.getElementById('up').value + '&';
		
		
		http = getXmlHttpObject();
		
		http.open("POST", url, true);
			
		http.onreadystatechange = function() 
		{
			if(http.readyState == 1 || http.readyState == 2 || http.readyState == 3) 
			{
				document.getElementById('loader_acceso').style.visibility = 'visible';
			}
			if(http.readyState == 4 && http.status == 200) 
			{
				document.getElementById('loader_acceso').style.visibility = 'hidden';
				document.getElementById('central').innerHTML = http.responseText;
				scroll(0,0);
			}
		}
		http.setRequestHeader('Content-Type','application/x-www-form-urlencoded'); 
		http.send(p1+p2+p3+p4+p5+p6+p7);
	}
}


