function $(id){
return document.getElementById(id);
}

function nuevoAjax(){
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;
}

function enviarMail(){
	
	//comprobamos que se han rellenado los campos obligarorios
	if($('nombre').value.length==0){
		$('errornombre').style.display='block';
		return false;
	}
	
	if($('email').value.length==0){
		$('erroremail').style.display='block';
		return false;
	}
	
	if($('mensaje').value.length==0){		
		$('errormensaje').style.display='block';
		return false;
	}
	
	c = document.getElementById('resultado_mensaje');
	
	//variable=documenet.nombre_del_form.nombre_del_control.value
	nom=document.enviar_email.nombre.value;
	emp=document.enviar_email.empresa.value;
	dir=document.enviar_email.direccion.value;
	pob=document.enviar_email.poblacion.value;
	pai=document.enviar_email.pais.value;
	tel=document.enviar_email.telefono.value;
	ema=document.enviar_email.email.value;
	asu=document.enviar_email.asunto.value;
	men=document.enviar_email.mensaje.value;
	
	ajax=nuevoAjax();
	c.innerHTML = '<p style="text-align:center;"><img src="img/esperando.gif"/></p>'; 
	ajax.open("POST", "phpmailer/envia_mail.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			c.innerHTML = ajax.responseText
		}
		borrarCampos()
	}
	ajax.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
	ajax.send("nombre="+nom+"&empresa="+emp+"&direccion="+dir+"&poblacion="+pob+"&pais="+pai+"&telefono="+tel+"&email="+ema+"&asunto="+asu+"&mensaje="+men)
}

function borrarCampos(){
	document.enviar_email.nombre.value="";
	document.enviar_email.empresa.value="";
	document.enviar_email.direccion.value="";
	document.enviar_email.poblacion.value="";
	document.enviar_email.pais.value="";
	document.enviar_email.telefono.value="";
	document.enviar_email.email.value="";
	document.enviar_email.asunto.value="";
	document.enviar_email.mensaje.value="";
	document.enviar_email.nombre.focus();
}