/**
 * Funcion para crear un nuevo objeto xmlhttp
 */
function newAJAX(){
	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;
}
/**
 * Funcion para ejecutar el script de envio del formulario y mostrar el mensaje
 */
function contacts_send( container ){

	var nombre, email, subject, message;

//Primero verificamos el email y el mensaje introducido
	document.sendMail.mail.style.background='#FFF'
	document.sendMail.text.style.background='#FFF'
	
  var error = 0;
	var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;

	container = document.getElementById( container );
	nombre = document.getElementById('name').value;
	email = document.getElementById('mail').value;
	subject = document.getElementById('subject').value;
	message = document.getElementById('text').value;

  if (!filter.test(document.sendMail.mail.value)){
     document.sendMail.mail.style.background='#FCC'
     error++;
  }
  if( message.length < 5 ){
     document.sendMail.text.style.background='#FCC'
     error++;
  }
  if(error > 0 ) return false;

	ajax=newAJAX();

	document.getElementById('loading').style.display='inline';

	ajax.open("POST", "includes/sendMail.php",true);
	ajax.onreadystatechange=function() {
		if (ajax.readyState==4) {
			container.style.display= 'block';
			container.innerHTML = ajax.responseText;
			document.getElementById('loading').style.display='none';
	 	}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send( 'name='+nombre+'&email='+email+'&subject='+subject+'&message='+message );

}