//Crea un objeto Ajax
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;
}

//Consulta1
function consulta1(datos,destino){
	divResultado1 = document.getElementById(destino);
	ajax=objetoAjax();
	ajax.open("GET", datos);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			divResultado1.innerHTML = ajax.responseText
		}
	}
	ajax.send(null)
}

function sendForm() {  
	seguridad = new Array;
	seguridad[0] = "26C91801F25CAAF4BF5DB26F6AD5802B";
	seguridad[1] = "BAA3894263BCEAD1E24604B6C4932B9D";
	seguridad[2] = "9962E199793DA0E1F53A93067268B5E0";
	
	var errores = "";
	if (document.getElementById('nombre').value==""){
		errores += "Falta el nombre.\n";
	}
	if (document.getElementById('apellidos').value==""){
		errores += "Faltan los apellidos.\n";
	}
	if (document.getElementById('dni').value==""){
		errores += "Falta el dni.\n";
	}
	if (document.getElementById('telefono').value==""){
		errores += "Falta el telefono.\n";
	}
	if (document.getElementById('personas').value==""){
		errores += "Falta el numero de personas.\n";
	}
	if (document.getElementById('tiendas').value==""){
		errores += "Falta el numero de tiendas.\n";
	}
	if (!((document.getElementById('email').value.indexOf(".") > 2) && (document.getElementById('email').value.indexOf("@") > 0))){
		errores += "Direccion de email no valida.\n";
	}
	if (calcMD5(document.getElementById('seguridad').value)!=seguridad[rand1]){
		errores += "Codigo de seguridad erroneo.\n";
	}
	if (errores!=""){
		imgSeguridad();
		document.getElementById('seguridad').value="";
		alert(errores);		
	} else {
		var nombre = document.getElementById('nombre').value;
		var apellidos = document.getElementById('apellidos').value;
		var dni = document.getElementById('dni').value;
		var telefono = document.getElementById('telefono').value;
		var email = document.getElementById('email').value;
		var personas = document.getElementById('personas').value;
		var tiendas = document.getElementById('tiendas').value;
		consulta1('http://getafeeling.com/_imaginafunk/formulario.php?email='+email+'&nombre='+nombre+'&apellidos='+apellidos+'&dni='+dni+'&telefono='+telefono+'&email='+email+'&personas='+personas+'&tiendas='+tiendas,'formulario');
	}
}

function imgSeguridad() {  
	var imagenumber = 3; 
	var randomnumber = Math.random(); 
	rand1 = Math.round( (imagenumber-1) * randomnumber); 

	images = new Array; 
	images[0] = "img/camping/codigo1.gif"; 
	images[1] = "img/camping/codigo2.gif"; 
	images[2] = "img/camping/codigo3.gif"; 
	document.getElementById('imgSeguridad').src = images[rand1];	
}
