
function get(id){
	var obj = document.getElementById(id);
	return obj;
}

function borra() {
	get('formu_contacto').reset();
}

var global=false;

function enviar() {
	var campos = new Array ('email');
	var nombres = new Array ('e-mail');
	for (var i=0; i<campos.length; i++) {
		if ( (get(campos[i]).value == '')||(get(campos[i]).value == ' ') ) {
			alert ('Debes rellenar todos los campos.\n"' + nombres[i] + '" no puede quedar en blanco.');
			get(campos[i]).focus();
			return false;
		}
	}
	var errores = $('#formu_contacto').find('#error');
	if(errores.length>0)
		return false;
	return true;
}



function es_email(email){
	return email.match("^([a-zA-Z0-9_\\-])+(\\.([a-zA-Z0-9_\\-])+)*@((\\[(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5])))\\.(((([0-1])?([0-9])?[0-9])|(2[0-4][0-9])|(2[0-5][0-5]))\\]))|((([a-zA-Z0-9])+(([\\-])+([a-zA-Z0-9])+)*\\.)+([a-zA-Z])+(([\\-])+([a-zA-Z0-9])+)*))$")
}

function inicio(){
	get('formu_contacto').onsubmit = enviar;
}

var http = getHTTPObject();
var nAleatorio = dameAleatorio(); 			

function dameAleatorio ()
{
	return Math.round(Math.random()*1000000000);
}

function llamadaCompruebaNoExistaMail(email)
{
	var url = "/servlet/boletin";
        
    http.open("GET", url + "?email="+email.value+"&param=" + nAleatorio, true);
	nAleatorio = dameAleatorio();  
	http.onreadystatechange = comprobarExisteMail;
	http.send("");
}
function comprobarExisteMail()
{
	
	if (http.readyState == 4)
	{
		if (http.status == 200)
		{
			var respuesta = http.responseText;
			$('#email').removeAttr('style');
			$('#error').remove();
			if (respuesta==0)
			{		
				if (!es_email(get('email').value)) {
					$('#formu_contacto').prepend('<p class="error" style="color:#FF0202" id="error">Esta direccion de email no parece válida o el campo está vacio.</p>');
					$('#email').css({ border:'1px solid #FF0202'});
					get('email').focus();
					return false;
				}else{
					return true;
				}
			}
			else
			{

				$('#formu_contacto').prepend('<p class="error" style="color:#FF0202" id="error">Esta direccion de email ya está dada de alta.</p>');				
				$('#email').css({ border:'1px solid #FF0202'});
				get('email').focus();
				return false;
			}
		}
	}
}
function mostrarClausula ()
{
	var margen = ($('body').width()-860)/2;
	$('body').append('<div id="fondo" style="z-index:100; position:fixed; top:0; left:0; bottom:0; right:0; background-color:#356EF3;"></div>');
	$('#fondo').fadeTo('fast',0.3)
	$('#clausula').css({left:margen+'px', zIndex:'101'});
	$('#clausula').removeClass('oculto');
}

function cierraClausula ()
{
	$("#enviar").removeAttr('disabled');
	$("#acepto").attr('checked','checked');
	$('#fondo').remove();
	$('#clausula').addClass('oculto');
}
function getHTTPObject() 
{
	var xmlhttp;
	try{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   	}catch (e){
		try{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			xmlhttp = false;
		}
   }

   if (!xmlhttp && typeof XMLHttpRequest != 'undefined'){
		try{
			xmlhttp = new XMLHttpRequest();
      	} catch (e) {
			xmlhttp = false;
		}
   }
   if(!xmlhttp)
   		muestraAlerta("Error al inicializar XMLHttpRequest",tiempoAlerta1,1);

   return xmlhttp;
}


window.onload =  inicio;
