// JavaScript Document

/*
	Funciones AJAX
*/

function f_ajax_contacta_a()
{
    ajax=AJAX_Crea();
    ajax.open("POST", "index.php?spider_contacta_a=1",true);
    ajax.onreadystatechange=function()
    {
		if(ajax.readyState==1)
		{
			document.getElementById('contenido_principal').innerHTML = '<img src="http://www.mrbinder.com/images/loading.gif" />';
		}
		else if (ajax.readyState==4)
        {
 			document.getElementById('contenido_principal').innerHTML = ajax.responseText;
        }
		
    }
    ajax.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
	ajax.send(null)
}

function f_ajax_carga_contenido_contacta_datos() {
	ajax=AJAX_Crea();
    ajax.open("POST", "index.php?spider_contacta_datos=1",true);
    ajax.onreadystatechange=function()
    {
		if(ajax.readyState==1)
		{
			document.getElementById('contenido_contacta').innerHTML = '<br /><br /><br /><img src="http://www.mrbinder.com/images/loading.gif" />';
		}
		else if (ajax.readyState==4)
        {
 			document.getElementById('contenido_contacta').innerHTML = ajax.responseText;
        }
		
    }
    ajax.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
	ajax.send(null)	
}

function f_ajax_carga_contenido_contacta_form() {
	ajax=AJAX_Crea();
    ajax.open("POST", "index.php?spider_contacta_form=1",true);
    ajax.onreadystatechange=function()
    {
		if(ajax.readyState==1)
		{
			document.getElementById('contenido_contacta').innerHTML = '<br /><br /><br /><img src="http://www.mrbinder.com/images/loading.gif" />';
		}
		else if (ajax.readyState==4)
        {
 			document.getElementById('contenido_contacta').innerHTML = ajax.responseText;
        }
		
    }
    ajax.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
	ajax.send(null)	
}

// envio de email

function f_ajax_contacta_envio_mail()
{
	var nombre_user = document.getElementById('nombre').value;
	var direccion = document.getElementById('email').value;
	var titulo = document.getElementById('asunto').value;
	var contenido = document.getElementById('texto').value;
    ajax=AJAX_Crea();
    ajax.open("POST", "index.php?spider_contacta_envio_mail_ok=1",true);
    ajax.onreadystatechange=function()
    {
		if(ajax.readyState==1)
		{
			document.getElementById('contenido_contacta').innerHTML = '<img src="http://www.mrbinder.com/images/loading.gif" /><p align="center"><h3>Enviado mensaje</h3></p>';
		}
		else if (ajax.readyState==4)
        {
 			document.getElementById('contenido_contacta').innerHTML = ajax.responseText;
			initLightbox();
        }
		
    }
    ajax.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
	ajax.send("&nombre_user="+nombre_user+"&direccion="+direccion+"&titulo="+titulo+"&contenido="+contenido)
}

/*
	Funciones de control
*/

function cargacontenido_contacta(tipo) {	
	if (tipo == 1) {
		document.getElementById('formulario').checked = false;
		document.getElementById('datos').checked = true;
		f_ajax_carga_contenido_contacta_datos();
	} else {
		document.getElementById('datos').checked = false;
		document.getElementById('formulario').checked = true;
		f_ajax_carga_contenido_contacta_form();
	}	
}

function borra_contenido_form_contacta() {
	enviar = window.confirm('¿ Realmente desea borrar todos los datos del formulario ?');
	if (enviar==true) {
		document.getElementById('nombre').value = '';
		document.getElementById('email').value = '';
		document.getElementById('asunto').value = '';
		document.getElementById('texto').value = '';
	}
}

function envia_contenido_form_contacta() {
	var aux1 = 	document.getElementById('nombre').value;
	var aux2 = 	document.getElementById('email').value;
	var aux3 = 	document.getElementById('asunto').value;
	var aux4 = 	document.getElementById('texto').value;
	
	if (vacio(aux1) == true) {
		if (checkemail(aux2) == true) {
			if (vacio(aux4) == true) {
				f_ajax_contacta_envio_mail();
			} else {
				alert('ERROR: Debes informarnos acerca del motivo del contacto');
			}
		} else {
			alert('ERROR: El e-mail introducido NO es correcto');
		}
	} else {
		alert('ERROR: Debes introducir un nombre de contacto para enviar el mensaje');
	}
}
